HIPO  4.3.0
High Performance Output data format for experimental physics
hipoeventiterator.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <bank.h>
11 #include <dictionary.h>
12 #include <event.h>
13 #include <reader.h>
14 #include <iterator>
15 #include <stdexcept>
16 #include <string>
17 #include <string_view>
18 #include <unordered_map>
19 
20 namespace hipo {
21 
23 
37  friend class iter_event;
38 
39  public:
40  class iterator;
42 
43  private:
44  std::unique_ptr<hipo::reader> reader;
45  dictionary dict;
46  mutable std::unordered_map<std::string, bank> bank_templates;
47 
48  public:
49  explicit hipoeventfile(const std::string& filename) {
50  reader = std::make_unique<hipo::reader>();
51  reader->open(filename.c_str());
52  reader->readDictionary(dict);
53 
54  // Pre-create bank templates for all known schemas
55  const auto& schema_list = dict.getSchemaList();
56  bank_templates.reserve(schema_list.size());
57  for (const std::string& name : schema_list) {
58  schema& sch = dict.getSchema(name.c_str());
59  bank_templates.emplace(name, bank(sch));
60  }
61  }
62 
63  hipoeventfile(hipoeventfile&& other) noexcept = default;
64  hipoeventfile& operator=(hipoeventfile&& other) noexcept = default;
65  hipoeventfile(const hipoeventfile&) = delete;
67 
70  iterator begin();
73  iterator end();
74 };
75 
82 class iter_event {
83  friend class hipoeventfile;
85 
86  private:
87  event* event_ptr;
88  hipoeventfile* file_ptr;
89  iter_event(event* ev, hipoeventfile* file) : event_ptr(ev), file_ptr(file) {}
90 
91  public:
92  iter_event() : event_ptr(nullptr), file_ptr(nullptr) {}
93  iter_event(iter_event&&) noexcept = default;
94  iter_event& operator=(iter_event&&) noexcept = default;
95 
99  inline bank& get_bank(std::string_view bankName) const {
100  if (!event_ptr || !file_ptr) throw std::runtime_error("Invalid HipoEvent (no event data)");
101  auto it = file_ptr->bank_templates.find(std::string(bankName));
102  if (it == file_ptr->bank_templates.end()) throw std::runtime_error("Schema not found for bank: " + std::string(bankName));
103  event_ptr->read(it->second);
104  return it->second;
105  }
106 };
107 
110  friend class hipoeventfile;
111 
112  public:
113  using iterator_category = std::input_iterator_tag;
116  using pointer = iter_event*;
117 
118  private:
119  hipo::reader* reader_ptr;
120  hipoeventfile* file_ptr;
121  event current_event;
122  iter_event current_wrap;
123  bool at_end;
124 
125  iterator(hipo::reader* rdr, hipoeventfile* file) : reader_ptr(rdr), file_ptr(file), at_end(false) {
126  if (!reader_ptr || !reader_ptr->next(current_event)) {
127  reader_ptr = nullptr;
128  file_ptr = nullptr;
129  at_end = true;
130  } else {
131  current_wrap = iter_event(&current_event, file_ptr);
132  }
133  }
134 
135  public:
137  iterator() : reader_ptr(nullptr), file_ptr(nullptr), at_end(true) {}
138 
141  inline iterator& operator++() {
142  if (!reader_ptr || at_end) return *this;
143  if (!reader_ptr->next(current_event)) {
144  reader_ptr = nullptr;
145  file_ptr = nullptr;
146  at_end = true;
147  } else {
148  current_wrap.event_ptr = &current_event;
149  current_wrap.file_ptr = file_ptr;
150  }
151  return *this;
152  }
153 
156  inline reference operator*() { return current_wrap; }
157 
160  inline pointer operator->() { return &current_wrap; }
161 
165  inline bool operator!=(const iterator& other) const { return at_end != other.at_end; }
166 
170  inline bool operator==(const iterator& other) const { return at_end == other.at_end; }
171 };
172 
176  reader->rewind();
177  return {reader.get(), this};
178 }
179 
183  return {};
184 }
185 
186 } // namespace hipo
Core HIPO data structures: structure, composite, and bank classes for tabular data access.
Represents a HIPO bank, a tabular data structure with rows and typed columns.
Definition: bank.h:352
Collection of schema definitions, typically read from a HIPO file header.
Definition: dictionary.h:248
std::vector< std::string > getSchemaList()
Get a list of all schema names in the dictionary.
Definition: dictionary.cpp:121
schema & getSchema(const char *name)
Retrieve a schema by name.
Definition: dictionary.h:271
Represents a HIPO event, a container for multiple structures/banks.
Definition: event.h:77
void read(hipo::bank &b)
Read a bank from this event (alias for getStructure).
Definition: event.cpp:67
Input iterator for traversing events in a hipoeventfile.
reference operator*()
Dereference operator; get a reference to the current event wrapper.
std::input_iterator_tag iterator_category
bool operator!=(const iterator &other) const
Inequality comparison operator.
bool operator==(const iterator &other) const
Equality comparison operator.
pointer operator->()
Member access operator; get a pointer to the current event wrapper.
iterator & operator++()
Increment operator; advance to the next event.
iterator()
Construct an end-of-file sentinel iterator.
HIPO file wrapper providing range-based iteration over events.
hipoeventfile & operator=(const hipoeventfile &)=delete
hipoeventfile(const std::string &filename)
hipoeventfile & operator=(hipoeventfile &&other) noexcept=default
iterator end()
Return an iterator representing the end of the file.
hipoeventfile(hipoeventfile &&other) noexcept=default
iterator begin()
Return an iterator to the first event in the file.
hipoeventfile(const hipoeventfile &)=delete
Lightweight event wrapper providing bank access by name.
iter_event(iter_event &&) noexcept=default
bank & get_bank(std::string_view bankName) const
Read and return a bank from the current event by name.
Sequential reader for HIPO files.
Definition: reader.h:250
void readDictionary(hipo::dictionary &dict)
Read the schema dictionary from the file header.
Definition: reader.cpp:311
void rewind()
Rewind to the beginning of the file.
Definition: reader.h:301
bool next()
Advance to the next event.
Definition: reader.cpp:334
void open(const char *filename)
Open a HIPO file for reading.
Definition: reader.cpp:81
Schema definition for a HIPO bank.
Definition: dictionary.h:73
Schema definitions and schema dictionary for HIPO banks.
HIPO event container and manipulation interface.
Definition: bank.cpp:47
Sequential and random-access reader for HIPO files with event filtering and dictionary support.