Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:14:58

0001 // lAger: General Purpose l/A-event Generator
0002 // Copyright (C) 2016-2021 Sylvester Joosten <sjoosten@anl.gov>
0003 // 
0004 // This file is part of lAger.
0005 // 
0006 // lAger is free software: you can redistribute it and/or modify
0007 // it under the terms of the GNU General Public License as published by
0008 // the Free Shoftware Foundation, either version 3 of the License, or
0009 // (at your option) any later version.
0010 // 
0011 // lAger is distributed in the hope that it will be useful,
0012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 // GNU General Public License for more details.
0015 // 
0016 // You should have received a copy of the GNU General Public License
0017 // along with lAger.  If not, see <https://www.gnu.org/licenses/>.
0018 // 
0019 
0020 #ifndef LAGER_CORE_PROGRESS_METER_LOADED
0021 #define LAGER_CORE_PROGRESS_METER_LOADED
0022 
0023 #include <cmath>
0024 #include <cstdint>
0025 #include <iostream>
0026 #include <limits>
0027 
0028 // =============================================================================
0029 // A simple console progress meter
0030 // =============================================================================
0031 namespace lager {
0032 class progress_meter {
0033 public:
0034   constexpr static const size_t PRECISION = 10000.; // 0.01 percent
0035   explicit progress_meter(size_t max, size_t start_index = 0,
0036                           size_t precision = PRECISION)
0037       : max_{max}
0038       , index_{start_index}
0039       , precision_{precision > max ? max : precision} {
0040     std::cerr << "\nProcessing... " << std::endl;
0041     update();
0042   }
0043   void update(size_t i) {
0044     index_ = i;
0045     if (index_ > max_) {
0046       index_ = max_;
0047     }
0048     // update when needed
0049     // commented out because of FPE, need to fix this TODO
0050 //    if (!index_ || !(index_ % (max_ / precision_) || !(index_ % 1000))) {
0051     if (!(index_ % 100)) {
0052       const double cnt = (index_ > 50) ? (100. * index_) / max_ : 0.;
0053       char msg[15];
0054       sprintf(msg, "  %3.2f%%\r", cnt);
0055       std::cerr << msg << std::flush;
0056     }
0057   }
0058   void update(size_t i, const size_t max) {
0059     max_ = max;
0060     update(i);
0061   }
0062   void update() {
0063     index_ += 1;
0064     update(index_);
0065   }
0066   ~progress_meter() { std::cerr << "      Done!" << std::endl; }
0067 
0068 private:
0069   size_t max_;
0070   size_t index_;
0071   const size_t precision_;
0072 };
0073 } // ns lager
0074 
0075 #endif