All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BackTrackerTruth_tool.cc
Go to the documentation of this file.
2 
3 #include "fhiclcpp/ParameterSet.h"
4 #include "art/Utilities/ToolMacros.h"
5 #include "art/Framework/Services/Registry/ServiceHandle.h"
6 #include "art_root_io/TFileService.h"
7 #include "art/Framework/Core/ModuleMacros.h"
8 #include "art_root_io/TFileDirectory.h"
9 #include "canvas/Utilities/InputTag.h"
10 #include "messagefacility/MessageLogger/MessageLogger.h"
11 #include "canvas/Persistency/Common/FindManyP.h"
12 
14 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
18 
19 #include <cmath>
20 #include <algorithm>
21 
22 namespace truth
23 {
24  ////////////////////////////////////////////////////////////////////////
25  //
26  // Class: BackTrackerTruth
27  // Module Type: art tool
28  // File: BackTrackerTruth.h
29  //
30  // This provides MC truth information by interfacing to the
31  // LarSoft BackTracker service
32  //
33  // Configuration parameters:
34  //
35  // TruncMeanFraction - the fraction of waveform bins to discard when
36  //
37  // Created by Tracy Usher (usher@slac.stanford.edu) on November 21, 2017
38  //
39  ////////////////////////////////////////////////////////////////////////
40 
41 class BackTrackerTruth : virtual public IMCTruthMatching
42 {
43 public:
44  /**
45  * @brief Constructor
46  *
47  * @param pset
48  */
49  explicit BackTrackerTruth(fhicl::ParameterSet const & pset);
50 
51  /**
52  * @brief Destructor
53  */
55 
56  /**
57  * @brief This rebuilds the internal maps, is a noop for this module since
58  * the BackTracker is a service and rebuilds its maps at begin run
59  */
60  void Rebuild(const art::Event& evt) override {};
61 
62  // provide for initialization
63  void reconfigure(fhicl::ParameterSet const & pset) override;
64 
65  /**
66  * @brief Get a reference to the ParticleList
67  */
68  const sim::ParticleList& ParticleList() const override;
69 
70  // // Set the EveIdCalculator for the owned ParticleList
71  // void SetEveIdCalculator(sim::EveIdCalculator *ec) { fParticleList.AdoptEveIdCalculator(ec); }
72 
73  // Return a pointer to the simb::MCParticle object corresponding to
74  // the given TrackID
75  const simb::MCParticle* TrackIDToParticle(int const id) const override;
76  const simb::MCParticle* TrackIDToMotherParticle(int const id) const override;
77 
78  // Get art::Ptr<> to simb::MCTruth and related information
79  const art::Ptr<simb::MCTruth>& TrackIDToMCTruth(int const id) const override;
80  const art::Ptr<simb::MCTruth>& ParticleToMCTruth(const simb::MCParticle* p) const override;
81  std::vector<const simb::MCParticle*> MCTruthToParticles(art::Ptr<simb::MCTruth> const& mct) const override;
82  const std::vector< art::Ptr<simb::MCTruth> >& MCTruthVector() const override;
83 
84  // this method will return the Geant4 track IDs of
85  // the particles contributing ionization electrons to the identified hit
86  std::vector<sim::TrackIDE> HitToTrackID(detinfo::DetectorClocksData const& clockData,
87  recob::Hit const& hit) const override;
88  std::vector<sim::TrackIDE> HitToTrackID(detinfo::DetectorClocksData const& clockData,
89  art::Ptr<recob::Hit> const& hit) const override;
90 
91  // method to return a subset of allhits that are matched to a list of TrackIDs
92  std::vector<std::vector<art::Ptr<recob::Hit>>> TrackIDsToHits(detinfo::DetectorClocksData const& clockData,
93  std::vector<art::Ptr<recob::Hit>> const& allhits,
94  std::vector<int> const& tkIDs) const override;
95 
96  // method to return the EveIDs of particles contributing ionization
97  // electrons to the identified hit
98  std::vector<sim::TrackIDE> HitToEveID(detinfo::DetectorClocksData const& clockData,
99  art::Ptr<recob::Hit> const& hit) const override;
100 
101  // method to return the XYZ position of the weighted average energy deposition for a given hit
102  std::vector<double> HitToXYZ(detinfo::DetectorClocksData const& clockData,
103  art::Ptr<recob::Hit> const& hit) const override;
104 
105  // method to return the XYZ position of a space point (unweighted average XYZ of component hits).
106  std::vector<double> SpacePointToXYZ(detinfo::DetectorClocksData const&,
107  art::Ptr<recob::SpacePoint> const& spt,
108  art::Event const& evt,
109  std::string const& label) const override;
110 
111  // method to return the XYZ position of a space point (unweighted average XYZ of component hits).
112  std::vector<double> SpacePointHitsToXYZ(detinfo::DetectorClocksData const& clockData,
113  art::PtrVector<recob::Hit> const& hits) const override;
114 
115  // method to return the fraction of hits in a collection that come from the specified Geant4 track ids
116  double HitCollectionPurity(detinfo::DetectorClocksData const& clockData,
117  std::set<int> const& trackIDs,
118  std::vector< art::Ptr<recob::Hit> > const& hits) const override;
119 
120  // method to return the fraction of all hits in an event from a specific set of Geant4 track IDs that are
121  // represented in a collection of hits
123  std::set<int> const& trackIDs,
124  std::vector< art::Ptr<recob::Hit> > const& hits,
125  std::vector< art::Ptr<recob::Hit> > const& allhits,
126  geo::View_t const view) const override;
127 
128  // method to return the fraction of charge in a collection that come from the specified Geant4 track ids
130  std::set<int> const& trackIDs,
131  std::vector< art::Ptr<recob::Hit> > const& hits) const override;
132 
133  // method to return the fraction of all charge in an event from a specific set of Geant4 track IDs that are
134  // represented in a collection of hits
136  std::set<int> const& trackIDs,
137  std::vector< art::Ptr<recob::Hit> > const& hits,
138  std::vector< art::Ptr<recob::Hit> > const& allhits,
139  geo::View_t const view) const override;
140 
141  // method to return all EveIDs corresponding to the current sim::ParticleList
142  std::set<int> GetSetOfEveIDs() const override;
143 
144  // method to return all TrackIDs corresponding to the current sim::ParticleList
145  std::set<int> GetSetOfTrackIDs() const override;
146 
147  // method to return all EveIDs corresponding to the given list of hits
148  std::set<int> GetSetOfEveIDs(detinfo::DetectorClocksData const& clockData,
149  std::vector< art::Ptr<recob::Hit> > const& hits) const override;
150 
151  // method to return all TrackIDs corresponding to the given list of hits
152  std::set<int> GetSetOfTrackIDs(detinfo::DetectorClocksData const& clockData,
153  std::vector< art::Ptr<recob::Hit> > const& hits) const override;
154 
155 private:
156 
157  // Fcl parameters.
158  art::InputTag fTrackProducerLabel; ///< tag for finding the tracks
159 
160  // Useful services, keep copies for now (we can update during begin run periods)
161  const geo::GeometryCore* fGeometry; ///< pointer to Geometry service
162 };
163 
164 //----------------------------------------------------------------------------
165 /// Constructor.
166 ///
167 /// Arguments:
168 ///
169 /// pset - Fcl parameters.
170 ///
171 BackTrackerTruth::BackTrackerTruth(fhicl::ParameterSet const & pset)
172 {
173  fGeometry = lar::providerFrom<geo::Geometry>();
174 
175  reconfigure(pset);
176 
177  // Report.
178  mf::LogInfo("BackTrackerTruth") << "BackTrackerTruth configured\n";
179 }
180 
181 //----------------------------------------------------------------------------
182 /// Destructor.
184 {}
185 
186 //----------------------------------------------------------------------------
187 /// Reconfigure method.
188 ///
189 /// Arguments:
190 ///
191 /// pset - Fcl parameter set.
192 ///
193 void BackTrackerTruth::reconfigure(fhicl::ParameterSet const & pset)
194 {
195  fTrackProducerLabel = pset.get<art::InputTag>("TrackProducerLabel", "");
196 }
197 
198 //----------------------------------------------------------------------
199 const sim::ParticleList& BackTrackerTruth::ParticleList() const
200 {
201  art::ServiceHandle<cheat::ParticleInventoryService> partInventory;
202  return partInventory->ParticleList();
203 }
204 
205 
206 //----------------------------------------------------------------------
207 const simb::MCParticle* BackTrackerTruth::TrackIDToParticle(int const id) const
208 {
209  art::ServiceHandle<cheat::ParticleInventoryService> partInventory;
210  return partInventory->TrackIdToParticle_P(id);
211 }
212 
213 //----------------------------------------------------------------------
214 const simb::MCParticle* BackTrackerTruth::TrackIDToMotherParticle(int const id) const
215 {
216  art::ServiceHandle<cheat::ParticleInventoryService> partInventory;
217  return partInventory->TrackIdToMotherParticle_P(id);
218 }
219 
220 //----------------------------------------------------------------------
221 const art::Ptr<simb::MCTruth>& BackTrackerTruth::TrackIDToMCTruth(int const id) const
222 {
223  art::ServiceHandle<cheat::ParticleInventoryService> partInventory;
224  return partInventory->TrackIdToMCTruth_P(id);
225 }
226 
227 //----------------------------------------------------------------------
228 const art::Ptr<simb::MCTruth>& BackTrackerTruth::ParticleToMCTruth(const simb::MCParticle* p) const
229 {
230  art::ServiceHandle<cheat::ParticleInventoryService> partInventory;
231  return partInventory->ParticleToMCTruth_P(p);
232 }
233 
234 //----------------------------------------------------------------------
235 std::vector<const simb::MCParticle*> BackTrackerTruth::MCTruthToParticles(art::Ptr<simb::MCTruth> const& mct) const
236 {
237  art::ServiceHandle<cheat::ParticleInventoryService> partInventory;
238  return partInventory->MCTruthToParticles_Ps(mct);
239 }
240 
241 //----------------------------------------------------------------------
242 const std::vector< art::Ptr<simb::MCTruth> >& BackTrackerTruth::MCTruthVector() const
243 {
244  art::ServiceHandle<cheat::ParticleInventoryService> partInventory;
245  return partInventory->MCTruthVector_Ps();
246 }
247 
248 //----------------------------------------------------------------------
249 std::vector<sim::TrackIDE> BackTrackerTruth::HitToTrackID(detinfo::DetectorClocksData const& clockData,
250  recob::Hit const& hit) const
251 {
252  art::ServiceHandle<cheat::BackTrackerService> backTracker;
253  return backTracker->HitToTrackIDEs(clockData, hit);
254 }
255 
256 //----------------------------------------------------------------------
257 std::vector<sim::TrackIDE> BackTrackerTruth::HitToTrackID(detinfo::DetectorClocksData const& clockData,
258  art::Ptr<recob::Hit> const& hit) const
259 {
260  return HitToTrackID(clockData, *hit);
261 }
262 
263 //----------------------------------------------------------------------
264 std::vector<std::vector<art::Ptr<recob::Hit>>> BackTrackerTruth::TrackIDsToHits(detinfo::DetectorClocksData const& clockData,
265  std::vector<art::Ptr<recob::Hit>> const& allhits,
266  std::vector<int> const& tkIDs) const
267 {
268  std::vector<std::vector<art::Ptr<recob::Hit>>> tkIDsToHitsVec;
269  art::ServiceHandle<cheat::BackTrackerService> backTracker;
270 
271  for(const auto& tkID : tkIDs)
272  {
273  std::vector<art::Ptr<recob::Hit>> hitVec = backTracker->TrackIdToHits_Ps(clockData, tkID, allhits);
274  tkIDsToHitsVec.push_back(hitVec);
275  }
276 
277  return tkIDsToHitsVec;
278 }
279 
280 //----------------------------------------------------------------------
281 // plist is assumed to have adopted the appropriate EveIdCalculator prior to
282 // having been passed to this method. It is likely that the EmEveIdCalculator is
283 // the one you always want to use
284 std::vector<sim::TrackIDE> BackTrackerTruth::HitToEveID(detinfo::DetectorClocksData const& clockData,
285  art::Ptr<recob::Hit> const& hit) const
286 {
287  art::ServiceHandle<cheat::BackTrackerService> backTracker;
288  return backTracker->HitToEveTrackIDEs(clockData, hit);
289 }
290 
291 //----------------------------------------------------------------------
292 std::set<int> BackTrackerTruth::GetSetOfEveIDs() const
293 {
294  art::ServiceHandle<cheat::BackTrackerService> backTracker;
295  return backTracker->GetSetOfEveIds();
296 }
297 
298 //----------------------------------------------------------------------
300 {
301  art::ServiceHandle<cheat::BackTrackerService> backTracker;
302  return backTracker->GetSetOfTrackIds();
303 }
304 
305 //----------------------------------------------------------------------
307  std::vector< art::Ptr<recob::Hit> > const& hits) const
308 {
309  art::ServiceHandle<cheat::BackTrackerService> backTracker;
310  return backTracker->GetSetOfEveIds(clockData, hits);
311 }
312 
313 //----------------------------------------------------------------------
315  std::vector< art::Ptr<recob::Hit> > const& hits) const
316 {
317  art::ServiceHandle<cheat::BackTrackerService> backTracker;
318  return backTracker->GetSetOfTrackIds(clockData, hits);
319 }
320 
321 //----------------------------------------------------------------------
323  std::set<int> const& trackIDs,
324  std::vector< art::Ptr<recob::Hit> > const& hits) const
325 {
326  art::ServiceHandle<cheat::BackTrackerService> backTracker;
327  return backTracker->HitCollectionPurity(clockData, trackIDs, hits);
328 }
329 
330 //----------------------------------------------------------------------
332  std::set<int> const& trackIDs,
333  std::vector< art::Ptr<recob::Hit> > const& hits) const
334 {
335  art::ServiceHandle<cheat::BackTrackerService> backTracker;
336  return backTracker->HitChargeCollectionPurity(clockData, trackIDs, hits);
337 }
338 
339 
340 //----------------------------------------------------------------------
342  std::set<int> const& trackIDs,
343  std::vector< art::Ptr<recob::Hit> > const& hits,
344  std::vector< art::Ptr<recob::Hit> > const& allhits,
345  geo::View_t const view) const
346 {
347  art::ServiceHandle<cheat::BackTrackerService> backTracker;
348  return backTracker->HitCollectionEfficiency(clockData, trackIDs, hits, allhits, view);
349 }
350 
351 //----------------------------------------------------------------------
353  std::set<int> const& trackIDs,
354  std::vector< art::Ptr<recob::Hit> > const& hits,
355  std::vector< art::Ptr<recob::Hit> > const& allhits,
356  geo::View_t const view) const
357 {
358  art::ServiceHandle<cheat::BackTrackerService> backTracker;
359  return backTracker->HitChargeCollectionEfficiency(clockData, trackIDs, hits, allhits, view);
360 }
361 
362 //----------------------------------------------------------------------
363 std::vector<double> BackTrackerTruth::HitToXYZ(detinfo::DetectorClocksData const& clockData,
364  art::Ptr<recob::Hit> const& hit) const
365 {
366  art::ServiceHandle<cheat::BackTrackerService> backTracker;
367  return backTracker->HitToXYZ(clockData, hit);
368 }
369 
370 //----------------------------------------------------------------------
372  art::Ptr<recob::SpacePoint> const& spt,
373  art::Event const& evt,
374  std::string const& label) const
375 {
376  std::vector<double> hitVec = {0.,0.,0.};
377 
378  art::ServiceHandle<cheat::BackTrackerService> backTracker;
379  //backTracker->SpacePointHitsToWeightedXYZ(spt, evt, label);
380 
381  return hitVec;
382 }
383 
384 //----------------------------------------------------------------------
386  art::PtrVector<recob::Hit> const& hits) const
387 {
388  std::vector<art::Ptr<recob::Hit>> hitVec;
389  for(size_t idx=0; idx<hits.size(); idx++) hitVec.push_back(hits.at(idx));
390 
391  art::ServiceHandle<cheat::BackTrackerService> backTracker;
392  return backTracker->SpacePointHitsToWeightedXYZ(clockData, hitVec);
393 }
394 
395 //----------------------------------------------------------------------------
396 
397 DEFINE_ART_CLASS_TOOL(BackTrackerTruth)
398 }
void Rebuild(const art::Event &evt) override
This rebuilds the internal maps, is a noop for this module since the BackTracker is a service and reb...
Utilities related to art service access.
std::set< int > GetSetOfTrackIDs() const override
const simb::MCParticle * TrackIDToParticle(int const id) const override
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
Declaration of signal hit object.
const sim::ParticleList & ParticleList() const override
Get a reference to the ParticleList.
pdgs p
Definition: selectors.fcl:22
This provides an interface which defines truth matching functions made available to downstream analys...
const art::Ptr< simb::MCTruth > & TrackIDToMCTruth(int const id) const override
void reconfigure(fhicl::ParameterSet const &pset) override
const simb::MCParticle * TrackIDToMotherParticle(int const id) const override
process_name hit
Definition: cheaterreco.fcl:51
double HitChargeCollectionEfficiency(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIDs, std::vector< art::Ptr< recob::Hit > > const &hits, std::vector< art::Ptr< recob::Hit > > const &allhits, geo::View_t const view) const override
std::vector< std::vector< art::Ptr< recob::Hit > > > TrackIDsToHits(detinfo::DetectorClocksData const &clockData, std::vector< art::Ptr< recob::Hit >> const &allhits, std::vector< int > const &tkIDs) const override
std::vector< double > SpacePointToXYZ(detinfo::DetectorClocksData const &, art::Ptr< recob::SpacePoint > const &spt, art::Event const &evt, std::string const &label) const override
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
std::vector< double > HitToXYZ(detinfo::DetectorClocksData const &clockData, art::Ptr< recob::Hit > const &hit) const override
std::vector< sim::TrackIDE > HitToEveID(detinfo::DetectorClocksData const &clockData, art::Ptr< recob::Hit > const &hit) const override
BackTrackerTruth(fhicl::ParameterSet const &pset)
Constructor.
double HitCollectionPurity(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIDs, std::vector< art::Ptr< recob::Hit > > const &hits) const override
Description of geometry of one entire detector.
const art::Ptr< simb::MCTruth > & ParticleToMCTruth(const simb::MCParticle *p) const override
std::vector< sim::TrackIDE > HitToTrackID(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const override
Contains all timing reference information for the detector.
const std::vector< art::Ptr< simb::MCTruth > > & MCTruthVector() const override
std::set< int > GetSetOfEveIDs() const override
art::InputTag fTrackProducerLabel
tag for finding the tracks
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
TCEvent evt
Definition: DataStructs.cxx:8
const geo::GeometryCore * fGeometry
pointer to Geometry service
double HitChargeCollectionPurity(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIDs, std::vector< art::Ptr< recob::Hit > > const &hits) const override
std::vector< const simb::MCParticle * > MCTruthToParticles(art::Ptr< simb::MCTruth > const &mct) const override
std::vector< double > SpacePointHitsToXYZ(detinfo::DetectorClocksData const &clockData, art::PtrVector< recob::Hit > const &hits) const override
art framework interface to geometry description
double HitCollectionEfficiency(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIDs, std::vector< art::Ptr< recob::Hit > > const &hits, std::vector< art::Ptr< recob::Hit > > const &allhits, geo::View_t const view) const override