All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackScatterClosestApproach_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: TrackScatterClosestApproach
3 // Plugin Type: producer (art v3_06_03)
4 // File: TrackScatterClosestApproach_module.cc
5 //
6 // Generated at Mon Mar 1 10:30:45 2021 by Edward Tyley using cetskelgen
7 // from cetlib version v3_11_01.
8 //
9 // Producer to look at the average distance of closest approach from
10 // the centroid of a track to quantify the scattering
11 ////////////////////////////////////////////////////////////////////////
12 
13 #include "art/Framework/Core/EDProducer.h"
14 #include "art/Framework/Core/ModuleMacros.h"
15 #include "art/Framework/Principal/Event.h"
16 #include "art/Framework/Principal/Handle.h"
17 #include "art/Framework/Principal/Run.h"
18 #include "art/Framework/Principal/SubRun.h"
19 #include "canvas/Utilities/InputTag.h"
20 #include "fhiclcpp/ParameterSet.h"
22 #include "messagefacility/MessageLogger/MessageLogger.h"
23 
26 
27 #include <memory>
28 
29 namespace sbn {
30 class TrackScatterClosestApproach : public art::EDProducer {
31  public:
32  explicit TrackScatterClosestApproach(fhicl::ParameterSet const& p);
33  // The compiler-generated destructor is fine for non-base
34  // classes without bare pointers or other resource use.
35 
36  // Plugins should not be copied or assigned.
41 
42  // Required functions.
43  void produce(art::Event& e) override;
44 
45  private:
46  // Declare member data here.
47  const art::InputTag fTrackLabel;
48  const float fMinTrackLength;
49 
51 };
52 
54  : EDProducer { p }
55  , fTrackLabel(p.get<std::string>("TrackLabel"))
56  , fMinTrackLength(p.get<float>("MinTrackLength"))
57 {
58  produces<std::vector<ScatterClosestApproach>>();
59  produces<art::Assns<recob::Track, ScatterClosestApproach>>();
60 }
61 
63 {
64  // Implementation of required member function here.
65  auto const trackHandle(e.getValidHandle<std::vector<recob::Track>>(fTrackLabel));
66 
67  std::vector<art::Ptr<recob::Track>> tracks;
68  art::fill_ptr_vector(tracks, trackHandle);
69 
70  auto closestapproachVec = std::make_unique<std::vector<ScatterClosestApproach>>();
71  auto trackAssns = std::make_unique<art::Assns<recob::Track, ScatterClosestApproach>>();
72 
73  for (auto const& track : tracks) {
74 
75  if (track->Length() < fMinTrackLength)
76  continue;
77 
78  ScatterClosestApproach closestapproach(this->CalculateClosestApproach(*track));
79 
80  closestapproachVec->push_back(closestapproach);
81  util::CreateAssn(*this, e, *closestapproachVec, track, *trackAssns);
82  }
83 
84  e.put(std::move(closestapproachVec));
85  e.put(std::move(trackAssns));
86 }
87 
89 {
90  // Interpolate the start and end of the track to find the centroid axis
91  const TVector3 start(track.Start<TVector3>());
92  const TVector3 dir((start - track.End<TVector3>()).Unit());
93 
94  // Calculate the perpendicular distance from the centroid to each traj point
95  float sumClosestApproach(0), maxClosestApproach(0);
96  unsigned int counter(0);
97  for (size_t i = 0; i < track.NumberTrajectoryPoints(); i++) {
98  if (!track.HasValidPoint(i))
99  continue;
100  counter++;
101 
102  const TVector3 pos(track.LocationAtPoint<TVector3>(i));
103  const TVector3 disp(pos - start);
104  const float proj(disp.Dot(dir));
105  const float thisClosestApproach((disp - proj * dir).Mag());
106 
107  sumClosestApproach += thisClosestApproach;
108  maxClosestApproach = std::max(maxClosestApproach, thisClosestApproach);
109  }
110 
111  if (!counter)
112  return ScatterClosestApproach();
113 
114  const float meanClosestApproach(sumClosestApproach / counter);
115 
116  // Calculate the spread in ClosestApproach around the mean value
117  float sumStdDev(0);
118  for (size_t i = 0; i < track.NumberTrajectoryPoints(); i++) {
119  if (!track.HasValidPoint(i))
120  continue;
121 
122  const TVector3 pos(track.LocationAtPoint<TVector3>(i));
123  const TVector3 disp(pos - start);
124  const float proj(disp.Dot(dir));
125  const float thisClosestApproach((disp - proj * dir).Mag());
126  const float thisClosestApproachDev(thisClosestApproach - meanClosestApproach);
127 
128  sumStdDev += thisClosestApproachDev * thisClosestApproachDev;
129  }
130 
131  const float stdDevClosestApproach(std::sqrt(sumStdDev / counter));
132 
133  return ScatterClosestApproach(meanClosestApproach, stdDevClosestApproach, maxClosestApproach);
134 }
135 }
136 
137 DEFINE_ART_MODULE(sbn::TrackScatterClosestApproach)
ClusterModuleLabel join with tracks
ScatterClosestApproach CalculateClosestApproach(const recob::Track &track) const
Point_t const & LocationAtPoint(size_t i) const
pdgs p
Definition: selectors.fcl:22
bool HasValidPoint(size_t i) const
size_t NumberTrajectoryPoints() const
Various functions related to the presence and the number of (valid) points.
process_name use argoneut_mc_hitfinder track
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:285
Point_t const & Start() const
Access to track position at different points.
Provides recob::Track data product.
TrackScatterClosestApproach(fhicl::ParameterSet const &p)
tuple dir
Definition: dropbox.py:28
bool CreateAssn(art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t index=UINT_MAX)
Creates a single one-to-one association.
do i e
Point_t const & End() const
TrackScatterClosestApproach & operator=(TrackScatterClosestApproach const &)=delete
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track: