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