LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
MCHitAnaExample_module.cc
Go to the documentation of this file.
1 // Class: MCHitAnaExample
3 // Module Type: analyzer
4 // File: MCHitAnaExample_module.cc
5 //
6 // Generated at Tue Jun 24 22:54:57 2014 by Kazuhiro Terao using artmod
7 // from cetpkgsupport v1_05_04.
9 
15 #include "art_root_io/TFileService.h"
16 #include "fhiclcpp/ParameterSet.h"
17 
22 #include <TH1D.h>
23 #include <TH2D.h>
24 #include <TStopwatch.h>
25 
26 namespace hit {
27 
29  public:
30  explicit MCHitAnaExample(fhicl::ParameterSet const& p);
31 
32  private:
33  void analyze(art::Event const& e) override;
34 
35  //
36  // Module names
37  //
38 
40  std::string fRecoHitModuleName;
41 
43  std::string fMCHitModuleName;
44 
45  //
46  // Stopwatch for timings
47  //
49 
50  //
51  // Histogram for timing record
52  //
53 
56 
59 
62 
65 
68 
69  //
70  // Histograms purely from MCHit
71  //
72 
74  std::vector<TH1D*> hMCHitQ_v;
75 
77  std::vector<TH1D*> hMCHitMult_v;
78 
79  //
80  // Histograms purely from RecoHit
81  //
82 
84  std::vector<TH1D*> hRecoHitQ_v;
85 
87  std::vector<TH1D*> hRecoHitMult_v;
88 
89  //
90  // Histograms from MC+Reco combined/compared information
91  //
92 
94  std::vector<TH2D*> hCorrMult_v;
95 
97  std::vector<TH1D*> hVoidHitQ_v;
98 
100  std::vector<TH2D*> hCorrQ_v;
101 
103  std::vector<TH2D*> hCorrQSum_v;
104 
106  std::vector<TH1D*> hQRatio_v;
107 
112  std::vector<TH1D*> hQSumRatio_v;
113 
115  std::vector<TH1D*> hDT_v;
116 
118  std::vector<TH1D*> hMCHitMultPerRecoHit_v;
119  };
120 
122 
123  {
124  fRecoHitModuleName = p.get<std::string>("RecoHitModuleName");
125  fMCHitModuleName = p.get<std::string>("MCHitModuleName");
126 
128 
129  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout>()->Get();
130 
131  for (unsigned char plane = 0; plane < wireReadoutGeom.Nplanes(); ++plane) {
132 
133  hMCHitQ_v.push_back(
134  fs->make<TH1D>(Form("hMCHitQ_%d", plane),
135  Form("MCHit Charge (Plane %d); Charge [ADC]; # MCHit", plane),
136  250,
137  0,
138  2000));
139 
140  hMCHitMult_v.push_back(
141  fs->make<TH1D>(Form("hMCHitMult_%d", plane),
142  Form("MCHit Multiplicity (Plane %d); Multiplicity; # MCHit", plane),
143  250,
144  0,
145  5000));
146 
147  hRecoHitQ_v.push_back(
148  fs->make<TH1D>(Form("hRecoHitQ_%d", plane),
149  Form("RecoHit Charge (Plane %d); Charge [ADC]; # RecoHits", plane),
150  250,
151  0,
152  2000));
153 
154  hRecoHitMult_v.push_back(
155  fs->make<TH1D>(Form("hRecoHitMult_%d", plane),
156  Form("RecoHit Multiplicity (Plane %d); Multiplicity; # RecoHits", plane),
157  200,
158  0,
159  2000));
160 
161  hCorrMult_v.push_back(
162  fs->make<TH2D>(Form("hCorrMult_%d", plane),
163  Form("# MCHits vs. # RecoHits (Plane %d); # MCHits; # RecoHits", plane),
164  250,
165  0,
166  5000,
167  200,
168  0,
169  2000));
170 
171  hVoidHitQ_v.push_back(fs->make<TH1D>(
172  Form("hVoidHitQ_%d", plane),
173  Form("RecoHit Charge (No Corresponding MCHit)) (Plane %d); Charge [ADC]; # RecoHits",
174  plane),
175  250,
176  0,
177  2000));
178 
179  hCorrQ_v.push_back(fs->make<TH2D>(
180  Form("hCorrQ_%d", plane),
181  Form("RecoHit Q vs. Closest MCHit Q (Plane %d); MCHit Charge [ADC]; RecoHit Charge [ADC]",
182  plane),
183  250,
184  0,
185  2000,
186  250,
187  0,
188  2000));
189 
190  hCorrQSum_v.push_back(
191  fs->make<TH2D>(Form("hCorrQSum_%d", plane),
192  Form("RecoHit Q vs. MCHit QSum Within Start=>End (Plane %d); MCHit Charge "
193  "[ADC]; RecoHit Charge [ADC]",
194  plane),
195  250,
196  0,
197  2000,
198  250,
199  0,
200  2000));
201 
202  hQRatio_v.push_back(
203  fs->make<TH1D>(Form("hQRatio_%d", plane),
204  Form("Reco/MCHit Charge (Plane %d); Ratio; # RecoHits", plane),
205  200,
206  0,
207  2));
208 
209  hQSumRatio_v.push_back(
210  fs->make<TH1D>(Form("hQSumRatio_%d", plane),
211  Form("Reco/MCHit Charge (Plane %d); Ratio; # RecoHits", plane),
212  200,
213  0,
214  2));
215 
216  hDT_v.push_back(fs->make<TH1D>(
217  Form("hDT_%d", plane),
218  Form("#DeltaT btw Reco and Closest MCHit (Plane %d); #DeltaT; # RecoHits", plane),
219  190,
220  -9.5,
221  9.5));
222 
223  hMCHitMultPerRecoHit_v.push_back(fs->make<TH1D>(
224  Form("hMCHitMultPerRecoHit_%d", plane),
225  Form("MCHit Multiplicity per RecoHit (Plane %d); Multiplicity; # RecoHits", plane),
226  21,
227  -1.5,
228  19.5));
229  }
230 
232  fs->make<TH1D>("hRecoHitReadTime",
233  "Real Time to Read recob::Hit From Disk; Real Time [ms]; Events",
234  200,
235  0,
236  1000);
237 
239  fs->make<TH1D>("hMCHitReadTime",
240  "Real Time to Read sim::MCHit From Disk; Real Time [ms]; Events",
241  200,
242  0,
243  1000);
244 
245  hAnalysisTime =
246  fs->make<TH1D>("hAnalysisTime",
247  "Analysis Time to Run analyze() Function; Real Time [ms]; Events",
248  200,
249  0,
250  1000);
251 
253  fs->make<TH1D>("hMCHitSearchTime",
254  "Time to Search Multiple sim::MCHit Per RecoHit; RealTime [us]; # RecoHit",
255  200,
256  0,
257  200);
258 
259  hMCHitSearchTimeSum = fs->make<TH1D>(
260  "hMCHitSearchTimeSum",
261  "Time to Search Multiple sim::MCHit for All RecoHit in Event; RealTime [ms]; # Event",
262  200,
263  0,
264  200);
265  }
266 
268  {
269  fAnaWatch.Start();
270 
271  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout const>()->Get();
272  auto const clock_data = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(e);
273 
274  fReadWatch.Start();
275  auto const& mchits_v = *e.getValidHandle<std::vector<sim::MCHitCollection>>(fMCHitModuleName);
276  hMCHitReadTime->Fill(fReadWatch.RealTime() * 1.e3);
277 
278  fReadWatch.Start();
279  auto const& recohits = *e.getValidHandle<std::vector<recob::Hit>>(fRecoHitModuleName);
280  hRecoHitReadTime->Fill(fReadWatch.RealTime() * 1.e3);
281 
282  //
283  // Work on purely MCHit info
284  //
285  std::vector<size_t> mchit_mult(wireReadoutGeom.Nplanes(), 0);
286  for (auto const& mchits : mchits_v) {
287  auto plane = wireReadoutGeom.ChannelToWire(mchits.Channel()).at(0).Plane;
288  mchit_mult.at(plane) += mchits.size();
289 
290  for (auto const& mchit : mchits)
291  hMCHitQ_v.at(plane)->Fill(mchit.Charge(true));
292  }
293 
294  std::vector<size_t> recohit_mult(wireReadoutGeom.Nplanes(), 0);
295 
296  double search_time_sum = 0;
297 
298  // Loop over RecoHit
299  for (size_t hit_index = 0; hit_index < recohits.size(); ++hit_index) {
300  auto const& hit = recohits.at(hit_index);
301 
302  auto const& wire_id = hit.WireID();
303 
304  // Fill purly RecoHit info
305  hRecoHitQ_v.at(wire_id.Plane)->Fill(hit.PeakAmplitude());
306  recohit_mult.at(wire_id.Plane) += 1;
307 
308  // Figure out channel & retrieve MCHitCollection for this channel
309  auto ch = wireReadoutGeom.PlaneWireToChannel(wire_id);
310 
311  if (mchits_v.size() <= ch)
312  throw cet::exception(__PRETTY_FUNCTION__)
313  << "Channel " << ch << " not found in MCHit vector!" << std::endl;
314 
315  // Found MCHitCollection
316  auto const& mchits = mchits_v.at(ch);
317 
318  if (ch != mchits.Channel())
319  throw cet::exception(__PRETTY_FUNCTION__)
320  << "MCHit channel & vector index mismatch: " << mchits.Channel() << " != " << ch
321  << std::endl;
322 
323  // Locate corresponding MCHit(s) to this RecoHit
324  sim::MCHit start_time, end_time, peak_time;
325 
326  start_time.SetTime(clock_data.TPCTick2TDC(hit.PeakTimeMinusRMS()), 0);
327  peak_time.SetTime(clock_data.TPCTick2TDC(hit.PeakTime()), 0);
328  end_time.SetTime(clock_data.TPCTick2TDC(hit.PeakTimePlusRMS()), 0);
329 
330  fSearchWatch.Start();
331  auto start_iter = std::lower_bound(mchits.begin(), mchits.end(), start_time);
332  auto end_iter = std::upper_bound(mchits.begin(), mchits.end(), end_time);
333  search_time_sum += fSearchWatch.RealTime() * 1.e3;
334  hMCHitSearchTime->Fill(fSearchWatch.RealTime() * 1.e6);
335 
336  double reco_q = hit.PeakAmplitude();
337  double mc_qsum = 0;
338  double mc_q = 0;
339  double mult = 0;
340  double dt_min = 0;
341  double abs_dt_min = 1e9;
342 
343  // Loop over MCHit(s) that reside in start=>end time of MCHit
344  while (start_iter != end_iter) {
345  mc_qsum += (*start_iter).Charge(true);
346  ++mult;
347  double dt = (*start_iter).PeakTime() - peak_time.PeakTime();
348  double abs_dt = dt;
349  if (abs_dt < 0) abs_dt *= -1;
350 
351  if (abs_dt < abs_dt_min) {
352  abs_dt_min = abs_dt;
353  dt_min = dt;
354  mc_q = (*start_iter).Charge(true);
355  }
356  ++start_iter;
357  }
358 
359  // Fill histograms
360  hMCHitMultPerRecoHit_v.at(wire_id.Plane)->Fill(mult);
361 
362  if (!mc_qsum)
363  hVoidHitQ_v.at(wire_id.Plane)->Fill(reco_q);
364 
365  else {
366 
367  hRecoHitQ_v.at(wire_id.Plane)->Fill(reco_q);
368  hCorrQ_v.at(wire_id.Plane)->Fill(mc_q, reco_q);
369  hCorrQSum_v.at(wire_id.Plane)->Fill(mc_qsum, reco_q);
370  hQSumRatio_v.at(wire_id.Plane)->Fill(reco_q / mc_qsum);
371  hQRatio_v.at(wire_id.Plane)->Fill(reco_q / mc_q);
372  hDT_v.at(wire_id.Plane)->Fill(dt_min);
373  }
374 
375  } // end looping over hits
376 
377  // Fill purely-MCHit and purely-RecoHit multiplicity histograms
378  for (unsigned char plane = 0; plane < wireReadoutGeom.Nplanes(); ++plane) {
379  std::cout << mchit_mult.at(plane) << std::endl;
380  hMCHitMult_v.at(plane)->Fill(mchit_mult.at(plane));
381  hRecoHitMult_v.at(plane)->Fill(recohit_mult.at(plane));
382  hCorrMult_v.at(plane)->Fill(mchit_mult.at(plane), recohit_mult.at(plane));
383  }
384 
385  hAnalysisTime->Fill(fAnaWatch.RealTime() * 1.e3);
386  hMCHitSearchTimeSum->Fill(search_time_sum);
387  }
388 }
float PeakTime() const
Getter for start time.
Definition: MCHit.h:70
TH1D * hMCHitSearchTimeSum
Time to search for corresponding MCHit per RecoHit summed over all RecoHits (per event) ...
void SetTime(const float peak, const float width)
Setter function for time.
Definition: MCHit.h:54
void analyze(art::Event const &e) override
Declaration of signal hit object.
MCHitAnaExample(fhicl::ParameterSet const &p)
std::vector< TH1D * > hQRatio_v
Histogram (per plane) for a ratio of RecoHit charge to the closest (in time) RecoHit charge...
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
std::vector< TH2D * > hCorrMult_v
Histogram (per plane) for # of MCHit vs. # of RecoHit.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
TH1D * hMCHitReadTime
Time to read MCHit from disk.
std::vector< TH1D * > hDT_v
Histogram (per plane) for time-diff between one Reco hit and the closest (in time) RecoHit...
std::string fMCHitModuleName
Producer module for MCHit.
std::vector< TH1D * > hRecoHitMult_v
Histogram (per plane) for RecoHit multiplicity in each event.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
std::vector< TH1D * > hRecoHitQ_v
Histogram (per plane) for RecoHit charge.
std::string fRecoHitModuleName
Producer module for recob::Hit.
T get(std::string const &key) const
Definition: ParameterSet.h:314
std::vector< TH1D * > hQSumRatio_v
std::vector< TH1D * > hMCHitMultPerRecoHit_v
Histogram (per plane) for MCHit multiplicity within start=>end time region of RecoHit.
Detector simulation of raw signals on wires.
std::vector< TH2D * > hCorrQ_v
Histogram (per plane) for charge of one MCHit that is closest (in time) to one RecoHit.
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
TH1D * hMCHitSearchTime
Time to search for corresponding MCHit per RecoHit.
std::vector< TH1D * > hMCHitQ_v
Histogram (per plane) for all MCHit charge.
TH1D * hRecoHitReadTime
Time to read recob::Hit from disk.
Float_t e
Definition: plot.C:35
std::vector< TH1D * > hMCHitMult_v
Histogram (per plane) for MCHit multiplicity in each event.
TH1D * hAnalysisTime
Time to run analyze() function.
std::vector< TH2D * > hCorrQSum_v
Histogram (per plane) for charge sum of multiple MCHit found within start=>end time of one RecoHit...
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< TH1D * > hVoidHitQ_v
Histogram (per plane) for charge of some RecoHit that has no corresponding MCHit. ...