GCC Code Coverage Report


Directory: ./
File: include/na64dp/abstractEventSource.hh
Date: 2025-09-01 06:19:01
Exec Total Coverage
Lines: 1 7 14.3%
Functions: 1 5 20.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /* This file is a part of NA64SW software.
2 * Copyright (C) 2015-2022 NA64 Collaboration, CERN
3 *
4 * NA64SW is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #pragma once
18
19 #include "na64dp/processingInfo.hh"
20 #include "na64calib/manager.hh"
21 #include "na64util/YAMLAssertions.hh"
22 #include "na64util/vctr.hh"
23 #include "na64util/mem/fwd.hh"
24 #include "na64detID/TBName.hh"
25
26 #include <map>
27 #include <yaml-cpp/yaml.h>
28
29 namespace na64dp {
30
31 namespace errors {
32 class UnsupportedDataSourceFeature : public GenericRuntimeError {
33 public:
34 UnsupportedDataSourceFeature(const char * reason) throw()
35 : GenericRuntimeError(reason) {}
36 };
37 } // namespace errors
38
39 namespace event {
40 struct Event; // fwd
41 } // namespace event
42
43 ///\brief An interface for event source entities (files, sockets, etc).
44 class AbstractEventSource {
45 public:
46 typedef uint8_t Features_t;
47 static constexpr Features_t kBackwardReading = 0x1;
48 static constexpr Features_t kRandomAccess = 0x2;
49 static constexpr Features_t kProvidesEventsList = 0x4;
50 protected:
51 /// Sources logger category
52 log4cpp::Category & _log;
53 /// Online event processing stats monitor handle
54 iEvProcInfo * _epi;
55 public:
56 /// Returns features flags; default is `forward reading only`
57 virtual Features_t features() const { return 0x0; }
58
59 /// Default ctr, optionally can bind source to `iEvProcInfo` instance
60 AbstractEventSource( iEvProcInfo * epi=nullptr );
61
62 /// Recommended ctr, with explicitly specified logging category, optionally
63 /// can bind source to `iEvProcInfo` instance
64 AbstractEventSource( iEvProcInfo * epi, log4cpp::Category & logCat );
65
66 ///\brief Read next event
67 ///
68 /// Must be implemented for any particular data source type.
69 /// Shall read event from file, socket, whatever into reentrant buffer.
70 /// Shall not clear event, but rather rely on event has been already
71 /// re-set. Must return whether the event iteration shall be proceed.
72 virtual bool read( event::Event &, event::LocalMemory & lmem ) = 0;
73
74 ///\brief Reads "previous" event
75 ///
76 /// Shall read "previous" event from or data source of any kind.
77 /// Default implementation throws `errors::UnsupportedDataSourceFeature`.
78 /// Subclasses implementing this method must set `kBackwardReading` flag
79 /// in `features()`.
80 virtual bool read_prev( event::Event &, event::LocalMemory & lmem );
81
82 ///\brief Reads event specified by ID
83 ///
84 /// Shall read specified event from or data source of any kind.
85 /// Default implementation throws `UnsupportedDataSourceFeature`.
86 /// Subclasses implementing this method must set `kRandomAccess` flag
87 /// in `features()`.
88 virtual bool read_by_id( EventID, event::Event &, event::LocalMemory & lmem );
89
90 ///\brief Should fill event IDs with given pagination settings
91 ///
92 /// Shall return list of events IDs available in source respecting given
93 /// pagination settings.
94 /// Default implementation throws `UnsupportedDataSourceFeature`.
95 /// Subclasses implementing this method must set `kProvidesEventsList` flag
96 /// in `features()`.
97 virtual size_t list_events( std::vector<EventID> &, size_t nPage, size_t nPerPage );
98
99 ///\brief Should append node with type-specific information
100 ///
101 /// This method is sometimes used by various monitoring utils that may
102 /// or may not assume specific type. Second argument is used to specify
103 /// various options (e.g. a querystring). Default implementation does
104 /// nothing.
105 virtual void append_state_info(YAML::Node &,
106 const std::unordered_map<std::string, std::string> &) {}
107
108 /// This method is called by pipeline code when the source is processed.
109 /// Used to clean up things after reading is done, print reports, etc.
110 virtual void finalize() {}
111 2 virtual ~AbstractEventSource() {}
112 /// Returns logger instance affiliated to sources objects.
113 log4cpp::Category & log() { return _log; }
114 };
115
116 template<> struct CtrTraits<AbstractEventSource> {
117 typedef AbstractEventSource * (*Constructor)
118 (calib::Manager &, const YAML::Node &, const std::vector<std::string> & );
119 };
120
121 ///\brief Common helper class for NA64 cachinh chip and detector codes
122 class AbstractNameCachedEventSource : public AbstractEventSource
123 , protected calib::Handle<nameutils::DetectorNaming>
124 {
125 public:
126 /// Cache instance keeping numerical codes for chips and detector types
127 struct NameCache {
128 DetChip_t kSADC
129 , kAPV
130 , kStwTDC
131 , kF1
132 , kNA64WB
133 ;
134 DetKin_t kECAL, kHCAL, kWCAL, kZDCAL, kSRD, kVETO, kS, kVTEC, kVTWC
135 , kWCAT, kVHCAL, kDM, kV
136 ;
137 DetKin_t kMM, kGEM;
138 DetKin_t kSt, kStt;
139 DetKin_t kBMS;
140 // ... others, used by ChipTraits
141 };
142 private:
143 /// Internal detector naming cache
144 NameCache _detIDsCache;
145 protected:
146 /// Reference to calibration manager, used to handle new event number
147 calib::Manager & _mgr;
148 protected:
149 /// Updates `_detIDsCache` name cache
150 virtual void handle_update( const nameutils::DetectorNaming & ) override;
151
152 AbstractNameCachedEventSource( calib::Manager & mgr
153 , log4cpp::Category & loggingCategory
154 , const std::string & namingClass="default"
155 , iEvProcInfo * epi=nullptr
156 );
157 virtual ~AbstractNameCachedEventSource() {}
158 /// Returns reference to known calib manager instance
159 calib::Manager & calib_manager() { return _mgr; }
160 /// Returns reference to known calib manager instance (const)
161 const calib::Manager & calib_manager() const { return _mgr; }
162 /// Returns current detector naming instance
163 const NameCache & naming() const;
164 };
165
166 } // namespace na64dp
167
168 /**\brief Registers new event source type for pipeline data processing
169 * \ingroup vctr-defs
170 *
171 * Registers subclasses of `AbstractEventSource` base to be created with `VCtr`.
172 *
173 * \param clsName The name of the handler type
174 * \param calibMgr The name of calibations manager instance to be used in ctr
175 * \param ni The name of the config (YAML) node to be used in the ctr
176 * \param ids The name for list of input source IDs to be used in ctr
177 * \param desc A source type description string
178 */
179 # define REGISTER_SOURCE( clsName, calibMgr, ni, ids, desc ) \
180 static na64dp::AbstractEventSource * _new_ ## clsName ## _instance( \
181 na64dp::calib::Manager & \
182 , const YAML::Node & \
183 , const std::vector<std::string> & ); \
184 static bool _regResult_ ## clsName = \
185 ::na64dp::VCtr::self().register_class<::na64dp::AbstractEventSource>( # clsName, _new_ ## clsName ## _instance, desc ); \
186 \
187 static na64dp::AbstractEventSource * _new_ ## clsName ## _instance( \
188 na64dp::calib::Manager & calibMgr \
189 , const YAML::Node & ni \
190 , const std::vector<std::string> & ids )
191
192