DirectHitParticleAssns_tool.cc
Go to the documentation of this file.
2 
7 
11 
12 namespace t0 {
13  ////////////////////////////////////////////////////////////////////////
14  //
15  // Class: DirectHitParticleAssns
16  // Module Type: art tool
17  // File: DirectHitParticleAssns.h
18  //
19  // This provides MC truth information by using output
20  // reco Hit <--> MCParticle associations
21  //
22  // Configuration parameters:
23  //
24  // TruncMeanFraction - the fraction of waveform bins to discard when
25  //
26  // Created by Tracy Usher (usher@slac.stanford.edu) on November 21, 2017
27  //
28  ////////////////////////////////////////////////////////////////////////
29 
31  public:
32  /**
33  * @brief Constructor
34  *
35  * @param pset
36  */
37  explicit DirectHitParticleAssns(fhicl::ParameterSet const& pset);
38 
39  // provide for initialization
40  void reconfigure(fhicl::ParameterSet const& pset) override;
41 
42  /**
43  * @brief This rebuilds the internal maps
44  */
46 
47  private:
48  std::vector<art::InputTag> fHitModuleLabelVec;
50 
51  struct TrackIDEinfo {
52  float E;
53  float NumElectrons;
54  };
55  std::unordered_map<int, TrackIDEinfo> fTrkIDECollector;
56  };
57 
58  //----------------------------------------------------------------------------
59  /// Constructor.
60  ///
61  /// Arguments:
62  ///
63  /// pset - Fcl parameters.
64  ///
66  {
67  reconfigure(pset);
68 
69  // Report.
70  mf::LogInfo("DirectHitParticleAssns") << "Configured\n";
71  }
72 
73  //----------------------------------------------------------------------------
74  /// Reconfigure method.
75  ///
76  /// Arguments:
77  ///
78  /// pset - Fcl parameter set.
79  ///
80  void
82  {
83  fMCParticleModuleLabel = pset.get<art::InputTag>("MCParticleLabel");
84  fHitModuleLabelVec = pset.get<std::vector<art::InputTag>>("HitModuleLabelVec");
85  }
86 
87  //----------------------------------------------------------------------------
88  /// Rebuild method -> rebuild the basic maps to get truth information
89  ///
90  /// Arguments:
91  ///
92  /// event - the art event used to extract all information
93  ///
94  void
96  HitParticleAssociations* hitPartAssns)
97  {
98  // This function handles the "direct" creation of hit<-->MCParticle associations through use of the BackTracker
99  //
100  auto mcpartHandle = evt.getValidHandle<std::vector<simb::MCParticle>>(fMCParticleModuleLabel);
101 
102  // Access art services...
105 
106  auto const clockData =
108 
109  // Loop over input hit producer labels
110  for (const auto& inputTag : fHitModuleLabelVec) {
112  evt.getByLabel(inputTag, hitListHandle);
113 
114  if (!hitListHandle.isValid()) {
115  mf::LogInfo("DirectHitParticleAssns")
116  << "InputTag not associating to valid hit collection, tag: " << inputTag << "\n";
117  continue;
118  }
119 
121  std::unordered_map<int, int>
122  trkid_lookup; //indexed by geant4trkid, delivers MC particle location
123 
124  auto const& hitList(*hitListHandle);
125  auto const& mcpartList(*mcpartHandle);
126 
127  for (size_t i_h = 0; i_h < hitList.size(); ++i_h) {
128  art::Ptr<recob::Hit> hitPtr(hitListHandle, i_h);
129 
130  auto trkide_list = btService->HitToTrackIDEs(clockData, hitPtr);
131 
132  double maxe(-1.);
133  double tote(0.);
134  int maxtrkid(-1);
135  double maxn(-1.);
136  double totn(0.);
137  int maxntrkid(-1);
138 
139  fTrkIDECollector.clear();
140 
141  //for(auto const& t : trkide_list){
142  for (size_t i_t = 0; i_t < trkide_list.size(); ++i_t) {
143  auto const& t(trkide_list[i_t]);
144  fTrkIDECollector[t.trackID].E += t.energy;
145  tote += t.energy;
146  if (fTrkIDECollector[t.trackID].E > maxe) {
147  maxe = fTrkIDECollector[t.trackID].E;
148  maxtrkid = t.trackID;
149  }
150  fTrkIDECollector[t.trackID].NumElectrons += t.numElectrons;
151  totn += t.numElectrons;
152  if (fTrkIDECollector[t.trackID].NumElectrons > maxn) {
153  maxn = fTrkIDECollector[t.trackID].NumElectrons;
154  maxntrkid = t.trackID;
155  }
156 
157  //if not found, find mc particle...
158  if (trkid_lookup.find(t.trackID) == trkid_lookup.end()) {
159  size_t i_p = 0;
160  while (i_p < mcpartList.size()) {
161  if (mcpartList[i_p].TrackId() == abs(t.trackID)) {
162  trkid_lookup[t.trackID] = (int)i_p;
163  break;
164  }
165  ++i_p;
166  }
167  if (i_p == mcpartList.size()) trkid_lookup[t.trackID] = -1;
168  }
169  }
170  //end loop on TrackIDs
171 
172  //now find the mcparticle and loop back through ...
173  for (auto const& t : fTrkIDECollector) {
174  int mcpart_i = trkid_lookup[t.first];
175  if (mcpart_i == -1) continue; //no mcparticle here
176  art::Ptr<simb::MCParticle> mcpartPtr(mcpartHandle, mcpart_i);
177  bthmd.ideFraction = t.second.E / tote;
178  bthmd.isMaxIDE = (t.first == maxtrkid);
179  bthmd.ideNFraction = t.second.NumElectrons / totn;
180  bthmd.isMaxIDEN = (t.first == maxntrkid);
181  bthmd.energy = t.second.E;
182  bthmd.numElectrons = t.second.NumElectrons;
183  hitPartAssns->addSingle(mcpartPtr, hitPtr, bthmd);
184  }
185 
186  } //end loop on hits
187  } // end loop on producers
188 
189  return;
190  }
191 
192  //----------------------------------------------------------------------------
193 
195 }
code to link reconstructed objects back to the MC truth information
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
std::unordered_map< int, TrackIDEinfo > fTrkIDECollector
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void CreateHitParticleAssociations(art::Event &, HitParticleAssociations *) override
This rebuilds the internal maps.
DirectHitParticleAssns(fhicl::ParameterSet const &pset)
Constructor.
bool isValid() const noexcept
Definition: Handle.h:191
T abs(T value)
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
T get(std::string const &key) const
Definition: ParameterSet.h:271
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
std::vector< art::InputTag > fHitModuleLabelVec
void addSingle(Ptr< left_t > const &left, Ptr< right_t > const &right, data_t const &data)
Definition: Assns.h:546
void reconfigure(fhicl::ParameterSet const &pset) override
TCEvent evt
Definition: DataStructs.cxx:7