All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WindowTopologyAlg.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/PMT/Trigger/Algorithms/WindowTopologyAlg.h
3  * @brief Assembles the topology of trigger windows.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date March 25, 2021
6  * @see icaruscode/PMT/Trigger/Algorithms/WindowTopologyAlg.cxx
7  */
8 
9 
10 #ifndef ICARUSCODE_PMT_TRIGGER_ALGORITHMS_WINDOWTOPOLOGYALG_H
11 #define ICARUSCODE_PMT_TRIGGER_ALGORITHMS_WINDOWTOPOLOGYALG_H
12 
13 
14 // ICARUS libraries
17 #include "icaruscode/IcarusObj/OpDetWaveformMeta.h" // sbn::OpDetWaveformMeta
19 
20 // framework libraries
21 #include "cetlib_except/exception.h"
22 
23 // C/C++ standard libraries
24 #include <optional>
25 #include <vector>
26 #include <string>
27 
28 
29 // -----------------------------------------------------------------------------
30 // forward declaration
31 namespace geo {
32  class GeometryCore;
33  class CryostatGeo;
34 }
35 
36 // -----------------------------------------------------------------------------
37 namespace icarus::trigger {
38  class WindowTopologyAlg;
39  class WindowTopologyVerification;
40  class WindowTopologyManager;
41 }
42 // -----------------------------------------------------------------------------
43 /**
44  * @brief Algorithm to create trigger window topology information.
45  *
46  * The algorithm defines the windows based on trigger gates and their channels,
47  * and establishes their topology cryostat by cryostat.
48  *
49  */
52 {
53 
54  public:
55 
56  /// Type of trigger gate extracted from the input event.
57  using InputTriggerGate_t
59 
60  /// A list of trigger gates from input.
61  using TriggerGates_t = std::vector<InputTriggerGate_t>;
62 
63  /// Type of lists of gates, one list per cryostat (outer index: cryostat no).
64  using TriggerGatesPerCryostat_t = std::vector<TriggerGates_t>;
65 
66 
67  /**
68  * @brief Constructor.
69  * @param geom geometry service provider
70  * @param logCategory category tag for messages from this algorithm
71  */
73  geo::GeometryCore const& geom,
74  std::string const& logCategory = "WindowTopologyAlg"
75  );
76 
77  /**
78  * @brief Returns the topology of the windows described by the gates.
79  * @param gates a set of gates, grouped by cryostat
80  * @return topology of the windows described by the gates
81  *
82  * The input `gates` is a collection with as many entries as there are
83  * cryostats in the detector, the first entry collecting all gates from the
84  * first cryostat (`C:0`), the next all the gates from the second one (`C:2`),
85  * and so on.
86  *
87  * Each gate covers one or more channels: each window is made out of all the
88  * channels in a gate. The opening content of the gates is not considered.
89  */
91  (TriggerGatesPerCryostat_t const& gates) const;
92 
93 
94  /**
95  * @brief Returns the topology of the windows described by the gates.
96  * @param gates a set of gates
97  * @return topology of the windows described by the gates
98  *
99  * The input `gates` is a collection with all relevant gates in the detector.
100  *
101  * Each gate covers one or more channels: each window is made out of all the
102  * channels in a gate. The opening content of the gates is not considered.
103  *
104  * Gates are first split by the cryostat they belong to (cross-cryostat gates
105  * are forbidden), and then the window topology is extracted.
106  */
107  WindowChannelMap createFromGates(TriggerGates_t const& gates) const;
108 
109 
110  /**
111  * @brief Returns the topology of the windows described by the gates.
112  * @param gates a set of gates within a single cryostat
113  * @param cryo the cryostat the gates (and their channels) belong to
114  * @return topology of the windows described by the gates
115  *
116  * The input `gates` is a collection with all the gates from the specified
117  * cryostat `cryo`.
118  *
119  * Each gate covers one or more channels: each window is made out of all the
120  * channels in a gate. The opening content of the gates is not considered.
121  */
123  (TriggerGates_t const& windowChannels, geo::CryostatGeo const& cryo) const;
124 
125 
126  /**
127  * @brief Returns the topology of the windows described by the gates.
128  * @param gates a set of gates within a single cryostat
129  * @param cryoID ID of the cryostat the gates (and their channels) belong to
130  * @return topology of the windows described by the gates
131  * @see `createFromCryostatGates(TriggerGates_t const&, geo::CryostatGeo const&)`
132  *
133  * The input `gates` is a collection with all the gates from the specified
134  * cryostat with ID `cryoID`.
135  *
136  * See `createFromCryostatGates(WindowChannelsPerCryostat_t const&, geo::CryostatGeo const&)`
137  * for more information.
138  */
140  (TriggerGates_t const& windowChannels, geo::CryostatID cryoID) const;
141 
142 
143  private:
144  /// All channels in a gate.
145  using WindowChannels_t = std::vector<raw::Channel_t>;
146 
147  /// All channels in many gates (one list per gate).
148  using WindowChannelColl_t = std::vector<WindowChannels_t>;
149 
150 
151  geo::GeometryCore const* const fGeom; ///< Geometry service provider.
152 
153 
154  /// Convenience function: creates and returns a `WindowChannelMap` from the
155  /// arguments (`args`); in the meanwhile, it also dumps it into debug stream.
156  template <typename... Args>
157  WindowChannelMap emplaceAndDumpMap(Args&&... args) const;
158 
159 
160  /**
161  * @brief Extracts topology information from a set of windows.
162  * @param windowChannels the windows, specified by composition in channel ID
163  * @param cryo the cryostat the windows belong to
164  * @param geom geometry service provider
165  * @param firstWindowIndex (default: `0`) the index assigned to the first
166  * window
167  * @return window topology information, one entry per window
168  */
169  static std::vector<WindowChannelMap::WindowInfo_t> createWindowsFromCryostat(
170  WindowChannelColl_t const& windowChannels,
171  geo::CryostatGeo const& cryo,
172  geo::GeometryCore const& geom,
173  std::size_t firstWindowIndex = 0U
174  );
175 
176  /**
177  * @brief Extracts the channel ID from a collection of gates.
178  * @param gates a collection of gates
179  * @return a collection of channel lists
180  *
181  * The returned collection has one entry per input gate in `gates`: each entry
182  * is the list of channels that the corresponding gate covers.
183  */
185 
186  /**
187  * @brief Returns the window in `windowList` closest to the `targetWindow`.
188  * @param windowList list of (pointers to) the candidate windows
189  * @param targetWindow the reference window
190  * @return a pointer to the closest among `windowList`, `nullptr` if none
191  *
192  * A pointer to the window with the smallest distance from `targetWindow` is
193  * returned. The distance is computed between the centers of the windows as
194  * reported by the windows themselves.
195  *
196  * The only case where no window (`nullptr`) is returned is when the window
197  * candidate list `windowList` is empty.
198  */
200  std::vector<WindowChannelMap::WindowInfo_t*> const& windowList,
201  WindowChannelMap::WindowInfo_t const* targetWindow
202  );
203 
204 }; // icarus::trigger::WindowTopologyAlg
205 
206 
207 //------------------------------------------------------------------------------
208 /**
209  * @brief Algorithm verifying the topology from trigger gates.
210  *
211  * The algorithm needs to be provided a reference topology information.
212  * It will then analyze any set of trigger gates to confirm that their grouping
213  * matches the one defined in that topology.
214  */
217 {
218 
219  /// The reference topology to check against.
220  std::optional<icarus::trigger::WindowChannelMap> fWindowMap;
221 
222  public:
223 
224  /// Type of sets of trigger gates.
226 
227  /// Type of sets of trigger gates, grouped by cryostat.
230 
231 
232  /**
233  * @brief Constructor.
234  * @param topology the window topology to be verified against
235  * @param logCategory category tag for messages from this algorithm
236  */
239  std::string const& logCategory = "WindowTopologyVerification"
240  );
241 
242  /**
243  * @brief Constructor.
244  * @param logCategory category tag for messages from this algorithm
245  *
246  * Topology must be set up with `setTopology()` before any verification can
247  * occur.
248  */
250  (std::string const& logCategory = "WindowTopologyVerification");
251 
252 
253  /// Returns whether a window topology is set (see `setTopology()`).
254  bool hasTopology() const;
255 
256  /// Resets the topology to be verified against.
258 
259  /// Forgets the current topology. Set a new one with `setTopology()`.
260  void clearTopology();
261 
262  /// Returns the current window topology.
263  /// @throw std::bad_optional_access if no topology has been set
264  WindowChannelMap const& getTopology() const;
265 
266  //@{
267  /**
268  * @brief Verifies that `gates` match the topology current set up.
269  * @param gates the set of gates to match to the topology
270  * @return a composite error message (empty on success)
271  * @throw cet::exception (category: `WindowTopologyVerification`) if no window
272  * topology is set up yet
273  *
274  * Gates can be specified either as a collection, or as a set of collections,
275  * one per cryostat.
276  */
277  std::string verify(TriggerGatesPerCryostat_t const& gates) const;
278  std::string verify(TriggerGates_t const& gates) const;
279  //@}
280 
281  //@{
282  /**
283  * @brief Verifies that `gates` match the topology current set up.
284  * @param gates the set of gates to match to the topology
285  * @throw cet::exception (category: `WindowTopologyVerification`) if no window
286  * topology is set up yet
287  * @throw cet::exception (category: `WindowTopologyVerification`) if
288  * verification fails
289  * @see `verify()`
290  */
291  void operator() (TriggerGatesPerCryostat_t const& gates) const;
292  void operator() (TriggerGates_t const& gates) const;
293  //@}
294 
295 
296  private:
297 
298  /// Type of trigger gate used for input to the algorithm.
299  using InputTriggerGate_t
301 
302  /// Checks that the specified `gate` matched the window with index `iWindow`.
303  /// @return message describing the failure, empty on success
304  std::string verifyGate
305  (std::size_t iWindow, InputTriggerGate_t const& gate) const;
306 
307  /// Implementation of `operator()`.
308  template <typename Gates>
309  void verifyOrThrow(Gates const& gates) const;
310 
311 }; // icarus::trigger::WindowTopologyVerification
312 
313 
314 //------------------------------------------------------------------------------
315 /**
316  * @brief Class to extract and verify a window topology from trigger gates.
317  *
318  * This class is meant to provide a convenient interface for the creation
319  * (`WindowTopologyAlg`) and following verification
320  * (`WindowTopologyVerification`) of a window topology.
321  * The first time the class is given a trigger gate set, it extracts the
322  * topology from it. The following times, it will verify that the gates match
323  * that topology, and throw an exception otherwise.
324  *
325  * The class can be reset to restart the cycle, and the topology can be obtained
326  * directly with the `operator->`.
327  *
328  * The main methods support different types of input, collectively known as
329  * `Gates`. The complete set of supported input depends on what is supported
330  * by the underlying classes. Currently, supported input includes (flat)
331  * collections of gates and collections of gates grouped by cryostat.
332  */
335 {
336 
337  /// Verification algorithm; holds the current topology information.
339 
340  geo::GeometryCore const* const fGeom; ///< Geometry service provider.
341 
342  public:
343 
344  /// Type of sets of trigger gates.
346 
347  /// Type of sets of trigger gates, grouped by cryostat.
350 
351 
352  /**
353  * @brief Constructor.
354  * @param topology the window topology to be verified against
355  * @param logCategory category tag for messages from this algorithm
356  */
358  geo::GeometryCore const& geom,
359  std::string const& logCategory = "WindowTopologyManager"
360  );
361 
362  /**
363  * @brief Extracts topology or verifies it against `gates` .
364  * @tparam Gates type of gate collection
365  * @param gates the set of gates to match to the topology
366  * @return a composite error message (empty on success)
367  *
368  */
369  template <typename Gates>
370  std::string setOrVerify(Gates const& gates);
371 
372  /**
373  * @brief Extracts topology or verifies it against `gates` .
374  * @tparam Gates type of gate collection
375  * @param gates the set of gates to match to the topology
376  * @return whether this method extracted the topology
377  * @throw cet::exception (category: `WindowTopologyVerification`) if
378  * verification fails
379  */
380  template <typename Gates>
381  bool operator() (Gates const& gates);
382 
383  /// Access to the stored topology.
384  /// @throw std::bad_optional_access if no topology has been set
385  WindowChannelMap const& operator* () const;
386 
387  /// Access to the stored topology.
388  /// @throw std::bad_optional_access if no topology has been set
389  WindowChannelMap const* operator-> () const;
390 
391 
392 
393  private:
394 
395  /// Helper: creates the topology from `gates`.
396  template <typename Gates>
397  void extractTopology(Gates const& gates);
398 
399 }; // icarus::trigger::WindowTopologyManager
400 
401 
402 // -----------------------------------------------------------------------------
403 // --- inline implementation
404 // -----------------------------------------------------------------------------
405 // --- icarus::trigger::WindowTopologyVerification
406 // -----------------------------------------------------------------------------
409  std::string const& logCategory /* = "WindowTopologyVerification" */
410  )
411  : icarus::ns::util::mfLoggingClass(logCategory)
412  , fWindowMap(std::move(topology))
413 {}
414 
415 
416 // -----------------------------------------------------------------------------
418  std::string const& logCategory /* = "WindowTopologyVerification" */
419  )
420  : icarus::ns::util::mfLoggingClass(logCategory)
421 {}
422 
423 
424 // -----------------------------------------------------------------------------
426  { return fWindowMap.has_value(); }
427 
428 
429 // -----------------------------------------------------------------------------
432  { fWindowMap = std::move(topology); }
433 
434 
435 //------------------------------------------------------------------------------
438  { return fWindowMap.value(); }
439 
440 
441 //------------------------------------------------------------------------------
443  { return fWindowMap.reset(); }
444 
445 
446 //------------------------------------------------------------------------------
447 inline void icarus::trigger::WindowTopologyVerification::operator()
448  (TriggerGates_t const& gates) const
449  { verifyOrThrow(gates); }
450 
451 
452 //------------------------------------------------------------------------------
453 inline void icarus::trigger::WindowTopologyVerification::operator()
454  (TriggerGatesPerCryostat_t const& gates) const
455  { verifyOrThrow(gates); }
456 
457 
458 //------------------------------------------------------------------------------
459 //--- icarus::trigger::WindowTopologyManager
460 //------------------------------------------------------------------------------
462  geo::GeometryCore const& geom,
463  std::string const& logCategory /* = "WindowTopologyManager" */
464  )
465  : icarus::ns::util::mfLoggingClass(logCategory)
466  , fVerify{ logCategory }
467  , fGeom(&geom)
468 {}
469 
470 
471 //------------------------------------------------------------------------------
474  { return fVerify.getTopology(); }
475 
476 
477 //------------------------------------------------------------------------------
480  { return &(fVerify.getTopology()); }
481 
482 
483 //------------------------------------------------------------------------------
484 //--- Template implementation
485 //------------------------------------------------------------------------------
486 //--- icarus::trigger::WindowTopologyVerification
487 // -----------------------------------------------------------------------------
488 template <typename Gates>
490  (Gates const& gates) const
491 {
492 
493  std::string const errorMsg = verify(gates);
494  if (errorMsg.empty()) return;
495 
496  // put together the exception message and throw it.
497  throw cet::exception("WindowTopologyVerification")
498  << "Some channels from trigger gates do not match the previous window allocation:\n"
499  << errorMsg
500  << "\n"
501  << "Window allocation: "
502  << fWindowMap.value()
503  ;
504 
505 } // icarus::trigger::WindowTopologyVerification::operator()
506 
507 
508 //------------------------------------------------------------------------------
509 //--- icarus::trigger::WindowTopologyManager
510 //------------------------------------------------------------------------------
511 template <typename Gates>
513  (Gates const& gates)
514 {
515  if (fVerify.hasTopology()) {
516  return fVerify.verify(gates);
517  }
518  else {
519  extractTopology(gates);
520  return {};
521  }
522 } // icarus::trigger::WindowTopologyManager::setOrVerify()
523 
524 
525 //------------------------------------------------------------------------------
526 template <typename Gates>
528  if (fVerify.hasTopology()) {
529  fVerify(gates);
530  return false;
531  }
532  else {
533  extractTopology(gates);
534  return true;
535  }
536 } // icarus::trigger::WindowTopologyManager::operator()
537 
538 
539 //------------------------------------------------------------------------------
540 template <typename Gates>
542 {
543  icarus::trigger::WindowTopologyAlg const topoMaker
544  { *fGeom, logCategory() + "_Extractor" };
545  fVerify.setTopology(topoMaker.createFromGates(gates));
546 } // icarus::trigger::WindowTopologyManager::extractTopology()
547 
548 
549 //------------------------------------------------------------------------------
550 
551 #endif // ICARUSCODE_PMT_TRIGGER_ALGORITHMS_WINDOWTOPOLOGYALG_H
552 
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
icarus::trigger::WindowTopologyAlg::TriggerGatesPerCryostat_t TriggerGatesPerCryostat_t
Type of sets of trigger gates, grouped by cryostat.
icarus::trigger::WindowTopologyAlg::TriggerGates_t TriggerGates_t
Type of sets of trigger gates.
Class to extract and verify a window topology from trigger gates.
std::vector< InputTriggerGate_t > TriggerGates_t
A list of trigger gates from input.
static WindowChannelColl_t extractGateChannels(TriggerGates_t const &gates)
Extracts the channel ID from a collection of gates.
A wrapper to trigger gate objects tracking the input of operations.
Base class facilitating logging to message facility.
WindowChannelMap const * operator->() const
Derivative information from raw::OpDetWaveform data.
WindowChannelMap const & getTopology() const
icarus::trigger::TrackedOpticalTriggerGate< sbn::OpDetWaveformMeta > InputTriggerGate_t
Type of trigger gate extracted from the input event.
std::vector< TriggerGates_t > TriggerGatesPerCryostat_t
Type of lists of gates, one list per cryostat (outer index: cryostat no).
Information about composition and topology of trigger sliding windows.
icarus::trigger::WindowTopologyAlg::TriggerGatesPerCryostat_t TriggerGatesPerCryostat_t
Type of sets of trigger gates, grouped by cryostat.
icarus::trigger::WindowTopologyAlg::TriggerGates_t TriggerGates_t
Type of sets of trigger gates.
std::vector< WindowChannels_t > WindowChannelColl_t
All channels in many gates (one list per gate).
Geometry information for a single cryostat.
Definition: CryostatGeo.h:43
std::optional< icarus::trigger::WindowChannelMap > fWindowMap
The reference topology to check against.
Helper for logging classes.
void verifyOrThrow(Gates const &gates) const
Implementation of operator().
void clearTopology()
Forgets the current topology. Set a new one with setTopology().
geo::GeometryCore const *const fGeom
Geometry service provider.
WindowTopologyVerification(icarus::trigger::WindowChannelMap topology, std::string const &logCategory="WindowTopologyVerification")
Constructor.
WindowChannelMap const & operator*() const
WindowTopologyAlg(geo::GeometryCore const &geom, std::string const &logCategory="WindowTopologyAlg")
Constructor.
WindowChannelMap emplaceAndDumpMap(Args &&...args) const
bool operator()(Gates const &gates)
Extracts topology or verifies it against gates .
bool hasTopology() const
Returns whether a window topology is set (see setTopology()).
std::string verifyGate(std::size_t iWindow, InputTriggerGate_t const &gate) const
void extractTopology(Gates const &gates)
Helper: creates the topology from gates.
WindowChannelMap createFromGates(TriggerGatesPerCryostat_t const &gates) const
Returns the topology of the windows described by the gates.
Algorithm verifying the topology from trigger gates.
std::vector< raw::Channel_t > WindowChannels_t
All channels in a gate.
Description of geometry of one entire detector.
geo::GeometryCore const *const fGeom
Geometry service provider.
Data structure enclosing information for trigger sliding windows.
Algorithm to create trigger window topology information.
static WindowChannelMap::WindowInfo_t const * findClosestWindow(std::vector< WindowChannelMap::WindowInfo_t * > const &windowList, WindowChannelMap::WindowInfo_t const *targetWindow)
Returns the window in windowList closest to the targetWindow.
std::string verify(TriggerGatesPerCryostat_t const &gates) const
Verifies that gates match the topology current set up.
A simple alias for a most commonly used TrackedTriggerGate type.
static std::vector< WindowChannelMap::WindowInfo_t > createWindowsFromCryostat(WindowChannelColl_t const &windowChannels, geo::CryostatGeo const &cryo, geo::GeometryCore const &geom, std::size_t firstWindowIndex=0U)
Extracts topology information from a set of windows.
WindowChannelMap createFromCryostatGates(TriggerGates_t const &windowChannels, geo::CryostatGeo const &cryo) const
Returns the topology of the windows described by the gates.
std::string setOrVerify(Gates const &gates)
Extracts topology or verifies it against gates .
void operator()(TriggerGatesPerCryostat_t const &gates) const
Verifies that gates match the topology current set up.
void setTopology(icarus::trigger::WindowChannelMap topology)
Resets the topology to be verified against.
std::string logCategory() const
Returns the logging category string for this object.
WindowTopologyVerification fVerify
Verification algorithm; holds the current topology information.
WindowTopologyManager(geo::GeometryCore const &geom, std::string const &logCategory="WindowTopologyManager")
Constructor.
The data type to uniquely identify a cryostat.
Definition: geo_types.h:190