All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RecoProxyUsageExample_module.cc
Go to the documentation of this file.
1 #include "art/Framework/Core/EDAnalyzer.h"
2 #include "art/Framework/Core/ModuleMacros.h"
3 #include "art/Framework/Principal/Event.h"
4 #include "art/Framework/Principal/Handle.h"
5 #include "canvas/Utilities/InputTag.h"
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 
37 class RecoProxyUsageExample : public art::EDAnalyzer {
38 public:
39 
40  struct Config {
41  fhicl::Atom<art::InputTag> trackInputTag{
42  fhicl::Name("trackInputTag"),
43  fhicl::Comment("data product tag for tracks")
44  };
45  fhicl::Atom<art::InputTag> vertexInputTag{
46  fhicl::Name("vertexInputTag"),
47  fhicl::Comment("data product tag for vertices")
48  };
49  fhicl::Atom<art::InputTag> mcsInputTag{
50  fhicl::Name("mcsInputTag"),
51  fhicl::Comment("data product tag for track momentum reconstruction")
52  };
53  }; // Config
54 
55  using Parameters = art::EDAnalyzer::Table<Config>;
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.
73  art::InputTag trkTag, vtxTag, mcsTag;
74 };
75 
76 
78  : EDAnalyzer(config)
79  , trkTag(config().trackInputTag())
80  , vtxTag(config().vertexInputTag())
81  , mcsTag(config().mcsInputTag())
82 {}
83 
84 void RecoProxyUsageExample::analyze(art::Event const & e)
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 
175 DEFINE_ART_MODULE(RecoProxyUsageExample)
ClusterModuleLabel join with tracks
float propDist() const
pdgs p
Definition: selectors.fcl:22
RecoProxyUsageExample & operator=(RecoProxyUsageExample const &)=delete
process_name use argoneut_mc_hitfinder track
Definition of vertex object for LArSoft.
Definition: Vertex.h:35
Offers proxy::Tracks and proxy::Track class for recob::Track access.
double chi2() const
Definition: Vertex.h:64
while getopts h
void analyze(art::Event const &e) override
float bestMomentum() const
momentum for best direction fit
Definition: MCSFitResult.h:56
double Length(size_t p=0) const
Access to various track properties.
Example of analyzer accessing vertices, tracks, and hits, using RecoBaseProxy.
Class storing the meta-data for track-vertex association: status, propagation distance, impact parameter, impact parameter error, chi2.
BEGIN_PROLOG vertical distance to the surface Name
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
do i e
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
fhicl::Atom< art::InputTag > vertexInputTag
art::EDAnalyzer::Table< Config > Parameters
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 &quot;fitted&quot; track:
RecoProxyUsageExample(Parameters const &p)