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)
 
 Looper (long long nEvents)
 
 ~Looper ()
 
void operator++ ()
 
bool operator() ()
 
long long operator* () const
 
template<typename TTreePtr >
 Looper (const TTreePtr &t)
 
 Looper (long long nEvents)
 
 ~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)
 
 Looper (TTree *t, long long nEvents)
 

Private Attributes

TTree * tree
 
long long nEv
 
long long iEv
 
long long percent
 
std::chrono::time_point< std::chrono::system_clock > start_t
 

Constructor & Destructor Documentation

◆ Looper() [1/6]

Looper ( TTree *  t,
long long  nEvents 
)
inlineprivate
Parameters
tn-tuple
nEventstotal number of events to generate
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  }

◆ Looper() [2/6]

Looper ( const TTreePtr &  t)
inline

Constructor for existing tree with raw pointer.

Parameters
tn-tuple
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  }

◆ Looper() [3/6]

Looper ( long long  nEvents)
inline

Constructor for simple counter.

Parameters
nEventstotal number of events to generate
69  : Looper(nullptr, nEvents)
70  {
71  }

◆ ~Looper() [1/2]

~Looper ( )
inline
74  {
75  if (percent < 100ll)
76  std::cerr << red << "Warning: the event loop has stopped at entry "
77  << iEv << def << '\n';
78  }

◆ Looper() [4/6]

Looper ( TTree *  t,
long long  nEvents 
)
inlineprivate
Parameters
tn-tuple
nEventstotal number of events to generate
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  }

◆ Looper() [5/6]

Looper ( const TTreePtr &  t)
inline

Constructor for existing tree with raw pointer.

Parameters
tn-tuple
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  }

◆ Looper() [6/6]

Looper ( long long  nEvents)
inline

Constructor for simple counter.

Parameters
nEventstotal number of events to generate
69  : Looper(nullptr, nEvents)
70  {
71  }

◆ ~Looper() [2/2]

~Looper ( )
inline
74  {
75  if (percent < 100ll)
76  std::cerr << red << "Warning: the event loop has stopped at entry "
77  << iEv << def << '\n';
78  }

Member Function Documentation

◆ operator()() [1/2]

bool operator() ( )
inline

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

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  }

◆ operator()() [2/2]

bool operator() ( )
inline

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

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  }

◆ operator*() [1/2]

long long operator* ( ) const
inline

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

118  {
119  return iEv;
120  }

◆ operator*() [2/2]

long long operator* ( ) const
inline

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

118  {
119  return iEv;
120  }

◆ operator++() [1/2]

void operator++ ( )
inline

Increments the counter and load entry (if applicable)

83  {
84  ++iEv;
85  if (tree) tree->GetEntry(iEv);
86  }

◆ operator++() [2/2]

void operator++ ( )
inline

Increments the counter and load entry (if applicable)

83  {
84  ++iEv;
85  if (tree) tree->GetEntry(iEv);
86  }

Member Data Documentation

◆ division

static long long division = 10
static

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

◆ iEv

long long iEv
private

◆ nEv

long long nEv
private

◆ percent

long long percent
private

◆ start_t

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

starting time

◆ 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
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::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::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::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::percent
long long percent
Definition: Looper.h:27
Darwin::Tools::Looper::iEv
long long iEv
Definition: Looper.h:26