All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DirectHitParticleAssns_tool.cc
Go to the documentation of this file.
2 
3 #include "art/Framework/Principal/Event.h"
4 #include "art/Framework/Principal/Handle.h"
5 #include "art/Framework/Services/Registry/ServiceHandle.h"
6 #include "art/Utilities/ToolMacros.h"
7 #include "canvas/Persistency/Common/Ptr.h"
8 #include "messagefacility/MessageLogger/MessageLogger.h"
9 #include "fhiclcpp/ParameterSet.h"
10 
15 
16 #include "nusimdata/SimulationBase/MCParticle.h"
17 
18 namespace t0 {
19  ////////////////////////////////////////////////////////////////////////
20  //
21  // Class: DirectHitParticleAssns
22  // Module Type: art tool
23  // File: DirectHitParticleAssns.h
24  //
25  // This provides MC truth information by using output
26  // reco Hit <--> MCParticle associations
27  //
28  // Configuration parameters:
29  //
30  // TruncMeanFraction - the fraction of waveform bins to discard when
31  //
32  // Created by Tracy Usher (usher@slac.stanford.edu) on November 21, 2017
33  //
34  ////////////////////////////////////////////////////////////////////////
35 
37  public:
38  /**
39  * @brief Constructor
40  *
41  * @param pset
42  */
43  explicit DirectHitParticleAssns(fhicl::ParameterSet const& pset);
44 
45  // provide for initialization
46  void reconfigure(fhicl::ParameterSet const& pset) override;
47 
48  /**
49  * @brief This rebuilds the internal maps
50  */
51  void CreateHitParticleAssociations(art::Event&, HitParticleAssociations*) override;
52 
53  private:
54  std::vector<art::InputTag> fHitModuleLabelVec;
55  art::InputTag fMCParticleModuleLabel;
56 
57  struct TrackIDEinfo {
58  float E;
59  float NumElectrons;
60  };
61  std::unordered_map<int, TrackIDEinfo> fTrkIDECollector;
62  };
63 
64  //----------------------------------------------------------------------------
65  /// Constructor.
66  ///
67  /// Arguments:
68  ///
69  /// pset - Fcl parameters.
70  ///
71  DirectHitParticleAssns::DirectHitParticleAssns(fhicl::ParameterSet const& pset)
72  {
73  reconfigure(pset);
74 
75  // Report.
76  mf::LogInfo("DirectHitParticleAssns") << "Configured\n";
77  }
78 
79  //----------------------------------------------------------------------------
80  /// Reconfigure method.
81  ///
82  /// Arguments:
83  ///
84  /// pset - Fcl parameter set.
85  ///
86  void
87  DirectHitParticleAssns::reconfigure(fhicl::ParameterSet const& pset)
88  {
89  fMCParticleModuleLabel = pset.get<art::InputTag>("MCParticleLabel");
90  fHitModuleLabelVec = pset.get<std::vector<art::InputTag>>("HitModuleLabelVec");
91  }
92 
93  //----------------------------------------------------------------------------
94  /// Rebuild method -> rebuild the basic maps to get truth information
95  ///
96  /// Arguments:
97  ///
98  /// event - the art event used to extract all information
99  ///
100  void
102  HitParticleAssociations* hitPartAssns)
103  {
104  // This function handles the "direct" creation of hit<-->MCParticle associations through use of the BackTracker
105  //
106  auto mcpartHandle = evt.getValidHandle<std::vector<simb::MCParticle>>(fMCParticleModuleLabel);
107 
108  // Access art services...
109  art::ServiceHandle<cheat::BackTrackerService const> btService;
110  art::ServiceHandle<cheat::ParticleInventoryService const> piService;
111 
112  auto const clockData =
113  art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
114 
115  // Loop over input hit producer labels
116  for (const auto& inputTag : fHitModuleLabelVec) {
117  art::Handle<std::vector<recob::Hit>> hitListHandle;
118  evt.getByLabel(inputTag, hitListHandle);
119 
120  if (!hitListHandle.isValid()) {
121  mf::LogInfo("DirectHitParticleAssns")
122  << "InputTag not associating to valid hit collection, tag: " << inputTag << "\n";
123  continue;
124  }
125 
127  std::unordered_map<int, int>
128  trkid_lookup; //indexed by geant4trkid, delivers MC particle location
129 
130  auto const& hitList(*hitListHandle);
131  auto const& mcpartList(*mcpartHandle);
132 
133  for (size_t i_h = 0; i_h < hitList.size(); ++i_h) {
134  art::Ptr<recob::Hit> hitPtr(hitListHandle, i_h);
135 
136  auto trkide_list = btService->HitToTrackIDEs(clockData, hitPtr);
137 
138  double maxe(-1.);
139  double tote(0.);
140  int maxtrkid(-1);
141  double maxn(-1.);
142  double totn(0.);
143  int maxntrkid(-1);
144 
145  fTrkIDECollector.clear();
146 
147  //for(auto const& t : trkide_list){
148  for (size_t i_t = 0; i_t < trkide_list.size(); ++i_t) {
149  auto const& t(trkide_list[i_t]);
150  fTrkIDECollector[t.trackID].E += t.energy;
151  tote += t.energy;
152  if (fTrkIDECollector[t.trackID].E > maxe) {
153  maxe = fTrkIDECollector[t.trackID].E;
154  maxtrkid = t.trackID;
155  }
156  fTrkIDECollector[t.trackID].NumElectrons += t.numElectrons;
157  totn += t.numElectrons;
158  if (fTrkIDECollector[t.trackID].NumElectrons > maxn) {
159  maxn = fTrkIDECollector[t.trackID].NumElectrons;
160  maxntrkid = t.trackID;
161  }
162 
163  //if not found, find mc particle...
164  if (trkid_lookup.find(t.trackID) == trkid_lookup.end()) {
165  size_t i_p = 0;
166  while (i_p < mcpartList.size()) {
167  if (mcpartList[i_p].TrackId() == abs(t.trackID)) {
168  trkid_lookup[t.trackID] = (int)i_p;
169  break;
170  }
171  ++i_p;
172  }
173  if (i_p == mcpartList.size()) trkid_lookup[t.trackID] = -1;
174  }
175  }
176  //end loop on TrackIDs
177 
178  //now find the mcparticle and loop back through ...
179  for (auto const& t : fTrkIDECollector) {
180  int mcpart_i = trkid_lookup[t.first];
181  if (mcpart_i == -1) continue; //no mcparticle here
182  art::Ptr<simb::MCParticle> mcpartPtr(mcpartHandle, mcpart_i);
183  bthmd.ideFraction = t.second.E / tote;
184  bthmd.isMaxIDE = (t.first == maxtrkid);
185  bthmd.ideNFraction = t.second.NumElectrons / totn;
186  bthmd.isMaxIDEN = (t.first == maxntrkid);
187  bthmd.energy = t.second.E;
188  bthmd.numElectrons = t.second.NumElectrons;
189  hitPartAssns->addSingle(mcpartPtr, hitPtr, bthmd);
190  }
191 
192  } //end loop on hits
193  } // end loop on producers
194 
195  return;
196  }
197 
198  //----------------------------------------------------------------------------
199 
200  DEFINE_ART_CLASS_TOOL(DirectHitParticleAssns)
201 }
art::Assns< simb::MCParticle, recob::Hit, anab::BackTrackerHitMatchingData > HitParticleAssociations
std::unordered_map< int, TrackIDEinfo > fTrkIDECollector
Declaration of signal hit object.
void CreateHitParticleAssociations(art::Event &, HitParticleAssociations *) override
This rebuilds the internal maps.
DirectHitParticleAssns(fhicl::ParameterSet const &pset)
Constructor.
T abs(T value)
std::vector< art::InputTag > fHitModuleLabelVec
void reconfigure(fhicl::ParameterSet const &pset) override
TCEvent evt
Definition: DataStructs.cxx:8