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 
21 //
22  /**
23  * @file larexamples/RecoProxyUsageExample/RecoProxyUsageExample_module.cc
24  * @class RecoProxyUsageExample
25  *
26  * @brief Example of analyzer accessing vertices, tracks, and hits, using RecoBaseProxy.
27  *
28  * The corresponding code without using RecoBaseProxy is also provided as a reference.
29  * @see @ref LArSoftProxies.
30  *
31  * @author G. Cerati (FNAL, MicroBooNE)
32  * @date 2018
33  * @version 1.0
34  */
35 //
36 
38 public:
39 
40  struct Config {
42  fhicl::Name("trackInputTag"),
43  fhicl::Comment("data product tag for tracks")
44  };
46  fhicl::Name("vertexInputTag"),
47  fhicl::Comment("data product tag for vertices")
48  };
50  fhicl::Name("mcsInputTag"),
51  fhicl::Comment("data product tag for track momentum reconstruction")
52  };
53  }; // Config
54 
56 
57  explicit RecoProxyUsageExample(Parameters const & p);
58  // The compiler-generated destructor is fine for non-base
59  // classes without bare pointers or other resource use.
60 
61  // Plugins should not be copied or assigned.
66 
67  // Required functions.
68  void analyze(art::Event const & e) override;
69 
70 private:
71 
72  // Declare member data here.
74 };
75 
76 
78  : EDAnalyzer(config)
79  , trkTag(config().trackInputTag())
80  , vtxTag(config().vertexInputTag())
81  , mcsTag(config().mcsInputTag())
82 {}
83 
85 {
86 
87  //
88  // Example using proxies.
89  //
90 
91  //
92  // Get vertex collection proxy and associated tracks, with meta data
93  auto const& vertices = proxy::getCollection<std::vector<recob::Vertex> >(e,vtxTag,proxy::withAssociatedMeta<recob::Track, recob::VertexAssnMeta>());
94  //
95  // Get track collection proxy and parallel mcs fit data (associated hits loaded by default)
96  // Note: if tracks were produced from a TrackTrajectory collection you could access the original trajectories adding ',proxy::withOriginalTrajectory()' to the list of arguments
97  auto const& tracks = proxy::getCollection<proxy::Tracks>(e,trkTag,proxy::withParallelData<recob::MCSFitResult>(mcsTag));
98  //
99  // Loop over vertex proxies (get recob::Vertex with '->')
100  for (const auto& v : vertices) {
101  mf::LogVerbatim("ProxyExample") << "vertex pos=" << v->position() << " chi2=" << v->chi2();
102  //
103  // Get tracks(+meta) associated to vertex, and loop over them
104  const auto& assocTracks = v.get<recob::Track>();
105  for (const auto& trackAssn : assocTracks) {
106  //
107  // Note that here we access the methods of recob::Track using '->' and that we get the recob::VertexAssnMeta with '.data()'
108  mf::LogVerbatim("ProxyExample") << "track with key=" << trackAssn.key() << " and length=" << trackAssn->Length() << " has propDist from vertex=" << trackAssn.data().propDist();
109  //
110  // 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
111  const auto& track = tracks[trackAssn.key()];
112  const recob::MCSFitResult& assocMCS = track.get<recob::MCSFitResult>();
113  //
114  // Print some information; here we access the methods of recob::Track using '->' and proxy::Track with '.'
115  // Note: if the original trajectories were associated to the proxy, you could get the original/unfitted length with 'track(proxy::Tracks::Unfitted)->Length()'
116  mf::LogVerbatim("ProxyExample") << "\tCountValidPoints=" << track->CountValidPoints() << " and nHits=" << track.nHits() << " and MCSMom=" << assocMCS.bestMomentum();
117  //
118  // Now loop over the associated hits from the track proxy
119  if (track.nHits()<50) {
120  for (const art::Ptr<recob::Hit>& h : track.hits()) {
121  mf::LogVerbatim("ProxyExample") << "\t\thit wire=" << h->WireID() << " peak time=" << h->PeakTime();
122  }
123  }
124  } // for associated tracks
125  } // for vertices
126 
127  //
128  // Same example without using proxies.
129  //
130 
131  //
132  // Get vertex collection handle and get associated tracks with meta data using FindManyP
133  auto const& vertexHandle = e.getValidHandle<std::vector<recob::Vertex> >(vtxTag);
134  auto const& vertexColl = *vertexHandle;
135  art::FindManyP<recob::Track, recob::VertexAssnMeta> assocTracksWithMeta(vertexHandle, e, vtxTag);
136  //
137  // Get track collection handle, get associated hits using FindMany, and get mcs collection (parallel to track collection)
138  auto const& trackHandle = e.getValidHandle<std::vector<recob::Track> >(trkTag);
139  art::FindMany<recob::Hit> assocHits(trackHandle, e, trkTag);
140  auto const& mcsColl = *(e.getValidHandle<std::vector<recob::MCSFitResult> >(mcsTag));
141  //
142  // Loop over the vertex collection
143  for (size_t iv=0; iv<vertexColl.size(); ++iv) {
144  const recob::Vertex& v = vertexColl[iv];
145  mf::LogVerbatim("ProxyExample") << "vertex pos=" << v.position() << " chi2=" << v.chi2();
146  //
147  // Get tracks(+meta) associated to vertex, and loop over them
148  auto const& assocTks = assocTracksWithMeta.at(iv);
149  auto const& assocTksMeta = assocTracksWithMeta.data(iv);
150  for (size_t itk=0;itk<assocTks.size();++itk) {
151  //
152  // Get the recob::Track and VertexAssnMeta instance
153  const art::Ptr<recob::Track>& trackAssn = assocTks[itk];
154  const recob::VertexAssnMeta* trackMeta = assocTksMeta[itk];
155  mf::LogVerbatim("ProxyExample") << "track with key=" << trackAssn.key() << " and length=" << trackAssn->Length() << " 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") << "\tCountValidPoints=" << trackAssn->CountValidPoints() << " and nHits=" << hits.size() << " and MCSMom=" << assocMCS.bestMomentum();
163  //
164  // Now loop over the associated hits
165  if (hits.size()<50) {
166  for (const recob::Hit* h : hits) {
167  mf::LogVerbatim("ProxyExample") << "\t\thit wire=" << h->WireID() << " peak time=" << h->PeakTime();
168  }
169  }
170  } // for track
171  } // for vertex
172 
173 } // RecoProxyUsageExample::analyze()
174 
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
float propDist() const
ChannelGroupService::Name Name
RecoProxyUsageExample & operator=(RecoProxyUsageExample const &)=delete
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
Definition of vertex object for LArSoft.
Definition: Vertex.h:35
double chi2() const
Definition: Vertex.h:64
Offers proxy::Tracks and proxy::Track class for recob::Track access.
void analyze(art::Event const &e) override
unsigned int CountValidPoints() const
Definition: Track.h:112
float bestMomentum() const
momentum for best direction fit
Definition: MCSFitResult.h:56
const double e
double Length(size_t p=0) const
Access to various track properties.
Definition: Track.h:167
Example of analyzer accessing vertices, tracks, and hits, using RecoBaseProxy.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
key_type key() const noexcept
Definition: Ptr.h:216
Class storing the meta-data for track-vertex association: status, propagation distance, impact parameter, impact parameter error, chi2.
p
Definition: test.py:223
Class storing the result of the Maximum Likelihood fit of Multiple Coulomb Scattering angles between ...
Definition: MCSFitResult.h:19
Definition: tracks.py:1
fhicl::Atom< art::InputTag > mcsInputTag
fhicl::Atom< art::InputTag > trackInputTag
#define Comment
Provides recob::Track data product.
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
fhicl::Atom< art::InputTag > vertexInputTag
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:49
RecoProxyUsageExample(Parameters const &p)