CMS 3D CMS Logo

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 
16 
17 #include "Rtypes.h"
18 #include "TBranch.h"
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 #include <unordered_set>
24 #include <unordered_map>
25 
26 class TBranch;
27 class TClass;
28 class TTree;
29 class TTreeCache;
30 
31 namespace edm {
32  class BranchKey;
33  class RootDelayedReader;
34  class InputFile;
35  class RootTree;
36 
37  class StreamContext;
38  class ModuleCallingContext;
39 
40  namespace signalslot {
41  template <typename T> class Signal;
42  }
43 
44 
45  namespace roottree {
46  unsigned int const defaultCacheSize = 20U * 1024 * 1024;
47  unsigned int const defaultNonEventCacheSize = 1U * 1024 * 1024;
48  unsigned int const defaultLearningEntries = 20U;
49  unsigned int const defaultNonEventLearningEntries = 1U;
51  struct BranchInfo {
53  branchDescription_(prod),
54  productBranch_(nullptr),
55  provenanceBranch_(nullptr),
56  classCache_(nullptr),
57  offsetToWrapperBase_(0) {}
59  TBranch* productBranch_;
60  TBranch* provenanceBranch_; // For backward compatibility
61  mutable TClass* classCache_;
62  mutable Int_t offsetToWrapperBase_;
63  };
64 
65  class BranchMap {
66  enum {
69  };
70  public:
71  void reserve(size_t iSize) {
72  map_.reserve(iSize);
73  }
74  void insert(edm::BranchID const& iKey, BranchInfo const& iInfo) {
75  map_.emplace(iKey.id(),iInfo);
76  }
77  BranchInfo const* find(BranchID const& iKey) const {
78  auto itFound = map_.find(iKey.id());
79  if(itFound == map_.end()) {return nullptr;}
80  return &itFound->second;
81  }
82  BranchInfo* find(BranchID const& iKey) {
83  auto itFound = map_.find(iKey.id());
84  if(itFound == map_.end()) {return nullptr;}
85  return &itFound->second;
86  }
87 
88  private:
89  std::unordered_map<unsigned int,BranchInfo> map_;
90  };
91 
92  Int_t getEntry(TBranch* branch, EntryNumber entryNumber);
93  Int_t getEntry(TTree* tree, EntryNumber entryNumber);
94  std::unique_ptr<TTreeCache> trainCache(TTree* tree, InputFile& file, unsigned int cacheSize, char const* branchNames);
95  }
96 
97  class RootTree {
98  public:
101  RootTree(std::shared_ptr<InputFile> filePtr,
102  BranchType const& branchType,
103  unsigned int nIndexes,
104  unsigned int maxVirtualSize,
105  unsigned int cacheSize,
106  unsigned int learningEntries,
107  bool enablePrefetching,
108  InputType inputType);
109  ~RootTree();
110 
111  RootTree(RootTree const&) = delete; // Disallow copying and moving
112  RootTree& operator=(RootTree const&) = delete; // Disallow copying and moving
113 
114  bool isValid() const;
115  void numberOfBranchesToAdd(size_t iSize) { branches_.reserve(iSize);}
116  void addBranch(BranchDescription const& prod,
117  std::string const& oldBranchName);
118  void dropBranch(std::string const& oldBranchName);
119  void getEntry(TBranch *branch, EntryNumber entry) const;
120  void setPresence(BranchDescription& prod,
121  std::string const& oldBranchName);
122 
123  bool next() {return ++entryNumber_ < entries_;}
124  bool nextWithCache();
125  bool previous() {return --entryNumber_ >= 0;}
126  bool current() const {return entryNumber_ < entries_ && entryNumber_ >= 0;}
127  bool current(EntryNumber entry) const {return entry < entries_ && entry >= 0;}
128  void rewind() {entryNumber_ = 0;}
129  void close();
130  bool skipEntries(unsigned int& offset);
131  EntryNumber const& entryNumber() const {return entryNumber_;}
132  EntryNumber const& entryNumberForIndex(unsigned int index) const;
133  EntryNumber const& entries() const {return entries_;}
134  void setEntryNumber(EntryNumber theEntryNumber);
135  void insertEntryForIndex(unsigned int index);
136  std::vector<std::string> const& branchNames() const {return branchNames_;}
137  DelayedReader* rootDelayedReader() const;
138  DelayedReader* resetAndGetRootDelayedReader() const;
139  template <typename T>
140  void fillAux(T*& pAux) {
141  auxBranch_->SetAddress(&pAux);
142  getEntry(auxBranch_, entryNumber_);
143  }
144  template <typename T>
145  void fillBranchEntryMeta(TBranch* branch, T*& pbuf) {
146  if (metaTree_ != nullptr) {
147  // Metadata was in separate tree. Not cached.
148  branch->SetAddress(&pbuf);
149  roottree::getEntry(branch, entryNumber_);
150  } else {
151  fillBranchEntry<T>(branch, pbuf);
152  }
153  }
154 
155  template <typename T>
156  void fillBranchEntry(TBranch* branch, T*& pbuf) {
157  branch->SetAddress(&pbuf);
158  getEntry(branch, entryNumber_);
159  }
160 
161  template <typename T>
162  void fillBranchEntryMeta(TBranch* branch, EntryNumber entryNumber, T*& pbuf) {
163  if (metaTree_ != nullptr) {
164  // Metadata was in separate tree. Not cached.
165  branch->SetAddress(&pbuf);
166  roottree::getEntry(branch, entryNumber);
167  } else {
168  fillBranchEntry<T>(branch, entryNumber, pbuf);
169  }
170  }
171 
172  template <typename T>
173  void fillBranchEntry(TBranch* branch, EntryNumber entryNumber, T*& pbuf) {
174  branch->SetAddress(&pbuf);
175  getEntry(branch, entryNumber);
176  }
177 
178  TTree const* tree() const {return tree_;}
179  TTree* tree() {return tree_;}
180  TTree const* metaTree() const {return metaTree_;}
181  BranchMap const& branches() const;
182 
183  //For backwards compatibility
184  TBranch* branchEntryInfoBranch() const {return branchEntryInfoBranch_;}
185 
186  inline TTreeCache* checkTriggerCache(TBranch* branch, EntryNumber entryNumber) const;
187  TTreeCache* checkTriggerCacheImpl(TBranch* branch, EntryNumber entryNumber) const;
188  inline TTreeCache* selectCache(TBranch* branch, EntryNumber entryNumber) const;
189  void trainCache(char const* branchNames);
190  void resetTraining() {trainNow_ = true;}
191 
192  BranchType branchType() const {return branchType_;}
193 
194  void setSignals(signalslot::Signal<void(StreamContext const&, ModuleCallingContext const&)> const* preEventReadSource,
195  signalslot::Signal<void(StreamContext const&, ModuleCallingContext const&)> const* postEventReadSource);
196 
197  private:
198  void setCacheSize(unsigned int cacheSize);
199  void setTreeMaxVirtualSize(int treeMaxVirtualSize);
200  void startTraining();
201  void stopTraining();
202 
203  std::shared_ptr<InputFile> filePtr_;
204 // We use bare pointers for pointers to some ROOT entities.
205 // Root owns them and uses bare pointers internally.
206 // Therefore,using smart pointers here will do no good.
207  TTree* tree_;
208  TTree* metaTree_;
210  TBranch* auxBranch_;
211 // We use a smart pointer to own the TTreeCache.
212 // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
213 // So, we make sure to it is detached before closing the TFile so there is no double delete.
214  std::shared_ptr<TTreeCache> treeCache_;
215  std::shared_ptr<TTreeCache> rawTreeCache_;
216  mutable std::shared_ptr<TTreeCache> triggerTreeCache_;
217  mutable std::shared_ptr<TTreeCache> rawTriggerTreeCache_;
218  mutable std::unordered_set<TBranch*> trainedSet_;
219  mutable std::unordered_set<TBranch*> triggerSet_;
220  EntryNumber entries_;
221  EntryNumber entryNumber_;
222  std::unique_ptr<std::vector<EntryNumber> > entryNumberForIndex_;
223  std::vector<std::string> branchNames_;
224  BranchMap branches_;
225  bool trainNow_;
226  EntryNumber switchOverEntry_;
227  mutable EntryNumber rawTriggerSwitchOverEntry_;
228  mutable bool performedSwitchOver_;
229  unsigned int learningEntries_;
230  unsigned int cacheSize_;
231  unsigned long treeAutoFlush_;
232 // Enable asynchronous I/O in ROOT (done in a separate thread). Only takes
233 // effect on the primary treeCache_; all other caches have this explicitly disabled.
236  std::unique_ptr<RootDelayedReader> rootDelayedReader_;
237 
238  TBranch* branchEntryInfoBranch_; //backwards compatibility
239  // below for backward compatibility
240  TTree* infoTree_; // backward compatibility
241  TBranch* statusBranch_; // backward compatibility
242  };
243 }
244 #endif
EntryNumber entryNumber_
Definition: RootTree.h:221
TBranch * statusBranch_
Definition: RootTree.h:241
bool current(EntryNumber entry) const
Definition: RootTree.h:127
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: RootTree.cc:510
Map map_
std::unordered_map< unsigned int, BranchInfo > map_
Definition: RootTree.h:89
unsigned int const defaultNonEventLearningEntries
Definition: RootTree.h:49
unsigned int learningEntries_
Definition: RootTree.h:229
InputType
Definition: InputType.h:5
std::shared_ptr< TTreeCache > rawTriggerTreeCache_
Definition: RootTree.h:217
void fillBranchEntryMeta(TBranch *branch, EntryNumber entryNumber, T *&pbuf)
Definition: RootTree.h:162
std::vector< std::string > branchNames_
Definition: RootTree.h:223
std::vector< std::string > const & branchNames() const
Definition: RootTree.h:136
BranchInfo * find(BranchID const &iKey)
Definition: RootTree.h:82
std::shared_ptr< TTreeCache > treeCache_
Definition: RootTree.h:214
TTree * tree_
Definition: RootTree.h:207
bool trainNow_
Definition: RootTree.h:225
EntryNumber rawTriggerSwitchOverEntry_
Definition: RootTree.h:227
roottree::BranchMap BranchMap
Definition: RootTree.h:99
BranchMap branches_
Definition: RootTree.h:224
EntryNumber const & entries() const
Definition: RootTree.h:133
TTree * metaTree_
Definition: RootTree.h:208
std::unordered_set< TBranch * > trainedSet_
Definition: RootTree.h:218
TTree * tree()
Definition: RootTree.h:179
TBranch * branchEntryInfoBranch_
Definition: RootTree.h:238
bool current() const
Definition: RootTree.h:126
EntryNumber entries_
Definition: RootTree.h:220
#define nullptr
bool enablePrefetching_
Definition: RootTree.h:234
void fillBranchEntryMeta(TBranch *branch, T *&pbuf)
Definition: RootTree.h:145
unsigned int const defaultCacheSize
Definition: RootTree.h:46
unsigned int id() const
Definition: BranchID.h:23
std::unique_ptr< RootDelayedReader > rootDelayedReader_
Definition: RootTree.h:236
TBranch * branchEntryInfoBranch() const
Definition: RootTree.h:184
BranchType
Definition: BranchType.h:11
bool previous()
Definition: RootTree.h:125
TTree const * metaTree() const
Definition: RootTree.h:180
TTree const * tree() const
Definition: RootTree.h:178
long long EntryNumber_t
bool next()
Definition: RootTree.h:123
BranchInfo const * find(BranchID const &iKey) const
Definition: RootTree.h:77
BranchDescription const branchDescription_
Definition: RootTree.h:58
BranchType branchType() const
Definition: RootTree.h:192
unsigned long treeAutoFlush_
Definition: RootTree.h:231
IndexIntoFile::EntryNumber_t EntryNumber
Definition: RootTree.h:50
TBranch * auxBranch_
Definition: RootTree.h:210
EntryNumber const & entryNumber() const
Definition: RootTree.h:131
TBranch * provenanceBranch_
Definition: RootTree.h:60
bool enableTriggerCache_
Definition: RootTree.h:235
std::shared_ptr< TTreeCache > rawTreeCache_
Definition: RootTree.h:215
BranchInfo(BranchDescription const &prod)
Definition: RootTree.h:52
void fillBranchEntry(TBranch *branch, T *&pbuf)
Definition: RootTree.h:156
void insert(edm::BranchID const &iKey, BranchInfo const &iInfo)
Definition: RootTree.h:74
std::shared_ptr< InputFile > filePtr_
Definition: RootTree.h:203
TBranch * productBranch_
Definition: RootTree.h:59
bool performedSwitchOver_
Definition: RootTree.h:228
std::unique_ptr< std::vector< EntryNumber > > entryNumberForIndex_
Definition: RootTree.h:222
HLT enums.
unsigned int cacheSize_
Definition: RootTree.h:230
unsigned int const defaultNonEventCacheSize
Definition: RootTree.h:47
std::unordered_set< TBranch * > triggerSet_
Definition: RootTree.h:219
void numberOfBranchesToAdd(size_t iSize)
Definition: RootTree.h:115
void resetTraining()
Definition: RootTree.h:190
Definition: tree.py:1
void rewind()
Definition: RootTree.h:128
void reserve(size_t iSize)
Definition: RootTree.h:71
std::unique_ptr< TTreeCache > trainCache(TTree *tree, InputFile &file, unsigned int cacheSize, char const *branchNames)
Definition: RootTree.cc:534
long double T
unsigned int const defaultLearningEntries
Definition: RootTree.h:48
EntryNumber switchOverEntry_
Definition: RootTree.h:226
std::shared_ptr< TTreeCache > triggerTreeCache_
Definition: RootTree.h:216
void fillBranchEntry(TBranch *branch, EntryNumber entryNumber, T *&pbuf)
Definition: RootTree.h:173
def branchType(schema, name)
Definition: revisionDML.py:113
void fillAux(T *&pAux)
Definition: RootTree.h:140
BranchType branchType_
Definition: RootTree.h:209
TTree * infoTree_
Definition: RootTree.h:240
roottree::EntryNumber EntryNumber
Definition: RootTree.h:100
Definition: vlib.h:39