All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCTruthAssociations.h
Go to the documentation of this file.
1 /**
2  * @file MCTruthAssociations.h
3  * @brief This algorithm attempts to decode Track and Hit <--> MCParticle assocations
4  * @author Tracy Usher (usher@slac.stanford.edu)
5  * @date October 25, 2017
6  * @see galleryAnalysis.cpp
7  *
8  */
9 
10 #ifndef MCTruthAssociations_H
11 #define MCTruthAssociations_H
12 
13 // LArSoft libraries
16 #include "nusimdata/SimulationBase/MCTruth.h"
17 #include "nusimdata/SimulationBase/MCParticle.h"
22 //#include "lardataobj/Simulation/SimChannel.h" // need this for TrackIDE definition
23 #include "canvas/Persistency/Common/Assns.h"
24 #include "canvas/Persistency/Common/FindOneP.h"
25 namespace detinfo {
27 }
28 
29 // nutools
31 
32 // canvas libraries
33 #include "fhiclcpp/ParameterSet.h"
34 
35 // C/C++ standard libraries
36 #include <vector>
37 #include <memory> // std::unique_ptr<>
38 
39 namespace truth
40 {
41 using MCTruthTruthVec = std::vector<art::Ptr<simb::MCTruth>>;
42 using MCParticleVec = std::vector<art::Ptr<simb::MCParticle>>;
43 using MCTruthAssns = art::FindOneP<simb::MCTruth>;
44 using HitParticleAssociations = art::Assns<simb::MCParticle, recob::Hit, anab::BackTrackerHitMatchingData>;
45 using HitParticleAssociationsVec = std::vector<const HitParticleAssociations*>;
46 using MCTruthParticleAssociations = art::Assns<simb::MCTruth, simb::MCParticle, void>;
47 
48 // Definition of TrackIDE here to avoid pulling in SimChannel.h
49 /// Ionization energy from a Geant4 track
50 struct TrackIDE{
51  int trackID; ///< Geant4 supplied trackID
52  float energyFrac; ///< fraction of hit energy from the particle with this trackID
53  float energy; ///< energy from the particle with this trackID [MeV]
54  float numElectrons; ///< number of electrons from the particle detected on the wires
55 };
56 
57 /**
58  * @brief Obtains truth matching by using hit <--> MCParticle associations
59  *
60  * Configuration
61  * --------------
62  *
63  */
65 {
66 public:
67 
68  MCTruthAssociations(fhicl::ParameterSet const& config);
69 
71  const MCParticleVec&,
72  const MCTruthAssns&,
73  const geo::GeometryCore&);
74 
75  const MCTruthParticleList& getParticleList() const;
76 
77  // Return a pointer to the simb::MCParticle object corresponding to
78  // the given TrackID
79  const simb::MCParticle* TrackIDToParticle(int const& id) const;
80  const simb::MCParticle* TrackIDToMotherParticle(int const& id) const;
81 
82  // Get art::Ptr<> to simb::MCTruth and related information
83  const art::Ptr<simb::MCTruth>& TrackIDToMCTruth(int const& id) const;
84  const art::Ptr<simb::MCTruth>& ParticleToMCTruth(const simb::MCParticle* p) const;
85  std::vector<const simb::MCParticle*> MCTruthToParticles(art::Ptr<simb::MCTruth> const& mct) const;
86  const MCTruthTruthVec& MCTruthVector() const;
87 
88  // this method will return the Geant4 track IDs of
89  // the particles contributing ionization electrons to the identified hit
90  std::vector<TrackIDE> HitToTrackID(const recob::Hit*) const;
91  std::vector<TrackIDE> HitToTrackID(art::Ptr<recob::Hit> const&) const;
92 
93  // method to return a subset of allhits that are matched to a list of TrackIDs
94  const std::vector<std::vector<art::Ptr<recob::Hit>>> TrackIDsToHits(std::vector<art::Ptr<recob::Hit>> const&,
95  std::vector<int> const&) const;
96 
97  // method to return the EveIDs of particles contributing ionization
98  // electrons to the identified hit
99  std::vector<TrackIDE> HitToEveID(art::Ptr<recob::Hit> const& hit) const;
100 
101  // method to return the XYZ position of the weighted average energy deposition for a given hit
102  std::vector<double> HitToXYZ(art::Ptr<recob::Hit> const& hit) const;
103 
104  // method to return the XYZ position of a space point (unweighted average XYZ of component hits).
105  std::vector<double> SpacePointHitsToXYZ(art::PtrVector<recob::Hit> const& hits) const;
106 
107  // method to return the fraction of hits in a collection that come from the specified Geant4 track ids
108  double HitCollectionPurity(std::set<int>,
109  std::vector< art::Ptr<recob::Hit> > const&) const;
110 
111  // method to return the fraction of all hits in an event from a specific set of Geant4 track IDs that are
112  // represented in a collection of hits
113  double HitCollectionEfficiency(std::set<int>,
114  std::vector< art::Ptr<recob::Hit> > const&,
115  std::vector< art::Ptr<recob::Hit> > const&,
116  geo::View_t const&) const;
117 
118  // method to return the fraction of charge in a collection that come from the specified Geant4 track ids
119  double HitChargeCollectionPurity(std::set<int>,
120  std::vector< art::Ptr<recob::Hit> > const&) const;
121 
122  // method to return the fraction of all charge in an event from a specific set of Geant4 track IDs that are
123  // represented in a collection of hits
124  double HitChargeCollectionEfficiency(std::set<int>,
125  std::vector< art::Ptr<recob::Hit> > const&,
126  std::vector< art::Ptr<recob::Hit> > const&,
127  geo::View_t const&) const;
128 
129  // method to return all EveIDs corresponding to the current sim::ParticleList
130  std::set<int> GetSetOfEveIDs() const;
131 
132  // method to return all TrackIDs corresponding to the current sim::ParticleList
133  std::set<int> GetSetOfTrackIDs() const;
134 
135  // method to return all EveIDs corresponding to the given list of hits
136  std::set<int> GetSetOfEveIDs(std::vector< art::Ptr<recob::Hit> > const&) const;
137 
138  // method to return all TrackIDs corresponding to the given list of hits
139  std::set<int> GetSetOfTrackIDs(std::vector< art::Ptr<recob::Hit> > const&) const;
140 
141 private:
142 
143  // Declare the containers for the basic maps
144  using HitMatchDataPair = std::pair<const recob::Hit*,const anab::BackTrackerHitMatchingData*>;
145  using PartMatchDataPair = std::pair<const simb::MCParticle*,const anab::BackTrackerHitMatchingData*>;
146  using HitToPartVecMap = std::map<const recob::Hit*,std::set<PartMatchDataPair>>;
147  using PartToHitVecMap = std::map<const simb::MCParticle*, std::set<HitMatchDataPair>>;
148  using MCTruthTrackIDMap = std::unordered_map<int, art::Ptr<simb::MCTruth>>;
149 
150  // Must allow for the case of multiple instances of hit <--> MCParticle associations
151  // You ask "why do it this way? Can't these all be in a single set of containers?"
152  // The answer is no because you want to avoid multiple counting
154  {
155  // Declare containers as member variables
156  HitToPartVecMap fHitToPartVecMap; ///< Mapping from hits to associated MCParticle/data pairs
157  PartToHitVecMap fPartToHitVecMap; ///< Mapping from MCParticle to associated hit/data pairs
158  };
159 
160  using HitPartAssnsList = std::list<HitPartAssnsStruct>;
161 
162  int calculateEvdID(int) const;
163  double length(const recob::Track*) const;
165  const simb::MCParticle& part, double dx,
166  TVector3& start, TVector3& end, TVector3& startmom, TVector3& endmom,
167  unsigned int tpc = 0, unsigned int cstat = 0) const;
168 
169  HitPartAssnsList fHitPartAssnsVec; ///< Container for the (multiple) associations
170  MCTruthParticleList fParticleList; ///< ParticleList to map track ID to
171  MCTruthTruthVec fMCTruthVec; ///< all the MCTruths for the event
172  MCTruthTrackIDMap fTrackIDToMCTruthIndex; ///< map of track ids to MCTruthList entry
173 
174  float fMinHitEnergyFraction; ///< minimum fraction of energy a track id has to
175  ///< contribute to a hit to be counted in
176  ///< purity and efficiency calculations
177  ///< based on hit collections
178 
179  geo::GeometryCore const* fGeometry = nullptr;
180 
181 }; // class MCTruthAssociations
182 
183 } // End of namespace
184 
185 #endif // MCTruthAssociations_H
std::pair< const simb::MCParticle *, const anab::BackTrackerHitMatchingData * > PartMatchDataPair
art::Assns< simb::MCParticle, recob::Hit, anab::BackTrackerHitMatchingData > HitParticleAssociations
std::list< HitPartAssnsStruct > HitPartAssnsList
std::pair< const recob::Hit *, const anab::BackTrackerHitMatchingData * > HitMatchDataPair
MCTruthTruthVec fMCTruthVec
all the MCTruths for the event
double HitChargeCollectionEfficiency(std::set< int >, std::vector< art::Ptr< recob::Hit > > const &, std::vector< art::Ptr< recob::Hit > > const &, geo::View_t const &) const
std::vector< art::Ptr< simb::MCTruth >> MCTruthTruthVec
Particle list in DetSim contains Monte Carlo particle information.
const std::vector< std::vector< art::Ptr< recob::Hit > > > TrackIDsToHits(std::vector< art::Ptr< recob::Hit >> const &, std::vector< int > const &) const
HitToPartVecMap fHitToPartVecMap
Mapping from hits to associated MCParticle/data pairs.
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
Declaration of signal hit object.
art::Assns< simb::MCTruth, simb::MCParticle, void > MCTruthParticleAssociations
HitPartAssnsList fHitPartAssnsVec
Container for the (multiple) associations.
pdgs p
Definition: selectors.fcl:22
Ionization energy from a Geant4 track.
std::vector< TrackIDE > HitToEveID(art::Ptr< recob::Hit > const &hit) const
MCTruthTrackIDMap fTrackIDToMCTruthIndex
map of track ids to MCTruthList entry
process_name hit
Definition: cheaterreco.fcl:51
PartToHitVecMap fPartToHitVecMap
Mapping from MCParticle to associated hit/data pairs.
art::FindOneP< simb::MCTruth > MCTruthAssns
std::vector< art::Ptr< simb::MCParticle >> MCParticleVec
MCTruthAssociations(fhicl::ParameterSet const &config)
float numElectrons
number of electrons from the particle detected on the wires
const simb::MCParticle * TrackIDToMotherParticle(int const &id) const
std::vector< TrackIDE > HitToTrackID(const recob::Hit *) const
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Access the description of detector geometry.
double length(const recob::Track *) const
float energy
energy from the particle with this trackID [MeV]
int calculateEvdID(int) const
std::set< int > GetSetOfTrackIDs() const
std::set< int > GetSetOfEveIDs() const
std::map< const recob::Hit *, std::set< PartMatchDataPair >> HitToPartVecMap
Obtains truth matching by using hit &lt;–&gt; MCParticle associations.
double HitCollectionPurity(std::set< int >, std::vector< art::Ptr< recob::Hit > > const &) const
const art::Ptr< simb::MCTruth > & TrackIDToMCTruth(int const &id) const
int trackID
Geant4 supplied trackID.
std::map< const simb::MCParticle *, std::set< HitMatchDataPair >> PartToHitVecMap
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
double HitCollectionEfficiency(std::set< int >, std::vector< art::Ptr< recob::Hit > > const &, std::vector< art::Ptr< recob::Hit > > const &, geo::View_t const &) const
double HitChargeCollectionPurity(std::set< int >, std::vector< art::Ptr< recob::Hit > > const &) const
const simb::MCParticle * TrackIDToParticle(int const &id) const
Description of geometry of one entire detector.
const art::Ptr< simb::MCTruth > & ParticleToMCTruth(const simb::MCParticle *p) const
Provides recob::Track data product.
const MCTruthParticleList & getParticleList() const
std::unordered_map< int, art::Ptr< simb::MCTruth >> MCTruthTrackIDMap
std::vector< const HitParticleAssociations * > HitParticleAssociationsVec
std::vector< double > HitToXYZ(art::Ptr< recob::Hit > const &hit) const
std::vector< double > SpacePointHitsToXYZ(art::PtrVector< recob::Hit > const &hits) const
MCTruthParticleList fParticleList
ParticleList to map track ID to.
float energyFrac
fraction of hit energy from the particle with this trackID
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
std::vector< const simb::MCParticle * > MCTruthToParticles(art::Ptr< simb::MCTruth > const &mct) const
geo::GeometryCore const * fGeometry
void setup(const HitParticleAssociationsVec &, const MCParticleVec &, const MCTruthAssns &, const geo::GeometryCore &)
auto const detProp
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track:
const MCTruthTruthVec & MCTruthVector() const