Event.cc
Go to the documentation of this file.
1 #include "gallery/Event.h"
2 
9 
10 namespace gallery {
11 
12  Event::Event(std::vector<std::string> const& fileNames,
13  bool const useTTreeCache,
14  unsigned int const eventsToLearnUsedBranches)
15  : randomAccessOK_{fileNames.size() == 1}
16  , eventNavigator_{std::make_unique<EventNavigator>(fileNames)}
17  , dataGetterHelper_{std::make_unique<DataGetterHelper>(
18  eventNavigator_.get(),
19  std::make_shared<EventHistoryGetter>(eventNavigator_.get()))}
20  , useTTreeCache_{useTTreeCache}
21  , eventsToLearnUsedBranches_{eventsToLearnUsedBranches}
22  {
23 
26  if (!atEnd()) {
27  bool constexpr initializeTheCache{false};
28  dataGetterHelper_->updateFile(eventNavigator_->getTFile(),
29  eventNavigator_->getTTree(),
30  initializeTheCache);
31  }
32  }
33 
34  art::EventAuxiliary const&
36  {
37  return eventNavigator_->eventAuxiliary();
38  }
39 
40  art::History const&
42  {
43  return eventNavigator_->history();
44  }
45 
48  {
49  return eventNavigator_->processHistoryID();
50  }
51 
52  art::ProcessHistory const&
54  {
55  return eventNavigator_->processHistory();
56  }
57 
60  {
61  return dataGetterHelper_->getProductDescription(pid);
62  }
63 
64  long long
66  {
67  return eventNavigator_->entriesInCurrentFile();
68  }
69 
70  long long
72  {
73  return eventNavigator_->eventEntry();
74  }
75 
76  long long
78  {
79  return eventNavigator_->fileEntry();
80  }
81 
82  void
83  Event::goToEntry(long long const entry)
84  {
85  if (!randomAccessOK_)
87  if (entry < 0)
89  if (entry >= numberOfEventsInFile())
91  eventNavigator_->goToEntry(entry);
92  }
93 
94  bool
96  {
97  return eventNavigator_->isValid();
98  }
99 
100  bool
101  Event::atEnd() const
102  {
103  return eventNavigator_->atEnd();
104  }
105 
106  void
108  {
109  long long const oldEventEntry = eventEntry();
110  long long const oldFileEntry = fileEntry();
111  eventNavigator_->toBegin();
112  if (oldEventEntry == eventEntry() && oldFileEntry == fileEntry()) {
113  return;
114  }
115  updateAfterEventChange(oldFileEntry);
116  }
117 
118  Event&
120  {
121  auto const oldFileEntry = fileEntry();
122  eventNavigator_->next();
123  updateAfterEventChange(oldFileEntry);
124  return *this;
125  }
126 
127  Event&
129  {
130  if (!randomAccessOK_)
132  if (atEnd())
134  auto const oldFileEntry = fileEntry();
135  eventNavigator_->previous();
136  updateAfterEventChange(oldFileEntry);
137  return *this;
138  }
139 
140  void
141  Event::updateAfterEventChange(long long const oldFileEntry)
142  {
144  if (atEnd())
145  return;
146  if (oldFileEntry != fileEntry()) {
147  bool initializeTheCache =
149  dataGetterHelper_->updateFile(eventNavigator_->getTFile(),
150  eventNavigator_->getTTree(),
151  initializeTheCache);
152  } else {
153  dataGetterHelper_->updateEvent();
155  dataGetterHelper_->initializeTTreeCache();
156  }
157  }
158  }
159 
160  void
162  {
163  operator++();
164  }
165 
166  void
168  {
169  operator--();
170  }
171 
172  TFile*
174  {
175  return eventNavigator_->getTFile();
176  }
177 
178  TTree*
180  {
181  return eventNavigator_->getTTree();
182  }
183 
185  Event::getByLabel(std::type_info const& typeInfoOfWrapper,
186  art::InputTag const& inputTag) const
187  {
188  return dataGetterHelper_->getByLabel(typeInfoOfWrapper, inputTag);
189  }
190 
191  std::vector<ProductWithID>
192  Event::getManyByType(std::type_info const& typeInfoOfWrapper) const
193  {
194  return dataGetterHelper_->getManyByType(typeInfoOfWrapper);
195  }
196 
197  void
198  Event::throwProductNotFoundException(std::type_info const& typeInfo,
199  art::InputTag const& tag) const
200  {
201  auto e = makeProductNotFoundException(typeInfo, tag);
202  throw *e;
203  }
204 
205  std::shared_ptr<art::Exception const>
206  Event::makeProductNotFoundException(std::type_info const& typeInfo,
207  art::InputTag const& tag) const
208  {
209  auto e = std::make_shared<art::Exception>(art::errors::ProductNotFound);
210  *e << "Failed to find product for \n type = '"
211  << art::TypeID{typeInfo}.className() << "'\n module = '" << tag.label()
212  << "'\n productInstance = '"
213  << ((!tag.instance().empty()) ? tag.instance().c_str() : "")
214  << "'\n process='"
215  << ((!tag.process().empty()) ? tag.process().c_str() : "") << "'\n";
216  return e;
217  }
218 
219  void
221  {
222  if (atEnd()) {
224  << "You have requested data past the last event\n";
225  }
226  }
227 } // namespace gallery
QList< Entry > entry
long long eventEntry() const
Definition: Event.cc:71
void throwIllegalRandomAccess()
std::string const & instance() const noexcept
Definition: InputTag.cc:85
std::unique_ptr< DataGetterHelper > dataGetterHelper_
Definition: Event.h:146
void throwIllegalDecrement()
std::string const & process() const noexcept
Definition: InputTag.cc:91
art::EventAuxiliary const & eventAuxiliary() const
Definition: Event.cc:35
TFile * getTFile() const
Definition: Event.cc:173
Event & operator++()
Definition: Event.cc:119
std::string const & label() const noexcept
Definition: InputTag.cc:79
const double e
void goToEntry(long long entry)
Definition: Event.cc:83
bool atEnd() const
Definition: Event.cc:101
std::shared_ptr< art::Exception const > makeProductNotFoundException(std::type_info const &typeInfo, art::InputTag const &tag) const
Definition: Event.cc:206
std::string className() const
Definition: TypeID.cc:48
std::unique_ptr< EventNavigator > eventNavigator_
Definition: Event.h:145
void toBegin()
Definition: Event.cc:107
long long fileEntry() const
Definition: Event.cc:77
unsigned int eventsProcessed_
Definition: Event.h:150
long long numberOfEventsInFile() const
Definition: Event.cc:65
bool getByLabel(art::InputTag const &, Handle< PROD > &result) const
Definition: Event.h:174
bool useTTreeCache_
Definition: Event.h:148
void throwProductNotFoundException(std::type_info const &typeInfo, art::InputTag const &tag) const
Definition: Event.cc:198
void updateAfterEventChange(long long oldFileEntry)
Definition: Event.cc:141
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void getManyByType(std::vector< Handle< PROD >> &result) const
Definition: Event.h:200
void previous()
Definition: Event.cc:167
void next()
Definition: Event.cc:161
TTree * getTTree() const
Definition: Event.cc:179
std::pair< art::EDProduct const *, art::ProductID > ProductWithID
void checkForEnd() const
Definition: Event.cc:220
art::ProcessHistory const & processHistory() const
Definition: Event.cc:53
Event & operator--()
Definition: Event.cc:128
unsigned int eventsToLearnUsedBranches_
Definition: Event.h:149
art::ProcessHistoryID const & processHistoryID() const
Definition: Event.cc:47
bool randomAccessOK_
Definition: Event.h:144
bool isValid() const
Definition: Event.cc:95
art::BranchDescription const & getProductDescription(art::ProductID) const
Definition: Event.cc:59
Event(std::vector< std::string > const &fileNames, bool useTTreeCache=true, unsigned int eventsToLearnUsedBranches=7)
Definition: Event.cc:12
art::History const & history() const
Definition: Event.cc:41