CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RootTree.h
Go to the documentation of this file.
1 #ifndef IOPool_Input_RootTree_h
2 #define IOPool_Input_RootTree_h
3 
4 /*----------------------------------------------------------------------
5 
6 RootTree.h // used by ROOT input sources
7 
8 ----------------------------------------------------------------------*/
9 
15 
16 #include "Rtypes.h"
17 #include "TBranch.h"
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 #include <unordered_set>
24 
25 class TBranch;
26 class TClass;
27 class TTree;
28 class TTreeCache;
29 
30 namespace edm {
31  class BranchKey;
32  class DelayedReader;
33  class InputFile;
34  class RootTree;
35 
36  namespace roottree {
37  unsigned int const defaultCacheSize = 20U * 1024 * 1024;
38  unsigned int const defaultNonEventCacheSize = 1U * 1024 * 1024;
39  unsigned int const defaultLearningEntries = 20U;
40  unsigned int const defaultNonEventLearningEntries = 1U;
42  struct BranchInfo {
44  branchDescription_(prod),
45  productBranch_(0),
47  classCache_(0) {}
49  TBranch* productBranch_;
50  TBranch* provenanceBranch_; // For backward compatibility
51  mutable TClass* classCache_;
52  };
53  typedef std::map<BranchKey const, BranchInfo> BranchMap;
54  Int_t getEntry(TBranch* branch, EntryNumber entryNumber);
55  Int_t getEntry(TTree* tree, EntryNumber entryNumber);
56  std::unique_ptr<TTreeCache> trainCache(TTree* tree, InputFile& file, unsigned int cacheSize, char const* branchNames);
57  }
58 
59  class RootTree {
60  public:
63  RootTree(boost::shared_ptr<InputFile> filePtr,
64  BranchType const& branchType,
65  unsigned int nIndexes,
66  unsigned int maxVirtualSize,
67  unsigned int cacheSize,
68  unsigned int learningEntries,
69  bool enablePrefetching,
70  InputType inputType);
71  ~RootTree();
72 
73  RootTree(RootTree const&) = delete; // Disallow copying and moving
74  RootTree& operator=(RootTree const&) = delete; // Disallow copying and moving
75 
76  bool isValid() const;
77  void addBranch(BranchKey const& key,
78  BranchDescription const& prod,
79  std::string const& oldBranchName);
80  void dropBranch(std::string const& oldBranchName);
81  void getEntry(TBranch *branch, EntryNumber entry) const;
83  std::string const& oldBranchName);
84 
85  bool next() {return ++entryNumber_ < entries_;}
86  bool previous() {return --entryNumber_ >= 0;}
87  bool current() const {return entryNumber_ < entries_ && entryNumber_ >= 0;}
88  bool current(EntryNumber entry) const {return entry < entries_ && entry >= 0;}
89  void rewind() {entryNumber_ = 0;}
90  void close();
91  EntryNumber const& entryNumber() const {return entryNumber_;}
92  EntryNumber const& entryNumberForIndex(unsigned int index) const;
93  EntryNumber const& entries() const {return entries_;}
94  void setEntryNumber(EntryNumber theEntryNumber);
95  void insertEntryForIndex(unsigned int index);
96  std::vector<std::string> const& branchNames() const {return branchNames_;}
98  template <typename T>
99  void fillAux(T*& pAux) {
100  auxBranch_->SetAddress(&pAux);
102  }
103  template <typename T>
104  void fillBranchEntryMeta(TBranch* branch, T*& pbuf) {
105  if (metaTree_ != 0) {
106  // Metadata was in separate tree. Not cached.
107  branch->SetAddress(&pbuf);
109  } else {
110  fillBranchEntry<T>(branch, pbuf);
111  }
112  }
113 
114  template <typename T>
115  void fillBranchEntry(TBranch* branch, T*& pbuf) {
116  branch->SetAddress(&pbuf);
117  getEntry(branch, entryNumber_);
118  }
119 
120  template <typename T>
121  void fillBranchEntryMeta(TBranch* branch, EntryNumber entryNumber, T*& pbuf) {
122  if (metaTree_ != 0) {
123  // Metadata was in separate tree. Not cached.
124  branch->SetAddress(&pbuf);
125  roottree::getEntry(branch, entryNumber);
126  } else {
127  fillBranchEntry<T>(branch, entryNumber, pbuf);
128  }
129  }
130 
131  template <typename T>
132  void fillBranchEntry(TBranch* branch, EntryNumber entryNumber, T*& pbuf) {
133  branch->SetAddress(&pbuf);
134  getEntry(branch, entryNumber);
135  }
136 
137  TTree const* tree() const {return tree_;}
138  TTree* tree() {return tree_;}
139  TTree const* metaTree() const {return metaTree_;}
140  BranchMap const& branches() const;
141 
142  //For backwards compatibility
144 
145  inline TTreeCache* checkTriggerCache(TBranch* branch, EntryNumber entryNumber) const;
146  TTreeCache* checkTriggerCacheImpl(TBranch* branch, EntryNumber entryNumber) const;
147  inline TTreeCache* selectCache(TBranch* branch, EntryNumber entryNumber) const;
148  void trainCache(char const* branchNames);
149  void resetTraining() {trainNow_ = true;}
150 
152  private:
153  void setCacheSize(unsigned int cacheSize);
154  void setTreeMaxVirtualSize(int treeMaxVirtualSize);
155  void startTraining();
156  void stopTraining();
157 
158  boost::shared_ptr<InputFile> filePtr_;
159 // We use bare pointers for pointers to some ROOT entities.
160 // Root owns them and uses bare pointers internally.
161 // Therefore,using smart pointers here will do no good.
162  TTree* tree_;
163  TTree* metaTree_;
165  TBranch* auxBranch_;
166 // We use a smart pointer to own the TTreeCache.
167 // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
168 // So, we make sure to it is detached before closing the TFile so there is no double delete.
169  boost::shared_ptr<TTreeCache> treeCache_;
170  boost::shared_ptr<TTreeCache> rawTreeCache_;
171  mutable boost::shared_ptr<TTreeCache> triggerTreeCache_;
172  mutable boost::shared_ptr<TTreeCache> rawTriggerTreeCache_;
173  mutable std::unordered_set<TBranch*> trainedSet_;
174  mutable std::unordered_set<TBranch*> triggerSet_;
177  std::unique_ptr<std::vector<EntryNumber> > entryNumberForIndex_;
178  std::vector<std::string> branchNames_;
179  boost::shared_ptr<BranchMap> branches_;
180  bool trainNow_;
183  mutable bool performedSwitchOver_;
184  unsigned int learningEntries_;
185  unsigned int cacheSize_;
186  unsigned long treeAutoFlush_;
187 // Enable asynchronous I/O in ROOT (done in a separate thread). Only takes
188 // effect on the primary treeCache_; all other caches have this explicitly disabled.
191  std::unique_ptr<DelayedReader> rootDelayedReader_;
192 
193  TBranch* branchEntryInfoBranch_; //backwards compatibility
194  // below for backward compatibility
195  TTree* infoTree_; // backward compatibility
196  TBranch* statusBranch_; // backward compatibility
197  };
198 }
199 #endif
EntryNumber entryNumber_
Definition: RootTree.h:176
TBranch * statusBranch_
Definition: RootTree.h:196
bool current(EntryNumber entry) const
Definition: RootTree.h:88
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: RootTree.cc:461
boost::shared_ptr< TTreeCache > treeCache_
Definition: RootTree.h:169
unsigned int const defaultNonEventLearningEntries
Definition: RootTree.h:40
unsigned int learningEntries_
Definition: RootTree.h:184
InputType
Definition: InputType.h:5
TTreeCache * checkTriggerCache(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:222
void addBranch(BranchKey const &key, BranchDescription const &prod, std::string const &oldBranchName)
Definition: RootTree.cc:130
void dropBranch(std::string const &oldBranchName)
Definition: RootTree.cc:149
void fillBranchEntryMeta(TBranch *branch, EntryNumber entryNumber, T *&pbuf)
Definition: RootTree.h:121
RootTree(boost::shared_ptr< InputFile > filePtr, BranchType const &branchType, unsigned int nIndexes, unsigned int maxVirtualSize, unsigned int cacheSize, unsigned int learningEntries, bool enablePrefetching, InputType inputType)
Definition: RootTree.cc:28
std::vector< std::string > branchNames_
Definition: RootTree.h:178
std::vector< std::string > const & branchNames() const
Definition: RootTree.h:96
TTree * tree_
Definition: RootTree.h:162
bool trainNow_
Definition: RootTree.h:180
EntryNumber rawTriggerSwitchOverEntry_
Definition: RootTree.h:182
roottree::BranchMap BranchMap
Definition: RootTree.h:61
EntryNumber const & entries() const
Definition: RootTree.h:93
TTree * metaTree_
Definition: RootTree.h:163
std::unordered_set< TBranch * > trainedSet_
Definition: RootTree.h:173
TTree * tree()
Definition: RootTree.h:138
void stopTraining()
Definition: RootTree.cc:400
TBranch * branchEntryInfoBranch_
Definition: RootTree.h:193
void setPresence(BranchDescription &prod, std::string const &oldBranchName)
Definition: RootTree.cc:122
bool current() const
Definition: RootTree.h:87
EntryNumber entries_
Definition: RootTree.h:175
boost::shared_ptr< TTreeCache > rawTreeCache_
Definition: RootTree.h:170
bool enablePrefetching_
Definition: RootTree.h:189
void fillBranchEntryMeta(TBranch *branch, T *&pbuf)
Definition: RootTree.h:104
void trainCache(char const *branchNames)
Definition: RootTree.cc:430
unsigned int const defaultCacheSize
Definition: RootTree.h:37
void setTreeMaxVirtualSize(int treeMaxVirtualSize)
Definition: RootTree.cc:185
TBranch * branchEntryInfoBranch() const
Definition: RootTree.h:143
long long EntryNumber_t
std::map< BranchKey const, BranchInfo > BranchMap
Definition: RootTree.h:53
BranchType
Definition: BranchType.h:11
bool previous()
Definition: RootTree.h:86
RootTree & operator=(RootTree const &)=delete
TTree const * metaTree() const
Definition: RootTree.h:139
TTree const * tree() const
Definition: RootTree.h:137
boost::shared_ptr< TTreeCache > triggerTreeCache_
Definition: RootTree.h:171
bool next()
Definition: RootTree.h:85
boost::shared_ptr< TTreeCache > rawTriggerTreeCache_
Definition: RootTree.h:172
void insertEntryForIndex(unsigned int index)
Definition: RootTree.cc:98
BranchDescription const branchDescription_
Definition: RootTree.h:48
TTreeCache * selectCache(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:336
BranchType branchType() const
Definition: RootTree.h:151
unsigned long treeAutoFlush_
Definition: RootTree.h:186
IndexIntoFile::EntryNumber_t EntryNumber
Definition: RootTree.h:41
TTreeCache * checkTriggerCacheImpl(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:233
TBranch * auxBranch_
Definition: RootTree.h:165
EntryNumber const & entryNumber() const
Definition: RootTree.h:91
TBranch * provenanceBranch_
Definition: RootTree.h:50
bool enableTriggerCache_
Definition: RootTree.h:190
DelayedReader * rootDelayedReader() const
Definition: RootTree.cc:116
EntryNumber const & entryNumberForIndex(unsigned int index) const
Definition: RootTree.cc:92
void close()
Definition: RootTree.cc:408
BranchInfo(BranchDescription const &prod)
Definition: RootTree.h:43
void getEntry(TBranch *branch, EntryNumber entry) const
Definition: RootTree.cc:355
void fillBranchEntry(TBranch *branch, T *&pbuf)
Definition: RootTree.h:115
void startTraining()
Definition: RootTree.cc:372
boost::shared_ptr< BranchMap > branches_
Definition: RootTree.h:179
bool isValid() const
Definition: RootTree.cc:104
TBranch * productBranch_
Definition: RootTree.h:49
void setCacheSize(unsigned int cacheSize)
Definition: RootTree.cc:175
bool performedSwitchOver_
Definition: RootTree.h:183
std::unique_ptr< std::vector< EntryNumber > > entryNumberForIndex_
Definition: RootTree.h:177
list key
Definition: combine.py:13
unsigned int cacheSize_
Definition: RootTree.h:185
unsigned int const defaultNonEventCacheSize
Definition: RootTree.h:38
std::unordered_set< TBranch * > triggerSet_
Definition: RootTree.h:174
BranchMap const & branches() const
Definition: RootTree.cc:172
std::unique_ptr< DelayedReader > rootDelayedReader_
Definition: RootTree.h:191
boost::shared_ptr< InputFile > filePtr_
Definition: RootTree.h:158
void resetTraining()
Definition: RootTree.h:149
void rewind()
Definition: RootTree.h:89
std::unique_ptr< TTreeCache > trainCache(TTree *tree, InputFile &file, unsigned int cacheSize, char const *branchNames)
Definition: RootTree.cc:485
long double T
unsigned int const defaultLearningEntries
Definition: RootTree.h:39
EntryNumber switchOverEntry_
Definition: RootTree.h:181
void fillBranchEntry(TBranch *branch, EntryNumber entryNumber, T *&pbuf)
Definition: RootTree.h:132
void fillAux(T *&pAux)
Definition: RootTree.h:99
BranchType branchType_
Definition: RootTree.h:164
TTree * infoTree_
Definition: RootTree.h:195
void setEntryNumber(EntryNumber theEntryNumber)
Definition: RootTree.cc:190
roottree::EntryNumber EntryNumber
Definition: RootTree.h:62