All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
29 #include "canvas/Utilities/InputTag.h"
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 triggerTag tag of the `raw::Trigger` collection data product to read
42  * @param event the event the trigger objects are stored into
43  * @return optional pair of trigger and beam gate time, empty if not found
44  * @throws cet::exception (category `"setDetectorClocksStandardTrigger"`)
45  * if trigger data product has more than one trigger
46  *
47  * This function returns the relative trigger and beam gate times read from an `event`.
48  * It attempts to read the information from a `raw::Trigger` collection data product
49  * with tag `triggerTag` in the `event`. Times are expected to be in microseconds
50  * on the electronics time scale.
51  * In case that data product does not exist, or it exists but empty,
52  * an empty result is quietly returned.
53  * If instead there are multiple trigger objects in the collection,
54  * 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 triggerTag tag of the `raw::Trigger` collection data product to read
87  * @param event the event the trigger objects are stored into
88  * @return optional G4 reference time value, empty if not found
89  * @throws cet::exception (category `"setDetectorClocksStandardTrigger"`)
90  * if trigger data product has more than one trigger
91  *
92  * This function returns the simulation (G4) reference time from an `event`.
93  * It is assumed to match the trigger time (or, it is assumed that the trigger
94  * fired at the time the event was generated).
95  * The function attempts to read the information from a `raw::Trigger` collection
96  * data product with tag `triggerTag` in the `event`. The time is expected to be
97  * in microseconds on the electronics time scale.
98  * In case that data product does not exist, or it exists but empty,
99  * an empty result is quietly returned.
100  * If instead there are multiple trigger objects in the collection,
101  * no choice is made, and an exception is thrown.
102  */
103  template <typename Event>
104  std::optional<double>
105  g4ref_time_for_event(art::InputTag const& triggerTag, Event const& event)
106  {
107  // fetch the trigger data product
108  using TriggerHandle_t = typename Event::template HandleT<std::vector<raw::Trigger>>;
109 
110  TriggerHandle_t triggerHandle;
111  if (!event.template getByLabel(triggerTag, triggerHandle)) return std::nullopt;
112 
113  // check that we do have a trigger
114  // (we have already checked whether the handle is valid above)
115  auto const& triggers = *triggerHandle;
116  if (triggers.empty()) return std::nullopt;
117 
118  // select which trigger to set (i.e., the only one!)
119  if (triggers.size() != 1) {
120  throw cet::exception("setDetectorClocksStandardTrigger")
121  << "Found " << triggers.size() << " trigger objects in '" << triggerTag.encode()
122  << "' (only one trigger per event is supported)\n";
123  }
124 
125  return std::make_optional(triggers.front().TriggerTime());
126  }
127 
128 } // namespace detinfo
129 
130 #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::optional< std::pair< double, double > > trigger_times_for_event(art::InputTag const &triggerTag, Event const &event)
Loads DetectorClocksStandard trigger times.