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
edm::RunNumber_t
unsigned int RunNumber_t
Definition: RunLumiEventNumber.h:14
electrons_cff.bool
bool
Definition: electrons_cff.py:366
edm::poolNames::eventTreeName
std::string const & eventTreeName()
Definition: BranchType.cc:220
fwlite::ChainEvent::triggerResultsByName
edm::TriggerResultsByName triggerResultsByName(edm::TriggerResults const &triggerResults) const override
Definition: ChainEvent.cc:265
fwlite::ChainEvent::eventAuxiliary
edm::EventAuxiliary const & eventAuxiliary() const override
Definition: ChainEvent.cc:206
fwlite::ChainEvent::getByProductID
edm::WrapperBase const * getByProductID(edm::ProductID const &) const override
Definition: ChainEvent.cc:220
fwlite::ChainEvent::event
Event const * event() const
Definition: ChainEvent.h:98
fwlite
Definition: TFileDirectory.h:16
fwlite::ChainEvent::getThinnedProducts
void getThinnedProducts(edm::ProductID const &pid, std::vector< edm::WrapperBase const * > &foundContainers, std::vector< unsigned int > &keys) const
Definition: ChainEvent.cc:229
fwlite::ChainEvent::getByLabel
bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *) const override
Definition: ChainEvent.cc:212
tree
Definition: tree.py:1
triggerResults
static const std::string triggerResults
Definition: EdmProvDump.cc:45
edm::propagate_const::get
constexpr element_type const * get() const
Definition: propagate_const.h:64
fwlite::ChainEvent::throwProductNotFoundException
static void throwProductNotFoundException(std::type_info const &, char const *, char const *, char const *)
Definition: ChainEvent.cc:272
relativeConstraints.keys
keys
Definition: relativeConstraints.py:89
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
fwlite::ChainEvent::switchToFile
void switchToFile(Long64_t)
Definition: ChainEvent.cc:180
fwlite::ChainEvent::ChainEvent
ChainEvent(std::vector< std::string > const &iFileNames)
Definition: ChainEvent.cc:35
edm::LuminosityBlockNumber_t
unsigned int LuminosityBlockNumber_t
Definition: RunLumiEventNumber.h:13
Utilities.operator
operator
Definition: Utilities.py:24
fwlite::ChainEvent::isValid
bool isValid() const
Definition: ChainEvent.cc:241
fwlite::ChainEvent::event_
edm::propagate_const< std::shared_ptr< Event > > event_
Definition: ChainEvent.h:138
ProcessHistory.h
fwlite::ChainEvent::fileNames_
std::vector< std::string > fileNames_
Definition: ChainEvent.h:136
ChainEvent.h
compare_using_db.ifile
ifile
Definition: compare_using_db.py:251
fwlite::ChainEvent::accumulatedSize_
std::vector< Long64_t > accumulatedSize_
Definition: ChainEvent.h:140
edm::Hash< ParameterSetType >
edm::EventNumber_t
unsigned long long EventNumber_t
Definition: RunLumiEventNumber.h:12
fwlite::ChainEvent::triggerNames
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: ChainEvent.cc:255
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
edm::EventAuxiliary
Definition: EventAuxiliary.h:14
fwlite::ChainEvent::processHistory
edm::ProcessHistory const & processHistory() const override
Definition: ChainEvent.cc:204
fwlite::ChainEvent::file_
edm::propagate_const< std::shared_ptr< TFile > > file_
Definition: ChainEvent.h:137
edm::ParameterSet
Definition: ParameterSet.h:47
fwlite::ChainEvent::toBegin
ChainEvent const & toBegin() override
Definition: ChainEvent.cc:170
fwlite::ChainEvent::getBranchDescriptions
std::vector< edm::BranchDescription > const & getBranchDescriptions() const
Definition: ChainEvent.cc:198
edm::EventBase::luminosityBlock
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
fwlite::ChainEvent::operator++
ChainEvent const & operator++() override
Definition: ChainEvent.cc:91
fwlite::ChainEvent::to
bool to(Long64_t iIndex)
Go to the event at index iIndex.
Definition: ChainEvent.cc:106
event_
void event_()
fwlite::ChainEvent::getRun
fwlite::Run const & getRun()
Definition: ChainEvent.cc:210
edm::TriggerResultsByName
Definition: TriggerResultsByName.h:48
fwlite::Run
Definition: Run.h:54
BranchType.h
edm::WrapperBase
Definition: WrapperBase.h:23
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
fwlite::ChainEvent::getProcessHistory
std::vector< std::string > const & getProcessHistory() const
Definition: ChainEvent.cc:202
fwlite::ChainEvent::eventIndex_
Long64_t eventIndex_
Definition: ChainEvent.h:139
edm::OptionalThinnedKey
std::variant< unsigned int, detail::GetThinnedKeyFromExceptionFactory, std::monostate > OptionalThinnedKey
Definition: EDProductGetter.h:39
fwlite::ChainEvent::getBranchNameFor
const std::string getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const override
Definition: ChainEvent.cc:191
writedatasetfile.run
run
Definition: writedatasetfile.py:27
fwlite::ChainEvent::getThinnedProduct
std::optional< std::tuple< edm::WrapperBase const *, unsigned int > > getThinnedProduct(edm::ProductID const &pid, unsigned int key) const
Definition: ChainEvent.cc:224
Exception
Definition: hltDiff.cc:245
fwlite::ChainEvent::getThinnedKeyFrom
edm::OptionalThinnedKey getThinnedKeyFrom(edm::ProductID const &parent, unsigned int key, edm::ProductID const &thinned) const
Definition: ChainEvent.cc:235
edm::TriggerNames
Definition: TriggerNames.h:55
fwlite::ChainEvent::atEnd
bool atEnd() const override
Definition: ChainEvent.cc:244
fwlite::ChainEvent::parameterSet
edm::ParameterSet const * parameterSet(edm::ParameterSetID const &psID) const override
Definition: ChainEvent.cc:259
fwlite::ChainEvent::~ChainEvent
~ChainEvent() override
Definition: ChainEvent.cc:73
edm::ProcessHistory
Definition: ProcessHistory.h:13
fwlite::ChainEvent
Definition: ChainEvent.h:46
fwlite::ChainEvent::size
Long64_t size() const
Definition: ChainEvent.cc:253
fwlite::ChainEvent::fillParameterSetRegistry
void fillParameterSetRegistry() const
Definition: ChainEvent.cc:263
fwlite::LuminosityBlock
Definition: LuminosityBlock.h:57
event
Definition: event.py:1
edm::EventID
Definition: EventID.h:31
nEvents
UInt_t nEvents
Definition: hcalCalib.cc:40
crabWrapper.key
key
Definition: crabWrapper.py:19
lumi
Definition: LumiSectionData.h:20
fwlite::ChainEvent::getLuminosityBlock
fwlite::LuminosityBlock const & getLuminosityBlock()
Definition: ChainEvent.cc:208
class-composition.parent
parent
Definition: class-composition.py:98
edm::TriggerResults
Definition: TriggerResults.h:35
edm::ProductID
Definition: ProductID.h:27
fwlite::Event::throwProductNotFoundException
static void throwProductNotFoundException(std::type_info const &, char const *, char const *, char const *)
Definition: Event.cc:492