Pottu
DAQChannels.hpp
Go to the documentation of this file.
1 
8 #ifndef H_DAQCHANNELS
9 #define H_DAQCHANNELS
10 
11 #include "DetectorEvent.hpp"
12 #include "ContextBase.hpp"
13 #include "StageDetectorEvent.hpp"
15 
16 #include <vector>
17 #include <string>
18 #include <stdexcept>
19 #include <map>
20 #include <algorithm>
21 
22 
23 
24 namespace pottu {
25 
38  public:
39 
40 
44  struct channel_info_t {
46  DetectorEvent::group_type group{0};
48  DetectorEvent::coord_type coord{0};
50  std::string name;
51  };
52 
53 
57  class DetectorGroup {
58  public:
59 
64  void add( uint16_t ch ) {
65  _channels.push_back(ch);
66  std::sort( _channels.begin(), _channels.end() );
67  }
68 
73  void remove( uint16_t ch ) {
74  auto it = std::find( _channels.begin(), _channels.end(), ch );
75  if( it != _channels.end() )
76  _channels.erase( it );
77  }
78 
83  const std::string &getName() const noexcept { return _name; }
84 
90  const std::vector<uint16_t> &getChannels() const noexcept { return _channels; }
91 
92  private:
93  friend class DAQChannels;
94 
95  std::string _name;
96  // \brief Vector of channels which belongs to this group
97  std::vector<uint16_t> _channels;
98  };
99 
100 
106  : _ctxh( ContextBase::getActive().createHandle("DAQChannels") ),
107  _chInfos(4096) {}
108 
109 
116  void addGroup( DetectorEvent::group_type group, const std::string &name ) {
117  throwIfNotValidName( name );
118  if( group < 1 )
119  throw std::runtime_error( "DAQChannels::addGroup(): group must be >= 1!" );
120  if( _groups.find( group ) != _groups.end() )
121  throw std::runtime_error( "DAQChannels::addGroup(): group exists already" );
122  DetectorGroup g;
123  g._name = name;
124  _groups[ group ] = std::move(g);
125  }
126 
127 
140  void setChannel( uint16_t channel, DetectorEvent::group_type group,
141  DetectorEvent::coord_type coord, const std::string &name ) {
142  throwIfNotValidName(name);
143  // Erases channel from earlier group if assigned
144  if( _chInfos[channel].group != 0 )
145  _groups[_chInfos[channel].group].remove( channel );
146 
147  auto it = _groups.find( group );
148  if( it == _groups.end() )
149  throw std::runtime_error( fmt::format( "DAQChannels::setChannel(): group {} doesnt exist", group ) );
150  it->second.add( channel );
151  _chInfos[channel] = channel_info_t{ group, coord, name };
152  }
153 
154 
162  uint16_t getCh( const std::string &name ) const {
163  for( uint16_t ch = 0; ch < _chInfos.size(); ++ch ) {
164  if( _chInfos[ch].name == name )
165  return ch;
166  }
167  throw std::runtime_error( "Channel with given name not found" );
168  }
169 
170 
175  uint16_t getCh( DetectorEvent::group_type group, DetectorEvent::coord_type coord ) const {
176  for( uint16_t ch = 0; ch < _chInfos.size(); ++ch ) {
177  if( _chInfos[ch].group == group &&
178  _chInfos[ch].coord == coord )
179  return ch;
180  }
181  throw std::runtime_error( "Channel with given group and coord not found" );
182  }
183 
184 
189  const std::map<DetectorEvent::group_type, DetectorGroup> &getGroups() const noexcept {
190  return _groups;
191  }
192 
193 
198  const DetectorGroup &getGroup( DetectorEvent::group_type group ) const {
199  auto it = _groups.find( group );
200  if( it == _groups.end() )
201  throw std::runtime_error( fmt::format( "DAQChannels::getChannel(): group {} doesnt exist", group ) );
202  return it->second;
203  }
204 
205 
212  const std::vector<channel_info_t> &getChInfos() const noexcept { return _chInfos; }
213 
214 
222  virtual void process( std::vector<DetectorEvent> &data ) noexcept {
223  auto proci = _ctxh->processInstance();
224 
225  for( auto &event : data ) {
226  event.group = _chInfos[event.ch].group;
227  event.coord = _chInfos[event.ch].coord;
228  }
229  }
230 
231  private:
232  ContextHandle *_ctxh;
233 
234  std::vector<channel_info_t> _chInfos;
235  std::map<DetectorEvent::group_type, DetectorGroup> _groups;
236  };
237 
238 
239 
240 }
241 
242 
243 #endif
244 
245 
Handle to context used by specific class.
Definition: ContextBase.hpp:188
Simple class to store name and included channels for this group.
Definition: DAQChannels.hpp:57
const std::string & getName() const noexcept
Returns the name of the group.
Definition: DAQChannels.hpp:83
void add(uint16_t ch)
Adds channel to this group.
Definition: DAQChannels.hpp:64
const std::vector< uint16_t > & getChannels() const noexcept
Returns the vector of channel numbers assigned to this group.
Definition: DAQChannels.hpp:90
void remove(uint16_t ch)
Removes channel from this group.
Definition: DAQChannels.hpp:73
A pipeline stage which sets the group and coord for the detector events.
Definition: DAQChannels.hpp:37
void setChannel(uint16_t channel, DetectorEvent::group_type group, DetectorEvent::coord_type coord, const std::string &name)
Assigns a DAQ channel to a detector group and coordinate.
Definition: DAQChannels.hpp:140
const DetectorGroup & getGroup(DetectorEvent::group_type group) const
Returns registered group by its index.
Definition: DAQChannels.hpp:198
uint16_t getCh(const std::string &name) const
Returns channel info for given name
Definition: DAQChannels.hpp:162
virtual void process(std::vector< DetectorEvent > &data) noexcept
Fills detector group index and coord index to the events.
Definition: DAQChannels.hpp:222
const std::vector< channel_info_t > & getChInfos() const noexcept
Returns vector containing group, channel and name of all channels.
Definition: DAQChannels.hpp:212
uint16_t getCh(DetectorEvent::group_type group, DetectorEvent::coord_type coord) const
Returns channel info for given (group, coord) combination.
Definition: DAQChannels.hpp:175
const std::map< DetectorEvent::group_type, DetectorGroup > & getGroups() const noexcept
Returns all registered groups.
Definition: DAQChannels.hpp:189
void addGroup(DetectorEvent::group_type group, const std::string &name)
Adds a new detector group.
Definition: DAQChannels.hpp:116
DAQChannels()
Constructor.
Definition: DAQChannels.hpp:105
Abstract baseclass for all stages which uses or modifies detector events.
Definition: StageDetectorEvent.hpp:20
Definition: mainpage.dox:6
static void throwIfNotValidName(const std::string &name)
Checks that the given string can be used as a filename.
Definition: result_collection_support.hpp:216
Default context for printing and collecting statistics.
Definition: ContextBase.hpp:41
Group, coordinate and name information for a daq channel.
Definition: DAQChannels.hpp:44
std::string name
Name of the channel.
Definition: DAQChannels.hpp:50
DetectorEvent::coord_type coord
Coordinate of the channel in the group.
Definition: DAQChannels.hpp:48
DetectorEvent::group_type group
Group index of the channel.
Definition: DAQChannels.hpp:46