DAS  3.0
Das Analysis System
Looper.h
Go to the documentation of this file.
1 #ifndef DARWIN_LOOPER_H
2 #define DARWIN_LOOPER_H
3 
4 #include <algorithm>
5 #include <memory>
6 #include <iostream>
7 #include <string>
8 
9 #include <chrono>
10 #include <ctime>
11 
12 #include <boost/exception/all.hpp>
13 
14 #include <TChain.h>
15 #include <TTree.h>
16 
17 #include "exceptions.h"
18 
19 namespace Darwin::Tools {
20 
21 typedef std::pair<unsigned, unsigned> Slice;
22 
25 std::ostream& operator<< (std::ostream& Stream, const Darwin::Tools::Slice& slice)
26 {
27  Stream << slice.second << '/' << slice.first;
28  return Stream;
29 }
30 
33 class Looper {
34  TTree * tree;
35  const Slice slice;
36 
37  const long long nEvTot,
39  stop,
40  nEvSlice; // number of entries covered by the current process
41  long long iEvSlice; // current entry
42  long long percent; // percentage of progress for current entry
43 
44  const std::chrono::time_point<std::chrono::system_clock> start_t;
45 
46  Looper (TTree * t,
47  long long nEvents,
48  Slice s = {1,0}
49  ) : tree(t), slice(s),
50  nEvTot(nEvents),
51  start(nEvTot*((s.second+0.)/s.first)),
52  stop (nEvTot*((s.second+1.)/s.first)),
53  nEvSlice(stop - start), iEvSlice(0), percent(-1),
54  start_t(std::chrono::system_clock::now())
55  {
56  using namespace std;
57 
58  if (slice.first < slice.second)
59  BOOST_THROW_EXCEPTION(invalid_argument("The number of slices " + to_string(slice.first)
60  + " must be larger than the index of the current slice " + to_string(slice.second)));
61 
62  if (nEvSlice <= 0)
63  BOOST_THROW_EXCEPTION(invalid_argument("The number of events cannot be " + to_string(nEvSlice)));
64 
65  cerr << slice << '\t' << start << '-' << stop;
66 
67  time_t now = chrono::system_clock::to_time_t(start_t);
68  cerr << '\t' << ctime(&now);
69  }
70 
71 public:
72 
73  static long long division;
74 
77  template<typename TTreePtr>
78  Looper (const TTreePtr& t,
79  Slice s = {1,0}
80  ) : Looper(&*t, t->GetEntries(), s)
81  {
82  using namespace std;
83  namespace DE = Darwin::Exceptions;
84  string root_log = DE::intercept_printf([this]() { printf("loading first entry"); tree->GetEntry(start); });
85  tree->GetEntry(start);
86  if (root_log.find("Error") != string::npos)
87  BOOST_THROW_EXCEPTION(runtime_error("Error while loading a TTree entry:\n" + root_log));
88  }
89 
92  Looper (long long nEvents,
93  Slice s = {1,0}
94  ) : Looper(nullptr, nEvents, s)
95  {
96  }
97 
99  {
100  if (percent < 100ll)
101  std::cerr << red << "Warning: the event loop has stopped at entry "
102  << iEvSlice << def << '\n';
103  }
104 
107  void operator++ ()
108  {
109  ++iEvSlice;
110  if (tree) tree->GetEntry(start + iEvSlice);
111  }
112 
115  bool operator() ()
116  {
117  using namespace std;
118  long long test_percent = (100ll*iEvSlice)/nEvSlice;
119 
120  if (test_percent != percent && test_percent % division == 0) {
121  percent = test_percent;
122  cerr << slice.second << '/' << slice.first << '\t' << percent << '%';
123  auto now_t = chrono::system_clock::now();
124  auto elapsed_time {now_t - start_t};
125  time_t now = chrono::system_clock::to_time_t(now_t);
126  auto secs = chrono::duration_cast<chrono::seconds>(elapsed_time);
127  cerr << '\t' << secs.count() << "s\t" << ctime(&now);
128  }
129 
130  return iEvSlice < nEvSlice;
131  }
132 
135  inline const long long operator* () const
136  {
137  return start + iEvSlice;
138  }
139 };
140 
141 long long Looper::division = 10;
142 
143 } // end of Darwin::Tools namespace
144 
145 using Darwin::Tools::operator<<;
146 
147 #endif
148 
Ntupliser_cfg.cerr
cerr
Definition: Ntupliser_cfg.py:93
exceptions.h
Darwin::Tools::Looper::iEvSlice
long long iEvSlice
Definition: Looper.h:41
Step::def
static const char * def
Definition: Step.h:36
Darwin::Tools::Looper::nEvSlice
const long long nEvSlice
Definition: Looper.h:40
Darwin::Tools::Looper
Facility to loop over a n-tuple, including parallelisation and printing.
Definition: Looper.h:33
Darwin::Tools::Looper::nEvTot
const long long nEvTot
{#processes, process index}
Definition: Looper.h:37
Darwin::Tools::Slice
std::pair< unsigned, unsigned > Slice
current slice (>=0) / total number of slices (>0)
Definition: Looper.h:21
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:34
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:107
Darwin::Tools::Looper::Looper
Looper(const TTreePtr &t, Slice s={1, 0})
Constructor for existing tree with raw pointer.
Definition: Looper.h:78
Darwin::Tools::operator<<
std::ostream & operator<<(std::ostream &Stream, const Darwin::Tools::Slice &slice)
Prints the current slice and the total number of slices.
Definition: Looper.h:25
Darwin::Tools::Looper::start_t
const std::chrono::time_point< std::chrono::system_clock > start_t
starting time
Definition: Looper.h:44
Darwin::Tools::Looper::operator*
const long long operator*() const
Pointer-like operator to return the value of the entry.
Definition: Looper.h:135
Darwin::Tools::Looper::tree
TTree * tree
tree
Definition: Looper.h:34
Darwin::Tools
Classes and functions related to the framework.
Definition: forceMetaInfo.cc:28
Darwin::Tools::Looper::division
static long long division
steps at which progress is printed (100%/division)
Definition: Looper.h:73
Darwin::Tools::Looper::slice
const Slice slice
Definition: Looper.h:35
Darwin::Tools::Looper::Looper
Looper(long long nEvents, Slice s={1, 0})
Constructor for simple counter.
Definition: Looper.h:92
Darwin::Exceptions::intercept_printf
std::string intercept_printf(std::function< void()> const lambda=[]() { printf(__func__);})
Definition: exceptions.h:25
Darwin::Tools::Looper::start
const long long start
first entry (included)
Definition: Looper.h:38
Darwin::Tools::Looper::operator()
bool operator()()
Check that the counter is still in the range and prints percentage.
Definition: Looper.h:115
Darwin::Tools::Looper::stop
const long long stop
last entry (excluded)
Definition: Looper.h:39
Darwin::Tools::Looper::Looper
Looper(TTree *t, long long nEvents, Slice s={1, 0})
Definition: Looper.h:46
Darwin::Tools::Looper::percent
long long percent
Definition: Looper.h:42
Darwin::Tools::Looper::~Looper
~Looper()
Definition: Looper.h:98