Jpp 21.0.0-rc.1-88-g0130508c4
the software that should make you happy
Loading...
Searching...
No Matches
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"
17#include "Jeep/JMessage.hh"
21
22
23/**
24 * \author mdejong
25 */
26
27namespace JSUPPORT {}
28namespace JPP { using namespace JSUPPORT; }
29
30namespace JSUPPORT {
31
32 using JLANG::JPointer;
33 using JLANG::JEquals;
34 using JLANG::JNullType;
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
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>
116 class JTreeScanner;
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 std;
266 using namespace JROOT;
267
268 this->setLimit(limit);
269
270 this->rewind();
271
272 this->get()->Reset();
273
274 for (JMultipleFileScanner<>::const_iterator i = file_list.begin(); i != file_list.end(); ++i) {
275
276 TFile* file = TFile::Open(i->c_str());
277
278 if (file != NULL) {
279
280 if (file->GetListOfKeys()->Contains(this->get()->getTreeName())) {
281 this->get()->Add(i->c_str());
282 }
283
284 delete file;
285 }
286 }
287
288 //this->getEntries(); // load TTree to set internal file
289 this->get()->LoadTree(0); // load TTree to set internal file
290
291 actionAtFileOpen<JDerived_t>(this->get()->GetCurrentFile());
292 actionAtFileOpen<JBase_t> (this->get()->GetCurrentFile());
293 }
294
295
296 /**
297 * Get number of entries.
298 *
299 * \return number of entries
300 */
301 virtual Long64_t getEntries() const override
302 {
303 return this->get()->GetEntries();
304 }
305
306
307 /**
308 * Get entry at given index.
309 *
310 * \param index index
311 * \return pointer to object (may be NULL)
312 */
313 virtual data_type* getEntry(Long64_t index) override
314 {
315 if (index >= 0 && index < this->getEntries()) {
316
317 this->get()->GetEvent(index);
318
319 return this->get()->getAddress();
320 }
321
322 return NULL;
323 }
324
325
326 /**
327 * Get internal counter.
328 *
329 * \return counter
330 */
331 virtual counter_type getCounter() const override
332 {
333 return counter;
334 }
335
336
337 /**
338 * Get actual class name.
339 *
340 * \return class name
341 */
342 virtual const char* getClassName() const override
343 {
344 return JDerived_t::Class_Name();
345 }
346
347 protected:
349 private:
352 };
353
354
355 /**
356 * Specialiation of class JTreeScanner for unordered direct access of elements in ROOT TChain.
357 *
358 * The iteration of elements in the TChain will be in order of appearance.
359 */
360 template<class JClass_t>
361 class JTreeScanner<JClass_t, JNullType> :
362 public JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>
363 {
364 public:
365 /**
366 * Default constructor.
367 */
369 {}
370
371
372 /**
373 * Constructor.
374 *
375 * \param file_list file list
376 * \param limit limit
377 */
378 JTreeScanner(const JMultipleFileScanner_t& file_list, const JLimit& limit = JLimit()) :
379 JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>(file_list, limit)
380 {}
381
382
383 /**
384 * Copy constructor.
385 *
386 * \param input TTree scanner
387 */
389 JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JNullType>(input)
390 {}
391 };
392
393
394 /**
395 * Specialisation of class JTreeScanner for ordered direct access of elements in ROOT TChain.
396 *
397 * The optional second template argument is used to the determine the value of an element.
398 * The elements in the TChain are then ordered according these values (from low to high).
399 * To this end, the evaluator class should provide for the following operator:
400 * <pre>
401 * value_type operator()(const JBase_t& element) const;
402 * </pre>
403 * where <tt>value_type</tt> should internally be defined and
404 * for which the operators <tt><</tt> and <tt>-</tt> should be provided.\n
405 * As a result, the iteration of elements in the TChain will then be in the specified order.
406 * It also provides for the mechanism to find a corresponding entry at O(log(n)) look up time.
407 */
408 template<class JDerived_t, class JBase_t, class JEvaluator_t>
409 class JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JEvaluator_t> :
410 public JTreeScannerInterface<JBase_t, JEvaluator_t>,
411 public JTreeScanner<JAssertConversion<JDerived_t, JBase_t>, JNullType>
412 {
413 public:
414
415 typedef JBase_t data_type;
418
419 using JTreeScannerInterface<JBase_t>::configure;
420 using JTreeScannerInterface<JBase_t, JEvaluator_t>::find;
421 using JTreeScannerInterface<JBase_t, JEvaluator_t>::getValue;
422 using JTreeScanner<>::debug;
423
424
425 /**
426 * Default constructor.
427 */
429 JTreeScannerInterface<JBase_t, JEvaluator_t>()
430 {}
431
432
433 /**
434 * Constructor.
435 *
436 * \param file_list file list
437 * \param evaluator evaluator
438 */
440 const JEvaluator_t& evaluator = JEvaluator_t()) :
441 JTreeScannerInterface<JBase_t, JEvaluator_t>(evaluator)
442 {
443 this->configure(file_list);
444 }
445
446
447 /**
448 * Constructor.
449 *
450 * \param file_list file list
451 * \param limit limit
452 * \param evaluator evaluator
453 */
455 const JLimit& limit,
456 const JEvaluator_t& evaluator = JEvaluator_t()) :
457 JTreeScannerInterface<JBase_t, JEvaluator_t>(evaluator)
458 {
459 this->configure(file_list, limit);
460 }
461
462
463 /**
464 * Get next element.
465 *
466 * \return pointer to element.
467 */
468 virtual const pointer_type& next() override
469 {
470 if (this->hasNext()) {
471
472 this->get()->GetEvent(queue[this->counter++].index);
473
474 ps.reset(this->get()->getAddress());
475
476 } else {
477
478 ps.reset(NULL);
479 }
480
481 return ps;
482 }
483
484
485 /**
486 * Configure.
487 *
488 * \param file_list file list
489 * \param limit limit
490 */
491 virtual void configure(const JMultipleFileScanner_t& file_list, const JLimit& limit) override
492 {
493 using namespace std;
494
496
497 setBranchStatus(this->get()->GetBranch(this->get()->getBranchName()), BRANCH_PATTERN, false);
498
499 queue.resize(this->getEntries());
500
501 typename queue_type::iterator out = queue.begin();
502
503 for (Long64_t i = 0, n0 = 0; i != this->getEntries(); ++i, ++out) {
504
505 const Long64_t n1 = (100 * (i + 1)) / this->getEntries();
506
507 if (n1 > n0) {
508
509 STATUS(left << setw(24) << this->get()->GetName() << right << ' ' << setw(3) << n1 << "%\r"); DEBUG(endl);
510
511 n0 = n1;
512 }
513
514 this->get()->GetEvent(i);
515
516 const data_type* p = this->get()->getAddress();
517
519 }
520 STATUS(left << setw(24) << this->get()->GetName() << right << endl);
521
522 this->get()->SetBranchStatus("*", 1);
523
524 sort(queue.begin(), queue.end());
525 }
526
527
528 /**
529 * Get entry at given index.
530 *
531 * \param index index
532 * \return pointer to object (may be NULL)
533 */
534 virtual data_type* getEntry(Long64_t index) override
535 {
536 if (index >= 0 && index < (Long64_t) queue.size()) {
537
538 this->get()->GetEvent(queue[index].index);
539
540 return this->get()->getAddress();
541 }
542
543 return NULL;
544 }
545
546
547 /**
548 * Find index of element that is closest in value to given value.
549 *
550 * A subsequent call to method getEntry() will point to the corresponding object.
551 *
552 * \param value value
553 * \return index (-1 in case of error)
554 */
555 virtual Long64_t find(const value_type value) const override
556 {
557 using namespace std;
558
559 if (!queue.empty()) {
560
561 typename queue_type::const_iterator p = lower_bound(queue.begin(), queue.end(), value);
562
563 if (p == queue.end()) {
564
565 return queue.size() - 1;
566
567 } else if (p == queue.begin()) {
568
569 return 0;
570
571 } else {
572
573 typename queue_type::const_iterator q = p--;
574
575 if (value - p->value < q->value - value)
576 return distance(queue.begin(), p);
577 else
578 return distance(queue.begin(), q);
579 }
580 }
581
582 return -1;
583 }
584
585 protected:
586 /**
587 * Auxiliary data structure for sorting of objects in TChain.
588 */
589 struct JEntry_t {
590 /**
591 * Default constructor.
592 */
594 index(-1),
595 value()
596 {}
597
598
599 /**
600 * Constructor.
601 *
602 * \param index index
603 * \param value value
604 */
605 JEntry_t(const Long64_t index,
606 const value_type value) :
607 index(index),
608 value(value)
609 {}
610
611
612 /**
613 * Comparison between two TChain entries.
614 *
615 * \param first first entry
616 * \param second second entry
617 * \return true if first value less than second; else false
618 */
619 friend inline bool operator<(const JEntry_t& first, const JEntry_t& second)
620 {
621 return first.value < second.value;
622 }
623
624
625 /**
626 * Comparison between TChain entry and value.
627 *
628 * \param entry entry
629 * \param value value
630 * \return true if given entry has less value; else false
631 */
632 friend inline bool operator<(const JEntry_t& entry, const value_type value)
633 {
634 return entry.value < value;
635 }
636
637
638 Long64_t index; //!< index in TChain
639 value_type value; //!< corresponding value
640 };
641
642
643 /**
644 * Type definition of internal queue for ordering the elements in the TChain.
645 */
647
648
649 /**
650 * Set status of branch.
651 *
652 * Note that the status of the branch and all sub-branches with names including given pattern will recursively be set.
653 *
654 * \param branch pointer to branch
655 * \param pattern pattern
656 * \param status status
657 */
658 static void setBranchStatus(TBranch* branch, const char* pattern, const bool status)
659 {
660 using namespace std;
661
662 if (branch != NULL) {
663
664 TObjArray* array = branch->GetListOfBranches();
665
666 for (Int_t i = 0; i != array->GetEntries(); ++i) {
667
668 TBranch* p = (TBranch*) array->At(i);
669
670 if (p != NULL && string(p->GetName()).find(pattern) != string::npos) {
671
672 if (p->GetSplitLevel() == 0) {
673
674 NOTICE("Set status of branch " << p->GetName() << " to " << status << endl);
675
676 p->SetStatus(status);
677 }
678 } else {
679
680 setBranchStatus(p, pattern, status);
681 }
682 }
683 }
684 }
685
686
688
689 private:
691 };
692
693
694 /**
695 * Specialiation of class JTreeScanner for ordered direct access of elements in ROOT TChain.
696 */
697 template<class JClass_t, class JEvaluator_t>
699 public JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>
700 {
701 public:
702 /**
703 * Default constructor.
704 */
706 {}
707
708
709 /**
710 * Constructor.
711 *
712 * \param file_list file list
713 * \param evaluator evaluator
714 */
716 const JEvaluator_t& evaluator = JEvaluator_t()) :
717 JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>(file_list, evaluator)
718 {}
719
720
721 /**
722 * Constructor.
723 *
724 * \param file_list file list
725 * \param limit limit
726 * \param evaluator evaluator
727 */
729 const JLimit& limit,
730 const JEvaluator_t& evaluator = JEvaluator_t()) :
731 JTreeScanner<JAssertConversion<JClass_t, JClass_t>, JEvaluator_t>(file_list, limit, evaluator)
732 {}
733 };
734}
735
736#endif
TChain reading for template data type.
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition JManip.hh:222
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define STATUS(A)
Definition JMessage.hh:63
#define NOTICE(A)
Definition JMessage.hh:64
int debug
debug level
Definition JSirene.cc:74
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Type definition for counter for ROOT TTree and auxiliary methods.
Data type dependent action methods for customised ROOT version management.
Scanning of objects from a single file according a format that follows from the extension of each fil...
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
bool is_valid() const
Check validity of pointer.
Auxialiary class to assert type conversion.
Template implementation of class that holds pointer to object(s).
Definition JPointer.hh:24
virtual JClass_t * get() const override
Get pointer.
Definition JPointer.hh:64
Auxiliary class for template TChain reading.
General purpose class for object reading from a list of file names.
Auxiliary interface for direct access of elements in ROOT TChain.
JEvaluator_t::value_type value_type
Type definition of time value.
virtual const pointer_type & next() override
Get next element.
std::vector< JEntry_t > queue_type
Type definition of internal queue for ordering the elements in the TChain.
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.
virtual data_type * getEntry(Long64_t index) override
Get entry at given index.
virtual Long64_t find(const value_type value) const override
Find index of element that is closest in value to given value.
static void setBranchStatus(TBranch *branch, const char *pattern, const bool status)
Set status of branch.
virtual void configure(const JMultipleFileScanner_t &file_list, const JLimit &limit) override
Configure.
JTreeScannerInterface< JBase_t, JEvaluator_t >::value_type value_type
JTreeScannerInterface< JBase_t, JEvaluator_t >::pointer_type pointer_type
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit=JLimit())
Constructor.
bool equals(const JTreeScanner &object) const
Check equality.
virtual const pointer_type & next() override
Get next element.
virtual skip_type skip(const skip_type ns) override
Skip items.
virtual data_type * getEntry(Long64_t index) override
Get entry at given index.
virtual Long64_t getEntries() const override
Get number of entries.
virtual void configure(const JMultipleFileScanner_t &file_list, const JLimit &limit) override
Configure.
virtual bool hasNext() override
Check availability of next element.
virtual const char * getClassName() const override
Get actual class name.
virtual counter_type getCounter() const override
Get internal counter.
JTreeScanner(const JTreeScanner &input)
Copy constructor.
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit=JLimit())
Constructor.
Base class for JTreeScanner.
JTreeScanner_t()
Default constructor.
JMultipleFileScanner_t getFilelist() const
Get file list.
Template definition for direct access of elements in ROOT TChain.
JTreeScanner(const JMultipleFileScanner_t &file_list, const JLimit &limit, const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
JTreeScanner()
Default constructor.
JTreeScanner(const JMultipleFileScanner_t &file_list, const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition JScale.hh:47
unsigned int skip_type
Type definition for number of objects to skip.
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
void actionAtFileOpen(TFile *file)
General action method at file open.
Long64_t counter_type
Type definition for counter.
Support classes and methods for experiment specific I/O.
static const char *const BRANCH_PATTERN
Pattern match for names of sub-branches that will not be read when ordering elements in a TTree.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, std::false_type option)
Configuration of value.
Auxiliary class for handling debug parameter within a class.
Definition JMessage.hh:44
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84
Auxiliary class for no type definition.
Definition JNullType.hh:19
Auxiliary class for defining the range of iterations of objects.
Definition JLimit.hh:45
Auxiliary base class for list of file names.
friend bool operator<(const JEntry_t &entry, const value_type value)
Comparison between TChain entry and value.
friend bool operator<(const JEntry_t &first, const JEntry_t &second)
Comparison between two TChain entries.