DAS  3.0
Das Analysis System
Looper.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <memory>
5 #include <iostream>
6 #include <string>
7 
8 #include <chrono>
9 #include <ctime>
10 
11 #include <boost/exception/all.hpp>
12 
13 #include <TChain.h>
14 #include <TTree.h>
15 
16 #include "exceptions.h"
17 
18 namespace Darwin::Tools {
19 
22 class Looper {
23  TTree * tree;
24 
25  long long nEv, // number of entries covered by the current process
26  iEv, // current entry
27  percent; // percentage of progress for current entry
28 
29  std::chrono::time_point<std::chrono::system_clock> start_t;
30 
31  Looper (TTree * t,
32  long long nEvents
33  ) : tree(t)
34  , nEv(nEvents)
35  , iEv(0)
36  , percent(-1)
37  , start_t(std::chrono::system_clock::now())
38  {
39  using namespace std;
40 
41  if (nEv <= 0)
42  BOOST_THROW_EXCEPTION(
43  invalid_argument("The number of events must be nonnegative"));
44  }
45 
46 public:
47 
48  static long long division;
49 
52  template<typename TTreePtr>
53  Looper (const TTreePtr& t
54  ) : Looper(&*t, t->GetEntries())
55  {
56  using namespace std;
57  namespace DE = Darwin::Exceptions;
58  string root_log = DE::intercept_printf([this]() {
59  printf("loading first entry");
60  tree->GetEntry(0);
61  });
62  if (root_log.find("Error") != string::npos)
63  BOOST_THROW_EXCEPTION(runtime_error("Error while loading a TTree entry:\n" + root_log));
64  }
65 
68  Looper (long long nEvents
69  ) : Looper(nullptr, nEvents)
70  {
71  }
72 
74  {
75  if (percent < 100ll)
76  std::cerr << red << "Warning: the event loop has stopped at entry "
77  << iEv << def << '\n';
78  }
79 
82  void operator++ ()
83  {
84  ++iEv;
85  if (tree) tree->GetEntry(iEv);
86  }
87 
90  bool operator() ()
91  {
92  using namespace std;
93  long long test_percent = (100ll*iEv)/nEv;
94 
95  if (test_percent != percent && test_percent % division == 0) {
96 
97  // display total number of seconds
98  auto now_t = chrono::system_clock::now();
99  auto elapsed_time {now_t - start_t};
100  auto secs = chrono::duration_cast<chrono::seconds>(elapsed_time);
101  cout << secs.count() << 's';
102 
103  // display current time in human readable format
104  time_t now = chrono::system_clock::to_time_t(now_t);
105  cout << '\t' << put_time(localtime(&now), "%Y-%m-%d %H:%M:%S %Z");
106 
107  // display progress
108  percent = test_percent;
109  cout << '\t' << percent << '%' << endl;
110  }
111 
112  return iEv < nEv;
113  }
114 
117  inline long long operator* () const
118  {
119  return iEv;
120  }
121 };
122 
123 } // end of Darwin::Tools namespace
Ntupliser_cfg.cerr
cerr
Definition: Ntupliser_cfg.py:93
exceptions.h
Step::def
static const char * def
Definition: Step.h:36
Darwin::Tools::Looper::start_t
std::chrono::time_point< std::chrono::system_clock > start_t
starting time
Definition: Looper.h:29
Darwin::Tools::Looper
Facility to loop over a n-tuple, including parallelisation and printing.
Definition: Looper.h:22
Darwin::Tools::Looper::nEv
long long nEv
Definition: Looper.h:25
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:36
Step::red
static const char * red
Definition: Step.h:34
Darwin::Tools::Looper::operator++
void operator++()
Increments the counter and load entry (if applicable)
Definition: Looper.h:82
Darwin::Tools::Looper::operator*
long long operator*() const
Pointer-like operator to return the value of the entry.
Definition: Looper.h:117
Darwin::Tools::Looper::division
static long long division
steps at which progress is printed (100%/division)
Definition: Looper.h:48
Darwin::Tools::Looper::tree
TTree * tree
tree
Definition: Looper.h:23
Darwin::Tools
Classes and functions related to the framework.
Definition: Dict_rdict.cxx:990
Darwin::Tools::Looper::Looper
Looper(const TTreePtr &t)
Constructor for existing tree with raw pointer.
Definition: Looper.h:53
Darwin::Exceptions::intercept_printf
std::string intercept_printf(std::function< void()> const lambda=[]() { printf(__func__);})
Definition: exceptions.h:33
Darwin::Tools::Looper::Looper
Looper(TTree *t, long long nEvents)
Definition: Looper.h:31
Darwin::Tools::Looper::operator()
bool operator()()
Check that the counter is still in the range and prints percentage.
Definition: Looper.h:90
Darwin::Tools::Looper::Looper
Looper(long long nEvents)
Constructor for simple counter.
Definition: Looper.h:68
Darwin::Tools::Looper::percent
long long percent
Definition: Looper.h:27
Darwin::Tools::Looper::iEv
long long iEv
Definition: Looper.h:26
Darwin::Tools::Looper::~Looper
~Looper()
Definition: Looper.h:73