All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TriggerGateBuilder.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/PMT/Trigger/Algorithms/TriggerGateBuilder.h
3  * @brief Algorithm to produce trigger gates out of optical readout waveforms.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date April 1, 2019
6  * @see `icaruscode/PMT/Trigger/Algorithms/TriggerGateBuilder.cxx`
7  *
8  */
9 
10 #ifndef ICARUSCODE_PMT_TRIGGER_ALGORITHMS_TRIGGERGATEBUILDER_H
11 #define ICARUSCODE_PMT_TRIGGER_ALGORITHMS_TRIGGERGATEBUILDER_H
12 
13 
14 // ICARUS libraries
15 #include "icaruscode/PMT/Trigger/Algorithms/TriggerTypes.h" // icarus::trigger::ADCCounts_t
19 
20 // LArSoft libraries
24 
25 // framework libraries
26 #include "fhiclcpp/types/Atom.h"
27 #include "fhiclcpp/types/Sequence.h"
28 
29 // C/C++ standard libraries
30 #include <vector>
31 #include <tuple>
32 #include <algorithm> // std::count_if()
33 #include <functional> // std::mem_fn()
34 #include <optional>
35 #include <cstddef> // std::size_t
36 
37 
38 namespace icarus::trigger {
39 
40  // ---------------------------------------------------------------------------
41  //
42  // declarations
43  //
44 
45  struct WaveformWithBaseline;
46 
47  class TriggerGateBuilder;
48 
49  // ---------------------------------------------------------------------------
50 
51 } // namespace icarus::trigger
52 
53 
54 //------------------------------------------------------------------------------
55 /// Object to carry around waveform ant its baseline.
57  : std::tuple<raw::OpDetWaveform const*, icarus::WaveformBaseline const*>
58 {
59 
62  using Base_t = std::tuple<Waveform_t const*, Baseline_t const*>;
63 
64  using Base_t::Base_t; // inherit constructors
65 
66  /// Returns a reference to the waveform.
67  Waveform_t const& waveform() const { return *waveformPtr(); }
68 
69  /// Returns a reference to the waveform baseline.
70  Baseline_t const& baseline() const { return *baselinePtr(); }
71 
72  /// Returns a pointer to the waveform.
73  Waveform_t const* waveformPtr() const
74  { return std::get<Waveform_t const*>(*this); }
75 
76  /// Returns a pointer to the waveform baseline.
77  Baseline_t const* baselinePtr() const
78  { return std::get<Baseline_t const*>(*this); }
79 
80  /// Returns whether the baseline is available for this waveform.
81  bool hasBaseline() const { return baselinePtr() != nullptr; }
82 
83 
84  // questionable practices...
85  operator Waveform_t const& () const { return waveform(); }
86  operator Waveform_t const* () const { return waveformPtr(); }
87  operator Baseline_t const& () const { return baseline(); }
88  operator Baseline_t const* () const { return baselinePtr(); }
89 
90 }; // struct icarus::trigger::WaveformWithBaseline
91 
92 
93 //------------------------------------------------------------------------------
94 /**
95  * @brief Algorithm to produce trigger gates out of optical readout waveforms.
96  * @see `icarus::trigger::DynamicTriggerGateBuilder`,
97  * `icarus::trigger::FixedTriggerGateBuilder`
98  *
99  * This is an abstract class.
100  * Derived algorithms need to provide a way to actually `build()` the gates.
101  */
103 
104  public:
105 
106  // --- BEGIN Data types ------------------------------------------------------
107 
109 
110  /// Mnemonic for an invalid optical detector channel.
111  static constexpr Channel_t InvalidChannel
112  = std::numeric_limits<Channel_t>::max();
113 
114 
115  /// Container of logical gates for all triggering channels for a threshold.
116  class TriggerGates {
117 
118  public:
119 
120  // we need a gate type able to track its source, and the source here is
121  // unequivocally a waveform
122  using triggergate_t
124  using GateData_t = std::vector<triggergate_t>;
125 
126 
127  /// Constructor: acquires data of the specified gates.
128  TriggerGates(ADCCounts_t threshold, GateData_t&& gates)
129  : fThreshold(threshold), fGates(std::move(gates))
130  {}
131 
132  /// Constructor: no trigger gate added.
134  : TriggerGates(threshold, {})
135  {}
136 
137  /// Returns the threshold used for the gates.
138  ADCCounts_t threshold() const { return fThreshold; }
139 
140  /// Returns the complete collection of trigger gates on all channels.
141  GateData_t const& gates() const& { return fGates; }
142 
143  /**
144  * @brief Yields the complete collection of trigger gates on all channels.
145  * @return the gates that were contained in this object
146  *
147  * After this call, the gate information is lost (except for the threshold).
148  */
149  GateData_t gates() && { return std::move(fGates); }
150 
151  /// Returns (and creates, if necessary) the gate for the specified waveform.
152  triggergate_t& gateFor(raw::OpDetWaveform const& waveform);
153 
154  /// Dumps the content of this set of gates into the `out` stream.
155  template <typename Stream>
156  void dump(Stream& out) const;
157 
158  /// Comparison: sorts by increasing threshold.
159  bool operator< (TriggerGates const& other) const
160  { return threshold() < other.threshold(); }
161 
162 
163  static constexpr bool isValidChannel(Channel_t channel)
164  { return channel != InvalidChannel; }
165 
166  private:
167  ADCCounts_t fThreshold; ///< The threshold for all the gates.
168  GateData_t fGates; ///< All the gates, at most one per channel.
169 
170  }; // class TriggerGates
171  // --- END Data types --------------------------------------------------------
172 
173 
174  // --- BEGIN Configuration ---------------------------------------------------
175  struct Config {
176 
177  using Name = fhicl::Name;
178  using Comment = fhicl::Comment;
179 
180 
181  fhicl::Sequence<ADCCounts_t::value_t> ChannelThresholds {
182  Name("ChannelThresholds"),
183  Comment("triggering thresholds [ADC counts]")
184  // mandatory
185  };
186 
187 
188  }; // struct Config
189  // --- END Configuration -----------------------------------------------------
190 
191 
192  /// Constructor: sets the configuration.
193  TriggerGateBuilder(Config const& config);
194 
195  /// Virtual destructor. Nothing special about it, except that it's virtual.
196  virtual ~TriggerGateBuilder() = default;
197 
198  /// Algorithm setup.
199  virtual void setup(detinfo::DetectorTimings const& timings);
200 
201  /// Algorithm reset. It will require a new setup before using it again.
202  virtual void reset() {}
203 
204  /// Resets and sets up.
205  virtual void resetup(detinfo::DetectorTimings const& timings)
206  { reset(); setup(timings); }
207 
208  /// Resets and sets up (including a new set of thresholds).
209  virtual void resetup(
210  detinfo::DetectorTimings const& timings,
211  std::vector<ADCCounts_t> const& thresholds
212  )
213  { resetup(timings); doSetThresholds(thresholds); }
214 
215  /// Returns a collection of `TriggerGates` objects sorted by threshold.
216  virtual std::vector<TriggerGates> build
217  (std::vector<WaveformWithBaseline> const& waveforms) const = 0;
218 
219  /// Returns all the configured thresholds.
220  std::vector<ADCCounts_t> const& channelThresholds() const
221  { return fChannelThresholds; }
222 
223  /// Returns the number of configured thresholds.
224  std::size_t nChannelThresholds() const { return channelThresholds().size(); }
225 
226 
227  /// Converts a time [&micro;s] into optical ticks.
229 
230  /// Converts a timestamp from `raw::OpDetWaveform` into optical ticks.
232 
233 
234  /// Returns whether `channel` is valid.
235  static constexpr bool isValidChannel(Channel_t channel)
236  { return channel != InvalidChannel; }
237 
238  protected:
239 
240  /// Returns a detector timings object.
241  detinfo::DetectorTimings const& detTimings() const { return *fDetTimings; }
242 
243  /// Creates an empty TriggerGates object for each threshold;
244  /// thresholds are kept relative.
245  std::vector<TriggerGates> prepareAllGates() const;
246 
247  /// Sets all thresholds anew.
248  virtual void doSetThresholds(std::vector<ADCCounts_t> const& thresholds)
249  { fChannelThresholds = thresholds; }
250 
251  private:
252 
253  // --- BEGIN Configuration parameters ----------------------------------------
254 
255  /// All single channel thresholds, sorted in increasing order.
256  std::vector<ADCCounts_t> fChannelThresholds;
257 
258  // --- END Configuration parameters ------------------------------------------
259 
260 
261  // --- BEGIN Setup -----------------------------------------------------------
262 
263  /// LArSoft detector timing utility.
264  std::optional<detinfo::DetectorTimings> fDetTimings;
265 
266  // --- END Setup -------------------------------------------------------------
267 
268 
269 }; // class icarus::trigger::TriggerGateBuilder
270 
271 
272 //------------------------------------------------------------------------------
273 namespace icarus::trigger::details {
274 
275  // Category used for debugging information output
276  constexpr auto TriggerGateDebugLog = "TriggerSimulation";
277 
278 } // namespace icarus::trigger::details
279 
280 
281 //------------------------------------------------------------------------------
282 //--- template implementation
283 //------------------------------------------------------------------------------
284 template <typename Stream>
286 {
287 
288  auto const nOpenGates = fGates.size() - std::count_if(
289  fGates.begin(), fGates.end(),
290  [](auto const& gate){ return gate.gate().alwaysClosed(); }
291  );
292  out << nOpenGates << "/" << fGates.size() << " trigger gates on threshold "
293  << fThreshold;
294  if (nOpenGates) {
295  out << ":";
296  for (auto const& gate: icarus::trigger::gatesIn(fGates)) {
297  if (gate.alwaysClosed()) continue;
298  out << "\n " << gate;
299  }
300  }
301  out << "\n";
302 } // icarus::trigger::TriggerGateBuilder::TriggerGates::dump()
303 
304 
305 //------------------------------------------------------------------------------
306 
307 #endif // ICARUSCODE_PMT_TRIGGER_ALGORITHMS_TRIGGERGATEBUILDER_H
virtual void setup(detinfo::DetectorTimings const &timings)
Algorithm setup.
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
bool hasBaseline() const
Returns whether the baseline is available for this waveform.
std::vector< ADCCounts_t > const & channelThresholds() const
Returns all the configured thresholds.
A wrapper to trigger gate objects tracking the input of operations.
TriggerGates(ADCCounts_t threshold, GateData_t &&gates)
Constructor: acquires data of the specified gates.
microsecond_as<> microsecond
Type of time stored in microseconds, in double precision.
Definition: spacetime.h:119
fhicl::Sequence< ADCCounts_t::value_t > ChannelThresholds
virtual ~TriggerGateBuilder()=default
Virtual destructor. Nothing special about it, except that it&#39;s virtual.
auto gatesIn(TrackingGateColl &trackingGates)
double TimeStamp_t
us since 1970, based on TimeService
virtual void resetup(detinfo::DetectorTimings const &timings)
Resets and sets up.
Waveform_t const * waveformPtr() const
Returns a pointer to the waveform.
pure virtual base interface for detector clocks
std::optional< detinfo::DetectorTimings > fDetTimings
LArSoft detector timing utility.
optical_tick timeStampToOpticalTick(raw::TimeStamp_t time) const
Converts a timestamp from raw::OpDetWaveform into optical ticks.
Interface to detinfo::DetectorClocks.
icarus::trigger::TrackedOpticalTriggerGate< raw::OpDetWaveform > triggergate_t
std::size_t nChannelThresholds() const
Returns the number of configured thresholds.
detinfo::DetectorTimings const & detTimings() const
Returns a detector timings object.
Container of logical gates for all triggering channels for a threshold.
bool operator<(TriggerGates const &other) const
Comparison: sorts by increasing threshold.
virtual void reset()
Algorithm reset. It will require a new setup before using it again.
virtual void doSetThresholds(std::vector< ADCCounts_t > const &thresholds)
Sets all thresholds anew.
constexpr auto TriggerGateDebugLog
Baseline_t const & baseline() const
Returns a reference to the waveform baseline.
void dump(Stream &out) const
Dumps the content of this set of gates into the out stream.
A value measured in the specified unit.
Definition: quantities.h:566
Simple type definitions for trigger algorithms.
ADCCounts_t fThreshold
The threshold for all the gates.
BEGIN_PROLOG vertical distance to the surface Name
virtual void resetup(detinfo::DetectorTimings const &timings, std::vector< ADCCounts_t > const &thresholds)
Resets and sets up (including a new set of thresholds).
std::vector< TriggerGates > prepareAllGates() const
TriggerGateBuilder(Config const &config)
Constructor: sets the configuration.
static constexpr bool isValidChannel(Channel_t channel)
Returns whether channel is valid.
std::tuple< Waveform_t const *, Baseline_t const * > Base_t
Baseline_t const * baselinePtr() const
Returns a pointer to the waveform baseline.
A wrapper to trigger gate objects tracking the contributions.
timescale_traits< OpticalTimeCategory >::tick_t optical_tick
static constexpr bool isValidChannel(Channel_t channel)
std::vector< ADCCounts_t > fChannelThresholds
All single channel thresholds, sorted in increasing order.
optical_tick timeToOpticalTick(microsecond time) const
Converts a time [s] into optical ticks.
virtual std::vector< TriggerGates > build(std::vector< WaveformWithBaseline > const &waveforms) const =0
Returns a collection of TriggerGates objects sorted by threshold.
Waveform_t const & waveform() const
Returns a reference to the waveform.
A class exposing an upgraded interface of detinfo::DetectorClocksData.
Class containing a waveform baseline value.
GateData_t fGates
All the gates, at most one per channel.
A simple alias for a most commonly used TrackedTriggerGate type.
TriggerGates(ADCCounts_t threshold)
Constructor: no trigger gate added.
bnb BNB Stream
Algorithm to produce trigger gates out of optical readout waveforms.
Object to carry around waveform ant its baseline.
static constexpr Channel_t InvalidChannel
Mnemonic for an invalid optical detector channel.