CMS 3D CMS Logo

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