HIPO4 C++ Library 4.4.1
Columnar I/O library for CLAS12 physics data
Loading...
Searching...
No Matches
hipoeventiterator.h
Go to the documentation of this file.
1#pragma once
2
3#include "bank.h"
4#include "dictionary.h"
5#include "event.h"
6#include "reader.h"
7#include <iterator>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11#include <unordered_map>
12
13namespace hipo {
15 friend class iter_event;
16
17 public:
18 class iterator;
20
21 private:
22 std::unique_ptr<hipo::reader> reader;
23 dictionary dict;
24 mutable std::unordered_map<std::string, bank> bank_templates;
25
26 public:
27 explicit hipoeventfile(const std::string& filename) {
28 reader = std::make_unique<hipo::reader>();
29 reader->open(filename.c_str());
31
32 // Pre-create bank templates for all known schemas
33 const auto& schema_list = dict.getSchemaList();
34 bank_templates.reserve(schema_list.size());
35 for (const std::string& name : schema_list) {
36 schema& sch = dict.getSchema(name.c_str());
37 bank_templates.emplace(name, bank(sch));
38 }
39 }
40
41 hipoeventfile(hipoeventfile&& other) noexcept = default;
42 hipoeventfile& operator=(hipoeventfile&& other) noexcept = default;
43 hipoeventfile(const hipoeventfile&) = delete;
45
47 iterator end();
48};
49
51 friend class hipoeventfile;
53
54 private:
55 event* event_ptr;
56 hipoeventfile* file_ptr;
57 iter_event(event* ev, hipoeventfile* file) : event_ptr(ev), file_ptr(file) {}
58
59 public:
60 iter_event() : event_ptr(nullptr), file_ptr(nullptr) {}
61 iter_event(iter_event&&) noexcept = default;
62 iter_event& operator=(iter_event&&) noexcept = default;
63
64 inline bank& get_bank(std::string_view bankName) const {
65 if (!event_ptr || !file_ptr) throw std::runtime_error("Invalid HipoEvent (no event data)");
66 auto it = file_ptr->bank_templates.find(std::string(bankName));
67 if (it == file_ptr->bank_templates.end()) throw std::runtime_error("Schema not found for bank: " + std::string(bankName));
68 event_ptr->read(it->second);
69 return it->second;
70 }
71};
72
74 friend class hipoeventfile;
75
76 public:
77 using iterator_category = std::input_iterator_tag;
81
82 private:
83 hipo::reader* reader_ptr;
84 hipoeventfile* file_ptr;
85 event current_event;
86 iter_event current_wrap;
87 bool at_end;
88
89 iterator(hipo::reader* rdr, hipoeventfile* file) : reader_ptr(rdr), file_ptr(file), at_end(false) {
90 if (!reader_ptr || !reader_ptr->next(current_event)) {
91 reader_ptr = nullptr;
92 file_ptr = nullptr;
93 at_end = true;
94 } else {
95 current_wrap = iter_event(&current_event, file_ptr);
96 }
97 }
98
99 public:
100 iterator() : reader_ptr(nullptr), file_ptr(nullptr), at_end(true) {}
101
103 if (!reader_ptr || at_end) return *this;
104 if (!reader_ptr->next(current_event)) {
105 reader_ptr = nullptr;
106 file_ptr = nullptr;
107 at_end = true;
108 } else {
109 current_wrap.event_ptr = &current_event;
110 current_wrap.file_ptr = file_ptr;
111 }
112 return *this;
113 }
114
115 inline reference operator*() { return current_wrap; }
116
117 inline pointer operator->() { return &current_wrap; }
118
119 inline bool operator!=(const iterator& other) const { return at_end != other.at_end; }
120
121 inline bool operator==(const iterator& other) const { return at_end == other.at_end; }
122};
123
125 reader->rewind();
126 return {reader.get(), this};
127}
128
130 return {};
131}
132
133} // namespace hipo
Definition bank.h:210
Collection of schema definitions, typically read from a HIPO file header.
Definition dictionary.h:157
schema & getSchema(const char *name)
Definition dictionary.h:167
std::vector< std::string > getSchemaList()
Definition dictionary.cpp:117
Definition event.h:62
void read(hipo::bank &b)
Definition event.cpp:63
Definition hipoeventiterator.h:73
reference operator*()
Definition hipoeventiterator.h:115
std::input_iterator_tag iterator_category
Definition hipoeventiterator.h:77
bool operator!=(const iterator &other) const
Definition hipoeventiterator.h:119
bool operator==(const iterator &other) const
Definition hipoeventiterator.h:121
iterator & operator++()
Definition hipoeventiterator.h:102
pointer operator->()
Definition hipoeventiterator.h:117
iterator()
Definition hipoeventiterator.h:100
Definition hipoeventiterator.h:14
friend class iter_event
Definition hipoeventiterator.h:15
hipoeventfile(const std::string &filename)
Definition hipoeventiterator.h:27
iterator end()
Definition hipoeventiterator.h:129
hipoeventfile & operator=(hipoeventfile &&other) noexcept=default
hipoeventfile(hipoeventfile &&other) noexcept=default
iterator begin()
Definition hipoeventiterator.h:124
hipoeventfile(const hipoeventfile &)=delete
hipoeventfile & operator=(const hipoeventfile &)=delete
Definition hipoeventiterator.h:50
iter_event(iter_event &&) noexcept=default
bank & get_bank(std::string_view bankName) const
Definition hipoeventiterator.h:64
iter_event()
Definition hipoeventiterator.h:60
Definition reader.h:197
void readDictionary(hipo::dictionary &dict)
Reads the dictionary for the file.
Definition reader.cpp:318
void rewind()
Definition reader.h:238
bool next()
Advances the event pointer to the next event.
Definition reader.cpp:341
void open(const char *filename)
Open file, if file stream is open, it is closed first.
Definition reader.cpp:78
Schema definition for a HIPO bank.
Definition dictionary.h:56
HIPO namespace is used for the classes that read/write files and records.
Definition bank.cpp:45