All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
9 #include "art/Utilities/ToolMacros.h"
10 
11 //LArSoft Includes
18 
19 namespace ShowerRecoTools {
20 
22 
23  public:
24  ShowerTrackFinderCheater(const fhicl::ParameterSet& pset);
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;
37  const art::InputTag fPFParticleLabel;
38  const art::InputTag fHitModuleLabel;
39 
40  const std::string fTrueParticleIntputLabel;
41  const std::string fShowerStartPositionInputTag;
42  const std::string fShowerDirectionInputTag;
43  const std::string fInitialTrackHitsOutputLabel;
45  };
46 
47  ShowerTrackFinderCheater::ShowerTrackFinderCheater(const fhicl::ParameterSet& pset)
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"))
57  , fInitialTrackSpacePointsOutputLabel(
58  pset.get<std::string>("InitialTrackSpacePointsOutputLabel"))
59  {}
60 
61  int
62  ShowerTrackFinderCheater::CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
63  art::Event& Event,
64  reco::shower::ShowerElementHolder& ShowerEleHolder)
65  {
66 
67  const simb::MCParticle* trueParticle;
68 
69  //Get the hits from the shower:
70  auto const pfpHandle = Event.getValidHandle<std::vector<recob::PFParticle>>(fPFParticleLabel);
71  auto const clockData =
72  art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(Event);
73 
74  //Get the clusters
75  auto const clusHandle = Event.getValidHandle<std::vector<recob::Cluster>>(fPFParticleLabel);
76  art::FindManyP<recob::Cluster> fmc(pfpHandle, Event, fPFParticleLabel);
77  std::vector<art::Ptr<recob::Cluster>> clusters = fmc.at(pfparticle.key());
78 
79  //Get the hit association
80  art::FindManyP<recob::Hit> fmhc(clusHandle, Event, fPFParticleLabel);
81 
82  std::vector<art::Ptr<recob::Hit>> showerHits;
83  for (auto const& cluster : clusters) {
84 
85  //Get the hits
86  std::vector<art::Ptr<recob::Hit>> hits = fmhc.at(cluster.key());
87  showerHits.insert(showerHits.end(), hits.begin(), hits.end());
88  }
89 
90  if (ShowerEleHolder.CheckElement(fTrueParticleIntputLabel)) {
91  ShowerEleHolder.GetElement(fTrueParticleIntputLabel, trueParticle);
92  }
93  else {
94 
95  //Could store these in the shower element holder and just calculate once?
96  std::map<int, const simb::MCParticle*> trueParticles =
98  std::map<int, std::vector<int>> showersMothers =
100 
101  //Get the true particle from the shower
102  std::pair<int, double> ShowerTrackInfo =
104  clockData, showersMothers, showerHits, 2);
105 
106  if (ShowerTrackInfo.first == -99999) {
107  mf::LogError("ShowerStartPosition") << "True Shower Not Found";
108  return 1;
109  }
110  trueParticle = trueParticles[ShowerTrackInfo.first];
111  ShowerEleHolder.SetElement(trueParticle, fTrueParticleIntputLabel);
112  }
113 
114  if (!trueParticle) {
115  mf::LogError("ShowerDirectionCheater") << "True shower not found, returning";
116  return 1;
117  }
118 
119  //This is all based on the shower vertex being known. If it is not lets not do the track
120  if (!ShowerEleHolder.CheckElement(fShowerStartPositionInputTag)) {
121  mf::LogError("ShowerTrackFinderCheater") << "Start position not set, returning " << std::endl;
122  return 1;
123  }
124  if (!ShowerEleHolder.CheckElement(fShowerDirectionInputTag)) {
125  mf::LogError("ShowerTrackFinderCheater") << "Direction not set, returning " << std::endl;
126  return 1;
127  }
128 
129  TVector3 ShowerStartPosition = {-999, -999, -999};
130  ShowerEleHolder.GetElement(fShowerStartPositionInputTag, ShowerStartPosition);
131 
132  TVector3 ShowerDirection = {-999, -999, -999};
133  ShowerEleHolder.GetElement(fShowerDirectionInputTag, ShowerDirection);
134 
135  auto const hitHandle = Event.getValidHandle<std::vector<recob::Hit>>(fHitModuleLabel);
136 
137  // Get the hits associated with the space points
138  art::FindManyP<recob::SpacePoint> fmsph(hitHandle, Event, fPFParticleLabel);
139  if (!fmsph.isValid()) {
140  throw cet::exception("ShowerTrackFinderCheater")
141  << "Spacepoint and hit association not valid. Stopping.";
142  }
143 
144  std::vector<int> trueParticleIdVec;
145 
146  // If we have an electron, take the hits from the primary only
147  // This will also cover the cases Pandora misclassify a track as a shower
148  if (trueParticle->PdgCode() != 22) { trueParticleIdVec.push_back(trueParticle->TrackId()); }
149  else {
150  // To check if we are rolling up showers, check the number of daughters the photon has
151  const int nDaughters = trueParticle->NumberDaughters();
152  if (nDaughters == 0) {
153  // If we roll up showers, we have no choice but to take all of the hits from the photon
154  trueParticleIdVec.push_back(-trueParticle->TrackId());
155  }
156  else {
157  // If we do not roll up the showers, take all of the primary daughters
158  for (int i = 0; i < nDaughters; i++) {
159  trueParticleIdVec.push_back(trueParticle->Daughter(i));
160  }
161  }
162  }
163 
164  std::vector<art::Ptr<recob::Hit>> trackHits;
165  std::vector<art::Ptr<recob::SpacePoint>> trackSpacePoints;
166 
167  //Get the hits from the true particle
168  for (auto hit : showerHits) {
169  int trueHitId = fLArPandoraShowerCheatingAlg.TrueParticleID(clockData, hit);
170  if (std::find(trueParticleIdVec.cbegin(), trueParticleIdVec.cend(), trueHitId) !=
171  trueParticleIdVec.cend()) {
172  trackHits.push_back(hit);
173  std::vector<art::Ptr<recob::SpacePoint>> sps = fmsph.at(hit.key());
174  if (sps.size() == 1) { trackSpacePoints.push_back(sps.front()); }
175  }
176  }
177 
178  if (trackHits.empty() || trackSpacePoints.empty())
179  mf::LogWarning("ShowerTrackFinderCheater")
180  << "Creating intial track with " << trackHits.size() << " hits and "
181  << trackSpacePoints.size() << " spacepoints" << std::endl;
182 
183  ShowerEleHolder.SetElement(trackHits, fInitialTrackHitsOutputLabel);
184  ShowerEleHolder.SetElement(trackSpacePoints, fInitialTrackSpacePointsOutputLabel);
185 
186  if (fDebugEVD) {
188  clockData, trueParticle, Event, ShowerEleHolder, pfparticle);
189  }
190 
191  return 0;
192  }
193 }
194 
195 DEFINE_ART_CLASS_TOOL(ShowerRecoTools::ShowerTrackFinderCheater)
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
process_name cluster
Definition: cheaterreco.fcl:51
std::map< int, const simb::MCParticle * > GetTrueParticleMap() const
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
static constexpr bool
process_name hit
Definition: cheaterreco.fcl:51
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
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
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
shower::LArPandoraShowerCheatingAlg fLArPandoraShowerCheatingAlg