CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ChainEvent.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWLite
4 // Class : ChainEvent
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Sat Jun 16 06:48:39 EDT 2007
11 //
12 
13 // system include files
14 
15 // user include files
19 #include "TFile.h"
20 #include "TTree.h"
21 #include "TROOT.h"
22 
23 namespace fwlite {
24 //
25 // constants, enums and typedefs
26 //
27 
28 //
29 // static data member definitions
30 //
31 
32 //
33 // constructors and destructor
34 //
35 ChainEvent::ChainEvent(std::vector<std::string> const& iFileNames):
36  fileNames_(),
37  file_(),
38  event_(),
39  eventIndex_(0),
40  accumulatedSize_()
41 {
42  Long64_t summedSize=0;
43  accumulatedSize_.reserve(iFileNames.size()+1);
44  fileNames_.reserve(iFileNames.size());
45 
46  for (std::vector<std::string>::const_iterator it= iFileNames.begin(), itEnd = iFileNames.end();
47  it!=itEnd;
48  ++it) {
49  TFile *tfilePtr = TFile::Open(it->c_str());
50  file_ = boost::shared_ptr<TFile>(tfilePtr);
51  gROOT->GetListOfFiles()->Remove(tfilePtr);
52  TTree* tree = dynamic_cast<TTree*>(file_->Get(edm::poolNames::eventTreeName().c_str()));
53  if (0 == tree) {
54  throw cms::Exception("NotEdmFile")<<"The file "<<*it<<" has no 'Events' TTree and therefore is not an EDM ROOT file";
55  }
56  Long64_t nEvents = tree->GetEntries();
57  if (nEvents > 0) { // skip empty files
58  fileNames_.push_back(*it);
59  // accumulatedSize_ is the entry # at the beginning of this file
60  accumulatedSize_.push_back(summedSize);
61  summedSize += nEvents;
62  }
63  }
64  // total accumulated size (last enry + 1) at the end of last file
65  accumulatedSize_.push_back(summedSize);
66 
67  if (fileNames_.size() > 0)
68  switchToFile(0);
69 }
70 
71 // ChainEvent::ChainEvent(ChainEvent const& rhs)
72 // {
73 // // do actual copying here;
74 // }
75 
77 {
78 }
79 
80 //
81 // assignment operators
82 //
83 // ChainEvent const& ChainEvent::operator=(ChainEvent const& rhs)
84 // {
85 // //An exception safe implementation is
86 // ChainEvent temp(rhs);
87 // swap(rhs);
88 //
89 // return *this;
90 // }
91 
92 //
93 // member functions
94 //
95 
96 ChainEvent const&
98 {
99  if(eventIndex_ != static_cast<Long64_t>(fileNames_.size())-1)
100  {
101  ++(*event_);
102  if(event_->atEnd()) {
104  }
105  } else {
106  if(*event_) {
107  ++(*event_);
108  }
109  }
110  return *this;
111 }
112 
114 bool
115 ChainEvent::to(Long64_t iIndex)
116 {
117  if (iIndex >= accumulatedSize_.back())
118  {
119  // if we're here, then iIndex was not valid
120  return false;
121  }
122 
123  Long64_t offsetIndex = eventIndex_;
124 
125  // we're going backwards, so start from the beginning
126  if (iIndex < accumulatedSize_[offsetIndex]) {
127  offsetIndex = 0;
128  }
129 
130  // is it past the end of this file?
131  while (iIndex >= accumulatedSize_[offsetIndex+1]) {
132  ++offsetIndex;
133  }
134 
135  if(offsetIndex != eventIndex_) {
136  switchToFile(eventIndex_ = offsetIndex);
137  }
138 
139  // adjust to entry # in this file
140  return event_->to(iIndex-accumulatedSize_[offsetIndex]);
141 }
142 
143 
145 bool
147 {
148  return to(id.run(), id.luminosityBlock(), id.event());
149 }
150 
153 bool
155 {
156 
157  // First try this file
158  if (event_->to(run, lumi, event))
159  {
160  // found it, return
161  return true;
162  }
163  else
164  {
165  // Did not find it, try the other files sequentially.
166  // Someday I can make this smarter. For now... we get something working.
167  Long64_t thisFile = eventIndex_;
168  std::vector<std::string>::const_iterator filesBegin = fileNames_.begin(),
169  filesEnd = fileNames_.end(), ifile = filesBegin;
170  for (; ifile != filesEnd; ++ifile)
171  {
172  // skip the "first" file that we tried
173  if (ifile - filesBegin != thisFile)
174  {
175  // switch to the next file
176  switchToFile(ifile - filesBegin);
177  // check that tree for the desired event
178  if (event_->to(run, lumi, event))
179  {
180  // found it, return
181  return true;
182  }
183  }// end ignore "first" file that we tried
184  }// end loop over files
185 
186  // did not find the event with id "id".
187  return false;
188  }// end if we did not find event id in "first" file
189 }
190 
191 bool
193 {
194  return to(run, 0U, event);
195 }
196 
199 ChainEvent const&
201 {
202  if (eventIndex_ != 0)
203  {
204  switchToFile(0);
205  }
206  event_->toBegin();
207  return *this;
208 }
209 
210 void
211 ChainEvent::switchToFile(Long64_t iIndex)
212 {
213  eventIndex_= iIndex;
214  TFile *tfilePtr = TFile::Open(fileNames_[iIndex].c_str());
215  file_ = boost::shared_ptr<TFile>(tfilePtr);
216  gROOT->GetListOfFiles()->Remove(tfilePtr);
217  event_ = boost::shared_ptr<Event>(new Event(file_.get()));
218 }
219 
220 //
221 // const member functions
222 //
223 std::string const
224 ChainEvent::getBranchNameFor(std::type_info const& iType,
225  char const* iModule,
226  char const* iInstance,
227  char const* iProcess) const
228 {
229  return event_->getBranchNameFor(iType,iModule,iInstance,iProcess);
230 }
231 
232 std::vector<edm::BranchDescription> const&
234 {
235  return event_->getBranchDescriptions();
236 }
237 
238 std::vector<std::string> const&
240 {
241  return event_->getProcessHistory();
242 }
243 
244 edm::ProcessHistory const&
246 {
247  return event_->processHistory();
248 }
249 
250 edm::EventAuxiliary const&
252 {
253  return event_->eventAuxiliary();
254 }
255 
257 {
258  return event_->getLuminosityBlock();
259 }
260 
262 {
263  return event_->getRun();
264 }
265 
266 bool
267 ChainEvent::getByLabel(std::type_info const& iType,
268  char const* iModule,
269  char const* iInstance,
270  char const* iProcess,
271  void* iValue) const
272 {
273  return event_->getByLabel(iType, iModule, iInstance, iProcess, iValue);
274 }
275 
276 bool
277 ChainEvent::getByLabel(std::type_info const& iType,
278  char const* iModule,
279  char const* iInstance,
280  char const* iProcess,
281  edm::WrapperHolder& holder) const
282 {
283  return event_->getByLabel(iType, iModule, iInstance, iProcess, holder);
284 }
285 
287 {
288  return event_->getByProductID(iID);
289 }
290 
291 bool
293 {
294  return event_->isValid();
295 }
297 {
298  return *event_;
299 }
300 
301 bool
303 {
304  if (eventIndex_ == static_cast<Long64_t>(fileNames_.size())-1) {
305  return event_->atEnd();
306  }
307  return false;
308 }
309 
310 Long64_t
312 {
313  return accumulatedSize_.back();
314 }
315 
316 edm::TriggerNames const&
318 {
319  return event_->triggerNames(triggerResults);
320 }
321 
322 void
324 {
325  event_->fillParameterSetRegistry();
326 }
327 
329 ChainEvent::triggerResultsByName(std::string const& process) const {
330  return event_->triggerResultsByName(process);
331 }
332 
333 //
334 // static member functions
335 //
336 void
337 ChainEvent::throwProductNotFoundException(std::type_info const& iType,
338  char const* iModule,
339  char const* iInstance,
340  char const* iProcess) {
341  Event::throwProductNotFoundException(iType,iModule,iInstance,iProcess);
342 }
343 }
virtual edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const
Definition: ChainEvent.cc:317
virtual edm::TriggerResultsByName triggerResultsByName(std::string const &process) const
Definition: ChainEvent.cc:329
Long64_t size() const
Definition: ChainEvent.cc:311
unsigned int EventNumber_t
Definition: EventID.h:30
virtual edm::EventAuxiliary const & eventAuxiliary() const
Definition: ChainEvent.cc:251
void switchToFile(Long64_t)
Definition: ChainEvent.cc:211
tuple lumi
Definition: fjr2json.py:35
edm::WrapperHolder getByProductID(edm::ProductID const &) const
Definition: ChainEvent.cc:286
virtual bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *) const
Definition: ChainEvent.cc:267
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:59
Long64_t eventIndex_
Definition: ChainEvent.h:130
virtual ~ChainEvent()
Definition: ChainEvent.cc:76
unsigned int LuminosityBlockNumber_t
Definition: EventID.h:31
static void throwProductNotFoundException(std::type_info const &, char const *, char const *, char const *)
Definition: ChainEvent.cc:337
virtual std::string const getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const
Definition: ChainEvent.cc:224
fwlite::LuminosityBlock const & getLuminosityBlock()
Definition: ChainEvent.cc:256
void fillParameterSetRegistry() const
Definition: ChainEvent.cc:323
std::vector< std::string > const & getProcessHistory() const
Definition: ChainEvent.cc:239
boost::shared_ptr< Event > event_
Definition: ChainEvent.h:129
fwlite::Run const & getRun()
Definition: ChainEvent.cc:261
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
bool to(Long64_t iIndex)
Go to the event at index iIndex.
Definition: ChainEvent.cc:115
virtual bool atEnd() const
Definition: ChainEvent.cc:302
std::vector< Long64_t > accumulatedSize_
Definition: ChainEvent.h:131
std::vector< edm::BranchDescription > const & getBranchDescriptions() const
Definition: ChainEvent.cc:233
ChainEvent const & toBegin()
Definition: ChainEvent.cc:200
ChainEvent const & operator++()
Definition: ChainEvent.cc:97
void event_()
static void throwProductNotFoundException(std::type_info const &, char const *, char const *, char const *)
Definition: Event.cc:485
std::string const & eventTreeName()
Definition: BranchType.cc:254
unsigned int RunNumber_t
Definition: EventRange.h:32
virtual edm::ProcessHistory const & processHistory() const
Definition: ChainEvent.cc:245
UInt_t nEvents
Definition: hcalCalib.cc:43
tuple process
Definition: LaserDQM_cfg.py:3
Event const * event() const
Definition: ChainEvent.h:101
bool isValid() const
Definition: ChainEvent.cc:292
boost::shared_ptr< TFile > file_
Definition: ChainEvent.h:128
std::vector< std::string > fileNames_
Definition: ChainEvent.h:127
ChainEvent(std::vector< std::string > const &iFileNames)
Definition: ChainEvent.cc:35