LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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/FindManyP.h" //needed only if you do not use the proxies
18 #include "canvas/Persistency/Common/FindMany.h" //needed only if you do not use the proxies
19 
20 #include <vector>
21 
22 //
36 //
37 
39 public:
40 
41  struct Config {
43  fhicl::Name("trackInputTag"),
44  fhicl::Comment("data product tag for tracks")
45  };
47  fhicl::Name("vertexInputTag"),
48  fhicl::Comment("data product tag for vertices")
49  };
51  fhicl::Name("mcsInputTag"),
52  fhicl::Comment("data product tag for track momentum reconstruction")
53  };
54  }; // Config
55 
57 
58  explicit RecoProxyUsageExample(Parameters const & p);
59  // The compiler-generated destructor is fine for non-base
60  // classes without bare pointers or other resource use.
61 
62  // Plugins should not be copied or assigned.
67 
68  // Required functions.
69  void analyze(art::Event const & e) override;
70 
71 private:
72 
73  // Declare member data here.
75 };
76 
77 
79  : EDAnalyzer(config)
80  , trkTag(config().trackInputTag())
81  , vtxTag(config().vertexInputTag())
82  , mcsTag(config().mcsInputTag())
83 {}
84 
86 {
87 
88  //
89  // Example using proxies.
90  //
91 
92  //
93  // Get vertex collection proxy and associated tracks, with meta data
94  auto const& vertices = proxy::getCollection<std::vector<recob::Vertex> >(e,vtxTag,proxy::withAssociatedMeta<recob::Track, recob::VertexAssnMeta>());
95  //
96  // Get track collection proxy and parallel mcs fit data (associated hits loaded by default)
97  // Note: if tracks were produced from a TrackTrajectory collection you could access the original trajectories adding ',proxy::withOriginalTrajectory()' to the list of arguments
98  auto const& tracks = proxy::getCollection<proxy::Tracks>(e,trkTag,proxy::withParallelData<recob::MCSFitResult>(mcsTag));
99  //
100  // Loop over vertex proxies (get recob::Vertex with '->')
101  for (const auto& v : vertices) {
102  mf::LogVerbatim("ProxyExample") << "vertex pos=" << v->position() << " chi2=" << v->chi2();
103  //
104  // Get tracks(+meta) associated to vertex, and loop over them
105  const auto& assocTracks = v.get<recob::Track>();
106  for (const auto& trackAssn : assocTracks) {
107  //
108  // Note that here we access the methods of recob::Track using '->' and that we get the recob::VertexAssnMeta with '.data()'
109  mf::LogVerbatim("ProxyExample") << "track with key=" << trackAssn.key() << " and length=" << trackAssn->Length() << " has propDist from vertex=" << trackAssn.data().propDist();
110  //
111  // 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
112  const auto& track = tracks[trackAssn.key()];
113  const recob::MCSFitResult& assocMCS = track.get<recob::MCSFitResult>();
114  //
115  // Print some information; here we access the methods of recob::Track using '->' and proxy::Track with '.'
116  // Note: if the original trajectories were associated to the proxy, you could get the original/unfitted length with 'track(proxy::Tracks::Unfitted)->Length()'
117  mf::LogVerbatim("ProxyExample") << "\tCountValidPoints=" << track->CountValidPoints() << " and nHits=" << track.nHits() << " and MCSMom=" << assocMCS.bestMomentum();
118  //
119  // Now loop over the associated hits from the track proxy
120  if (track.nHits()<50) {
121  for (const art::Ptr<recob::Hit>& h : track.hits()) {
122  mf::LogVerbatim("ProxyExample") << "\t\thit wire=" << h->WireID() << " peak time=" << h->PeakTime();
123  }
124  }
125  } // for associated tracks
126  } // for vertices
127 
128  //
129  // Same example without using proxies.
130  //
131 
132  //
133  // Get vertex collection handle and get associated tracks with meta data using FindManyP
134  auto const& vertexHandle = e.getValidHandle<std::vector<recob::Vertex> >(vtxTag);
135  auto const& vertexColl = *vertexHandle;
136  art::FindManyP<recob::Track, recob::VertexAssnMeta> assocTracksWithMeta(vertexHandle, e, vtxTag);
137  //
138  // Get track collection handle, get associated hits using FindMany, and get mcs collection (parallel to track collection)
139  auto const& trackHandle = e.getValidHandle<std::vector<recob::Track> >(trkTag);
140  art::FindMany<recob::Hit> assocHits(trackHandle, e, trkTag);
141  auto const& mcsColl = *(e.getValidHandle<std::vector<recob::MCSFitResult> >(mcsTag));
142  //
143  // Loop over the vertex collection
144  for (size_t iv=0; iv<vertexColl.size(); ++iv) {
145  const recob::Vertex& v = vertexColl[iv];
146  mf::LogVerbatim("ProxyExample") << "vertex pos=" << v.position() << " chi2=" << v.chi2();
147  //
148  // Get tracks(+meta) associated to vertex, and loop over them
149  auto const& assocTks = assocTracksWithMeta.at(iv);
150  auto const& assocTksMeta = assocTracksWithMeta.data(iv);
151  for (size_t itk=0;itk<assocTks.size();++itk) {
152  //
153  // Get the recob::Track and VertexAssnMeta instance
154  const art::Ptr<recob::Track>& trackAssn = assocTks[itk];
155  const recob::VertexAssnMeta* trackMeta = assocTksMeta[itk];
156  mf::LogVerbatim("ProxyExample") << "track with key=" << trackAssn.key() << " and length=" << trackAssn->Length() << " has propDist from vertex=" << trackMeta->propDist();
157  //
158  // Get the associated recob::Hit and the MCSFitResult
159  const recob::MCSFitResult& assocMCS = mcsColl[trackAssn.key()];
160  const std::vector<recob::Hit const*>& hits = assocHits.at(trackAssn.key());
161  //
162  // Print some information
163  mf::LogVerbatim("ProxyExample") << "\tCountValidPoints=" << trackAssn->CountValidPoints() << " and nHits=" << hits.size() << " and MCSMom=" << assocMCS.bestMomentum();
164  //
165  // Now loop over the associated hits
166  if (hits.size()<50) {
167  for (const recob::Hit* h : hits) {
168  mf::LogVerbatim("ProxyExample") << "\t\thit wire=" << h->WireID() << " peak time=" << h->PeakTime();
169  }
170  }
171  } // for track
172  } // for vertex
173 
174 } // RecoProxyUsageExample::analyze()
175 
key_type key() const
Definition: Ptr.h:356
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:458
Definition of vertex object for LArSoft.
Definition: Vertex.h:35
double chi2() const
Definition: Vertex.h:64
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:115
float bestMomentum() const
momentum for best direction fit
Definition: MCSFitResult.h:56
void hits()
Definition: readHits.C:15
double Length(size_t p=0) const
Access to various track properties.
Definition: Track.h:170
Example of analyzer accessing vertices, tracks, and hits, using RecoBaseProxy.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
Provides recob::Track data product.
Class storing the meta-data for track-vertex association: status, propagation distance, impact parameter, impact parameter error, chi2.
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
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
Offers proxy::Tracks and proxy::Track class for recob::Track access.
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:49
fhicl::Atom< art::InputTag > vertexInputTag
Float_t e
Definition: plot.C:34
Float_t track
Definition: plot.C:34
const Point_t & position() const
Return vertex 3D position.
Definition: Vertex.h:60
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:52
RecoProxyUsageExample(Parameters const &p)