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