LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
HitAnaAlg.cxx
Go to the documentation of this file.
1 
13 #include "HitAnaAlg.h"
15 
16 #include <functional>
17 #include <unordered_map>
18 
19 #include "TTree.h"
20 
22 {
24 }
25 
27 {
28  wireDataTree = wdt;
30 }
31 
32 void hit::HitAnaAlg::SetHitDataTree(std::vector<TTree*>& trees)
33 {
34 
35  hitDataTree.clear();
36  hitData.clear();
37 
38  hitDataTree.reserve(trees.size());
39  // This is particularly important: to establish the hitData memory -- all before
40  // individually making the tree->Branch() calls, specifying those addresses.
41  hitData.reserve(trees.size());
42 
43  // Construct the local attribute data container
44  for (auto const& t : trees) {
45  hitDataTree.push_back(t);
46  hitData.push_back(new recob::Hit);
47  }
48 
49  for (size_t i = 0; i < hitData.size(); ++i)
50  hitDataTree[i]->Branch(hitDataTree[i]->GetName(), "recob::Hit", &(hitData[i]));
51 }
52 
54 {
55  wireDataTree->Branch("event", &wireData.event, "event/i");
56  wireDataTree->Branch("run", &wireData.run, "run/i");
57  wireDataTree->Branch("channel", &wireData.channel, "channel/i");
58  wireDataTree->Branch("plane", &wireData.plane, "plane/i");
59  wireDataTree->Branch("roi_index", &wireData.range_index, "roi_index/i");
60  wireDataTree->Branch("roi_start", &wireData.range_start, "roi_start/i");
61  wireDataTree->Branch("roi_size", &wireData.range_size, "roi_size/i");
62  wireDataTree->Branch("roi_charge", &wireData.integrated_charge, "roi_charge/F");
63  wireDataTree->Branch("roi_peak_charge", &wireData.peak_charge, "roi_peak_charge/F");
64  wireDataTree->Branch("roi_peak_time", &wireData.peak_time, "roi_peak_time/F");
65  wireDataTree->Branch("nHitModules", &wireData.NHitModules, "nHitModules/I");
66  wireDataTree->Branch("HitModuleLabels", &wireData.HitModuleLabels);
67  wireDataTree->Branch("NHits", &wireData.NHits);
68  wireDataTree->Branch("Hits_IntegratedCharge", &wireData.Hits_IntegratedCharge);
69  wireDataTree->Branch("Hits_AverageCharge", &wireData.Hits_AverageCharge);
70  wireDataTree->Branch("Hits_PeakCharge", &wireData.Hits_PeakCharge);
71  wireDataTree->Branch("Hits_Peak", &wireData.Hits_PeakTime);
72  wireDataTree->Branch("Hits_wAverageCharge", &wireData.Hits_wAverageCharge);
73  wireDataTree->Branch("Hits_wAverageTime", &wireData.Hits_wAverageTime);
74  wireDataTree->Branch("Hits_MeanMultiplicity", &wireData.Hits_MeanMultiplicity);
75 
76  wireDataTree->Branch("NMCHits", &wireData.NMCHits);
77  wireDataTree->Branch("MCHits_IntegratedCharge", &wireData.MCHits_IntegratedCharge);
78  wireDataTree->Branch("MCHits_AverageCharge", &wireData.MCHits_AverageCharge);
79  wireDataTree->Branch("MCHits_PeakCharge", &wireData.MCHits_PeakCharge);
80  wireDataTree->Branch("MCHits_Peak", &wireData.MCHits_PeakTime);
81  wireDataTree->Branch("MCHits_wAverageCharge", &wireData.MCHits_wAverageCharge);
82  wireDataTree->Branch("MCHits_wAverageTime", &wireData.MCHits_wAverageTime);
83 }
84 
86 {
87  HitModuleLabels.clear();
88  HitProcessingQueue.clear();
90 }
91 
92 void hit::HitAnaAlg::LoadHitAssocPair(std::vector<recob::Hit> const& HitVector,
93  std::vector<std::vector<int>> const& AssocVector,
94  std::string const& HitModuleLabel)
95 {
96 
97  HitProcessingQueue.push_back(std::make_pair(std::cref(HitVector), std::cref(AssocVector)));
98  HitModuleLabels.push_back(HitModuleLabel);
99 
100  if (HitProcessingQueue.size() != HitModuleLabels.size()) throw hitanaalgexception;
101 }
102 
103 void hit::HitAnaAlg::AnalyzeWires(std::vector<recob::Wire> const& WireVector,
104  std::vector<sim::MCHitCollection> const& MCHitCollectionVector,
105  std::vector<std::vector<int>> const& AssocVector,
106  detinfo::DetectorClocksData const& clock_data,
107  unsigned int event,
108  unsigned int run)
109 {
110 
111  InitWireData(event, run);
112  for (size_t iwire = 0; iwire < WireVector.size(); iwire++)
113  FillWireInfo(WireVector[iwire], iwire, MCHitCollectionVector, AssocVector[iwire], clock_data);
114 }
115 
116 void hit::HitAnaAlg::InitWireData(unsigned int event, unsigned int run)
117 {
118 
119  wireData.event = event;
120  wireData.run = run;
123 }
124 
126 {
127  wireData.NMCHits = 0;
134 
135  wireData.NHits.assign(wireData.NHitModules, 0);
143  wireData.Hits.clear();
145 }
146 
148  int WireIndex,
149  std::vector<sim::MCHitCollection> const& MCHitCollectionVector,
150  std::vector<int> const& thisAssocVector,
151  detinfo::DetectorClocksData const& clock_data)
152 {
153 
154  wireData.channel = wire.Channel();
155  wireData.plane = wire.View();
156  unsigned int range_index = 0;
157 
158  for (auto const& range : wire.SignalROI().get_ranges()) {
159 
160  wireData.range_index = range_index;
161  wireData.range_start = range.begin_index();
162  wireData.range_size = range.size();
163 
165 
166  ProcessROI(range, WireIndex, MCHitCollectionVector, thisAssocVector, clock_data);
167  range_index++;
168 
169  } //end loop over roi ranges
170 }
171 
173  float& charge_sum,
174  float& charge_peak,
175  float& charge_peak_time)
176 {
177 
178  charge_sum = 0;
179  charge_peak = -999;
180  unsigned int counter = range.begin_index();
181 
182  for (auto const& value : range) {
183  charge_sum += value;
184  if (value > charge_peak) {
185  charge_peak = value;
186  charge_peak_time = (float)counter;
187  }
188  counter++;
189  }
190 }
191 
193  int WireIndex,
194  std::vector<sim::MCHitCollection> const& MCHitCollectionVector,
195  std::vector<int> const& thisAssocVector,
196  detinfo::DetectorClocksData const& clock_data)
197 {
198 
200 
201  //std::cout << "----------------------------------------------------------------" << std::endl;
202  //std::cout << "WireIndex = " << WireIndex << std::endl;
203  //std::cout << "\tRange begin: " << range.begin_index() << std::endl;
204  //std::cout << "\tRange end: " << range.begin_index()+range.size() << std::endl;
205 
206  for (size_t iter = 0; iter < HitProcessingQueue.size(); iter++)
208  HitProcessingQueue[iter].second.at(WireIndex),
209  iter,
210  range.begin_index(),
211  range.begin_index() + range.size());
212 
213  FindAndStoreMCHitsInRange(MCHitCollectionVector,
214  thisAssocVector,
215  range.begin_index(),
216  range.begin_index() + range.size(),
217  clock_data);
218 
219  wireDataTree->Fill();
220 }
221 
222 void hit::HitAnaAlg::FindAndStoreHitsInRange(std::vector<recob::Hit> const& HitVector,
223  std::vector<int> const& HitsOnWire,
224  size_t hitmodule_iter,
225  size_t begin_wire_tdc,
226  size_t end_wire_tdc)
227 {
228 
229  wireData.Hits_PeakCharge[hitmodule_iter] = -999;
230 
231  for (auto const& hit_index : HitsOnWire) {
232  recob::Hit const& thishit = HitVector.at(hit_index);
233 
234  //check if this hit is on this ROI
235  if (thishit.PeakTimeMinusRMS() < begin_wire_tdc || thishit.PeakTimePlusRMS() > end_wire_tdc)
236  continue;
237 
238  FillHitInfo(thishit, wireData.Hits[hitmodule_iter]);
239  wireData.NHits[hitmodule_iter]++;
240  wireData.Hits_IntegratedCharge[hitmodule_iter] += thishit.Integral();
241 
242  if (thishit.PeakAmplitude() > wireData.Hits_PeakCharge[hitmodule_iter]) {
243  wireData.Hits_PeakCharge[hitmodule_iter] = thishit.PeakAmplitude();
244  wireData.Hits_PeakTime[hitmodule_iter] = thishit.PeakTime();
245  }
246 
247  wireData.Hits_wAverageCharge[hitmodule_iter] += thishit.Integral() * thishit.Integral();
248  wireData.Hits_wAverageTime[hitmodule_iter] += thishit.Integral() * thishit.PeakTime();
249  wireData.Hits_MeanMultiplicity[hitmodule_iter] += thishit.Multiplicity();
250 
251  *(hitData.at(hitmodule_iter)) = thishit;
252  (hitDataTree.at(hitmodule_iter))->Fill();
253  }
254 
255  wireData.Hits_AverageCharge[hitmodule_iter] =
256  wireData.Hits_IntegratedCharge[hitmodule_iter] / wireData.NHits[hitmodule_iter];
257  wireData.Hits_wAverageCharge[hitmodule_iter] =
258  wireData.Hits_wAverageCharge[hitmodule_iter] / wireData.Hits_IntegratedCharge[hitmodule_iter];
259  wireData.Hits_wAverageTime[hitmodule_iter] =
260  wireData.Hits_wAverageTime[hitmodule_iter] / wireData.Hits_IntegratedCharge[hitmodule_iter];
261 
262  wireData.Hits_MeanMultiplicity[hitmodule_iter] /= wireData.NHits[hitmodule_iter];
263 }
264 
266  std::vector<sim::MCHitCollection> const& MCHitCollectionVector,
267  std::vector<int> const& HitsOnWire,
268  size_t begin_wire_tdc,
269  size_t end_wire_tdc,
270  detinfo::DetectorClocksData const& clock_data)
271 {
272 
274 
275  for (auto const& hit_index : HitsOnWire) {
276  sim::MCHitCollection const& thismchitcol = MCHitCollectionVector.at(hit_index);
277 
278  //let's have a map to keep track of the number of total particles
279  std::unordered_map<int, unsigned int> nmchits_per_trackID_map;
280  for (auto const& thishit : thismchitcol) {
281 
282  //std::cout << "\t************************************************************" << std::endl;
283  //std::cout << "\t\tMCHit begin: " << ts->TPCTDC2Tick( thishit.PeakTime()-thishit.PeakWidth() ) << std::endl;
284  //std::cout << "\t\tMCHit end: " << ts->TPCTDC2Tick( thishit.PeakTime()+thishit.PeakWidth() ) << std::endl;
285 
286  //check if this hit is on this ROI
287  if (clock_data.TPCTDC2Tick(thishit.PeakTime() - thishit.PeakWidth()) < begin_wire_tdc ||
288  clock_data.TPCTDC2Tick(thishit.PeakTime() + thishit.PeakWidth()) > end_wire_tdc)
289  continue;
290 
291  nmchits_per_trackID_map[thishit.PartTrackId()] += 1;
292  wireData.MCHits_IntegratedCharge += thishit.Charge();
293 
294  if (thishit.Charge(true) > wireData.MCHits_PeakCharge) {
295  wireData.MCHits_PeakCharge = thishit.Charge(true);
296  wireData.MCHits_PeakTime = clock_data.TPCTDC2Tick(thishit.PeakTime());
297  }
298 
299  wireData.MCHits_wAverageCharge += thishit.Charge() * thishit.Charge();
300  wireData.MCHits_wAverageTime += thishit.Charge() * clock_data.TPCTDC2Tick(thishit.PeakTime());
301  }
302 
303  wireData.NMCHits = nmchits_per_trackID_map.size();
304 
309  }
310 }
311 
312 void hit::HitAnaAlg::FillHitInfo(recob::Hit const& hit, std::vector<HitInfo>& HitInfoVector)
313 {
314  HitInfoVector.emplace_back(hit.PeakTime(),
315  hit.SigmaPeakTime(),
316  hit.RMS(),
317  hit.StartTick(),
318  hit.EndTick(),
319  hit.Integral(),
320  hit.SigmaIntegral(),
321  hit.PeakAmplitude(),
322  hit.SigmaPeakAmplitude(),
323  hit.GoodnessOfFit());
324 }
std::vector< std::string > HitModuleLabels
Definition: HitAnaAlg.h:164
TTree * wireDataTree
Definition: HitAnaAlg.h:168
void SetupWireDataTree()
Definition: HitAnaAlg.cxx:53
unsigned int range_index
Definition: HitAnaAlg.h:75
void LoadHitAssocPair(std::vector< recob::Hit > const &, std::vector< std::vector< int >> const &, std::string const &)
Definition: HitAnaAlg.cxx:92
void SetHitDataTree(std::vector< TTree * > &trees)
Definition: HitAnaAlg.cxx:32
unsigned int event
Definition: HitAnaAlg.h:71
void ProcessROI(lar::sparse_vector< float >::datarange_t const &, int, std::vector< sim::MCHitCollection > const &, std::vector< int > const &, detinfo::DetectorClocksData const &)
Definition: HitAnaAlg.cxx:192
WireROIInfo wireData
Definition: HitAnaAlg.h:161
void FillHitInfo(recob::Hit const &, std::vector< HitInfo > &)
Definition: HitAnaAlg.cxx:312
std::vector< float > Hits_wAverageCharge
Definition: HitAnaAlg.h:88
std::vector< float > Hits_MeanMultiplicity
Definition: HitAnaAlg.h:90
float RMS() const
RMS of the hit shape, in tick units.
Definition: Hit.h:228
hit::HitAnaAlgException hitanaalgexception
std::vector< recob::Hit * > hitData
Definition: HitAnaAlg.h:162
TNtupleSim Fill(f1, f2, f3, f4)
void ClearWireDataHitInfo()
Definition: HitAnaAlg.cxx:125
float SigmaPeakAmplitude() const
Uncertainty on estimated amplitude of the hit at its peak, in ADC units.
Definition: Hit.h:236
float SigmaIntegral() const
Initial tdc tick for hit.
Definition: Hit.h:248
std::vector< float > Hits_PeakTime
Definition: HitAnaAlg.h:87
const range_list_t & get_ranges() const
Returns the internal list of non-void ranges.
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
Definition: Hit.h:244
size_type begin_index() const
Returns the first absolute index included in the range.
float GoodnessOfFit() const
Degrees of freedom in the determination of the hit signal shape (-1 by default)
Definition: Hit.h:260
short int Multiplicity() const
How many hits could this one be shared with.
Definition: Hit.h:252
double TPCTDC2Tick(double const tdc) const
Given electronics clock count [tdc] returns TPC time-tick.
pure virtual base interface for detector clocks
unsigned int range_start
Definition: HitAnaAlg.h:76
void SetWireDataTree(TTree *)
Definition: HitAnaAlg.cxx:26
float PeakAmplitude() const
The estimated amplitude of the hit at its peak, in ADC units.
Definition: Hit.h:232
std::vector< std::string > HitModuleLabels
Definition: HitAnaAlg.h:82
geo::View_t View() const
Returns the view the channel belongs to.
Definition: Wire.h:219
float MCHits_wAverageTime
Definition: HitAnaAlg.h:98
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
unsigned int run
Definition: HitAnaAlg.h:72
std::vector< float > Hits_PeakCharge
Definition: HitAnaAlg.h:86
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:295
std::vector< int > NHits
Definition: HitAnaAlg.h:83
float MCHits_IntegratedCharge
Definition: HitAnaAlg.h:93
raw::ChannelID_t Channel() const
Returns the ID of the channel (or InvalidChannelID)
Definition: Wire.h:223
float MCHits_wAverageCharge
Definition: HitAnaAlg.h:97
float peak_charge
Definition: HitAnaAlg.h:79
std::vector< float > Hits_AverageCharge
Definition: HitAnaAlg.h:85
const RegionsOfInterest_t & SignalROI() const
Returns the list of regions of interest.
Definition: Wire.h:211
void AnalyzeWires(std::vector< recob::Wire > const &, std::vector< sim::MCHitCollection > const &, std::vector< std::vector< int >> const &, detinfo::DetectorClocksData const &, unsigned int, unsigned int)
Definition: HitAnaAlg.cxx:103
std::vector< float > Hits_wAverageTime
Definition: HitAnaAlg.h:89
unsigned int channel
Definition: HitAnaAlg.h:73
raw::TDCtick_t StartTick() const
Initial tdc tick for hit.
Definition: Hit.h:212
float PeakTimeMinusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:290
float MCHits_PeakCharge
Definition: HitAnaAlg.h:95
double value
Definition: spectrum.C:18
Detector simulation of raw signals on wires.
raw::TDCtick_t EndTick() const
Final tdc tick for hit.
Definition: Hit.h:216
size_t range_size
Definition: HitAnaAlg.h:77
float MCHits_PeakTime
Definition: HitAnaAlg.h:96
unsigned int plane
Definition: HitAnaAlg.h:74
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:220
std::vector< art::Ptr< recob::Hit > > HitVector
size_type size() const
Returns the size of the range.
void InitWireData(unsigned int, unsigned int)
Definition: HitAnaAlg.cxx:116
Contains all timing reference information for the detector.
std::vector< art::Ptr< recob::Wire > > WireVector
std::vector< float > Hits_IntegratedCharge
Definition: HitAnaAlg.h:84
std::vector< TTree * > hitDataTree
Definition: HitAnaAlg.h:170
std::vector< std::vector< HitInfo > > Hits
Definition: HitAnaAlg.h:91
Class holding the regions of interest of signal from a channel.
Definition: Wire.h:116
Range class, with range and data.
float integrated_charge
Definition: HitAnaAlg.h:78
float MCHits_AverageCharge
Definition: HitAnaAlg.h:94
float SigmaPeakTime() const
Uncertainty for the signal peak, in tick units.
Definition: Hit.h:224
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:46
float PeakTimePlusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:285
void FindAndStoreHitsInRange(std::vector< recob::Hit > const &, std::vector< int > const &, size_t, size_t, size_t)
Definition: HitAnaAlg.cxx:222
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:82
void ClearHitModules()
Definition: HitAnaAlg.cxx:85
std::vector< HitAssocPair > HitProcessingQueue
Definition: HitAnaAlg.h:165
void ROIInfo(lar::sparse_vector< float >::datarange_t const &, float &, float &, float &)
Definition: HitAnaAlg.cxx:172
void FindAndStoreMCHitsInRange(std::vector< sim::MCHitCollection > const &, std::vector< int > const &, size_t, size_t, detinfo::DetectorClocksData const &)
Definition: HitAnaAlg.cxx:265
Event finding and building.
void FillWireInfo(recob::Wire const &, int, std::vector< sim::MCHitCollection > const &, std::vector< int > const &, detinfo::DetectorClocksData const &)
Definition: HitAnaAlg.cxx:147