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