All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Analysis/trigger/details/TriggerResponseManager.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Analysis/trigger/details/TriggerResponseManager.h
3  * @brief Helper managing the trigger response part of a TTree.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date November 24, 2021
6  *
7  *
8  */
9 
10 #ifndef ICARUSCODE_ANALYSIS_TRIGGER_DETAILS_TRIGGERRESPONSEMANAGER_H
11 #define ICARUSCODE_ANALYSIS_TRIGGER_DETAILS_TRIGGERRESPONSEMANAGER_H
12 
13 
14 // LArSoft libraries
16 #include "lardataobj/RawData/TriggerData.h" // raw::Trigger
17 
18 // framework libraries
19 #include "art/Framework/Core/ConsumesCollector.h"
20 #include "art/Framework/Principal/Event.h"
21 #include "canvas/Utilities/InputTag.h"
22 
23 // C/C++ libraries
24 #include <vector>
25 #include <string>
26 #include <memory> // std::make_unique<>
27 #include <cstddef> // std::size_t
28 
29 
30 // -----------------------------------------------------------------------------
31 // forward declarations
32 class TTree;
33 class TBranch;
34 
35 // -----------------------------------------------------------------------------
36 namespace sbn::details { class TriggerResponseManager; }
37 /**
38  * @brief Manages extraction of trigger results and filling of their branches.
39  *
40  * This class defines and manages the structure of the branches pertaining the
41  * trigger simulation in a ROOT tree.
42  *
43  * It supports multiple trigger algorithms in a single ROOT tree entry
44  * (which may be defined for example as a physics event or as a particle within
45  * a physics event).
46  *
47  *
48  * Configuration
49  * --------------
50  *
51  * The manager is configured with a list of trigger specifications
52  * (`TriggerInputSpec_t`).
53  * One simple branch (with multiple leaves) is created for each specifications.
54  *
55  *
56  * Usage pattern
57  * --------------
58  *
59  * This manager class encloses all the steps needed to fill a tree with trigger
60  * response data. Users will have to take very few steps to integrate its
61  * functionality in a module.
62  *
63  * 1. A single instance of this class is created associated to a ROOT tree,
64  * configured with a set of trigger specifications. A class data member is
65  * recommended for this pattern.
66  * 2. On each _art_ event, an extractor object is created (`extractFor()`)
67  * which will manage the extraction of trigger data from that event.
68  * 3. On each entry in the tree, `Extractor::fetch()` is called for that entry,
69  * which will fill all the relevant branch data from the available
70  * information. This single steps makes all trigger data ready for filling.
71  * 4. When all additional data is ready, the tree can be filled with the entry
72  * data (`TTree::Fill()`).
73  *
74  * The configuration of this class is performed via a custom configuration data
75  * structure. The module may read it directly from its own (FHiCL) configuration
76  * or fill it in any other way.
77  *
78  *
79  * @note This class owns the memory associated to the tree branches.
80  * As such, it needs to exist as long as the buffers are needed, and those
81  * buffers must not change memory location.
82  * For this reason, the class is not copyable, but it is moveable (moving
83  * will preserve the address of the buffers).
84  *
85  */
87  : private lar::UncopiableClass // this class is moveable but not copyable
88 {
89 
90  struct TriggerInfoBranch_t;
91 
92  public:
93 
94  /**
95  * @brief Information about a single trigger logic (hardware or emulated).
96  *
97  * This data structure is the base for the tree branch.
98  * Each instance of the data structure represents a single trigger logic
99  * response.
100  *
101  * Default constructor represents a trigger that did not fire at all.
102  */
103  struct TriggerInfo_t {
104 
105  /// ROOT TTree specification for this data structure (leaf list).
106  static std::string const& TriggerResponseBranchStructure();
107 
108  /// Mnemonic value for absence of trigger time information
109  static constexpr double NotTriggeredTime = -999999.0;
110 
111  /// Time of the trigger
112  /// (@ref DetectorClocksElectronicsTime "electronics time scale").
114 
115  /// Time of the opening of the gate for the trigger evaluation.
117 
118  bool fired = false; ///< Whether this trigger fired.
119 
120  }; // TriggerInfo_t
121 
122 
123  /// Configuration specifications for the emulation of a trigger logic.
125  std::string name;
126  art::InputTag inputTag;
127  }; // TriggerInputSpec_t
128 
129  /// Trigger information extractors tied to an event.
130  class Extractors {
131 
132  /// Data pertaining a single branch.
134  std::vector<raw::Trigger> const* triggers; ///< Trigger results.
135  }; // struct TriggerInputData_t
136 
137  /// Pointer to the information for each supported branch.
138  std::vector<TriggerInfoBranch_t> const* fBranchInfo { nullptr };
139 
140  /// Data for each branch in this event (index parallel to `fBranchInfo`).
141  std::vector<TriggerInputData_t> fInputData;
142 
143 
144  /// Returns all data necessary to `branchInfo` extracted from `event`.
146  (art::Event const& event, TriggerInfoBranch_t const& branchInfo) const;
147 
148  /// Fills the specified trigger logic branch with information from its
149  /// trigger results using the `inputData` entry with index `iEntry`.
150  void fetchBranch(
151  TriggerInfoBranch_t const& info,
152  TriggerInputData_t& inputData,
153  std::size_t iEntry
154  );
155 
156  public:
157 
158  /// Reads all data from `events` needed for branches in `branchInfoList`.
159  Extractors(
160  art::Event const& event,
161  std::vector<TriggerInfoBranch_t>& branchInfoList
162  );
163 
164  /// Fills all branch data with information from trigger entries with index
165  /// `iEntry`.
166  void fetch(std::size_t iEntry);
167 
168 
169  /// Declares all data products that need to be read for `branchInfo`.
170  static void consumesInputData(
171  art::ConsumesCollector& collector, TriggerInfoBranch_t const& branchInfo
172  );
173 
174  }; // class Extractors
175 
176 
177  /// Initializes `tree` to accommodate the specified trigger information.
179  std::vector<TriggerInputSpec_t> const& triggerSpecs,
180  art::ConsumesCollector& collector,
181  TTree& tree
182  );
183 
184 
185  /// Returns an object to extract trigger information from `event`.
186  Extractors extractorsFor(art::Event const& event);
187 
188 
189  private:
190 
191  /// Data for a single trigger logic output branch.
193  std::string name;
194  art::InputTag triggerTag;
195  std::unique_ptr<TriggerInfo_t> data = std::make_unique<TriggerInfo_t>();
196  TBranch* branch = nullptr;
197  }; // TriggerInfoBranch_t
198 
199 
200  /// Data structures for the tree.
201  std::vector<TriggerInfoBranch_t> fBranchInfo;
202 
203 
204  /// Sets up the tree branches and returns the branch information structures.
205  std::vector<TriggerInfoBranch_t> buildTriggerResponseBranches
206  (TTree& tree, std::vector<TriggerInputSpec_t> const& triggerSpecs) const;
207 
208  /// Sets up a tree branch and returns its branch information structure.
210  (TTree& tree, TriggerInputSpec_t const& spec) const;
211 
212  /// Declares all the data products we are going to read.
213  void declareConsumables(art::ConsumesCollector& collector) const;
214 
215 }; // class sbn::details::TriggerResponseManager
216 
217 // -----------------------------------------------------------------------------
218 
219 
220 #endif // ICARUSCODE_ANALYSIS_TRIGGER_DETAILS_TRIGGERRESPONSEMANAGER_H
void fetchBranch(TriggerInfoBranch_t const &info, TriggerInputData_t &inputData, std::size_t iEntry)
Manages extraction of trigger results and filling of their branches.
void declareConsumables(art::ConsumesCollector &collector) const
Declares all the data products we are going to read.
Defines classes that can&#39;t be copied nor moved.
TriggerInputData_t buildInputData(art::Event const &event, TriggerInfoBranch_t const &branchInfo) const
Returns all data necessary to branchInfo extracted from event.
TriggerInfoBranch_t buildTriggerResponseBranch(TTree &tree, TriggerInputSpec_t const &spec) const
Sets up a tree branch and returns its branch information structure.
std::vector< TriggerInfoBranch_t > fBranchInfo
Data structures for the tree.
std::vector< TriggerInfoBranch_t > const * fBranchInfo
Pointer to the information for each supported branch.
static std::string const & TriggerResponseBranchStructure()
ROOT TTree specification for this data structure (leaf list).
static constexpr double NotTriggeredTime
Mnemonic value for absence of trigger time information.
std::vector< TriggerInfoBranch_t > buildTriggerResponseBranches(TTree &tree, std::vector< TriggerInputSpec_t > const &triggerSpecs) const
Sets up the tree branches and returns the branch information structures.
TriggerResponseManager(std::vector< TriggerInputSpec_t > const &triggerSpecs, art::ConsumesCollector &collector, TTree &tree)
Initializes tree to accommodate the specified trigger information.
Extractors extractorsFor(art::Event const &event)
Returns an object to extract trigger information from event.
An empty class that can&#39;t be copied (moving is allowed).
Information about a single trigger logic (hardware or emulated).
static void consumesInputData(art::ConsumesCollector &collector, TriggerInfoBranch_t const &branchInfo)
Declares all data products that need to be read for branchInfo.
std::vector< TriggerInputData_t > fInputData
Data for each branch in this event (index parallel to fBranchInfo).
Extractors(art::Event const &event, std::vector< TriggerInfoBranch_t > &branchInfoList)
Reads all data from events needed for branches in branchInfoList.
double gateTime
Time of the opening of the gate for the trigger evaluation.
Configuration specifications for the emulation of a trigger logic.