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  bool skipEntries(unsigned int& offset);
94  EntryNumber const& entryNumber() const {return entryNumber_;}
95  EntryNumber const& entryNumberForIndex(unsigned int index) const;
96  EntryNumber const& entries() const {return entries_;}
97  void setEntryNumber(EntryNumber theEntryNumber);
98  void insertEntryForIndex(unsigned int index);
99  std::vector<std::string> const& branchNames() const {return branchNames_;}
101  template <typename T>
102  void fillAux(T*& pAux) {
103  auxBranch_->SetAddress(&pAux);
105  }
106  template <typename T>
107  void fillBranchEntryMeta(TBranch* branch, T*& pbuf) {
108  if (metaTree_ != nullptr) {
109  // Metadata was in separate tree. Not cached.
110  branch->SetAddress(&pbuf);
112  } else {
113  fillBranchEntry<T>(branch, pbuf);
114  }
115  }
116 
117  template <typename T>
118  void fillBranchEntry(TBranch* branch, T*& pbuf) {
119  branch->SetAddress(&pbuf);
120  getEntry(branch, entryNumber_);
121  }
122 
123  template <typename T>
124  void fillBranchEntryMeta(TBranch* branch, EntryNumber entryNumber, T*& pbuf) {
125  if (metaTree_ != nullptr) {
126  // Metadata was in separate tree. Not cached.
127  branch->SetAddress(&pbuf);
128  roottree::getEntry(branch, entryNumber);
129  } else {
130  fillBranchEntry<T>(branch, entryNumber, pbuf);
131  }
132  }
133 
134  template <typename T>
135  void fillBranchEntry(TBranch* branch, EntryNumber entryNumber, T*& pbuf) {
136  branch->SetAddress(&pbuf);
137  getEntry(branch, entryNumber);
138  }
139 
140  TTree const* tree() const {return tree_;}
141  TTree* tree() {return tree_;}
142  TTree const* metaTree() const {return metaTree_;}
143  BranchMap const& branches() const;
144 
145  //For backwards compatibility
147 
148  inline TTreeCache* checkTriggerCache(TBranch* branch, EntryNumber entryNumber) const;
149  TTreeCache* checkTriggerCacheImpl(TBranch* branch, EntryNumber entryNumber) const;
150  inline TTreeCache* selectCache(TBranch* branch, EntryNumber entryNumber) const;
151  void trainCache(char const* branchNames);
152  void resetTraining() {trainNow_ = true;}
153 
155  private:
156  void setCacheSize(unsigned int cacheSize);
157  void setTreeMaxVirtualSize(int treeMaxVirtualSize);
158  void startTraining();
159  void stopTraining();
160 
161  std::shared_ptr<InputFile> filePtr_;
162 // We use bare pointers for pointers to some ROOT entities.
163 // Root owns them and uses bare pointers internally.
164 // Therefore,using smart pointers here will do no good.
165  TTree* tree_;
166  TTree* metaTree_;
168  TBranch* auxBranch_;
169 // We use a smart pointer to own the TTreeCache.
170 // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
171 // So, we make sure to it is detached before closing the TFile so there is no double delete.
172  std::shared_ptr<TTreeCache> treeCache_;
173  std::shared_ptr<TTreeCache> rawTreeCache_;
174  mutable std::shared_ptr<TTreeCache> triggerTreeCache_;
175  mutable std::shared_ptr<TTreeCache> rawTriggerTreeCache_;
176  mutable std::unordered_set<TBranch*> trainedSet_;
177  mutable std::unordered_set<TBranch*> triggerSet_;
180  std::unique_ptr<std::vector<EntryNumber> > entryNumberForIndex_;
181  std::vector<std::string> branchNames_;
182  std::shared_ptr<BranchMap> branches_;
183  bool trainNow_;
186  mutable bool performedSwitchOver_;
187  unsigned int learningEntries_;
188  unsigned int cacheSize_;
189  unsigned long treeAutoFlush_;
190 // Enable asynchronous I/O in ROOT (done in a separate thread). Only takes
191 // effect on the primary treeCache_; all other caches have this explicitly disabled.
194  std::unique_ptr<DelayedReader> rootDelayedReader_;
195 
196  TBranch* branchEntryInfoBranch_; //backwards compatibility
197  // below for backward compatibility
198  TTree* infoTree_; // backward compatibility
199  TBranch* statusBranch_; // backward compatibility
200  };
201 }
202 #endif
EntryNumber entryNumber_
Definition: RootTree.h:179
TBranch * statusBranch_
Definition: RootTree.h:199
bool current(EntryNumber entry) const
Definition: RootTree.h:90
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: RootTree.cc:480
unsigned int const defaultNonEventLearningEntries
Definition: RootTree.h:40
unsigned int learningEntries_
Definition: RootTree.h:187
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:175
void dropBranch(std::string const &oldBranchName)
Definition: RootTree.cc:149
void fillBranchEntryMeta(TBranch *branch, EntryNumber entryNumber, T *&pbuf)
Definition: RootTree.h:124
std::vector< std::string > branchNames_
Definition: RootTree.h:181
std::vector< std::string > const & branchNames() const
Definition: RootTree.h:99
std::shared_ptr< TTreeCache > treeCache_
Definition: RootTree.h:172
TTree * tree_
Definition: RootTree.h:165
bool trainNow_
Definition: RootTree.h:183
std::shared_ptr< BranchMap > branches_
Definition: RootTree.h:182
EntryNumber rawTriggerSwitchOverEntry_
Definition: RootTree.h:185
roottree::BranchMap BranchMap
Definition: RootTree.h:63
EntryNumber const & entries() const
Definition: RootTree.h:96
TTree * metaTree_
Definition: RootTree.h:166
std::unordered_set< TBranch * > trainedSet_
Definition: RootTree.h:176
TTree * tree()
Definition: RootTree.h:141
void stopTraining()
Definition: RootTree.cc:419
TBranch * branchEntryInfoBranch_
Definition: RootTree.h:196
void setPresence(BranchDescription &prod, std::string const &oldBranchName)
Definition: RootTree.cc:122
bool current() const
Definition: RootTree.h:89
EntryNumber entries_
Definition: RootTree.h:178
#define nullptr
bool enablePrefetching_
Definition: RootTree.h:192
void fillBranchEntryMeta(TBranch *branch, T *&pbuf)
Definition: RootTree.h:107
void trainCache(char const *branchNames)
Definition: RootTree.cc:449
unsigned int const defaultCacheSize
Definition: RootTree.h:37
void setTreeMaxVirtualSize(int treeMaxVirtualSize)
Definition: RootTree.cc:185
TBranch * branchEntryInfoBranch() const
Definition: RootTree.h:146
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:142
TTree const * tree() const
Definition: RootTree.h:140
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:154
unsigned long treeAutoFlush_
Definition: RootTree.h:189
IndexIntoFile::EntryNumber_t EntryNumber
Definition: RootTree.h:41
TTreeCache * checkTriggerCacheImpl(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:233
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
TBranch * auxBranch_
Definition: RootTree.h:168
EntryNumber const & entryNumber() const
Definition: RootTree.h:94
TBranch * provenanceBranch_
Definition: RootTree.h:51
bool enableTriggerCache_
Definition: RootTree.h:193
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:173
void close()
Definition: RootTree.cc:427
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:118
void startTraining()
Definition: RootTree.cc:388
bool isValid() const
Definition: RootTree.cc:104
std::shared_ptr< InputFile > filePtr_
Definition: RootTree.h:161
TBranch * productBranch_
Definition: RootTree.h:50
void setCacheSize(unsigned int cacheSize)
Definition: RootTree.cc:175
bool performedSwitchOver_
Definition: RootTree.h:186
std::unique_ptr< std::vector< EntryNumber > > entryNumberForIndex_
Definition: RootTree.h:180
unsigned int cacheSize_
Definition: RootTree.h:188
unsigned int const defaultNonEventCacheSize
Definition: RootTree.h:38
bool skipEntries(unsigned int &offset)
Definition: RootTree.cc:372
std::unordered_set< TBranch * > triggerSet_
Definition: RootTree.h:177
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:194
void resetTraining()
Definition: RootTree.h:152
void rewind()
Definition: RootTree.h:91
std::unique_ptr< TTreeCache > trainCache(TTree *tree, InputFile &file, unsigned int cacheSize, char const *branchNames)
Definition: RootTree.cc:504
long double T
unsigned int const defaultLearningEntries
Definition: RootTree.h:39
EntryNumber switchOverEntry_
Definition: RootTree.h:184
std::shared_ptr< TTreeCache > triggerTreeCache_
Definition: RootTree.h:174
void fillBranchEntry(TBranch *branch, EntryNumber entryNumber, T *&pbuf)
Definition: RootTree.h:135
void fillAux(T *&pAux)
Definition: RootTree.h:102
BranchType branchType_
Definition: RootTree.h:167
TTree * infoTree_
Definition: RootTree.h:198
void setEntryNumber(EntryNumber theEntryNumber)
Definition: RootTree.cc:190
roottree::EntryNumber EntryNumber
Definition: RootTree.h:64