Jpp - 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 && file->GetListOfKeys()->Contains(this->get()->getTreeName())) {
238  this->get()->Add(i->c_str());
239  }
240 
241  delete file;
242  }
243 
244  this->getEntries(); // load TTree to set internal file
245 
246  actionAtFileOpen<JDerived_t>(this->get()->GetCurrentFile());
247  actionAtFileOpen<JBase_t> (this->get()->GetCurrentFile());
248  }
249 
250 
251  /**
252  * Get number of entries.
253  *
254  * \return number of entries
255  */
256  virtual Long64_t getEntries() const override
257  {
258  return this->get()->GetEntries();
259  }
260 
261 
262  /**
263  * Get entry at given index.
264  *
265  * \param index index
266  * \return pointer to object (may be NULL)
267  */
268  virtual data_type* getEntry(Long64_t index) override
269  {
270  if (index >= 0 && index < this->getEntries()) {
271 
272  this->get()->GetEvent(index);
273 
274  return this->get()->getAddress();
275  }
276 
277  return NULL;
278  }
279 
280 
281  /**
282  * Get internal counter.
283  *
284  * \return counter
285  */
286  virtual counter_type getCounter() const override
287  {
288  return counter;
289  }
290 
291 
292  /**
293  * Get actual class name.
294  *
295  * \return class name
296  */
297  virtual const char* getClassName() const override
298  {
299  return JDerived_t::Class_Name();
300  }
301 
302  protected:
304  private:
306  };
307 
308 
309  /**
310  * Specialiation of class JTreeScanner for unordered direct access of elements in ROOT TChain.
311  *
312  * The iteration of elements in the TChain will be in order of appearance.
313  */
314  template<class JClass_t>
315  struct JTreeScanner<JClass_t, JNullType> :
316  public JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>
317  {
318  /**
319  * Default constructor.
320  */
322  {}
323 
324 
325  /**
326  * Constructor.
327  *
328  * \param file_list file list
329  * \param limit limit
330  */
331  JTreeScanner(const JMultipleFileScanner_t& file_list, const JLimit& limit = JLimit()) :
332  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>(file_list, limit)
333  {}
334 
335 
336  /**
337  * Copy constructor.
338  *
339  * \param input TTree scanner
340  */
341  JTreeScanner(const JTreeScanner& input) :
342  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>(input)
343  {}
344  };
345 
346 
347  /**
348  * Specialiation of class JTreeScanner for ordered direct access of elements in ROOT TChain.
349  *
350  * The optional second template argument is used to the determine the value of an element.
351  * The elements in the TChain are then ordered according these values (from low to high).
352  * To this end, the evaluator class should provide for the following operator:
353  * <pre>
354  * double operator()(const JBase_t& element) const;
355  * </pre>
356  * As a result, the iteration of elements in the TChain will then be in the specified order.
357  * It also provides for the mechanism to find a corresponding entry at O(log(n)) look up time.
358  */
359  template<class JDerived_t, class JBase_t, class JEvaluator_t>
360  class JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JEvaluator_t> :
361  public JTreeScannerInterface<JBase_t, JEvaluator_t>,
362  public JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JNullType>
363  {
364  /**
365  * Auxiliary data structure for sorting of objects in TChain.
366  */
367  struct JEntry_t {
368  /**
369  * Default constructor.
370  */
372  index(-1),
373  value(0.0)
374  {}
375 
376 
377  /**
378  * Constructor.
379  *
380  * \param __index index
381  * \param __value value
382  */
383  JEntry_t(const Long64_t __index,
384  const double __value) :
385  index(__index),
386  value(__value)
387  {}
388 
389 
390  /**
391  * Comparison between two TChain entries.
392  *
393  * \param first first entry
394  * \param second second entry
395  * \return true if first value less than second; else false
396  */
397  friend inline bool operator<(const JEntry_t& first, const JEntry_t& second)
398  {
399  return first.value < second.value;
400  }
401 
402 
403  /**
404  * Comparison between TChain entry and value.
405  *
406  * \param entry entry
407  * \param value value
408  * \return true if given entry has less value; else false
409  */
410  friend inline bool operator<(const JEntry_t& entry, const double value)
411  {
412  return entry.value < value;
413  }
414 
415 
416  Long64_t index; //!< index in TChain
417  double value; //!< corresponding value
418  };
419 
420 
421  /**
422  * Type definition of internal queue for ordering the elements in the TChain.
423  */
425 
426 
427  public:
428 
429  typedef JBase_t data_type;
431 
435  using JTreeScanner<>::debug;
436 
437 
438  /**
439  * Default constructor.
440  */
442  JTreeScannerInterface<JBase_t, JEvaluator_t>()
443  {}
444 
445 
446  /**
447  * Constructor.
448  *
449  * \param file_list file list
450  * \param evaluator evaluator
451  */
453  const JEvaluator_t& evaluator = JEvaluator_t()) :
454  JTreeScannerInterface<JBase_t, JEvaluator_t>(evaluator)
455  {
456  this->configure(file_list);
457  }
458 
459 
460  /**
461  * Constructor.
462  *
463  * \param file_list file list
464  * \param limit limit
465  * \param evaluator evaluator
466  */
468  const JLimit& limit,
469  const JEvaluator_t& evaluator = JEvaluator_t()) :
470  JTreeScannerInterface<JBase_t, JEvaluator_t>(evaluator)
471  {
472  this->configure(file_list, limit);
473  }
474 
475 
476  /**
477  * Get next element.
478  *
479  * \return pointer to element.
480  */
481  virtual const pointer_type& next() override
482  {
483  if (this->hasNext()) {
484 
485  this->get()->GetEvent(queue[this->counter++].index);
486 
487  ps.reset(this->get()->getAddress());
488 
489  } else {
490 
491  ps.reset(NULL);
492  }
493 
494  return ps;
495  }
496 
497 
498  /**
499  * Configure.
500  *
501  * \param file_list file list
502  * \param limit limit
503  */
504  virtual void configure(const JMultipleFileScanner_t& file_list, const JLimit& limit) override
505  {
506  using namespace std;
507 
509 
510  setBranchStatus(this->get()->GetBranch(this->get()->getBranchName()), BRANCH_PATTERN, false);
511 
512  queue.resize(this->getEntries());
513 
514  typename queue_type::iterator out = queue.begin();
515 
516  for (Long64_t i = 0, n0 = 0; i != this->getEntries(); ++i, ++out) {
517 
518  const Long64_t n1 = (100 * (i + 1)) / this->getEntries();
519 
520  if (n1 > n0) {
521 
522  STATUS(left << setw(24) << this->get()->GetName() << right << ' ' << setw(3) << n1 << "%\r"); DEBUG(endl);
523 
524  n0 = n1;
525  }
526 
527  this->get()->GetEvent(i);
528 
529  const data_type* p = this->get()->getAddress();
530 
531  *out = JEntry_t(i, this->JTreeScannerInterface<JBase_t, JEvaluator_t>::getValue(*p));
532  }
533  STATUS(endl);
534 
535  this->get()->SetBranchStatus("*", 1);
536 
537  sort(queue.begin(), queue.end());
538  }
539 
540 
541  /**
542  * Get entry at given index.
543  *
544  * \param index index
545  * \return pointer to object (may be NULL)
546  */
547  virtual data_type* getEntry(Long64_t index) override
548  {
549  if (index >= 0 && index < (Long64_t) queue.size()) {
550 
551  this->get()->GetEvent(queue[index].index);
552 
553  return this->get()->getAddress();
554  }
555 
556  return NULL;
557  }
558 
559 
560  /**
561  * Find index of element that is closest in value to given value.
562  *
563  * A subsequent call to method getEntry() will point to the corresponding object.
564  *
565  * \param value value
566  * \return index (-1 in case of error)
567  */
568  virtual Long64_t find(const double value) const override
569  {
570  using namespace std;
571 
572  if (!queue.empty()) {
573 
574  typename queue_type::const_iterator p = lower_bound(queue.begin(), queue.end(), value);
575 
576  if (p == queue.end()) {
577 
578  return queue.size() - 1;
579 
580  } else if (p == queue.begin()) {
581 
582  return 0;
583 
584  } else {
585 
586  typename queue_type::const_iterator q = p--;
587 
588  if (value - p->value < q->value - value)
589  return distance(queue.begin(), p);
590  else
591  return distance(queue.begin(), q);
592  }
593  }
594 
595  return -1;
596  }
597 
598  protected:
599  /**
600  * Set status of branch.
601  *
602  * Note that the status of the branch and all sub-branches with names including given pattern will recursively be set.
603  *
604  * \param branch pointer to branch
605  * \param pattern pattern
606  * \param status status
607  */
608  static void setBranchStatus(TBranch* branch, const char* pattern, const bool status)
609  {
610  using namespace std;
611 
612  if (branch != NULL) {
613 
614  TObjArray* array = branch->GetListOfBranches();
615 
616  for (Int_t i = 0; i != array->GetEntries(); ++i) {
617 
618  TBranch* p = (TBranch*) array->At(i);
619 
620  if (p != NULL && string(p->GetName()).find(pattern) != string::npos) {
621 
622  if (p->GetSplitLevel() == 0) {
623 
624  NOTICE("Set status of branch " << p->GetName() << " to " << status << endl);
625 
626  p->SetStatus(status);
627  }
628  } else {
629 
630  setBranchStatus(p, pattern, status);
631  }
632  }
633  }
634  }
635 
636 
638 
639  private:
641  };
642 
643 
644  /**
645  * Specialiation of class JTreeScanner for ordered direct access of elements in ROOT TChain.
646  */
647  template<class JClass_t, class JEvaluator_t>
648  struct JTreeScanner :
649  public JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>
650  {
651  /**
652  * Default constructor.
653  */
655  {}
656 
657 
658  /**
659  * Constructor.
660  *
661  * \param file_list file list
662  * \param evaluator evaluator
663  */
665  const JEvaluator_t& evaluator = JEvaluator_t()) :
666  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>(file_list, evaluator)
667  {}
668 
669 
670  /**
671  * Constructor.
672  *
673  * \param file_list file list
674  * \param limit limit
675  * \param evaluator evaluator
676  */
678  const JLimit& limit,
679  const JEvaluator_t& evaluator = JEvaluator_t()) :
680  JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>(file_list, limit, evaluator)
681  {}
682  };
683 }
684 
685 #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.
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.
then usage $script< string identifier >< detectorfile > input file(toashort file)+" "\nNote that the input files and toashort files should be one-to-one related." fi if (( $
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.
function queue()
Definition: qlib.sh:50
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit=JLimit())
Constructor.
JTreeScanner_t()
Default constructor.
Definition: JTreeScanner.hh:73
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Auxiliary class for handling debug parameter within a class.
Definition: JMessage.hh:44