Event.h
Go to the documentation of this file.
1 #ifndef gallery_Event_h
2 #define gallery_Event_h
3 
4 // ====================================================================
5 // Main interface to users. It uses the DataGetterHelper and
6 // EventNavigator to iterate over events in a set of input files and
7 // find products in them.
8 // ====================================================================
9 
20 #include "gallery/EventNavigator.h"
21 #include "gallery/Handle.h"
22 #include "gallery/ValidHandle.h"
23 
24 #include <cassert>
25 #include <memory>
26 #include <string>
27 #include <typeinfo>
28 #include <utility>
29 #include <vector>
30 
31 class TFile;
32 class TTree;
33 
34 namespace gallery {
35 
36  // The gallery::Event provides read-only access to event data
37  // products. It also provides the means to iterate through one or
38  // more art/ROOT files, reading the events in those files; in this
39  // sense, the gallery::Event is a kind of iterator. Only a valid
40  // Event (as defined by Event::isValid()) can be used to access
41  // event data products.
42  class Event {
43  public:
44  // gallery::Event objects can be neither copied nor assigned. They
45  // can be move-copied and move-assigned.
46 
47  // Construct an Event that will read events from each of the
48  // named art/ROOT files, in order.
49  explicit Event(std::vector<std::string> const& fileNames,
50  bool useTTreeCache = true,
51  unsigned int eventsToLearnUsedBranches = 7);
52 
53  // Get access to a data product of type PROD, using a ValidHandle.
54  template <typename PROD>
56 
57  // Get access to a data product of type PROD, using a Handle.
58  // Only if the return value is 'true' is the Handle valid.
59  template <typename PROD>
60  bool getByLabel(art::InputTag const&, Handle<PROD>& result) const;
61 
62  // Get access to all data products of type PROD, using a
63  // std::vector<Handle>.
64  template <typename PROD>
65  void getManyByType(std::vector<Handle<PROD>>& result) const;
66 
67  // Return all input tags corresponding to products of type PROD.
68  // This function call *does not* read or provide access to any
69  // products.
70  template <typename PROD>
71  std::vector<art::InputTag> getInputTags() const;
72 
73  template <typename PROD>
74  std::vector<art::ProductToken<PROD>> getProductTokens() const;
75 
76  art::EventAuxiliary const& eventAuxiliary() const;
77  art::History const& history() const;
79  art::ProcessHistory const& processHistory() const;
80 
81  // Return the product description if it is present.
83 
84  // Return the number of events in the currently-open file.
85  long long numberOfEventsInFile() const;
86 
87  // Return the current entry number (the entry number of the Events
88  // tree in the current art/ROOT file).
89  long long eventEntry() const;
90 
91  // Return the index of the current file.
92  long long fileEntry() const;
93 
94  // Go to the entry with the given index. If the index is
95  // out-of-bounds, or negative, or if the Event is not suitable for
96  // random access, an exception will be thrown.
97  void goToEntry(long long entry);
98 
99  // Return true if the Event can be used to access data products.
100  bool isValid() const;
101 
102  // Return true if we are at the end of the sequence of events
103  // through which we are iterating.
104  bool atEnd() const;
105 
106  // Go to the first event of the sequence we are to traverse.
107  void toBegin();
108  void first();
109 
110  // Go to the next event in the sequence.
111  Event& operator++();
112  void next();
113 
114  // Throws an exception if the Event was constructed with more than
115  // one filename, or if we are already at the beginning of the
116  // sequence. Otherwise, go to the previous event in the sequence.
117  Event& operator--();
118  void previous();
119 
120  TFile* getTFile() const;
121  TTree* getTTree() const;
122 
123  template <typename T>
125 
126  private:
127  ProductWithID getByLabel(std::type_info const& typeInfoOfWrapper,
128  art::InputTag const& inputTag) const;
129 
130  std::vector<ProductWithID> getManyByType(
131  std::type_info const& typeInfoOfWrapper) const;
132 
133  [[noreturn]] void throwProductNotFoundException(
134  std::type_info const& typeInfo,
135  art::InputTag const& tag) const;
136 
137  std::shared_ptr<art::Exception const> makeProductNotFoundException(
138  std::type_info const& typeInfo,
139  art::InputTag const& tag) const;
140 
141  void checkForEnd() const;
142  void updateAfterEventChange(long long oldFileEntry);
143 
145  std::unique_ptr<EventNavigator> eventNavigator_;
146  std::unique_ptr<DataGetterHelper> dataGetterHelper_;
147 
150  unsigned int eventsProcessed_{};
151  };
152 } // namespace gallery
153 
154 template <typename PROD>
157 {
158  checkForEnd();
159  std::type_info const& typeInfoOfWrapper{typeid(art::Wrapper<PROD>)};
160  auto res = getByLabel(typeInfoOfWrapper, inputTag);
161  auto edProduct = res.first;
162 
163  auto ptrToWrapper = dynamic_cast<art::Wrapper<PROD> const*>(edProduct);
164  if (ptrToWrapper == nullptr) {
165  throwProductNotFoundException(typeid(PROD), inputTag);
166  }
167 
168  auto product = ptrToWrapper->product();
169  return ValidHandle<PROD>{product, res.second};
170 }
171 
172 template <typename PROD>
173 inline bool
175  Handle<PROD>& result) const
176 {
177  checkForEnd();
178  if (inputTag.empty()) {
179  result = Handle<PROD>{makeProductNotFoundException(typeid(PROD), inputTag)};
180  return false;
181  }
182 
183  std::type_info const& typeInfoOfWrapper{typeid(art::Wrapper<PROD>)};
184  auto res = getByLabel(typeInfoOfWrapper, inputTag);
185  auto edProduct = res.first;
186 
187  auto ptrToWrapper = dynamic_cast<art::Wrapper<PROD> const*>(edProduct);
188 
189  if (ptrToWrapper == nullptr) {
190  result = Handle<PROD>{makeProductNotFoundException(typeid(PROD), inputTag)};
191  return false;
192  }
193  auto product = ptrToWrapper->product();
194  result = Handle<PROD>{product, res.second};
195  return true;
196 }
197 
198 template <typename PROD>
199 inline void
201 {
202  std::type_info const& typeInfoOfWrapper{typeid(art::Wrapper<PROD>)};
203  auto products = getManyByType(typeInfoOfWrapper);
204  std::vector<Handle<PROD>> tmp;
205  cet::transform_all(products, back_inserter(tmp), [](auto const& pr) {
206  auto product = pr.first;
207  auto wrapped_product = dynamic_cast<art::Wrapper<PROD> const*>(product);
208  assert(wrapped_product != nullptr);
209  auto user_product = wrapped_product->product();
210  assert(user_product != nullptr);
211  return Handle<PROD>{user_product, pr.second};
212  });
213  swap(tmp, result);
214 }
215 
216 template <typename PROD>
217 inline std::vector<art::InputTag>
219 {
220  std::type_info const& typeInfoOfWrapper{typeid(art::Wrapper<PROD>)};
221  return dataGetterHelper_->getInputTags(typeInfoOfWrapper);
222 }
223 
224 template <typename PROD>
225 inline std::vector<art::ProductToken<PROD>>
227 {
228  std::vector<art::ProductToken<PROD>> result;
229  auto const tags = getInputTags<PROD>();
230  cet::transform_all(tags, back_inserter(result), [](auto const& tag) {
232  });
233  return result;
234 }
235 
236 inline void
238 {
239  return toBegin();
240 }
241 
242 #endif /* gallery_Event_h */
243 
244 // Local Variables:
245 // mode: c++
246 // End:
gallery::ValidHandle< PROD > getValidHandle(art::InputTag const &) const
Definition: Event.h:156
QList< Entry > entry
static QCString result
long long eventEntry() const
Definition: Event.cc:71
void first()
Definition: Event.h:237
struct vector vector
std::unique_ptr< DataGetterHelper > dataGetterHelper_
Definition: Event.h:146
art::EventAuxiliary const & eventAuxiliary() const
Definition: Event.cc:35
TFile * getTFile() const
Definition: Event.cc:173
bool empty() const noexcept
Definition: InputTag.cc:73
Event & operator++()
Definition: Event.cc:119
std::vector< art::InputTag > getInputTags() const
Definition: Event.h:218
void goToEntry(long long entry)
Definition: Event.cc:83
void swap(Handle< T > &a, Handle< T > &b)
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
string tmp
Definition: languages.py:63
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
auto transform_all(Container &, OutputIt, UnaryOp)
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
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
std::vector< art::ProductToken< PROD > > getProductTokens() const
Definition: Event.h:226
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