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)
 
template<typename TTreePtr >
 Looper (const TTreePtr &t)
 
 Looper (long long nEvents, Slice s={1, 0})
 
 ~Looper ()
 
void operator++ ()
 
bool operator() ()
 
long long operator* () const
 
template<typename TTreePtr >
 Looper (const TTreePtr &t, Slice s)
 
template<typename TTreePtr >
 Looper (const TTreePtr &t)
 
 Looper (long long nEvents, Slice s={1, 0})
 
 ~Looper ()
 
void operator++ ()
 
bool operator() ()
 
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/8]

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()`
47  {1,0}
48  ) : tree(t), slice(s),
49  nEvTot(nEvents),
50  start(nEvTot*((s.second+0.)/s.first)),
51  stop (nEvTot*((s.second+1.)/s.first)),
52  nEvSlice(stop - start), iEvSlice(0), percent(-1),
53  start_t(std::chrono::system_clock::now())
54  {
55  using namespace std;
56 
57  if (slice.first < slice.second)
58  BOOST_THROW_EXCEPTION(invalid_argument("The number of slices " + to_string(slice.first)
59  + " must be larger than the index of the current slice " + to_string(slice.second)));
60 
61  if (nEvSlice <= 0)
62  BOOST_THROW_EXCEPTION(invalid_argument("The number of events cannot be " + to_string(nEvSlice)));
63 
64  cout << slice << '\t' << start << '-' << stop << endl;
65  }

◆ Looper() [2/8]

Looper ( const TTreePtr &  t,
Slice  s 
)
inline

Constructor for existing tree with raw pointer.

Parameters
tn-tuple
sexactly like the output of `Options::slice()`
77  : Looper(&*t, t->GetEntries(), s)
78  {
79  using namespace std;
80  namespace DE = Darwin::Exceptions;
81  string root_log = DE::intercept_printf([this]() { printf("loading first entry"); tree->GetEntry(start); });
82  tree->GetEntry(start);
83  if (root_log.find("Error") != string::npos)
84  BOOST_THROW_EXCEPTION(runtime_error("Error while loading a TTree entry:\n" + root_log));
85  }

◆ Looper() [3/8]

Looper ( const TTreePtr &  t)
inline

Constructor for existing tree with raw pointer.

Parameters
tn-tuple
91  : Looper(&*t, t->GetEntries(), {1, 0})
92  {
93  using namespace std;
94  namespace DE = Darwin::Exceptions;
95  string root_log = DE::intercept_printf([this]() { printf("loading first entry"); tree->GetEntry(start); });
96  tree->GetEntry(start);
97  if (root_log.find("Error") != string::npos)
98  BOOST_THROW_EXCEPTION(runtime_error("Error while loading a TTree entry:\n" + root_log));
99  }

◆ Looper() [4/8]

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()`
104  {1,0}
105  ) : Looper(nullptr, nEvents, s)
106  {
107  }

◆ ~Looper() [1/2]

~Looper ( )
inline
110  {
111  if (percent < 100ll)
112  std::cerr << red << "Warning: the event loop has stopped at entry "
113  << iEvSlice << def << '\n';
114  }

◆ Looper() [5/8]

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()`
47  {1,0}
48  ) : tree(t), slice(s),
49  nEvTot(nEvents),
50  start(nEvTot*((s.second+0.)/s.first)),
51  stop (nEvTot*((s.second+1.)/s.first)),
52  nEvSlice(stop - start), iEvSlice(0), percent(-1),
53  start_t(std::chrono::system_clock::now())
54  {
55  using namespace std;
56 
57  if (slice.first < slice.second)
58  BOOST_THROW_EXCEPTION(invalid_argument("The number of slices " + to_string(slice.first)
59  + " must be larger than the index of the current slice " + to_string(slice.second)));
60 
61  if (nEvSlice <= 0)
62  BOOST_THROW_EXCEPTION(invalid_argument("The number of events cannot be " + to_string(nEvSlice)));
63 
64  cout << slice << '\t' << start << '-' << stop << endl;
65  }

◆ Looper() [6/8]

Looper ( const TTreePtr &  t,
Slice  s 
)
inline

Constructor for existing tree with raw pointer.

Parameters
tn-tuple
sexactly like the output of `Options::slice()`
77  : Looper(&*t, t->GetEntries(), s)
78  {
79  using namespace std;
80  namespace DE = Darwin::Exceptions;
81  string root_log = DE::intercept_printf([this]() { printf("loading first entry"); tree->GetEntry(start); });
82  tree->GetEntry(start);
83  if (root_log.find("Error") != string::npos)
84  BOOST_THROW_EXCEPTION(runtime_error("Error while loading a TTree entry:\n" + root_log));
85  }

◆ Looper() [7/8]

Looper ( const TTreePtr &  t)
inline

Constructor for existing tree with raw pointer.

Parameters
tn-tuple
91  : Looper(&*t, t->GetEntries(), {1, 0})
92  {
93  using namespace std;
94  namespace DE = Darwin::Exceptions;
95  string root_log = DE::intercept_printf([this]() { printf("loading first entry"); tree->GetEntry(start); });
96  tree->GetEntry(start);
97  if (root_log.find("Error") != string::npos)
98  BOOST_THROW_EXCEPTION(runtime_error("Error while loading a TTree entry:\n" + root_log));
99  }

◆ Looper() [8/8]

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()`
104  {1,0}
105  ) : Looper(nullptr, nEvents, s)
106  {
107  }

◆ ~Looper() [2/2]

~Looper ( )
inline
110  {
111  if (percent < 100ll)
112  std::cerr << red << "Warning: the event loop has stopped at entry "
113  << iEvSlice << def << '\n';
114  }

Member Function Documentation

◆ operator()() [1/2]

bool operator() ( )
inline

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

127  {
128  using namespace std;
129  long long test_percent = (100ll*iEvSlice)/nEvSlice;
130 
131  if (test_percent != percent && test_percent % division == 0) {
132  // display current slice
133  cout << slice.second << '/' << slice.first;
134 
135  // display total number of seconds
136  auto now_t = chrono::system_clock::now();
137  auto elapsed_time {now_t - start_t};
138  auto secs = chrono::duration_cast<chrono::seconds>(elapsed_time);
139  cout << '\t' << secs.count() << 's';
140 
141  // display current time in human readable format
142  time_t now = chrono::system_clock::to_time_t(now_t);
143  cout << '\t' << put_time(localtime(&now), "%Y-%m-%d %H:%M:%S %Z");
144 
145  // display progress
146  percent = test_percent;
147  cout << '\t' << percent << '%' << endl;
148  }
149 
150  return iEvSlice < nEvSlice;
151  }

◆ operator()() [2/2]

bool operator() ( )
inline

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

127  {
128  using namespace std;
129  long long test_percent = (100ll*iEvSlice)/nEvSlice;
130 
131  if (test_percent != percent && test_percent % division == 0) {
132  // display current slice
133  cout << slice.second << '/' << slice.first;
134 
135  // display total number of seconds
136  auto now_t = chrono::system_clock::now();
137  auto elapsed_time {now_t - start_t};
138  auto secs = chrono::duration_cast<chrono::seconds>(elapsed_time);
139  cout << '\t' << secs.count() << 's';
140 
141  // display current time in human readable format
142  time_t now = chrono::system_clock::to_time_t(now_t);
143  cout << '\t' << put_time(localtime(&now), "%Y-%m-%d %H:%M:%S %Z");
144 
145  // display progress
146  percent = test_percent;
147  cout << '\t' << percent << '%' << endl;
148  }
149 
150  return iEvSlice < nEvSlice;
151  }

◆ operator*() [1/2]

long long operator* ( ) const
inline

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

156  {
157  return start + iEvSlice;
158  }

◆ operator*() [2/2]

long long operator* ( ) const
inline

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

156  {
157  return start + iEvSlice;
158  }

◆ operator++() [1/2]

void operator++ ( )
inline

Increments the counter and load entry (if applicable)

119  {
120  ++iEvSlice;
121  if (tree) tree->GetEntry(start + iEvSlice);
122  }

◆ operator++() [2/2]

void operator++ ( )
inline

Increments the counter and load entry (if applicable)

119  {
120  ++iEvSlice;
121  if (tree) tree->GetEntry(start + iEvSlice);
122  }

Member Data Documentation

◆ division

static 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 files:
Ntupliser_cfg.cerr
cerr
Definition: Ntupliser_cfg.py:93
Darwin::Tools::Looper::iEvSlice
long long iEvSlice
Definition: Looper.h:40
Step::def
static const char * def
Definition: Step.h:36
Darwin::Tools::Looper::nEvSlice
const long long nEvSlice
Definition: Looper.h:39
Darwin::Tools::Looper::nEvTot
const long long nEvTot
{#processes, process index}
Definition: Looper.h:36
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:35
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:43
Darwin::Tools::Looper::division
static long long division
steps at which progress is printed (100%/division)
Definition: Looper.h:69
Darwin::Tools::Looper::tree
TTree * tree
tree
Definition: Looper.h:33
Darwin::Tools::Looper::slice
const Slice slice
Definition: Looper.h:34
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:37
Darwin::Tools::Looper::stop
const long long stop
last entry (excluded)
Definition: Looper.h:38
Darwin::Tools::Looper::Looper
Looper(TTree *t, long long nEvents, Slice s={1, 0})
Definition: Looper.h:45
Darwin::Tools::Looper::percent
long long percent
Definition: Looper.h:41