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