All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
icaruscode/icaruscode/CRT/CRTT0Matching_module.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 /// Class: CRTT0Matching
3 /// Module Type: producer
4 /// File: CRTT0Matching_module.cc
5 ///
6 /// Author: Thomas Brooks
7 /// E-mail address: tbrooks@fnal.gov
8 ///
9 /// Modified from CRTT0Matching by Thomas Warburton.
10 /////////////////////////////////////////////////////////////////////////////
11 
12 // sbndcode includes
15 
16 // Framework includes
17 #include "art/Framework/Core/EDProducer.h"
18 #include "art/Framework/Core/ModuleMacros.h"
19 #include "canvas/Persistency/Common/FindManyP.h"
20 #include "art/Framework/Principal/Handle.h"
21 #include "art/Framework/Principal/Event.h"
22 #include "canvas/Persistency/Common/Ptr.h"
23 #include "canvas/Persistency/Common/PtrVector.h"
24 #include "art/Framework/Principal/Run.h"
25 #include "art/Framework/Principal/SubRun.h"
26 #include "art_root_io/TFileService.h"
27 #include "art_root_io/TFileDirectory.h"
28 
29 #include "canvas/Utilities/InputTag.h"
30 #include "fhiclcpp/ParameterSet.h"
31 #include "messagefacility/MessageLogger/MessageLogger.h"
32 
33 #include <memory>
34 #include <iostream>
35 #include <map>
36 #include <iterator>
37 #include <algorithm>
38 
39 // LArSoft
47 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
59 // ROOT
60 #include "TVector3.h"
61 #include "TH1.h"
62 #include "TH2.h"
63 #include "TVector3.h"
64 #include "TTree.h"
65 
66 namespace icarus {
67 
68  class CRTT0Matching : public art::EDProducer {
69  public:
70 
71  explicit CRTT0Matching(fhicl::ParameterSet const & p);
72 
73  // The destructor generated by the compiler is fine for classes
74  // without bare pointers or other resource use.
75 
76  // Plugins should not be copied or assigned.
77  CRTT0Matching(CRTT0Matching const &) = delete;
78  CRTT0Matching(CRTT0Matching &&) = delete;
79  CRTT0Matching & operator = (CRTT0Matching const &) = delete;
81 
82  // Required functions.
83  void produce(art::Event & e) override;
84 
85  // Selected optional functions.
86  void beginJob() override;
87 
88  void endJob() override;
89 
90  void reconfigure(fhicl::ParameterSet const & p);
91 
92  private:
93 
94  // Params got from fcl file.......
95  // art::InputTag fTpcTrackModuleLabel; ///< name of track producer
96  std::vector<art::InputTag> fTpcTrackModuleLabel; ///< name of track producer
97  std::vector<art::InputTag> fPFParticleLabel; ///< labels for source of PFParticle
98  art::InputTag fCrtHitModuleLabel; ///< name of crt producer
99  art::InputTag fTriggerLabel; ///< labels for trigger
101 
102 
103  geo::GeometryCore const* fGeometryService; ///< pointer to Geometry provider
105  // CRTCommonUtils* fCrtutils;
106 
107  TTree* fTree;
108  int fEvent; ///< number of the event being processed
109  int fRun; ///< number of the run being processed
110  int fSubRun; ///< number of the sub-run being processed
111  vector<int> fCrtRegion; //CRT hit region code
112  vector<double> fDCA; //CRT hit region code
113  vector<double> fDOL; //CRT hit region code
114  vector<double> fT0; //CRT hit region code
115  vector<double> ftpcx; // x-position from tpc
116  vector<double> ftpcy; // y-position from tpc
117  vector<double> ftpcz; // z-position from tpc
118  vector<double> fcrtx; // x-position from crt
119  vector<double> fcrty; // y-position from crt
120  vector<double> fcrtz; // z-position from crt
121  vector<double> fpandorat0; ///< Track T0 based on Pandora (Cathode Crossing Track)
122  vector<int> fcryo; ///< cryo number
123  vector<int> fntracks; ///< total number of tracks
124 
125  //add trigger data product vars
126  unsigned int m_gate_type;
127  std::string m_gate_name;
131  uint64_t m_gate_crt_diff;
132 
133 
134  // Histograms
135  std::map<std::string, TH1F*> hDCA;
136  std::map<std::string, TH1F*> hMatchDCA;
137  std::map<std::string, TH1F*> hNoMatchDCA;
138 
139  std::map<std::string, TH1F*> hDoL;
140  std::map<std::string, TH1F*> hMatchDoL;
141  std::map<std::string, TH1F*> hNoMatchDoL;
142 
143  std::map<std::string, TH1F*> hT0;
144  std::map<std::string, TH1F*> hMatchT0;
145  std::map<std::string, TH1F*> hNoMatchT0;
146  }; // class CRTT0Matching
147 
148 
149  CRTT0Matching::CRTT0Matching(fhicl::ParameterSet const & p)
150  : EDProducer(p), t0Alg(p.get<fhicl::ParameterSet>("T0Alg"))
151  , fCrtutils(new icarus::crt::CRTCommonUtils())
152  // Initialize member data here, if know don't want to reconfigure on the fly
153  {
154 
155  // Call appropriate produces<>() functions here.
156  produces< std::vector<anab::T0> >();
157  produces< art::Assns<recob::Track , anab::T0> >();
158  produces< art::Assns<sbn::crt::CRTHit, anab::T0> >();
159 
160  fGeometryService = lar::providerFrom<geo::Geometry>();
161  reconfigure(p);
162 
163  } // CRTT0Matching()
164 
165 
166  void CRTT0Matching::reconfigure(fhicl::ParameterSet const & p)
167  {
168  fTpcTrackModuleLabel = p.get< std::vector<art::InputTag>>("TpcTrackModuleLabel", {"pandoraTrackGausCryoE"});
169  fCrtHitModuleLabel = p.get<art::InputTag> ("CrtHitModuleLabel", "crthit");
170  fPFParticleLabel = p.get< std::vector<art::InputTag> >("PFParticleLabel", {""});
171  fTriggerLabel = p.get<art::InputTag>("TriggerLabel","daqTrigger");
172  } // CRTT0Matching::reconfigure()
173 
174 
176  {
177  // Access tfileservice to handle creating and writing histograms
178  art::ServiceHandle<art::TFileService> tfs;
179 
180  fTree = tfs->make<TTree>("matchTree","CRTHit - TPC track matching analysis");
181 
182  fTree->Branch("Event", &fEvent, "Event/I");
183  fTree->Branch("SubRun", &fSubRun, "SubRun/I");
184  fTree->Branch("Run", &fRun, "Run/I");
185  fTree->Branch("crtRegion", "std::vector<int>", &fCrtRegion);
186  fTree->Branch("DCA", "std::vector<double>", &fDCA);
187  fTree->Branch("DOL", "std::vector<double>", &fDOL);
188  fTree->Branch("t0", "std::vector<double>", &fT0);
189  fTree->Branch("tpcx", "std::vector<double>", &ftpcx);
190  fTree->Branch("tpcy", "std::vector<double>", &ftpcy);
191  fTree->Branch("tpcz", "std::vector<double>", &ftpcz);
192  fTree->Branch("crtx", "std::vector<double>", &fcrtx);
193  fTree->Branch("crty", "std::vector<double>", &fcrty);
194  fTree->Branch("crtz", "std::vector<double>", &fcrtz);
195  fTree->Branch("cryo", "std::vector<int>", &fcryo);
196  fTree->Branch("ntracks", "std::vector<int>", &fntracks);
197  fTree->Branch("pandorat0", "std::vector<double>", &fpandorat0);
198  fTree->Branch("gate_type", &m_gate_type, "gate_type/b");
199  fTree->Branch("gate_name", &m_gate_name);
200  fTree->Branch("trigger_timestamp", &m_trigger_timestamp, "trigger_timestamp/l");
201  fTree->Branch("gate_start_timestamp", &m_gate_start_timestamp, "gate_start_timestamp/l");
202  fTree->Branch("trigger_gate_diff", &m_trigger_gate_diff, "trigger_gate_diff/l");
203  fTree->Branch("gate_crt_diff",&m_gate_crt_diff, "gate_crt_diff/l");
204 
205  for(int i = 30; i < 50 + 1; i++){
206  std::string tagger = "All";
207  if (i >= 35 && i < 40) continue;
208  if (i==48 || i==49) continue;
209  // if(i < ){
210  tagger = fCrtutils->GetRegionNameFromNum(i);//fCrtGeo.GetTagger(i).name;
211  // std::cout << "tagger: " << tagger.c_str() << std::endl;
212  hDCA[tagger] = tfs->make<TH1F>(Form("DCA_%s", tagger.c_str()), "", 50, 0, 100);
213  hDoL[tagger] = tfs->make<TH1F>(Form("DoL_%s", tagger.c_str()), "", 100, 0, 0.25);
214  hT0[tagger] = tfs->make<TH1F>(Form("T0_%s", tagger.c_str()), "", 600, -3000, 3000);
215  }
216 
217  } // CRTT0Matching::beginJob()
218 
219  void CRTT0Matching::produce(art::Event & event)
220  {
221  fDCA.clear();
222  fDOL.clear();
223  fT0.clear();
224  fcrtx.clear();
225  fcrty.clear();
226  fcrtz.clear();
227  ftpcx.clear();
228  ftpcy.clear();
229  ftpcz.clear();
230  fCrtRegion.clear();
231  fpandorat0.clear();
232  fcryo.clear();
233  fntracks.clear();
234  fEvent = event.id().event();
235  fRun = event.run();
236  fSubRun = event.subRun();
237 
238  // Create anab::T0 objects and make association with recob::Track
239  std::unique_ptr< std::vector<anab::T0> > T0col( new std::vector<anab::T0>);
240  std::unique_ptr< art::Assns<recob::Track, anab::T0> > Trackassn( new art::Assns<recob::Track, anab::T0>);
241  std::unique_ptr< art::Assns <sbn::crt::CRTHit, anab::T0> > t0_crthit_assn( new art::Assns<sbn::crt::CRTHit, anab::T0> );
242 
243  //add trigger info
244  if( !fTriggerLabel.empty() ) {
245 
246  art::Handle<sbn::ExtraTriggerInfo> trigger_handle;
247  event.getByLabel( fTriggerLabel, trigger_handle );
248  if( trigger_handle.isValid() ) {
249  sbn::triggerSource bit = trigger_handle->sourceType;
250  m_gate_type = (unsigned int)bit;
251  m_gate_name = bitName(bit);
252  m_trigger_timestamp = trigger_handle->triggerTimestamp;
253  m_gate_start_timestamp = trigger_handle->beamGateTimestamp;
254  m_trigger_gate_diff = trigger_handle->triggerTimestamp - trigger_handle->beamGateTimestamp;
255 
256  }
257  else{
258  mf::LogError("CRTT0Matching:") << "No raw::Trigger associated to label: " << fTriggerLabel.label() << "\n" ;
259  }
260  }
261  else {
262  mf::LogError("CRTT0Matching:") << "Trigger Data product " << fTriggerLabel.label() << " not found!\n" ;
263  }
264 
265  // Retrieve CRT hit list
266  art::Handle<std::vector<sbn::crt::CRTHit>> crtListHandle;
267  std::vector<art::Ptr<sbn::crt::CRTHit>> crtList;
268  if(event.getByLabel(fCrtHitModuleLabel, crtListHandle))
269  art::fill_ptr_vector(crtList, crtListHandle);
270 
271  std::vector<sbn::crt::CRTHit> crtHits;
272  for (auto const& crtHit : crtList){
273  crtHits.push_back(*crtHit);
274  }
275 
276  // Retrieve track list
277  for(const auto& trackLabel : fTpcTrackModuleLabel){
278 
279  auto it = &trackLabel - fTpcTrackModuleLabel.data();
280 
281  art::Handle< std::vector<recob::Track> > trackListHandle;
282  std::vector<art::Ptr<recob::Track> > trackList;
283  if (event.getByLabel(trackLabel,trackListHandle))
284  art::fill_ptr_vector(trackList, trackListHandle);
285 
286  // if (event.getByLabel(fTpcTrackModuleLabel,trackListHandle))
287  // std::cout << "crtlabel: " << fCrtHitModuleLabel << " , Tpctrklabel: " << fTpcTrackModuleLabel << std::endl;
288 
289  mf::LogInfo("CRTT0Matching")
290  <<"Number of reconstructed tracks = "<<trackList.size()<<"\n"
291  <<"Number of CRT hits = "<<crtList.size();
292 
293  fntracks.push_back(trackList.size());
294  //Get PFParticles
295  auto pfpListHandle = event.getValidHandle<std::vector<recob::PFParticle> >(fPFParticleLabel[it]);
296  if (!pfpListHandle.isValid()) continue;
297 
298  //Get PFParticle-Track association
299  art::FindManyP<recob::PFParticle> fmpfp(trackListHandle, event, trackLabel);
300 
301  //Get T0-PFParticle association
302  art::FindManyP<anab::T0> fmt0pandora(pfpListHandle, event, fPFParticleLabel[it]);
303 
304 
305  if (trackListHandle.isValid() && crtListHandle.isValid() ){
306 
307  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(event);
308  art::FindManyP<recob::Hit> findManyHits(trackListHandle, event, trackLabel);
309 
310  // Loop over all the reconstructed tracks
311  for(size_t track_i = 0; track_i < trackList.size(); track_i++) {
312 
313  double t0 = -99999999;
314  //Find PFParticle for track i
315  //art::Ptr::key() gives the index in the vector
316  auto pfps = fmpfp.at(trackList[track_i]->ID());
317 
318  if (!pfps.empty()){
319  //Find T0 for PFParticle
320  auto t0s = fmt0pandora.at(pfps[0].key());
321  if (!t0s.empty()){
322 
323  t0 = t0s[0]->Time(); //Get T0
324  }
325  }
326 
327  std::vector<art::Ptr<recob::Hit>> hits = findManyHits.at(trackList[track_i]->ID());
328  if (hits.size() == 0) continue;
329  int const cryoNumber = hits[0]->WireID().Cryostat;
330  // std::pair<double, double> matchedTime = t0Alg.T0AndDCAFromCRTHits(detProp, *trackList[track_i], crtHits, event);
331  matchCand closest = t0Alg.GetClosestCRTHit(detProp, *trackList[track_i], hits, crtHits, m_gate_start_timestamp);
332  // std::vector <matchCand> closestvec = t0Alg.GetClosestCRTHit(detProp, *trackList[track_i], crtHits, event);
333  // matchCand closest = closestvec.back();
334 
335  if(closest.dca >=0 ){
336  mf::LogInfo("CRTT0Matching")
337  <<"Matched time = "<<closest.t0<<" [us] to track "<<trackList[track_i]->ID()<<" with DCA = "<<closest.dca;
338  T0col->push_back(anab::T0(closest.t0*1e3, trackList[track_i]->ID(), closest.thishit.plane, (int)closest.extrapLen, closest.dca));
339  util::CreateAssn(*this, event, *T0col, trackList[track_i], *Trackassn);
340 
341  //std::cout << "---------------------- line #156 " << std::endl;
342  double sin_angle = -99999;
343  if(closest.dca != -99999){
344  auto start = trackList[track_i]->Vertex<TVector3>();
345  //auto end = trackList[track_i]->End<TVector3>();
346  hDCA[closest.thishit.tagger]->Fill(closest.dca);
347  //hDCA["All"]->Fill(closest.dca);
348  sin_angle = closest.dca/closest.extrapLen;
349  hDoL[closest.thishit.tagger]->Fill(sin_angle);
350  // hDoL["All"]->Fill(sin_angle);
351  hT0[closest.thishit.tagger]->Fill(closest.t0);
352  fDCA.push_back(closest.dca);
353  fDOL.push_back(sin_angle);
354  fT0.push_back(closest.t0);
355  fcryo.push_back(cryoNumber);
356  fpandorat0.push_back(t0);
357  ftpcx.push_back(start.X());
358  ftpcy.push_back(start.Y());
359  ftpcz.push_back(start.Z());
360  fcrtx.push_back(closest.thishit.x_pos);
361  fcrty.push_back(closest.thishit.y_pos);
362  fcrtz.push_back(closest.thishit.z_pos);
363  // fCrtRegion.push_back(closest.thishit.tagger);
365  }
366  //find this CRThit in the collection
367  // note this does not work (both the loop and the assoc !!)
368  unsigned CRThitIndex = std::numeric_limits<unsigned>::max();
369  for (int ic=0; ic<(int)crtList.size(); ++ic){
370  if (crtList[ic]->ts0_ns==closest.thishit.ts0_ns && crtList[ic]->z_pos==closest.thishit.z_pos && crtList[ic]->peshit==closest.thishit.peshit)
371  CRThitIndex=ic;
372  // std::cout << "CRThitIndex: \t" << CRThitIndex << std::endl;
373  }
374  if (CRThitIndex != std::numeric_limits<unsigned>::max()){
375  // std::cout <<"CRThitIndex: " << CRThitIndex << " passed......: \t" << std::endl;
376  util::CreateAssn(*this, event, *T0col, crtList[CRThitIndex], *t0_crthit_assn);
377  }
378  } // DCA check
379 
380  } // Loop over tracks
381 
382  } // Validity check
383 
384  } // all track labels in a vector
385  event.put(std::move(T0col));
386  event.put(std::move(Trackassn));
387  event.put(std::move(t0_crthit_assn));
388  fTree->Fill();
389  } // CRTT0Matching::produce()
390 
391 
393  {
394 
395  } // CRTT0Matching::endJob()
396 
397 
398  DEFINE_ART_MODULE(CRTT0Matching)
399 
400 } // sbnd namespace
401 
402 namespace {
403 
404 }
vector< double > fpandorat0
Track T0 based on Pandora (Cathode Crossing Track)
Utilities related to art service access.
int fEvent
number of the event being processed
std::vector< art::InputTag > fTpcTrackModuleLabel
name of track producer
int plane
Name of the CRT wall (in the form of numbers).
Definition: CRTHit.hh:36
float peshit
Total photo-electron (PE) in a crt hit.
Definition: CRTHit.hh:27
Declaration of signal hit object.
pdgs p
Definition: selectors.fcl:22
art::InputTag fCrtHitModuleLabel
name of crt producer
Definition: T0.h:16
int fRun
number of the run being processed
CRTT0Matching & operator=(CRTT0Matching const &)=delete
std::string bitName(triggerSource bit)
Returns a mnemonic short name of the beam type.
Definition: BeamBits.h:267
std::vector< art::InputTag > fPFParticleLabel
labels for source of PFParticle
float z_pos
position in z-direction (cm).
Definition: CRTHit.hh:42
double ts0_ns
Timestamp T0 (from White Rabbit), in UTC absolute time scale in nanoseconds from the Epoch...
Definition: CRTHit.hh:32
Access the description of detector geometry.
Description of geometry of one entire detector.
vector< int > fntracks
total number of tracks
Provides recob::Track data product.
Encapsulate the geometry of a wire.
bool CreateAssn(art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t index=UINT_MAX)
Creates a single one-to-one association.
float y_pos
position in y-direction (cm).
Definition: CRTHit.hh:40
Encapsulate the construction of a single detector plane.
matchCand GetClosestCRTHit(detinfo::DetectorPropertiesData const &detProp, recob::Track tpcTrack, std::pair< double, double > t0MinMax, std::vector< sbn::crt::CRTHit > crtHits, int driftDirection, uint64_t &trigger_timestamp)
float x_pos
position in x-direction (cm).
Definition: CRTHit.hh:38
geo::GeometryCore const * fGeometryService
pointer to Geometry provider
do i e
void reconfigure(fhicl::ParameterSet const &p)
triggerSource
Type of beam or beam gate or other trigger source.
Definition: BeamBits.h:97
Data product holding additional trigger information.
art::ServiceHandle< art::TFileService > tfs
Collection of Physical constants used in LArSoft.
process_name crt
std::string tagger
Name of the CRT wall (in the form of strings).
Definition: CRTHit.hh:45
art framework interface to geometry description
auto const detProp
int fSubRun
number of the sub-run being processed