LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
ShowerTrackFinderCheater_tool.cc
Go to the documentation of this file.
1 //############################################################################
2 //### Name: ShowerTrackFinderCheater ###
3 //### Author: Ed Tyley ###
4 //### Date: 16.07.19 ###
5 //### Description: Cheating tool using truth for shower direction ###
6 //############################################################################
7 
8 //Framework Includes
10 
11 //LArSoft Includes
18 
19 namespace ShowerRecoTools {
20 
22 
23  public:
25 
26  //Generic Direction Finder
27  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
28  art::Event& Event,
29  reco::shower::ShowerElementHolder& ShowerEleHolder) override;
30 
31  private:
32  //Algorithm functions
34 
35  //fcl
36  const bool fDebugEVD;
39 
40  const std::string fTrueParticleIntputLabel;
41  const std::string fShowerStartPositionInputTag;
42  const std::string fShowerDirectionInputTag;
43  const std::string fInitialTrackHitsOutputLabel;
45  };
46 
48  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
49  , fLArPandoraShowerCheatingAlg(pset.get<fhicl::ParameterSet>("LArPandoraShowerCheatingAlg"))
50  , fDebugEVD(pset.get<bool>("DebugEVD"))
51  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
52  , fHitModuleLabel(pset.get<art::InputTag>("HitModuleLabel"))
53  , fTrueParticleIntputLabel(pset.get<std::string>("TrueParticleIntputLabel"))
54  , fShowerStartPositionInputTag(pset.get<std::string>("ShowerStartPositionInputTag"))
55  , fShowerDirectionInputTag(pset.get<std::string>("ShowerDirectionInputTag"))
56  , fInitialTrackHitsOutputLabel(pset.get<std::string>("InitialTrackHitsOutputLabel"))
58  pset.get<std::string>("InitialTrackSpacePointsOutputLabel"))
59  {}
60 
62  art::Event& Event,
63  reco::shower::ShowerElementHolder& ShowerEleHolder)
64  {
65  const simb::MCParticle* trueParticle = nullptr;
66 
67  //Get the hits from the shower:
68  auto const pfpHandle = Event.getValidHandle<std::vector<recob::PFParticle>>(fPFParticleLabel);
69  auto const clockData =
71 
72  //Get the clusters
73  auto const clusHandle = Event.getValidHandle<std::vector<recob::Cluster>>(fPFParticleLabel);
74  art::FindManyP<recob::Cluster> fmc(pfpHandle, Event, fPFParticleLabel);
75  std::vector<art::Ptr<recob::Cluster>> clusters = fmc.at(pfparticle.key());
76 
77  //Get the hit association
78  art::FindManyP<recob::Hit> fmhc(clusHandle, Event, fPFParticleLabel);
79 
80  std::vector<art::Ptr<recob::Hit>> showerHits;
81  for (auto const& cluster : clusters) {
82 
83  //Get the hits
84  std::vector<art::Ptr<recob::Hit>> hits = fmhc.at(cluster.key());
85  showerHits.insert(showerHits.end(), hits.begin(), hits.end());
86  }
87 
88  if (ShowerEleHolder.CheckElement(fTrueParticleIntputLabel)) {
89  ShowerEleHolder.GetElement(fTrueParticleIntputLabel, trueParticle);
90  }
91  else {
92 
93  //Could store these in the shower element holder and just calculate once?
94  std::map<int, const simb::MCParticle*> trueParticles =
96  std::map<int, std::vector<int>> showersMothers =
98 
99  //Get the true particle from the shower
100  std::pair<int, double> ShowerTrackInfo =
102  clockData, showersMothers, showerHits, 2);
103 
104  if (ShowerTrackInfo.first == -99999) {
105  mf::LogError("ShowerStartPosition") << "True Shower Not Found";
106  return 1;
107  }
108  trueParticle = trueParticles[ShowerTrackInfo.first];
109  ShowerEleHolder.SetElement(trueParticle, fTrueParticleIntputLabel);
110  }
111 
112  if (!trueParticle) {
113  mf::LogError("ShowerDirectionCheater") << "True shower not found, returning";
114  return 1;
115  }
116 
117  //This is all based on the shower vertex being known. If it is not lets not do the track
118  if (!ShowerEleHolder.CheckElement(fShowerStartPositionInputTag)) {
119  mf::LogError("ShowerTrackFinderCheater") << "Start position not set, returning " << std::endl;
120  return 1;
121  }
122  if (!ShowerEleHolder.CheckElement(fShowerDirectionInputTag)) {
123  mf::LogError("ShowerTrackFinderCheater") << "Direction not set, returning " << std::endl;
124  return 1;
125  }
126 
127  geo::Point_t ShowerStartPosition = {-999, -999, -999};
128  ShowerEleHolder.GetElement(fShowerStartPositionInputTag, ShowerStartPosition);
129 
130  geo::Vector_t ShowerDirection = {-999, -999, -999};
131  ShowerEleHolder.GetElement(fShowerDirectionInputTag, ShowerDirection);
132 
133  auto const hitHandle = Event.getValidHandle<std::vector<recob::Hit>>(fHitModuleLabel);
134 
135  // Get the hits associated with the space points
136  art::FindManyP<recob::SpacePoint> fmsph(hitHandle, Event, fPFParticleLabel);
137  if (!fmsph.isValid()) {
138  throw cet::exception("ShowerTrackFinderCheater")
139  << "Spacepoint and hit association not valid. Stopping.";
140  }
141 
142  std::vector<int> trueParticleIdVec;
143 
144  // If we have an electron, take the hits from the primary only
145  // This will also cover the cases Pandora misclassify a track as a shower
146  if (trueParticle->PdgCode() != 22) { trueParticleIdVec.push_back(trueParticle->TrackId()); }
147  else {
148  // To check if we are rolling up showers, check the number of daughters the photon has
149  const int nDaughters = trueParticle->NumberDaughters();
150  if (nDaughters == 0) {
151  // If we roll up showers, we have no choice but to take all of the hits from the photon
152  trueParticleIdVec.push_back(-trueParticle->TrackId());
153  }
154  else {
155  // If we do not roll up the showers, take all of the primary daughters
156  for (int i = 0; i < nDaughters; i++) {
157  trueParticleIdVec.push_back(trueParticle->Daughter(i));
158  }
159  }
160  }
161 
162  std::vector<art::Ptr<recob::Hit>> trackHits;
163  std::vector<art::Ptr<recob::SpacePoint>> trackSpacePoints;
164 
165  //Get the hits from the true particle
166  for (auto hit : showerHits) {
167  int trueHitId = fLArPandoraShowerCheatingAlg.TrueParticleID(clockData, hit);
168  if (std::find(trueParticleIdVec.cbegin(), trueParticleIdVec.cend(), trueHitId) !=
169  trueParticleIdVec.cend()) {
170  trackHits.push_back(hit);
171  std::vector<art::Ptr<recob::SpacePoint>> sps = fmsph.at(hit.key());
172  if (sps.size() == 1) { trackSpacePoints.push_back(sps.front()); }
173  }
174  }
175 
176  if (trackHits.empty() || trackSpacePoints.empty())
177  mf::LogWarning("ShowerTrackFinderCheater")
178  << "Creating intial track with " << trackHits.size() << " hits and "
179  << trackSpacePoints.size() << " spacepoints" << std::endl;
180 
181  ShowerEleHolder.SetElement(trackHits, fInitialTrackHitsOutputLabel);
182  ShowerEleHolder.SetElement(trackSpacePoints, fInitialTrackSpacePointsOutputLabel);
183 
184  if (fDebugEVD) {
186  clockData, trueParticle, Event, ShowerEleHolder, pfparticle);
187  }
188 
189  return 0;
190  }
191 }
192 
int PdgCode() const
Definition: MCParticle.h:213
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
Definition: geo_vectors.h:160
std::map< int, const simb::MCParticle * > GetTrueParticleMap() const
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
STL namespace.
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
Cluster finding and building.
int NumberDaughters() const
Definition: MCParticle.h:218
int TrackId() const
Definition: MCParticle.h:211
int Daughter(const int i) const
Definition: MCParticle.cxx:118
void hits()
Definition: readHits.C:15
parameter set interface
key_type key() const noexcept
Definition: Ptr.h:166
std::map< int, std::vector< int > > GetTrueChain(std::map< int, const simb::MCParticle * > &trueParticles) const
bool CheckElement(const std::string &Name) const
void CheatDebugEVD(detinfo::DetectorClocksData const &clockData, const simb::MCParticle *trueParticle, art::Event const &Event, reco::shower::ShowerElementHolder &ShowerEleHolder, const art::Ptr< recob::PFParticle > &pfparticle) const
int GetElement(const std::string &Name, T &Element) const
Declaration of cluster object.
int TrueParticleID(detinfo::DetectorClocksData const &clockData, const art::Ptr< recob::Hit > &hit) const
Detector simulation of raw signals on wires.
ShowerTrackFinderCheater(const fhicl::ParameterSet &pset)
std::pair< int, double > TrueParticleIDFromTrueChain(detinfo::DetectorClocksData const &clockData, std::map< int, std::vector< int >> const &ShowersMothers, std::vector< art::Ptr< recob::Hit >> const &hits, int planeid) const
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:180
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
Definition: MVAAlg.h:12
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
shower::LArPandoraShowerCheatingAlg fLArPandoraShowerCheatingAlg
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33