LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 
18 #include "fhiclcpp/ParameterSet.h"
19 
24 #include <TH1D.h>
25 #include <TH2D.h>
26 #include <TStopwatch.h>
27 
28 namespace hit {
29 
30  class MCHitAnaExample;
31 
33  public:
34  explicit MCHitAnaExample(fhicl::ParameterSet const & p);
35  virtual ~MCHitAnaExample();
36 
37  void analyze(art::Event const & e) override;
38 
39 
40  private:
41 
42  //
43  // Module names
44  //
45 
47  std::string fRecoHitModuleName;
48 
50  std::string fMCHitModuleName;
51 
52  //
53  // Stopwatch for timings
54  //
56 
57  //
58  // Histogram for timing record
59  //
60 
63 
66 
69 
72 
75 
76  //
77  // Histograms purely from MCHit
78  //
79 
81  std::vector<TH1D*> hMCHitQ_v;
82 
84  std::vector<TH1D*> hMCHitMult_v;
85 
86  //
87  // Histograms purely from RecoHit
88  //
89 
91  std::vector<TH1D*> hRecoHitQ_v;
92 
94  std::vector<TH1D*> hRecoHitMult_v;
95 
96  //
97  // Histograms from MC+Reco combined/compared information
98  //
99 
101  std::vector<TH2D*> hCorrMult_v;
102 
104  std::vector<TH1D*> hVoidHitQ_v;
105 
107  std::vector<TH2D*> hCorrQ_v;
108 
110  std::vector<TH2D*> hCorrQSum_v;
111 
113  std::vector<TH1D*> hQRatio_v;
114 
119  std::vector<TH1D*> hQSumRatio_v;
120 
122  std::vector<TH1D*> hDT_v;
123 
125  std::vector<TH1D*> hMCHitMultPerRecoHit_v;
126 
127  };
128 
129 
131  :
132  EDAnalyzer(p) // ,
133 
134  {
135  fRecoHitModuleName = p.get<std::string>( "RecoHitModuleName" );
136  fMCHitModuleName = p.get<std::string>( "MCHitModuleName" );
137 
139 
141 
142  for(unsigned char plane=0; plane < geo->Nplanes(); ++plane) {
143 
144  hMCHitQ_v.push_back(fs->make<TH1D>(Form("hMCHitQ_%d",plane),
145  Form("MCHit Charge (Plane %d); Charge [ADC]; # MCHit",plane),
146  250,0,2000));
147 
148  hMCHitMult_v.push_back(fs->make<TH1D>(Form("hMCHitMult_%d",plane),
149  Form("MCHit Multiplicity (Plane %d); Multiplicity; # MCHit",plane),
150  250,0,5000));
151 
152  hRecoHitQ_v.push_back(fs->make<TH1D>(Form("hRecoHitQ_%d",plane),
153  Form("RecoHit Charge (Plane %d); Charge [ADC]; # RecoHits",plane),
154  250,0,2000));
155 
156  hRecoHitMult_v.push_back(fs->make<TH1D>(Form("hRecoHitMult_%d",plane),
157  Form("RecoHit Multiplicity (Plane %d); Multiplicity; # RecoHits",plane),
158  200,0,2000));
159 
160  hCorrMult_v.push_back(fs->make<TH2D>(Form("hCorrMult_%d",plane),
161  Form("# MCHits vs. # RecoHits (Plane %d); # MCHits; # RecoHits",plane),
162  250,0,5000,
163  200,0,2000));
164 
165  hVoidHitQ_v.push_back(fs->make<TH1D>(Form("hVoidHitQ_%d",plane),
166  Form("RecoHit Charge (No Corresponding MCHit)) (Plane %d); Charge [ADC]; # RecoHits",plane),
167  250,0,2000));
168 
169  hCorrQ_v.push_back(fs->make<TH2D>(Form("hCorrQ_%d",plane),
170  Form("RecoHit Q vs. Closest MCHit Q (Plane %d); MCHit Charge [ADC]; RecoHit Charge [ADC]",plane),
171  250,0,2000,
172  250,0,2000));
173 
174  hCorrQSum_v.push_back(fs->make<TH2D>(Form("hCorrQSum_%d",plane),
175  Form("RecoHit Q vs. MCHit QSum Within Start=>End (Plane %d); MCHit Charge [ADC]; RecoHit Charge [ADC]",plane),
176  250,0,2000,
177  250,0,2000));
178 
179  hQRatio_v.push_back(fs->make<TH1D>(Form("hQRatio_%d",plane),
180  Form("Reco/MCHit Charge (Plane %d); Ratio; # RecoHits",plane),
181  200, 0, 2));
182 
183  hQSumRatio_v.push_back(fs->make<TH1D>(Form("hQSumRatio_%d",plane),
184  Form("Reco/MCHit Charge (Plane %d); Ratio; # RecoHits",plane),
185  200, 0, 2));
186 
187  hDT_v.push_back(fs->make<TH1D>(Form("hDT_%d",plane),
188  Form("#DeltaT btw Reco and Closest MCHit (Plane %d); #DeltaT; # RecoHits",plane),
189  190,-9.5,9.5));
190 
191  hMCHitMultPerRecoHit_v.push_back(fs->make<TH1D>(Form("hMCHitMultPerRecoHit_%d",plane),
192  Form("MCHit Multiplicity per RecoHit (Plane %d); Multiplicity; # RecoHits",plane),
193  21,-1.5,19.5));
194  }
195 
196  hRecoHitReadTime = fs->make<TH1D>("hRecoHitReadTime",
197  "Real Time to Read recob::Hit From Disk; Real Time [ms]; Events",
198  200,0,1000);
199 
200  hMCHitReadTime = fs->make<TH1D>("hMCHitReadTime",
201  "Real Time to Read sim::MCHit From Disk; Real Time [ms]; Events",
202  200,0,1000);
203 
204  hAnalysisTime = fs->make<TH1D>("hAnalysisTime",
205  "Analysis Time to Run analyze() Function; Real Time [ms]; Events",
206  200,0,1000);
207 
208  hMCHitSearchTime = fs->make<TH1D>("hMCHitSearchTime",
209  "Time to Search Multiple sim::MCHit Per RecoHit; RealTime [us]; # RecoHit",
210  200,0,200);
211 
212  hMCHitSearchTimeSum = fs->make<TH1D>("hMCHitSearchTimeSum",
213  "Time to Search Multiple sim::MCHit for All RecoHit in Event; RealTime [ms]; # Event",
214  200,0,200);
215 
216  }
217 
219  {}
220 
222  {
223  fAnaWatch.Start();
224 
226  const detinfo::DetectorClocks* ts = lar::providerFrom<detinfo::DetectorClocksService>();
227 
228  fReadWatch.Start();
230  e.getByLabel(fMCHitModuleName,mcHandle);
231  if(!mcHandle.isValid()) throw cet::exception(__PRETTY_FUNCTION__) << "MCHit not found!" <<std::endl;
232  hMCHitReadTime->Fill(fReadWatch.RealTime() * 1.e3);
233  const std::vector<sim::MCHitCollection> &mchits_v = *mcHandle;
234 
235  fReadWatch.Start();
237  e.getByLabel(fRecoHitModuleName,recoHandle);
238  if(!recoHandle.isValid()) throw cet::exception(__PRETTY_FUNCTION__) << "RecoHit not found!" << std::endl;
239  hRecoHitReadTime->Fill(fReadWatch.RealTime() * 1.e3);
240  const std::vector<recob::Hit> &recohits = *recoHandle;
241 
242  //
243  // Work on purely MCHit info
244  //
245  std::vector<size_t> mchit_mult(geo->Nplanes(),0);
246  for(auto const& mchits : mchits_v) {
247 
248  auto plane = geo->ChannelToWire(mchits.Channel()).at(0).Plane;
249 
250  mchit_mult.at(plane) += mchits.size();
251 
252  for(auto const& mchit : mchits)
253 
254  hMCHitQ_v.at(plane)->Fill(mchit.Charge(true));
255 
256  }
257 
258  std::vector<size_t> recohit_mult(geo->Nplanes(),0);
259 
260  double search_time_sum = 0;
261 
262  // Loop over RecoHit
263  for(size_t hit_index=0; hit_index<recohits.size(); ++hit_index) {
264 
265  auto const &hit = recohits.at(hit_index);
266 
267  auto const &wire_id = hit.WireID();
268 
269  // Fill purly RecoHit info
270  hRecoHitQ_v.at(wire_id.Plane)->Fill(hit.PeakAmplitude());
271  recohit_mult.at(wire_id.Plane) += 1;
272 
273  // Figure out channel & retrieve MCHitCollection for this channel
274  auto ch = geo->PlaneWireToChannel(wire_id.Plane,
275  wire_id.Wire,
276  wire_id.TPC,
277  wire_id.Cryostat);
278 
279  if(mchits_v.size() <= ch )
280  throw cet::exception(__PRETTY_FUNCTION__)
281  << "Channel "<<ch<<" not found in MCHit vector!"<<std::endl;
282 
283  // Found MCHitCollection
284  auto const& mchits = mchits_v.at(ch);
285 
286  if( ch != mchits.Channel() )
287  throw cet::exception(__PRETTY_FUNCTION__)
288  << "MCHit channel & vector index mismatch: "
289  << mchits.Channel() << " != " << ch << std::endl;
290 
291  // Locate corresponding MCHit(s) to this RecoHit
292  sim::MCHit start_time, end_time, peak_time;
293 
294  start_time.SetTime ( ts->TPCTick2TDC( hit.PeakTimeMinusRMS() ), 0 );
295  peak_time.SetTime ( ts->TPCTick2TDC( hit.PeakTime() ), 0 );
296  end_time.SetTime ( ts->TPCTick2TDC( hit.PeakTimePlusRMS() ), 0 );
297 
298  fSearchWatch.Start();
299  auto start_iter = std::lower_bound ( mchits.begin(), mchits.end(), start_time );
300  auto end_iter = std::upper_bound ( mchits.begin(), mchits.end(), end_time );
301  search_time_sum += fSearchWatch.RealTime() * 1.e3;
302  hMCHitSearchTime->Fill(fSearchWatch.RealTime() * 1.e6);
303 
304  double reco_q = hit.PeakAmplitude();
305  double mc_qsum = 0;
306  double mc_q = 0;
307  double mult = 0;
308  double dt_min = 0;
309  double abs_dt_min = 1e9;
310 
311  // Loop over MCHit(s) that reside in start=>end time of MCHit
312  while(start_iter != end_iter) {
313  mc_qsum += (*start_iter).Charge(true);
314  ++mult;
315  double dt = (*start_iter).PeakTime() - peak_time.PeakTime();
316  double abs_dt = dt;
317  if(abs_dt<0) abs_dt *= -1;
318 
319  if(abs_dt < abs_dt_min) {
320  abs_dt_min = abs_dt;
321  dt_min = dt;
322  mc_q = (*start_iter).Charge(true);
323  }
324  ++start_iter;
325  }
326 
327  // Fill histograms
328  hMCHitMultPerRecoHit_v.at (wire_id.Plane) -> Fill( mult );
329 
330  if(!mc_qsum) hVoidHitQ_v.at (wire_id.Plane) -> Fill( reco_q );
331 
332  else {
333 
334  hRecoHitQ_v.at (wire_id.Plane) -> Fill ( reco_q );
335  hCorrQ_v.at (wire_id.Plane) -> Fill ( mc_q, reco_q );
336  hCorrQSum_v.at (wire_id.Plane) -> Fill ( mc_qsum, reco_q );
337  hQSumRatio_v.at (wire_id.Plane) -> Fill ( reco_q / mc_qsum );
338  hQRatio_v.at (wire_id.Plane) -> Fill ( reco_q / mc_q );
339  hDT_v.at (wire_id.Plane) -> Fill ( dt_min );
340  }
341 
342  } // end looping over hits
343 
344  // Fill purely-MCHit and purely-RecoHit multiplicity histograms
345  for(unsigned char plane=0; plane<geo->Nplanes(); ++plane) {
346  std::cout<<mchit_mult.at(plane)<<std::endl;
347  hMCHitMult_v.at (plane) -> Fill( mchit_mult.at (plane) );
348  hRecoHitMult_v.at (plane) -> Fill( recohit_mult.at (plane) );
349  hCorrMult_v.at (plane) -> Fill( mchit_mult.at (plane), recohit_mult.at (plane) );
350  }
351 
352  hAnalysisTime->Fill(fAnaWatch.RealTime() * 1.e3);
353  hMCHitSearchTimeSum->Fill(search_time_sum);
354 
355  }
356 }
358 
float PeakTime() const
Getter for start time.
Definition: MCHit.h:79
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:61
void analyze(art::Event const &e) override
Declaration of signal hit object.
TNtupleSim Fill(f1, f2, f3, f4)
MCHitAnaExample(fhicl::ParameterSet const &p)
std::vector< geo::WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
std::vector< TH1D * > hQRatio_v
Histogram (per plane) for a ratio of RecoHit charge to the closest (in time) RecoHit charge...
std::vector< TH2D * > hCorrMult_v
Histogram (per plane) for # of MCHit vs. # of RecoHit.
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...
bool isValid() const
Definition: Handle.h:190
std::string fMCHitModuleName
Producer module for MCHit.
std::vector< TH1D * > hRecoHitMult_v
Histogram (per plane) for RecoHit multiplicity in each event.
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
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:231
std::vector< TH1D * > hQSumRatio_v
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
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.
Conversion of times between different formats and references.
std::vector< TH2D * > hCorrQ_v
Histogram (per plane) for charge of one MCHit that is closest (in time) to one RecoHit.
raw::ChannelID_t PlaneWireToChannel(WireID const &wireid) const
Returns the ID of the TPC channel connected to the specified wire.
virtual double TPCTick2TDC(double tick) const =0
Converts a TPC time tick into a electronics time tick.
TH1D * hMCHitSearchTime
Time to search for corresponding MCHit per RecoHit.
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
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:34
std::vector< TH1D * > hMCHitMult_v
Histogram (per plane) for MCHit multiplicity in each event.
Namespace collecting geometry-related classes utilities.
TH1D * hAnalysisTime
Time to run analyze() function.
art framework interface to geometry description
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. ...