DetectorClocksStandardTriggerLoader.h
Go to the documentation of this file.
1 /**
2  * @file lardataalg/DetectorInfo/DetectorClocksStandardTriggerLoader.h
3  * @brief Functions to load trigger time in `detinfo::DetectorClocksStandard`.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date March 21, 2018
6  *
7  * This header contains framework-dependent functions to:
8  * * set `DetectorClocksStandard` trigger time from a `raw::Trigger`
9  * * set `DetectorClocksStandard` trigger time from a `raw::Trigger` in an event
10  * * do the former, and fall back to default values on failure
11  *
12  * These functions are compatible with both _art_ and _gallery_.
13  *
14  * This is a pure header library. Effective dependencies include:
15  * * `lardata_DetectorInfo`
16  * * `lardataobj_RawData`
17  * * `canvas`
18  * * `cetlib_except`
19  *
20  */
21 
22 #ifndef LARDATAALG_DETECTORINFO_DETECTORCLOCKSSTANDARDTRIGGERLOADER_H
23 #define LARDATAALG_DETECTORINFO_DETECTORCLOCKSSTANDARDTRIGGERLOADER_H
24 
25 // LArSoft libraries
26 #include "lardataobj/RawData/TriggerData.h" // raw::Trigger
27 
28 // framework libraries
30 #include "cetlib_except/exception.h"
31 
32 // C++ standard libraries
33 #include <optional>
34 #include <vector>
35 
36 namespace detinfo {
37 
38  /**
39  * @brief Loads `DetectorClocksStandard` trigger times.
40  * @tparam Event type of event where trigger data might be stored
41  * @param detClocks the instance of `detinfo::DetectorClocksStandard` to set
42  * @param event the event the trigger objects are stored into
43  * @return whether the times were set from a trigger object
44  * @throws cet::exception if trigger data product has more than one trigger
45  *
46  * This function sets the trigger and beam gate times of `detClocks`.
47  * First, it attempts to read the settings from the event (see
48  * `setDetectorClocksStandardTriggersFromEvent()`).
49  * If that fails "nicely", then sets them with the default values from the
50  * configuration.
51  *
52  * A "nice" failure is where there is no trigger object in the event.
53  * Other types of failure include when there are more than one trigger objects
54  * in the event, in which case no choice is made, and an exception is thrown.
55  */
56  template <typename Event>
57  std::optional<std::pair<double, double>>
58  trigger_times_for_event(art::InputTag const& triggerTag, Event const& event)
59  {
60  // try to read the trigger from the event
61  // fetch the trigger data product
62  using TriggerHandle_t = typename Event::template HandleT<std::vector<raw::Trigger>>;
63 
64  TriggerHandle_t triggerHandle;
65  if (!event.template getByLabel(triggerTag, triggerHandle)) { return std::nullopt; }
66 
67  // check that we do have a trigger
68  // (we have already checked whether the handle is valid above)
69  auto const& triggers = *triggerHandle;
70  if (triggers.empty()) { return std::nullopt; }
71 
72  // select which trigger to set (i.e., the only one!)
73  if (triggers.size() != 1) {
74  throw cet::exception("setDetectorClocksStandardTrigger")
75  << "Found " << triggers.size() << " trigger objects in '" << triggerTag.encode()
76  << "' (only one trigger per event is supported)\n";
77  }
78 
79  auto const& trigger = triggers.front();
80  return std::make_optional(std::make_pair(trigger.TriggerTime(), trigger.BeamGateTime()));
81  }
82 
83  /**
84  * @brief Loads `DetectorClocksStandard` G4Ref correction times.
85  * @tparam Event type of event where trigger data might be stored
86  * @param detClocks the instance of `detinfo::DetectorClocksStandard` to set
87  * @param event the event the trigger objects are stored into
88  * @return whether the g4 ref correction was set from a trigger object
89  * @throws cet::exception if trigger data product has more than one trigger
90  *
91  * This function sets the trigger and beam gate times of `detClocks`.
92  * First, it attempts to read the settings from the event (see
93  * `setDetectorClocksStandardG4RefTimeCorrectionFromEvent()`).
94  * If that fails "nicely", then sets them with the default values from the
95  * configuration.
96  *
97  * A "nice" failure is where there is no trigger object in the event.
98  * Other types of failure include when there are more than one trigger objects
99  * in the event, in which case no choice is made, and an exception is thrown.
100  */
101  template <typename Event>
102  std::optional<double>
103  g4ref_time_for_event(art::InputTag const& triggerTag, Event const& event)
104  {
105  // fetch the trigger data product
106  using TriggerHandle_t = typename Event::template HandleT<std::vector<raw::Trigger>>;
107 
108  TriggerHandle_t triggerHandle;
109  if (!event.template getByLabel(triggerTag, triggerHandle)) return std::nullopt;
110 
111  // check that we do have a trigger
112  // (we have already checked whether the handle is valid above)
113  auto const& triggers = *triggerHandle;
114  if (triggers.empty()) return std::nullopt;
115 
116  // select which trigger to set (i.e., the only one!)
117  if (triggers.size() != 1) {
118  throw cet::exception("setDetectorClocksStandardTrigger")
119  << "Found " << triggers.size() << " trigger objects in '" << triggerTag.encode()
120  << "' (only one trigger per event is supported)\n";
121  }
122 
123  return std::make_optional(triggers.front().TriggerTime());
124  }
125 
126 } // namespace detinfo
127 
128 #endif // LARDATAALG_DETECTORINFO_DETECTORCLOCKSSTANDARDTRIGGERLOADER_H
std::optional< double > g4ref_time_for_event(art::InputTag const &triggerTag, Event const &event)
Loads DetectorClocksStandard G4Ref correction times.
std::string encode() const
Definition: InputTag.cc:97
General LArSoft Utilities.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::optional< std::pair< double, double > > trigger_times_for_event(art::InputTag const &triggerTag, Event const &event)
Loads DetectorClocksStandard trigger times.
Event finding and building.