LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
BackTracker.cc
Go to the documentation of this file.
1 //
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 //
12 
13 //Includes
14 #include "BackTracker.h"
17 
24 
25 
26 namespace cheat{
27 
28  //-----------------------------------------------------------------------
29  BackTracker::BackTracker(const fhiclConfig& config, const cheat::ParticleInventory* partInv, const geo::GeometryCore* geom, const detinfo::DetectorClocks* detClock )
30  :fPartInv (partInv),
31  fGeom (geom),
32  fDetClocks(detClock),
33  fG4ModuleLabel(config.G4ModuleLabel()),
34  fHitLabel(config.DefaultHitModuleLabel()),
35  fMinHitEnergyFraction(config.MinHitEnergyFraction())
36  {
37  }
38 
39  //-----------------------------------------------------------------------
41  :fPartInv (partInv),
42  fGeom (geom),
43  fDetClocks(detClock),
44  fG4ModuleLabel (pSet.get<art::InputTag>("G4ModuleLabel", "largeant")),
45  fHitLabel (pSet.get<art::InputTag>("DefaultHitModuleLabel", "hitfd")),
46  fMinHitEnergyFraction(pSet.get<double> ("MinHitEnergyFraction", 0.010))
47  {
48  }
49 
50  //-----------------------------------------------------------------------
52  fSimChannels.clear();
53 // fAllHitList.clear();
54  }
55 
56  //-----------------------------------------------------------------------
57  const std::vector< const sim::IDE* > BackTracker::TrackIdToSimIDEs_Ps( int const& id ) const{
58  std::vector< const sim::IDE* > ideps;
59  for(size_t sc = 0; sc < fSimChannels.size(); ++sc){
60  const auto & tdcidemap = fSimChannels[sc]->TDCIDEMap(); //This returns a reference.
61  // loop over the IDEMAP
62  for(auto mapitr = tdcidemap.begin(); mapitr != tdcidemap.end(); mapitr++){
63  // loop over the vector of IDE objects.
64  const std::vector<sim::IDE>& idevec = (*mapitr).second; //note, mapitr.second returns the actual data from the map, not a copy
65  for(size_t iv = 0; iv < idevec.size(); ++iv){
66  //const sim::IDE* const idep = &idevec[iv];
67  //if( abs(idevec[iv].trackID) == id) continue;
68  //ideps.push_back(idep);
69  if( abs(idevec[iv].trackID) == id) ideps.push_back(&(idevec[iv]));
70  }//end for index in idevec
71  } // end loop over map from sim::SimChannel
72  } // end loop over sim::SimChannels
73  return ideps;
74  }
75 
76 
77  //-----------------------------------------------------------------------
78  const std::vector<const sim::IDE* > BackTracker::TrackIdToSimIDEs_Ps (int const& id, const geo::View_t view) const
79  {
80  std::vector<const sim::IDE*> ide_Ps;
82  if (fGeom->View(sc->Channel()) != view) continue;
83 
84  // loop over the IDEMAP
85  for(const auto & item : sc->TDCIDEMap()){
86 
87  // loop over the vector of IDE objects.
88  for(const sim::IDE & ide : item.second){
89  if (abs(ide.trackID) == id) ide_Ps.push_back(&ide);
90  }
91  } // end loop over map from sim::SimChannel
92  } // end loop over sim::SimChannels
93 
94  return ide_Ps;
95  }
96 
97 
98  //-----------------------------------------------------------------------
101  auto ilb = std::lower_bound(fSimChannels.begin(),fSimChannels.end(),channel,[](art::Ptr<sim::SimChannel> a, raw:: ChannelID_t channel) {return(a->Channel()<channel);});
102  if (ilb != fSimChannels.end())
103  if ( (*ilb)->Channel() == channel) {chan = *ilb;}
104  if(!chan)
105  throw cet::exception("BackTracker") << "No sim::SimChannel corresponding "
106  << "to channel: " << channel << "\n";
107  return chan;
108  }
109 
110 
111  //-----------------------------------------------------------------------
112  const std::vector< sim::TrackIDE > BackTracker::ChannelToTrackIDEs(raw::ChannelID_t channel, const double hit_start_time, const double hit_end_time) const{
113  std::vector< sim::TrackIDE > trackIDEs;
114  double totalE=0.;
115  try{
116  art::Ptr<sim::SimChannel> schannel = this->FindSimChannel(channel);
117 
118  // loop over the electrons in the channel and grab those that are in time
119  // with the identified hit start and stop times
120  int start_tdc = fDetClocks->TPCTick2TDC( hit_start_time );
121  int end_tdc = fDetClocks->TPCTick2TDC( hit_end_time );
122  if(start_tdc<0) start_tdc = 0;
123  if(end_tdc<0) end_tdc = 0;
124  std::vector<sim::IDE> simides = schannel->TrackIDsAndEnergies(start_tdc, end_tdc);
125 
126  // first get the total energy represented by all track ids for
127  // this channel and range of tdc values
128  for(size_t e = 0; e < simides.size(); ++e)
129  totalE += simides[e].energy;
130 
131 
132  // protect against a divide by zero below
133  if(totalE < 1.e-5) totalE = 1.;
134 
135  // loop over the entries in the map and fill the input vectors
136 
137  for(size_t e = 0; e < simides.size(); ++e){
138 
139  if(simides[e].trackID == sim::NoParticleId) continue;
140 
141  sim::TrackIDE info;
142  info.trackID = simides[e].trackID;
143  info.energyFrac = simides[e].energy/totalE;
144  info.energy = simides[e].energy;
145 
146  trackIDEs.push_back(info);
147 
148  }
149  }// end try
150  catch(cet::exception e){
151  mf::LogWarning("BackTracker") << "caught exception \n"
152  << e;
153  }
154 
155  return trackIDEs;
156 
157  }
158 
159 
160  //-----------------------------------------------------------------------
161  const std::vector< sim::TrackIDE > BackTracker::HitToTrackIDEs( recob::Hit const& hit) const {
162  std::vector< sim::TrackIDE > trackIDEs;
163  const double start = hit.PeakTimeMinusRMS();
164  const double end = hit.PeakTimePlusRMS();
165  trackIDEs = this->ChannelToTrackIDEs(hit.Channel(), start, end);
166  return trackIDEs;
167  }
168 
169 
170  //-----------------------------------------------------------------------
171  const std::vector< int > BackTracker::HitToTrackIds(recob::Hit const& hit) const {
172  std::vector< int > retVec;
173  for(auto const trackIDE : this->HitToTrackIDEs( hit ) ){
174  retVec.push_back( trackIDE.trackID );
175  }
176  return retVec;
177  }
178 
179  //These don't exist in the event. They are generated on the fly.
180  //-----------------------------------------------------------------------
181  const std::vector<sim::TrackIDE> BackTracker::HitToEveTrackIDEs( recob::Hit const& hit) const{
182  std::vector<sim::TrackIDE> eveIDEs;
183  std::vector<sim::TrackIDE> trackIDEs = this->HitToTrackIDEs(hit);
184  std::map<int, double> eveToEMap;
185  double totalE=0.0;
186  for (const auto& trackIDE : trackIDEs){
187  eveToEMap[fPartInv->TrackIdToEveTrackId(trackIDE.trackID)] += trackIDE.energy;
188  totalE+=trackIDE.energy;
189  }//End for trackIDEs
190  eveIDEs.reserve(eveToEMap.size());
191  for(const auto& eveToE : eveToEMap){
192  sim::TrackIDE eveTrackIDE_tmp;
193  eveTrackIDE_tmp.trackID = eveToE.first;
194  eveTrackIDE_tmp.energy = eveToE.second;
195  eveTrackIDE_tmp.energyFrac = (eveTrackIDE_tmp.energy)/(totalE);
196  eveIDEs.push_back(eveTrackIDE_tmp);
197  }//END eveToEMap loop
198  return eveIDEs;
199  }
200 
201  //-----------------------------------------------------------------------
202  std::vector < art::Ptr< recob::Hit > > BackTracker::TrackIdToHits_Ps( const int& tkId, std::vector<art::Ptr<recob::Hit>> const& hitsIn ) const{
203  // returns a subset of the hits in the hitsIn collection that are matched
204  // to the given track
205 
206  // temporary vector of TrackIds and Ptrs to hits so only one
207  // loop through the (possibly large) hitsIn collection is needed
208  std::vector< art::Ptr<recob::Hit>> hitList;
209  std::vector<sim::TrackIDE> trackIDE;
210  for(auto itr = hitsIn.begin(); itr != hitsIn.end(); ++itr) {
211  trackIDE.clear();
212  art::Ptr<recob::Hit> const& hit = *itr;
213  trackIDE = this->ChannelToTrackIDEs( hit->Channel(), hit->PeakTimeMinusRMS(), hit->PeakTimePlusRMS());
214  for(auto itr_trakIDE = trackIDE.begin(); itr_trakIDE != trackIDE.end(); ++itr_trakIDE) {
215  if(itr_trakIDE->trackID == tkId && itr_trakIDE->energyFrac > fMinHitEnergyFraction)
216  hitList.push_back( hit);
217  } // itr_trakIDE
218  } // itr
219  return hitList;
220  }
221 
222 
223  //-----------------------------------------------------------------------
224  //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.
225  std::vector< std::vector< art::Ptr<recob::Hit> > > BackTracker::TrackIdsToHits_Ps( std::vector<int> const& tkIds, std::vector< art::Ptr< recob::Hit > > const& hitsIn ) const{
226  // returns a subset of the hits in the hitsIn collection that are matched
227  // to MC particles listed in tkIds
228 
229  // temporary vector of TrackIds and Ptrs to hits so only one
230  // loop through the (possibly large) hitsIn collection is needed
231  std::vector<std::pair<int, art::Ptr<recob::Hit>>> hitList;
232  std::vector<sim::TrackIDE> tids;
233  for(auto itr = hitsIn.begin(); itr != hitsIn.end(); ++itr) {
234  tids.clear();
235  art::Ptr<recob::Hit> const& hit = *itr;
236  tids=this->ChannelToTrackIDEs( hit->Channel(), hit->PeakTimeMinusRMS(), hit->PeakTimePlusRMS());
237  for(auto itid = tids.begin(); itid != tids.end(); ++itid) {
238  for(auto itkid = tkIds.begin(); itkid != tkIds.end(); ++itkid) {
239  if(itid->trackID == *itkid) {
240  if(itid->energyFrac > fMinHitEnergyFraction)
241  hitList.push_back(std::make_pair(*itkid, hit));
242  }
243  } // itkid
244  } // itid
245  } // itr
246 
247  // now build the truHits vector that will be returned to the caller
248  std::vector<std::vector<art::Ptr<recob::Hit>>> truHits;
249  // temporary vector containing hits assigned to one MC particle
250  std::vector<art::Ptr<recob::Hit>> tmpHits;
251  for(auto itkid = tkIds.begin(); itkid != tkIds.end(); ++itkid) {
252  tmpHits.clear();
253  for(auto itr = hitList.begin(); itr != hitList.end(); ++itr) {
254  if(*itkid == (*itr).first) tmpHits.push_back((*itr).second);
255  }
256  truHits.push_back(tmpHits);
257  }
258  return truHits;
259  }
260 
261  //-----------------------------------------------------------------------
262  //Cannot be returned as a pointer, as these IDEs do not exist in the event. They are constructed on the fly.
263  const std::vector< sim::IDE > BackTracker::HitToAvgSimIDEs (recob::Hit const& hit) const{
264  // Get services.
265 
266  int start_tdc = fDetClocks->TPCTick2TDC( hit.PeakTimeMinusRMS() );
267  int end_tdc = fDetClocks->TPCTick2TDC( hit.PeakTimePlusRMS() );
268  if(start_tdc<0) start_tdc = 0;
269  if(end_tdc<0) end_tdc = 0;
270 
271  return (this->FindSimChannel(hit.Channel()))->TrackIDsAndEnergies(start_tdc, end_tdc);
272  }
273 
274  //-----------------------------------------------------------------------
275  const std::vector< const sim::IDE* > BackTracker::HitToSimIDEs_Ps (recob::Hit const& hit) const{
276  std::vector< const sim::IDE* > retVec;
277 // const auto start_tdc = hit.PeakTimeMinusRMS();
278 // const auto end_tdc = hit.PeakTimePlusRMS();
279  int start_tdc = fDetClocks->TPCTick2TDC( hit.PeakTimeMinusRMS() );
280  int end_tdc = fDetClocks->TPCTick2TDC( hit.PeakTimePlusRMS() );
281  if(start_tdc<0) start_tdc = 0;
282  if(end_tdc<0) end_tdc = 0;
283 
284  if(start_tdc > end_tdc){throw;}
285 
286 /* auto sc = this->FindSimChannel(hit.Channel());
287  auto tdcidemap = sc->TDCIDEMap();
288  auto& tdcidemapref = sc->TDCIDEMap();*/
289 
290  const std::vector< std::pair<unsigned short, std::vector<sim::IDE>> >& tdcIDEMap = (this->FindSimChannel(hit.Channel()))->TDCIDEMap(); //This in fact does not return a map. It returns a vector... with no guarantee that it is sorted...
291  std::vector<const std::pair<unsigned short, std::vector<sim::IDE>>*> tdcIDEMap_SortedPointers;
292  for ( auto& pair : tdcIDEMap ){
293  tdcIDEMap_SortedPointers.push_back(&pair);
294  }
295 
296  //Sort a vector of pointers to the TDCIDEs
297  /*
298  auto pairSort = [](auto& a, auto& b) { return a.first < b.first ; } ;
299  if( !std::is_sorted( tdcIDEMap.begin(), tdcIDEMap.end(), pairSort)) {
300  std::cout<<"AlreadySorted\n";
301  // std::sort (tdcIDEMap.begin(), tdcIDEMap.end(), pairSort);
302  }else{
303  throw;
304  }*/
305 
306  //This is a bunch of extra steps, due to needing a vector we can sort, and needing those items in the sorted vector to be the items from the sim channels (so a pointer to the IDEs inside the sim channels can be made). The work around is to make a vector of pointers to IDEs inside the TDCIDEMap (which is a constant reference to the fTDCIDEs in the SimChannel.)
307  auto pairSort = [](auto& a, auto& b) { return a->first < b->first ; } ;
308  if( !std::is_sorted( tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), pairSort)) {
309  std::sort (tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), pairSort);
310  }
311 
312 
313  std::vector<sim::IDE> dummyVec; //I need something to stick in a pair to compare pair<tdcVal, IDE>. This is an otherwise useless "hack".
314  std::pair<double, std::vector<sim::IDE>> start_tdcPair = std::make_pair(start_tdc,dummyVec); //This pair is a "hack" to make my comparison work for lower and upper bound.
315  std::pair<double, std::vector<sim::IDE>> end_tdcPair = std::make_pair(end_tdc,dummyVec);
316  //const std::vector<std::pair<unsigned short, std::vector<sim::IDE> > >::iterator //iterator to the first interesting IDE
317  auto start_tdcPair_P = &start_tdcPair;
318  auto end_tdcPair_P = &end_tdcPair;
319  auto
320  mapFirst = std::lower_bound(tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), start_tdcPair_P, pairSort);
321  //mapFirst = std::lower_bound(tdcIDEMap.begin(), tdcIDEMap.end(), start_tdcPair, pairSort);
322  //const std::vector<std::pair<unsigned short, std::vector<sim::IDE> > >::iterator //iterator to just after the last interesting IDE
323  auto
324  mapLast = std::upper_bound(tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), end_tdcPair_P, pairSort);
325  for( auto& mapitr = mapFirst; mapitr != mapLast; ++mapitr ){
326  for( auto& ide : (*mapitr)->second){
327  retVec.push_back(&ide);
328  //std::cout<<"Dumping Selected IDEs from tdcIDEMap:\nTrackID: "<<ide.trackID<<"\nnumElectrons: "<<ide.numElectrons<<"\nenergy: "<<ide.energy<<"\nXYZ: "<<ide.x<<" "<<ide.y<<" "<<ide.z<<"\n";
329  } //Add all interesting IDEs to the retVec
330  }
331 /* for( auto ide : retVec){
332  std::cout<<"Dumping Selected IDEs from retVec:\nTrackID: "<<ide->trackID<<"\nnumElectrons: "<<ide->numElectrons<<"\nenergy: "<<ide->energy<<"\nXYZ: "<<ide->x<<" "<<ide->y<<" "<<ide->z<<"\n";
333  }*/
334  return retVec;
335  }
336 
337  //------------------------------------------------------------------------------
338  const std::vector<double> BackTracker::SimIDEsToXYZ(std::vector<sim::IDE> const& ides) const
339  {
340  std::vector<double> xyz(3,0.0);
341  double w = 0.0;
342  for( auto const& ide : ides){
343  double weight=ide.numElectrons;
344  w += weight;
345  xyz[0] += (weight*ide.x);
346  xyz[1] += (weight*ide.y);
347  xyz[2] += (weight*ide.z);
348  }
349  if(w<1.e-5)
350  throw cet::exception("BackTracker")<<"No sim::IDEs providing non-zero number of electrons"
351  << " can't determine originating location from truth\n";
352  xyz[0] = xyz[0]/w;
353  xyz[1] = xyz[1]/w;
354  xyz[2] = xyz[2]/w;
355  return xyz;
356  }
357 
358  //-------------------------------------------------------------------------------
359  const std::vector<double> BackTracker::SimIDEsToXYZ( std::vector< const sim::IDE* > const& ide_Ps) const{
360  std::vector<sim::IDE> ides;
361  for(auto ide_P : ide_Ps ){ ides.push_back(*ide_P);}
362  return this->SimIDEsToXYZ(ides);
363  }
364 
365 
366  //--------------------------------------------------------------------------------
367  const std::vector<double> BackTracker::HitToXYZ(const recob::Hit& hit) const{
368  std::vector<const sim::IDE*> ide_Ps = this->HitToSimIDEs_Ps(hit);
369  return this->SimIDEsToXYZ(ide_Ps);
370  }
371 
372  //-----------------------------------------------------------------------------------
373  const double BackTracker::HitCollectionPurity( std::set<int> const& trackIds, std::vector< art::Ptr<recob::Hit> > const& hits) const {
374  int desired =0;
375  for( const auto& hit : hits ){
376  std::vector<sim::TrackIDE> hitTrackIDEs=this->HitToTrackIDEs(hit);
377  for(const auto& tIDE : hitTrackIDEs){
378  if(trackIds.find(tIDE.trackID)!=trackIds.end()){
379  ++desired;
380  break;
381  }//End if TID Found
382  }//END for trackIDE in TrackIDEs
383  }//End for hit in hits
384  if(hits.size()>0){return double(double(desired)/double(hits.size()));}
385  return 0;
386  }
387 
388  //-----------------------------------------------------------------------------------
389  const double BackTracker::HitChargeCollectionPurity( std::set<int> const& trackIds, std::vector< art::Ptr<recob::Hit> > const& hits) const {
390  double totalCharge=0.,desired=0.;
391  for(const auto& hit : hits){
392  totalCharge+=hit->Integral();
393  std::vector<sim::TrackIDE> trackIDEs= this->HitToTrackIDEs(hit);
394  for(const auto& trackIDE : trackIDEs){
395  if(trackIds.find(trackIDE.trackID)!=trackIds.end()){
396  desired+=hit->Integral();
397  break;
398  }//End if trackId in trackIds.
399  }//End for trackIDE in trackIDEs
400  }//End for Hit in Hits
401  if(totalCharge>0.0){return (desired/totalCharge);}
402  return 0.0;
403  }
404 
405  //-----------------------------------------------------------------------------------
406  const double BackTracker::HitCollectionEfficiency( std::set<int> const& trackIds,
408  std::vector< art::Ptr<recob::Hit> > const& allHits,
409  geo::View_t const& view) const {
410 
411  int desired=0,total=0;
412 
413  for( const auto& hit : hits){
414  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(hit);
415  for( const auto& trackIDE : hitTrackIDEs){
416  if( trackIds.find(trackIDE.trackID)!=trackIds.end() && trackIDE.energyFrac >= fMinHitEnergyFraction){
417  ++desired;
418  break;
419  }//End if trackID in trackIds.
420  }//end for trackIDE in TrackIDEs
421  }//end for hit in hits
422 
423  for( const auto& hit : allHits){
424  if(hit->View()!=view && view != geo::k3D) {continue;}//End if hit.view = view or view = geo::k3D
425  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(hit);
426  for( const auto& hitIDE : hitTrackIDEs){
427  if(trackIds.find(hitIDE.trackID)!=trackIds.end() && hitIDE.energyFrac>=fMinHitEnergyFraction){
428  ++total;
429  break;
430  }
431  }//END for all IDEs in HitTrackIDEs.
432  }//end for hit in allHits.
433  if(total >= 0){ return double(double(desired)/double(total));}
434  return 0.;
435  }
436 
437  //-----------------------------------------------------------------------------------
438  const double BackTracker::HitChargeCollectionEfficiency(std::set<int> trackIds,
440  std::vector< art::Ptr<recob::Hit> > const& allHits,
441  geo::View_t const& view) const{
442  double desired=0.,total=0.;
443  for( const auto& hit : hits){
444  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(hit);
445  for(const auto& hitIDE : hitTrackIDEs){
446  if(trackIds.find(hitIDE.trackID) != trackIds.end() && hitIDE.energyFrac >= fMinHitEnergyFraction){
447  desired+=hit->Integral();
448  break;
449  }//end if hit id matches and energy sufficient.
450  }//End for IDE in HitTrackIDEs.
451  }//End for hit in hits.
452 
453  for( const auto& hit : allHits ){
454  if(hit->View() != view && view != geo::k3D){ continue; }
455  std::vector<sim::TrackIDE> hitTrackIDEs = this->HitToTrackIDEs(hit);
456  for( const auto& hitIDE : hitTrackIDEs ){
457  if(trackIds.find(hitIDE.trackID) != trackIds.end() && hitIDE.energyFrac >= fMinHitEnergyFraction){
458  total += hit->Integral();
459  break;
460  }//end if hit matches
461  }//end for ide in ides
462  }//End for hit in allHits
463 
464  if(total>0.) {return desired/total;}
465  return 0.;
466 
467  }
468 
469 
470  //-----------------------------------------------------------------------------------
472  std::set<int> tids;
473  for( const auto& hit : hits){
474  const double start = hit->PeakTimeMinusRMS();
475  const double end = hit->PeakTimePlusRMS();
476  std::vector<sim::TrackIDE> trackIDEs = this->ChannelToTrackIDEs(hit->Channel(), start, end);
477  for(const auto& ide : trackIDEs) {
478  tids.insert(ide.trackID);
479  }//End for TrackIDEs
480  }//End for hits
481  return tids;
482  }//End GetSetOfTrackIds
483 
484  //-----------------------------------------------------------------------------------
485  const std::set<int> BackTracker::GetSetOfEveIds( std::vector< art::Ptr< recob::Hit > > const& hits ) const {
486  std::set<int> eveIds;
487  for(const auto& hit : hits){
488  const std::vector<sim::TrackIDE> ides = this->HitToEveTrackIDEs(hit);
489  for(const auto& ide : ides){eveIds.insert(ide.trackID);}//end ides
490  }//End for hits
491  return eveIds;
492  }
493 
494 
495  //This function definitely needs a new implimentation. There must be abetter way that so many loops.
497  std::vector<double> xyz(3,-99999.9);
498  std::vector< std::vector<std::vector<int>>> numHits (fGeom->Ncryostats());
499  std::vector< std::vector<std::vector<double>>> hitWeight (fGeom->Ncryostats());
500  std::vector< std::vector<std::vector<std::vector<double>>>> hitPos (fGeom->Ncryostats());
501  //Do we need to resize everything...
502  for(size_t c = 0; c < numHits.size(); ++c){
503  numHits[c].resize( fGeom->NTPC(c) );
504  hitWeight[c].resize( fGeom->NTPC(c) );
505  hitPos[c].resize( fGeom->NTPC(c) );
506  for(size_t t = 0; t < numHits[c].size(); ++t){
507  numHits[c][t].resize( fGeom->Nplanes(t, c) );
508  hitWeight[c][t].resize( fGeom->Nplanes(t, c) );
509  hitPos[c][t].resize( fGeom->Nplanes(t, c) );
510  }
511  }
512 
513  for(art::PtrVector<recob::Hit>::const_iterator ihit = hits.begin(); ihit != hits.end(); ++ihit) {
514 
515  const recob::Hit& hit = **ihit;
516 
517  // use the HitToXYZ and Geometry::PositionToTPC
518  // to figure out which drift volume the hit originates from
519  std::vector<double> hitOrigin = this->HitToXYZ(*ihit);
520  unsigned int cstat = 0;
521  unsigned int tpc = 0;
522  const double worldLoc[3] = {hitOrigin[0], hitOrigin[1], hitOrigin[2]};
523  fGeom->PositionToTPC(worldLoc, tpc, cstat);
524 
525  if(hit.WireID().Cryostat == cstat && hit.WireID().TPC == tpc){
526  ++numHits[cstat][tpc][hit.WireID().Plane];
527  hitWeight[cstat][tpc][hit.WireID().Plane] = hit.Integral();
528  hitPos[cstat][tpc][hit.WireID().Plane] = hitOrigin;
529  }
530 
531  }
532 
533  // loop over the vectors we made and find the average position for the hits
534  // in the future we might take a weighted average
535  int nhits = 0;
536  xyz[0] = 0.;
537  xyz[1] = 0.;
538  xyz[2] = 0.;
539  for(size_t c = 0; c < numHits.size(); ++c){
540  for(size_t t = 0; t < numHits[c].size(); ++t){
541  for(size_t p = 0; p < numHits[c][t].size(); ++p){
542 
543  if(numHits[c][t][p] == 1) {
544  ++nhits;
545  xyz[0] += hitPos[c][t][p][0];
546  xyz[1] += hitPos[c][t][p][1];
547  xyz[2] += hitPos[c][t][p][2];
548  }
549 
550  } // end loop over planes
551  } // end loop over tpcs
552  } // end loop over cryostats
553 
554  // get the average position
555  if(nhits < 1)
556  throw cet::exception("BackTracker") << "No hits to determine originating location from truth\n";
557 
558 
559  xyz[0] /= nhits;
560  xyz[1] /= nhits;
561  xyz[2] /= nhits;
562 
563  // Done.
564  return xyz;
565 
566  }
567 
568 
569 }//End namespace cheat
TrackID_t trackID
Geant4 supplied track ID.
Definition: SimChannel.h:115
const art::InputTag fHitLabel
Definition: BackTracker.h:160
const std::vector< double > SpacePointHitsToWeightedXYZ(std::vector< art::Ptr< recob::Hit >> const &hits) const
Definition: BackTracker.cc:496
Utilities related to art service access.
const geo::GeometryCore * fGeom
Definition: BackTracker.h:157
std::vector< art::Ptr< sim::SimChannel > > fSimChannels
Definition: BackTracker.h:164
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
const detinfo::DetectorClocks * fDetClocks
Definition: BackTracker.h:158
geo::WireID WireID() const
Initial tdc tick for hit.
Definition: Hit.h:234
const std::set< int > GetSetOfEveIds() const
Definition: BackTracker.h:148
const std::vector< sim::TrackIDE > ChannelToTrackIDEs(raw::ChannelID_t channel, const double hit_start_time, const double hit_end_time) const
Definition: BackTracker.cc:112
const double fMinHitEnergyFraction
Definition: BackTracker.h:161
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:130
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
Definition: Hit.h:225
float energy
energy from the particle with this trackID [MeV]
Definition: SimChannel.h:31
unsigned int Ncryostats() const
Returns the number of cryostats in the detector.
const std::vector< sim::TrackIDE > HitToTrackIDEs(recob::Hit const &hit) const
Definition: BackTracker.cc:161
const double HitCollectionEfficiency(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:406
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:82
const std::vector< int > HitToTrackIds(recob::Hit const &hit) const
Definition: BackTracker.cc:171
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Access the description of detector geometry.
const std::vector< const sim::IDE * > TrackIdToSimIDEs_Ps(int const &id) const
Definition: BackTracker.cc:57
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
void hits()
Definition: readHits.C:15
Ionization at a point of the TPC sensitive volume.
Definition: SimChannel.h:87
static const int NoParticleId
Definition: sim.h:28
art::Ptr< sim::SimChannel > FindSimChannel(raw::ChannelID_t channel) const
Definition: BackTracker.cc:99
double energy
Definition: plottest35.C:25
const std::vector< double > HitToXYZ(const recob::Hit &hit) const
Definition: BackTracker.cc:367
float energyFrac
fraction of hit energy from the particle with this trackID
Definition: SimChannel.h:30
std::vector< std::vector< art::Ptr< recob::Hit > > > TrackIdsToHits_Ps(std::vector< int > const &tkIds, std::vector< art::Ptr< recob::Hit > > const &hitsIn) const
Definition: BackTracker.cc:225
const double HitChargeCollectionPurity(std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit > > const &hits) const
Definition: BackTracker.cc:389
const cheat::ParticleInventory * fPartInv
Definition: BackTracker.h:156
const std::vector< const sim::IDE * > HitToSimIDEs_Ps(recob::Hit const &hit) const
Definition: BackTracker.cc:275
const std::vector< sim::TrackIDE > HitToEveTrackIDEs(recob::Hit const &hit) const
Definition: BackTracker.cc:181
PlaneID_t Plane
Index of the plane within its TPC.
Definition: geo_types.h:258
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
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:240
Detector simulation of raw signals on wires.
Conversion of times between different formats and references.
Encapsulate the geometry of a wire.
unsigned int NTPC(unsigned int cstat=0) const
Returns the total number of TPCs in the specified cryostat.
data_t::const_iterator const_iterator
Definition: PtrVector.h:61
double weight
Definition: plottest35.C:25
code to link reconstructed objects back to the MC truth information
Definition: BackTracker.cc:26
int trackID
Geant4 supplied trackID.
Definition: SimChannel.h:29
virtual double TPCTick2TDC(double tick) const =0
Converts a TPC time tick into a electronics time tick.
const double HitChargeCollectionEfficiency(std::set< int > 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:438
const std::set< int > GetSetOfTrackIds() const
Definition: BackTracker.h:147
std::vector< sim::IDE > TrackIDsAndEnergies(TDC_t startTDC, TDC_t endTDC) const
Return all the recorded energy deposition within a time interval.
Definition: SimChannel.cxx:178
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
const std::vector< sim::IDE > HitToAvgSimIDEs(recob::Hit const &hit) const
Definition: BackTracker.cc:263
const art::InputTag fG4ModuleLabel
Definition: BackTracker.h:159
HLT enums.
const std::vector< double > SimIDEsToXYZ(std::vector< sim::IDE > const &ides) const
Definition: BackTracker.cc:338
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:49
float PeakTimePlusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:237
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:27
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:203
Float_t e
Definition: plot.C:34
int TrackIdToEveTrackId(const int &tid) const
std::vector< art::Ptr< recob::Hit > > TrackIdToHits_Ps(const int &tkId, std::vector< art::Ptr< recob::Hit > > const &hitsIn) const
Definition: BackTracker.cc:202
Float_t w
Definition: plot.C:23
Ionization energy from a Geant4 track.
Definition: SimChannel.h:28
raw::ChannelID_t Channel() const
ID of the readout channel the hit was extracted from.
Definition: Hit.h:231
Tools and modules for checking out the basics of the Monte Carlo.
const double HitCollectionPurity(std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit > > const &hits) const
Definition: BackTracker.cc:373
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Encapsulate the construction of a single detector plane.
BackTracker(const fhiclConfig &config, const cheat::ParticleInventory *partInv, const geo::GeometryCore *geom, const detinfo::DetectorClocks *detClock)
Definition: BackTracker.cc:29