All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCParticleTrackMatching_module.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 /// Class: MCParticleTrackMatching
3 /// Module Type: producer
4 /// File: MCParticleTrackMatching_module.cc
5 ///
6 /// Author: Wesley Ketchum
7 /// E-mail address: wketchum@fnal.gov
8 ///
9 /// This module uses existing hit<-->MCParticle assns to make assns of tracks
10 /// to mcparticles. It also stores some idea of cleanliness of the matching.
11 /// Note: it's probably better to do fancier things with the hit<->particle
12 /// info, but this is a start of sorts.
13 ///
14 /// Input: recob::Tracks, recob::Hit collection, recob::Hit<--->simb::MCparticle assns
15 /// Output: recob::Track/simb::MCParticle assns, with BackTrackerMatchingData.
16 ///
17 /////////////////////////////////////////////////////////////////////////////
18 
19 // Framework includes
20 #include "art/Framework/Core/EDProducer.h"
21 #include "art/Framework/Core/ModuleMacros.h"
22 #include "art/Framework/Principal/Event.h"
23 #include "art/Framework/Principal/Handle.h"
24 #include "canvas/Persistency/Common/FindManyP.h"
25 #include "canvas/Persistency/Common/Ptr.h"
26 #include "canvas/Utilities/InputTag.h"
27 #include "fhiclcpp/ParameterSet.h"
28 
29 #include <memory>
30 #include <iostream>
31 #include <iterator>
32 
33 // LArSoft
37 #include "nusimdata/SimulationBase/MCParticle.h"
38 
39 namespace t0 {
40  class MCParticleTrackMatching;
41 }
42 
43 class t0::MCParticleTrackMatching : public art::EDProducer {
44 public:
45  explicit MCParticleTrackMatching(fhicl::ParameterSet const & p);
46  // The destructor generated by the compiler is fine for classes
47  // without bare pointers or other resource use.
48 
49  // Plugins should not be copied or assigned.
54 
55  // Required functions.
56  void produce(art::Event & e) override;
57 
58 private:
59 
60  art::InputTag fTrackModuleLabel;
61  art::InputTag fTrackHitAssnLabel;
62  art::InputTag fHitModuleLabel;
63  art::InputTag fHitParticleAssnLabel;
64 
65 };
66 
67 
69  : EDProducer{p}
70 {
71  fTrackModuleLabel = p.get<art::InputTag>("TrackModuleLabel");
72  fTrackHitAssnLabel = p.get<art::InputTag>("TrackHitAssnLabel",fTrackModuleLabel);
73  fHitModuleLabel = p.get<art::InputTag>("HitModuleLabel");
74  fHitParticleAssnLabel = p.get<art::InputTag>("HitParticleAssnLabel");
75 
76  produces< art::Assns<recob::Track , simb::MCParticle, anab::BackTrackerMatchingData > > ();
77 }
78 
80 {
81  if (evt.isRealData()) return;
82 
83  //auto mcpartHandle = evt.getValidHandle< std::vector<simb::MCParticle> >("largeant");
84  std::unique_ptr< art::Assns<recob::Track, simb::MCParticle, anab::BackTrackerMatchingData > > MCPartTrackassn( new art::Assns<recob::Track, simb::MCParticle, anab::BackTrackerMatchingData >);
85 
86 
87  double maxe = -1;
88  double tote = 0;
89  // int trkid = -1;
90  //int maxtrkid = -1;
91  //double maxn = -1;
92  //double totn = 0;
93  //int maxntrkid = -1;
94 
96  std::unordered_map<int,double> trkide;
97 
98  art::Handle< std::vector<recob::Track> > trackListHandle;
99  evt.getByLabel(fTrackModuleLabel,trackListHandle);
100 
101  art::Handle< std::vector<recob::Hit> > hitListHandle;
102  evt.getByLabel(fHitModuleLabel,hitListHandle);
103 
104  if(!trackListHandle.isValid()){
105  std::cerr << "Track handle is not valid!" << std::endl;
106  return;
107  }
108 
109  if(!hitListHandle.isValid()){
110  std::cerr << "Hit handle is not valid!" << std::endl;
111  return;
112  }
113 
114  auto const& trackList(*trackListHandle);
115  art::FindManyP<recob::Hit> fmtht(trackListHandle, evt, fTrackHitAssnLabel);
116  //auto const& mcpartList(*mcpartHandle);
117 
118  for(size_t i_t=0; i_t<trackList.size(); ++i_t){
119  art::Ptr<recob::Track> trkPtr(trackListHandle,i_t);
120  trkide.clear();
121  tote = 0; maxe=-1; art::Ptr<simb::MCParticle> maxp;
122 
123  std::vector< art::Ptr<recob::Hit> > allHits = fmtht.at(i_t);
124 
125  std::vector<anab::BackTrackerHitMatchingData const*> bthmd_vec;
126  std::vector< art::Ptr<simb::MCParticle> > matchedParticlePtrs;
127 
128  art::FindManyP<simb::MCParticle,anab::BackTrackerHitMatchingData>
129  particles_per_hit(hitListHandle, evt, fHitParticleAssnLabel);
130 
131  for(size_t i_h=0; i_h<allHits.size(); ++i_h){
132  bthmd_vec.clear(); matchedParticlePtrs.clear();
133  particles_per_hit.get(allHits[i_h].key(),matchedParticlePtrs,bthmd_vec);
134 
135  for(size_t i_p=0; i_p<matchedParticlePtrs.size(); ++i_p){
136  trkide[ matchedParticlePtrs[i_p]->TrackId() ] += bthmd_vec[i_p]->energy;
137  tote += bthmd_vec[i_p]->energy;
138  if( trkide[ matchedParticlePtrs[i_p]->TrackId() ] > maxe ){
139  maxe = trkide[ matchedParticlePtrs[i_p]->TrackId() ];
140  maxp = matchedParticlePtrs[i_p];
141  }
142  }//end loop over particles per hit
143 
144  }//end loop over hits
145 
146  btdata.cleanliness = maxe/tote;
147  if(maxe>0)
148  MCPartTrackassn->addSingle(trkPtr, maxp, btdata);
149 
150  }//end loop over tracks
151 
152  evt.put(std::move(MCPartTrackassn));
153 } // Produce
154 
155 DEFINE_ART_MODULE(t0::MCParticleTrackMatching)
BEGIN_PROLOG could also be cerr
Declaration of signal hit object.
pdgs p
Definition: selectors.fcl:22
MCParticleTrackMatching & operator=(MCParticleTrackMatching const &)=delete
MCParticleTrackMatching(fhicl::ParameterSet const &p)
Provides recob::Track data product.
do i e
TCEvent evt
Definition: DataStructs.cxx:8
void produce(art::Event &e) override