All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PMTDecoderUtils.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Decode/DecoderTools/details/PMTDecoderUtils.h
3  * @brief Some helpers for PMT decoder tool.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date April 13, 2021
6  */
7 
8 #ifndef ICARUSCODE_DECODE_DECODERTOOLS_DETAILS_PMTDECODERUTILS_H
9 #define ICARUSCODE_DECODE_DECODERTOOLS_DETAILS_PMTDECODERUTILS_H
10 
11 
12 // ICARUS/SBN libraries
13 #include "sbnobj/Common/PMT/Data/PMTconfiguration.h" // sbn::PMTconfiguration
15 
16 // LArSoft libraries
17 #include "lardataalg/Utilities/quantities/spacetime.h" // nanoseconds
18 
19 // C/C++ standard libraries
20 #include <ostream>
21 #include <algorithm> // std::lower_bound(), std::sort()
22 #include <vector>
23 #include <array>
24 #include <optional>
25 #include <string>
26 #include <utility> // std::move()
27 #include <tuple>
28 #include <limits>
29 #include <cstddef> // std::size_t
30 
31 
32 // -----------------------------------------------------------------------------
33 namespace daq::details {
34 
35  using namespace util::quantities::time_literals;
37 
38  // --- BEGIN -- Board information management ---------------------------------
39 
40  /// Information of the setup of a V1730 readout board.
41  struct BoardSetup_t {
42 
43  ///< V1730B has 16 channels (can become a template parameter).
44  static constexpr std::size_t NBoardChannels = 16U;
45 
46  /// Special value to mark the absence of fragment ID information.
47  static constexpr unsigned int NoFragmentID
48  = std::numeric_limits<unsigned int>::max();
49 
50  /// Special settings for one channel on the board.
51  struct ChannelSetup_t {
52 
53  /// Associated off-line (LArSoft) channel ID.
55 
56  /// Whether the channel should be forcedly ignored or acquired.
57  std::optional<bool> forcedSkip;
58 
59  /// Save the channel only when including the global trigger time.
60  bool onGlobalOnly = false;
61 
62  std::uint16_t minSpan = 0; ///< Minimum acceptable waveform span.
63 
64  /// Category of this channel (will become instance name).
65  std::string category;
66 
67  /// Returns if this channel is associated to a off-line channel number.
68  bool hasChannel() const { return isChannel(channelID); }
69 
70  /// Whether this channel is requested to be skipped.
71  bool mustSkip() const { return forcedSkip.value_or(false); }
72 
73  /// Whether this channel is requested to be saved.
74  bool mustSave() const { return !forcedSkip.value_or(true); }
75 
76  /// Returns whether this is the default configuration.
77  bool isDefault() const
78  {
79  // C++20: equality operator might be automatically generated
80  // return *this == ChannelSetup_t{};
81  return !hasChannel()
82  && !forcedSkip && !onGlobalOnly && category.empty();
83  }
84 
85  /// Returns if `channel` is a valid channel ID.
86  static bool isChannel(raw::Channel_t channel)
87  { return channel != sbn::V1730channelConfiguration::NoChannelID; }
88 
89  }; // ChannelSetup_t
90 
91  using AllChannelSetup_t = std::array<ChannelSetup_t, NBoardChannels>;
92 
93  std::string name; ///< Board name as specified in DAQ configuration.
94 
95  /// ID of the DAQ fragment associated to this board.
96  unsigned int fragmentID = NoFragmentID;
97 
98  nanoseconds triggerDelay = 0_ns; ///< Delay from global trigger to TTT set.
99 
100  nanoseconds TTTresetDelay = 0_ns; ///< Delay from TTT reset issue to enact.
101 
102  /// Set of settings channel by channel.
104 
105  /// Returns whether this object contains a valid fragment ID.
106  constexpr bool hasFragmentID() const { return fragmentID != NoFragmentID; }
107 
108  }; // struct BoardSetup_t
109 
110  std::ostream& operator<< (std::ostream&, BoardSetup_t::ChannelSetup_t const&);
111 
112  /// Derivative information of a V1730 readout board.
113  struct BoardFacts_t {
114  nanoseconds preTriggerTime; ///< How long from waveform start to PMT board trigger.
115  }; // struct BoardFacts_t
116 
117  class BoardInfoLookup;
118 
119  std::ostream& operator<< (std::ostream&, BoardInfoLookup const&);
120 
121  // --- END -- Board information management -----------------------------------
122 
123 
124  template <std::size_t KeyIndex = 0U>
126 
127  template <typename Coll, typename Key, typename KeyExtractor>
128  typename Coll::value_type const* binarySearch
129  (Coll const& coll, Key const& key, KeyExtractor&& extractKey);
130 
131  /// Finds the element in `coll` with the item `KeyIndex` equal to `key`.
132  /// @return a pointer to the item found, or `nullptr` if none
133  template <std::size_t KeyIndex = 0U, typename Coll, typename Key>
134  typename Coll::value_type const* binarySearch
135  (Coll const& coll, Key const& key);
136 
137 
138 } // namespace daq::details
139 
140 
141 
142 // -----------------------------------------------------------------------------
143 /// Utility class for fast lookup of board data by fragment ID.
145 
146  public:
147  using FragmentID_t = unsigned int;
148 
149  /// Record of information about a readout board.
150  struct BoardInfo_t {
151  FragmentID_t fragmentID = std::numeric_limits<FragmentID_t>::max();
152  BoardSetup_t const* setup = nullptr;
153  sbn::V1730Configuration const* config = nullptr;
155 
156  bool operator< (BoardInfo_t const& other) const
157  { return fragmentID < other.fragmentID; }
158  }; // struct BoardInfo_t
159 
160  using Database_t = std::vector<BoardInfo_t>;
161 
162 
163  explicit BoardInfoLookup(Database_t&& database)
164  : fDatabase(sortDatabase(std::move(database)))
165  {}
166 
167  /// Returns the number of registered boards.
168  unsigned int nBoardInfo() const { return fDatabase.size(); }
169 
170  /// Returns an object that can be iterated for all available information.
171  auto const& allBoardInfo() const { return fDatabase; } // C++20: replace with std::span<>
172 
173  /// Returns a pointer to the information for board with `fragmentID`.
174  BoardInfo_t const* findBoardInfo(FragmentID_t fragmentID) const;
175 
176  /// Returns a pointer to the setup information for board with `fragmentID`.
177  BoardSetup_t const* findBoardSetup(FragmentID_t fragmentID) const
178  {
179  auto const* pInfo = findBoardInfo(fragmentID);
180  return pInfo? pInfo->setup: nullptr;
181  }
182 
183  /// Returns a pointer to the configuration of board with `fragmentID`.
185  {
186  auto const* pInfo = findBoardInfo(fragmentID);
187  return pInfo? pInfo->config: nullptr;
188  }
189 
190 
191  private:
192 
194 
195  /// Sorts the database structure in the argument and returns it.
197  { std::sort(db.begin(), db.end()); return std::move(db); }
198 
199 }; // class daq::details::BoardInfoLookup
200 
201 
202 
203 // -----------------------------------------------------------------------------
204 // --- inline implementation
205 // -----------------------------------------------------------------------------
207  (FragmentID_t fragmentID) const -> BoardInfo_t const*
208 {
209  return binarySearch(fDatabase, fragmentID, std::mem_fn(&BoardInfo_t::fragmentID));
210 }
211 
212 
213 // -----------------------------------------------------------------------------
214 // --- template implementation
215 // -----------------------------------------------------------------------------
216 template <typename Coll, typename Key, typename KeyExtractor>
217 typename Coll::value_type const* daq::details::binarySearch
218  (Coll const& coll, Key const& key, KeyExtractor&& extractKey)
219 {
220  using std::begin, std::end;
221  auto const b = begin(coll);
222  auto const e = end(coll);
223  auto it = std::lower_bound(
224  b, e, key,
225  [&extractKey](auto const& a, Key const& key){ return extractKey(a) < key; }
226  );
227  return ((it != e) && (extractKey(*it) == key))? &*it: nullptr;
228 } // daq::details::binarySearch()
229 
230 
231 // -----------------------------------------------------------------------------
232 template <std::size_t KeyIndex /* = 0U */, typename Coll, typename Key>
233 typename Coll::value_type const* daq::details::binarySearch
234  (Coll const& coll, Key const& key)
235 {
236  return binarySearch
237  (coll, key, [](auto const& a){ return std::get<KeyIndex>(a); });
238 } // daq::details::binarySearch()
239 
240 
241 // -----------------------------------------------------------------------------
242 
243 
244 #endif // ICARUSCODE_DECODE_DECODERTOOLS_DETAILS_PMTDECODERUTILS_H
AllChannelSetup_t channelSettings
Set of settings channel by channel.
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
then source grid fermiapp products dune setup_dune_fermiapp sh exit else echo No setup file found exit fi setup
std::optional< bool > forcedSkip
Whether the channel should be forcedly ignored or acquired.
bool isDefault() const
Returns whether this is the default configuration.
Utility class for fast lookup of board data by fragment ID.
Derivative information of a V1730 readout board.
bool mustSave() const
Whether this channel is requested to be saved.
std::array< ChannelSetup_t, NBoardChannels > AllChannelSetup_t
constexpr bool operator<(CryostatID const &a, CryostatID const &b)
Order cryostats with increasing ID.
Definition: geo_types.h:706
bool mustSkip() const
Whether this channel is requested to be skipped.
unsigned int nBoardInfo() const
Returns the number of registered boards.
std::vector< BoardInfo_t > Database_t
BoardInfo_t const * findBoardInfo(FragmentID_t fragmentID) const
Returns a pointer to the information for board with fragmentID.
std::ostream & operator<<(std::ostream &, BoardSetup_t::ChannelSetup_t const &)
process_name gaushit a
BoardSetup_t const * findBoardSetup(FragmentID_t fragmentID) const
Returns a pointer to the setup information for board with fragmentID.
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
Special settings for one channel on the board.
Information from the configuration of PMT readout.
static bool isChannel(raw::Channel_t channel)
Returns if channel is a valid channel ID.
Coll::value_type const * binarySearch(Coll const &coll, Key const &key, KeyExtractor &&extractKey)
Information from the configuration of a V1730 PMT readout board.
Record of information about a readout board.
std::string name
Board name as specified in DAQ configuration.
nanoseconds_as<> nanoseconds
Type of time interval stored in nanoseconds, in double precision.
Definition: spacetime.h:270
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
sbn::V1730Configuration const * findBoardConfig(FragmentID_t fragmentID) const
Returns a pointer to the configuration of board with fragmentID.
BoardInfoLookup(Database_t &&database)
std::string category
Category of this channel (will become instance name).
constexpr bool hasFragmentID() const
Returns whether this object contains a valid fragment ID.
Dimensioned variables representing space or time quantities.
nanoseconds preTriggerTime
How long from waveform start to PMT board trigger.
do i e
bool hasChannel() const
Returns if this channel is associated to a off-line channel number.
static constexpr auto NoChannelID
Special value for unassigned channel ID.
auto const & allBoardInfo() const
Returns an object that can be iterated for all available information.
height to which particles are projected pnfs larsoft persistent physics cosmics Fermilab CORSIKA standard He_showers_ * db
static Database_t sortDatabase(Database_t &&db)
Sorts the database structure in the argument and returns it.
Information of the setup of a V1730 readout board.
nanosecond nanoseconds
Alias for common language habits.
Definition: spacetime.h:139
Class containing configuration for a V1730 board.