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