All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRTTPCMatchingAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CRTTPCMatchingAna
3 // Plugin Type: analyzer (Unknown Unknown)
4 // File: CRTTPCMatchingAna_module.cc
5 //
6 // Generated at Wed Feb 16 22:07:45 2022 by Biswaranjan Behera using cetskelgen
7 // from version .
8 ////////////////////////////////////////////////////////////////////////
9 // Framework includes
10 #include "art/Framework/Core/EDAnalyzer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Principal/Run.h"
15 #include "art/Framework/Principal/SubRun.h"
16 #include "canvas/Utilities/InputTag.h"
17 #include "fhiclcpp/ParameterSet.h"
18 #include "fhiclcpp/types/Table.h"
19 #include "fhiclcpp/types/Atom.h"
20 #include "cetlib/pow.h" // cet::sum_of_squares()
21 #include "messagefacility/MessageLogger/MessageLogger.h"
22 #include "art/Framework/Services/Registry/ServiceHandle.h"
23 #include "art_root_io/TFileService.h"
24 #include "canvas/Persistency/Common/FindManyP.h"
25 #include "canvas/Utilities/Exception.h"
28 
29 
30 // LArSoft includes
34 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
38 #include "nusimdata/SimulationBase/MCParticle.h"
40 
41 // icaruscode includes
45 //#include "icaruscode/CRT/CRTUtils/CRTBackTracker.h"
46 //#include "icaruscode/CRT/CRTUtils/CRTEventDisplay.h"
49 
50 #include "canvas/Persistency/Common/FindMany.h"
51 #include "canvas/Persistency/Common/FindManyP.h"
52 #include "canvas/Persistency/Common/FindOne.h"
53 #include "canvas/Persistency/Common/FindOneP.h"
54 #include "canvas/Persistency/Common/Ptr.h"
55 #include "canvas/Persistency/Common/PtrVector.h"
56 
60 
61 
62 #include "TH1.h"
63 #include "TH2.h"
64 #include "TVector3.h"
65 
66 // C++ includes
67 #include <map>
68 #include <vector>
69 #include <string>
70 
71 namespace icarus {
72  class CRTTPCMatchingAna;
73 }
74 
75 
76 class icarus::CRTTPCMatchingAna : public art::EDAnalyzer {
77 public:
78  explicit CRTTPCMatchingAna(fhicl::ParameterSet const& p);
79  // The compiler-generated destructor is fine for non-base
80  // classes without bare pointers or other resource use.
81 
82  // Plugins should not be copied or assigned.
83  CRTTPCMatchingAna(CRTTPCMatchingAna const&) = delete;
87 
88  // Called once, at start of the job
89  virtual void beginJob() override;
90 
91  // Called once per event
92  virtual void analyze(const art::Event& event) override;
93 
94  // Called once, at end of the job
95  virtual void endJob() override;
96 
97  // Calculate the distance from the track crossing point to CRT overlap coordinates
98  double DistToCrtHit(TVector3 trackPos, sbn::crt::CRTHit crtHit);
99 
100  void reconfigure(fhicl::ParameterSet const & p);
101  // Required functions.
102  // void analyze(art::Event const& e) override;
103 
104 private:
105 
106  // Declare member data here.
107  art::InputTag fCRTHitLabel; ///< name of CRT producer
108  std::vector<art::InputTag> fTPCTrackLabel; ///< labels for source of tracks
109  std::vector<art::InputTag> fPFParticleLabel; ///< labels for source of PFParticle
110  art::InputTag fTriggerLabel; ///< labels for trigger
111  bool fVerbose; ///< print information about what's going on
112 
114  //CRTEventDisplay evd;
115  geo::GeometryCore const* fGeometryService; ///< pointer to Geometry provider
117  // CRTCommonUtils* fCrtutils;
118 
119  TTree* fTree;
120  int fEvent; ///< number of the event being processed
121  int fRun; ///< number of the run being processed
122  int fSubRun; ///< number of the sub-run being processed
123  vector<int> fCrtRegion; ///< CRT hit region code
124  vector<double> fDCA; ///< Distance of closest approach between CRT hit and extended TPC track
125  vector<double> fDOL; ///<
126  vector<double> fT0; ///< Track T0 based on CRT hit and extended TPC Track match
127  vector<double> fpandorat0; ///< Track T0 based on Pandora (Cathode Crossing Track)
128  vector<int> fcryo; ///< cryo number
129 
130  //add trigger data product vars
131  unsigned int m_gate_type;
132  std::string m_gate_name;
136  uint64_t m_gate_crt_diff;
137 
138  // Histograms
139  std::map<std::string, TH1F*> hDCA;
140  std::map<std::string, TH1F*> hMatchDCA;
141  std::map<std::string, TH1F*> hNoMatchDCA;
142 
143  std::map<std::string, TH1F*> hDoL;
144  std::map<std::string, TH1F*> hMatchDoL;
145  std::map<std::string, TH1F*> hNoMatchDoL;
146 
147  std::map<std::string, TH1F*> hT0;
148  std::map<std::string, TH1F*> hMatchT0;
149  std::map<std::string, TH1F*> hNoMatchT0;
150 
151 };
152 
153 
155  : EDAnalyzer{p} // ,
156  , t0Alg(p.get<fhicl::ParameterSet>("t0Alg"))
157  // , evd(p.get<fhicl::ParameterSet>("evd"))
158  , fCrtutils(new icarus::crt::CRTCommonUtils())
159 
160  // More initializers here.
161 {
162  // Call appropriate consumes<>() for any products to be retrieved by this module.
163  fGeometryService = lar::providerFrom<geo::Geometry>();
164  reconfigure(p);
165 }
166 
167 void icarus::CRTTPCMatchingAna::reconfigure(fhicl::ParameterSet const & p)
168 {
169  fCRTHitLabel = p.get<art::InputTag> ("CRTHitLabel", "crthit");
170  fTPCTrackLabel = p.get< std::vector<art::InputTag> >("TPCTrackLabel", {""});
171  fPFParticleLabel = p.get< std::vector<art::InputTag> >("PFParticleLabel", {""});
172  fTriggerLabel = p.get<art::InputTag>("TriggerLabel","daqTrigger");
173  //fTPCTrackLabel = p.get< std::vector<art::InputTag> >("TPCTrackLabel", std::vector<art::InputTag>() = {""});
174  fVerbose = p.get<bool>("Verbose");
175 } // CRTT0Matching::reconfigure()
176 
178 {
179 
180  // Access tfileservice to handle creating and writing histograms
181  art::ServiceHandle<art::TFileService> tfs;
182 
183  fTree = tfs->make<TTree>("matchTree","CRTHit - TPC track matching analysis");
184 
185  fTree->Branch("Event", &fEvent, "Event/I");
186  fTree->Branch("SubRun", &fSubRun, "SubRun/I");
187  fTree->Branch("Run", &fRun, "Run/I");
188  fTree->Branch("cryo", "std::vector<int>", &fcryo);
189  fTree->Branch("pandorat0", "std::vector<double>", &fpandorat0);
190  fTree->Branch("crtRegion", "std::vector<int>",&fCrtRegion);
191  fTree->Branch("DCA", "std::vector<double>", &fDCA);
192  fTree->Branch("DOL", "std::vector<double>", &fDOL);
193  fTree->Branch("t0", "std::vector<double>", &fT0);
194  fTree->Branch("gate_type", &m_gate_type, "gate_type/b");
195  fTree->Branch("gate_name", &m_gate_name);
196  fTree->Branch("trigger_timestamp", &m_trigger_timestamp, "trigger_timestamp/l");
197  fTree->Branch("gate_start_timestamp", &m_gate_start_timestamp, "gate_start_timestamp/l");
198  fTree->Branch("trigger_gate_diff", &m_trigger_gate_diff, "trigger_gate_diff/l");
199  fTree->Branch("gate_crt_diff", &m_gate_crt_diff, "gate_crt_diff/l");
200 
201 
202  for(int i = 30; i < 50 + 1; i++){
203  std::string tagger = "All";
204  if (i >= 35 && i < 40) continue;
205  if (i==48 || i==49) continue;
206 
207  tagger = fCrtutils->GetRegionNameFromNum(i);//fCrtGeo.GetTagger(i).name;
208  // std::cout << "tagger: " << tagger.c_str() << std::endl;
209  hDCA[tagger] = tfs->make<TH1F>(Form("DCA_%s", tagger.c_str()), "", 50, 0, 100);
210  hDoL[tagger] = tfs->make<TH1F>(Form("DoL_%s", tagger.c_str()), "", 100, 0, 0.25);
211  hT0[tagger] = tfs->make<TH1F>(Form("T0_%s", tagger.c_str()), "", 600, -3000, 3000);
212  }
213 
214 }
215 
216 void icarus::CRTTPCMatchingAna::analyze(const art::Event& event)
217 {
218 
219  fDCA.clear();
220  fDOL.clear();
221  fT0.clear();
222  fCrtRegion.clear();
223  fpandorat0.clear();
224  fcryo.clear();
225  // Fetch basic event info
226  if(fVerbose){
227  std::cout<<"============================================"<<std::endl
228  <<"Run = "<<event.run()<<", SubRun = "<<event.subRun()<<", Event = "<<event.id().event()<<std::endl
229  <<"============================================"<<std::endl;
230  }
231 
232  fEvent = event.id().event();
233  fRun = event.run();
234  fSubRun = event.subRun();
235 
236  //----------------------------------------------------------------------------------------------------------
237  // GETTING PRODUCTS
238  //----------------------------------------------------------------------------------------------------------
239  //add trigger info
240  if( !fTriggerLabel.empty() ) {
241 
242  art::Handle<sbn::ExtraTriggerInfo> trigger_handle;
243  event.getByLabel( fTriggerLabel, trigger_handle );
244  if( trigger_handle.isValid() ) {
245  sbn::triggerSource bit = trigger_handle->sourceType;
246  m_gate_type = (unsigned int)bit;
247  m_gate_name = bitName(bit);
248  m_trigger_timestamp = trigger_handle->triggerTimestamp;
249  m_gate_start_timestamp = trigger_handle->beamGateTimestamp;
250  m_trigger_gate_diff = trigger_handle->triggerTimestamp - trigger_handle->beamGateTimestamp;
251  }
252  else{
253  mf::LogError("CRTTPCMatchingAna:") << "No raw::Trigger associated to label: " << fTriggerLabel.label() << "\n" ;
254  }
255  }
256  else {
257  mf::LogError("CRTTPCMatchingAna:") << "Trigger Data product " << fTriggerLabel.label() << " not found!\n" ;
258  }
259 
260 
261  // Get CRT hits from the event
262  art::Handle< std::vector<sbn::crt::CRTHit>> crtHitHandle;
263  std::vector<art::Ptr<sbn::crt::CRTHit> > crtHitList;
264  if (event.getByLabel(fCRTHitLabel, crtHitHandle))
265  art::fill_ptr_vector(crtHitList, crtHitHandle);
266 
267 
268  // auto tpcTrackHandle = event.getValidHandle<std::vector<recob::Track>>(fTPCTrackLabel);
269  //art::FindManyP<recob::Hit> findManyHits(tpcTrackHandle, event, fTPCTrackLabel);
270 
271  std::vector<sbn::crt::CRTHit> crtHits;
272  int hit_i = 0;
273  double minHitTime = 99999;
274  double maxHitTime = -99999;
275 
276  for(auto const& hit : (*crtHitHandle)){
277  double hitTime = double(hit.ts0_ns - (m_gate_start_timestamp%1'000'000'000))/1e3;
278 //'
279  if(hitTime<-0.5e6) hitTime+=1e6;
280  else if(hitTime>0.5e6) hitTime-=1e6;
281  // double hitTime = double(m_gate_start_timestamp - hit.ts0_ns)/1e3;
282 // hitTime = -hitTime+1e6;
283  //double hitTime = (double)(int)hit.ts0_ns * 1e-3;
284  if(hitTime < minHitTime) minHitTime = hitTime;
285  if(hitTime > maxHitTime) maxHitTime = hitTime;
286 
287  crtHits.push_back(hit);
288  hit_i++;
289  }
290 
291  mf::LogError("CRTTPCMatchingAna:") << "# of TPC tracks: " << fTPCTrackLabel.size()
292  << " \t # of CRT Hits: " << crtHits.size() << "\n" ;
293 
294  //----------------------------------------------------------------------------------------------------------
295  // DISTANCE OF CLOSEST APPROACH ANALYSIS
296  //----------------------------------------------------------------------------------------------------------
297 
298  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(event);
299  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(event, clockData);
300 
301 
302  // if(fVerbose) std::cout<<"----------------- DCA Analysis -------------------"<<std::endl;
303 
304  for(const auto& trackLabel : fTPCTrackLabel)
305  {
306  auto it = &trackLabel - fTPCTrackLabel.data();
307 
308  //std::cout << "size of the track label: " << trackLabel << std::endl;
309  // Get reconstructed tracks from the event
310  auto tpcTrackHandle = event.getValidHandle<std::vector<recob::Track>>(trackLabel);
311  if (!tpcTrackHandle.isValid()) continue;
312 
313  //Get PFParticles
314  auto pfpListHandle = event.getValidHandle<std::vector<recob::PFParticle> >(fPFParticleLabel[it]);
315  if (!pfpListHandle.isValid()) continue;
316 
317  // art::Handle< std::vector<recob::PFParticle> > pfpListHandle;
318  //event.getByLabel({"pandoraGausCryoE", "pandoraGausCryoW"}, pfpListHandle);
319 
320  //Get PFParticle-Track association
321  art::FindManyP<recob::PFParticle> fmpfp(tpcTrackHandle, event, trackLabel);
322 
323  //Get T0-PFParticle association
324  art::FindManyP<anab::T0> fmt0pandora(pfpListHandle, event, fPFParticleLabel[it]);
325 
326 
327  art::FindManyP<recob::Hit> findManyHits(tpcTrackHandle, event, trackLabel);
328  //std::cout << "# track: " << (*tpcTrackHandle).size() << std::endl;
329  // Loop over reconstructed tracks
330  for (auto const& tpcTrack : (*tpcTrackHandle)){
331  auto idx = &tpcTrack - (*tpcTrackHandle).data();
332 
333  double t0 = -9999999;
334  //Find PFParticle for track i
335  //art::Ptr::key() gives the index in the vector
336  auto pfps = fmpfp.at(tpcTrack.ID());
337 
338  if (!pfps.empty()){
339  //Find T0 for PFParticle
340  auto t0s = fmt0pandora.at(pfps[0].key());
341  if (!t0s.empty()){
342 
343  t0 = t0s[0]->Time(); //Get T0
344  }
345  }
346 
347  //std::cout<< "T0: " << t0 << std::endl;
348 
349 
350  //if (idx == 1) break;
351  std::vector<art::Ptr<recob::Hit>> hits = findManyHits.at(tpcTrack.ID());
352  if (hits.size() == 0) continue;
353 
354  //std::cout<< "#hits in a tpc track: " << hits.size() << std::endl;
355 
356  int const cryoNumber = hits[0]->WireID().Cryostat;
357  // matchCand closest = t0Alg.GetClosestCRTHit(detProp, tpcTrack, crtHits, event);
358  // std::vector <matchCand> closestvec = t0Alg.GetClosestCRTHit(detProp, tpcTrack, crtHits, event);
359  //matchCand closest = closestvec[idx];// closestvec.back();
360  // std::cout<< "track index: " << idx << " [ 1st, last, cryoNumber, PandoraT0 ] = [ "
361  // << hits[0]->WireID().TPC << " , " << hits[hits.size()-1]->WireID().TPC
362  // << " , " << cryoNumber << " , " << t0 << " ] "<< std::endl;
363 
364  matchCand closest = t0Alg.GetClosestCRTHit(detProp, tpcTrack, hits, crtHits, m_trigger_timestamp);
365  if(closest.dca >=0 )
366  mf::LogInfo("CRTTPCMatchingAna")
367  << "Track # " << idx <<" Matched time = "<<closest.t0<<" [us] to track "<< tpcTrack.ID()<<" with DCA = "<<closest.dca
368  << " for crt region " << closest.thishit.tagger;
369  /*for (long unsigned int i = 0; i < closestvec.size(); i++){
370 
371  std::cout << "[ #closestvec , closest.dca, closest.extrapLen, closest.thishit.tagger ]= [ "
372  << i <<" , "<< closestvec[i].dca <<" , "<< closestvec[i].extrapLen <<" , "<< closestvec[i].thishit.tagger <<" ]" <<std::endl;
373  }*/
374  double sin_angle = -99999;
375  if(closest.dca != -99999){
376  hDCA[closest.thishit.tagger]->Fill(closest.dca);
377  //hDCA["All"]->Fill(closest.dca);
378  sin_angle = closest.dca/closest.extrapLen;
379  hDoL[closest.thishit.tagger]->Fill(sin_angle);
380  // hDoL["All"]->Fill(sin_angle);
381  hT0[closest.thishit.tagger]->Fill(closest.t0);
382  fDCA.push_back(closest.dca);
383  fDOL.push_back(sin_angle);
384  fT0.push_back(closest.t0);
385  fcryo.push_back(cryoNumber);
386  fpandorat0.push_back(t0);
387  // fCrtRegion.push_back(closest.thishit.tagger);
388  fCrtRegion.push_back(fCrtutils->AuxDetRegionNameToNum(closest.thishit.tagger));
389  } // if closest dca is physical
390  } // track loops in each tracklebel
391  } // trackLabel vector
392 
393 // evd.SetDrawTpc(true);
394  // evd.Draw(event);
395  fTree->Fill();
396 }
398 {
399 
400 } // CRTT0Matching::endJob()
401 
402 
403 DEFINE_ART_MODULE(icarus::CRTTPCMatchingAna)
std::map< std::string, TH1F * > hNoMatchDCA
Functions to help with numbers.
int fEvent
number of the event being processed
Utilities related to art service access.
CRTTPCMatchingAna(fhicl::ParameterSet const &p)
std::vector< art::InputTag > fPFParticleLabel
labels for source of PFParticle
createEngine fT0
Declaration of signal hit object.
pdgs p
Definition: selectors.fcl:22
std::map< std::string, TH1F * > hMatchDoL
int fSubRun
number of the sub-run being processed
std::map< std::string, TH1F * > hMatchT0
int fRun
number of the run being processed
for pfile in ack l reconfigure(.*) override"` do echo "checking $
process_name hit
Definition: cheaterreco.fcl:51
geo::GeometryCore const * fGeometryService
pointer to Geometry provider
std::vector< art::InputTag > fTPCTrackLabel
labels for source of tracks
vector< int > fCrtRegion
CRT hit region code.
std::map< std::string, TH1F * > hT0
std::map< std::string, TH1F * > hNoMatchDoL
std::string bitName(triggerSource bit)
Returns a mnemonic short name of the beam type.
Definition: BeamBits.h:267
vector< double > fDCA
Distance of closest approach between CRT hit and extended TPC track.
vector< double > fpandorat0
Track T0 based on Pandora (Cathode Crossing Track)
virtual void beginJob() override
std::map< std::string, TH1F * > hDoL
virtual void analyze(const art::Event &event) override
vector< int > fcryo
cryo number
std::map< std::string, TH1F * > hMatchDCA
Description of geometry of one entire detector.
Definition of data types for geometry description.
Provides recob::Track data product.
CRTTPCMatchingAna & operator=(CRTTPCMatchingAna const &)=delete
vector< double > fT0
Track T0 based on CRT hit and extended TPC Track match.
icarus::crt::CRTCommonUtils * fCrtutils
bool fVerbose
print information about what&#39;s going on
object containing MC truth information necessary for making RawDigits and doing back tracking ...
triggerSource
Type of beam or beam gate or other trigger source.
Definition: BeamBits.h:97
Data product holding additional trigger information.
void reconfigure(fhicl::ParameterSet const &p)
art::ServiceHandle< art::TFileService > tfs
art::InputTag fCRTHitLabel
name of CRT producer
std::map< std::string, TH1F * > hNoMatchT0
std::string tagger
Name of the CRT wall (in the form of strings).
Definition: CRTHit.hh:45
std::map< std::string, TH1F * > hDCA
BEGIN_PROLOG could also be cout
auto const detProp
double DistToCrtHit(TVector3 trackPos, sbn::crt::CRTHit crtHit)
art::InputTag fTriggerLabel
labels for trigger