Jpp  17.0.0-rc.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  {
127  };
128 
129 
130  /**
131  * Specialiation of class JTreeScanner for unordered direct access of elements in ROOT TChain.
132  *
133  * The method JROOT::actionAtFileOpen is called at opening of the first file.
134  *
135  * The iteration of elements in the TChain will be in order of appearance.
136  */
137  template<class JDerived_t, class JBase_t>
138  class JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JNullType> :
139  public virtual JTreeScannerInterface<JBase_t>, // interface
140  public JTreeScanner_t<JDerived_t>, // data source
141  public JTreeScanner<>, // messaging
142  public JEquals< JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JNullType> >
143  {
144  public:
145 
146  typedef JBase_t data_type;
148 
150 
151 
152  /**
153  * Default constructor.
154  */
156  counter(0)
157  {}
158 
159 
160  /**
161  * Constructor.
162  *
163  * \param file_list file list
164  * \param limit limit
165  */
166  JTreeScanner(const JMultipleFileScanner_t& file_list, const JLimit& limit = JLimit()) :
167  counter(0)
168  {
169  this->configure(file_list, limit);
170  }
171 
172 
173  /**
174  * Copy constructor.
175  *
176  * \param input TTree scanner
177  */
179  {
180  this->configure(input, JLimit());
181  }
182 
183 
184  /**
185  * Check equality.
186  *
187  * \param object TTree scanner
188  * \return true if TTree scanners are equal; else false
189  */
190  bool equals(const JTreeScanner& object) const
191  {
192  return (this->getLimit() == object.getLimit() &&
193  this->getFilelist() == object.getFilelist());
194  }
195 
196 
197  /**
198  * Rewind.
199  */
200  virtual void rewind() override
201  {
202  counter = 0;
203  }
204 
205 
206  /**
207  * Check availability of next element.
208  *
209  * \return true if the iteration has more elements.
210  */
211  virtual bool hasNext() override
212  {
213  if (counter < this->getLowerLimit()) {
214  skip(this->getLowerLimit() - counter);
215  }
216 
217  return (counter < this->getEntries() &&
218  counter < this->getUpperLimit());
219  }
220 
221 
222  /**
223  * Get next element.
224  *
225  * \return pointer to element.
226  */
227  virtual const pointer_type& next() override
228  {
229  if (this->hasNext()) {
230 
231  this->get()->GetEvent(counter++);
232 
233  ps.reset(this->get()->getAddress());
234 
235  } else {
236 
237  ps.reset(NULL);
238  }
239 
240  return ps;
241  }
242 
243 
244  /**
245  * Skip items.
246  *
247  * \param ns number of items to skip
248  * \return number of items skipped
249  */
250  virtual skip_type skip(const skip_type ns) override
251  {
252  return JROOT::advance(counter, ns, this->getEntries());
253  }
254 
255 
256  /**
257  * Configure.
258  *
259  * \param file_list file list
260  * \param limit limit
261  */
262  virtual void configure(const JMultipleFileScanner_t& file_list, const JLimit& limit) override
263  {
264  using namespace JROOT;
265 
266  this->setLimit(limit);
267 
268  this->rewind();
269 
270  this->get()->Reset();
271 
272  for (JMultipleFileScanner<>::const_iterator i = file_list.begin(); i != file_list.end(); ++i) {
273 
274  TFile* file = TFile::Open(i->c_str());
275 
276  if (file != NULL) {
277 
278  if (file->GetListOfKeys()->Contains(this->get()->getTreeName())) {
279  this->get()->Add(i->c_str());
280  }
281 
282  delete file;
283  }
284  }
285 
286  this->getEntries(); // load TTree to set internal file
287 
288  actionAtFileOpen<JDerived_t>(this->get()->GetCurrentFile());
289  actionAtFileOpen<JBase_t> (this->get()->GetCurrentFile());
290  }
291 
292 
293  /**
294  * Get number of entries.
295  *
296  * \return number of entries
297  */
298  virtual Long64_t getEntries() const override
299  {
300  return this->get()->GetEntries();
301  }
302 
303 
304  /**
305  * Get entry at given index.
306  *
307  * \param index index
308  * \return pointer to object (may be NULL)
309  */
310  virtual data_type* getEntry(Long64_t index) override
311  {
312  if (index >= 0 && index < this->getEntries()) {
313 
314  this->get()->GetEvent(index);
315 
316  return this->get()->getAddress();
317  }
318 
319  return NULL;
320  }
321 
322 
323  /**
324  * Get internal counter.
325  *
326  * \return counter
327  */
328  virtual counter_type getCounter() const override
329  {
330  return counter;
331  }
332 
333 
334  /**
335  * Get actual class name.
336  *
337  * \return class name
338  */
339  virtual const char* getClassName() const override
340  {
341  return JDerived_t::Class_Name();
342  }
343 
344  protected:
346  private:
348  };
349 
350 
351  /**
352  * Specialiation of class JTreeScanner for unordered direct access of elements in ROOT TChain.
353  *
354  * The iteration of elements in the TChain will be in order of appearance.
355  */
356  template<class JClass_t>
357  struct JTreeScanner<JClass_t, JNullType> :
358  public JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>
359  {
360  /**
361  * Default constructor.
362  */
364  {}
365 
366 
367  /**
368  * Constructor.
369  *
370  * \param file_list file list
371  * \param limit limit
372  */
373  JTreeScanner(const JMultipleFileScanner_t& file_list, const JLimit& limit = JLimit()) :
374  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>(file_list, limit)
375  {}
376 
377 
378  /**
379  * Copy constructor.
380  *
381  * \param input TTree scanner
382  */
383  JTreeScanner(const JTreeScanner& input) :
384  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>(input)
385  {}
386  };
387 
388 
389  /**
390  * Specialiation of class JTreeScanner for ordered direct access of elements in ROOT TChain.
391  *
392  * The optional second template argument is used to the determine the value of an element.
393  * The elements in the TChain are then ordered according these values (from low to high).
394  * To this end, the evaluator class should provide for the following operator:
395  * <pre>
396  * double operator()(const JBase_t& element) const;
397  * </pre>
398  * As a result, the iteration of elements in the TChain will then be in the specified order.
399  * It also provides for the mechanism to find a corresponding entry at O(log(n)) look up time.
400  */
401  template<class JDerived_t, class JBase_t, class JEvaluator_t>
402  class JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JEvaluator_t> :
403  public JTreeScannerInterface<JBase_t, JEvaluator_t>,
404  public JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JNullType>
405  {
406  /**
407  * Auxiliary data structure for sorting of objects in TChain.
408  */
409  struct JEntry_t {
410  /**
411  * Default constructor.
412  */
414  index(-1),
415  value(0.0)
416  {}
417 
418 
419  /**
420  * Constructor.
421  *
422  * \param __index index
423  * \param __value value
424  */
425  JEntry_t(const Long64_t __index,
426  const double __value) :
427  index(__index),
428  value(__value)
429  {}
430 
431 
432  /**
433  * Comparison between two TChain entries.
434  *
435  * \param first first entry
436  * \param second second entry
437  * \return true if first value less than second; else false
438  */
439  friend inline bool operator<(const JEntry_t& first, const JEntry_t& second)
440  {
441  return first.value < second.value;
442  }
443 
444 
445  /**
446  * Comparison between TChain entry and value.
447  *
448  * \param entry entry
449  * \param value value
450  * \return true if given entry has less value; else false
451  */
452  friend inline bool operator<(const JEntry_t& entry, const double value)
453  {
454  return entry.value < value;
455  }
456 
457 
458  Long64_t index; //!< index in TChain
459  double value; //!< corresponding value
460  };
461 
462 
463  /**
464  * Type definition of internal queue for ordering the elements in the TChain.
465  */
467 
468 
469  public:
470 
471  typedef JBase_t data_type;
473 
477  using JTreeScanner<>::debug;
478 
479 
480  /**
481  * Default constructor.
482  */
484  JTreeScannerInterface<JBase_t, JEvaluator_t>()
485  {}
486 
487 
488  /**
489  * Constructor.
490  *
491  * \param file_list file list
492  * \param evaluator evaluator
493  */
495  const JEvaluator_t& evaluator = JEvaluator_t()) :
496  JTreeScannerInterface<JBase_t, JEvaluator_t>(evaluator)
497  {
498  this->configure(file_list);
499  }
500 
501 
502  /**
503  * Constructor.
504  *
505  * \param file_list file list
506  * \param limit limit
507  * \param evaluator evaluator
508  */
510  const JLimit& limit,
511  const JEvaluator_t& evaluator = JEvaluator_t()) :
512  JTreeScannerInterface<JBase_t, JEvaluator_t>(evaluator)
513  {
514  this->configure(file_list, limit);
515  }
516 
517 
518  /**
519  * Get next element.
520  *
521  * \return pointer to element.
522  */
523  virtual const pointer_type& next() override
524  {
525  if (this->hasNext()) {
526 
527  this->get()->GetEvent(queue[this->counter++].index);
528 
529  ps.reset(this->get()->getAddress());
530 
531  } else {
532 
533  ps.reset(NULL);
534  }
535 
536  return ps;
537  }
538 
539 
540  /**
541  * Configure.
542  *
543  * \param file_list file list
544  * \param limit limit
545  */
546  virtual void configure(const JMultipleFileScanner_t& file_list, const JLimit& limit) override
547  {
548  using namespace std;
549 
551 
552  setBranchStatus(this->get()->GetBranch(this->get()->getBranchName()), BRANCH_PATTERN, false);
553 
554  queue.resize(this->getEntries());
555 
556  typename queue_type::iterator out = queue.begin();
557 
558  for (Long64_t i = 0, n0 = 0; i != this->getEntries(); ++i, ++out) {
559 
560  const Long64_t n1 = (100 * (i + 1)) / this->getEntries();
561 
562  if (n1 > n0) {
563 
564  STATUS(left << setw(24) << this->get()->GetName() << right << ' ' << setw(3) << n1 << "%\r"); DEBUG(endl);
565 
566  n0 = n1;
567  }
568 
569  this->get()->GetEvent(i);
570 
571  const data_type* p = this->get()->getAddress();
572 
573  *out = JEntry_t(i, this->JTreeScannerInterface<JBase_t, JEvaluator_t>::getValue(*p));
574  }
575  STATUS(endl);
576 
577  this->get()->SetBranchStatus("*", 1);
578 
579  sort(queue.begin(), queue.end());
580  }
581 
582 
583  /**
584  * Get entry at given index.
585  *
586  * \param index index
587  * \return pointer to object (may be NULL)
588  */
589  virtual data_type* getEntry(Long64_t index) override
590  {
591  if (index >= 0 && index < (Long64_t) queue.size()) {
592 
593  this->get()->GetEvent(queue[index].index);
594 
595  return this->get()->getAddress();
596  }
597 
598  return NULL;
599  }
600 
601 
602  /**
603  * Find index of element that is closest in value to given value.
604  *
605  * A subsequent call to method getEntry() will point to the corresponding object.
606  *
607  * \param value value
608  * \return index (-1 in case of error)
609  */
610  virtual Long64_t find(const double value) const override
611  {
612  using namespace std;
613 
614  if (!queue.empty()) {
615 
616  typename queue_type::const_iterator p = lower_bound(queue.begin(), queue.end(), value);
617 
618  if (p == queue.end()) {
619 
620  return queue.size() - 1;
621 
622  } else if (p == queue.begin()) {
623 
624  return 0;
625 
626  } else {
627 
628  typename queue_type::const_iterator q = p--;
629 
630  if (value - p->value < q->value - value)
631  return distance(queue.begin(), p);
632  else
633  return distance(queue.begin(), q);
634  }
635  }
636 
637  return -1;
638  }
639 
640  protected:
641  /**
642  * Set status of branch.
643  *
644  * Note that the status of the branch and all sub-branches with names including given pattern will recursively be set.
645  *
646  * \param branch pointer to branch
647  * \param pattern pattern
648  * \param status status
649  */
650  static void setBranchStatus(TBranch* branch, const char* pattern, const bool status)
651  {
652  using namespace std;
653 
654  if (branch != NULL) {
655 
656  TObjArray* array = branch->GetListOfBranches();
657 
658  for (Int_t i = 0; i != array->GetEntries(); ++i) {
659 
660  TBranch* p = (TBranch*) array->At(i);
661 
662  if (p != NULL && string(p->GetName()).find(pattern) != string::npos) {
663 
664  if (p->GetSplitLevel() == 0) {
665 
666  NOTICE("Set status of branch " << p->GetName() << " to " << status << endl);
667 
668  p->SetStatus(status);
669  }
670  } else {
671 
672  setBranchStatus(p, pattern, status);
673  }
674  }
675  }
676  }
677 
678 
680 
681  private:
683  };
684 
685 
686  /**
687  * Specialiation of class JTreeScanner for ordered direct access of elements in ROOT TChain.
688  */
689  template<class JClass_t, class JEvaluator_t>
690  struct JTreeScanner :
691  public JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>
692  {
693  /**
694  * Default constructor.
695  */
697  {}
698 
699 
700  /**
701  * Constructor.
702  *
703  * \param file_list file list
704  * \param evaluator evaluator
705  */
707  const JEvaluator_t& evaluator = JEvaluator_t()) :
708  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>(file_list, evaluator)
709  {}
710 
711 
712  /**
713  * Constructor.
714  *
715  * \param file_list file list
716  * \param limit limit
717  * \param evaluator evaluator
718  */
720  const JLimit& limit,
721  const JEvaluator_t& evaluator = JEvaluator_t()) :
722  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>(file_list, limit, evaluator)
723  {}
724  };
725 }
726 
727 #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.
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit=JLimit())
Constructor.
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.
then echo Submitting reweighting and histogram comparison jobs to nikhef stbc batch queue
static void setBranchStatus(TBranch *branch, const char *pattern, const bool status)
Set status of branch.
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:66
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.
Auxiliary interface for direct access of elements in ROOT TChain.
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
int debug
debug level
Definition: JSirene.cc:66
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.
JTreeScanner(const JTreeScanner &input)
Copy constructor.
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.
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit=JLimit())
Constructor.
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