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