Public Member Functions | Protected Attributes | Private Member Functions | List of all members
gar::cheat::BackTrackerCore Class Reference

#include <BackTrackerCore.h>

Inheritance diagram for gar::cheat::BackTrackerCore:
gar::cheat::BackTracker

Public Member Functions

 BackTrackerCore (fhicl::ParameterSet const &pset)
 
 ~BackTrackerCore ()
 
void AdoptEveIdCalculator (sim::EveIdCalculator *ec)
 
bool HasMC ()
 
bool HasHits ()
 
bool HasCalHits ()
 
bool HasTracks ()
 
bool HasClusters ()
 
sim::ParticleList * GetParticleList ()
 
simb::MCParticleTrackIDToParticle (int const &id) const
 
simb::MCParticleFindMother (simb::MCParticle *const p) const
 
simb::MCParticleFindEve (simb::MCParticle *const p) const
 
simb::MCParticleFindTPCEve (simb::MCParticle *const p) const
 
simb::MCParticleFindTPCEve (int const trackId) const
 
bool IsForebearOf (simb::MCParticle *const forebear, simb::MCParticle *const afterbear) const
 
art::Ptr< simb::MCTruth > const ParticleToMCTruth (simb::MCParticle *const p) const
 
std::vector< HitIDEHitToHitIDEs (art::Ptr< rec::Hit > const &hit) const
 
std::vector< HitIDEHitToHitIDEs (rec::Hit const &hit) const
 
std::vector< art::Ptr< rec::Hit > > ParticleToHits (simb::MCParticle *const p, std::vector< art::Ptr< rec::Hit >> const &allhits, bool checkNeutrals=false) const
 
std::pair< double, double > HitPurity (simb::MCParticle *const p, std::vector< art::Ptr< rec::Hit >> const &hits, bool weightByCharge=false) const
 
std::pair< double, double > HitEfficiency (simb::MCParticle *const p, std::vector< art::Ptr< rec::Hit > > const &hits, std::vector< art::Ptr< rec::Hit > > const &allhits, bool weightByCharge=false) const
 
std::vector< CalIDECaloHitToCalIDEs (art::Ptr< rec::CaloHit > const &hit) const
 
std::vector< CalIDECaloHitToCalIDEs (rec::CaloHit const &hit) const
 
std::vector< art::Ptr< rec::CaloHit > > ParticleToCaloHits (simb::MCParticle *const p, std::vector< art::Ptr< rec::CaloHit >> const &allhits) const
 
std::pair< double, double > CaloHitPurity (simb::MCParticle *const p, std::vector< art::Ptr< rec::CaloHit >> const &hits, bool weightByCharge=false) const
 
std::pair< double, double > CaloHitEfficiency (simb::MCParticle *const p, std::vector< art::Ptr< rec::CaloHit >> const &hits, std::vector< art::Ptr< rec::CaloHit >> const &allhits, bool weightByCharge=false) const
 
std::vector< art::Ptr< rec::Hit > > const TrackToHits (rec::Track *const t)
 
std::vector< art::Ptr< rec::Hit > > const TPCClusterToHits (rec::TPCCluster *const clust)
 
std::vector< const rec::TPCCluster * > const TrackToClusters (rec::Track *const t)
 
std::vector< std::pair< simb::MCParticle *, float > > TrackToMCParticles (rec::Track *const t)
 
double TrackToTotalEnergy (rec::Track *const t)
 
std::vector< art::Ptr< rec::Track > > MCParticleToTracks (simb::MCParticle *const p, std::vector< art::Ptr< rec::Track >> const &tracks)
 
std::vector< art::Ptr< rec::CaloHit > > const ClusterToCaloHits (rec::Cluster *const c)
 
std::vector< std::pair< simb::MCParticle *, float > > ClusterToMCParticles (rec::Cluster *const c)
 
std::vector< art::Ptr< rec::Cluster > > MCParticleToClusters (simb::MCParticle *const p, std::vector< art::Ptr< rec::Cluster >> const &clusters)
 
bool ClusterCreatedMCParticle (simb::MCParticle *const p, rec::Cluster *const c)
 
bool MCParticleCreatedCluster (simb::MCParticle *const p, rec::Cluster *const c)
 

Protected Attributes

bool fHasMC
 
bool fHasHits
 
bool fHasCalHits
 
bool fHasTracks
 
bool fHasClusters
 
int fSTFU
 
const detinfo::DetectorClocksfClocks
 Detector clock information. More...
 
const geo::GeometryCorefGeo
 pointer to the geometry More...
 
bool fDisableRebuild
 for switching off backtracker's rebuild of the MCParticle tables More...
 
std::string fG4ModuleLabel
 label for geant4 module More...
 
std::string fRawTPCDataLabel
 label for TPC readout module More...
 
std::string fRawCaloDataLabel
 label for ECAL readout module More...
 
std::string fRawCaloDataECALInstance
 instance name for the ECAL raw hits More...
 
double fECALtimeResolution
 time resolution for hits in ECAL, nsec. More...
 
double fMinHitEnergyFraction
 min frac of ionization a track has to count in a TPC hit More...
 
double fMinCaloHitEnergyFrac
 min frac of ionization a track has to count in a CaloHit More...
 
std::string fTrackLabel
 label for final track producing module More...
 
std::string fTPCClusterLabel
 label for TPCCluster producing module More...
 
double fTrackFracMCP
 min frac of ionization in a track for matching to an MCParticle More...
 
std::string fClusterLabel
 label for ECAL cluster producing module More...
 
std::string fClusterECALInstance
 instance name for the ECAL clusters More...
 
double fClusterFracMCP
 min frac of ionization in a cluster for matching to an MCParticle More...
 
sim::ParticleList fParticleList
 Maps MCParticle::TrackId() to same MCParticle. More...
 
std::vector< art::Ptr< simb::MCTruth > > fMCTruthList
 all the MCTruths for the event More...
 
std::unordered_map< int, int > fTrackIDToMCTruthIndex
 
std::unordered_map< int, int > * fECALTrackToTPCTrack
 results of previous FindTPCEve calls More...
 
double fInverseVelocity
 inverse drift velocity More...
 
double fLongDiffConst
 longitudinal diffusion constant More...
 
bool fSplitEDeps
 use weights from PRFs to break true energy deposits into channel specific contributions More...
 
std::vector< std::vector< std::pair< const sdp::EnergyDeposit *, float const > > > fChannelToEDepCol
 convenience collections of EnergyDeposits for each channel More...
 
std::unordered_map< raw::CellID_t, std::vector< const sdp::CaloDeposit * > > fCellIDToEDepCol
 convenience collections of EnergyDeposit for each cell More...
 
std::unordered_map< rec::IDNumber, std::vector< art::Ptr< rec::Hit > > > fTrackIDToHits
 Reco track ID to track's hits. More...
 
std::unordered_map< rec::IDNumber, std::vector< const rec::TPCCluster * > > fTrackIDToClusters
 Reco track ID to track's clusters. More...
 
std::unordered_map< rec::IDNumber, std::vector< art::Ptr< rec::Hit > > > fTPCClusterIDToHits
 Reco TPC cluster ID to cluster's hits. More...
 
std::unordered_map< rec::IDNumber, std::vector< art::Ptr< rec::CaloHit > > > fClusterIDToCaloHits
 Reco ECAL cluster ID to CaloHits. More...
 

Private Member Functions

std::vector< HitIDEChannelToHitIDEs (raw::Channel_t const &channel, double const start, double const stop) const
 
std::vector< CalIDECellIDToCalIDEs (raw::CellID_t const &cellID, float const time) const
 
std::vector< simb::MCParticle * > MCPartsInCluster (rec::Cluster *const c)
 
TLorentzVector EnergyDepositToMomentum (const int &trackID, const TLorentzVector &position, size_t &startTrajIndex) const
 

Detailed Description

Definition at line 111 of file BackTrackerCore.h.

Constructor & Destructor Documentation

gar::cheat::BackTrackerCore::BackTrackerCore ( fhicl::ParameterSet const &  pset)
explicit

Definition at line 29 of file BackTrackerCore.cxx.

30  : fClocks(nullptr), fGeo(nullptr) {
31 
32  fClocks = gar::providerFrom<detinfo::DetectorClocksServiceGAr>();
33  fGeo = gar::providerFrom<geo::GeometryGAr>();
34 
35  fDisableRebuild = pset.get<bool >("DisableRebuild", false);
36 
37  fG4ModuleLabel = pset.get<std::string>("G4ModuleLabel", "geant");
38 
39  fRawTPCDataLabel = pset.get<std::string>("RawTPCDataLabel", "daq");
40 
41  fRawCaloDataLabel = pset.get<std::string>("RawCaloDataLabel", "daqsipm");
42  fRawCaloDataECALInstance = pset.get<std::string>("RawCaloDataECALInstance", "ECAL" );
43  fECALtimeResolution = pset.get<double >("ECALtimeResolution", 1.0);
44  fMinHitEnergyFraction = pset.get<double >("MinHitEnergyFraction", 0.1);
45  fMinCaloHitEnergyFrac = pset.get<double >("MinCaloHitEnergyFrac", 0.1);
46 
47  fTrackLabel = pset.get<std::string>("TrackLabel", "track");
48  fTPCClusterLabel = pset.get<std::string>("TPCClusterLabel", "tpccluster");
49  fTrackFracMCP = pset.get<double >("TrackFracMCP", 0.8);
50 
51  fClusterLabel = pset.get<std::string>("ClusterLabel", "calocluster");
52  fClusterECALInstance = pset.get<std::string>("ClusterECALInstance", "ECAL" );
53  fClusterFracMCP = pset.get<double >("ClusterFracMCP", 0.8);
54 
55  fSplitEDeps = pset.get<bool >("SplitEDeps", true);
56 
57  fECALTrackToTPCTrack = new std::unordered_map<int, int>;
58  return;
59  }
double fClusterFracMCP
min frac of ionization in a cluster for matching to an MCParticle
std::string string
Definition: nybbler.cc:12
const detinfo::DetectorClocks * fClocks
Detector clock information.
double fMinHitEnergyFraction
min frac of ionization a track has to count in a TPC hit
std::string fRawCaloDataECALInstance
instance name for the ECAL raw hits
std::string fClusterLabel
label for ECAL cluster producing module
const geo::GeometryCore * fGeo
pointer to the geometry
double fTrackFracMCP
min frac of ionization in a track for matching to an MCParticle
std::string fClusterECALInstance
instance name for the ECAL clusters
std::string fRawTPCDataLabel
label for TPC readout module
std::string fTPCClusterLabel
label for TPCCluster producing module
std::string fTrackLabel
label for final track producing module
std::unordered_map< int, int > * fECALTrackToTPCTrack
results of previous FindTPCEve calls
std::string fG4ModuleLabel
label for geant4 module
bool fSplitEDeps
use weights from PRFs to break true energy deposits into channel specific contributions ...
std::string fRawCaloDataLabel
label for ECAL readout module
double fECALtimeResolution
time resolution for hits in ECAL, nsec.
bool fDisableRebuild
for switching off backtracker&#39;s rebuild of the MCParticle tables
double fMinCaloHitEnergyFrac
min frac of ionization a track has to count in a CaloHit
gar::cheat::BackTrackerCore::~BackTrackerCore ( )

Definition at line 62 of file BackTrackerCore.cxx.

62 {}

Member Function Documentation

void gar::cheat::BackTrackerCore::AdoptEveIdCalculator ( sim::EveIdCalculator *  ec)
inline

Definition at line 118 of file BackTrackerCore.h.

118  {
119  fParticleList.AdoptEveIdCalculator(ec);
120  }
sim::ParticleList fParticleList
Maps MCParticle::TrackId() to same MCParticle.
std::pair< double, double > gar::cheat::BackTrackerCore::CaloHitEfficiency ( simb::MCParticle *const  p,
std::vector< art::Ptr< rec::CaloHit >> const &  hits,
std::vector< art::Ptr< rec::CaloHit >> const &  allhits,
bool  weightByCharge = false 
) const

Definition at line 680 of file BackTrackerCore.cxx.

682  {
683 
684  if (!fHasMC || !fHasHits) {
685  throw cet::exception("BackTrackerCore::CaloHitEfficiency")
686  << "Attempting to backtrack without MC truth or gar::rec::Hit information";
687  }
688 
689  int trackIDin = p->TrackId();
690 
691  float weight;
692  float desired = 0.0;
693  for (auto hit : hits) {
694  std::vector<CalIDE> calIDEs = this->CaloHitToCalIDEs(hit);
695 
696  weight = (weightByCharge) ? hit->Energy() : 1.0;
697 
698  for (auto iCalIDE : calIDEs) {
699  // Don't double count if this hit has > 1 of the desired GEANT track IDs
700  if (iCalIDE.trackID == trackIDin &&
701  iCalIDE.energyFrac >= fMinCaloHitEnergyFrac) {
702  desired += weight;
703  break;
704  }
705  }
706  } // end loop over hits
707 
708  // Next figure out how many hits in the whole collection are associated with this id
709  float total = 0.0;
710  if (desired>0.0) {
711  for (auto hit : allhits) {
712  std::vector<CalIDE> calIDEs = this->CaloHitToCalIDEs(hit);
713 
714  weight = (weightByCharge) ? hit->Energy() : 1.0;
715 
716  for (auto iCalIDE : calIDEs) {
717  // Don't double count if this hit has > 1 of the desired GEANT track IDs
718  if (iCalIDE.trackID == trackIDin &&
719  iCalIDE.energyFrac >= fMinCaloHitEnergyFrac) {
720  total += weight;
721  break;
722  }
723  }
724  } // end loop over all hits
725  }
726 
727  double efficiency = 0.0;
728  double uncertainty = 0.0;
729  if (total > 0.0) {
730  efficiency = desired/total;
731  uncertainty = std::sqrt( desired*(total -desired)/total )/total;
732  }
733  return std::make_pair(efficiency,uncertainty);
734  }
std::vector< CalIDE > CaloHitToCalIDEs(art::Ptr< rec::CaloHit > const &hit) const
weight
Definition: test.py:257
int TrackId() const
Definition: MCParticle.h:210
Detector simulation of raw signals on wires.
double fMinCaloHitEnergyFrac
min frac of ionization a track has to count in a CaloHit
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::pair< double, double > gar::cheat::BackTrackerCore::CaloHitPurity ( simb::MCParticle *const  p,
std::vector< art::Ptr< rec::CaloHit >> const &  hits,
bool  weightByCharge = false 
) const

Definition at line 637 of file BackTrackerCore.cxx.

638  {
639 
640  if (!fHasMC || !fHasHits) {
641  throw cet::exception("BackTrackerCore::CaloHitPurity")
642  << "Attempting to backtrack without MC truth or gar::rec::Hit information";
643  }
644 
645 
646  int trackIDin = p->TrackId();
647 
648  float weight;
649  float desired = 0.0;
650  float total = 0.0;
651  for (auto hit : hits) {
652  std::vector<CalIDE> calIDEs = this->CaloHitToCalIDEs(hit);
653 
654  weight = (weightByCharge) ? hit->Energy() : 1.0;
655  total += weight;
656 
657  for (auto iCalIDE : calIDEs) {
658  // Don't double count if this hit has > 1 of the desired GEANT trackIDs
659  if (iCalIDE.trackID == trackIDin &&
660  iCalIDE.energyFrac >= fMinCaloHitEnergyFrac) {
661  desired += weight;
662  break;
663  }
664  }
665  } // end loop over hits
666 
667  double purity = 0.0;
668  double uncertainty = 0.0;
669  if (total > 0.0) {
670  purity = desired/total;
671  uncertainty = std::sqrt( desired*(total -desired)/total )/total;
672  }
673  return std::make_pair(purity,uncertainty);
674  }
std::vector< CalIDE > CaloHitToCalIDEs(art::Ptr< rec::CaloHit > const &hit) const
weight
Definition: test.py:257
int TrackId() const
Definition: MCParticle.h:210
Detector simulation of raw signals on wires.
double fMinCaloHitEnergyFrac
min frac of ionization a track has to count in a CaloHit
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< CalIDE > gar::cheat::BackTrackerCore::CaloHitToCalIDEs ( art::Ptr< rec::CaloHit > const &  hit) const

Definition at line 511 of file BackTrackerCore.cxx.

511  {
512  if (!fHasMC || !fHasCalHits) {
513  throw cet::exception("BackTrackerCore::CaloHitToCalIDEs")
514  << "Attempting to backtrack without MC truth or gar::rec::CaloHit information";
515  }
516 
517  // Cut on Time for this hit +/- ECAL time resolution given in BackTracker.fcl
518  std::vector<CalIDE> ides;
519  ides = this->CellIDToCalIDEs(hit->CellID(),hit->Time().first);
520  return ides;
521  }
std::vector< CalIDE > CellIDToCalIDEs(raw::CellID_t const &cellID, float const time) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< CalIDE > gar::cheat::BackTrackerCore::CaloHitToCalIDEs ( rec::CaloHit const &  hit) const

Definition at line 525 of file BackTrackerCore.cxx.

525  {
526  if (!fHasMC || !fHasCalHits) {
527  throw cet::exception("BackTrackerCore::CaloHitToCalIDEs")
528  << "Attempting to backtrack without MC truth or gar::rec::CaloHit information";
529  }
530 
531  // Cut on Time for this hit +/- ECALtimeResolution given in BackTracker.fcl
532  std::vector<CalIDE> ides;
533  ides = this->CellIDToCalIDEs(hit.CellID(),hit.Time().first);
534  return ides;
535  }
Detector simulation of raw signals on wires.
std::vector< CalIDE > CellIDToCalIDEs(raw::CellID_t const &cellID, float const time) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< CalIDE > gar::cheat::BackTrackerCore::CellIDToCalIDEs ( raw::CellID_t const &  cellID,
float const  time 
) const
private

Definition at line 573 of file BackTrackerCore.cxx.

574  {
575 
576  // loop over the energy deposits in the channel and grab those in time window
577 
578  std::vector<CalIDE> calIDEs;
579 
580  std::vector<const sdp::CaloDeposit*> cellEDeps;
581  try {
582  cellEDeps = fCellIDToEDepCol.at(cell);
583  }
584  catch (std::out_of_range &) {
585  MF_LOG_WARNING("BackTrackerCore::CellIDToCalIDEs")
586  << "No sdp::EnergyDeposits for selected ECAL cell: " << cell
587  << ". There is no way to backtrack the given calorimeter hit,"
588  << " returning an empty vector.";
589  return calIDEs;
590  }
591 
592  std::map<int,size_t> tidmap; // Maps track IDs to index into calIDEs
593 
594  // Get the total energy produced by all track ids for this cell & time
595  float start = time -fECALtimeResolution;
596  float stop = time +fECALtimeResolution;
597 
598  double totalEallTracks = 0.0;
599  for (auto const& edep : cellEDeps) {
600  if (edep->TrackID() == sdp::NoParticleId) continue;
601 
602  if ( start<=edep->Time() && edep->Time()<=stop) {
603  float energy = edep->Energy();
604  totalEallTracks += energy;
605  int tid = edep->TrackID();
606  // Chase the tid up to the parent which started in the gas.
607  // tid < 0 is possible here, but FindTPCEve calls
608  // TrackIDToParticle, which fixes that little issue.
609  simb::MCParticle* TPCEve = FindTPCEve(tid);
610  int tidTPC;
611  if (TPCEve!=nullptr) {
612  tidTPC = TPCEve->TrackId();
613  } else {
614  tidTPC = tid;
615  }
616  auto tidmapiter = tidmap.find(tidTPC);
617  if (tidmapiter == tidmap.end()) {
618  calIDEs.emplace_back(tidTPC, 0.0, energy);
619  tidmap[tidTPC] = calIDEs.size()-1;
620  } else {
621  calIDEs.at(tidmapiter->second).energyTot += energy;
622  }
623  }
624  }
625 
626  // loop over the hitIDEs to set the fractional energy for each TrackID
627  for (size_t i = 0; i < calIDEs.size(); ++i) {
628  calIDEs[i].energyFrac = calIDEs[i].energyTot / totalEallTracks;
629  }
630  return calIDEs;
631  }
std::unordered_map< raw::CellID_t, std::vector< const sdp::CaloDeposit * > > fCellIDToEDepCol
convenience collections of EnergyDeposit for each cell
static const int NoParticleId
Definition: sim.h:30
int TrackId() const
Definition: MCParticle.h:210
simb::MCParticle * FindTPCEve(simb::MCParticle *const p) const
#define MF_LOG_WARNING(category)
double fECALtimeResolution
time resolution for hits in ECAL, nsec.
std::vector< HitIDE > gar::cheat::BackTrackerCore::ChannelToHitIDEs ( raw::Channel_t const &  channel,
double const  start,
double const  stop 
) const
private

Definition at line 298 of file BackTrackerCore.cxx.

299  {
300 
301  // loop over the energy deposits in the channel and grab those in time window
302 
303  if(start<0 || stop <0)
304  MF_LOG_WARNING("BackTrackerCore::ChannelToHitIDEs") << "start and/or stop times are negative!";
305  std::vector<HitIDE> hitIDEs;
306 
307  if(fChannelToEDepCol.size() < channel){
308  MF_LOG_WARNING("BackTrackerCore::ChannelToHitIDEs")
309  << "Attempting to find energy deposits for channel " << channel
310  << " while there are only " << fChannelToEDepCol.size()
311  << " channels available, returning an empty vector.";
312  return hitIDEs;
313  }
314 
315  auto chanEDeps = fChannelToEDepCol[channel];
316 
317  if(chanEDeps.size() < 1){
318  MF_LOG_WARNING("BackTrackerCore::ChannelToHitIDEs")
319  << "No sdp::EnergyDeposits for selected channel: " << channel
320  << ". There is no way to backtrack the given hit, returning"
321  << " an empty vector.";
322  return hitIDEs;
323  }
324 
325  std::map<int,size_t> tidmap; // Maps track IDs to index into hitIDEs
326 
327  // first get the total energy represented by all track ids for
328  // this channel and range of tdc values
329  unsigned short tdc = 0;
330  float chanpos[3]={0.,0.,0};
331  fGeo->ChannelToPosition(channel, chanpos);
332 
333  // loop over all EnergyDeposits for this channel and
334  // calculate a weighted average, in contributed energy,
335  // 4-position and 4-momentum
336  double totalEallTracks = 0.0;
337  size_t itraj=0;
338  for (auto const& edepfrac : chanEDeps) {
339 
340  auto edep = edepfrac.first;
341  float weight = edepfrac.second;
342  if(weight<0 || weight>1)
343  MF_LOG_WARNING("BackTrackerCore::ChannelToHitIDEs") << "bad weight (" << weight << ")";
344  if (edep->TrackID() == sdp::NoParticleId) continue;
345 
346  tdc = fClocks->TPCG4Time2TDC
347  (edep->Time() +TMath::Abs(edep->X()-chanpos[0]) *fInverseVelocity);
348  if ( (unsigned int)start <= tdc && tdc <= (unsigned int)stop) {
349  float energy = edep->Energy() * weight;
350  MF_LOG_DEBUG("BackTrackerCore::ChannelToHitIDEs") << "edep energy: " << edep->Energy()
351  << ", weight: " << weight << ", contributed energy: " << energy;
352  totalEallTracks += energy;
353 
354  TLorentzVector simpos(edep->X(),edep->Y(),edep->Z(),edep->Time());
355  TLorentzVector simmom = EnergyDepositToMomentum(edep->TrackID(),simpos,itraj);
356  simpos *= energy;
357  simmom *= energy;
358 
359 
360  // EnergyDeposits can contain negative track IDs corresponding to tracks
361  // created in showers. The absolute magnitude of the track is the track
362  // id of the parent of the EM showering process. That is the particle
363  // to which we want to assign this energy for backtracking purposes.
364  int tid = abs( edep->TrackID() );
365  auto tidmapiter = tidmap.find(tid);
366  if (tidmapiter == tidmap.end()) {
367  hitIDEs.emplace_back(tid, 0.0, energy, simpos, simmom);
368  tidmap[tid] = hitIDEs.size()-1;
369  } else {
370  hitIDEs.at(tidmapiter->second).energyTot += energy;
371  hitIDEs.at(tidmapiter->second).position += simpos;
372  hitIDEs.at(tidmapiter->second).momentum += simmom;
373  }
374  }
375  }
376 
377  /* DEB hits without edeps
378  std::cout << "Channel " << channel << ", at (z,y) = (" << chanpos[2] << ", "
379  << chanpos[1] << ") has " << chanEDeps.size() << " edeps, and " <<
380  hitIDEs.size() << " are in-time between " << start << " and " << stop
381  << std::endl; */
382 
383  // loop over the hitIDEs to set the fractional energy for each TrackID
384  for (size_t i = 0; i < hitIDEs.size(); ++i) {
385  hitIDEs[i].energyFrac = hitIDEs[i].energyTot / totalEallTracks;
386  hitIDEs[i].position *= 1.0/totalEallTracks;
387  hitIDEs[i].momentum *= 1.0/totalEallTracks;
388  }
389 
390  // remove duplicate HitIDEs (not that any should be there)
391  size_t nbefore = hitIDEs.size();
392  std::sort(hitIDEs.begin(),hitIDEs.end());
393  hitIDEs.erase(std::unique(hitIDEs.begin(),hitIDEs.end()),hitIDEs.end());
394  size_t nafter = hitIDEs.size();
395  if(nbefore!=nafter)
396  MF_LOG_WARNING("BackTrackerCore::ChannelToHitIDEs") << "found and removed "
397  << nbefore-nafter << " duplicate HitIDEs (not expected for single channel)";
398 
399  return hitIDEs;
400  }
std::vector< std::vector< std::pair< const sdp::EnergyDeposit *, float const > > > fChannelToEDepCol
convenience collections of EnergyDeposits for each channel
const detinfo::DetectorClocks * fClocks
Detector clock information.
uint8_t channel
Definition: CRTFragment.hh:201
const geo::GeometryCore * fGeo
pointer to the geometry
weight
Definition: test.py:257
static const int NoParticleId
Definition: sim.h:30
T abs(T value)
TLorentzVector EnergyDepositToMomentum(const int &trackID, const TLorentzVector &position, size_t &startTrajIndex) const
virtual double TPCG4Time2TDC(double g4time) const =0
Given G4 time [ns], returns corresponding TPC electronics clock count [tdc].
#define MF_LOG_DEBUG(id)
#define MF_LOG_WARNING(category)
double fInverseVelocity
inverse drift velocity
void ChannelToPosition(unsigned int const channel, float *const worldLoc) const
bool gar::cheat::BackTrackerCore::ClusterCreatedMCParticle ( simb::MCParticle *const  p,
rec::Cluster *const  c 
)

Definition at line 1013 of file BackTrackerCore.cxx.

1014  {
1015  std::vector<simb::MCParticle*> partsIsParts = MCPartsInCluster(c);
1016  for (simb::MCParticle* clusPart : partsIsParts) {
1017  if (IsForebearOf(clusPart,p)) return true;
1018  }
1019  return false;
1020  }
std::vector< simb::MCParticle * > MCPartsInCluster(rec::Cluster *const c)
bool IsForebearOf(simb::MCParticle *const forebear, simb::MCParticle *const afterbear) const
std::vector< art::Ptr< rec::CaloHit > > const gar::cheat::BackTrackerCore::ClusterToCaloHits ( rec::Cluster *const  c)

Definition at line 919 of file BackTrackerCore.cxx.

919  {
920 
921  // Evidently, use of std::unordered_map keeps this method from being const
922  std::vector<art::Ptr<gar::rec::CaloHit>> retval;
923  if ( !fClusterIDToCaloHits.empty() ) {
924  retval = fClusterIDToCaloHits[ c->getIDNumber() ];
925  }
926  return retval;
927  }
std::unordered_map< rec::IDNumber, std::vector< art::Ptr< rec::CaloHit > > > fClusterIDToCaloHits
Reco ECAL cluster ID to CaloHits.
std::vector< std::pair< simb::MCParticle *, float > > gar::cheat::BackTrackerCore::ClusterToMCParticles ( rec::Cluster *const  c)

Definition at line 933 of file BackTrackerCore.cxx.

933  {
934  // Can't be const because it uses TrackToHits
935 
936  std::vector<art::Ptr<gar::rec::CaloHit>> const hitCol = ClusterToCaloHits(c);
937  std::vector<CalIDE> hitIDECol;
938  for (auto hit : hitCol) {
939  std::vector<CalIDE> IDEsThisHit = CaloHitToCalIDEs(hit);
940  hitIDECol.insert(hitIDECol.end(), IDEsThisHit.begin(),IDEsThisHit.end());
941  }
942 
943  // A tree is a good way to accumulate the info we need
944  std::map<int,float> lClusterIdToEnergy;
945  for (auto calIDE : hitIDECol) {
946  simb::MCParticle* thisTrack = TrackIDToParticle(calIDE.trackID);
947  simb::MCParticle* incomingTrack = FindTPCEve(thisTrack);
948  int iTrackID = incomingTrack->TrackId();
949  lClusterIdToEnergy[iTrackID] += calIDE.energyFrac * calIDE.energyTot;
950  }
951 
952  // Next, sort the map by value.
953  // Declaring the type of the sorting predicate
954  typedef std::function<bool(std::pair<int,float>, std::pair<int,float>)> Comparator;
955  // Declaring a set that will store the pairs using above comparison logic
956  std::set<std::pair<int,float>, Comparator> setOfTracks(
957  lClusterIdToEnergy.begin(), lClusterIdToEnergy.end(),
958  [](std::pair<int,float> a ,std::pair<int,float> b) {
959  return a.second > b.second;
960  }
961  );
962 
963  // Make the returned vector. Could have no tracks at all?
964  float Etot = 0.0;
965  auto iTrackSet = setOfTracks.begin();
966  for (; iTrackSet!=setOfTracks.end(); ++iTrackSet) {
967  Etot += iTrackSet->second; // Can't do this in loop on lClusterIdToEnergy
968  }
969 
970  std::pair<simb::MCParticle*,float> emptee(nullptr,0.0);
971  std::vector<std::pair<simb::MCParticle*,float>> retval(lClusterIdToEnergy.size(),emptee);
972 
973  std::vector<std::pair<simb::MCParticle*,float>>::iterator iRetval = retval.begin();
974  iTrackSet = setOfTracks.begin();
975  for (; iTrackSet != setOfTracks.end(); ++iTrackSet,++iRetval) {
976  iRetval->first = TrackIDToParticle(iTrackSet->first);
977  iRetval->second = iTrackSet->second / Etot;
978  }
979 
980  return retval;
981  }
std::vector< CalIDE > CaloHitToCalIDEs(art::Ptr< rec::CaloHit > const &hit) const
int TrackId() const
Definition: MCParticle.h:210
simb::MCParticle * FindTPCEve(simb::MCParticle *const p) const
const double a
simb::MCParticle * TrackIDToParticle(int const &id) const
Detector simulation of raw signals on wires.
static bool * b
Definition: config.cpp:1043
std::vector< art::Ptr< rec::CaloHit > > const ClusterToCaloHits(rec::Cluster *const c)
TLorentzVector gar::cheat::BackTrackerCore::EnergyDepositToMomentum ( const int &  trackID,
const TLorentzVector &  position,
size_t &  startTrajIndex 
) const
private

Definition at line 1066 of file BackTrackerCore.cxx.

1067  {
1068 
1069  auto const& mcp = TrackIDToParticle(trackID); // MCParticle
1070  float dr=FLT_MAX, dt=FLT_MAX; // spatial and temporal distance
1071 
1072  // stepping through the trajectory, the distance should decrease until
1073  // min dist found (could be starting point)
1074  for(;startTrajIndex<mcp->NumberTrajectoryPoints(); startTrajIndex++){
1075 
1076  if( (position - mcp->Position(startTrajIndex)).Vect().Mag() < dr &&
1077  abs((position - mcp->Position(startTrajIndex)).T()) < dt) {
1078 
1079  dr = (position - mcp->Position(startTrajIndex)).Vect().Mag();
1080  dt = abs((position - mcp->Position(startTrajIndex)).T());
1081  } else {
1082  break;
1083  }
1084 
1085  } // for trajectory points
1086 
1087  return mcp->Momentum(startTrajIndex);
1088 
1089  } // def EnergyDepositToMomentum()
T abs(T value)
simb::MCParticle * TrackIDToParticle(int const &id) const
simb::MCParticle * gar::cheat::BackTrackerCore::FindEve ( simb::MCParticle *const  p) const

Definition at line 95 of file BackTrackerCore.cxx.

95  {
96  if (!fHasMC) {
97  throw cet::exception("BackTrackerCore::FindEve")
98  << "Attempting to backtrack without MC truth information";
99  }
100 
101  int EveTrackId = fParticleList.EveId( p->TrackId() );
102  return TrackIDToParticle(EveTrackId);
103  }
int TrackId() const
Definition: MCParticle.h:210
sim::ParticleList fParticleList
Maps MCParticle::TrackId() to same MCParticle.
simb::MCParticle * TrackIDToParticle(int const &id) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
simb::MCParticle* gar::cheat::BackTrackerCore::FindMother ( simb::MCParticle *const  p) const
inline

Definition at line 146 of file BackTrackerCore.h.

146  {
147  // Always walk up the ParticleList in case someday somebody changes it
148  return TrackIDToParticle(p->Mother());
149  }
int Mother() const
Definition: MCParticle.h:213
simb::MCParticle * TrackIDToParticle(int const &id) const
simb::MCParticle * gar::cheat::BackTrackerCore::FindTPCEve ( simb::MCParticle *const  p) const

Definition at line 108 of file BackTrackerCore.cxx.

108  {
109  if (!fHasMC) {
110  throw cet::exception("BackTrackerCore::FindTPCEve")
111  << "Attempting to backtrack without MC truth information";
112  }
113 
114  // If I had ever been here before I would probably know just what to do / Don't you?
115  int dejaVu = p->TrackId();
116  if ( fECALTrackToTPCTrack->find(dejaVu) != fECALTrackToTPCTrack->end() ) {
117  return TrackIDToParticle( fECALTrackToTPCTrack->at(dejaVu) );
118  }
119 
120  simb::MCParticle* walker = p;
121  if (walker==nullptr) return nullptr;
122  while ( walker != nullptr &&
123  !fGeo->PointInGArTPC( TVector3(walker->Vx(),walker->Vy(),walker->Vz()) ) ) {
124  // Walk up the ParticleList not the event store so someday somebody can
125  // change the ParticleList and this will still work.
126  int mommaTID = fParticleList[walker->TrackId()]->Mother();
127  if (mommaTID == 0) {
128  // you are at the top of the tree
129  break;
130  }
131  simb::MCParticle* momma = TrackIDToParticle( mommaTID );
132  walker = momma;
133  }
134  (*fECALTrackToTPCTrack)[dejaVu] = walker->TrackId();
135  return walker;
136  }
bool PointInGArTPC(TVector3 const &point) const
const geo::GeometryCore * fGeo
pointer to the geometry
int TrackId() const
Definition: MCParticle.h:210
sim::ParticleList fParticleList
Maps MCParticle::TrackId() to same MCParticle.
simb::MCParticle * TrackIDToParticle(int const &id) const
p
Definition: test.py:223
std::unordered_map< int, int > * fECALTrackToTPCTrack
results of previous FindTPCEve calls
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
simb::MCParticle * gar::cheat::BackTrackerCore::FindTPCEve ( int const  trackId) const

Definition at line 141 of file BackTrackerCore.cxx.

141  {
142  if (!fHasMC) {
143  throw cet::exception("BackTrackerCore::FindTPCEve")
144  << "Attempting to backtrack without MC truth information";
145  }
146 
147  // If I had ever been here before I would probably know just what to do / Don't you?
148  if ( fECALTrackToTPCTrack->find(trackID) != fECALTrackToTPCTrack->end() ) {
149  return TrackIDToParticle( fECALTrackToTPCTrack->at(trackID) );
150  }
151 
152  // Negative track ID, as found in EnergyDeposit or CaloEnergyDeposit,
153  // gets turned into positve track ID corresponding to parent of EM
154  // that created that particular EnergyDeposit or CaloEnergyDeposit
155  simb::MCParticle* walker = TrackIDToParticle(trackID);
156  while ( walker != nullptr &&
157  !fGeo->PointInGArTPC( TVector3(walker->Vx(),walker->Vy(),walker->Vz()) ) ) {
158  // Walk up the ParticleList not the event store so someday somebody can
159  // change the ParticleList and this will still work.
160  int mommaTID = fParticleList[walker->TrackId()]->Mother();
161  if (mommaTID == 0) {
162  // you are at the top of the tree
163  break;
164  }
165  simb::MCParticle* momma = TrackIDToParticle( mommaTID );
166  walker = momma;
167  }
168 
169  (*fECALTrackToTPCTrack)[trackID] = walker->TrackId();
170  return walker;
171  }
bool PointInGArTPC(TVector3 const &point) const
const geo::GeometryCore * fGeo
pointer to the geometry
int TrackId() const
Definition: MCParticle.h:210
sim::ParticleList fParticleList
Maps MCParticle::TrackId() to same MCParticle.
simb::MCParticle * TrackIDToParticle(int const &id) const
std::unordered_map< int, int > * fECALTrackToTPCTrack
results of previous FindTPCEve calls
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
sim::ParticleList* gar::cheat::BackTrackerCore::GetParticleList ( )
inline

Definition at line 137 of file BackTrackerCore.h.

137  {
138  return &fParticleList;
139  }
sim::ParticleList fParticleList
Maps MCParticle::TrackId() to same MCParticle.
bool gar::cheat::BackTrackerCore::HasCalHits ( )
inline

Definition at line 125 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::HasClusters ( )
inline

Definition at line 127 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::HasHits ( )
inline

Definition at line 124 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::HasMC ( )
inline

Definition at line 123 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::HasTracks ( )
inline

Definition at line 126 of file BackTrackerCore.h.

std::pair< double, double > gar::cheat::BackTrackerCore::HitEfficiency ( simb::MCParticle *const  p,
std::vector< art::Ptr< rec::Hit > > const &  hits,
std::vector< art::Ptr< rec::Hit > > const &  allhits,
bool  weightByCharge = false 
) const

Definition at line 449 of file BackTrackerCore.cxx.

451  {
452 
453  if (!fHasMC || !fHasHits) {
454  throw cet::exception("BackTrackerCore::HitCollectionEfficiency")
455  << "Attempting to backtrack without MC truth or gar::rec::Hit information";
456  }
457 
458  int trackIDin = p->TrackId();
459 
460  float weight;
461  float desired = 0.0;
462  for (auto hit : hits) {
463  std::vector<HitIDE> hitIDEs = this->HitToHitIDEs(hit);
464 
465  weight = (weightByCharge) ? hit->Signal() : 1.0;
466 
467  for (auto iHitIDE : hitIDEs) {
468  // Don't double count if this hit has > 1 of the desired GEANT track IDs
469  if (iHitIDE.trackID == trackIDin &&
470  iHitIDE.energyFrac >= fMinHitEnergyFraction) {
471  desired += weight;
472  break;
473  }
474  }
475  } // end loop over hits
476 
477  // Next figure out how many hits in the whole collection are associated with this id
478  float total = 0.0;
479  if (desired>0) {
480  for (auto hit : allhits) {
481  std::vector<HitIDE> hitIDEs = this->HitToHitIDEs(hit);
482 
483  weight = (weightByCharge) ? hit->Signal() : 1.0;
484 
485  for (auto iHitIDE : hitIDEs) {
486  // Don't double count if this hit has > 1 of the desired GEANT track IDs
487  if (iHitIDE.trackID == trackIDin &&
488  iHitIDE.energyFrac >= fMinHitEnergyFraction) {
489  total += weight;
490  break;
491  }
492  }
493  } // end loop over all hits
494  }
495 
496  double efficiency = 0.0;
497  double uncertainty = 0.0;
498  if (total > 0.0) {
499  efficiency = desired/total;
500  uncertainty = std::sqrt( desired*(total -desired)/total )/total;
501  }
502  return std::make_pair(efficiency,uncertainty);
503  }
double fMinHitEnergyFraction
min frac of ionization a track has to count in a TPC hit
weight
Definition: test.py:257
int TrackId() const
Definition: MCParticle.h:210
std::vector< HitIDE > HitToHitIDEs(art::Ptr< rec::Hit > const &hit) const
Detector simulation of raw signals on wires.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::pair< double, double > gar::cheat::BackTrackerCore::HitPurity ( simb::MCParticle *const  p,
std::vector< art::Ptr< rec::Hit >> const &  hits,
bool  weightByCharge = false 
) const

Definition at line 406 of file BackTrackerCore.cxx.

407  {
408 
409  if (!fHasMC || !fHasHits) {
410  throw cet::exception("BackTrackerCore::HitCollectionPurity")
411  << "Attempting to backtrack without MC truth or gar::rec::Hit information";
412  }
413 
414 
415  int trackIDin = p->TrackId();
416 
417  float weight;
418  float desired = 0.0;
419  float total = 0.0;
420  for (auto hit : hits) {
421  std::vector<HitIDE> hitIDEs = this->HitToHitIDEs(hit);
422 
423  weight = (weightByCharge) ? hit->Signal() : 1.0;
424  total += weight;
425 
426  for (auto iHitIDE : hitIDEs) {
427  // Don't double count if this hit has > 1 of the desired GEANT trackIDs
428  if (iHitIDE.trackID == trackIDin &&
429  iHitIDE.energyFrac >= fMinHitEnergyFraction) {
430  desired += weight;
431  break;
432  }
433  }
434  } // end loop over hits
435 
436  double purity = 0.0;
437  double uncertainty = 0.0;
438  if (total > 0.0) {
439  purity = desired/total;
440  uncertainty = std::sqrt( desired*(total -desired)/total )/total;
441  }
442  return std::make_pair(purity,uncertainty);
443  }
double fMinHitEnergyFraction
min frac of ionization a track has to count in a TPC hit
weight
Definition: test.py:257
int TrackId() const
Definition: MCParticle.h:210
std::vector< HitIDE > HitToHitIDEs(art::Ptr< rec::Hit > const &hit) const
Detector simulation of raw signals on wires.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< HitIDE > gar::cheat::BackTrackerCore::HitToHitIDEs ( art::Ptr< rec::Hit > const &  hit) const

Definition at line 225 of file BackTrackerCore.cxx.

225  {
226  if (!fHasMC || !fHasHits) {
227  throw cet::exception("BackTrackerCore::HitToHitIDEs")
228  << "Attempting to backtrack without MC truth or gar::rec::Hit information";
229  }
230 
231  // By cutting on the StartTime and EndTime for this hit, we get only the
232  // IDEs for this hit; ides.size() should basically be the number of
233  // MCParticles that contributed to this hit.
234  std::vector<HitIDE> ides;
235  ides = this->ChannelToHitIDEs(hit->Channel(),hit->StartTime(),hit->EndTime());
236  return ides;
237  }
std::vector< HitIDE > ChannelToHitIDEs(raw::Channel_t const &channel, double const start, double const stop) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< HitIDE > gar::cheat::BackTrackerCore::HitToHitIDEs ( rec::Hit const &  hit) const

Definition at line 241 of file BackTrackerCore.cxx.

241  {
242  if (!fHasMC || !fHasHits) {
243  throw cet::exception("BackTrackerCore::HitToHitIDEs")
244  << "Attempting to backtrack without MC truth or gar::rec::Hit information";
245  }
246 
247  // By cutting on the StartTime and EndTime for this hit, we get only the
248  // IDEs for this hit; ides.size() should basically be the number of
249  // MCParticles that contributed to this hit.
250  std::vector<HitIDE> ides;
251  ides = this->ChannelToHitIDEs(hit.Channel(),hit.StartTime(),hit.EndTime());
252  return ides;
253  }
Detector simulation of raw signals on wires.
std::vector< HitIDE > ChannelToHitIDEs(raw::Channel_t const &channel, double const start, double const stop) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
bool gar::cheat::BackTrackerCore::IsForebearOf ( simb::MCParticle *const  forebear,
simb::MCParticle *const  afterbear 
) const

Definition at line 176 of file BackTrackerCore.cxx.

177  {
178  if (!fHasMC) {
179  throw cet::exception("BackTrackerCore::IsForebearOf")
180  << "Attempting to backtrack without MC truth information";
181  }
182 
183  if (forebear==nullptr || afterbear==nullptr) return false;
184 
185  int stopper = forebear->TrackId();
186  int walker = afterbear->TrackId();
187  while ( walker!=stopper ) {
188  // Walk up the ParticleList not the event store so someday somebody can
189  // change the ParticleList and this will still work.
190  int momma = fParticleList[walker]->Mother();
191  if (momma == 0) {
192  return false;
193  } else {
194  walker = momma;
195  }
196  }
197  return true;
198  }
int TrackId() const
Definition: MCParticle.h:210
sim::ParticleList fParticleList
Maps MCParticle::TrackId() to same MCParticle.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
bool gar::cheat::BackTrackerCore::MCParticleCreatedCluster ( simb::MCParticle *const  p,
rec::Cluster *const  c 
)

Definition at line 1025 of file BackTrackerCore.cxx.

1026  {
1027  std::vector<simb::MCParticle*> partsIsParts = MCPartsInCluster(c);
1028 
1029  for (simb::MCParticle* clusPart : partsIsParts) {
1030  if (IsForebearOf(p,clusPart)) return true;
1031  }
1032  return false;
1033  }
std::vector< simb::MCParticle * > MCPartsInCluster(rec::Cluster *const c)
bool IsForebearOf(simb::MCParticle *const forebear, simb::MCParticle *const afterbear) const
std::vector< art::Ptr< rec::Cluster > > gar::cheat::BackTrackerCore::MCParticleToClusters ( simb::MCParticle *const  p,
std::vector< art::Ptr< rec::Cluster >> const &  clusters 
)

Definition at line 987 of file BackTrackerCore.cxx.

988  {
989 
990  // Can't be const because it uses ClusterToMCParticles
991  std::vector<art::Ptr<rec::Cluster>> retval;
992  std::vector<std::pair<simb::MCParticle*,float>> fromMatch;
993  int desiredTrackId = p->TrackId();
994 
995  for (art::Ptr<rec::Cluster> iCluster : clusters) {
996  // de-ref the art::Ptr and create a bare pointer for
997  // TrackToMCParticles call, managing const-correctness as best we can
998  rec::Cluster* argument = const_cast<rec::Cluster*>( &(*iCluster) );
999  fromMatch = ClusterToMCParticles( argument );
1000  for (std::pair<simb::MCParticle*,float> matches : fromMatch) {
1001  if ( matches.first->TrackId() == desiredTrackId &&
1002  matches.second > fClusterFracMCP) {
1003  retval.push_back( iCluster );
1004  }
1005  }
1006  }
1007  return retval;
1008  }
double fClusterFracMCP
min frac of ionization in a cluster for matching to an MCParticle
int TrackId() const
Definition: MCParticle.h:210
std::vector< std::pair< simb::MCParticle *, float > > ClusterToMCParticles(rec::Cluster *const c)
Definition: fwd.h:31
std::vector< art::Ptr< rec::Track > > gar::cheat::BackTrackerCore::MCParticleToTracks ( simb::MCParticle *const  p,
std::vector< art::Ptr< rec::Track >> const &  tracks 
)

Definition at line 890 of file BackTrackerCore.cxx.

891  {
892 
893  // Can't be const because it uses TrackToMCParticles
894  std::vector<art::Ptr<rec::Track>> retval;
895  std::vector<std::pair<simb::MCParticle*,float>> fromMatch;
896  int desiredTrackId = p->TrackId();
897 
898  for (art::Ptr<rec::Track> iTrack : tracks) {
899  // de-ref the art::Ptr and create a bare pointer for
900  // TrackToMCParticles call, managing const-correctness as best we can
901  rec::Track* argument = const_cast<rec::Track*>( &(*iTrack) );
902  fromMatch = TrackToMCParticles( argument );
903  for (std::pair<simb::MCParticle*,float> matches : fromMatch) {
904  if ( matches.first->TrackId() == desiredTrackId &&
905  matches.second > fTrackFracMCP) {
906  retval.push_back( iTrack );
907  }
908  }
909  }
910  return retval;
911  }
int TrackId() const
Definition: MCParticle.h:210
double fTrackFracMCP
min frac of ionization in a track for matching to an MCParticle
std::vector< std::pair< simb::MCParticle *, float > > TrackToMCParticles(rec::Track *const t)
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
Definition: Track.h:1036
Definition: fwd.h:31
std::vector< simb::MCParticle * > gar::cheat::BackTrackerCore::MCPartsInCluster ( rec::Cluster *const  c)
private

Definition at line 1039 of file BackTrackerCore.cxx.

1039  {
1040 
1041  std::vector<int> trackIdsInCluster;
1042  std::vector<art::Ptr<rec::CaloHit>> const caloHits = ClusterToCaloHits(c);
1043  for (art::Ptr iCaloHit : caloHits) {
1044  std::vector<CalIDE> IDEsThisHit = CaloHitToCalIDEs(iCaloHit);
1045  for ( CalIDE iIDE : IDEsThisHit ) trackIdsInCluster.push_back(iIDE.trackID);
1046  }
1047 
1048  std::sort(trackIdsInCluster.begin(),trackIdsInCluster.end());
1050  std::unique(trackIdsInCluster.begin(),trackIdsInCluster.end());
1051  trackIdsInCluster.resize(std::distance(trackIdsInCluster.begin(),itr));
1052 
1053  std::vector<simb::MCParticle*> retval;
1054  for ( int trackId : trackIdsInCluster) {
1055  simb::MCParticle* pahtay = TrackIDToParticle(trackId);
1056  retval.push_back(pahtay);
1057  }
1058  return retval;
1059  }
intermediate_table::iterator iterator
std::vector< CalIDE > CaloHitToCalIDEs(art::Ptr< rec::CaloHit > const &hit) const
simb::MCParticle * TrackIDToParticle(int const &id) const
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
std::vector< art::Ptr< rec::CaloHit > > const ClusterToCaloHits(rec::Cluster *const c)
Definition: fwd.h:31
std::vector< art::Ptr< rec::CaloHit > > gar::cheat::BackTrackerCore::ParticleToCaloHits ( simb::MCParticle *const  p,
std::vector< art::Ptr< rec::CaloHit >> const &  allhits 
) const

Definition at line 541 of file BackTrackerCore.cxx.

542  {
543  // returns a subset of the CaloHits in the allhits collection that match
544  // the MC particle passed as an argument
545 
546  if (!fHasMC || !fHasHits) {
547  throw cet::exception("BackTrackerCore::ParticleToCaloHits")
548  << "Attempting to backtrack without MC truth or gar::rec::CaloHit information";
549  }
550 
551  std::vector<art::Ptr<rec::CaloHit>> calHitList;
552  int tkID = p->TrackId();
553  std::vector<CalIDE> calhids;
554 
555  for (auto hit : allhits) {
556  calhids.clear();
557 
558  calhids = this->CellIDToCalIDEs(hit->CellID(),hit->Time().first);
559 
560  for (auto const& hid : calhids) {
561  if ( hid.trackID==tkID && hid.energyFrac>fMinCaloHitEnergyFrac ) {
562  calHitList.push_back(hit);
563  }
564  }
565  }
566  return calHitList;
567  }
int TrackId() const
Definition: MCParticle.h:210
Detector simulation of raw signals on wires.
std::vector< CalIDE > CellIDToCalIDEs(raw::CellID_t const &cellID, float const time) const
double fMinCaloHitEnergyFrac
min frac of ionization a track has to count in a CaloHit
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< art::Ptr< rec::Hit > > gar::cheat::BackTrackerCore::ParticleToHits ( simb::MCParticle *const  p,
std::vector< art::Ptr< rec::Hit >> const &  allhits,
bool  checkNeutrals = false 
) const

Definition at line 259 of file BackTrackerCore.cxx.

261  {
262  // returns a subset of the hits in the allhits collection that match
263  // the MC particle passed as an argument
264 
265  if (!fHasMC || !fHasHits) {
266  throw cet::exception("BackTrackerCore::ParticleToHits")
267  << "Attempting to backtrack without MC truth or gar::rec::Hit information";
268  }
269 
270  std::vector<art::Ptr<rec::Hit>> hitList;
271  int PDGpid = p->PdgCode();
272  bool obviousNeutral = PDGpid==22 || PDGpid==2112 || PDGpid==111 ||
273  abs(PDGpid)==14 || abs(PDGpid)==12;
274  if (!checkNeutrals && obviousNeutral) return hitList;
275 
276  int tkID = p->TrackId();
277  std::vector<HitIDE> hids;
278 
279  for (auto hit : allhits) {
280  hids.clear();
281 
282  hids = this->ChannelToHitIDEs(hit->Channel(),hit->StartTime(),hit->EndTime());
283 
284  for (auto const& hid : hids) {
285  if ( hid.trackID==tkID && hid.energyFrac>fMinHitEnergyFraction ) {
286  hitList.push_back(hit);
287  }
288  }
289  }
290 
291  return hitList;
292  }
int PdgCode() const
Definition: MCParticle.h:212
double fMinHitEnergyFraction
min frac of ionization a track has to count in a TPC hit
int TrackId() const
Definition: MCParticle.h:210
T abs(T value)
Detector simulation of raw signals on wires.
std::vector< HitIDE > ChannelToHitIDEs(raw::Channel_t const &channel, double const start, double const stop) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
art::Ptr< simb::MCTruth > const gar::cheat::BackTrackerCore::ParticleToMCTruth ( simb::MCParticle *const  p) const

Definition at line 204 of file BackTrackerCore.cxx.

204  {
205  if (!fHasMC) {
206  throw cet::exception("BackTrackerCore::ParticleToMCTruth")
207  << "Attempting to backtrack without MC truth information";
208  }
209 
210  int id = p->TrackId();
211  // find the entry in the MCTruth collection for this track id.
212  // The std::map fTrackIDToMCTrutheIndex will not contain keys < 0
213  size_t mct = fTrackIDToMCTruthIndex.find(std::abs(id))->second;
214 
215  if( mct > fMCTruthList.size() ) {
216  throw cet::exception("BackTrackerCore::ParticleToMCTruth")
217  << "attempting to find MCTruth index for out-of-range value: "
218  << mct << "/" << fMCTruthList.size();
219  }
220  return fMCTruthList[mct];
221  }
std::unordered_map< int, int > fTrackIDToMCTruthIndex
int TrackId() const
Definition: MCParticle.h:210
T abs(T value)
std::vector< art::Ptr< simb::MCTruth > > fMCTruthList
all the MCTruths for the event
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< art::Ptr< rec::Hit > > const gar::cheat::BackTrackerCore::TPCClusterToHits ( rec::TPCCluster *const  clust)

Definition at line 763 of file BackTrackerCore.cxx.

763  {
764 
765  // Evidently, use of std::unordered_map keeps this method from being const
766  std::vector<art::Ptr<gar::rec::Hit>> retval;
767  if ( !fTPCClusterIDToHits.empty() ) {
768  retval = fTPCClusterIDToHits[ clust->getIDNumber() ];
769  }
770  return retval;
771  }
std::unordered_map< rec::IDNumber, std::vector< art::Ptr< rec::Hit > > > fTPCClusterIDToHits
Reco TPC cluster ID to cluster&#39;s hits.
simb::MCParticle * gar::cheat::BackTrackerCore::TrackIDToParticle ( int const &  id) const

Definition at line 69 of file BackTrackerCore.cxx.

69  {
70  if (!fHasMC) {
71  throw cet::exception("BackTrackerCore::TrackIDToParticle")
72  << "Attempting to backtrack without MC truth information";
73  }
74 
75  // What to do for negative trackID? ParticleList::find(key) does
76  // std::map<int,simb::MCParticle*>.find( abs(key) ) so a track ID
77  // corresponding to a radiated photon for example gets the original
78  // parent of that photon. In most parts of GArSoft, trackIDs are
79  // non-negative; the exception is in the EnergyDeposit class.
80  auto part_it = fParticleList.find(id);
81 
82  if (part_it == fParticleList.end()) {
83  MF_LOG_WARNING("BackTrackerCore::TrackIDToParticle")
84  << "can't find particle with track id " << id
85  << " in sim::ParticleList returning null pointer";
86  return nullptr;
87  }
88 
89  return part_it->second;
90  }
sim::ParticleList fParticleList
Maps MCParticle::TrackId() to same MCParticle.
#define MF_LOG_WARNING(category)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< const rec::TPCCluster * > const gar::cheat::BackTrackerCore::TrackToClusters ( rec::Track *const  t)

Definition at line 776 of file BackTrackerCore.cxx.

776  {
777 
778  // Evidently, use of std::unordered_map keeps this method from being const
779  //std::vector<art::Ptr<gar::rec::TPCCluster>> retval;
780  std::vector<const gar::rec::TPCCluster*> retval;
781  if ( !fTrackIDToClusters.empty() ) {
782  retval = fTrackIDToClusters[ t->getIDNumber() ];
783  }
784  return retval;
785  }
std::unordered_map< rec::IDNumber, std::vector< const rec::TPCCluster * > > fTrackIDToClusters
Reco track ID to track&#39;s clusters.
std::vector< art::Ptr< rec::Hit > > const gar::cheat::BackTrackerCore::TrackToHits ( rec::Track *const  t)

Definition at line 742 of file BackTrackerCore.cxx.

742  {
743 
744  //std::cout << "BT:TrackToHits called, read map with " << fTrackIDToHits.size() << " entries" << std::endl;
745 
746  // Evidently, use of std::unordered_map keeps this method from being const
747  std::vector<art::Ptr<gar::rec::Hit>> retval;
748  if ( !fTrackIDToHits.empty() ) {
749  retval = fTrackIDToHits[ t->getIDNumber() ];
750  }
751  // useful for debugging, but reduces performance
752  /*size_t nduplicate = 0;
753  for(size_t i=0; i<retval.size(); i++) {
754  for(size_t j=i+1; j<retval.size(); j++) {
755  if(*retval[i]==*retval[j]) nduplicate++;
756  }
757  }
758  if(nduplicate!=0) std::cout << nduplicate << " duplicate hits matched to track" << std::endl;*/
759  return retval;
760  }
std::unordered_map< rec::IDNumber, std::vector< art::Ptr< rec::Hit > > > fTrackIDToHits
Reco track ID to track&#39;s hits.
std::vector< std::pair< simb::MCParticle *, float > > gar::cheat::BackTrackerCore::TrackToMCParticles ( rec::Track *const  t)

Definition at line 823 of file BackTrackerCore.cxx.

823  {
824  // Can't be const because it uses TrackToHits
825 
826  std::vector<art::Ptr<gar::rec::Hit>> const hitCol = TrackToHits(t);
827  std::vector<HitIDE> hitIDECol;
828  for (auto hit : hitCol) {
829  std::vector<HitIDE> IDEsThisHit = HitToHitIDEs(hit);
830  hitIDECol.insert(hitIDECol.end(), IDEsThisHit.begin(),IDEsThisHit.end());
831  }
832 
833  // A tree to accumulate the found energy for each GEANT/GENIE track
834  std::map<int,float> lTrackIdToEnergy;
835  for (auto hitIDE : hitIDECol) {
836  lTrackIdToEnergy[hitIDE.trackID] += hitIDE.energyFrac * hitIDE.energyTot;
837  }
838 
839  // There are still electrons in lTrackIdToEnergy that have parents in
840  // lTrackIDToEnergy... these should be consolidated. Also find total Energy
841  std::map<int,float>::iterator iTrackIdTo = lTrackIdToEnergy.begin();
842  for (; iTrackIdTo != lTrackIdToEnergy.end(); ++iTrackIdTo) {
843  simb::MCParticle* iPart = TrackIDToParticle(iTrackIdTo->first);
844  if ( iPart->PdgCode() == 11 && iPart->Mother()!=0 ) {
845  if ( lTrackIdToEnergy.count(iPart->Mother())==1 ) {
846  lTrackIdToEnergy[iPart->Mother()] += iTrackIdTo->second;
847  iTrackIdTo = lTrackIdToEnergy.erase(iTrackIdTo);
848  if (iTrackIdTo != lTrackIdToEnergy.begin()) --iTrackIdTo;
849  }
850  }
851  }
852 
853  // Next, sort the map by value. If there are two entries with the exact same
854  // ionization, only one is found in resulting set! But the digitization scale
855  // for edep is eVs (single molecular ionizations) i.e. 1e-5 or less so this should
856  // be negligible for tracks of interesting ionizations.
857  // Declaring the type of the sorting predicate
858  typedef std::function<bool(std::pair<int,float>, std::pair<int,float>)> Comparator;
859  // Declaring a set that will store the pairs using above comparison logic
860  std::set<std::pair<int,float>, Comparator> setOfTracks(
861  lTrackIdToEnergy.begin(), lTrackIdToEnergy.end(),
862  [](std::pair<int,float> a ,std::pair<int,float> b) {
863  return a.second > b.second;
864  }
865  );
866 
867  // Make the returned vector
868  float Etot = 0.0;
869  auto iTrackSet = setOfTracks.begin();
870  for (; iTrackSet!=setOfTracks.end(); ++iTrackSet) {
871  Etot += iTrackSet->second; // Can't do this in loop on lTrackIdToEnergy
872  }
873 
874  std::pair<simb::MCParticle*,float> emptee(nullptr,0.0);
875  std::vector<std::pair<simb::MCParticle*,float>> retval(setOfTracks.size(),emptee);
876  std::vector<std::pair<simb::MCParticle*,float>>::iterator iRetval = retval.begin();
877  iTrackSet = setOfTracks.begin();
878  for (; iTrackSet != setOfTracks.end(); ++iTrackSet,++iRetval) {
879  iRetval->first = TrackIDToParticle(iTrackSet->first);
880  iRetval->second = iTrackSet->second / Etot;
881  }
882 
883  return retval;
884  }
intermediate_table::iterator iterator
int PdgCode() const
Definition: MCParticle.h:212
std::vector< art::Ptr< rec::Hit > > const TrackToHits(rec::Track *const t)
int Mother() const
Definition: MCParticle.h:213
const double a
simb::MCParticle * TrackIDToParticle(int const &id) const
std::vector< HitIDE > HitToHitIDEs(art::Ptr< rec::Hit > const &hit) const
Detector simulation of raw signals on wires.
static bool * b
Definition: config.cpp:1043
double gar::cheat::BackTrackerCore::TrackToTotalEnergy ( rec::Track *const  t)

Definition at line 788 of file BackTrackerCore.cxx.

788  {
789 
790  double etot = 0.; // total energy
791  std::vector<art::Ptr<rec::Hit>> const hits = TrackToHits(t); // all of this track's hits
792  std::vector<HitIDE> allIdes;
793 
794  // loop over hits, get IDEs, and sum up energy deposits
795  for(auto const& hit : hits) {
796  std::vector<HitIDE> ides = HitToHitIDEs(hit);
797  allIdes.insert(allIdes.end(),ides.begin(),ides.end());
798 
799  }// for hits
800 
801  /*size_t pretotal = allIdes.size();
802 
803  // remove duplicate HitIdes
804  // comment out for now since we could have meaningful duplicate HitIDEs
805  // e.g. a common sdp::EnergyDeposit split evenly among channels
806  std::sort(allIdes.begin(),allIdes.end());
807  allIdes.erase( std::unique(allIdes.begin(),allIdes.end()), allIdes.end());
808  size_t posttotal = allIdes.size();
809 
810  if(pretotal!=posttotal) std::cout << "removed " << pretotal-posttotal << " duplicated IDEs" << std::endl;*/
811 
812  for(auto const& ide : allIdes) {
813  etot += ide.energyTot;
814  }// for ides
815 
816  return etot;
817  }
std::vector< art::Ptr< rec::Hit > > const TrackToHits(rec::Track *const t)
std::vector< HitIDE > HitToHitIDEs(art::Ptr< rec::Hit > const &hit) const
Detector simulation of raw signals on wires.

Member Data Documentation

std::unordered_map<raw::CellID_t,std::vector<const sdp::CaloDeposit*> > gar::cheat::BackTrackerCore::fCellIDToEDepCol
protected

convenience collections of EnergyDeposit for each cell

Definition at line 320 of file BackTrackerCore.h.

std::vector<std::vector<std::pair<const sdp::EnergyDeposit*, float const> > > gar::cheat::BackTrackerCore::fChannelToEDepCol
protected

convenience collections of EnergyDeposits for each channel

Definition at line 316 of file BackTrackerCore.h.

const detinfo::DetectorClocks* gar::cheat::BackTrackerCore::fClocks
protected

Detector clock information.

Definition at line 287 of file BackTrackerCore.h.

std::string gar::cheat::BackTrackerCore::fClusterECALInstance
protected

instance name for the ECAL clusters

Definition at line 302 of file BackTrackerCore.h.

double gar::cheat::BackTrackerCore::fClusterFracMCP
protected

min frac of ionization in a cluster for matching to an MCParticle

Definition at line 303 of file BackTrackerCore.h.

std::unordered_map< rec::IDNumber, std::vector<art::Ptr<rec::CaloHit> > > gar::cheat::BackTrackerCore::fClusterIDToCaloHits
protected

Reco ECAL cluster ID to CaloHits.

Definition at line 336 of file BackTrackerCore.h.

std::string gar::cheat::BackTrackerCore::fClusterLabel
protected

label for ECAL cluster producing module

Definition at line 301 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::fDisableRebuild
protected

for switching off backtracker's rebuild of the MCParticle tables

Definition at line 290 of file BackTrackerCore.h.

double gar::cheat::BackTrackerCore::fECALtimeResolution
protected

time resolution for hits in ECAL, nsec.

Definition at line 295 of file BackTrackerCore.h.

std::unordered_map<int, int>* gar::cheat::BackTrackerCore::fECALTrackToTPCTrack
protected

results of previous FindTPCEve calls

Definition at line 309 of file BackTrackerCore.h.

std::string gar::cheat::BackTrackerCore::fG4ModuleLabel
protected

label for geant4 module

Definition at line 291 of file BackTrackerCore.h.

const geo::GeometryCore* gar::cheat::BackTrackerCore::fGeo
protected

pointer to the geometry

Definition at line 288 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::fHasCalHits
protected

Definition at line 284 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::fHasClusters
protected

Definition at line 284 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::fHasHits
protected

Definition at line 284 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::fHasMC
protected

Definition at line 284 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::fHasTracks
protected

Definition at line 284 of file BackTrackerCore.h.

double gar::cheat::BackTrackerCore::fInverseVelocity
protected

inverse drift velocity

Definition at line 311 of file BackTrackerCore.h.

double gar::cheat::BackTrackerCore::fLongDiffConst
protected

longitudinal diffusion constant

Definition at line 312 of file BackTrackerCore.h.

std::vector<art::Ptr<simb::MCTruth> > gar::cheat::BackTrackerCore::fMCTruthList
protected

all the MCTruths for the event

Definition at line 306 of file BackTrackerCore.h.

double gar::cheat::BackTrackerCore::fMinCaloHitEnergyFrac
protected

min frac of ionization a track has to count in a CaloHit

Definition at line 297 of file BackTrackerCore.h.

double gar::cheat::BackTrackerCore::fMinHitEnergyFraction
protected

min frac of ionization a track has to count in a TPC hit

Definition at line 296 of file BackTrackerCore.h.

sim::ParticleList gar::cheat::BackTrackerCore::fParticleList
protected

Maps MCParticle::TrackId() to same MCParticle.

Definition at line 305 of file BackTrackerCore.h.

std::string gar::cheat::BackTrackerCore::fRawCaloDataECALInstance
protected

instance name for the ECAL raw hits

Definition at line 294 of file BackTrackerCore.h.

std::string gar::cheat::BackTrackerCore::fRawCaloDataLabel
protected

label for ECAL readout module

Definition at line 293 of file BackTrackerCore.h.

std::string gar::cheat::BackTrackerCore::fRawTPCDataLabel
protected

label for TPC readout module

Definition at line 292 of file BackTrackerCore.h.

bool gar::cheat::BackTrackerCore::fSplitEDeps
protected

use weights from PRFs to break true energy deposits into channel specific contributions

Definition at line 313 of file BackTrackerCore.h.

int gar::cheat::BackTrackerCore::fSTFU
protected

Definition at line 285 of file BackTrackerCore.h.

std::unordered_map< rec::IDNumber, std::vector<art::Ptr<rec::Hit> > > gar::cheat::BackTrackerCore::fTPCClusterIDToHits
protected

Reco TPC cluster ID to cluster's hits.

Definition at line 333 of file BackTrackerCore.h.

std::string gar::cheat::BackTrackerCore::fTPCClusterLabel
protected

label for TPCCluster producing module

Definition at line 299 of file BackTrackerCore.h.

double gar::cheat::BackTrackerCore::fTrackFracMCP
protected

min frac of ionization in a track for matching to an MCParticle

Definition at line 300 of file BackTrackerCore.h.

std::unordered_map< rec::IDNumber, std::vector<const rec::TPCCluster*> > gar::cheat::BackTrackerCore::fTrackIDToClusters
protected

Reco track ID to track's clusters.

Definition at line 328 of file BackTrackerCore.h.

std::unordered_map< rec::IDNumber, std::vector<art::Ptr<rec::Hit> > > gar::cheat::BackTrackerCore::fTrackIDToHits
protected

Reco track ID to track's hits.

Definition at line 324 of file BackTrackerCore.h.

std::unordered_map<int, int> gar::cheat::BackTrackerCore::fTrackIDToMCTruthIndex
protected

map of track ids to MCTruthList entry. Track Ids from MCParticle table in event store (no track ID <0)

Definition at line 307 of file BackTrackerCore.h.

std::string gar::cheat::BackTrackerCore::fTrackLabel
protected

label for final track producing module

Definition at line 298 of file BackTrackerCore.h.


The documentation for this class was generated from the following files: