DAS  3.0
Das Analysis System
Looper

Description

Facility to loop over a n-tuple, including parallelisation and printing.

#include <Looper.h>

Public Member Functions

template<typename TTreePtr >
 Looper (const TTreePtr &t, Slice s={1, 0})
 
 Looper (long long nEvents, Slice s={1, 0})
 
 ~Looper ()
 
void operator++ ()
 
bool operator() ()
 
const long long operator* () const
 
template<typename TTreePtr >
 Looper (const TTreePtr &t, Slice s={1, 0})
 
 Looper (long long nEvents, Slice s={1, 0})
 
 ~Looper ()
 
void operator++ ()
 
bool operator() ()
 
const long long operator* () const
 

Static Public Attributes

static long long division = 10
 

Private Member Functions

 Looper (TTree *t, long long nEvents, Slice s={1, 0})
 
 Looper (TTree *t, long long nEvents, Slice s={1, 0})
 

Private Attributes

TTree * tree
 
const Slice slice
 
const long long nEvTot
 
const long long start
 
const long long stop
 
const long long nEvSlice
 
long long iEvSlice
 
long long percent
 
const std::chrono::time_point< std::chrono::system_clock > start_t
 

Constructor & Destructor Documentation

◆ Looper() [1/6]

Looper ( TTree *  t,
long long  nEvents,
Slice  s = {1,0} 
)
inlineprivate
Parameters
tn-tuple
nEventstotal number of events to generate (with all slices)
sexactly like the output of `Options::slice()`
48  {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  }

◆ Looper() [2/6]

Looper ( const TTreePtr &  t,
Slice  s = {1,0} 
)
inline

Constructor for existing tree with raw pointer.

Parameters
tn-tuple
sexactly like the output of `Options::slice()`
79  {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  }

◆ Looper() [3/6]

Looper ( long long  nEvents,
Slice  s = {1,0} 
)
inline

Constructor for simple counter.

Parameters
nEventstotal number of events to generate (with all slices)
sexactly like the output of `Options::slice()`
93  {1,0}
94  ) : Looper(nullptr, nEvents, s)
95  {
96  }

◆ ~Looper() [1/2]

~Looper ( )
inline
99  {
100  if (percent < 100ll)
101  std::cerr << red << "Warning: the event loop has stopped at entry "
102  << iEvSlice << def << '\n';
103  }

◆ Looper() [4/6]

Looper ( TTree *  t,
long long  nEvents,
Slice  s = {1,0} 
)
inlineprivate
Parameters
tn-tuple
nEventstotal number of events to generate (with all slices)
sexactly like the output of `Options::slice()`
48  {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  }

◆ Looper() [5/6]

Looper ( const TTreePtr &  t,
Slice  s = {1,0} 
)
inline

Constructor for existing tree with raw pointer.

Parameters
tn-tuple
sexactly like the output of `Options::slice()`
79  {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  }

◆ Looper() [6/6]

Looper ( long long  nEvents,
Slice  s = {1,0} 
)
inline

Constructor for simple counter.

Parameters
nEventstotal number of events to generate (with all slices)
sexactly like the output of `Options::slice()`
93  {1,0}
94  ) : Looper(nullptr, nEvents, s)
95  {
96  }

◆ ~Looper() [2/2]

~Looper ( )
inline
99  {
100  if (percent < 100ll)
101  std::cerr << red << "Warning: the event loop has stopped at entry "
102  << iEvSlice << def << '\n';
103  }

Member Function Documentation

◆ operator()() [1/2]

bool operator() ( )
inline

Check that the counter is still in the range and prints percentage.

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  }

◆ operator()() [2/2]

bool operator() ( )
inline

Check that the counter is still in the range and prints percentage.

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  }

◆ operator*() [1/2]

const long long operator* ( ) const
inline

Pointer-like operator to return the value of the entry.

136  {
137  return start + iEvSlice;
138  }

◆ operator*() [2/2]

const long long operator* ( ) const
inline

Pointer-like operator to return the value of the entry.

136  {
137  return start + iEvSlice;
138  }

◆ operator++() [1/2]

void operator++ ( )
inline

Increments the counter and load entry (if applicable)

108  {
109  ++iEvSlice;
110  if (tree) tree->GetEntry(start + iEvSlice);
111  }

◆ operator++() [2/2]

void operator++ ( )
inline

Increments the counter and load entry (if applicable)

108  {
109  ++iEvSlice;
110  if (tree) tree->GetEntry(start + iEvSlice);
111  }

Member Data Documentation

◆ division

long long division = 10
static

steps at which progress is printed (100%/division)

◆ iEvSlice

long long iEvSlice
private

◆ nEvSlice

const long long nEvSlice
private

◆ nEvTot

const long long nEvTot
private

{#processes, process index}

total number of entries in tree

◆ percent

long long percent
private

◆ slice

const Slice slice
private

◆ start

const long long start
private

first entry (included)

◆ start_t

const std::chrono::time_point< std::chrono::system_clock > start_t
private

starting time

◆ stop

const long long stop
private

last entry (excluded)

◆ tree

TTree * tree
private

tree


The documentation for this class was generated from the following file:
Ntupliser_cfg.cerr
cerr
Definition: Ntupliser_cfg.py:93
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::nEvTot
const long long nEvTot
{#processes, process index}
Definition: Looper.h:37
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:34
Step::red
static const char * red
Definition: Step.h:34
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::tree
TTree * tree
tree
Definition: Looper.h:34
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::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::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