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