Jpp  18.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTreeScanner.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JTREESCANNER__
2 #define __JSUPPORT__JTREESCANNER__
3 
4 #include <vector>
5 #include <string>
6 #include <ostream>
7 #include <iomanip>
8 #include <algorithm>
9 
10 #include "JLang/JNullType.hh"
11 #include "JLang/JPointer.hh"
12 #include "JLang/JConversion.hh"
13 #include "JLang/JEquals.hh"
14 #include "JROOT/JChainReader.hh"
15 #include "JROOT/JCounter.hh"
16 #include "JROOT/JRootSupportkit.hh"
17 #include "Jeep/JMessage.hh"
21 
22 
23 /**
24  * \author mdejong
25  */
26 
27 namespace JSUPPORT {}
28 namespace JPP { using namespace JSUPPORT; }
29 
30 namespace JSUPPORT {
31 
32  using JLANG::JPointer;
33  using JLANG::JEquals;
34  using JLANG::JNullType;
36  using JROOT::JChainReader;
37  using JROOT::counter_type;
38  using JEEP::JMessage;
39 
40 
41  /**
42  * Pattern match for names of sub-branches that will not be read when ordering elements in a TTree.
43  */
44  static const char* const BRANCH_PATTERN = "vector";
45 
46 
47  /**
48  * Base class for JTreeScanner.
49  *
50  * Following ROOT memory management, a JChainReader object is created
51  * at construction and not deleted at destruction of this object.
52  */
53  template<class JClass_t>
55  public JPointer< JChainReader<JClass_t> >
56  {
57  public:
58  /**
59  * Destructor.
60  */
62  {
63  if (this->is_valid()) {
64  this->get()->Reset();
65  }
66  }
67 
68 
69  /**
70  * Get file list.
71  *
72  * \return list of file names
73  */
75  {
76  if (this->is_valid())
77  return JMultipleFileScanner_t(*this->get());
78  else
79  return JMultipleFileScanner_t();
80  }
81 
82 
83  /**
84  * Type conversion.
85  *
86  * \return file list
87  */
88  operator JMultipleFileScanner_t() const
89  {
90  return getFilelist();
91  }
92 
93 
94  protected:
95  /**
96  * Default constructor.
97  */
99  JPointer< JChainReader<JClass_t> >(new JChainReader<JClass_t>())
100  {
101  gErrorIgnoreLevel = kError;
102  }
103  };
104 
105 
106  /**
107  * Template definition for direct access of elements in ROOT TChain.
108  *
109  * The optional second template argument is used to the determine the value of an element
110  * which defines the apparent order the elements in the TChain.
111  * It also provides for the mechanism to find a corresponding entry in the TChain.
112  *
113  * This class implements the JSUPPORT::JTreeScannerInterface interface.
114  */
115  template<class JClass_t = JNullType, class JEvaluator_t = JNullType>
117 
118 
119  /**
120  * Auxiliary base class for reporting messages.
121  */
122  template<>
124  public JMessage< JTreeScanner<> >
125  {
126  public:
128  };
129 
130 
131  /**
132  * Specialiation of class JTreeScanner for unordered direct access of elements in ROOT TChain.
133  *
134  * The method JROOT::actionAtFileOpen is called at opening of the first file.
135  *
136  * The iteration of elements in the TChain will be in order of appearance.
137  */
138  template<class JDerived_t, class JBase_t>
139  class JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JNullType> :
140  public virtual JTreeScannerInterface<JBase_t>, // interface
141  public JTreeScanner_t<JDerived_t>, // data source
142  public JTreeScanner<>, // messaging
143  public JEquals< JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JNullType> >
144  {
145  public:
146 
147  typedef JBase_t data_type;
149 
151 
152 
153  /**
154  * Default constructor.
155  */
157  counter(0)
158  {}
159 
160 
161  /**
162  * Constructor.
163  *
164  * \param file_list file list
165  * \param limit limit
166  */
167  JTreeScanner(const JMultipleFileScanner_t& file_list, const JLimit& limit = JLimit()) :
168  counter(0)
169  {
170  this->configure(file_list, limit);
171  }
172 
173 
174  /**
175  * Copy constructor.
176  *
177  * \param input TTree scanner
178  */
180  {
181  this->configure(input, JLimit());
182  }
183 
184 
185  /**
186  * Check equality.
187  *
188  * \param object TTree scanner
189  * \return true if TTree scanners are equal; else false
190  */
191  bool equals(const JTreeScanner& object) const
192  {
193  return (this->getLimit() == object.getLimit() &&
194  this->getFilelist() == object.getFilelist());
195  }
196 
197 
198  /**
199  * Rewind.
200  */
201  virtual void rewind() override
202  {
203  counter = 0;
204  }
205 
206 
207  /**
208  * Check availability of next element.
209  *
210  * \return true if the iteration has more elements.
211  */
212  virtual bool hasNext() override
213  {
214  if (counter < this->getLowerLimit()) {
215  skip(this->getLowerLimit() - counter);
216  }
217 
218  return (counter < this->getEntries() &&
219  counter < this->getUpperLimit());
220  }
221 
222 
223  /**
224  * Get next element.
225  *
226  * \return pointer to element.
227  */
228  virtual const pointer_type& next() override
229  {
230  if (this->hasNext()) {
231 
232  this->get()->GetEvent(counter++);
233 
234  ps.reset(this->get()->getAddress());
235 
236  } else {
237 
238  ps.reset(NULL);
239  }
240 
241  return ps;
242  }
243 
244 
245  /**
246  * Skip items.
247  *
248  * \param ns number of items to skip
249  * \return number of items skipped
250  */
251  virtual skip_type skip(const skip_type ns) override
252  {
253  return JROOT::advance(counter, ns, this->getEntries());
254  }
255 
256 
257  /**
258  * Configure.
259  *
260  * \param file_list file list
261  * \param limit limit
262  */
263  virtual void configure(const JMultipleFileScanner_t& file_list, const JLimit& limit) override
264  {
265  using namespace JROOT;
266 
267  this->setLimit(limit);
268 
269  this->rewind();
270 
271  this->get()->Reset();
272 
273  for (JMultipleFileScanner<>::const_iterator i = file_list.begin(); i != file_list.end(); ++i) {
274 
275  TFile* file = TFile::Open(i->c_str());
276 
277  if (file != NULL) {
278 
279  if (file->GetListOfKeys()->Contains(this->get()->getTreeName())) {
280  this->get()->Add(i->c_str());
281  }
282 
283  delete file;
284  }
285  }
286 
287  this->getEntries(); // load TTree to set internal file
288 
289  actionAtFileOpen<JDerived_t>(this->get()->GetCurrentFile());
290  actionAtFileOpen<JBase_t> (this->get()->GetCurrentFile());
291  }
292 
293 
294  /**
295  * Get number of entries.
296  *
297  * \return number of entries
298  */
299  virtual Long64_t getEntries() const override
300  {
301  return this->get()->GetEntries();
302  }
303 
304 
305  /**
306  * Get entry at given index.
307  *
308  * \param index index
309  * \return pointer to object (may be NULL)
310  */
311  virtual data_type* getEntry(Long64_t index) override
312  {
313  if (index >= 0 && index < this->getEntries()) {
314 
315  this->get()->GetEvent(index);
316 
317  return this->get()->getAddress();
318  }
319 
320  return NULL;
321  }
322 
323 
324  /**
325  * Get internal counter.
326  *
327  * \return counter
328  */
329  virtual counter_type getCounter() const override
330  {
331  return counter;
332  }
333 
334 
335  /**
336  * Get actual class name.
337  *
338  * \return class name
339  */
340  virtual const char* getClassName() const override
341  {
342  return JDerived_t::Class_Name();
343  }
344 
345  protected:
347  private:
350  };
351 
352 
353  /**
354  * Specialiation of class JTreeScanner for unordered direct access of elements in ROOT TChain.
355  *
356  * The iteration of elements in the TChain will be in order of appearance.
357  */
358  template<class JClass_t>
359  class JTreeScanner<JClass_t, JNullType> :
360  public JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>
361  {
362  public:
363  /**
364  * Default constructor.
365  */
367  {}
368 
369 
370  /**
371  * Constructor.
372  *
373  * \param file_list file list
374  * \param limit limit
375  */
376  JTreeScanner(const JMultipleFileScanner_t& file_list, const JLimit& limit = JLimit()) :
377  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>(file_list, limit)
378  {}
379 
380 
381  /**
382  * Copy constructor.
383  *
384  * \param input TTree scanner
385  */
386  JTreeScanner(const JTreeScanner& input) :
387  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>(input)
388  {}
389  };
390 
391 
392  /**
393  * Specialiation of class JTreeScanner for ordered direct access of elements in ROOT TChain.
394  *
395  * The optional second template argument is used to the determine the value of an element.
396  * The elements in the TChain are then ordered according these values (from low to high).
397  * To this end, the evaluator class should provide for the following operator:
398  * <pre>
399  * double operator()(const JBase_t& element) const;
400  * </pre>
401  * As a result, the iteration of elements in the TChain will then be in the specified order.
402  * It also provides for the mechanism to find a corresponding entry at O(log(n)) look up time.
403  */
404  template<class JDerived_t, class JBase_t, class JEvaluator_t>
405  class JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JEvaluator_t> :
406  public JTreeScannerInterface<JBase_t, JEvaluator_t>,
407  public JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JNullType>
408  {
409  /**
410  * Auxiliary data structure for sorting of objects in TChain.
411  */
412  struct JEntry_t {
413  /**
414  * Default constructor.
415  */
417  index(-1),
418  value(0.0)
419  {}
420 
421 
422  /**
423  * Constructor.
424  *
425  * \param __index index
426  * \param __value value
427  */
428  JEntry_t(const Long64_t __index,
429  const double __value) :
430  index(__index),
431  value(__value)
432  {}
433 
434 
435  /**
436  * Comparison between two TChain entries.
437  *
438  * \param first first entry
439  * \param second second entry
440  * \return true if first value less than second; else false
441  */
442  friend inline bool operator<(const JEntry_t& first, const JEntry_t& second)
443  {
444  return first.value < second.value;
445  }
446 
447 
448  /**
449  * Comparison between TChain entry and value.
450  *
451  * \param entry entry
452  * \param value value
453  * \return true if given entry has less value; else false
454  */
455  friend inline bool operator<(const JEntry_t& entry, const double value)
456  {
457  return entry.value < value;
458  }
459 
460 
461  Long64_t index; //!< index in TChain
462  double value; //!< corresponding value
463  };
464 
465 
466  /**
467  * Type definition of internal queue for ordering the elements in the TChain.
468  */
470 
471 
472  public:
473 
474  typedef JBase_t data_type;
476 
480  using JTreeScanner<>::debug;
481 
482 
483  /**
484  * Default constructor.
485  */
487  JTreeScannerInterface<JBase_t, JEvaluator_t>()
488  {}
489 
490 
491  /**
492  * Constructor.
493  *
494  * \param file_list file list
495  * \param evaluator evaluator
496  */
498  const JEvaluator_t& evaluator = JEvaluator_t()) :
499  JTreeScannerInterface<JBase_t, JEvaluator_t>(evaluator)
500  {
501  this->configure(file_list);
502  }
503 
504 
505  /**
506  * Constructor.
507  *
508  * \param file_list file list
509  * \param limit limit
510  * \param evaluator evaluator
511  */
513  const JLimit& limit,
514  const JEvaluator_t& evaluator = JEvaluator_t()) :
515  JTreeScannerInterface<JBase_t, JEvaluator_t>(evaluator)
516  {
517  this->configure(file_list, limit);
518  }
519 
520 
521  /**
522  * Get next element.
523  *
524  * \return pointer to element.
525  */
526  virtual const pointer_type& next() override
527  {
528  if (this->hasNext()) {
529 
530  this->get()->GetEvent(queue[this->counter++].index);
531 
532  ps.reset(this->get()->getAddress());
533 
534  } else {
535 
536  ps.reset(NULL);
537  }
538 
539  return ps;
540  }
541 
542 
543  /**
544  * Configure.
545  *
546  * \param file_list file list
547  * \param limit limit
548  */
549  virtual void configure(const JMultipleFileScanner_t& file_list, const JLimit& limit) override
550  {
551  using namespace std;
552 
554 
555  setBranchStatus(this->get()->GetBranch(this->get()->getBranchName()), BRANCH_PATTERN, false);
556 
557  queue.resize(this->getEntries());
558 
559  typename queue_type::iterator out = queue.begin();
560 
561  for (Long64_t i = 0, n0 = 0; i != this->getEntries(); ++i, ++out) {
562 
563  const Long64_t n1 = (100 * (i + 1)) / this->getEntries();
564 
565  if (n1 > n0) {
566 
567  STATUS(left << setw(24) << this->get()->GetName() << right << ' ' << setw(3) << n1 << "%\r"); DEBUG(endl);
568 
569  n0 = n1;
570  }
571 
572  this->get()->GetEvent(i);
573 
574  const data_type* p = this->get()->getAddress();
575 
576  *out = JEntry_t(i, this->JTreeScannerInterface<JBase_t, JEvaluator_t>::getValue(*p));
577  }
578  STATUS(left << setw(24) << this->get()->GetName() << right << endl);
579 
580  this->get()->SetBranchStatus("*", 1);
581 
582  sort(queue.begin(), queue.end());
583  }
584 
585 
586  /**
587  * Get entry at given index.
588  *
589  * \param index index
590  * \return pointer to object (may be NULL)
591  */
592  virtual data_type* getEntry(Long64_t index) override
593  {
594  if (index >= 0 && index < (Long64_t) queue.size()) {
595 
596  this->get()->GetEvent(queue[index].index);
597 
598  return this->get()->getAddress();
599  }
600 
601  return NULL;
602  }
603 
604 
605  /**
606  * Find index of element that is closest in value to given value.
607  *
608  * A subsequent call to method getEntry() will point to the corresponding object.
609  *
610  * \param value value
611  * \return index (-1 in case of error)
612  */
613  virtual Long64_t find(const double value) const override
614  {
615  using namespace std;
616 
617  if (!queue.empty()) {
618 
619  typename queue_type::const_iterator p = lower_bound(queue.begin(), queue.end(), value);
620 
621  if (p == queue.end()) {
622 
623  return queue.size() - 1;
624 
625  } else if (p == queue.begin()) {
626 
627  return 0;
628 
629  } else {
630 
631  typename queue_type::const_iterator q = p--;
632 
633  if (value - p->value < q->value - value)
634  return distance(queue.begin(), p);
635  else
636  return distance(queue.begin(), q);
637  }
638  }
639 
640  return -1;
641  }
642 
643  protected:
644  /**
645  * Set status of branch.
646  *
647  * Note that the status of the branch and all sub-branches with names including given pattern will recursively be set.
648  *
649  * \param branch pointer to branch
650  * \param pattern pattern
651  * \param status status
652  */
653  static void setBranchStatus(TBranch* branch, const char* pattern, const bool status)
654  {
655  using namespace std;
656 
657  if (branch != NULL) {
658 
659  TObjArray* array = branch->GetListOfBranches();
660 
661  for (Int_t i = 0; i != array->GetEntries(); ++i) {
662 
663  TBranch* p = (TBranch*) array->At(i);
664 
665  if (p != NULL && string(p->GetName()).find(pattern) != string::npos) {
666 
667  if (p->GetSplitLevel() == 0) {
668 
669  NOTICE("Set status of branch " << p->GetName() << " to " << status << endl);
670 
671  p->SetStatus(status);
672  }
673  } else {
674 
675  setBranchStatus(p, pattern, status);
676  }
677  }
678  }
679  }
680 
681 
683 
684  private:
686  };
687 
688 
689  /**
690  * Specialiation of class JTreeScanner for ordered direct access of elements in ROOT TChain.
691  */
692  template<class JClass_t, class JEvaluator_t>
693  class JTreeScanner :
694  public JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>
695  {
696  public:
697  /**
698  * Default constructor.
699  */
701  {}
702 
703 
704  /**
705  * Constructor.
706  *
707  * \param file_list file list
708  * \param evaluator evaluator
709  */
711  const JEvaluator_t& evaluator = JEvaluator_t()) :
712  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>(file_list, evaluator)
713  {}
714 
715 
716  /**
717  * Constructor.
718  *
719  * \param file_list file list
720  * \param limit limit
721  * \param evaluator evaluator
722  */
724  const JLimit& limit,
725  const JEvaluator_t& evaluator = JEvaluator_t()) :
726  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>(file_list, limit, evaluator)
727  {}
728  };
729 }
730 
731 #endif
virtual const pointer_type & next() override
Get next element.
virtual void configure(const JMultipleFileScanner_t &file_list, const JLimit &limit) override
Configure.
virtual Long64_t find(const double value) const override
Find index of element that is closest in value to given value.
then usage $script[< detector identifier >< run range >]< QA/QCfile > nExample script to produce data quality plots nWhen a detector identifier and run range are data are downloaded from the database nand subsequently stored in the given QA QC file
Definition: JDataQuality.sh:19
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit, const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
virtual bool hasNext() override
Check availability of next element.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
#define STATUS(A)
Definition: JMessage.hh:63
unsigned int skip_type
Type definition for number of objects to skip.
JTreeScanner(const JMultipleFileScanner_t &file_list, const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
std::vector< size_t > ns
static void setBranchStatus(TBranch *branch, const char *pattern, const bool status)
Set status of branch.
Auxiliary interface for direct access of elements in ROOT TChain.
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit, const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Type definition for counter for ROOT TTree and auxiliary methods.
JTreeScanner(const JMultipleFileScanner_t &file_list, const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Auxialiary class to assert type conversion.
Definition: JConversion.hh:43
Long64_t counter_type
Type definition for counter.
friend bool operator<(const JEntry_t &entry, const double value)
Comparison between TChain entry and value.
bool is_valid() const
Check validity of pointer.
Template definition for direct access of elements in ROOT TChain.
virtual skip_type skip(const skip_type ns) override
Skip items.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Auxiliary class for template TChain reading.
Definition: JChainReader.hh:24
virtual data_type * getEntry(Long64_t index) override
Get entry at given index.
virtual void configure(const JMultipleFileScanner_t &file_list, const JLimit &limit) override
Configure.
bool equals(const JTreeScanner &object) const
Check equality.
virtual Long64_t getEntries() const override
Get number of entries.
Scanning of objects from a single file according a format that follows from the extension of each fil...
virtual data_type * getEntry(Long64_t index) override
Get entry at given index.
friend bool operator<(const JEntry_t &first, const JEntry_t &second)
Comparison between two TChain entries.
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JManip.hh:222
virtual const pointer_type & next() override
Get next element.
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
JTreeScannerInterface< JBase_t, JEvaluator_t >::pointer_type pointer_type
TChain reading for template data type.
JTreeScanner()
Default constructor.
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
std::vector< JEntry_t > queue_type
Type definition of internal queue for ordering the elements in the TChain.
Auxiliary class for no type definition.
Definition: JNullType.hh:19
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
Data type dependent action methods for customised ROOT version management.
#define NOTICE(A)
Definition: JMessage.hh:64
virtual counter_type getCounter() const override
Get internal counter.
JMultipleFileScanner_t getFilelist() const
Get file list.
Definition: JTreeScanner.hh:74
~JTreeScanner_t()
Destructor.
Definition: JTreeScanner.hh:61
virtual const char * getClassName() const override
Get actual class name.
General purpose messaging.
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
static const char *const BRANCH_PATTERN
Pattern match for names of sub-branches that will not be read when ordering elements in a TTree...
Definition: JTreeScanner.hh:44
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Base class for JTreeScanner.
Definition: JTreeScanner.hh:54
Auxiliary base class for list of file names.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
General purpose class for object reading from a list of file names.
then echo Submitting reweighting and histogram comparison jobs to nikhef stbc batch queue
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit=JLimit())
Constructor.
JTreeScanner(const JTreeScanner &input)
Copy constructor.
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
Definition: JMathToolkit.hh:86
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit=JLimit())
Constructor.
int debug
debug level
JTreeScanner_t()
Default constructor.
Definition: JTreeScanner.hh:98
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Auxiliary class for handling debug parameter within a class.
Definition: JMessage.hh:44