CMS 3D CMS Logo

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