All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PMT/Trigger/Utilities/TriggerResponseManager.cxx
Go to the documentation of this file.
1 /**
2  * @file icaruscode/PMT/Trigger/Utilities/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/PMT/Trigger/Utilities/TriggerResponseManager.h
7  *
8  */
9 
10 // library header
12 
13 // LArSoft libraries
15 
16 // framework libraries
17 #include "art/Framework/Principal/Handle.h"
18 
19 // ROOT libraries
20 #include "TTree.h"
21 #include "TBranch.h"
22 
23 // C/C++ libraries
24 #include <vector>
25 #include <string>
26 #include <cassert>
27 
28 
29 // -----------------------------------------------------------------------------
30 // --- sbn::details::TriggerResponseManager::Extractors
31 // -----------------------------------------------------------------------------
33  (art::Event const& event, std::vector<TriggerInfoBranch_t>& branchInfoList)
34  : fBranchInfo(&branchInfoList)
35 {
36 
37  for (TriggerInfoBranch_t const& branchInfo: *fBranchInfo)
38  fInputData.emplace_back(buildInputData(event, branchInfo));
39 
40 } // sbn::details::TriggerResponseManager::Extractors::Extractors()
41 
42 
43 // -----------------------------------------------------------------------------
45 {
46  assert(fBranchInfo);
47 
48  std::vector<TriggerInfoBranch_t> const& branchInfo = *fBranchInfo;
49  assert(branchInfo.size() == fInputData.size());
50 
51  for (std::size_t iBranch: util::counter(branchInfo.size()))
52  fetchBranch(branchInfo[iBranch], fInputData[iBranch], iEntry);
53 
54 } // sbn::details::TriggerResponseManager::Extractors::fetch()
55 
56 
57 // -----------------------------------------------------------------------------
59  (art::ConsumesCollector& collector, TriggerInfoBranch_t const& branchInfo)
60 {
61 
62  collector.consumes<std::vector<raw::Trigger>>(branchInfo.triggerTag);
63 
64 } // sbn::details::TriggerResponseManager::Extractors::consumesInputData()
65 
66 
67 // -----------------------------------------------------------------------------
69  (art::Event const& event, TriggerInfoBranch_t const& branchInfo) const
70  -> TriggerInputData_t
71 {
72 
73  return TriggerInputData_t{
74  &(event.getProduct<std::vector<raw::Trigger>>(branchInfo.triggerTag))
75  // triggers
76  };
77 
78 } // sbn::details::TriggerResponseManager::Extractors::buildInputData()
79 
80 
81 // -----------------------------------------------------------------------------
83  TriggerInfoBranch_t const& info,
84  TriggerInputData_t& inputData,
85  std::size_t iEntry
86 ) {
87  assert(inputData.triggers);
88 
89  raw::Trigger const& trigger = inputData.triggers->at(iEntry);
90  TriggerInfo_t& data = *(info.data);
91 
92  data.fired = (trigger.TriggerBits() != 0);
93  data.triggerTime = trigger.TriggerTime();
94  data.gateTime = trigger.BeamGateTime();
95 
96 } // sbn::details::TriggerResponseManager::Extractors::fetchBranch()
97 
98 
99 // -----------------------------------------------------------------------------
100 // --- sbn::details::TriggerResponseManager
101 // -----------------------------------------------------------------------------
102 std::string const
104  { "time/D:gateStart/D:fired/O" };
105 
106 
107 // -----------------------------------------------------------------------------
109  std::vector<TriggerInputSpec_t> const& triggerSpecs,
110  art::ConsumesCollector& collector,
111  TTree& tree
112 )
113  : fBranchInfo{ buildTriggerResponseBranches(tree, triggerSpecs) }
114 {
115 
116  declareConsumables(collector);
117 
118 } // sbn::details::TriggerResponseManager::TriggerResponseManager()
119 
120 
121 // -----------------------------------------------------------------------------
123  (art::Event const& event) -> Extractors
124 {
125  return Extractors{ event, fBranchInfo };
126 } // sbn::details::TriggerResponseManager::extractorsFor()
127 
128 
129 // -----------------------------------------------------------------------------
131  (art::ConsumesCollector& collector) const
132 {
133  for (TriggerInfoBranch_t const& branchInfo: fBranchInfo)
134  Extractors::consumesInputData(collector, branchInfo);
135 } // sbn::details::TriggerResponseManager::declareConsumables()
136 
137 
138 // -----------------------------------------------------------------------------
140  (TTree& tree, std::vector<TriggerInputSpec_t> const& triggerSpecs) const
141  -> std::vector<TriggerInfoBranch_t>
142 {
143  std::vector<TriggerInfoBranch_t> branchInfoList;
144  for (TriggerInputSpec_t const& spec: triggerSpecs)
145  branchInfoList.push_back(buildTriggerResponseBranch(tree, spec));
146  return branchInfoList;
147 } // sbn::details::TriggerResponseManager::buildTriggerResponseBranches()
148 
149 
150 // -----------------------------------------------------------------------------
152  (TTree& tree, TriggerInputSpec_t const& spec) const -> TriggerInfoBranch_t
153 {
154  TriggerInfoBranch_t branchInfo {
155  spec.name // name
156  , spec.inputTag // triggerTag
157  // the rest is default-constructed
158  }; // TriggerInfoBranch_t
159 
160  branchInfo.branch = tree.Branch(
161  spec.name.c_str(), branchInfo.data.get(),
162  TriggerInfo_t::TriggerResponseBranchStructure.c_str()
163  );
164 
165  return branchInfo;
166 } // sbn::details::TriggerResponseManager::buildTriggerResponseBranches()
167 
168 
169 // -----------------------------------------------------------------------------
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.
static std::string const & TriggerResponseBranchStructure()
ROOT TTree specification for this data structure (leaf list).
Helper managing the trigger response part of a TTree.
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
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.
static void consumesInputData(art::ConsumesCollector &collector, TriggerInfoBranch_t const &branchInfo)
Declares all data products that need to be read for branchInfo.
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