CMS 3D CMS Logo

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