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 "boost/shared_ptr.hpp"
18 #include "boost/utility.hpp"
19 
20 #include <memory>
21 #include <map>
22 #include <string>
23 #include <vector>
24 
25 class TBranch;
26 class TClass;
27 class TTree;
28 class TTreeCache;
29 
30 namespace edm {
31  struct BranchKey;
32  class FileFormatVersion;
33  class InputFile;
34  class RootDelayedReader;
35  class RootFile;
36  class RootTree;
37 
38  namespace roottree {
39  unsigned int const defaultCacheSize = 20U * 1024 * 1024;
40  unsigned int const defaultNonEventCacheSize = 1U * 1024 * 1024;
41  unsigned int const defaultLearningEntries = 20U;
42  unsigned int const defaultNonEventLearningEntries = 1U;
43  typedef Long64_t EntryNumber;
44  struct BranchInfo {
46  branchDescription_(prod),
47  productBranch_(0),
49  classCache_(0),
50  offsetToEDProduct_(0) {}
52  TBranch* productBranch_;
53  TBranch* provenanceBranch_; // For backward compatibility
54  mutable TClass* classCache_;
55  mutable Int_t offsetToEDProduct_;
56  };
57  typedef std::map<BranchKey const, BranchInfo> BranchMap;
58  Int_t getEntry(TBranch* branch, EntryNumber entryNumber);
59  Int_t getEntry(TTree* tree, EntryNumber entryNumber);
60  std::auto_ptr<TTreeCache> trainCache(TTree* tree, InputFile& file, unsigned int cacheSize, char const* branchNames);
61  }
62 
63  class RootTree : private boost::noncopyable {
64  public:
67  RootTree(boost::shared_ptr<InputFile> filePtr,
68  BranchType const& branchType,
69  unsigned int maxVirtualSize,
70  unsigned int cacheSize,
71  unsigned int learningEntries);
72  ~RootTree();
73 
74  bool isValid() const;
75  void addBranch(BranchKey const& key,
76  BranchDescription const& prod,
77  std::string const& oldBranchName);
78  void dropBranch(std::string const& oldBranchName);
79  void getEntry(TBranch *branch, EntryNumber entry) const;
80  void setPresence(BranchDescription const& prod);
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_;}
90  boost::shared_ptr<DelayedReader> makeDelayedReader(FileFormatVersion const& fileFormatVersion, boost::shared_ptr<RootFile> rootFilePtr = boost::shared_ptr<RootFile>()) const;
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  std::vector<ProductStatus> const& productStatuses() const {return productStatuses_;} // backward compatibility
118 
119  // below for backward compatibility
120  void fillStatus() { // backward compatibility
121  statusBranch_->SetAddress(&pProductStatuses_); // backward compatibility
122  roottree::getEntry(statusBranch_, entryNumber_); // backward compatibility
123  } // backward compatibility
124 
125  TBranch* const branchEntryInfoBranch() const {return branchEntryInfoBranch_;}
126  void trainCache(char const* branchNames);
127  void resetTraining() {trainNow_ = true;}
128 
129  private:
130  void setCacheSize(unsigned int cacheSize);
131  void setTreeMaxVirtualSize(int treeMaxVirtualSize);
132  void startTraining();
133  void stopTraining();
134 
135  boost::shared_ptr<InputFile> filePtr_;
136 // We use bare pointers for pointers to some ROOT entities.
137 // Root owns them and uses bare pointers internally.
138 // Therefore,using smart pointers here will do no good.
139  TTree* tree_;
140  TTree* metaTree_;
142  TBranch* auxBranch_;
144 // We use a smart pointer to own the TTreeCache.
145 // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
146 // So, we make sure to it is detached before closing the TFile so there is no double delete.
147  boost::shared_ptr<TTreeCache> treeCache_;
148  boost::shared_ptr<TTreeCache> rawTreeCache_;
151  std::vector<std::string> branchNames_;
152  boost::shared_ptr<BranchMap> branches_;
153  bool trainNow_;
155  unsigned int learningEntries_;
156  unsigned int cacheSize_;
157 
158  // below for backward compatibility
159  std::vector<ProductStatus> productStatuses_; // backward compatibility
160  std::vector<ProductStatus>* pProductStatuses_; // backward compatibility
161  TTree* infoTree_; // backward compatibility
162  TBranch* statusBranch_; // backward compatibility
163  };
164 }
165 #endif
EntryNumber entryNumber_
Definition: RootTree.h:150
TBranch * statusBranch_
Definition: RootTree.h:162
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: RootTree.cc:251
boost::shared_ptr< TTreeCache > treeCache_
Definition: RootTree.h:147
unsigned int const defaultNonEventLearningEntries
Definition: RootTree.h:42
unsigned int learningEntries_
Definition: RootTree.h:155
void addBranch(BranchKey const &key, BranchDescription const &prod, std::string const &oldBranchName)
Definition: RootTree.cc:87
void dropBranch(std::string const &oldBranchName)
Definition: RootTree.cc:107
ConstBranchDescription branchDescription_
Definition: RootTree.h:51
std::vector< std::string > branchNames_
Definition: RootTree.h:151
TBranch *const branchEntryInfoBranch() const
Definition: RootTree.h:125
std::vector< std::string > const & branchNames() const
Definition: RootTree.h:89
TTree * tree_
Definition: RootTree.h:139
bool trainNow_
Definition: RootTree.h:153
roottree::BranchMap BranchMap
Definition: RootTree.h:65
EntryNumber const & entries() const
Definition: RootTree.h:87
TTree * metaTree_
Definition: RootTree.h:140
TTree * tree()
Definition: RootTree.h:114
void stopTraining()
Definition: RootTree.cc:210
TBranch * branchEntryInfoBranch_
Definition: RootTree.h:143
EntryNumber entries_
Definition: RootTree.h:149
boost::shared_ptr< TTreeCache > rawTreeCache_
Definition: RootTree.h:148
BranchInfo(ConstBranchDescription const &prod)
Definition: RootTree.h:45
void fillBranchEntryMeta(TBranch *branch, T *&pbuf)
Definition: RootTree.h:97
void trainCache(char const *branchNames)
Definition: RootTree.cc:231
unsigned int const defaultCacheSize
Definition: RootTree.h:39
void setTreeMaxVirtualSize(int treeMaxVirtualSize)
Definition: RootTree.cc:149
std::map< BranchKey const, BranchInfo > BranchMap
Definition: RootTree.h:57
BranchType
Definition: BranchType.h:11
bool previous()
Definition: RootTree.h:82
TTree const * metaTree() const
Definition: RootTree.h:115
TTree const * tree() const
Definition: RootTree.h:113
tuple InputFile
Open Root file and provide MEs ############.
std::auto_ptr< TTreeCache > trainCache(TTree *tree, InputFile &file, unsigned int cacheSize, char const *branchNames)
Definition: RootTree.cc:275
bool next()
Definition: RootTree.h:81
RootTree(boost::shared_ptr< InputFile > filePtr, BranchType const &branchType, unsigned int maxVirtualSize, unsigned int cacheSize, unsigned int learningEntries)
Definition: RootTree.cc:33
std::vector< ProductStatus > const & productStatuses() const
Definition: RootTree.h:117
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
std::vector< ProductStatus > productStatuses_
Definition: RootTree.h:159
void fillStatus()
Definition: RootTree.h:120
TBranch * auxBranch_
Definition: RootTree.h:142
EntryNumber const & entryNumber() const
Definition: RootTree.h:86
TBranch * provenanceBranch_
Definition: RootTree.h:53
boost::shared_ptr< DelayedReader > makeDelayedReader(FileFormatVersion const &fileFormatVersion, boost::shared_ptr< RootFile > rootFilePtr=boost::shared_ptr< RootFile >()) const
Definition: RootTree.cc:133
void close()
Definition: RootTree.cc:217
void getEntry(TBranch *branch, EntryNumber entry) const
Definition: RootTree.cc:169
void fillBranchEntry(TBranch *branch, T *&pbuf)
Definition: RootTree.h:108
void startTraining()
Definition: RootTree.cc:186
boost::shared_ptr< BranchMap > branches_
Definition: RootTree.h:152
void setPresence(BranchDescription const &prod)
Definition: RootTree.cc:78
bool isValid() const
Definition: RootTree.cc:66
TBranch * productBranch_
Definition: RootTree.h:52
void setCacheSize(unsigned int cacheSize)
Definition: RootTree.cc:140
list key
Definition: combine.py:13
unsigned int cacheSize_
Definition: RootTree.h:156
unsigned int const defaultNonEventCacheSize
Definition: RootTree.h:40
Long64_t EntryNumber
Definition: RootTree.h:43
std::vector< ProductStatus > * pProductStatuses_
Definition: RootTree.h:160
BranchMap const & branches() const
Definition: RootTree.cc:130
boost::shared_ptr< InputFile > filePtr_
Definition: RootTree.h:135
void resetTraining()
Definition: RootTree.h:127
void rewind()
Definition: RootTree.h:84
long double T
unsigned int const defaultLearningEntries
Definition: RootTree.h:41
EntryNumber switchOverEntry_
Definition: RootTree.h:154
void fillAux(T *&pAux)
Definition: RootTree.h:92
BranchType branchType_
Definition: RootTree.h:141
TTree * infoTree_
Definition: RootTree.h:161
void setEntryNumber(EntryNumber theEntryNumber)
Definition: RootTree.cc:154
roottree::EntryNumber EntryNumber
Definition: RootTree.h:66
bool current()
Definition: RootTree.h:83