All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CrtOpHitMatchAnalysis_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CrtOpHitMatchAnalysis
3 // Plugin Type: analyzer (art v3_05_00)
4 // File: CrtOpHitMatchAnalysis_module.cc
5 //
6 // Generated at Tue Apr 14 19:49:18 2020 by Christopher Hilgenberg using cetskelgen
7 // from cetlib version v3_10_00.
8 ////////////////////////////////////////////////////////////////////////
9 
10 //Framework includes
11 #include "art/Framework/Core/EDAnalyzer.h"
12 #include "art/Framework/Core/ModuleMacros.h"
13 #include "art/Framework/Principal/Event.h"
14 #include "art/Framework/Principal/Handle.h"
15 #include "art/Framework/Principal/Run.h"
16 #include "art/Framework/Principal/SubRun.h"
17 #include "canvas/Utilities/InputTag.h"
18 #include "fhiclcpp/ParameterSet.h"
19 #include "messagefacility/MessageLogger/MessageLogger.h"
20 #include "art_root_io/TFileService.h"
21 #include "art/Framework/Services/Registry/ServiceHandle.h"
22 #include "canvas/Persistency/Common/FindManyP.h"
23 
24 //LArSoft includes
29 //#include "larsim/MCCheater/PhotonBackTrackerService.h"
30 
31 //Data product includes
32 #include "nusimdata/SimulationBase/MCGeneratorInfo.h"
33 #include "nusimdata/SimulationBase/MCTruth.h"
34 #include "nusimdata/SimulationBase/MCParticle.h"
41 
42 //C++ includes
43 #include <vector>
44 #include <map>
45 
46 //ROOT includes
47 #include "TTree.h"
48 #include "TVector3.h"
49 
50 using std::vector;
51 using std::map;
52 
53 namespace icarus {
54  namespace crt {
56  }
57 }
58 
59 using namespace icarus::crt;
60 
61 class icarus::crt::CrtOpHitMatchAnalysis : public art::EDAnalyzer {
62  public:
63 
65 
66  explicit CrtOpHitMatchAnalysis(fhicl::ParameterSet const& p);
67  // The compiler-generated destructor is fine for non-base
68  // classes without bare pointers or other resource use.
69 
70  // Plugins should not be copied or assigned.
73  CrtOpHitMatchAnalysis& operator=(CrtOpHitMatchAnalysis const&) = delete;
74  CrtOpHitMatchAnalysis& operator=(CrtOpHitMatchAnalysis&&) = delete;
75 
76  // Required functions.
77  void analyze(art::Event const& e) override;
78 
79  private:
80 
81  const double LAR_PROP_DELAY = 1.0/(30.0/1.38); //[ns/cm]
82 
83  bool HitCompare(const art::Ptr<CRTHit>& h1, const art::Ptr<CRTHit>& h2);
84  void ClearVecs();
85  //const detinfo::DetectorClocks* fClock;
86 
87  art::InputTag fGenLabel;
88  art::InputTag fSimLabel;
89  art::InputTag fOpHitModuleLabel;
90  art::InputTag fOpFlashModuleLabel0;
91  art::InputTag fOpFlashModuleLabel1;
92  art::InputTag fOpFlashModuleLabel2;
93  art::InputTag fOpFlashModuleLabel3;
94  art::InputTag fCrtHitModuleLabel;
95  art::InputTag fCrtTrackModuleLabel;
96 
97  double fCoinWindow;
98  double fOpDelay;
99  double fCrtDelay;
106 
109 
110  map<int,art::InputTag> fFlashLabels;
111 
112  TTree* fMatchTree;
113  TTree* fHitTree;
114  TTree* fFlashTree;
115 
116  //matchTree vars
117  int fNCrt; //number of CRT hits per event
118  vector<vector<double>> fCrtXYZT; //CRT hit x,y,z,t [cm/ns]
119  vector<vector<double>> fCrtXYZErr; //CRT hit x,y,z,t error [cm/ns]
120  vector<int> fCrtRegion; //CRT hit region code
121  vector<double> fCrtPE; //total PE's for CRT hit
122  vector<double> fTofHit; //CRT - PMT TOF using OpHit
123  vector<double> fTofFlash; //CRT - PMT TOF using OpFlash
124  vector<double> fTofFlashHit;
125  vector<double> fDistHit; //CRT - PMT distance [cm]
126  vector<double> fDistFlash; //CRT - flash barycenter distance [cm]
127  vector<double> fDistFlashHit;
128  vector<double> fTofPeHit; //total PE for matched OpHit
129  vector<double> fTofPeFlash; //total PE for matched OpFlash
130  vector<double> fTofPeFlashHit;
131  vector<vector<double>> fTofXYZTHit; //position/time [cm/ns] for matched OpHit
132  vector<vector<double>> fTofXYZTFlash; //position/time [cm/ns] for matched OpFlash
133  vector<vector<double>> fTofXYZTFlashHit;
134  vector<int> fTofTpcHit; //TPC for matched OpHit
135  vector<int> fTofTpcFlash; //TPC for matched OpFlash
136  vector<bool> fMatchHit; //was CRT hit matched to OpHit?
137  vector<bool> fMatchFlash; //was CRT hit matched to OpFlash?
138  vector<double> fTrueDist; //true dist from reco CRT hit to PMT
139  vector<double> fTrueTOF; //true TOF from reco CRT hit to AV entry to PMT
140  vector<vector<double>> fEnterXYZT;
141  vector<vector<double>> fPMTXYZT;
142  vector<int> fHitPDG; //PDG of particle w/most dep'ed energy in hit
143  vector<bool> fIV; //does particle ass'ed with hit enter IV
144  vector<bool> fAV; //does particle ass'ed with hit enter AV
145  vector<bool> fFV; //does particle ass'ed with hit enter FV
146  bool fNu; //is this a neutrino event
147  bool fNuCC; //is nu int CC
148  double fNuE; //true neutrino energy [GeV]
149  double fNuXYZT[4]; //true neutrino vertex pos[cm]/int time[ns]
150  bool fNuIV; //is nu vertex in IV
151  bool fNuAV; //is nu vertex in AV
152  bool fNuFV; //is nu vertex in FV
153  vector<bool> fTrackFilt; //excluded by track filter
154 
155  //hitTree vars
156  int fNHit; //number of OpHits per event
157  vector<double> fHitPE; //PE for each OpHit
158  vector<int> fHitChan; //OpHit channel ID
159  vector<vector<double>> fHitXYZT; //position and time for each OpHit [cm/ns]
160 
161  //flashTree vars
162  int fNFlash; //number of OpFlashes per event
163  vector<int> fFlashTPC; //TPC where flash occured
164  vector<vector<double>> fFlashXYZT; //position and time of flash [cm/ns]
165  vector<double> fFlashPE; //total PE's per flash
166  vector<int> fFlashNHit; //number of OpHits per flash
167  vector<double> fFlashMeanPE; //mean OpHit PE averaged over all OpHits in flash
168  vector<double> fFlashRmsPE; //RMS of OPHit PE distro. in flash
169  vector<vector<double>> fFlashDelta;
170 
171  geo::GeometryCore const* fGeometryService; ///< pointer to Geometry provider
172 };
173 
174 
176  : EDAnalyzer{p} ,
177  fGenLabel(p.get<art::InputTag>("GenLabel","generator")),
178  fSimLabel(p.get<art::InputTag>("SimLabel","largeant")),
179  fOpHitModuleLabel(p.get<art::InputTag>("OpHitModuleLabel","ophit")),
180  fOpFlashModuleLabel0(p.get<art::InputTag>("OpFlashModuleLabel0","opflashTPC0")),
181  fOpFlashModuleLabel1(p.get<art::InputTag>("OpFlashModuleLabel1","opflashTPC1")),
182  fOpFlashModuleLabel2(p.get<art::InputTag>("OpFlashModuleLabel2","opflashTPC2")),
183  fOpFlashModuleLabel3(p.get<art::InputTag>("OpFlashModuleLabel3","opflashTPC3")),
184  fCrtHitModuleLabel(p.get<art::InputTag>("CrtHitModuleLabel","crthit")),
185  fCrtTrackModuleLabel(p.get<art::InputTag>("CrtTrackModuleLabel","crttrack")),
186  fCoinWindow(p.get<double>("CoincidenceWindow",60.0)),
187  fOpDelay(p.get<double>("OpDelay",55.1)),
188  fCrtDelay(p.get<double>("CrtDelay",1.6e6)),
189  fFlashPeThresh(p.get<int>("FlashPeThresh",9000)),
190  fHitPeThresh(p.get<int>("HitPeThresh",700)),
191  fFlashVelocity(p.get<double>("FlashVelocityThresh",-40.)),
192  fFlashZOffset(p.get<double>("FlashZOffset",0.)),
193  fHitVelocityMax(p.get<double>("HitVelocityMax",20.)),
194  fHitVelocityMin(p.get<double>("HitVelocityMin",1.)),
195  bt(new CRTBackTracker(p.get<fhicl::ParameterSet>("CRTBackTrack"))),
196  crtutil(new CRTCommonUtils())
197 {
198  fFlashLabels[0] = fOpFlashModuleLabel0;
199  fFlashLabels[1] = fOpFlashModuleLabel1;
200  fFlashLabels[2] = fOpFlashModuleLabel2;
201  fFlashLabels[3] = fOpFlashModuleLabel3;
202 
203  // Get a pointer to the geometry service provider.
204  fGeometryService = lar::providerFrom<geo::Geometry>();
205 
206  art::ServiceHandle<art::TFileService> tfs;
207 
208  fMatchTree = tfs->make<TTree>("matchTree","CRTHit - OpHit/Flash matching analysis");
209  fHitTree = tfs->make<TTree>("hitTree", "OpHit info");
210  fFlashTree = tfs->make<TTree>("flashTree", "OpFlash info");
211 
212  fHitTree->Branch("nhit", &fNHit, "nOpHit/I");
213  fHitTree->Branch("xyzt", &fHitXYZT);
214  fHitTree->Branch("pe", &fHitPE);
215  fHitTree->Branch("chan", &fHitChan);
216 
217  fFlashTree->Branch("nflash", &fNFlash, "nOpFlash/I");
218  fFlashTree->Branch("tpc", &fFlashTPC);
219  fFlashTree->Branch("xyzt", &fFlashXYZT);
220  fFlashTree->Branch("totpe", &fFlashPE);
221  fFlashTree->Branch("nhit", &fFlashNHit);
222  fFlashTree->Branch("meanpe", &fFlashMeanPE);
223  fFlashTree->Branch("rmspe", &fFlashRmsPE);
224  fFlashTree->Branch("delta", &fFlashDelta);
225 
226  fMatchTree->Branch("ncrt", &fNCrt, "nCrtHit/I");
227  fMatchTree->Branch("crtXYZT", &fCrtXYZT);
228  fMatchTree->Branch("crtXYZErr", &fCrtXYZErr);
229  fMatchTree->Branch("crtPE", &fCrtPE);
230  fMatchTree->Branch("crtRegion", &fCrtRegion);
231  fMatchTree->Branch("tofHit", &fTofHit);
232  fMatchTree->Branch("tofFlash", &fTofFlash);
233  fMatchTree->Branch("tofFlashHit", &fTofFlashHit);
234  fMatchTree->Branch("peHit", &fTofPeHit);
235  fMatchTree->Branch("peFlash", &fTofPeFlash);
236  fMatchTree->Branch("peFlashHit", &fTofPeFlashHit);
237  fMatchTree->Branch("xyztHit", &fTofXYZTHit);
238  fMatchTree->Branch("xyztFlash", &fTofXYZTFlash);
239  fMatchTree->Branch("xyztFlashHit", &fTofXYZTFlashHit);
240  fMatchTree->Branch("distHit", &fDistHit);
241  fMatchTree->Branch("distFlash", &fDistFlash);
242  fMatchTree->Branch("distFlashHit", &fDistFlashHit);
243  fMatchTree->Branch("tpcHit", &fTofTpcHit);
244  fMatchTree->Branch("tpcFlash", &fTofTpcFlash);
245  fMatchTree->Branch("matchHit", &fMatchHit);
246  fMatchTree->Branch("matchFlash", &fMatchFlash);
247  fMatchTree->Branch("distTrue", &fTrueDist);
248  fMatchTree->Branch("tofTrue", &fTrueTOF);
249  fMatchTree->Branch("enterXYZT", &fEnterXYZT);
250  fMatchTree->Branch("pmtXYZT", &fPMTXYZT);
251  fMatchTree->Branch("crtPDG", &fHitPDG);
252  fMatchTree->Branch("crtIV", &fIV);
253  fMatchTree->Branch("crtAV", &fAV);
254  fMatchTree->Branch("crtFV", &fFV);
255  fMatchTree->Branch("nu", &fNu, "nu/O");
256  fMatchTree->Branch("nuCC", &fNuCC, "nuCC/O");
257  fMatchTree->Branch("nuE", &fNuE, "nuE/D");
258  fMatchTree->Branch("nuXYZT", fNuXYZT, "nuXYZT[4]/D");
259  fMatchTree->Branch("nuIV", &fNuIV, "nuIV/O");
260  fMatchTree->Branch("nuAV", &fNuAV, "nuAV/O");
261  fMatchTree->Branch("nuFV", &fNuFV, "nuFV/O");
262  fMatchTree->Branch("trackfilt", &fTrackFilt);
263 
264  //fClock = lar::providerFrom<detinfo::DetectorClocksService>();
265 }
266 
267 void CrtOpHitMatchAnalysis::analyze(art::Event const& e)
268 {
269  //art::ServiceHandle<cheat::PhotonBackTrackerService> pbt;
270  //fTTrig = fClock->TriggerTime();
271 
272  geo::CryostatGeo const& cryo0 = fGeometryService->Cryostat(0);
273  geo::CryostatGeo const& cryo1 = fGeometryService->Cryostat(1);
274  geo::TPCGeo const& tpc00 = cryo0.TPC(0);
275  geo::TPCGeo const& tpc01 = cryo0.TPC(1);
276  geo::TPCGeo const& tpc10 = cryo1.TPC(0);
277  geo::TPCGeo const& tpc11 = cryo1.TPC(1);
278  ClearVecs();
279 
280  auto const& mctruths = //vector of MCTruths from GENIE
281  *e.getValidHandle<vector<simb::MCTruth>>(fGenLabel);
282 
283  fNuIV = false;
284  fNuAV = false;
285  fNuFV = false;
286  if(mctruths[0].GeneratorInfo().generator==simb::Generator_t::kGENIE){
287  fNu = true;
288  auto const& nu = mctruths[0].GetNeutrino();
289  const TLorentzVector xyzt = nu.Nu().Position(0);
290  fNuE = nu.Nu().E();
291  fNuCC = nu.CCNC();
292  fNuXYZT[0] = xyzt.X();
293  fNuXYZT[1] = xyzt.Y();
294  fNuXYZT[2] = xyzt.Z();
295  fNuXYZT[3] = xyzt.T();
296  double point[3];
297  for(int i=0; i<3; i++) point[i] = fNuXYZT[i];
298  if(cryo0.ContainsPosition(point) || cryo1.ContainsPosition(point)){
299  fNuIV = true;
300  if(tpc00.ContainsPosition(point) ||
301  tpc01.ContainsPosition(point) ||
302  tpc10.ContainsPosition(point) ||
303  tpc11.ContainsPosition(point) ) {
304 
305  fNuIV = false;
306  fNuAV = true;
307  fNuFV = true;
308  }
309  }
310  }
311  else{
312  fNu = false;
313  fNuE = DBL_MAX;
314  fNuCC = false;
315  for(int i=0; i<4; i++)
316  fNuXYZT[i] = DBL_MAX;
317  }
318 
319  auto const& simparticles = //vector of MCParticles from G4
320  *e.getValidHandle<vector<simb::MCParticle>>(fSimLabel);
321 
322  //loop over G4 tracks
323  map<int,const simb::MCParticle*> particleMap;
324  for(auto const& particle : simparticles){
325 
326  particleMap[particle.TrackId()] = &particle;
327 
328  }//G4 tracks
329 
330  //OpHits
331  art::Handle< std::vector<recob::OpHit> > opHitListHandle;
332  std::vector< art::Ptr<recob::OpHit> > opHitList;
333  if( e.getByLabel(fOpHitModuleLabel,opHitListHandle) )
334  art::fill_ptr_vector(opHitList, opHitListHandle);
335 
336  fNHit = opHitList.size();
337  for(auto const& ophit : opHitList){
338  double t = ophit->PeakTime()*1e3-fOpDelay;
339  fHitPE.push_back(ophit->PE());
340  fHitChan.push_back(ophit->OpChannel());
341  double pos[3];
342  fGeometryService->OpDetGeoFromOpChannel(ophit->OpChannel()).GetCenter(pos);
343  vector<double> xyzt = {pos[0],pos[1],pos[2],t};
344  fHitXYZT.push_back(xyzt);
345  }
346 
347  fHitTree->Fill();
348 
349  //OpFlash
350  map<int, art::Handle< std::vector<recob::OpFlash> > > flashHandles;
351  std::map<int,std::vector< art::Ptr<recob::OpFlash> >> opFlashLists;
352  fNFlash = 0;
353  for(int i=0; i<4; i++) {
354  if( e.getByLabel(fFlashLabels[i],flashHandles[i]) )
355  art::fill_ptr_vector(opFlashLists[i], flashHandles[i]);
356  }
357 
358  for(auto const& flashList : opFlashLists) {
359  fNFlash += flashList.second.size();
360 
361  for(size_t iflash=0; iflash<flashList.second.size(); iflash++) {
362  auto const& flash = flashList.second[iflash];
363 
364  vector<double> xyzt;
365  xyzt.push_back(0.);
366  xyzt.push_back(flash->YCenter());
367  xyzt.push_back(flash->ZCenter());
368  xyzt.push_back(flash->Time()*1e3-fOpDelay);
369  fFlashXYZT.push_back(xyzt);
370  fFlashPE.push_back(flash->TotalPE());
371  fFlashTPC.push_back(flashList.first);
372  auto const& pes = flash->PEs();
373  fFlashNHit.push_back(pes.size());
374  fFlashMeanPE.push_back(fFlashPE.back()/fFlashNHit.back());
375  double rms2=0.;
376  for(auto const& pe : pes)
377  rms2 += pow((pe-fFlashMeanPE.back()),2);
378  rms2*=1.0/(fFlashNHit.back()-1);
379  fFlashRmsPE.push_back(sqrt(rms2));
380  vector<double> delta = {0., flash->YWidth(),flash->ZWidth(),flash->TimeWidth()};
381  fFlashDelta.push_back(delta);
382 
383  /*vector<art::Ptr<recob::OpHit>> hits = findManyHits.at(iflash);
384  for(auto const& hit : hits) {
385  double tPmt = hit->PeakTime()*1.e3-fOpDelay;
386  if( tPmt < flashHitT) {
387  flashHitT = tPmt;
388  flashHitPE = hit->PE();
389 
390  //FlashHit position/time
391  geo::OpDetGeo const& opDet = cryo0.OpDet(hit->OpChannel());
392  double pos[3];
393  opDet.GetCenter(pos);
394  flashHitxyzt.clear();
395  for(int i=0; i<3; i++) flashHitxyzt.push_back(pos[i]);
396  flashHitxyzt.push_back(flashHitT);
397 
398  //FlashHit distance
399  TVector3 rflashHit(pos[0],pos[1],pos[2]);
400  TVector3 vdiffHit = rcrt-rflashHit;
401  flashHitDiff = vdiffHit.Mag();
402  }
403  }//loop over flash hits*/
404 
405  }
406  }
407 
408  fFlashTree->Fill();
409 
410  //CRTracks
411  art::Handle< std::vector<sbn::crt::CRTTrack> > crtTrackListHandle;
412  std::vector< art::Ptr<sbn::crt::CRTTrack> > crtTrackList;
413  if( e.getByLabel(fCrtTrackModuleLabel,crtTrackListHandle))
414  art::fill_ptr_vector(crtTrackList, crtTrackListHandle);
415 
416  art::FindManyP<CRTHit> findManyHits(
417  crtTrackListHandle, e, fCrtTrackModuleLabel);
418 
419 
420  //get time-ordered(ascending) vector of hits for each track
421  std::vector<std::vector<art::Ptr<CRTHit>>> trackhits;
422  for(size_t itrk=0; itrk<crtTrackList.size(); itrk++){
423 
424  std::vector<art::Ptr<CRTHit>> trkhits = findManyHits.at(itrk);
425  std::sort(trkhits.begin(),trkhits.end(),
426  [](const art::Ptr<CRTHit>& a, const art::Ptr<CRTHit>& b)->bool
427  {
428  return a->ts0_ns < b->ts0_ns;
429  });
430 
431  trackhits.push_back(trkhits);
432 
433  }//for tracks
434 
435 
436  //CRTHits
437  art::Handle< std::vector<CRTHit> > crtHitListHandle;
438  std::vector< art::Ptr<CRTHit> > crtHitList;
439  if( e.getByLabel(fCrtHitModuleLabel,crtHitListHandle))
440  art::fill_ptr_vector(crtHitList, crtHitListHandle);
441 
442  fNCrt = crtHitList.size();
443 
444  for(auto const& crt : crtHitList){
445 
446  bool trackfilt=false;
447  for(auto const& trkhits: trackhits){
448  for(size_t ihit=1; ihit<trkhits.size(); ihit++) {
449  if(HitCompare(trkhits[ihit],crt)){
450  trackfilt = true;
451  break;
452  }
453  }
454  if(trackfilt)
455  break;
456  }
457  fTrackFilt.push_back(trackfilt);
458 
459  vector<double> xyzt, xyzerr;
460  TVector3 rcrt(crt->x_pos,crt->y_pos,crt->z_pos);
461 
462  xyzt.push_back(rcrt.X());
463  xyzt.push_back(rcrt.Y());
464  xyzt.push_back(rcrt.Z());
465  double tcrt = (int32_t)crt->ts0_ns - fCrtDelay;
466  xyzt.push_back(tcrt);
467  fCrtXYZT.push_back(xyzt);
468 
469  xyzerr.push_back(crt->x_err);
470  xyzerr.push_back(crt->y_err);
471  xyzerr.push_back(crt->z_err);
472  fCrtXYZErr.push_back(xyzerr);
473 
474  fCrtPE.push_back(crt->peshit);
475  fCrtRegion.push_back(crtutil->AuxDetRegionNameToNum(crt->tagger));
476 
477  // -- get truth info --
478  //get trackID associated with CRTHit
479  int trackID = bt->TrueIdFromTotalEnergy(e,*crt);
480  bool firstIV=false, firstAV=false, firstFV=false;
481 
482  //if trackID exists in particle map
483  if(particleMap.find(abs(trackID))!=particleMap.end()){
484  auto const& particle = particleMap[abs(trackID)];
485  fHitPDG.push_back(particle->PdgCode());
486  for(size_t i=0; i<particle->NumberTrajectoryPoints(); i++){
487  const TLorentzVector& pos = particle->Position(i);
488  double point[3] = {pos.X(),pos.Y(),pos.Z()};
489  if(cryo0.ContainsPosition(point)) {
490  firstIV = true;
491  if(!firstAV && (tpc00.ContainsPosition(point) ||
492  tpc01.ContainsPosition(point)) ) {
493  double opDetPos[3];
494  (cryo0.OpDet(cryo0.GetClosestOpDet(point))).GetCenter(opDetPos);
495  double ddirect = sqrt(pow(opDetPos[0]-rcrt.X(),2)
496  + pow(opDetPos[1]-rcrt.Y(),2)
497  + pow(opDetPos[2]-rcrt.Z(),2));
498  double dprop = sqrt(pow(opDetPos[0]-pos[0],2)
499  + pow(opDetPos[1]-pos[1],2)
500  + pow(opDetPos[2]-pos[2],2));
501  double tprop = pos.T() + dprop*LAR_PROP_DELAY;
502  fTrueDist.push_back(ddirect);
503  fTrueTOF.push_back(tcrt-tprop);
504  vector<double> tmp1 = {pos.X(),pos.Y(),pos.Z(),pos.T()};
505  vector<double> tmp2 = {opDetPos[0],opDetPos[1],opDetPos[2],tprop};
506  fEnterXYZT.push_back(tmp1);
507  fPMTXYZT.push_back(tmp2);
508  firstIV = false;
509  firstAV = true;
510  firstFV = true;
511  }//if AV
512  }//if IV
513  if(!firstAV && cryo1.ContainsPosition(point)){
514  if(!firstAV && (tpc10.ContainsPosition(point) ||
515  tpc11.ContainsPosition(point)) ) {
516  double opDetPos[3];
517  (cryo1.OpDet(cryo0.GetClosestOpDet(point))).GetCenter(opDetPos);
518  double ddirect = sqrt(pow(opDetPos[0]-rcrt.X(),2)
519  + pow(opDetPos[1]-rcrt.Y(),2)
520  + pow(opDetPos[2]-rcrt.Z(),2));
521  double dprop = sqrt(pow(opDetPos[0]-pos[0],2)
522  + pow(opDetPos[1]-pos[1],2)
523  + pow(opDetPos[2]-pos[2],2));
524  double tprop = pos.T() + dprop*LAR_PROP_DELAY;
525  fTrueDist.push_back(ddirect);
526  fTrueTOF.push_back(tcrt-tprop);
527  vector<double> tmp1 = {pos.X(),pos.Y(),pos.Z(),pos.T()};
528  vector<double> tmp2 = {opDetPos[0],opDetPos[1],opDetPos[2],tprop};
529  fEnterXYZT.push_back(tmp1);
530  fPMTXYZT.push_back(tmp2);
531  firstIV = false;
532  firstAV = true;
533  firstFV = true;
534  }//if AV
535  }//if IV
536  if(firstAV) break;
537  }//for traj points
538  }
539  else {
540  std::cout << "CRTHit trackID " << trackID << " not found in particle map!" << std::endl;
541  fHitPDG.push_back(INT_MAX);
542  fTrueDist.push_back(DBL_MAX);
543  fTrueTOF.push_back(DBL_MAX);
544  fEnterXYZT.push_back({DBL_MAX,DBL_MAX,DBL_MAX,DBL_MAX});
545  fPMTXYZT.push_back({DBL_MAX,DBL_MAX,DBL_MAX,DBL_MAX});
546  }
547  fIV.push_back(firstIV);
548  fAV.push_back(firstAV);
549  fFV.push_back(firstFV);
550  if(!firstAV&&fHitPDG.back()!=INT_MAX){
551  fTrueDist.push_back(DBL_MAX);
552  fTrueTOF.push_back(DBL_MAX);
553  fEnterXYZT.push_back({DBL_MAX,DBL_MAX,DBL_MAX,DBL_MAX});
554  fPMTXYZT.push_back({DBL_MAX,DBL_MAX,DBL_MAX,DBL_MAX});
555  }//end get truth info
556 
557  // -- flash match --
558  int matchtpc = -1;
559  double tdiff = DBL_MAX, rdiff=DBL_MAX, peflash=DBL_MAX;
560  bool matched=false;
561  xyzt.clear();
562  double flashHitT = DBL_MAX, flashHitPE=DBL_MAX, flashHitDiff=DBL_MAX;
563  vector<double> flashHitxyzt;
564 
565  for(auto const& flashList : opFlashLists) {
566 
567  art::FindManyP<recob::OpHit> findManyHits(
568  flashHandles[flashList.first], e, fFlashLabels[flashList.first]);
569 
570  for(size_t iflash=0; iflash<flashList.second.size(); iflash++) {
571 
572  auto const& flash = flashList.second[iflash];
573  if(flash->TotalPE()<fFlashPeThresh){
574  continue;
575  }
576 
577  double tflash = flash->Time()*1e3-fOpDelay;
578  TVector3 rflash(0,flash->YCenter(),flash->ZCenter());
579  TVector3 vdiff = rcrt-rflash;
580  if(abs(tcrt-tflash)<abs(tdiff)) {
581  peflash = flash->TotalPE();
582  tdiff = tcrt-tflash;
583  rdiff = vdiff.Mag();
584  xyzt.clear();
585  xyzt.push_back(rflash.X());
586  xyzt.push_back(rflash.Y());
587  xyzt.push_back(rflash.Z());
588  xyzt.push_back(tflash);
589  matched = true;
590  matchtpc = flashList.first;
591 
592  vector<art::Ptr<recob::OpHit>> hits = findManyHits.at(iflash);
593  for(auto const& hit : hits) {
594  double tPmt = hit->PeakTime()*1.e3-fOpDelay;
595  if( tPmt < flashHitT) {
596  flashHitT = tPmt;
597  flashHitPE = hit->PE();
598 
599  //FlashHit position/time
600  double pos[3];
601  fGeometryService->OpDetGeoFromOpChannel(hit->OpChannel()).GetCenter(pos);
602  flashHitxyzt.clear();
603  for(int i=0; i<3; i++) flashHitxyzt.push_back(pos[i]);
604  flashHitxyzt.push_back(flashHitT);
605 
606  //FlashHit distance
607  TVector3 rflashHit(pos[0],pos[1],pos[2]);
608  TVector3 vdiffHit = rcrt-rflashHit;
609  flashHitDiff = vdiffHit.Mag();
610  }
611  }//loop over flash hits
612 
613  }//if minimum tdiff
614  }//for OpFlash in this flash list
615  }//for flash lists
616  if(!matched) {
617  peflash = DBL_MAX;
618  for(int i=0; i<4; i++) xyzt.push_back(DBL_MAX);
619  }
620 
621  fMatchFlash.push_back(matched);
622  fTofFlash.push_back(tdiff);
623  fTofPeFlash.push_back(peflash);
624  fTofXYZTFlash.push_back(xyzt);
625  fDistFlash.push_back(rdiff);
626  fTofTpcFlash.push_back(matchtpc);
627  fTofFlashHit.push_back(tcrt-flashHitT);
628  fTofPeFlashHit.push_back(flashHitPE);
629  fTofXYZTFlashHit.push_back(flashHitxyzt);
630  fDistFlashHit.push_back(flashHitDiff);
631 
632  // -- match OpHits to CRTHits --
633  tdiff = DBL_MAX;
634  matched = false;
635  peflash = DBL_MAX;
636  rdiff = DBL_MAX;
637  xyzt.clear();
638 
639  double pemax = 0.;
640  for(auto const& hit : opHitList) {
641  double thit = hit->PeakTime()*1e3-fOpDelay;
642  if(hit->PE()<fHitPeThresh){
643  continue;
644  }
645 
646  //if(abs(tcrt-thit)<abs(tdiff)) {
647  if(abs(tcrt-thit)<fCoinWindow && hit->PE()>pemax) {
648  pemax = hit->PE();
649 
650  //hitXYZT
651  double pos[3];
652  fGeometryService->OpDetGeoFromOpChannel(hit->OpChannel()).GetCenter(pos);
653 
654  //distHit
655  TVector3 rhit (pos[0],pos[1],pos[2]);
656  TVector3 vdiff = rcrt-rhit;
657 
658  //double vel = abs(vdiff.Mag()/(tcrt-thit));
659  //if(vel>fHitVelocityMax || vel<fHitVelocityMin)
660  // continue;
661  rdiff = vdiff.Mag();
662  peflash = hit->PE();
663  tdiff = tcrt-thit;
664  xyzt.clear();
665  for(int i=0; i<3; i++) xyzt.push_back(pos[i]);
666  xyzt.push_back(thit);
667 
668  matched = true;
669 
670  }//if min tof
671  }//for OpHits
672  if(!matched) {
673  tdiff = DBL_MAX;
674  peflash = DBL_MAX;
675  rdiff = DBL_MAX;
676  xyzt.clear();
677  for(int i=0; i<4; i++) xyzt.push_back(DBL_MAX);
678  }
679 
680  fMatchHit.push_back(matched);
681  fTofHit.push_back(tdiff);
682  fTofPeHit.push_back(peflash);
683  fTofXYZTHit.push_back(xyzt);
684  fDistHit.push_back(rdiff);
685  fTofTpcHit.push_back(matchtpc);
686  }//for CRTHits
687 
688  fMatchTree->Fill();
689 
690 }//analyze
691 
693 {
694  //matchTree
695  fCrtXYZT.clear();
696  fCrtXYZErr.clear();
697  fCrtRegion.clear();
698  fCrtPE.clear();
699  fTofHit.clear();
700  fTofFlash.clear();
701  fTofFlashHit.clear();
702  fDistHit.clear();
703  fDistFlash.clear();
704  fDistFlashHit.clear();
705  fTofPeHit.clear();
706  fTofPeFlash.clear();
707  fTofPeFlashHit.clear();
708  fTofXYZTHit.clear();
709  fTofXYZTFlash.clear();
710  fTofXYZTFlashHit.clear();
711  fTofTpcHit.clear();
712  fTofTpcFlash.clear();
713  fMatchHit.clear();
714  fMatchFlash.clear();
715  fTrueDist.clear();
716  fTrueTOF.clear();
717  fEnterXYZT.clear();
718  fPMTXYZT.clear();
719  fHitPDG.clear();
720  fIV.clear();
721  fAV.clear();
722  fFV.clear();
723  fTrackFilt.clear();
724 
725  //hitTree
726  fHitXYZT.clear();
727  fHitPE.clear();
728  fHitChan.clear();
729 
730  //flashTree
731  fFlashTPC.clear();
732  fFlashXYZT.clear();
733  fFlashPE.clear();
734  fFlashMeanPE.clear();
735  fFlashRmsPE.clear();
736  fFlashDelta.clear();
737 
738 }
739 
740 
741 bool CrtOpHitMatchAnalysis::HitCompare(const art::Ptr<CRTHit>& hit1, const art::Ptr<CRTHit>& hit2) {
742 
743  if(hit1->ts1_ns != hit2->ts1_ns) return false;
744  if(hit1->plane != hit2->plane) return false;
745  if(hit1->x_pos != hit2->x_pos) return false;
746  if(hit1->y_pos != hit2->y_pos) return false;
747  if(hit1->z_pos != hit2->z_pos) return false;
748  if(hit1->x_err != hit2->x_err) return false;
749  if(hit1->y_err != hit2->y_err) return false;
750  if(hit1->z_err != hit2->z_err) return false;
751  if(hit1->tagger != hit2->tagger) return false;
752 
753  return true;
754 
755 }
756 
757 DEFINE_ART_MODULE(CrtOpHitMatchAnalysis)
unsigned int GetClosestOpDet(geo::Point_t const &point) const
process_name opflashCryo1 flashfilter analyze
Utilities related to art service access.
void analyze(art::Event const &e) override
pdgs p
Definition: selectors.fcl:22
Geometry information for a single TPC.
Definition: TPCGeo.h:38
process_name hit
Definition: cheaterreco.fcl:51
Geometry information for a single cryostat.
Definition: CryostatGeo.h:43
process_name gaushit a
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Access the description of detector geometry.
T abs(T value)
const OpDetGeo & OpDet(unsigned int iopdet) const
Return the iopdet&#39;th optical detector in the cryostat.
bool HitCompare(const art::Ptr< CRTHit > &h1, const art::Ptr< CRTHit > &h2)
CryostatGeo const & Cryostat(geo::CryostatID const &cryoid) const
Returns the specified cryostat.
Description of geometry of one entire detector.
BEGIN_PROLOG opflashCryoW opflashCryoW triggerfilterBNB triggerfilterNuMI triggerfilterOffbeamBNB triggerfilterOffbeamNuMI triggerfilterUnknown roifinder roifinder2d gaushitTPCEE gaushitTPCWE purityana1 ophit
CrtOpHitMatchAnalysis(fhicl::ParameterSet const &p)
const TPCGeo & TPC(unsigned int itpc) const
Return the itpc&#39;th TPC in the cryostat.
Definition: CryostatGeo.cxx:93
do i e
static double tdiff(const art::Timestamp &ts1, const art::Timestamp &ts2)
int TrueIdFromTotalEnergy(const art::Event &event, const CRTData &data)
geo::GeometryCore const * fGeometryService
pointer to Geometry provider
art::ServiceHandle< art::TFileService > tfs
process_name crt
BEGIN_PROLOG SN nu
OpDetGeo const & OpDetGeoFromOpChannel(unsigned int OpChannel) const
Returns the geo::OpDetGeo object for the given channel number.
bool ContainsPosition(geo::Point_t const &point, double wiggle=1.0) const
Returns whether this volume contains the specified point.
art framework interface to geometry description
BEGIN_PROLOG could also be cout