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