CMS 3D CMS Logo

Run.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: DataFormats/FWLite
4 // Class : Run
5 //
14 //
15 // Original Author: Eric Vaandering
16 // Created: Wed Jan 13 15:01:20 EDT 2007
17 //
18 
19 // system include files
20 #include <iostream>
21 
22 // user include files
24 #include "TFile.h"
25 #include "TTree.h"
29 
32 
33 //used for backwards compatability
35 
36 //
37 // constants, enums and typedefs
38 //
39 namespace fwlite {
40 
41 //
42 // constructors and destructor
43 //
44  Run::Run(TFile* iFile):
45  branchMap_(new BranchMapReader(iFile)),
46  pAux_(&aux_),
47  pOldAux_(nullptr),
48  fileVersion_(-1),
49  dataHelper_(branchMap_->getRunTree(),
50  std::make_shared<RunHistoryGetter>(this),
51  branchMap_)
52  {
53  if(nullptr == iFile) {
54  throw cms::Exception("NoFile")<<"The TFile pointer passed to the constructor was null";
55  }
56 
57  if(nullptr == branchMap_->getRunTree()) {
58  throw cms::Exception("NoRunTree")<<"The TFile contains no TTree named " <<edm::poolNames::runTreeName();
59  }
60  //need to know file version in order to determine how to read the basic product info
61  fileVersion_ = branchMap_->getFileVersion(iFile);
62 
63  //got this logic from IOPool/Input/src/RootFile.cc
64 
65  TTree* runTree = branchMap_->getRunTree();
66  if(fileVersion_ >= 3) {
67  auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InRun).c_str());
68  if(nullptr == auxBranch_) {
69  throw cms::Exception("NoRunAuxilliary")<<"The TTree "
71  <<" does not contain a branch named 'RunAuxiliary'";
72  }
73  auxBranch_->SetAddress(&pAux_);
74  } else {
75  throw cms::Exception("OldFileVersion")<<"The FWLite Run code des not support old file versions";
76 // This code commented from fwlite::Event. May be portable if needed.
77 // pOldAux_ = new edm::EventAux();
78 // auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxBranchName(edm::InRun).c_str());
79 // if(nullptr == auxBranch_) {
80 // throw cms::Exception("NoRunAux")<<"The TTree "
81 // <<edm::poolNames::runTreeName()
82 // <<" does not contain a branch named 'RunAux'";
83 // }
84 // auxBranch_->SetAddress(&pOldAux_);
85  }
86  branchMap_->updateRun(0);
87 // getter_ = std::make_shared<ProductGetter>(this);
88 }
89 
90  Run::Run(std::shared_ptr<BranchMapReader> branchMap):
91  branchMap_(branchMap),
92  pAux_(&aux_),
94  fileVersion_(-1),
95  dataHelper_(branchMap_->getRunTree(),
96  std::make_shared<RunHistoryGetter>(this),
97  branchMap_)
98  {
99  if(nullptr == branchMap_->getRunTree()) {
100  throw cms::Exception("NoRunTree")<<"The TFile contains no TTree named " <<edm::poolNames::runTreeName();
101  }
102  //need to know file version in order to determine how to read the basic event info
103  fileVersion_ = branchMap_->getFileVersion();
104  //got this logic from IOPool/Input/src/RootFile.cc
105 
106  TTree* runTree = branchMap_->getRunTree();
107  if(fileVersion_ >= 3) {
108  auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InRun).c_str());
109  if(nullptr == auxBranch_) {
110  throw cms::Exception("NoRunAuxilliary")<<"The TTree "
112  <<" does not contain a branch named 'RunAuxiliary'";
113  }
114  auxBranch_->SetAddress(&pAux_);
115  } else {
116  throw cms::Exception("OldFileVersion")<<"The FWLite Run code des not support old file versions";
117 /* pOldAux_ = new edm::EventAux();
118  auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxBranchName(edm::InRun).c_str());
119  if(nullptr == auxBranch_) {
120  throw cms::Exception("NoRunAux")<<"The TTree "
121  <<edm::poolNames::runTreeName()
122  <<" does not contain a branch named 'RunAux'";
123  }
124  auxBranch_->SetAddress(&pOldAux_);*/
125  }
126  branchMap_->updateRun(0);
127 
128 // if(fileVersion_ >= 7) {
129 // eventHistoryTree_ = dynamic_cast<TTree*>(iFile->Get(edm::poolNames::eventHistoryTreeName().c_str()));
130 // }
131 
132 // getter_ = std::make_shared<ProductGetter>(this);
133 }
134 
136 {
137  for(auto const& label : labels_) {
138  delete [] label;
139  }
140  delete pOldAux_;
141 }
142 
143 //
144 // member functions
145 //
146 
147 const Run&
149 {
150  Long_t runIndex = branchMap_->getRunEntry();
151  if(runIndex < size())
152  {
153  branchMap_->updateRun(++runIndex);
154  }
155  return *this;
156 }
157 
158 
159 bool
161 {
164  if (entry == EntryFinder::invalidEntry) {
165  return false;
166  }
167  return branchMap_->updateRun(entry);
168 }
169 
170 const Run&
172 {
173  branchMap_->updateRun(0);
174  return *this;
175 }
176 
177 //
178 // const member functions
179 //
180 Long64_t
181 Run::size() const
182 {
183  return branchMap_->getRunTree()->GetEntries();
184 }
185 
186 bool
188 {
189  Long_t runIndex = branchMap_->getRunEntry();
190  return runIndex!=-1 and runIndex < size();
191 }
192 
193 
195 {
196  return isValid();
197 }
198 
199 bool
200 Run::atEnd() const
201 {
202  Long_t runIndex = branchMap_->getRunEntry();
203  return runIndex==-1 or runIndex == size();
204 }
205 
206 
207 std::string const
208 Run::getBranchNameFor(std::type_info const& iInfo,
209  char const* iModuleLabel,
210  char const* iProductInstanceLabel,
211  char const* iProcessLabel) const
212 {
213  return dataHelper_.getBranchNameFor(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel);
214 }
215 
216 bool
218  std::type_info const& iInfo,
219  char const* iModuleLabel,
220  char const* iProductInstanceLabel,
221  char const* iProcessLabel,
222  void* oData) const
223 {
224  if(atEnd()) {
225  throw cms::Exception("OffEnd")<<"You have requested data past the last run";
226  }
227  Long_t runIndex = branchMap_->getRunEntry();
228  return dataHelper_.getByLabel(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel, oData, runIndex);
229 }
230 
231 edm::RunAuxiliary const&
233 {
234  Long_t runIndex = branchMap_->getRunEntry();
235  updateAux(runIndex);
236  return aux_;
237 }
238 
239 void
240 Run::updateAux(Long_t runIndex) const
241 {
242  if(auxBranch_->GetEntryNumber() != runIndex) {
243  auxBranch_->GetEntry(runIndex);
244  //handling dealing with old version
245  if(nullptr != pOldAux_) {
247  }
248  }
249 }
250 
251 const edm::ProcessHistory&
253 {
254  edm::ProcessHistoryID processHistoryID;
255 
256  bool newFormat = false;//(fileVersion_ >= 5);
257 
258  Long_t runIndex = branchMap_->getRunEntry();
259  updateAux(runIndex);
260  if (!newFormat) {
261  processHistoryID = aux_.processHistoryID();
262  }
263 
264  if(historyMap_.empty() || newFormat) {
265  procHistoryNames_.clear();
266  TTree *meta = dynamic_cast<TTree*>(branchMap_->getFile()->Get(edm::poolNames::metaDataTreeName().c_str()));
267  if(nullptr == meta) {
268  throw cms::Exception("NoMetaTree")<<"The TFile does not appear to contain a TTree named "
270  }
271  if (historyMap_.empty()) {
272  if (fileVersion_ < 11) {
274  TBranch* b = meta->GetBranch(edm::poolNames::processHistoryMapBranchName().c_str());
275  b->SetAddress(&pPhm);
276  b->GetEntry(0);
277  } else {
278  edm::ProcessHistoryVector historyVector;
279  edm::ProcessHistoryVector* pPhv=&historyVector;
280  TBranch* b = meta->GetBranch(edm::poolNames::processHistoryBranchName().c_str());
281  b->SetAddress(&pPhv);
282  b->GetEntry(0);
283  for (auto& history : historyVector) {
284  historyMap_.insert(std::make_pair(history.setProcessHistoryID(), history));
285  }
286  }
287  }
288 // if (newFormat) {
289 // if (fileVersion_ >= 7) {
290 // edm::History history;
291 // edm::History* pHistory = &history;
292 // TBranch* eventHistoryBranch = eventHistoryTree_->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
293 // if (!eventHistoryBranch)
294 // throw edm::Exception(edm::errors::FatalRootError)
295 // << "Failed to find history branch in event history tree";
296 // eventHistoryBranch->SetAddress(&pHistory);
297 // eventHistoryTree_->GetEntry(runIndex);
298 // processHistoryID = history.processHistoryID();
299 // } else {
300 // std::vector<edm::EventProcessHistoryID> *pEventProcessHistoryIDs = &eventProcessHistoryIDs_;
301 // TBranch* b = meta->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
302 // b->SetAddress(&pEventProcessHistoryIDs);
303 // b->GetEntry(0);
304 // edm::EventProcessHistoryID target(aux_.id(), edm::ProcessHistoryID());
305 // processHistoryID = std::lower_bound(eventProcessHistoryIDs_.begin(), eventProcessHistoryIDs_.end(), target)->processHistoryID_;
306 // }
307 // }
308 
309  }
310  return historyMap_[processHistoryID];
311 }
312 
313 
314 edm::WrapperBase const*
316 {
317  Long_t runIndex = branchMap_->getRunEntry();
318  return dataHelper_.getByProductID(iID, runIndex);
319 }
320 
321 
322 //
323 // static member functions
324 //
325 void
326 Run::throwProductNotFoundException(std::type_info const& iType, char const* iModule, char const* iProduct, char const* iProcess)
327 {
328  edm::TypeID type(iType);
329  throw edm::Exception(edm::errors::ProductNotFound)<<"A branch was found for \n type ='"<<type.className()<<"'\n module='"<<iModule
330  <<"'\n productInstance='"<<((nullptr != iProduct)?iProduct:"")<<"'\n process='"<<((nullptr != iProcess)?iProcess:"")<<"'\n"
331  "but no data is available for this Run";
332 }
333 }
std::vector< ProcessHistory > ProcessHistoryVector
edm::RunAuxiliary const * pAux_
Definition: Run.h:123
type
Definition: HCALResponse.h:21
TBranch * auxBranch_
Definition: Run.h:125
bool isValid() const
Definition: Run.cc:187
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const &branchType)
Definition: BranchType.cc:116
RunNumber_t run() const
Definition: RunBase.h:40
bool atEnd() const override
Definition: Run.cc:200
void updateAux(Long_t runIndex) const
Definition: Run.cc:240
Run(TFile *iFile)
Definition: Run.cc:44
int fileVersion_
Definition: Run.h:126
#define nullptr
virtual std::string const getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const
ProcessHistoryID setProcessHistoryID()
EntryFinder entryFinder_
Definition: Run.h:122
~Run() override
Definition: Run.cc:135
char const * label
virtual std::string const getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const
Definition: Run.cc:208
std::string const & processHistoryMapBranchName()
Definition: BranchType.cc:194
std::vector< std::string > procHistoryNames_
Definition: Run.h:120
edm::WrapperBase const * getByProductID(edm::ProductID const &pid, Long_t eventEntry) const
const Run & toBegin() override
Definition: Run.cc:171
edm::IndexIntoFile::EntryNumber_t EntryNumber_t
Definition: EntryFinder.h:33
const edm::ProcessHistory & history() const
Definition: Run.cc:252
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
std::string const & metaDataTreeName()
Definition: BranchType.cc:169
std::string const & runTreeName()
Definition: BranchType.cc:280
EntryNumber_t findRun(edm::RunNumber_t const &run) const
Definition: EntryFinder.cc:91
void fillIndex(BranchMapReader &branchMap)
Definition: EntryFinder.cc:108
static EntryNumber_t const invalidEntry
Definition: EntryFinder.h:39
std::string const & processHistoryBranchName()
Definition: BranchType.cc:199
edm::RunAux const * pOldAux_
Definition: Run.h:124
bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *) const override
Definition: Run.cc:217
edm::RunAuxiliary aux_
Definition: Run.h:121
edm::WrapperBase const * getByProductID(edm::ProductID const &) const
Definition: Run.cc:315
static void throwProductNotFoundException(std::type_info const &, char const *, char const *, char const *)
Definition: Run.cc:326
void conversion(EventAux const &from, EventAuxiliary &to)
Definition: EventAux.cc:9
std::shared_ptr< BranchMapReader > branchMap_
Definition: Run.h:115
fwlite::DataGetterHelper dataHelper_
Definition: Run.h:128
double b
Definition: hdecay.h:120
ProcessHistoryID const & processHistoryID() const
Definition: RunAuxiliary.h:35
virtual bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *, Long_t) const
edm::ProcessHistoryMap historyMap_
Definition: Run.h:119
std::vector< char const * > labels_
Definition: Run.h:118
std::string const & className() const
Definition: TypeID.cc:43
const Run & operator++() override
Definition: Run.cc:148
edm::RunAuxiliary const & runAuxiliary() const override
Definition: Run.cc:232
unsigned int RunNumber_t
Long64_t size() const
Definition: Run.cc:181
std::map< ProcessHistoryID, ProcessHistory > ProcessHistoryMap
bool to(edm::RunNumber_t run)
Go to event by Run & Run number.
Definition: Run.cc:160