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