LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
RecoProxyUsageExample_module.cc
Go to the documentation of this file.
6 #include "fhiclcpp/types/Atom.h"
7 #include "messagefacility/MessageLogger/MessageLogger.h" // mf::LogVerbatim
8 
13 
14 #include "lardata/RecoBaseProxy/Track.h" //needed only if you do use the proxies
15 //#include "lardata/RecoBaseProxy/ProxyBase.h" //needed if you use proxies (already included above if using the Track proxy)
16 
17 #include "canvas/Persistency/Common/FindMany.h" //needed only if you do not use the proxies
18 #include "canvas/Persistency/Common/FindManyP.h" //needed only if you do not use the proxies
19 
20 //
34 //
35 
37 public:
38  struct Config {
40  fhicl::Comment("data product tag for tracks")};
42  fhicl::Comment("data product tag for vertices")};
44  fhicl::Name("mcsInputTag"),
45  fhicl::Comment("data product tag for track momentum reconstruction")};
46  }; // Config
47 
49 
50  explicit RecoProxyUsageExample(Parameters const& p);
51  // The compiler-generated destructor is fine for non-base
52  // classes without bare pointers or other resource use.
53 
54  // Plugins should not be copied or assigned.
59 
60  // Required functions.
61  void analyze(art::Event const& e) override;
62 
63 private:
64  // Declare member data here.
66 };
67 
69  : EDAnalyzer(config)
70  , trkTag(config().trackInputTag())
71  , vtxTag(config().vertexInputTag())
72  , mcsTag(config().mcsInputTag())
73 {}
74 
76 {
77 
78  //
79  // Example using proxies.
80  //
81 
82  //
83  // Get vertex collection proxy and associated tracks, with meta data
84  auto const& vertices = proxy::getCollection<std::vector<recob::Vertex>>(
85  e, vtxTag, proxy::withAssociatedMeta<recob::Track, recob::VertexAssnMeta>());
86  //
87  // Get track collection proxy and parallel mcs fit data (associated hits loaded by default)
88  // Note: if tracks were produced from a TrackTrajectory collection you could access the original trajectories adding ',proxy::withOriginalTrajectory()' to the list of arguments
89  auto const& tracks = proxy::getCollection<proxy::Tracks>(
90  e, trkTag, proxy::withParallelData<recob::MCSFitResult>(mcsTag));
91  //
92  // Loop over vertex proxies (get recob::Vertex with '->')
93  for (const auto& v : vertices) {
94  mf::LogVerbatim("ProxyExample") << "vertex pos=" << v->position() << " chi2=" << v->chi2();
95  //
96  // Get tracks(+meta) associated to vertex, and loop over them
97  const auto& assocTracks = v.get<recob::Track>();
98  for (const auto& trackAssn : assocTracks) {
99  //
100  // Note that here we access the methods of recob::Track using '->' and that we get the recob::VertexAssnMeta with '.data()'
101  mf::LogVerbatim("ProxyExample")
102  << "track with key=" << trackAssn.key() << " and length=" << trackAssn->Length()
103  << " has propDist from vertex=" << trackAssn.data().propDist();
104  //
105  // Now get the track proxy from the key, and use it to access the parallel MCSFitResult; note that the track proxy has already access to the associated hits
106  const auto& track = tracks[trackAssn.key()];
107  const recob::MCSFitResult& assocMCS = track.get<recob::MCSFitResult>();
108  //
109  // Print some information; here we access the methods of recob::Track using '->' and proxy::Track with '.'
110  // Note: if the original trajectories were associated to the proxy, you could get the original/unfitted length with 'track(proxy::Tracks::Unfitted)->Length()'
111  mf::LogVerbatim("ProxyExample")
112  << "\tCountValidPoints=" << track->CountValidPoints() << " and nHits=" << track.nHits()
113  << " and MCSMom=" << assocMCS.bestMomentum();
114  //
115  // Now loop over the associated hits from the track proxy
116  if (track.nHits() < 50) {
117  for (const art::Ptr<recob::Hit>& h : track.hits()) {
118  mf::LogVerbatim("ProxyExample")
119  << "\t\thit wire=" << h->WireID() << " peak time=" << h->PeakTime();
120  }
121  }
122  } // for associated tracks
123  } // for vertices
124 
125  //
126  // Same example without using proxies.
127  //
128 
129  //
130  // Get vertex collection handle and get associated tracks with meta data using FindManyP
131  auto const& vertexHandle = e.getValidHandle<std::vector<recob::Vertex>>(vtxTag);
132  auto const& vertexColl = *vertexHandle;
133  art::FindManyP<recob::Track, recob::VertexAssnMeta> assocTracksWithMeta(vertexHandle, e, vtxTag);
134  //
135  // Get track collection handle, get associated hits using FindMany, and get mcs collection (parallel to track collection)
136  auto const& trackHandle = e.getValidHandle<std::vector<recob::Track>>(trkTag);
137  art::FindMany<recob::Hit> assocHits(trackHandle, e, trkTag);
138  auto const& mcsColl = *(e.getValidHandle<std::vector<recob::MCSFitResult>>(mcsTag));
139  //
140  // Loop over the vertex collection
141  for (size_t iv = 0; iv < vertexColl.size(); ++iv) {
142  const recob::Vertex& v = vertexColl[iv];
143  mf::LogVerbatim("ProxyExample") << "vertex pos=" << v.position() << " chi2=" << v.chi2();
144  //
145  // Get tracks(+meta) associated to vertex, and loop over them
146  auto const& assocTks = assocTracksWithMeta.at(iv);
147  auto const& assocTksMeta = assocTracksWithMeta.data(iv);
148  for (size_t itk = 0; itk < assocTks.size(); ++itk) {
149  //
150  // Get the recob::Track and VertexAssnMeta instance
151  const art::Ptr<recob::Track>& trackAssn = assocTks[itk];
152  const recob::VertexAssnMeta* trackMeta = assocTksMeta[itk];
153  mf::LogVerbatim("ProxyExample")
154  << "track with key=" << trackAssn.key() << " and length=" << trackAssn->Length()
155  << " has propDist from vertex=" << trackMeta->propDist();
156  //
157  // Get the associated recob::Hit and the MCSFitResult
158  const recob::MCSFitResult& assocMCS = mcsColl[trackAssn.key()];
159  const std::vector<recob::Hit const*>& hits = assocHits.at(trackAssn.key());
160  //
161  // Print some information
162  mf::LogVerbatim("ProxyExample")
163  << "\tCountValidPoints=" << trackAssn->CountValidPoints() << " and nHits=" << hits.size()
164  << " and MCSMom=" << assocMCS.bestMomentum();
165  //
166  // Now loop over the associated hits
167  if (hits.size() < 50) {
168  for (const recob::Hit* h : hits) {
169  mf::LogVerbatim("ProxyExample")
170  << "\t\thit wire=" << h->WireID() << " peak time=" << h->PeakTime();
171  }
172  }
173  } // for track
174  } // for vertex
175 
176 } // RecoProxyUsageExample::analyze()
177 
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
float propDist() const
RecoProxyUsageExample & operator=(RecoProxyUsageExample const &)=delete
data_const_reference data(size_type i) const
Definition: FindManyP.h:446
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
Definition of vertex object for LArSoft.
Definition: Vertex.h:35
double chi2() const
Definition: Vertex.h:68
void analyze(art::Event const &e) override
unsigned int CountValidPoints() const
Various functions related to the presence and the number of (valid) points.
Definition: Track.h:146
float bestMomentum() const
momentum for best direction fit
Definition: MCSFitResult.h:73
void hits()
Definition: readHits.C:15
double Length(size_t p=0) const
Access to various track properties.
Definition: Track.h:207
Example of analyzer accessing vertices, tracks, and hits, using RecoBaseProxy.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
Offers proxy::Tracks and proxy::Track class for recob::Track access.
key_type key() const noexcept
Definition: Ptr.h:166
Class storing the meta-data for track-vertex association: status, propagation distance, impact parameter, impact parameter error, chi2.
Provides recob::Track data product.
Class storing the result of the Maximum Likelihood fit of Multiple Coulomb Scattering angles between ...
Definition: MCSFitResult.h:19
fhicl::Atom< art::InputTag > mcsInputTag
fhicl::Atom< art::InputTag > trackInputTag
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:46
fhicl::Atom< art::InputTag > vertexInputTag
Float_t e
Definition: plot.C:35
Float_t track
Definition: plot.C:35
const Point_t & position() const
Return vertex 3D position.
Definition: Vertex.h:64
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
Definition: Track.h:49
RecoProxyUsageExample(Parameters const &p)