All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Analysis/trigger/details/TriggerResponseManager.cxx
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Analysis/trigger/details/TriggerResponseManager.cxx
3  * @brief Helper managing the trigger response part of a TTree (implementation).
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date November 24, 2021
6  * @see icaruscode/Analysis/trigger/details/TriggerResponseManager.h
7  *
8  */
9 
10 // library header
12 
13 // LArSoft libraries
15 
16 // framework libraries
17 #include "art/Framework/Principal/Handle.h"
18 // #include "canvas/Persistency/Common/FindOneP.h"
19 // #include "canvas/Persistency/Common/Assns.h"
20 // #include "canvas/Persistency/Common/Ptr.h"
21 
22 // ROOT libraries
23 #include "TTree.h"
24 #include "TBranch.h"
25 
26 // C/C++ libraries
27 #include <vector>
28 #include <string>
29 #include <cassert>
30 
31 
32 // -----------------------------------------------------------------------------
33 // --- sbn::details::TriggerResponseManager::Extractors
34 // -----------------------------------------------------------------------------
36  (art::Event const& event, std::vector<TriggerInfoBranch_t>& branchInfoList)
37  : fBranchInfo(&branchInfoList)
38 {
39 
40  for (TriggerInfoBranch_t const& branchInfo: *fBranchInfo)
41  fInputData.emplace_back(buildInputData(event, branchInfo));
42 
43 } // sbn::details::TriggerResponseManager::Extractors::Extractors()
44 
45 
46 // -----------------------------------------------------------------------------
48 {
49  assert(fBranchInfo);
50 
51  std::vector<TriggerInfoBranch_t> const& branchInfo = *fBranchInfo;
52  assert(branchInfo.size() == fInputData.size());
53 
54  for (std::size_t iBranch: util::counter(branchInfo.size()))
55  fetchBranch(branchInfo[iBranch], fInputData[iBranch], iEntry);
56 
57 } // sbn::details::TriggerResponseManager::Extractors::fetch()
58 
59 
60 // -----------------------------------------------------------------------------
62  (art::ConsumesCollector& collector, TriggerInfoBranch_t const& branchInfo)
63 {
64 
65  collector.consumes<std::vector<raw::Trigger>>(branchInfo.triggerTag);
66 
67 } // sbn::details::TriggerResponseManager::Extractors::consumesInputData()
68 
69 
70 // -----------------------------------------------------------------------------
72  (art::Event const& event, TriggerInfoBranch_t const& branchInfo) const
74 {
75 
76  return TriggerInputData_t{
77  event.getValidHandle<std::vector<raw::Trigger>>(branchInfo.triggerTag)
78  .product() // triggers
79  };
80 
81 } // sbn::details::TriggerResponseManager::Extractors::buildInputData()
82 
83 
84 // -----------------------------------------------------------------------------
87  TriggerInputData_t& inputData,
88  std::size_t iEntry
89 ) {
90  assert(inputData.triggers);
91 
92  raw::Trigger const& trigger = inputData.triggers->at(iEntry);
93  TriggerInfo_t& data = *(info.data);
94 
95  data.fired = (trigger.TriggerBits() != 0);
96  data.triggerTime = trigger.TriggerTime();
97  data.gateTime = trigger.BeamGateTime();
98 
99 } // sbn::details::TriggerResponseManager::Extractors::fetchBranch()
100 
101 
102 // -----------------------------------------------------------------------------
103 // --- sbn::details::TriggerResponseManager
104 // -----------------------------------------------------------------------------
105 std::string const&
107 {
108  // using a static std::string constant data member caused segmentation fault
109  // at the closure of the program; so we get just a tiny bit less static.
110  static std::string const specs { "time/D:gateStart/D:fired/O" };
111  return specs;
112 }
113 
114 
115 // -----------------------------------------------------------------------------
117  std::vector<TriggerInputSpec_t> const& triggerSpecs,
118  art::ConsumesCollector& collector,
119  TTree& tree
120 )
121  : fBranchInfo{ buildTriggerResponseBranches(tree, triggerSpecs) }
122 {
123 
124  declareConsumables(collector);
125 
126 } // sbn::details::TriggerResponseManager::TriggerResponseManager()
127 
128 
129 // -----------------------------------------------------------------------------
131  (art::Event const& event) -> Extractors
132 {
133  return Extractors{ event, fBranchInfo };
134 } // sbn::details::TriggerResponseManager::extractorsFor()
135 
136 
137 // -----------------------------------------------------------------------------
139  (art::ConsumesCollector& collector) const
140 {
141  for (TriggerInfoBranch_t const& branchInfo: fBranchInfo)
142  Extractors::consumesInputData(collector, branchInfo);
143 } // sbn::details::TriggerResponseManager::declareConsumables()
144 
145 
146 // -----------------------------------------------------------------------------
148  (TTree& tree, std::vector<TriggerInputSpec_t> const& triggerSpecs) const
149  -> std::vector<TriggerInfoBranch_t>
150 {
151  std::vector<TriggerInfoBranch_t> branchInfoList;
152  for (TriggerInputSpec_t const& spec: triggerSpecs)
153  branchInfoList.push_back(buildTriggerResponseBranch(tree, spec));
154  return branchInfoList;
155 } // sbn::details::TriggerResponseManager::buildTriggerResponseBranches()
156 
157 
158 // -----------------------------------------------------------------------------
160  (TTree& tree, TriggerInputSpec_t const& spec) const -> TriggerInfoBranch_t
161 {
162  TriggerInfoBranch_t branchInfo {
163  spec.name // name
164  , spec.inputTag // triggerTag
165  // the rest is default-constructed
166  }; // TriggerInfoBranch_t
167 
168  branchInfo.branch = tree.Branch(
169  spec.name.c_str(), branchInfo.data.get(),
170  TriggerInfo_t::TriggerResponseBranchStructure().c_str()
171  );
172 
173  return branchInfo;
174 } // sbn::details::TriggerResponseManager::buildTriggerResponseBranches()
175 
176 
177 // -----------------------------------------------------------------------------
void fetchBranch(TriggerInfoBranch_t const &info, TriggerInputData_t &inputData, std::size_t iEntry)
void declareConsumables(art::ConsumesCollector &collector) const
Declares all the data products we are going to read.
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).
double TriggerTime() const
Trigger time w.r.t. electronics clock T0 in us.
Definition: TriggerData.h:50
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:285
Helper managing the trigger response part of a TTree.
double BeamGateTime() const
BeamGate time w.r.t. electronics clock T0 in us.
Definition: TriggerData.h:52
Test of util::counter and support utilities.
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.
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.
unsigned int TriggerBits() const
Trigger Bits.
Definition: TriggerData.h:54
double gateTime
Time of the opening of the gate for the trigger evaluation.
Configuration specifications for the emulation of a trigger logic.