LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
MCTruthT0Matching_module.cc
Go to the documentation of this file.
1 
42 // Framework includes
54 
56 #include "fhiclcpp/ParameterSet.h"
58 
59 #include <memory>
60 #include <iostream>
61 #include <map>
62 #include <iterator>
63 
64 // LArSoft
86 
87 // ROOT
88 #include "TTree.h"
89 #include "TFile.h"
90 
91 namespace t0 {
92  class MCTruthT0Matching;
93 }
94 
96 public:
97  explicit MCTruthT0Matching(fhicl::ParameterSet const & p);
98  // The destructor generated by the compiler is fine for classes
99  // without bare pointers or other resource use.
100 
101  // Plugins should not be copied or assigned.
102  MCTruthT0Matching(MCTruthT0Matching const &) = delete;
104  MCTruthT0Matching & operator = (MCTruthT0Matching const &) = delete;
106 
107  // Required functions.
108  void produce(art::Event & e) override;
109 
110  // Selected optional functions.
111  void beginJob() override;
112  void reconfigure(fhicl::ParameterSet const & p) ;
113 
114 
115 private:
116 
117  // Params got from fcl file
123 
126 
127  // Variable in TFS branches
128  TTree* fTree;
129  int TrackID = 0;
130  int TrueTrackID = 0;
132  double TrueTrackT0 = 0;
133 
134  int ShowerID = 0;
135  int ShowerMatchID = 0;
137  double ShowerT0 = 0;
138 };
139 
140 
142 // :
143 // Initialize member data here, if know don't want to reconfigure on the fly
144 {
145  // Call appropriate produces<>() functions here.
146  reconfigure(p);
147  if (fMakeT0Assns){ // T0 assns are deprecated - this allows one to use deprecated funcionality. Added 2017-08-15. Should not be kept around forever
148  std::cout << "WARNING - You are using deprecated functionality\n";
149  std::cout << "MCTruthT0Matching T0 assns will be removed soon\n";
150  std::cout << "set your fcl parameter makeT0Assns to false and use MCParticle direct associations instead" << std::endl;
151  produces< std::vector<anab::T0> >();
152  produces< art::Assns<recob::Track , anab::T0> >();
153  produces< art::Assns<recob::Shower, anab::T0> > ();
154  if (fMakePFParticleAssns){produces< art::Assns<recob::PFParticle, anab::T0> > ();}// only do PFParticles if desired by the user
155  }
156 
157  produces< art::Assns<recob::Track , simb::MCParticle, anab::BackTrackerMatchingData > > ();
158  produces< art::Assns<recob::Shower, simb::MCParticle, anab::BackTrackerMatchingData > > ();
159  if (fMakePFParticleAssns){ // only do PFParticles if desired by the user
160  produces< art::Assns<recob::PFParticle, simb::MCParticle, anab::BackTrackerMatchingData > > ();
161  }
162 
163  if(fMakeHitAssns)
164  produces< art::Assns<recob::Hit , simb::MCParticle, anab::BackTrackerHitMatchingData > > ();
165 }
166 
168 {
169  // Implementation of optional member function here.
170  fTrackModuleLabel = (p.get< art::InputTag > ("TrackModuleLabel" ) );
171  fShowerModuleLabel = (p.get< art::InputTag > ("ShowerModuleLabel") );
172  fPFParticleModuleLabel = (p.get< art::InputTag > ("PFParticleModuleLabel", "pandoraNu") );
173  fMakeT0Assns = (p.get< bool > ("makeT0Assns", true) );
174  fMakePFParticleAssns = (p.get< bool > ("makePFParticleAssns", false) );
175 
176  fMakeHitAssns = p.get<bool>("makeHitAssns",true);
177  if(fMakeHitAssns) fHitModuleLabel = p.get<art::InputTag>("HitModuleLabel");
178 }
179 
181 {
182  // Implementation of optional member function here.
184  fTree = tfs->make<TTree>("MCTruthT0Matching","MCTruthT0");
185  fTree->Branch("TrueTrackT0",&TrueTrackT0,"TrueTrackT0/D");
186  fTree->Branch("TrueTrackID",&TrueTrackID,"TrueTrackID/D");
187  //fTree->Branch("ShowerID" ,&ShowerID ,"ShowerID/I" );
188  //fTree->Branch("ShowerT0" ,&ShowerT0 ,"ShowerT0/D" );
189 }
190 
192 {
193  if (evt.isRealData()) return;
194 
195  // Access art services...
199 
200  //TrackList handle
201  art::Handle< std::vector<recob::Track> > trackListHandle;
202  std::vector<art::Ptr<recob::Track> > tracklist;
203  if (evt.getByLabel(fTrackModuleLabel,trackListHandle))
204  art::fill_ptr_vector(tracklist, trackListHandle);
205  //if (!trackListHandle.isValid()) trackListHandle.clear();
206 
207  //ShowerList handle
208  art::Handle< std::vector<recob::Shower> > showerListHandle;
209  std::vector<art::Ptr<recob::Shower> > showerlist;
210  if (evt.getByLabel(fShowerModuleLabel,showerListHandle))
211  art::fill_ptr_vector(showerlist, showerListHandle);
212  //if (!showerListHandle.isValid()) showerListHandle.clear();
213 
214  //PFParticleList handle
215  art::Handle< std::vector<recob::PFParticle> > pfparticleListHandle;
216  std::vector<art::Ptr<recob::PFParticle> > pfparticlelist;
217  if (evt.getByLabel(fPFParticleModuleLabel,pfparticleListHandle))
218  art::fill_ptr_vector(pfparticlelist, pfparticleListHandle);
219  //if (!pfparticleListHandle.isValid()) pfparticleListHandle.clear();
220 
221 
222  auto mcpartHandle = evt.getValidHandle< std::vector<simb::MCParticle> >("largeant");
223 // simb::MCParticle const *firstParticle = &mcpartHandle->front();
224  // Create anab::T0 objects and make association with recob::Track, recob::Shower, and recob::PFParticle objects
225  std::unique_ptr< std::vector<anab::T0> > T0col( new std::vector<anab::T0>);
226 // if (fMakeT0Assns){ // T0 assns are deprecated - this allows one to use deprecated funcionality. Added 2017-08-15. Should not be kept around forever
227  std::unique_ptr< art::Assns<recob::Track, anab::T0> > Trackassn( new art::Assns<recob::Track, anab::T0>);
228  std::unique_ptr< art::Assns<recob::Shower, anab::T0> > Showerassn( new art::Assns<recob::Shower, anab::T0>);
229 // if (fMakePFParticleAssns){
230  std::unique_ptr< art::Assns<recob::PFParticle, anab::T0> > PFParticleassn( new art::Assns<recob::PFParticle, anab::T0>);
231 // }
232 // }
233 
234  // Create associations directly between MCParticle and either recob::Track, recob::Shower, or recob::PFParticle
235  // These associations contain metadata summarising the quality of the match
236  std::unique_ptr< art::Assns<recob::Track, simb::MCParticle, anab::BackTrackerMatchingData > > MCPartTrackassn( new art::Assns<recob::Track, simb::MCParticle, anab::BackTrackerMatchingData >);
237  std::unique_ptr< art::Assns<recob::Shower, simb::MCParticle, anab::BackTrackerMatchingData> > MCPartShowerassn( new art::Assns<recob::Shower, simb::MCParticle, anab::BackTrackerMatchingData>);
238 // if (fMakePFParticleAssns){
239  std::unique_ptr< art::Assns<recob::PFParticle, simb::MCParticle,anab::BackTrackerMatchingData> > MCPartPFParticleassn( new art::Assns<recob::PFParticle, simb::MCParticle, anab::BackTrackerMatchingData>);
240 // }
241  // Association block for the hits<-->MCParticles
242  std::unique_ptr< art::Assns<recob::Hit, simb::MCParticle, anab::BackTrackerHitMatchingData > > MCPartHitassn( new art::Assns<recob::Hit, simb::MCParticle, anab::BackTrackerHitMatchingData >);
243 
244 
245  double maxe = -1;
246  double tote = 0;
247  // int trkid = -1;
248  int maxtrkid = -1;
249  double maxn = -1;
250  double totn = 0;
251  int maxntrkid = -1;
253 
254  std::unordered_map<int,int> trkid_lookup; //indexed by geant4trkid, delivers MC particle location
255 
256  //if we want to make per-hit assns
257  if(fMakeHitAssns){
258 
259 
260 
261  art::Handle< std::vector<recob::Hit> > hitListHandle;
262  evt.getByLabel(fHitModuleLabel,hitListHandle);
263 
264  if(hitListHandle.isValid()){
265 
266  auto const& hitList(*hitListHandle);
267  auto const& mcpartList(*mcpartHandle);
268  for(size_t i_h=0; i_h<hitList.size(); ++i_h){
269  art::Ptr<recob::Hit> hitPtr(hitListHandle, i_h);
270  auto trkide_list = bt_serv->HitToTrackIDEs(hitPtr);
271  struct TrackIDEinfo {
272  float E;
273  float NumElectrons;
274  };
275  std::map<int, TrackIDEinfo> trkide_collector;
276  maxe = -1; tote = 0; maxtrkid = -1;
277  maxn = -1; totn = 0; maxntrkid = -1;
278  for(auto const& t : trkide_list){
279  trkide_collector[t.trackID].E += t.energy;
280  tote += t.energy;
281  if(trkide_collector[t.trackID].E>maxe) { maxe = trkide_collector[t.trackID].E; maxtrkid = t.trackID; }
282  trkide_collector[t.trackID].NumElectrons += t.numElectrons;
283  totn += t.numElectrons;
284  if(trkide_collector[t.trackID].NumElectrons > maxn) {
285  maxn = trkide_collector[t.trackID].NumElectrons;
286  maxntrkid = t.trackID;
287  }
288 
289  //if not found, find mc particle...
290  if(trkid_lookup.find(t.trackID)==trkid_lookup.end()){
291  size_t i_p=0;
292  while(i_p<mcpartList.size()){
293  if(mcpartList[i_p].TrackId() == t.trackID) { trkid_lookup[t.trackID] = (int)i_p; break;}
294  ++i_p;
295  }
296  if(i_p==mcpartList.size()) trkid_lookup[t.trackID] = -1;
297  }
298 
299  }//end loop on TrackIDs
300 
301  //now find the mcparticle and loop back through ...
302  for(auto const& t : trkide_collector){
303  int mcpart_i = trkid_lookup[t.first];
304  if(mcpart_i==-1) continue; //no mcparticle here
305  art::Ptr<simb::MCParticle> mcpartPtr(mcpartHandle, mcpart_i);
306  bthmd.ideFraction = t.second.E / tote;
307  bthmd.isMaxIDE = (t.first==maxtrkid);
308  bthmd.ideNFraction = t.second.NumElectrons / totn;
309  bthmd.isMaxIDEN = ( t.first == maxntrkid );
310  MCPartHitassn->addSingle(hitPtr, mcpartPtr, bthmd);
311  }
312 
313 
314  }//end loop on hits
315 
316  }//end if handle valid
317 
318  }//end if make hit/mcpart assns
319 
320 
321  if (trackListHandle.isValid()){
322  //Access tracks and hits
323  art::FindManyP<recob::Hit> fmtht(trackListHandle, evt, fTrackModuleLabel);
324 
325  size_t NTracks = tracklist.size();
326 
327  // Now to access MCTruth for each track...
328  for(size_t iTrk=0; iTrk < NTracks; ++iTrk) {
329  TrueTrackT0 = 0;
330  TrackID = 0;
331  TrueTrackID = 0;
333  std::vector< art::Ptr<recob::Hit> > allHits = fmtht.at(iTrk);
334 
335  std::map<int,double> trkide;
336  for(size_t h = 0; h < allHits.size(); ++h){
337  art::Ptr<recob::Hit> hit = allHits[h];
338  std::vector<sim::IDE> ides;
339  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToTrackIDEs(hit);
340 
341  for(size_t e = 0; e < TrackIDs.size(); ++e){
342  trkide[TrackIDs[e].trackID] += TrackIDs[e].energy;
343  }
344 
345  }
346  // Work out which IDE despoited the most charge in the hit if there was more than one.
347  maxe = -1;
348  tote = 0;
349  for (std::map<int,double>::iterator ii = trkide.begin(); ii!=trkide.end(); ++ii){
350  tote += ii->second;
351  if ((ii->second)>maxe){
352  maxe = ii->second;
353  TrackID = ii->first;
354  }
355  }
356  btdata.cleanliness = maxe/tote;
357 
358  // Now have trackID, so get PdG code and T0 etc.
359  const simb::MCParticle *tmpParticle = pi_serv->TrackIdToParticle_P(TrackID);
360  if (!tmpParticle) continue; // Retain this check that the BackTracker can find the right particle
361  // Now, loop through the MCParticle's myself to find the correct match
362  int mcpart_i(-1);
363  for (auto const particle : *mcpartHandle){
364  mcpart_i++;
365  if (TrackID == particle.TrackId()){
366  break;
367  }
368  }
369  const simb::MCParticle particle = mcpartHandle.product()->at(mcpart_i);
370  TrueTrackT0 = particle.T();
371  TrueTrackID = particle.TrackId();
372  TrueTriggerType = 2; // Using MCTruth as trigger, so tigger type is 2.
373  //std::cout << "Got particle, PDG = " << particle.PdgCode() << std::endl;
374 
375  //std::cout << "Filling T0col with " << TrueTrackT0 << " " << TrueTriggerType << " " << TrueTrackID << " " << (*T0col).size() << std::endl;
376 
377  T0col->push_back(anab::T0(TrueTrackT0,
379  TrueTrackID,
380  (*T0col).size()
381  ));
382  //auto diff = particle - firstParticle;
383  auto diff = mcpart_i; // check I have a sensible value for this counter
384  if (diff >= (int)mcpartHandle->size()){
385  std::cout << "Error, the backtracker is doing weird things to your pointers!" << std::endl;
386  throw std::exception();
387  }
388 
389  art::Ptr<simb::MCParticle> mcpartPtr(mcpartHandle, mcpart_i);
390  MCPartTrackassn->addSingle(tracklist[iTrk], mcpartPtr, btdata);
391  if (fMakeT0Assns){
392  util::CreateAssn(*this, evt, *T0col, tracklist[iTrk], *Trackassn);
393  }
394  fTree -> Fill();
395 
396  } // Loop over tracks
397  }
398 
399  if (showerListHandle.isValid()){
400  art::FindManyP<recob::Hit> fmsht(showerListHandle,evt, fShowerModuleLabel);
401  // Now Loop over showers....
402  size_t NShowers = showerlist.size();
403  for (size_t Shower = 0; Shower < NShowers; ++Shower) {
404  ShowerMatchID = 0;
405  ShowerID = 0;
406  ShowerT0 = 0;
407  std::vector< art::Ptr<recob::Hit> > allHits = fmsht.at(Shower);
409 
410  std::map<int,double> showeride;
411  for(size_t h = 0; h < allHits.size(); ++h){
412  art::Ptr<recob::Hit> hit = allHits[h];
413  std::vector<sim::IDE> ides;
414  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToTrackIDEs(hit);
415 
416  for(size_t e = 0; e < TrackIDs.size(); ++e){
417  showeride[TrackIDs[e].trackID] += TrackIDs[e].energy;
418  }
419  }
420  // Work out which IDE despoited the most charge in the hit if there was more than one.
421  maxe = -1;
422  tote = 0;
423  for (std::map<int,double>::iterator ii = showeride.begin(); ii!=showeride.end(); ++ii){
424  tote += ii->second;
425  if ((ii->second)>maxe){
426  maxe = ii->second;
427  ShowerID = ii->first;
428  }
429  }
430  btdata.cleanliness = maxe/tote;
431 
432 
433 
434 
435  // Now have MCParticle trackID corresponding to shower, so get PdG code and T0 etc.
436  const simb::MCParticle *tmpParticle = pi_serv->TrackIdToParticle_P(ShowerID);
437  if (!tmpParticle) continue; // Retain this check that the BackTracker can find the right particle
438  // Now, loop through the MCParticle's myself to find the correct match
439  int mcpart_i(-1);
440  for (auto const particle : *mcpartHandle){
441  mcpart_i++;
442  if (ShowerID == particle.TrackId()){
443  break;
444  }
445  }
446  const simb::MCParticle particle = mcpartHandle.product()->at(mcpart_i);
447  ShowerT0 = particle.T();
448  ShowerID = particle.TrackId();
449  ShowerTriggerType = 2; // Using MCTruth as trigger, so tigger type is 2.
450  T0col->push_back(anab::T0(ShowerT0,
452  ShowerID,
453  (*T0col).size()
454  ));
455 
456  auto diff = mcpart_i; // check I have a sensible value for this counter
457  if (diff >= (int)mcpartHandle->size()){
458  std::cout << "Error, the backtracker is doing weird things to your pointers!" << std::endl;
459  throw std::exception();
460  }
461  art::Ptr<simb::MCParticle> mcpartPtr(mcpartHandle, mcpart_i);
462  if (fMakeT0Assns){
463  util::CreateAssn(*this, evt, *T0col, showerlist[Shower], *Showerassn);
464  }
465  MCPartShowerassn->addSingle(showerlist[Shower], mcpartPtr, btdata);
466 
467  }// Loop over showers
468  }
469 
470 
471  if (pfparticleListHandle.isValid()){
472  //Access pfparticles and hits
473  art::FindManyP<recob::Cluster> fmcp(pfparticleListHandle, evt, fPFParticleModuleLabel);
474  //art::FindManyP<recob::Hit> fmtht(pfparticleListHandle, evt, fPfparticleModuleLabel);
475 
476  size_t NPfparticles = pfparticlelist.size();
477 
478  // Now to access MCTruth for each pfparticle...
479  for(size_t iPfp=0; iPfp < NPfparticles; ++iPfp) {
480  TrueTrackT0 = 0;
481  TrackID = 0;
482  TrueTrackID = 0;
484 
485  std::vector< art::Ptr<recob::Hit> > allHits;
486  //Get all hits through associated clusters
487  std::vector< art::Ptr<recob::Cluster>> allClusters = fmcp.at(iPfp);
488  art::FindManyP<recob::Hit> fmhcl(allClusters, evt, fPFParticleModuleLabel);
489  for (size_t iclu = 0; iclu<allClusters.size(); ++iclu){
490  std::vector< art::Ptr<recob::Hit>> hits = fmhcl.at(iclu);
491  allHits.insert(allHits.end(), hits.begin(), hits.end());
492  }
493 
494  std::map<int,double> trkide;
495  for(size_t h = 0; h < allHits.size(); ++h){
496  art::Ptr<recob::Hit> hit = allHits[h];
497  std::vector<sim::IDE> ides;
498  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToTrackIDEs(hit);
499 
500  for(size_t e = 0; e < TrackIDs.size(); ++e){
501  trkide[TrackIDs[e].trackID] += TrackIDs[e].energy;
502  }
503  }
504  // Work out which IDE despoited the most charge in the hit if there was more than one.
505  double maxe = -1;
506  double tote = 0;
507  for (std::map<int,double>::iterator ii = trkide.begin(); ii!=trkide.end(); ++ii){
508  tote += ii->second;
509  if ((ii->second)>maxe){
510  maxe = ii->second;
511  TrackID = ii->first;
512  }
513  }
514  btdata.cleanliness = maxe/tote;
515 
516  // Now have trackID, so get PdG code and T0 etc.
517  const simb::MCParticle *tmpParticle = pi_serv->TrackIdToParticle_P(TrackID);
518  if (!tmpParticle) continue; // Retain this check that the BackTracker can find the right particle
519  // Now, loop through the MCParticle's myself to find the correct match
520  int mcpart_i(-1);
521  for (auto const particle : *mcpartHandle){
522  mcpart_i++;
523  if (TrackID == particle.TrackId()){
524  break;
525  }
526  }
527  const simb::MCParticle particle = mcpartHandle.product()->at(mcpart_i);
528  TrueTrackT0 = particle.T();
529  TrueTrackID = particle.TrackId();
530  TrueTriggerType = 2; // Using MCTruth as trigger, so tigger type is 2.
531  //std::cout << "Got particle, PDG = " << particle.PdgCode() << std::endl;
532 
533  //std::cout << "Filling T0col with " << TrueTrackT0 << " " << TrueTriggerType << " " << TrueTrackID << " " << (*T0col).size() << std::endl;
534 
535  T0col->push_back(anab::T0(TrueTrackT0,
537  TrueTrackID,
538  (*T0col).size()
539  ));
540  //auto diff = particle - firstParticle;
541  auto diff = mcpart_i; // check I have a sensible value for this counter
542  if (diff >= (int)mcpartHandle->size()){
543  std::cout << "Error, the backtracker is doing weird things to your pointers!" << std::endl;
544  throw std::exception();
545  }
546 // art::Ptr<simb::MCParticle> mcpartPtr(mcpartHandle, particle - firstParticle);
547  art::Ptr<simb::MCParticle> mcpartPtr(mcpartHandle, mcpart_i);
548  //std::cout << "made MCParticle Ptr" << std::endl;
549 // const std::vertex< std::pair<double, std::string> > cleanlinessCompleteness;
550 // const std::pair<double, double> tmp(0.5, 0.5);
551 // MCPartassn->addSingle(tracklist[iPfp], mcpartPtr, tmp);
553  if (fMakeT0Assns){
554  util::CreateAssn(*this, evt, *T0col, pfparticlelist[iPfp], *PFParticleassn);
555  }
556  MCPartPFParticleassn->addSingle(pfparticlelist[iPfp], mcpartPtr, btdata);
557  }
558  fTree -> Fill();
559  } // Loop over tracks
560  }
561 
562  if (fMakeT0Assns){
563  evt.put(std::move(T0col));
564  evt.put(std::move(Trackassn));
565  evt.put(std::move(Showerassn));
567  evt.put(std::move(PFParticleassn));
568  }
569  }
570  evt.put(std::move(MCPartTrackassn));
571  evt.put(std::move(MCPartShowerassn));
573  evt.put(std::move(MCPartPFParticleassn));
574  }
575  if(fMakeHitAssns)
576  evt.put(std::move(MCPartHitassn));
577 } // Produce
578 
580 
code to link reconstructed objects back to the MC truth information
const simb::MCParticle * TrackIdToParticle_P(int const &id)
Float_t E
Definition: plot.C:23
MCTruthT0Matching & operator=(MCTruthT0Matching const &)=delete
intermediate_table::iterator iterator
const std::vector< sim::TrackIDE > HitToTrackIDEs(recob::Hit const &hit)
std::vector< TrackID > TrackIDs
Declaration of signal hit object.
TNtupleSim Fill(f1, f2, f3, f4)
Definition: T0.h:19
bool isRealData() const
Definition: Event.h:83
Particle class.
MCTruthT0Matching(fhicl::ParameterSet const &p)
int TrackId() const
Definition: MCParticle.h:214
void produce(art::Event &e) override
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
bool isValid() const
Definition: Handle.h:190
void hits()
Definition: readHits.C:15
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
T get(std::string const &key) const
Definition: ParameterSet.h:231
double T(const int i=0) const
Definition: MCParticle.h:228
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
Declaration of cluster object.
Provides recob::Track data product.
Detector simulation of raw signals on wires.
Encapsulate the geometry of a wire.
T * make(ARGS...args) const
Utility object to perform functions of association.
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
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:464
Float_t e
Definition: plot.C:34
void reconfigure(fhicl::ParameterSet const &p)
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33