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 
13 
14 #include "Rtypes.h"
15 #include "TBranch.h"
16 
17 #include <map>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include <unordered_set>
22 
23 class TBranch;
24 class TClass;
25 class TTree;
26 class TTreeCache;
27 
28 namespace edm {
29  struct BranchKey;
30  class DelayedReader;
31  class InputFile;
32  class RootTree;
33 
34  namespace roottree {
35  unsigned int const defaultCacheSize = 20U * 1024 * 1024;
36  unsigned int const defaultNonEventCacheSize = 1U * 1024 * 1024;
37  unsigned int const defaultLearningEntries = 20U;
38  unsigned int const defaultNonEventLearningEntries = 1U;
39  typedef Long64_t EntryNumber;
40  struct BranchInfo {
42  branchDescription_(prod),
43  productBranch_(0),
45  classCache_(0) {}
47  TBranch* productBranch_;
48  TBranch* provenanceBranch_; // For backward compatibility
49  mutable TClass* classCache_;
50  };
51  typedef std::map<BranchKey const, BranchInfo> BranchMap;
52  Int_t getEntry(TBranch* branch, EntryNumber entryNumber);
53  Int_t getEntry(TTree* tree, EntryNumber entryNumber);
54  std::unique_ptr<TTreeCache> trainCache(TTree* tree, InputFile& file, unsigned int cacheSize, char const* branchNames);
55  }
56 
57  class RootTree {
58  public:
61  RootTree(boost::shared_ptr<InputFile> filePtr,
62  BranchType const& branchType,
63  unsigned int maxVirtualSize,
64  unsigned int cacheSize,
65  unsigned int learningEntries,
66  bool enablePrefetching);
67  ~RootTree();
68 
69  RootTree(RootTree const&) = delete; // Disallow copying and moving
70  RootTree& operator=(RootTree const&) = delete; // Disallow copying and moving
71 
72  bool isValid() const;
73  void addBranch(BranchKey const& key,
74  BranchDescription const& prod,
75  std::string const& oldBranchName);
76  void dropBranch(std::string const& oldBranchName);
77  void getEntry(TBranch *branch, EntryNumber entry) const;
79  std::string const& oldBranchName);
80 
81  bool next() {return ++entryNumber_ < entries_;}
82  bool previous() {return --entryNumber_ >= 0;}
83  bool current() {return entryNumber_ < entries_ && entryNumber_ >= 0;}
84  void rewind() {entryNumber_ = 0;}
85  void close();
86  EntryNumber const& entryNumber() const {return entryNumber_;}
87  EntryNumber const& entries() const {return entries_;}
88  void setEntryNumber(EntryNumber theEntryNumber);
89  std::vector<std::string> const& branchNames() const {return branchNames_;}
91  template <typename T>
92  void fillAux(T*& pAux) {
93  auxBranch_->SetAddress(&pAux);
95  }
96  template <typename T>
97  void fillBranchEntryMeta(TBranch* branch, T*& pbuf) {
98  if (metaTree_ != 0) {
99  // Metadata was in separate tree. Not cached.
100  branch->SetAddress(&pbuf);
102  } else {
103  fillBranchEntry<T>(branch, pbuf);
104  }
105  }
106 
107  template <typename T>
108  void fillBranchEntry(TBranch* branch, T*& pbuf) {
109  branch->SetAddress(&pbuf);
110  getEntry(branch, entryNumber_);
111  }
112 
113  TTree const* tree() const {return tree_;}
114  TTree* tree() {return tree_;}
115  TTree const* metaTree() const {return metaTree_;}
116  BranchMap const& branches() const;
117 
118  //For backwards compatibility
120 
121  inline TTreeCache* checkTriggerCache(TBranch* branch, EntryNumber entryNumber) const;
122  TTreeCache* checkTriggerCacheImpl(TBranch* branch, EntryNumber entryNumber) const;
123  inline TTreeCache* selectCache(TBranch* branch, EntryNumber entryNumber) const;
124  void trainCache(char const* branchNames);
125  void resetTraining() {trainNow_ = true;}
126 
128  private:
129  void setCacheSize(unsigned int cacheSize);
130  void setTreeMaxVirtualSize(int treeMaxVirtualSize);
131  void startTraining();
132  void stopTraining();
133 
134  boost::shared_ptr<InputFile> filePtr_;
135 // We use bare pointers for pointers to some ROOT entities.
136 // Root owns them and uses bare pointers internally.
137 // Therefore,using smart pointers here will do no good.
138  TTree* tree_;
139  TTree* metaTree_;
141  TBranch* auxBranch_;
142 // We use a smart pointer to own the TTreeCache.
143 // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
144 // So, we make sure to it is detached before closing the TFile so there is no double delete.
145  boost::shared_ptr<TTreeCache> treeCache_;
146  boost::shared_ptr<TTreeCache> rawTreeCache_;
147  mutable boost::shared_ptr<TTreeCache> triggerTreeCache_;
148  mutable boost::shared_ptr<TTreeCache> rawTriggerTreeCache_;
149  mutable std::unordered_set<TBranch*> trainedSet_;
150  mutable std::unordered_set<TBranch*> triggerSet_;
153  std::vector<std::string> branchNames_;
154  boost::shared_ptr<BranchMap> branches_;
155  bool trainNow_;
158  mutable bool performedSwitchOver_;
159  unsigned int learningEntries_;
160  unsigned int cacheSize_;
161  unsigned long treeAutoFlush_;
162 // Enable asynchronous I/O in ROOT (done in a separate thread). Only takes
163 // effect on the primary treeCache_; all other caches have this explicitly disabled.
166  std::unique_ptr<DelayedReader> rootDelayedReader_;
167 
168  TBranch* branchEntryInfoBranch_; //backwards compatibility
169  // below for backward compatibility
170  TTree* infoTree_; // backward compatibility
171  TBranch* statusBranch_; // backward compatibility
172  };
173 }
174 #endif
EntryNumber entryNumber_
Definition: RootTree.h:152
TBranch * statusBranch_
Definition: RootTree.h:171
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: RootTree.cc:446
boost::shared_ptr< TTreeCache > treeCache_
Definition: RootTree.h:145
unsigned int const defaultNonEventLearningEntries
Definition: RootTree.h:38
unsigned int learningEntries_
Definition: RootTree.h:159
TTreeCache * checkTriggerCache(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:207
void addBranch(BranchKey const &key, BranchDescription const &prod, std::string const &oldBranchName)
Definition: RootTree.cc:114
void dropBranch(std::string const &oldBranchName)
Definition: RootTree.cc:134
ConstBranchDescription branchDescription_
Definition: RootTree.h:46
std::vector< std::string > branchNames_
Definition: RootTree.h:153
std::vector< std::string > const & branchNames() const
Definition: RootTree.h:89
void setPresence(BranchDescription const &prod, std::string const &oldBranchName)
Definition: RootTree.cc:105
TTree * tree_
Definition: RootTree.h:138
bool trainNow_
Definition: RootTree.h:155
EntryNumber rawTriggerSwitchOverEntry_
Definition: RootTree.h:157
roottree::BranchMap BranchMap
Definition: RootTree.h:59
EntryNumber const & entries() const
Definition: RootTree.h:87
TTree * metaTree_
Definition: RootTree.h:139
RootTree(boost::shared_ptr< InputFile > filePtr, BranchType const &branchType, unsigned int maxVirtualSize, unsigned int cacheSize, unsigned int learningEntries, bool enablePrefetching)
Definition: RootTree.cc:27
std::unordered_set< TBranch * > trainedSet_
Definition: RootTree.h:149
TTree * tree()
Definition: RootTree.h:114
void stopTraining()
Definition: RootTree.cc:385
TBranch * branchEntryInfoBranch_
Definition: RootTree.h:168
EntryNumber entries_
Definition: RootTree.h:151
boost::shared_ptr< TTreeCache > rawTreeCache_
Definition: RootTree.h:146
BranchInfo(ConstBranchDescription const &prod)
Definition: RootTree.h:41
bool enablePrefetching_
Definition: RootTree.h:164
void fillBranchEntryMeta(TBranch *branch, T *&pbuf)
Definition: RootTree.h:97
void trainCache(char const *branchNames)
Definition: RootTree.cc:415
unsigned int const defaultCacheSize
Definition: RootTree.h:35
void setTreeMaxVirtualSize(int treeMaxVirtualSize)
Definition: RootTree.cc:170
TBranch * branchEntryInfoBranch() const
Definition: RootTree.h:119
std::map< BranchKey const, BranchInfo > BranchMap
Definition: RootTree.h:51
BranchType
Definition: BranchType.h:11
bool previous()
Definition: RootTree.h:82
RootTree & operator=(RootTree const &)=delete
TTree const * metaTree() const
Definition: RootTree.h:115
TTree const * tree() const
Definition: RootTree.h:113
tuple InputFile
Open Root file and provide MEs ############.
boost::shared_ptr< TTreeCache > triggerTreeCache_
Definition: RootTree.h:147
bool next()
Definition: RootTree.h:81
boost::shared_ptr< TTreeCache > rawTriggerTreeCache_
Definition: RootTree.h:148
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
TTreeCache * selectCache(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:321
BranchType branchType() const
Definition: RootTree.h:127
unsigned long treeAutoFlush_
Definition: RootTree.h:161
TTreeCache * checkTriggerCacheImpl(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:218
TBranch * auxBranch_
Definition: RootTree.h:141
EntryNumber const & entryNumber() const
Definition: RootTree.h:86
TBranch * provenanceBranch_
Definition: RootTree.h:48
bool enableTriggerCache_
Definition: RootTree.h:165
DelayedReader * rootDelayedReader() const
Definition: RootTree.cc:99
void close()
Definition: RootTree.cc:393
void getEntry(TBranch *branch, EntryNumber entry) const
Definition: RootTree.cc:340
void fillBranchEntry(TBranch *branch, T *&pbuf)
Definition: RootTree.h:108
void startTraining()
Definition: RootTree.cc:357
boost::shared_ptr< BranchMap > branches_
Definition: RootTree.h:154
bool isValid() const
Definition: RootTree.cc:87
TBranch * productBranch_
Definition: RootTree.h:47
void setCacheSize(unsigned int cacheSize)
Definition: RootTree.cc:160
bool performedSwitchOver_
Definition: RootTree.h:158
list key
Definition: combine.py:13
unsigned int cacheSize_
Definition: RootTree.h:160
unsigned int const defaultNonEventCacheSize
Definition: RootTree.h:36
Long64_t EntryNumber
Definition: RootTree.h:39
std::unordered_set< TBranch * > triggerSet_
Definition: RootTree.h:150
BranchMap const & branches() const
Definition: RootTree.cc:157
std::unique_ptr< DelayedReader > rootDelayedReader_
Definition: RootTree.h:166
boost::shared_ptr< InputFile > filePtr_
Definition: RootTree.h:134
void resetTraining()
Definition: RootTree.h:125
void rewind()
Definition: RootTree.h:84
std::unique_ptr< TTreeCache > trainCache(TTree *tree, InputFile &file, unsigned int cacheSize, char const *branchNames)
Definition: RootTree.cc:470
long double T
unsigned int const defaultLearningEntries
Definition: RootTree.h:37
EntryNumber switchOverEntry_
Definition: RootTree.h:156
void fillAux(T *&pAux)
Definition: RootTree.h:92
BranchType branchType_
Definition: RootTree.h:140
TTree * infoTree_
Definition: RootTree.h:170
void setEntryNumber(EntryNumber theEntryNumber)
Definition: RootTree.cc:175
roottree::EntryNumber EntryNumber
Definition: RootTree.h:60
bool current()
Definition: RootTree.h:83