LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
DirectHitParticleAssns_tool.cc
Go to the documentation of this file.
1 
3 
10 
14 
15 #include <cmath>
16 #include <algorithm>
17 
18 namespace t0
19 {
21 //
22 // Class: DirectHitParticleAssns
23 // Module Type: art tool
24 // File: DirectHitParticleAssns.h
25 //
26 // This provides MC truth information by using output
27 // reco Hit <--> MCParticle associations
28 //
29 // Configuration parameters:
30 //
31 // TruncMeanFraction - the fraction of waveform bins to discard when
32 //
33 // Created by Tracy Usher (usher@slac.stanford.edu) on November 21, 2017
34 //
36 
38 {
39 public:
45  explicit DirectHitParticleAssns(fhicl::ParameterSet const & pset);
46 
51 
52  // provide for initialization
53  void reconfigure(fhicl::ParameterSet const & pset) override;
54 
59 
60 private:
61 
64 
65  struct TrackIDEinfo {
66  float E;
67  float NumElectrons;
68  };
69  std::unordered_map<int, TrackIDEinfo> fTrkIDECollector;
70 };
71 
72 //----------------------------------------------------------------------------
80 {
81  reconfigure(pset);
82 
83  // Report.
84  mf::LogInfo("DirectHitParticleAssns") << "Configured\n";
85 }
86 
87 //----------------------------------------------------------------------------
90 {}
91 
92 //----------------------------------------------------------------------------
100 {
101  fMCParticleModuleLabel = pset.get<art::InputTag>("MCParticleLabel");
102  fHitModuleLabel = pset.get<art::InputTag>("HitModuleLabel");
103 }
104 
105 //----------------------------------------------------------------------------
113 {
114  // This function handles the "direct" creation of hit<-->MCParticle associations through use of the BackTracker
115  //
116  auto mcpartHandle = evt.getValidHandle< std::vector<simb::MCParticle> >(fMCParticleModuleLabel);
117 
118  art::Handle< std::vector<recob::Hit> > hitListHandle;
119  evt.getByLabel(fHitModuleLabel,hitListHandle);
120 
121  if(!hitListHandle.isValid()){
122  std::cerr << "Hit handle is not valid! Returning empty collection" << std::endl;
123  return;
124  }
125 
126  // Access art services...
129 
130  double maxe = -1;
131  double tote = 0;
132  // int trkid = -1;
133  int maxtrkid = -1;
134  double maxn = -1;
135  double totn = 0;
136  int maxntrkid = -1;
138  std::unordered_map<int,int> trkid_lookup; //indexed by geant4trkid, delivers MC particle location
139 
140  auto const& hitList(*hitListHandle);
141  auto const& mcpartList(*mcpartHandle);
142 
143  for(size_t i_h=0; i_h<hitList.size(); ++i_h){
144  art::Ptr<recob::Hit> hitPtr(hitListHandle, i_h);
145  auto trkide_list = btService->HitToTrackIDEs(hitPtr);
146 
147  maxe = -1; tote = 0; maxtrkid = -1;
148  maxn = -1; totn = 0; maxntrkid = -1;
149  fTrkIDECollector.clear();
150 
151  //for(auto const& t : trkide_list){
152  for(size_t i_t=0; i_t<trkide_list.size(); ++i_t){
153  auto const& t(trkide_list[i_t]);
154  fTrkIDECollector[t.trackID].E += t.energy;
155  tote += t.energy;
156  if(fTrkIDECollector[t.trackID].E>maxe) { maxe = fTrkIDECollector[t.trackID].E; maxtrkid = t.trackID; }
157  fTrkIDECollector[t.trackID].NumElectrons += t.numElectrons;
158  totn += t.numElectrons;
159  if(fTrkIDECollector[t.trackID].NumElectrons > maxn) {
160  maxn = fTrkIDECollector[t.trackID].NumElectrons;
161  maxntrkid = t.trackID;
162  }
163 
164  //if not found, find mc particle...
165  if(trkid_lookup.find(t.trackID)==trkid_lookup.end()){
166  size_t i_p=0;
167  while(i_p<mcpartList.size()){
168  if(mcpartList[i_p].TrackId() == abs(t.trackID)) { trkid_lookup[t.trackID] = (int)i_p; break;}
169  ++i_p;
170  }
171  if(i_p==mcpartList.size()) trkid_lookup[t.trackID] = -1;
172  }
173 
174  }
175  //end loop on TrackIDs
176 
177  //now find the mcparticle and loop back through ...
178  for(auto const& t : fTrkIDECollector){
179  int mcpart_i = trkid_lookup[t.first];
180  if(mcpart_i==-1) continue; //no mcparticle here
181  art::Ptr<simb::MCParticle> mcpartPtr(mcpartHandle, mcpart_i);
182  bthmd.ideFraction = t.second.E / tote;
183  bthmd.isMaxIDE = (t.first==maxtrkid);
184  bthmd.ideNFraction = t.second.NumElectrons / totn;
185  bthmd.isMaxIDEN = ( t.first == maxntrkid );
186  bthmd.energy = t.second.E;
187  bthmd.numElectrons = t.second.NumElectrons;
188  hitPartAssns->addSingle(mcpartPtr, hitPtr, bthmd);
189  }
190 
191  }//end loop on hits
192 
193  return;
194 }
195 
196 //----------------------------------------------------------------------------
197 
199 }
code to link reconstructed objects back to the MC truth information
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:45
std::unordered_map< int, TrackIDEinfo > fTrkIDECollector
const std::vector< sim::TrackIDE > HitToTrackIDEs(recob::Hit const &hit)
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void CreateHitParticleAssociations(art::Event &, HitParticleAssociations *) override
This rebuilds the internal maps.
DirectHitParticleAssns(fhicl::ParameterSet const &pset)
Constructor.
T get(std::string const &key) const
Definition: ParameterSet.h:231
Utility object to perform functions of association.
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
void addSingle(Ptr< left_t > const &left, Ptr< right_t > const &right, data_t const &data)
Definition: Assns.h:489
void reconfigure(fhicl::ParameterSet const &pset) override
TCEvent evt
Definition: DataStructs.cxx:5
ValidHandle< PROD > getValidHandle(InputTag const &tag) const