All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BackTracker.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////
2 //
3 //
4 // \file BackTracker.cc
5 // \brief The functions needed for the BackTracker class needed by the BackTracker service in order to connect truth information with reconstruction.
6 // \author jason.stock@mines.sdsmt.edu
7 //
8 // Based on the original BackTracker by brebel@fnal.gov
9 //
10 //
11 ///////////////////////////////////////////////////////////////////
12 
13 #include "messagefacility/MessageLogger/MessageLogger.h"
14 
15 #include "BackTracker.h"
17 
21 
22 namespace cheat {
23 
24  //-----------------------------------------------------------------------
26  const cheat::ParticleInventory* partInv,
27  const geo::GeometryCore* geom)
28  : fPartInv(partInv)
29  , fGeom(geom)
30  , fG4ModuleLabel(config.G4ModuleLabel())
31  , fSimChannelModuleLabel(config.SimChannelModuleLabel())
32  , fHitLabel(config.DefaultHitModuleLabel())
33  , fMinHitEnergyFraction(config.MinHitEnergyFraction())
34  , fOverrideRealData(config.OverrideRealData())
35  , fHitTimeRMS(config.HitTimeRMS())
36  {}
37 
38  //-----------------------------------------------------------------------
39  BackTracker::BackTracker(const fhicl::ParameterSet& pSet,
40  const cheat::ParticleInventory* partInv,
41  const geo::GeometryCore* geom)
42  : fPartInv(partInv)
43  , fGeom(geom)
44  , fG4ModuleLabel(pSet.get<art::InputTag>("G4ModuleLabel", "largeant"))
45  , fSimChannelModuleLabel(pSet.get<art::InputTag>("SimChannelModuleLabel", fG4ModuleLabel))
46  , // -- D.R. if not provided, default behavior is to use the G4ModuleLabel
47  fHitLabel(pSet.get<art::InputTag>("DefaultHitModuleLabel", "hitfd"))
48  , fMinHitEnergyFraction(pSet.get<double>("MinHitEnergyFraction", 0.010))
49  , fOverrideRealData(pSet.get<bool>("OverrideRealData", false))
50  , fHitTimeRMS(pSet.get<double>("HitTimeRMS", 1.0))
51  {}
52 
53  //-----------------------------------------------------------------------
54  void
56  {
57  fSimChannels.clear();
58  // fAllHitList.clear();
59  }
60 
61  //-----------------------------------------------------------------------
62  std::vector<const sim::IDE*>
63  BackTracker::TrackIdToSimIDEs_Ps(int const& id) const
64  {
65  std::vector<const sim::IDE*> ideps;
66  for (size_t sc = 0; sc < fSimChannels.size(); ++sc) {
67  const auto& tdcidemap = fSimChannels[sc]->TDCIDEMap(); // This returns a reference.
68  // loop over the IDEMAP
69  for (auto mapitr = tdcidemap.begin(); mapitr != tdcidemap.end(); mapitr++) {
70  // loop over the vector of IDE objects.
71  const std::vector<sim::IDE>& idevec =
72  (*mapitr).second; // note, mapitr.second returns the actual data from
73  // the map, not a copy
74  for (size_t iv = 0; iv < idevec.size(); ++iv) {
75  // const sim::IDE* const idep = &idevec[iv];
76  // if( abs(idevec[iv].trackID) == id) continue;
77  // ideps.push_back(idep);
78  if (abs(idevec[iv].trackID) == id) ideps.push_back(&(idevec[iv]));
79  } // end for index in idevec
80  } // end loop over map from sim::SimChannel
81  } // end loop over sim::SimChannels
82  return ideps;
83  }
84 
85  //-----------------------------------------------------------------------
86  std::vector<const sim::IDE*>
87  BackTracker::TrackIdToSimIDEs_Ps(int const& id, const geo::View_t view) const
88  {
89  std::vector<const sim::IDE*> ide_Ps;
90  for (const art::Ptr<sim::SimChannel> sc : fSimChannels) {
91  if (fGeom->View(sc->Channel()) != view) continue;
92 
93  // loop over the IDEMAP
94  for (const auto& item : sc->TDCIDEMap()) {
95 
96  // loop over the vector of IDE objects.
97  for (const sim::IDE& ide : item.second) {
98  if (abs(ide.trackID) == id) ide_Ps.push_back(&ide);
99  }
100  } // end loop over map from sim::SimChannel
101  } // end loop over sim::SimChannels
102 
103  return ide_Ps;
104  }
105 
106  //-----------------------------------------------------------------------
107  art::Ptr<sim::SimChannel>
109  {
110  auto ilb = std::lower_bound(fSimChannels.begin(),
111  fSimChannels.end(),
112  channel,
113  [](art::Ptr<sim::SimChannel> const& a, raw::ChannelID_t channel) {
114  return (a->Channel() < channel);
115  });
116  return ((ilb != fSimChannels.end()) && ((*ilb)->Channel() == channel))
117  ? *ilb: art::Ptr<sim::SimChannel>{};
118  }
119 
120  //-----------------------------------------------------------------------
121  art::Ptr<sim::SimChannel>
123  {
124  if (auto const chan = FindSimChannelPtr(channel)) return chan;
125  throw cet::exception("BackTracker") << "No sim::SimChannel corresponding "
126  << "to channel: " << channel << "\n";
127  }
128 
129  //-----------------------------------------------------------------------
130  std::vector<sim::TrackIDE>
132  raw::ChannelID_t channel,
133  const double hit_start_time,
134  const double hit_end_time) const
135  {
136  art::Ptr<sim::SimChannel> schannel = this->FindSimChannelPtr(channel);
137  if (!schannel) return {};
138 
139  std::vector<sim::TrackIDE> trackIDEs;
140  double totalE = 0.;
141 
142  // loop over the electrons in the channel and grab those that are in time
143  // with the identified hit start and stop times
144  int start_tdc = clockData.TPCTick2TDC(hit_start_time);
145  int end_tdc = clockData.TPCTick2TDC(hit_end_time);
146  if (start_tdc < 0) start_tdc = 0;
147  if (end_tdc < 0) end_tdc = 0;
148  std::vector<sim::IDE> simides = schannel->TrackIDsAndEnergies(start_tdc, end_tdc);
149 
150  // first get the total energy represented by all track ids for
151  // this channel and range of tdc values
152  for (size_t e = 0; e < simides.size(); ++e)
153  totalE += simides[e].energy;
154 
155  // protect against a divide by zero below
156  if (totalE < 1.e-5) totalE = 1.;
157 
158  // loop over the entries in the map and fill the input vectors
159 
160  for (size_t e = 0; e < simides.size(); ++e) {
161 
162  if (simides[e].trackID == sim::NoParticleId) continue;
163 
165  info.trackID = simides[e].trackID;
166  info.energyFrac = simides[e].energy / totalE;
167  info.energy = simides[e].energy;
168  info.numElectrons = simides[e].numElectrons;
169 
170  trackIDEs.push_back(info);
171  }
172 
173  return trackIDEs;
174  }
175 
176  //-----------------------------------------------------------------------
177  std::vector<sim::TrackIDE>
179  recob::Hit const& hit) const
180  {
181  const double start = hit.PeakTimeMinusRMS(fHitTimeRMS);
182  const double end = hit.PeakTimePlusRMS(fHitTimeRMS);
183  return this->ChannelToTrackIDEs(clockData, hit.Channel(), start, end);
184  }
185 
186  //-----------------------------------------------------------------------
187  std::vector<int>
189  recob::Hit const& hit) const
190  {
191  std::vector<int> retVec;
192  for (auto const trackIDE : this->HitToTrackIDEs(clockData, hit)) {
193  retVec.push_back(trackIDE.trackID);
194  }
195  return retVec;
196  }
197 
198  // These don't exist in the event. They are generated on the fly.
199  //----------------------------------------------------------------------------
200  std::vector<sim::TrackIDE>
202  recob::Hit const& hit) const
203  {
204  std::vector<sim::TrackIDE> eveIDEs;
205  std::vector<sim::TrackIDE> trackIDEs = this->HitToTrackIDEs(clockData, hit);
206  std::map<int, std::pair<double, double>> eveToEMap;
207  double totalE = 0.0;
208  for (const auto& trackIDE : trackIDEs) {
209  auto check = eveToEMap.emplace(fPartInv->TrackIdToEveTrackId(trackIDE.trackID),
210  std::make_pair(trackIDE.energy, trackIDE.numElectrons));
211  if (check.second == false) {
212  check.first->second.first += trackIDE.energy;
213  check.first->second.second += trackIDE.numElectrons;
214  }
215  // eveToEMap[fPartInv->TrackIdToEveTrackId(trackIDE.trackID)].first
216  // += trackIDE.energy;
217  totalE += trackIDE.energy;
218  } // End for trackIDEs
219  eveIDEs.reserve(eveToEMap.size());
220  for (const auto& eveToE : eveToEMap) {
221  sim::TrackIDE eveTrackIDE_tmp;
222 
223  eveTrackIDE_tmp.trackID = eveToE.first;
224  eveTrackIDE_tmp.energy = eveToE.second.first;
225  eveTrackIDE_tmp.energyFrac = (eveTrackIDE_tmp.energy) / (totalE);
226  eveTrackIDE_tmp.numElectrons = eveToE.second.second;
227 
228  eveIDEs.push_back(eveTrackIDE_tmp);
229  } // END eveToEMap loop
230  return eveIDEs;
231  }
232 
233  //-----------------------------------------------------------------------
234  std::vector<art::Ptr<recob::Hit>>
236  const int tkId,
237  std::vector<art::Ptr<recob::Hit>> const& hitsIn) const
238  {
239  // returns a subset of the hits in the hitsIn collection that are matched
240  // to the given track
241 
242  // temporary vector of TrackIds and Ptrs to hits so only one
243  // loop through the (possibly large) hitsIn collection is needed
244  std::vector<art::Ptr<recob::Hit>> hitList;
245  std::vector<sim::TrackIDE> trackIDE;
246  for (auto itr = hitsIn.begin(); itr != hitsIn.end(); ++itr) {
247  trackIDE.clear();
248  art::Ptr<recob::Hit> const& hit = *itr;
249  trackIDE = this->ChannelToTrackIDEs(
250  clockData, hit->Channel(), hit->PeakTimeMinusRMS(fHitTimeRMS), hit->PeakTimePlusRMS(fHitTimeRMS));
251  for (auto itr_trakIDE = trackIDE.begin(); itr_trakIDE != trackIDE.end(); ++itr_trakIDE) {
252  if (itr_trakIDE->trackID == tkId && itr_trakIDE->energyFrac > fMinHitEnergyFraction)
253  hitList.push_back(hit);
254  } // itr_trakIDE
255  } // itr
256  return hitList;
257  }
258 
259  //-----------------------------------------------------------------------
260  //This function could clearly be made by calling TrackIdToHits for each trackId, but that would be significantly slower because we would loop through all hits many times.
261  std::vector<std::vector<art::Ptr<recob::Hit>>>
263  std::vector<int> const& tkIds,
264  std::vector<art::Ptr<recob::Hit>> const& hitsIn) const
265  {
266  // returns a subset of the hits in the hitsIn collection that are matched
267  // to MC particles listed in tkIds
268 
269  // temporary vector of TrackIds and Ptrs to hits so only one
270  // loop through the (possibly large) hitsIn collection is needed
271  std::vector<std::pair<int, art::Ptr<recob::Hit>>> hitList;
272  std::vector<sim::TrackIDE> tids;
273  for (auto itr = hitsIn.begin(); itr != hitsIn.end(); ++itr) {
274  tids.clear();
275  art::Ptr<recob::Hit> const& hit = *itr;
276  tids = this->ChannelToTrackIDEs(
277  clockData, hit->Channel(), hit->PeakTimeMinusRMS(fHitTimeRMS), hit->PeakTimePlusRMS(fHitTimeRMS));
278  for (auto itid = tids.begin(); itid != tids.end(); ++itid) {
279  for (auto itkid = tkIds.begin(); itkid != tkIds.end(); ++itkid) {
280  if (itid->trackID == *itkid) {
281  if (itid->energyFrac > fMinHitEnergyFraction)
282  hitList.push_back(std::make_pair(*itkid, hit));
283  }
284  } // itkid
285  } // itid
286  } // itr
287 
288  // now build the truHits vector that will be returned to the caller
289  std::vector<std::vector<art::Ptr<recob::Hit>>> truHits;
290  // temporary vector containing hits assigned to one MC particle
291  std::vector<art::Ptr<recob::Hit>> tmpHits;
292  for (auto itkid = tkIds.begin(); itkid != tkIds.end(); ++itkid) {
293  tmpHits.clear();
294  for (auto itr = hitList.begin(); itr != hitList.end(); ++itr) {
295  if (*itkid == (*itr).first) tmpHits.push_back((*itr).second);
296  }
297  truHits.push_back(tmpHits);
298  }
299  return truHits;
300  }
301 
302  //-----------------------------------------------------------------------
303  //Cannot be returned as a reference, as these IDEs do not exist in the event. They are constructed on the fly.
304  std::vector<sim::IDE>
306  recob::Hit const& hit) const
307  {
308  // Get services.
309 
310  int start_tdc = clockData.TPCTick2TDC(hit.PeakTimeMinusRMS(fHitTimeRMS));
311  int end_tdc = clockData.TPCTick2TDC(hit.PeakTimePlusRMS(fHitTimeRMS));
312  if (start_tdc < 0) start_tdc = 0;
313  if (end_tdc < 0) end_tdc = 0;
314 
315  return (this->FindSimChannel(hit.Channel()))->TrackIDsAndEnergies(start_tdc, end_tdc);
316  }
317 
318  //-----------------------------------------------------------------------
319  std::vector<const sim::IDE*>
321  recob::Hit const& hit) const
322  {
323  std::vector<const sim::IDE*> retVec;
324  int start_tdc = clockData.TPCTick2TDC(hit.PeakTimeMinusRMS(fHitTimeRMS));
325  int end_tdc = clockData.TPCTick2TDC(hit.PeakTimePlusRMS(fHitTimeRMS));
326  if (start_tdc < 0) start_tdc = 0;
327  if (end_tdc < 0) end_tdc = 0;
328 
329  if (start_tdc > end_tdc) { throw; }
330 
331  const std::vector<std::pair<unsigned short, std::vector<sim::IDE>>>& tdcIDEMap =
332  (this->FindSimChannel(hit.Channel()))
333  ->TDCIDEMap(); // This in fact does not return a std::map. It returns a
334  // vector... with no guarantee that it is sorted...
335  std::vector<const std::pair<unsigned short, std::vector<sim::IDE>>*> tdcIDEMap_SortedPointers;
336  for (auto& pair : tdcIDEMap) {
337  tdcIDEMap_SortedPointers.push_back(&pair);
338  }
339 
340  // This is a bunch of extra steps, due to needing a vector we can sort, and
341  // needing those items in the sorted vector to be the items from the sim
342  // channels (so a pointer to the IDEs inside the sim channels can be made).
343  // The work around is to make a vector of pointers to IDEs inside the
344  // TDCIDEMap (which is a constant reference to the fTDCIDEs in the
345  // SimChannel.)
346  auto pairSort = [](auto& a, auto& b) { return a->first < b->first; };
347  if (!std::is_sorted(
348  tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), pairSort)) {
349  std::sort(tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), pairSort);
350  }
351 
352  std::vector<sim::IDE> dummyVec; // I need something to stick in a pair to compare pair<tdcVal,
353  // IDE>. This is an otherwise useless "hack".
354  std::pair<double, std::vector<sim::IDE>> start_tdcPair =
355  std::make_pair(start_tdc, dummyVec); // This pair is a "hack" to make my comparison work
356  // for lower and upper bound.
357  std::pair<double, std::vector<sim::IDE>> end_tdcPair = std::make_pair(end_tdc, dummyVec);
358  auto start_tdcPair_P = &start_tdcPair;
359  auto end_tdcPair_P = &end_tdcPair;
360  auto mapFirst = std::lower_bound(
361  tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), start_tdcPair_P, pairSort);
362 
363  // iterator to just after the last interesting IDE
364  auto mapLast = std::upper_bound(
365  tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), end_tdcPair_P, pairSort);
366  for (auto& mapitr = mapFirst; mapitr != mapLast; ++mapitr) {
367  for (auto& ide : (*mapitr)->second) {
368  retVec.push_back(&ide);
369  } // Add all interesting IDEs to the retVec
370  }
371  return retVec;
372  }
373 
374  //------------------------------------------------------------------------------
375  std::vector<double>
376  BackTracker::SimIDEsToXYZ(std::vector<sim::IDE> const& ides) const
377  {
378  std::vector<double> xyz(3, 0.0);
379  double w = 0.0;
380  for (auto const& ide : ides) {
381  double weight = ide.numElectrons;
382  w += weight;
383  xyz[0] += (weight * ide.x);
384  xyz[1] += (weight * ide.y);
385  xyz[2] += (weight * ide.z);
386  }
387  if (w < 1.e-5)
388  throw cet::exception("BackTracker") << "No sim::IDEs providing non-zero number of electrons"
389  << " can't determine originating location from truth\n";
390  xyz[0] = xyz[0] / w;
391  xyz[1] = xyz[1] / w;
392  xyz[2] = xyz[2] / w;
393  return xyz;
394  }
395 
396  //-------------------------------------------------------------------------------
397  std::vector<double>
398  BackTracker::SimIDEsToXYZ(std::vector<const sim::IDE*> const& ide_Ps) const
399  {
400  std::vector<sim::IDE> ides;
401  for (auto ide_P : ide_Ps) {
402  ides.push_back(*ide_P);
403  }
404  return this->SimIDEsToXYZ(ides);
405  }
406 
407  //--------------------------------------------------------------------------------
408  std::vector<double>
410  {
411  std::vector<const sim::IDE*> ide_Ps = this->HitToSimIDEs_Ps(clockData, hit);
412  return this->SimIDEsToXYZ(ide_Ps);
413  }
414 
415  //-----------------------------------------------------------------------------------
416  double
418  std::set<int> const& trackIds,
419  std::vector<art::Ptr<recob::Hit>> const& hits) const
420  {
421  int desired = 0;
422  for (const auto& hit : hits) {
423  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(clockData, hit);
424  for (const auto& tIDE : hitTrackIDEs) {
425  if (trackIds.find(tIDE.trackID) != trackIds.end()) {
426  ++desired;
427  break;
428  } // End if TID Found
429  } // END for trackIDE in TrackIDEs
430  } // End for hit in hits
431  if (hits.size() > 0) { return double(double(desired) / double(hits.size())); }
432  return 0;
433  }
434 
435  //-----------------------------------------------------------------------------------
436  double
438  std::set<int> const& trackIds,
439  std::vector<art::Ptr<recob::Hit>> const& hits) const
440  {
441  double totalCharge = 0., desired = 0.;
442  for (const auto& hit : hits) {
443  totalCharge += hit->Integral();
444  std::vector<sim::TrackIDE> trackIDEs = this->HitToTrackIDEs(clockData, hit);
445  for (const auto& trackIDE : trackIDEs) {
446  if (trackIds.find(trackIDE.trackID) != trackIds.end()) {
447  desired += hit->Integral();
448  break;
449  } // End if trackId in trackIds.
450  } // End for trackIDE in trackIDEs
451  } // End for Hit in Hits
452  if (totalCharge > 0.0) { return (desired / totalCharge); }
453  return 0.0;
454  }
455 
456  //-----------------------------------------------------------------------------------
457  double
459  std::set<int> const& trackIds,
460  std::vector<art::Ptr<recob::Hit>> const& hits,
461  std::vector<art::Ptr<recob::Hit>> const& allHits,
462  geo::View_t const& view) const
463  {
464 
465  int desired = 0, total = 0;
466 
467  for (const auto& hit : hits) {
468  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(clockData, hit);
469  for (const auto& trackIDE : hitTrackIDEs) {
470  if (trackIds.find(trackIDE.trackID) != trackIds.end() &&
471  trackIDE.energyFrac >= fMinHitEnergyFraction) {
472  ++desired;
473  break;
474  } // End if trackID in trackIds.
475  } // end for trackIDE in TrackIDEs
476  } // end for hit in hits
477 
478  for (const auto& hit : allHits) {
479  if (hit->View() != view && view != geo::k3D) {
480  continue;
481  } // End if hit.view = view or view = geo::k3D
482  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(clockData, hit);
483  for (const auto& hitIDE : hitTrackIDEs) {
484  if (trackIds.find(hitIDE.trackID) != trackIds.end() &&
485  hitIDE.energyFrac >= fMinHitEnergyFraction) {
486  ++total;
487  break;
488  }
489  } // END for all IDEs in HitTrackIDEs.
490  } // end for hit in allHits.
491  if (total >= 0) { return double(double(desired) / double(total)); }
492  return 0.;
493  }
494 
495  //-----------------------------------------------------------------------------------
496  double
498  std::set<int> const& trackIds,
499  std::vector<art::Ptr<recob::Hit>> const& hits,
500  std::vector<art::Ptr<recob::Hit>> const& allHits,
501  geo::View_t const& view) const
502  {
503  double desired = 0., total = 0.;
504  for (const auto& hit : hits) {
505  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(clockData, hit);
506  for (const auto& hitIDE : hitTrackIDEs) {
507  if (trackIds.find(hitIDE.trackID) != trackIds.end() &&
508  hitIDE.energyFrac >= fMinHitEnergyFraction) {
509  desired += hit->Integral();
510  break;
511  } // end if hit id matches and energy sufficient.
512  } // End for IDE in HitTrackIDEs.
513  } // End for hit in hits.
514 
515  for (const auto& hit : allHits) {
516  if (hit->View() != view && view != geo::k3D) { continue; }
517  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(clockData, hit);
518  for (const auto& hitIDE : hitTrackIDEs) {
519  if (trackIds.find(hitIDE.trackID) != trackIds.end() &&
520  hitIDE.energyFrac >= fMinHitEnergyFraction) {
521  total += hit->Integral();
522  break;
523  } // end if hit matches
524  } // end for ide in ides
525  } // End for hit in allHits
526 
527  if (total > 0.) { return desired / total; }
528  return 0.;
529  }
530 
531  //-----------------------------------------------------------------------------------
532  std::set<int>
534  std::vector<art::Ptr<recob::Hit>> const& hits) const
535  {
536  std::set<int> tids;
537  for (const auto& hit : hits) {
538  const double start = hit->PeakTimeMinusRMS(fHitTimeRMS);
539  const double end = hit->PeakTimePlusRMS(fHitTimeRMS);
540  std::vector<sim::TrackIDE> trackIDEs =
541  this->ChannelToTrackIDEs(clockData, hit->Channel(), start, end);
542  for (const auto& ide : trackIDEs) {
543  tids.insert(ide.trackID);
544  } // End for TrackIDEs
545  } // End for hits
546  return tids;
547  } // End GetSetOfTrackIds
548 
549  //-----------------------------------------------------------------------------------
550  std::set<int>
552  std::vector<art::Ptr<recob::Hit>> const& hits) const
553  {
554  std::set<int> eveIds;
555  for (const auto& hit : hits) {
556  const std::vector<sim::TrackIDE> ides = this->HitToEveTrackIDEs(clockData, hit);
557  for (const auto& ide : ides) {
558  eveIds.insert(ide.trackID);
559  } // end ides
560  } // End for hits
561  return eveIds;
562  }
563 
564  //This function definitely needs a new implimentation. There must be abetter way than so many loops.
565  std::vector<double>
567  std::vector<art::Ptr<recob::Hit>> const& hits) const
568  {
569  std::vector<double> xyz(3, -99999.9);
570  std::vector<std::vector<std::vector<int>>> numHits(fGeom->Ncryostats());
571  std::vector<std::vector<std::vector<double>>> hitWeight(fGeom->Ncryostats());
572  std::vector<std::vector<std::vector<std::vector<double>>>> hitPos(fGeom->Ncryostats());
573  //Do we need to resize everything...
574  for (size_t c = 0; c < numHits.size(); ++c) {
575  numHits[c].resize(fGeom->NTPC(c));
576  hitWeight[c].resize(fGeom->NTPC(c));
577  hitPos[c].resize(fGeom->NTPC(c));
578  for (size_t t = 0; t < numHits[c].size(); ++t) {
579  numHits[c][t].resize(fGeom->Nplanes(t, c));
580  hitWeight[c][t].resize(fGeom->Nplanes(t, c));
581  hitPos[c][t].resize(fGeom->Nplanes(t, c));
582  }
583  }
584 
585  for (art::PtrVector<recob::Hit>::const_iterator ihit = hits.begin(); ihit != hits.end();
586  ++ihit) {
587 
588  const recob::Hit& hit = **ihit;
589 
590  // use the HitToXYZ and Geometry::PositionToTPC
591  // to figure out which drift volume the hit originates from
592  std::vector<double> hitOrigin = this->HitToXYZ(clockData, *ihit);
593  unsigned int cstat = 0;
594  unsigned int tpc = 0;
595  const double worldLoc[3] = {hitOrigin[0], hitOrigin[1], hitOrigin[2]};
596  fGeom->PositionToTPC(worldLoc, tpc, cstat);
597 
598  if (hit.WireID().Cryostat == cstat && hit.WireID().TPC == tpc) {
599  ++numHits[cstat][tpc][hit.WireID().Plane];
600  hitWeight[cstat][tpc][hit.WireID().Plane] = hit.Integral();
601  hitPos[cstat][tpc][hit.WireID().Plane] = hitOrigin;
602  }
603  }
604 
605  // loop over the vectors we made and find the average position for the hits
606  // in the future we might take a weighted average
607  int nhits = 0;
608  xyz[0] = 0.;
609  xyz[1] = 0.;
610  xyz[2] = 0.;
611  for (size_t c = 0; c < numHits.size(); ++c) {
612  for (size_t t = 0; t < numHits[c].size(); ++t) {
613  for (size_t p = 0; p < numHits[c][t].size(); ++p) {
614 
615  if (numHits[c][t][p] == 1) {
616  ++nhits;
617  xyz[0] += hitPos[c][t][p][0];
618  xyz[1] += hitPos[c][t][p][1];
619  xyz[2] += hitPos[c][t][p][2];
620  }
621 
622  } // end loop over planes
623  } // end loop over tpcs
624  } // end loop over cryostats
625 
626  // get the average position
627  if (nhits < 1)
628  throw cet::exception("BackTracker")
629  << "No hits to determine originating location from truth\n";
630 
631  xyz[0] /= nhits;
632  xyz[1] /= nhits;
633  xyz[2] /= nhits;
634 
635  // Done.
636  return xyz;
637  }
638 
639 } // End namespace cheat
TrackID_t trackID
Geant4 supplied track ID.
Definition: SimChannel.h:116
const geo::GeometryCore * fGeom
Definition: BackTracker.h:263
std::vector< art::Ptr< sim::SimChannel > > fSimChannels
Definition: BackTracker.h:271
std::vector< std::vector< art::Ptr< recob::Hit > > > TrackIdsToHits_Ps(detinfo::DetectorClocksData const &clockData, std::vector< int > const &tkIds, std::vector< art::Ptr< recob::Hit >> const &hitsIn) const
Definition: BackTracker.cc:262
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
art::Ptr< sim::SimChannel > FindSimChannelPtr(raw::ChannelID_t channel) const
Returns the cached sim::SimChannel on the specified channel.
Definition: BackTracker.cc:108
std::vector< sim::TrackIDE > HitToEveTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
Definition: BackTracker.cc:201
geo::WireID WireID() const
Definition: Hit.h:233
BackTracker(const fhiclConfig &config, const cheat::ParticleInventory *partInv, const geo::GeometryCore *geom)
Definition: BackTracker.cc:25
pdgs p
Definition: selectors.fcl:22
float numElectrons
number of electrons from the particle detected on the wires
Definition: SimChannel.h:30
const double fMinHitEnergyFraction
Definition: BackTracker.h:267
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:212
static constexpr bool
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
Definition: Hit.h:224
process_name hit
Definition: cheaterreco.fcl:51
float energy
energy from the particle with this trackID [MeV]
Definition: SimChannel.h:29
unsigned int Ncryostats() const
Returns the number of cryostats in the detector.
pure virtual base interface for detector clocks
std::vector< sim::TrackIDE > HitToTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
Definition: BackTracker.cc:178
geo::TPCGeo const & PositionToTPC(geo::Point_t const &point) const
Returns the TPC at specified location.
3-dimensional objects, potentially hits, clusters, prongs, etc.
Definition: geo_types.h:135
process_name gaushit a
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Access the description of detector geometry.
T abs(T value)
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
std::vector< double > HitToXYZ(detinfo::DetectorClocksData const &clockData, const recob::Hit &hit) const
Definition: BackTracker.cc:409
Ionization at a point of the TPC sensitive volume.
Definition: SimChannel.h:86
static const int NoParticleId
Definition: sim.h:21
art::Ptr< sim::SimChannel > FindSimChannel(raw::ChannelID_t channel) const
Returns the cached sim::SimChannel on the specified channel.
Definition: BackTracker.cc:122
std::vector< art::Ptr< recob::Hit > > TrackIdToHits_Ps(detinfo::DetectorClocksData const &clockData, int tkId, std::vector< art::Ptr< recob::Hit >> const &hitsIn) const
Definition: BackTracker.cc:235
std::set< int > GetSetOfTrackIds() const
Definition: BackTracker.h:242
double HitCollectionPurity(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit >> const &hits) const
Definition: BackTracker.cc:417
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
float energyFrac
fraction of hit energy from the particle with this trackID
Definition: SimChannel.h:28
const cheat::ParticleInventory * fPartInv
Definition: BackTracker.h:262
process_name tightIsolTest check
const double fHitTimeRMS
Definition: BackTracker.h:269
PlaneID_t Plane
Index of the plane within its TPC.
Definition: geo_types.h:493
Description of geometry of one entire detector.
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
Definition of data types for geometry description.
float PeakTimeMinusRMS(float sigmas=+1.) const
Definition: Hit.h:239
unsigned int NTPC(unsigned int cstat=0) const
Returns the total number of TPCs in the specified cryostat.
std::vector< sim::TrackIDE > ChannelToTrackIDEs(detinfo::DetectorClocksData const &clockData, raw::ChannelID_t channel, const double hit_start_time, const double hit_end_time) const
Definition: BackTracker.cc:131
std::set< int > GetSetOfEveIds() const
Definition: BackTracker.h:247
double HitChargeCollectionPurity(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit >> const &hits) const
Definition: BackTracker.cc:437
int trackID
Geant4 supplied trackID.
Definition: SimChannel.h:27
Contains all timing reference information for the detector.
std::vector< int > HitToTrackIds(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
Definition: BackTracker.cc:188
double HitCollectionEfficiency(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit >> const &hits, std::vector< art::Ptr< recob::Hit >> const &allhits, geo::View_t const &view) const
Definition: BackTracker.cc:458
do i e
std::vector< const sim::IDE * > TrackIdToSimIDEs_Ps(int const &id) const
Definition: BackTracker.cc:63
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
float PeakTimePlusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:236
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:406
std::vector< sim::IDE > HitToAvgSimIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
Definition: BackTracker.cc:305
int TrackIdToEveTrackId(const int &tid) const
Ionization energy from a Geant4 track.
Definition: SimChannel.h:26
raw::ChannelID_t Channel() const
ID of the readout channel the hit was extracted from.
Definition: Hit.h:230
std::vector< double > SimIDEsToXYZ(std::vector< sim::IDE > const &ides) const
Definition: BackTracker.cc:376
double HitChargeCollectionEfficiency(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit >> const &hits, std::vector< art::Ptr< recob::Hit >> const &allhits, geo::View_t const &view) const
Definition: BackTracker.cc:497
double TPCTick2TDC(double const tick) const
Tools and modules for checking out the basics of the Monte Carlo.
std::vector< const sim::IDE * > HitToSimIDEs_Ps(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
Definition: BackTracker.cc:320
std::vector< double > SpacePointHitsToWeightedXYZ(detinfo::DetectorClocksData const &clockData, std::vector< art::Ptr< recob::Hit >> const &hits) const
Definition: BackTracker.cc:566