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
18 #include "TFile.h"
19 #include "TTree.h"
20 #include "TROOT.h"
21 
22 namespace fwlite {
23 //
24 // constants, enums and typedefs
25 //
26 
27 //
28 // static data member definitions
29 //
30 
31 //
32 // constructors and destructor
33 //
34 ChainEvent::ChainEvent(const std::vector<std::string>& iFileNames):
35  fileNames_(),
36  file_(),
37  event_(),
38  eventIndex_(0),
39  accumulatedSize_()
40 {
41  Long64_t summedSize=0;
42  accumulatedSize_.reserve(iFileNames.size()+1);
43  fileNames_.reserve(iFileNames.size());
44 
45  for (std::vector<std::string>::const_iterator it= iFileNames.begin(), itEnd = iFileNames.end();
46  it!=itEnd;
47  ++it) {
48  TFile *tfilePtr = TFile::Open(it->c_str());
49  file_ = boost::shared_ptr<TFile>(tfilePtr);
50  gROOT->GetListOfFiles()->Remove(tfilePtr);
51  TTree* tree = dynamic_cast<TTree*>(file_->Get(edm::poolNames::eventTreeName().c_str()));
52  if (0 == tree) {
53  throw cms::Exception("NotEdmFile")<<"The file "<<*it<<" has no 'Events' TTree and therefore is not an EDM ROOT file";
54  }
55  Long64_t nEvents = tree->GetEntries();
56  if (nEvents > 0) { // skip empty files
57  fileNames_.push_back(*it);
58  // accumulatedSize_ is the entry # at the beginning of this file
59  accumulatedSize_.push_back(summedSize);
60  summedSize += nEvents;
61  }
62  }
63  // total accumulated size (last enry + 1) at the end of last file
64  accumulatedSize_.push_back(summedSize);
65 
66  if (fileNames_.size() > 0)
67  switchToFile(0);
68 }
69 
70 // ChainEvent::ChainEvent(const ChainEvent& rhs)
71 // {
72 // // do actual copying here;
73 // }
74 
76 {
77 }
78 
79 //
80 // assignment operators
81 //
82 // const ChainEvent& ChainEvent::operator=(const ChainEvent& rhs)
83 // {
84 // //An exception safe implementation is
85 // ChainEvent temp(rhs);
86 // swap(rhs);
87 //
88 // return *this;
89 // }
90 
91 //
92 // member functions
93 //
94 
95 const ChainEvent&
97 {
98  if(eventIndex_ != static_cast<Long64_t>(fileNames_.size())-1)
99  {
100  ++(*event_);
101  if(event_->atEnd()) {
103  }
104  } else {
105  if(*event_) {
106  ++(*event_);
107  }
108  }
109  return *this;
110 }
111 
113 bool
114 ChainEvent::to(Long64_t iIndex)
115 {
116  if (iIndex >= accumulatedSize_.back())
117  {
118  // if we're here, then iIndex was not valid
119  return false;
120  }
121 
122  Long64_t offsetIndex = eventIndex_;
123 
124  // we're going backwards, so start from the beginning
125  if (iIndex < accumulatedSize_[offsetIndex]) {
126  offsetIndex = 0;
127  }
128 
129  // is it past the end of this file?
130  while (iIndex >= accumulatedSize_[offsetIndex+1]) {
131  ++offsetIndex;
132  }
133 
134  if(offsetIndex != eventIndex_) {
135  switchToFile(eventIndex_ = offsetIndex);
136  }
137 
138  // adjust to entry # in this file
139  return event_->to( iIndex-accumulatedSize_[offsetIndex] );
140 }
141 
142 
144 bool
146 {
147  return to(id.run(), id.luminosityBlock(), id.event());
148 }
149 
152 bool
154 {
155 
156  // First try this file
157  if ( event_->to( run, lumi, event ) )
158  {
159  // found it, return
160  return true;
161  }
162  else
163  {
164  // Did not find it, try the other files sequentially.
165  // Someday I can make this smarter. For now... we get something working.
166  Long64_t thisFile = eventIndex_;
167  std::vector<std::string>::const_iterator filesBegin = fileNames_.begin(),
168  filesEnd = fileNames_.end(), ifile = filesBegin;
169  for ( ; ifile != filesEnd; ++ifile )
170  {
171  // skip the "first" file that we tried
172  if ( ifile - filesBegin != thisFile )
173  {
174  // switch to the next file
175  switchToFile( ifile - filesBegin );
176  // check that tree for the desired event
177  if ( event_->to( run, lumi, event ) )
178  {
179  // found it, return
180  return true;
181  }
182  }// end ignore "first" file that we tried
183  }// end loop over files
184 
185  // did not find the event with id "id".
186  return false;
187  }// end if we did not find event id in "first" file
188 }
189 
190 bool
192 {
193  return to(run, 0U, event);
194 }
195 
198 const ChainEvent&
200 {
201  if (eventIndex_ != 0)
202  {
203  switchToFile(0);
204  }
205  event_->toBegin();
206  return *this;
207 }
208 
209 void
210 ChainEvent::switchToFile(Long64_t iIndex)
211 {
212  eventIndex_= iIndex;
213  TFile *tfilePtr = TFile::Open(fileNames_[iIndex].c_str());
214  file_ = boost::shared_ptr<TFile>(tfilePtr);
215  gROOT->GetListOfFiles()->Remove(tfilePtr);
216  event_ = boost::shared_ptr<Event>( new Event(file_.get()));
217 }
218 
219 //
220 // const member functions
221 //
222 const std::string
223 ChainEvent::getBranchNameFor(const std::type_info& iType,
224  const char* iModule,
225  const char* iInstance,
226  const char* iProcess) const
227 {
228  return event_->getBranchNameFor(iType,iModule,iInstance,iProcess);
229 }
230 
231 const std::vector<edm::BranchDescription>&
233 {
234  return event_->getBranchDescriptions();
235 }
236 
237 const std::vector<std::string>&
239 {
240  return event_->getProcessHistory();
241 }
242 
243 edm::EventAuxiliary const&
245 {
246  return event_->eventAuxiliary();
247 }
248 
250 {
251  return event_->getLuminosityBlock();
252 }
253 
255 {
256  return event_->getRun();
257 }
258 
259 
260 bool
261 ChainEvent::getByLabel(const std::type_info& iType,
262  const char* iModule,
263  const char* iInstance,
264  const char* iProcess,
265  void* iValue) const
266 {
267  return event_->getByLabel(iType,iModule,iInstance,iProcess,iValue);
268 }
269 
271 {
272  return event_->getByProductID( iID );
273 }
274 
275 bool
277 {
278  return event_->isValid();
279 }
281 {
282  return *event_;
283 }
284 
285 bool
287 {
288  if (eventIndex_ == static_cast<Long64_t>(fileNames_.size())-1) {
289  return event_->atEnd();
290  }
291  return false;
292 }
293 
294 Long64_t
296 {
297  return accumulatedSize_.back();
298 }
299 
300 edm::TriggerNames const&
301 ChainEvent::triggerNames(edm::TriggerResults const& triggerResults) const
302 {
303  return event_->triggerNames(triggerResults);
304 }
305 
306 void
308 {
309  event_->fillParameterSetRegistry();
310 }
311 
313 ChainEvent::triggerResultsByName(std::string const& process) const {
314  return event_->triggerResultsByName(process);
315 }
316 
317 //
318 // static member functions
319 //
320 void
321 ChainEvent::throwProductNotFoundException(const std::type_info& iType,
322  const char* iModule,
323  const char* iInstance,
324  const char* iProcess) {
325  Event::throwProductNotFoundException(iType,iModule,iInstance,iProcess);
326 }
327 }
virtual edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const
Definition: ChainEvent.cc:301
virtual edm::TriggerResultsByName triggerResultsByName(std::string const &process) const
Definition: ChainEvent.cc:313
static void throwProductNotFoundException(const std::type_info &, const char *, const char *, const char *)
Definition: ChainEvent.cc:321
Long64_t size() const
Definition: ChainEvent.cc:295
unsigned int EventNumber_t
Definition: EventID.h:30
virtual edm::EventAuxiliary const & eventAuxiliary() const
Definition: ChainEvent.cc:244
void switchToFile(Long64_t)
Definition: ChainEvent.cc:210
tuple lumi
Definition: fjr2json.py:41
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:59
edm::EDProduct const * getByProductID(edm::ProductID const &) const
Definition: ChainEvent.cc:270
virtual bool getByLabel(const std::type_info &, const char *, const char *, const char *, void *) const
Definition: ChainEvent.cc:261
Long64_t eventIndex_
Definition: ChainEvent.h:127
virtual ~ChainEvent()
Definition: ChainEvent.cc:75
unsigned int LuminosityBlockNumber_t
Definition: EventID.h:31
const std::vector< edm::BranchDescription > & getBranchDescriptions() const
Definition: ChainEvent.cc:232
const ChainEvent & operator++()
Definition: ChainEvent.cc:96
ChainEvent(const std::vector< std::string > &iFileNames)
Definition: ChainEvent.cc:34
fwlite::LuminosityBlock const & getLuminosityBlock()
Definition: ChainEvent.cc:249
const ChainEvent & toBegin()
Definition: ChainEvent.cc:199
void fillParameterSetRegistry() const
Definition: ChainEvent.cc:307
boost::shared_ptr< Event > event_
Definition: ChainEvent.h:126
fwlite::Run const & getRun()
Definition: ChainEvent.cc:254
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:114
virtual bool atEnd() const
Definition: ChainEvent.cc:286
const std::vector< std::string > & getProcessHistory() const
Definition: ChainEvent.cc:238
std::vector< Long64_t > accumulatedSize_
Definition: ChainEvent.h:128
virtual const std::string getBranchNameFor(const std::type_info &, const char *, const char *, const char *) const
Definition: ChainEvent.cc:223
tuple process
Definition: align_tpl.py:3
static void throwProductNotFoundException(const std::type_info &, const char *, const char *, const char *)
Definition: Event.cc:473
std::string const & eventTreeName()
Definition: BranchType.cc:244
unsigned int RunNumber_t
Definition: EventRange.h:32
UInt_t nEvents
Definition: hcalCalib.cc:43
tuple ifile
Definition: indexGen.py:77
Event const * event() const
Definition: ChainEvent.h:98
bool isValid() const
Definition: ChainEvent.cc:276
boost::shared_ptr< TFile > file_
Definition: ChainEvent.h:125
std::vector< std::string > fileNames_
Definition: ChainEvent.h:124