CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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_(0),
48  fileVersion_(-1),
49  dataHelper_(branchMap_->getRunTree(),
50  boost::shared_ptr<HistoryGetterBase>(new RunHistoryGetter(this)),
51  branchMap_)
52  {
53  if(0==iFile) {
54  throw cms::Exception("NoFile")<<"The TFile pointer passed to the constructor was null";
55  }
56 
57  if(0==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(0==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(0==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_ = boost::shared_ptr<edm::EDProductGetter>(new ProductGetter(this));
88 }
89 
90  Run::Run(boost::shared_ptr<BranchMapReader> branchMap):
91  branchMap_(branchMap),
92  pAux_(&aux_),
93  pOldAux_(0),
94  fileVersion_(-1),
95  dataHelper_(branchMap_->getRunTree(),
96  boost::shared_ptr<HistoryGetterBase>(new RunHistoryGetter(this)),
97  branchMap_)
98  {
99  if(0==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(0==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(0==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_ = boost::shared_ptr<edm::EDProductGetter>(new ProductGetter(this));
133 }
134 
136 {
137  for(std::vector<char const*>::iterator it = labels_.begin(), itEnd=labels_.end();
138  it != itEnd;
139  ++it) {
140  delete [] *it;
141  }
142  delete pOldAux_;
143 }
144 
145 //
146 // member functions
147 //
148 
149 const Run&
151 {
152  Long_t runIndex = branchMap_->getRunEntry();
153  if(runIndex < size())
154  {
155  branchMap_->updateRun(++runIndex);
156  }
157  return *this;
158 }
159 
160 
161 bool
163 {
166  if (entry == EntryFinder::invalidEntry) {
167  return false;
168  }
169  return branchMap_->updateRun(entry);
170 }
171 
172 const Run&
174 {
175  branchMap_->updateRun(0);
176  return *this;
177 }
178 
179 //
180 // const member functions
181 //
182 Long64_t
183 Run::size() const
184 {
185  return branchMap_->getRunTree()->GetEntries();
186 }
187 
188 bool
190 {
191  Long_t runIndex = branchMap_->getRunEntry();
192  return runIndex!=-1 and runIndex < size();
193 }
194 
195 
196 Run::operator bool() const
197 {
198  return isValid();
199 }
200 
201 bool
202 Run::atEnd() const
203 {
204  Long_t runIndex = branchMap_->getRunEntry();
205  return runIndex==-1 or runIndex == size();
206 }
207 
208 
209 std::string const
210 Run::getBranchNameFor(std::type_info const& iInfo,
211  char const* iModuleLabel,
212  char const* iProductInstanceLabel,
213  char const* iProcessLabel) const
214 {
215  return dataHelper_.getBranchNameFor(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel);
216 }
217 
218 bool
220  std::type_info const& iInfo,
221  char const* iModuleLabel,
222  char const* iProductInstanceLabel,
223  char const* iProcessLabel,
224  void* oData) const
225 {
226  if(atEnd()) {
227  throw cms::Exception("OffEnd")<<"You have requested data past the last run";
228  }
229  Long_t runIndex = branchMap_->getRunEntry();
230  return dataHelper_.getByLabel(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel, oData, runIndex);
231 }
232 
233 bool
234 Run::getByLabel(std::type_info const& iInfo,
235  char const* iModuleLabel,
236  char const* iProductInstanceLabel,
237  char const* iProcessLabel,
238  edm::WrapperHolder& holder) const {
239  if(atEnd()) {
240  throw cms::Exception("OffEnd") << "You have requested data past the last run";
241  }
242  Long_t runIndex = branchMap_->getRunEntry();
243  return dataHelper_.getByLabel(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel, holder, runIndex);
244 }
245 
246 edm::RunAuxiliary const&
248 {
249  Long_t runIndex = branchMap_->getRunEntry();
250  updateAux(runIndex);
251  return aux_;
252 }
253 
254 void
255 Run::updateAux(Long_t runIndex) const
256 {
257  if(auxBranch_->GetEntryNumber() != runIndex) {
258  auxBranch_->GetEntry(runIndex);
259  //handling dealing with old version
260  if(0 != pOldAux_) {
262  }
263  }
264 }
265 
266 const edm::ProcessHistory&
268 {
269  edm::ProcessHistoryID processHistoryID;
270 
271  bool newFormat = false;//(fileVersion_ >= 5);
272 
273  Long_t runIndex = branchMap_->getRunEntry();
274  updateAux(runIndex);
275  if (!newFormat) {
276  processHistoryID = aux_.processHistoryID();
277  }
278 
279  if(historyMap_.empty() || newFormat) {
280  procHistoryNames_.clear();
281  TTree *meta = dynamic_cast<TTree*>(branchMap_->getFile()->Get(edm::poolNames::metaDataTreeName().c_str()));
282  if(0==meta) {
283  throw cms::Exception("NoMetaTree")<<"The TFile does not appear to contain a TTree named "
285  }
286  if (historyMap_.empty()) {
287  if (fileVersion_ < 11) {
289  TBranch* b = meta->GetBranch(edm::poolNames::processHistoryMapBranchName().c_str());
290  b->SetAddress(&pPhm);
291  b->GetEntry(0);
292  } else {
293  edm::ProcessHistoryVector historyVector;
294  edm::ProcessHistoryVector* pPhv=&historyVector;
295  TBranch* b = meta->GetBranch(edm::poolNames::processHistoryBranchName().c_str());
296  b->SetAddress(&pPhv);
297  b->GetEntry(0);
298  for (edm::ProcessHistoryVector::const_iterator i = historyVector.begin(), e = historyVector.end();
299  i != e; ++i) {
300  historyMap_.insert(std::make_pair(i->id(), *i));
301  }
302  }
303  }
304 // if (newFormat) {
305 // if (fileVersion_ >= 7) {
306 // edm::History history;
307 // edm::History* pHistory = &history;
308 // TBranch* eventHistoryBranch = eventHistoryTree_->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
309 // if (!eventHistoryBranch)
310 // throw edm::Exception(edm::errors::FatalRootError)
311 // << "Failed to find history branch in event history tree";
312 // eventHistoryBranch->SetAddress(&pHistory);
313 // eventHistoryTree_->GetEntry(runIndex);
314 // processHistoryID = history.processHistoryID();
315 // } else {
316 // std::vector<edm::EventProcessHistoryID> *pEventProcessHistoryIDs = &eventProcessHistoryIDs_;
317 // TBranch* b = meta->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
318 // b->SetAddress(&pEventProcessHistoryIDs);
319 // b->GetEntry(0);
320 // edm::EventProcessHistoryID target(aux_.id(), edm::ProcessHistoryID());
321 // processHistoryID = std::lower_bound(eventProcessHistoryIDs_.begin(), eventProcessHistoryIDs_.end(), target)->processHistoryID_;
322 // }
323 // }
324 
325  }
326  return historyMap_[processHistoryID];
327 }
328 
329 
332 {
333  Long_t runIndex = branchMap_->getRunEntry();
334  return dataHelper_.getByProductID(iID, runIndex);
335 }
336 
337 
338 //
339 // static member functions
340 //
341 void
342 Run::throwProductNotFoundException(std::type_info const& iType, char const* iModule, char const* iProduct, char const* iProcess)
343 {
344  edm::TypeID type(iType);
345  throw edm::Exception(edm::errors::ProductNotFound)<<"A branch was found for \n type ='"<<type.className()<<"'\n module='"<<iModule
346  <<"'\n productInstance='"<<((0!=iProduct)?iProduct:"")<<"'\n process='"<<((0!=iProcess)?iProcess:"")<<"'\n"
347  "but no data is available for this Run";
348 }
349 }
type
Definition: HCALResponse.h:22
TBranch * auxBranch_
Definition: Run.h:128
int i
Definition: DBlmapReader.cc:9
edm::RunAuxiliary * pAux_
Definition: Run.h:126
bool isValid() const
Definition: Run.cc:189
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const &branchType)
Definition: BranchType.cc:114
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::EventIDconst &, edm::Timestampconst & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
void updateAux(Long_t runIndex) const
Definition: Run.cc:255
Run(TFile *iFile)
Definition: Run.cc:44
int fileVersion_
Definition: Run.h:129
edm::WrapperHolder getByProductID(edm::ProductID const &) const
Definition: Run.cc:331
virtual std::string const getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const
ProcessHistoryRegistry::collection_type ProcessHistoryMap
EntryFinder entryFinder_
Definition: Run.h:125
virtual ~Run()
Definition: Run.cc:135
virtual edm::RunAuxiliary const & runAuxiliary() const
Definition: Run.cc:247
virtual std::string const getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const
Definition: Run.cc:210
std::string const & processHistoryMapBranchName()
Definition: BranchType.cc:192
std::vector< std::string > procHistoryNames_
Definition: Run.h:123
edm::IndexIntoFile::EntryNumber_t EntryNumber_t
Definition: EntryFinder.h:34
const edm::ProcessHistory & history() const
Definition: Run.cc:267
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
edm::RunAux * pOldAux_
Definition: Run.h:127
ProcessHistoryRegistry::vector_type ProcessHistoryVector
void fillIndex(BranchMapReader const &branchMap)
Definition: EntryFinder.cc:108
std::string const & metaDataTreeName()
Definition: BranchType.cc:167
std::string const & runTreeName()
Definition: BranchType.cc:268
EntryNumber_t findRun(edm::RunNumber_t const &run) const
Definition: EntryFinder.cc:91
std::string className() const
Definition: TypeID.cc:51
static EntryNumber_t const invalidEntry
Definition: EntryFinder.h:40
std::string const & processHistoryBranchName()
Definition: BranchType.cc:197
edm::RunAuxiliary aux_
Definition: Run.h:124
static void throwProductNotFoundException(std::type_info const &, char const *, char const *, char const *)
Definition: Run.cc:342
void conversion(EventAux const &from, EventAuxiliary &to)
Definition: EventAux.cc:9
const Run & operator++()
Definition: Run.cc:150
const Run & toBegin()
Definition: Run.cc:173
virtual bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *) const
Definition: Run.cc:219
fwlite::DataGetterHelper dataHelper_
Definition: Run.h:131
double b
Definition: hdecay.h:120
ProcessHistoryID const & processHistoryID() const
Definition: RunAuxiliary.h:35
virtual bool atEnd() const
Definition: Run.cc:202
virtual bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *, Long_t) const
edm::ProcessHistoryMap historyMap_
Definition: Run.h:122
std::vector< char const * > labels_
Definition: Run.h:121
boost::shared_ptr< BranchMapReader > branchMap_
Definition: Run.h:118
unsigned int RunNumber_t
Definition: EventRange.h:32
Long64_t size() const
Definition: Run.cc:183
edm::WrapperHolder getByProductID(edm::ProductID const &, Long_t) const
bool to(edm::RunNumber_t run)
Go to event by Run &amp; Run number.
Definition: Run.cc:162