LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
GausHitFinderAna_module.cc
Go to the documentation of this file.
1 #ifndef GAUSHITFINDERANA_H
2 #define GAUSHITFINDERANA_H
3 //
5 // Gaus(s)HitFinder class designed to analyze signal on a wire in the TPC
6 //
7 // jaasaadi@syr.edu
8 //
9 // Note: This is a rework of the original hit finder ana module
10 // The only histograms that are saved are ones that can be used
11 // to make sure the hit finder is functioning...the rest is
12 // outputted to a TTree for offline analysis.
14 
15 
16 // LArSoft includes
23 
24 // ROOT includes
25 #include <TMath.h>
26 #include <TH1F.h>
27 #include <TGraph.h>
28 #include <TFile.h>
29 #include "TComplex.h"
30 #include "TString.h"
31 #include "TH1.h"
32 #include "TH2.h"
33 #include "TProfile.h"
34 #include "TTree.h"
35 
36 // C++ includes
37 #include <algorithm>
38 #include <sstream>
39 #include <fstream>
40 #include <iostream>
41 #include <bitset>
42 #include <vector>
43 #include <string>
44 
45 // Framework includes
47 #include "fhiclcpp/ParameterSet.h"
57 
58 constexpr int kMaxHits = 20000; //maximum number of hits;
59 
60 namespace geo { class Geometry; }
61 namespace sim { class SimChannel; }
62 
63 namespace hit{
64 
67 
68  public:
69 
70  explicit GausHitFinderAna(fhicl::ParameterSet const& pset);
71  virtual ~GausHitFinderAna();
72 
74  void analyze (const art::Event& evt);
75  void beginJob();
76  void reconfigure(fhicl::ParameterSet const& p);
77 
78  private:
79 
80  std::string fHitFinderModuleLabel;
81  std::string fLArG4ModuleLabel;
82  std::string fCalDataModuleLabel;
83 
84 
85 
86 
91 
92 
93 
94  // ### TTree for offline analysis ###
95  TTree* fHTree;
96  // === Event Information ===
97  Int_t fRun; // Run Number
98  Int_t fEvt; // Event Number
99 
100 
101  // === Wire Information ====
102  Float_t fWireTotalCharge; // Charge on all wires
103 
104 
105  // === Hit Information ===
106  Int_t fnHits; // Number of Hits in the Event
107  Int_t fWire[kMaxHits]; // Wire Number
108  Float_t fStartTime[kMaxHits]; // Start Time
109  Float_t fEndTime[kMaxHits]; // End Time
110  Float_t fPeakTime[kMaxHits]; // Peak Time
111  Float_t fPeakTimeUncert[kMaxHits]; // Peak Time Uncertainty
112  Float_t fCharge[kMaxHits]; // Charge of this hit
113  Float_t fChargeUncert[kMaxHits]; // Charge Uncertainty of this hit
114  Int_t fMultiplicity[kMaxHits]; // Hit pulse multiplicity
115  Float_t fGOF[kMaxHits]; // Goodness of Fit (Chi2/NDF)
116 
117  // === Total Hit Information ===
118  Float_t fTotalHitChargePerEvent; //Total charge recorded in each event
119 
120  // === Truth Hit Info from BackTrackerService ===
121  Float_t fTruePeakPos[kMaxHits]; // Truth Time Tick info from BackTrackerService
122 
123 
124 
125 
126 
127 
128  }; // class GausHitFinderAna
129 
130 
131  //-------------------------------------------------
132  GausHitFinderAna::GausHitFinderAna(fhicl::ParameterSet const& pset)
133  : EDAnalyzer(pset)
134  {
135  this->reconfigure(pset);
136  }
137 
138  //-------------------------------------------------
140  {
141  }
142 
144  {
145  fHitFinderModuleLabel = p.get< std::string >("HitsModuleLabel");
146  fLArG4ModuleLabel = p.get< std::string >("LArGeantModuleLabel");
147  fCalDataModuleLabel = p.get< std::string >("CalDataModuleLabel");
148  return;
149  }
150  //-------------------------------------------------
152  {
153  // get access to the TFile service
155 
156 
157  // ======================================
158  // === Hit Information for Histograms ===
159  fHitResidualAll = tfs->make<TH1F>("fHitResidualAll", "Hit Residual All", 1600, -400, 400);
160  fHitResidualAllAlt = tfs->make<TH1F>("fHitResidualAllAlt", "Hit Residual All", 1600, -400, 400);
161  fNumberOfHitsPerEvent= tfs->make<TH1F>("fNumberOfHitsPerEvent", "Number of Hits in Each Event", 10000, 0, 10000);
162  fPeakTimeVsWire = tfs->make<TH2F>("fPeakTimeVsWire", "Peak Time vs Wire Number", 3200, 0, 3200, 9500, 0, 9500);
163 
164 
165 
166 
167  // ##############
168  // ### TTree ####
169  fHTree = tfs->make<TTree>("HTree","HTree");
170 
171  // === Event Info ====
172  fHTree->Branch("Evt", &fEvt, "Evt/I");
173  fHTree->Branch("Run", &fRun, "Run/I");
174 
175  // === Wire Info ===
176  fHTree->Branch("WireTotalCharge", &fWireTotalCharge, "WireTotalCharge/F");
177 
178  // === Hit Info ===
179  fHTree->Branch("nHits", &fnHits, "nHits/I");
180  fHTree->Branch("Wire", &fWire, "Wire[nHits]/I");
181  fHTree->Branch("StartTime", &fStartTime, "fStartTime[nHits]/F");
182  fHTree->Branch("EndTime", &fEndTime, "fEndTime[nHits]/F");
183  fHTree->Branch("PeakTime", &fPeakTime, "fPeakTime[nHits]/F");
184  fHTree->Branch("PeakTimeUncert", &fPeakTimeUncert, "fPeakTimeUncert[nHits]/F");
185  fHTree->Branch("Charge", &fCharge, "fCharge[nHits]/F");
186  fHTree->Branch("ChargeUncert", &fChargeUncert, "fChargeUncert[nHits]/F");
187  fHTree->Branch("Multiplicity", &fMultiplicity, "fMultiplicity[nHits]/I");
188  fHTree->Branch("GOF", &fGOF, "fGOF[nHits]/F");
189 
190  // === Total Hit Information ===
191  fHTree->Branch("TotalHitChargePerEvent", &fTotalHitChargePerEvent, "TotalHitChargePerEvent/F");
192 
193  // === Truth Hit Information from BackTrackerService ===
194  fHTree->Branch("TruePeakPos", &fTruePeakPos, "fTruePeakPos[nHits]/F");
195 
196  return;
197 
198  }
199 
200  //-------------------------------------------------
202  {
203 
204  // ##############################################
205  // ### Outputting Run Number and Event Number ###
206  // ##############################################
207  //std::cout << "run : " << evt.run() <<" event : "<<evt.id().event() << std::endl;
208 
209  // ### TTree Run/Event ###
210  fEvt = evt.id().event();
211  fRun = evt.run();
212 
213  // ####################################
214  // ### Getting Geometry Information ###
215  // ####################################
217 
218  // ###################################
219  // ### Getting Detector Properties ###
220  // ###################################
221  const detinfo::DetectorProperties* detp = lar::providerFrom<detinfo::DetectorPropertiesService>();
222 
223  // ##########################################
224  // ### Reading in the Wire List object(s) ###
225  // ##########################################
227  evt.getByLabel(fCalDataModuleLabel,wireVecHandle);
228 
231 
232  float TotWireCharge = 0;
233 
234  //##############################
235  //### Looping over the wires ###
236  //##############################
237  for(size_t wireIter = 0; wireIter < wireVecHandle->size(); wireIter++)
238  {
239  art::Ptr<recob::Wire> wire(wireVecHandle, wireIter);
240  // #################################################
241  // ### Getting a vector of signals for this wire ###
242  // #################################################
243  std::vector<float> signal(wire->Signal());
244 
245  // ##########################################################
246  // ### Making an iterator for the time ticks of this wire ###
247  // ##########################################################
248  std::vector<float>::iterator timeIter; // iterator for time bins
249 
250 
251 
252  // ##################################
253  // ### Looping over Signal Vector ###
254  // ##################################
255  for(timeIter = signal.begin(); timeIter+2<signal.end(); timeIter++)
256  {
257 
258  // ###########################################################
259  // ### If the ADC value is less than 2 skip this time tick ###
260  // ###########################################################
261  if(*timeIter < 2) {continue;}
262 
263  // ### Filling Total Wire Charge ###
264  TotWireCharge += *timeIter;
265 
266 
267  }//<---End timeIter loop
268 
269 
270  }//<---End wireIter loop
271 
272  // ~~~ Filling the total charge in the event ~~~
273  //std::cout<<"Total Real Charge = "<<TotWireCharge<<std::endl;
274  fWireTotalCharge = TotWireCharge;
275 
278 
279  // ##################################################
280  // ### Getting the Reconstructed Hits (hitHandle) ###
281  // ##################################################
283  evt.getByLabel(fHitFinderModuleLabel,hitHandle);
284 
285  // #########################################
286  // ### Putting Hits into a vector (hits) ###
287  // #########################################
288  std::vector< art::Ptr<recob::Hit> > hits;
289  art::fill_ptr_vector(hits, hitHandle);
290 
291  float TotCharge = 0;
292  int hitCount = 0;
293  // ### Number of Hits in the event ###
294  fnHits = hitHandle->size();
295  fNumberOfHitsPerEvent->Fill(hitHandle->size());
296  // #########################
297  // ### Looping over Hits ###
298  // #########################
299  for(size_t numHit = 0; numHit < hitHandle->size(); ++numHit)
300  {
301  // === Finding Channel associated with the hit ===
302  art::Ptr<recob::Hit> hit(hitHandle, numHit);
303 
304  fWire[hitCount] = hit->WireID().Wire;
305  fStartTime[hitCount] = hit->PeakTimeMinusRMS();
306  fEndTime[hitCount] = hit->PeakTimePlusRMS();
307  fPeakTime[hitCount] = hit->PeakTime();
308  fPeakTimeUncert[hitCount]= hit->SigmaPeakTime();
309  fCharge[hitCount] = hit->Integral();
310  fChargeUncert[hitCount] = hit->SigmaIntegral();
311  fMultiplicity[hitCount] = hit->Multiplicity();
312  fGOF[hitCount] = hit->GoodnessOfFit();
313  //std::cout<<"Hit Charge = "<<hit->Charge()<<std::endl;
314  //std::cout<<"StartTime = "<<hit->StartTime()<<std::endl;
315 
316  hitCount++;
317  TotCharge += hit->Integral();
318 
319  fPeakTimeVsWire->Fill(hit->WireID().Wire, hit->PeakTime());
320  }//<---End numHit
321  //std::cout<<"Total Reco Charge = "<<TotCharge<<std::endl;
322  fTotalHitChargePerEvent = TotCharge;
323 
326 
327  // ###############################################################
328  // ### Integers used for setting Channel, TPC, Plane, and Wire ###
329  // ###############################################################
330  unsigned int plane = 0;
331 
332  // ############################################
333  // ### Variables used for Truth Calculation ###
334  // ############################################
335  Float_t TruthHitTime = 0 , TruthHitCalculated = 0;
336  int count = 0;
337 
338  // ================================================
339  // === Calculating Time Tick and Drift Velocity ===
340  // ================================================
341  double time_tick = detp->SamplingRate()/1000.;
342  double drift_velocity = detp->DriftVelocity(detp->Efield(),detp->Temperature());
343 
344  for(size_t nh = 0; nh < hitHandle->size(); nh++)
345  {
346  // === Finding Channel associated with the hit ===
347  art::Ptr<recob::Hit> hitPoint(hitHandle, nh);
348  plane = hitPoint->WireID().Plane;
349  //wire = hitPoint->WireID().Wire;
350 
351 
352  // ===================================================================
353  // Using Track IDE's to locate the XYZ location from truth information
354  // ===================================================================
355  std::vector<sim::TrackIDE> trackides;
356  std::vector<double> xyz;
357  try
358  {
359  // ####################################
360  // ### Using BackTrackerService HitCheater ###
361  // ####################################
363 
364  trackides = bt_serv->HitToTrackIDEs(hitPoint);
365  xyz = bt_serv->HitToXYZ(hitPoint);
366  }
367  catch(cet::exception e)
368  {mf::LogWarning("GausHitFinderAna") << "BackTrackerService Failed";
369  continue;}
370 
371 
372  // ==============================================================
373  // Calculating the truth tick position of the hit using 2 methods
374  // Method 1: ConvertXtoTicks from the detector properties package
375  // Method 2: Actually do the calculation myself to double check things
376  // ==============================================================
377 
378  // ### Method 1 ###
379  TruthHitTime = detp->ConvertXToTicks(xyz[0], plane, hitPoint->WireID().TPC, hitPoint->WireID().Cryostat);
380 
381  // ### Method 2 ###
382  // ================================================
383  // Establishing the x-position of the current plane
384  // ================================================
385  const double origin[3] = {0.};
386  double pos[3];
387  geom->Plane(plane).LocalToWorld(origin, pos);
388  double planePos_timeCorr = (pos[0]/drift_velocity)*(1./time_tick)+60;
389  //<---x position of plane / drift velocity + 60 (Trigger offset)
390 
391  TruthHitCalculated = ( (xyz[0]) / (drift_velocity * time_tick) ) + planePos_timeCorr;
392 
393  fTruePeakPos[count] = TruthHitTime;
394  count++;
395  double hitresid = ( ( TruthHitTime - hitPoint->PeakTime() ) / hitPoint->SigmaPeakTime() );
396  fHitResidualAll->Fill( hitresid );
397 
398  double hitresidAlt = ( ( TruthHitCalculated - hitPoint->PeakTime() ) / hitPoint->SigmaPeakTime() );
399  fHitResidualAllAlt->Fill(hitresidAlt);
400 
401  }//<---End nh loop
402 
403  fHTree->Fill();
404  return;
405 
406  }//end analyze method
407 
408  // --------------------------------------------------------
410 
411 } // end of hit namespace
412 
413 
414 
415 #endif // GAUSHITFINDERANA_H
constexpr int kMaxHits
PlaneGeo const & Plane(unsigned int const p, unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified wire.
const std::vector< double > HitToXYZ(const recob::Hit &hit)
intermediate_table::iterator iterator
void reconfigure(fhicl::ParameterSet const &p)
const std::vector< sim::TrackIDE > HitToTrackIDEs(recob::Hit const &hit)
geo::WireID WireID() const
Initial tdc tick for hit.
Definition: Hit.h:234
Declaration of signal hit object.
float SigmaIntegral() const
Initial tdc tick for hit.
Definition: Hit.h:226
virtual double SamplingRate() const =0
Returns the period of the TPC readout electronics clock.
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:130
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
Definition: Hit.h:225
WireID_t Wire
Index of the wire within its plane.
Definition: geo_types.h:313
float GoodnessOfFit() const
Degrees of freedom in the determination of the hit signal shape (-1 by default)
Definition: Hit.h:229
short int Multiplicity() const
How many hits could this one be shared with.
Definition: Hit.h:227
virtual double ConvertXToTicks(double X, int p, int t, int c) const =0
void hits()
Definition: readHits.C:15
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
void beginJob()
Definition: Breakpoints.cc:14
virtual double Temperature() const =0
T get(std::string const &key) const
Definition: ParameterSet.h:231
void analyze(const art::Event &evt)
read/write access to event
PlaneID_t Plane
Index of the plane within its TPC.
Definition: geo_types.h:258
Monte Carlo Simulation.
float PeakTimeMinusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:240
Detector simulation of raw signals on wires.
std::vector< float > Signal() const
Return a zero-padded full length vector filled with RoI signal.
Definition: Wire.cxx:47
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:219
T * make(ARGS...args) const
Encapsulate the construction of a single detector plane.
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
virtual double DriftVelocity(double efield=0., double temperature=0.) const =0
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
EventNumber_t event() const
Definition: EventID.h:117
Declaration of basic channel signal object.
Base class for creation of raw signals on wires.
float SigmaPeakTime() const
Uncertainty for the signal peak, in tick units.
Definition: Hit.h:220
float PeakTimePlusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:237
virtual double Efield(unsigned int planegap=0) const =0
Returns the nominal electric field in the specified volume.
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:464
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:203
Float_t e
Definition: plot.C:34
RunNumber_t run() const
Definition: Event.h:77
Namespace collecting geometry-related classes utilities.
Definition: fwd.h:25
void LocalToWorld(const double *plane, double *world) const
Transform point from local plane frame to world frame.
Definition: PlaneGeo.h:1124
EventID id() const
Definition: Event.h:56
art framework interface to geometry description
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:230
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33