All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SpacePointFinder_module.cc
Go to the documentation of this file.
1 //
2 // Name: SpacePointFinder_module.cc
3 //
4 // Purpose: Module SpacePointFinder.
5 //
6 // Configuration parameters.
7 //
8 // ClusterModuleLabel: // Cluster module label (e.g. "dbcluster").
9 // MinHits: // Ignore clusters with fewer than this number of hits.
10 // ClusterAssns: // If true, make associations between space points and clusters.
11 // SpacePointAlg: // Configuration for SpacePointtAlg algoriithm.
12 //
13 // Created: 15-Dec-2011 H. Greenlee
14 //
15 
16 #include <cassert>
17 
18 #include "art/Framework/Core/EDProducer.h"
19 #include "art/Framework/Core/ModuleMacros.h"
20 #include "art/Framework/Principal/Event.h"
21 #include "art/Framework/Services/Registry/ServiceHandle.h"
22 #include "canvas/Persistency/Common/FindManyP.h"
23 #include "messagefacility/MessageLogger/MessageLogger.h"
24 
33 
34 namespace trkf {
35 
36  class SpacePointFinder : public art::EDProducer {
37  public:
38  explicit SpacePointFinder(fhicl::ParameterSet const& pset);
39 
40  private:
41  void produce(art::Event& evt) override;
42  void endJob() override;
43 
44  // Fcl Attributes.
45 
46  SpacePointAlg fSptalg; // Algorithm object.
47  std::string fClusterModuleLabel;
48  unsigned int fMinHits; // Minimum number of hits per cluster.
49  bool fClusterAssns; // Make Cluster-SpacePoint associations.
50 
51  // Statistics.
52 
53  int fNumEvent; // Number of events.
54  int fNumSpt2; // Number of 2-view space points.
55  int fNumSpt3; // Number of 3-view space points.
56  };
57 
58  DEFINE_ART_MODULE(SpacePointFinder)
59 
60  //----------------------------------------------------------------------------
61  SpacePointFinder::SpacePointFinder(const fhicl::ParameterSet& pset)
62  //
63  // Purpose: Constructor.
64  //
65  // Arguments: pset - Module parameters.
66  //
67  : EDProducer{pset}
68  , fSptalg(pset.get<fhicl::ParameterSet>("SpacePointAlg"))
69  , fMinHits(0)
70  , fClusterAssns(false)
71  , fNumEvent(0)
72  , fNumSpt2(0)
73  , fNumSpt3(0)
74  {
75  fClusterModuleLabel = pset.get<std::string>("ClusterModuleLabel");
76  fMinHits = pset.get<unsigned int>("MinHits");
77  fClusterAssns = pset.get<bool>("ClusterAssns");
78 
79  produces<std::vector<art::PtrVector<recob::SpacePoint>>>();
80  produces<std::vector<recob::SpacePoint>>();
81  produces<art::Assns<recob::SpacePoint, recob::Hit>>();
82  if (fClusterAssns) produces<art::Assns<recob::SpacePoint, recob::Cluster>>();
83 
84  // Report.
85 
86  mf::LogInfo("SpacePointFinder")
87  << "SpacePointFinder configured with the following parameters:\n"
88  << " ClusterModuleLabel = " << fClusterModuleLabel << "\n"
89  << " Minimum Hits per Cluster = " << fMinHits << "\n"
90  << " Cluster associations = " << fClusterAssns;
91  }
92 
93  //----------------------------------------------------------------------------
94  void
96  //
97  // Purpose: Produce method.
98  //
99  // Arguments: event - Art event.
100  //
101  {
102  ++fNumEvent;
103 
104  // Get Services.
105 
106  art::ServiceHandle<geo::Geometry const> geom;
107 
108  // Get clusters.
109 
110  art::Handle<std::vector<recob::Cluster>> clusterh;
111  evt.getByLabel(fClusterModuleLabel, clusterh);
112 
113  // Make a double or triple loop over clusters in distinct views
114  // (depending on minimum number of views configured in SpacePointAlg).
115 
116  if (clusterh.isValid()) {
117 
118  // Make a collection of space points that will be inserted into the event.
119 
120  std::unique_ptr<std::vector<art::PtrVector<recob::SpacePoint>>> sptvecs(
121  new std::vector<art::PtrVector<recob::SpacePoint>>);
122  std::unique_ptr<std::vector<recob::SpacePoint>> spts(new std::vector<recob::SpacePoint>);
123  std::unique_ptr<art::Assns<recob::SpacePoint, recob::Hit>> sphitassn(
124  new art::Assns<recob::SpacePoint, recob::Hit>);
125  std::unique_ptr<art::Assns<recob::SpacePoint, recob::Cluster>> spclassn(
126  new art::Assns<recob::SpacePoint, recob::Cluster>);
127 
128  // Make a hit vector which will be used to store hits to be passed
129  // to SpacePointAlg.
130 
131  art::PtrVector<recob::Hit> hits;
132  art::FindManyP<recob::Hit> fm(clusterh, evt, fClusterModuleLabel);
133 
134  auto const clockData =
135  art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
136  auto const detProp =
137  art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(evt);
138 
139  // Loop over first cluster.
140 
141  int nclus = clusterh->size();
142  for (int iclus = 0; iclus < nclus; ++iclus) {
143  art::Ptr<recob::Cluster> piclus(clusterh, iclus);
144  geo::View_t iview = piclus->View();
145 
146  std::vector<art::Ptr<recob::Hit>> ihits = fm.at(iclus);
147 
148  // Test first view.
149 
150  if (ihits.size() >= fMinHits &&
151  ((iview == geo::kU && fSptalg.enableU()) || (iview == geo::kV && fSptalg.enableV()) ||
152  (iview == geo::kZ && fSptalg.enableW()))) {
153 
154  // Store hits from first view into hit vector.
155 
156  unsigned int nihits = ihits.size();
157  hits.clear();
158  hits.reserve(nihits);
159  for (std::vector<art::Ptr<recob::Hit>>::const_iterator i = ihits.begin();
160  i != ihits.end();
161  ++i)
162  hits.push_back(*i);
163 
164  // Loop over second cluster.
165 
166  for (int jclus = 0; jclus < iclus; ++jclus) {
167  art::Ptr<recob::Cluster> pjclus(clusterh, jclus);
168  geo::View_t jview = pjclus->View();
169 
170  std::vector<art::Ptr<recob::Hit>> jhits = fm.at(jclus);
171 
172  // Test second view.
173 
174  if (jhits.size() >= fMinHits &&
175  ((jview == geo::kU && fSptalg.enableU()) ||
176  (jview == geo::kV && fSptalg.enableV()) ||
177  (jview == geo::kZ && fSptalg.enableW())) &&
178  jview != iview) {
179 
180  // Store hits from second view into hit vector.
181 
182  unsigned int njhits = jhits.size();
183  assert(hits.size() >= nihits);
184  //hits.resize(nihits);
185  while (hits.size() > nihits)
186  hits.pop_back();
187  assert(hits.size() == nihits);
188  hits.reserve(nihits + njhits);
189  for (std::vector<art::Ptr<recob::Hit>>::const_iterator j = jhits.begin();
190  j != jhits.end();
191  ++j)
192  hits.push_back(*j);
193 
194  // If two-view space points are allowed, make them here.
195 
196  if (fSptalg.minViews() <= 2) {
197  std::vector<recob::SpacePoint> new_spts;
198  fSptalg.makeSpacePoints(clockData, detProp, hits, new_spts);
199 
200  // If we found some space points, insert them into the event.
201 
202  if (new_spts.size() > 0) {
203  fNumSpt2 += new_spts.size();
204  art::PtrVector<recob::Cluster> clusters;
205  clusters.reserve(2);
206  clusters.push_back(piclus);
207  clusters.push_back(pjclus);
208 
209  // Insert newly found space points into event collection.
210 
211  int nspt = spts->size();
212  spts->insert(spts->end(), new_spts.begin(), new_spts.end());
213 
214  // Associate space points with hits and clusters.
215 
216  art::PtrVector<recob::SpacePoint> sptvec;
217  for (unsigned int ispt = nspt; ispt < spts->size(); ++ispt) {
218  const recob::SpacePoint& spt = (*spts)[ispt];
219  const art::PtrVector<recob::Hit>& hits = fSptalg.getAssociatedHits(spt);
220  util::CreateAssn(evt, *spts, hits, *sphitassn, ispt);
221  if (fClusterAssns) util::CreateAssn(evt, *spts, clusters, *spclassn, ispt);
222 
223  // make the PtrVector for this collection of space points
224  // Do not reproduce the following lines
225  // Contact brebel@fnal.gov if you think you need to reproduce these lines.
226  art::ProductID spid = evt.getProductID<std::vector<recob::SpacePoint>>();
227  art::Ptr<recob::SpacePoint> spptr(spid, ispt, evt.productGetter(spid));
228  sptvec.push_back(spptr);
229  }
230  sptvecs->push_back(sptvec);
231  }
232  }
233 
234  // Loop over third cluster.
235 
236  for (int kclus = 0; kclus < jclus; ++kclus) {
237  art::Ptr<recob::Cluster> pkclus(clusterh, kclus);
238  geo::View_t kview = pkclus->View();
239 
240  std::vector<art::Ptr<recob::Hit>> khits = fm.at(kclus);
241 
242  // Test third view.
243 
244  if (khits.size() >= fMinHits &&
245  ((kview == geo::kU && fSptalg.enableU()) ||
246  (kview == geo::kV && fSptalg.enableV()) ||
247  (kview == geo::kZ && fSptalg.enableW())) &&
248  kview != iview && kview != jview) {
249 
250  // Store hits from third view into hit vector.
251 
252  unsigned int nkhits = khits.size();
253  assert(hits.size() >= nihits + njhits);
254  //hits.resize(nihits + njhits);
255  while (hits.size() > nihits + njhits)
256  hits.pop_back();
257  assert(hits.size() == nihits + njhits);
258  hits.reserve(nihits + njhits + nkhits);
259  for (std::vector<art::Ptr<recob::Hit>>::const_iterator k = khits.begin();
260  k != khits.end();
261  ++k)
262  hits.push_back(*k);
263 
264  // Make three-view space points.
265 
266  std::vector<recob::SpacePoint> new_spts;
267  fSptalg.makeSpacePoints(clockData, detProp, hits, new_spts);
268 
269  // If we found some space points, insert them into the event.
270 
271  if (new_spts.size() > 0) {
272  fNumSpt3 += new_spts.size();
273  art::PtrVector<recob::Cluster> clusters;
274  clusters.reserve(3);
275  clusters.push_back(piclus);
276  clusters.push_back(pjclus);
277  clusters.push_back(pkclus);
278 
279  // Insert newly found space points into event collection.
280 
281  int nspt = spts->size();
282  spts->insert(spts->end(), new_spts.begin(), new_spts.end());
283 
284  // Associate space points with hits and clusters.
285 
286  art::PtrVector<recob::SpacePoint> sptvec;
287  for (unsigned int ispt = nspt; ispt < spts->size(); ++ispt) {
288  const recob::SpacePoint& spt = (*spts)[ispt];
289  const art::PtrVector<recob::Hit>& hits = fSptalg.getAssociatedHits(spt);
290  util::CreateAssn(evt, *spts, hits, *sphitassn, ispt);
291  if (fClusterAssns) util::CreateAssn(evt, *spts, clusters, *spclassn, ispt);
292 
293  // make the PtrVector for this collection of space points
294  // Do not reproduce the following lines
295  // Contact brebel@fnal.gov if you think you need to reproduce these lines.
296  art::ProductID spid = evt.getProductID<std::vector<recob::SpacePoint>>();
297  art::Ptr<recob::SpacePoint> spptr(spid, ispt, evt.productGetter(spid));
298  sptvec.push_back(spptr);
299  }
300  sptvecs->push_back(sptvec);
301  }
302  }
303  }
304  }
305  }
306  }
307  }
308 
309  // Add space points and associations to event.
310 
311  evt.put(std::move(spts));
312  evt.put(std::move(sptvecs));
313  evt.put(std::move(sphitassn));
314  if (fClusterAssns) evt.put(std::move(spclassn));
315  }
316  }
317 
318  //----------------------------------------------------------------------------
319  void
321  //
322  // Purpose: Print summary.
323  //
324  {
325  mf::LogInfo("SpacePointFinder")
326  << "SpacePointFinder statistics:\n"
327  << " Number of events = " << fNumEvent << "\n"
328  << " Number of 2-view space points created = " << fNumSpt2 << "\n"
329  << " Number of 3-view space points created = " << fNumSpt3;
330  }
331 }
SpacePointFinder(fhicl::ParameterSet const &pset)
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
Planes which measure V.
Definition: geo_types.h:130
Declaration of signal hit object.
Planes which measure Z direction.
Definition: geo_types.h:132
bool enableV() const noexcept
const art::PtrVector< recob::Hit > & getAssociatedHits(const recob::SpacePoint &spt) const
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Planes which measure U.
Definition: geo_types.h:129
void makeSpacePoints(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, const art::PtrVector< recob::Hit > &hits, std::vector< recob::SpacePoint > &spts) const
bool enableU() const noexcept
Declaration of cluster object.
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.
bool enableW() const noexcept
TCEvent evt
Definition: DataStructs.cxx:8
pdgs k
Definition: selectors.fcl:22
Algorithm for generating space points from hits.
int minViews() const noexcept
art framework interface to geometry description
void produce(art::Event &evt) override
auto const detProp