SpacePointCheater_module.cc
Go to the documentation of this file.
1 //
2 // Name: SpacePointCheater_module.cc
3 //
4 // Purpose: Module SpacePointCheater.
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 
22 #include "canvas/Persistency/Common/FindManyP.h"
24 
33 
34 namespace trkf {
35 
37  public:
38  explicit SpacePointCheater(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.
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 
59 
60  //----------------------------------------------------------------------------
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  mf::LogInfo("SpacePointCheater")
85  << "SpacePointCheater configured with the following parameters:\n"
86  << " ClusterModuleLabel = " << fClusterModuleLabel << "\n"
87  << " Minimum Hits per Cluster = " << fMinHits << "\n"
88  << " Cluster associations = " << fClusterAssns;
89  }
90 
91  //----------------------------------------------------------------------------
92  void
94  //
95  // Purpose: Produce method.
96  //
97  // Arguments: event - Art event.
98  //
99  {
100  ++fNumEvent;
101 
102  // Get Services.
103 
105 
106  // Get clusters.
107 
109  evt.getByLabel(fClusterModuleLabel, clusterh);
110 
111  // Make a double or triple loop over clusters in distinct views
112  // (depending on minimum number of views configured in SpacePointAlg).
113 
114  if (clusterh.isValid()) {
115 
116  // Make a collection of space points that will be inserted into the event.
117 
118  auto sptvecs = std::make_unique<std::vector<art::PtrVector<recob::SpacePoint>>>();
119  auto spts = std::make_unique<std::vector<recob::SpacePoint>>();
120  auto sphitassn = std::make_unique<art::Assns<recob::SpacePoint, recob::Hit>>();
121  auto spclassn = std::make_unique<art::Assns<recob::SpacePoint, recob::Cluster>>();
122 
123  // Make a hit vector which will be used to store hits to be passed
124  // to SpacePointAlg.
125 
127  art::FindManyP<recob::Hit> fm(clusterh, evt, fClusterModuleLabel);
128 
129  // Loop over first cluster.
130 
131  auto const clockData =
133  auto const detProp =
135 
136  int nclus = clusterh->size();
137  for (int iclus = 0; iclus < nclus; ++iclus) {
138  art::Ptr<recob::Cluster> piclus(clusterh, iclus);
139  geo::View_t iview = piclus->View();
140 
141  std::vector<art::Ptr<recob::Hit>> ihits = fm.at(iclus);
142 
143  // Test first view.
144 
145  if (ihits.size() >= fMinHits &&
146  ((iview == geo::kU && fSptalg.enableU()) || (iview == geo::kV && fSptalg.enableV()) ||
147  (iview == geo::kZ && fSptalg.enableW()))) {
148 
149  // Store hits from first view into hit vector.
150 
151  unsigned int nihits = ihits.size();
152  hits.clear();
153  hits.reserve(nihits);
154  for (std::vector<art::Ptr<recob::Hit>>::const_iterator i = ihits.begin();
155  i != ihits.end();
156  ++i)
157  hits.push_back(*i);
158 
159  // Loop over second cluster.
160 
161  for (int jclus = 0; jclus < iclus; ++jclus) {
162  art::Ptr<recob::Cluster> pjclus(clusterh, jclus);
163  geo::View_t jview = pjclus->View();
164 
165  std::vector<art::Ptr<recob::Hit>> jhits = fm.at(jclus);
166 
167  // Test second view.
168 
169  if (jhits.size() >= fMinHits &&
170  ((jview == geo::kU && fSptalg.enableU()) ||
171  (jview == geo::kV && fSptalg.enableV()) ||
172  (jview == geo::kZ && fSptalg.enableW())) &&
173  jview != iview) {
174 
175  // Store hits from second view into hit vector.
176 
177  unsigned int njhits = jhits.size();
178  assert(hits.size() >= nihits);
179  //hits.resize(nihits);
180  while (hits.size() > nihits)
181  hits.pop_back();
182  assert(hits.size() == nihits);
183  hits.reserve(nihits + njhits);
184  for (std::vector<art::Ptr<recob::Hit>>::const_iterator j = jhits.begin();
185  j != jhits.end();
186  ++j)
187  hits.push_back(*j);
188 
189  // If two-view space points are allowed, make them here.
190 
191  if (fSptalg.minViews() <= 2) {
192  std::vector<recob::SpacePoint> new_spts;
193  fSptalg.makeMCTruthSpacePoints(clockData, detProp, hits, new_spts);
194 
195  // If we found some space points, insert them into the event.
196 
197  if (new_spts.size() > 0) {
198  fNumSpt2 += new_spts.size();
200  clusters.reserve(2);
201  clusters.push_back(piclus);
202  clusters.push_back(pjclus);
203 
204  // Insert newly found space points into event collection.
205 
206  int nspt = spts->size();
207  spts->insert(spts->end(), new_spts.begin(), new_spts.end());
208 
209  // Associate space points with hits and clusters.
210 
212  for (unsigned int ispt = nspt; ispt < spts->size(); ++ispt) {
213  const recob::SpacePoint& spt = (*spts)[ispt];
215  util::CreateAssn(evt, *spts, hits, *sphitassn, ispt);
216  if (fClusterAssns) util::CreateAssn(evt, *spts, clusters, *spclassn, ispt);
217 
218  // make the PtrVector for this collection of space points
219  // Do not reproduce the following lines
220  // Contact brebel@fnal.gov if you think you need to reproduce these lines.
221  art::ProductID spid = evt.getProductID<std::vector<recob::SpacePoint>>();
222  art::Ptr<recob::SpacePoint> spptr(spid, ispt, evt.productGetter(spid));
223  sptvec.push_back(spptr);
224  }
225  sptvecs->push_back(sptvec);
226  }
227  }
228 
229  // Loop over third cluster.
230 
231  for (int kclus = 0; kclus < jclus; ++kclus) {
232  art::Ptr<recob::Cluster> pkclus(clusterh, kclus);
233  geo::View_t kview = pkclus->View();
234 
235  std::vector<art::Ptr<recob::Hit>> khits = fm.at(kclus);
236 
237  // Test third view.
238 
239  if (khits.size() >= fMinHits &&
240  ((kview == geo::kU && fSptalg.enableU()) ||
241  (kview == geo::kV && fSptalg.enableV()) ||
242  (kview == geo::kZ && fSptalg.enableW())) &&
243  kview != iview && kview != jview) {
244 
245  // Store hits from third view into hit vector.
246 
247  unsigned int nkhits = khits.size();
248  assert(hits.size() >= nihits + njhits);
249  //hits.resize(nihits + njhits);
250  while (hits.size() > nihits + njhits)
251  hits.pop_back();
252  assert(hits.size() == nihits + njhits);
253  hits.reserve(nihits + njhits + nkhits);
254  for (std::vector<art::Ptr<recob::Hit>>::const_iterator k = khits.begin();
255  k != khits.end();
256  ++k)
257  hits.push_back(*k);
258 
259  // Make three-view space points.
260 
261  std::vector<recob::SpacePoint> new_spts;
262  fSptalg.makeMCTruthSpacePoints(clockData, detProp, hits, new_spts);
263 
264  // If we found some space points, insert them into the event.
265 
266  if (new_spts.size() > 0) {
267  fNumSpt3 += new_spts.size();
269  clusters.reserve(3);
270  clusters.push_back(piclus);
271  clusters.push_back(pjclus);
272  clusters.push_back(pkclus);
273 
274  // Insert newly found space points into event collection.
275 
276  int nspt = spts->size();
277  spts->insert(spts->end(), new_spts.begin(), new_spts.end());
278 
279  // Associate space points with hits and clusters.
280 
282  for (unsigned int ispt = nspt; ispt < spts->size(); ++ispt) {
283  const recob::SpacePoint& spt = (*spts)[ispt];
285  util::CreateAssn(evt, *spts, hits, *sphitassn, ispt);
286  if (fClusterAssns) util::CreateAssn(evt, *spts, clusters, *spclassn, ispt);
287 
288  // make the PtrVector for this collection of space points
289  // Do not reproduce the following lines
290  // Contact brebel@fnal.gov if you think you need to reproduce these lines.
291  art::ProductID spid = evt.getProductID<std::vector<recob::SpacePoint>>();
292  art::Ptr<recob::SpacePoint> spptr(spid, ispt, evt.productGetter(spid));
293  sptvec.push_back(spptr);
294  }
295  sptvecs->push_back(sptvec);
296  }
297  }
298  }
299  }
300  }
301  }
302  }
303 
304  // Add space points and associations to event.
305 
306  evt.put(std::move(spts));
307  evt.put(std::move(sptvecs));
308  evt.put(std::move(sphitassn));
309  if (fClusterAssns) evt.put(std::move(spclassn));
310  }
311  }
312 
313  //----------------------------------------------------------------------------
314  void
316  //
317  // Purpose: Print summary.
318  //
319  {
320  mf::LogInfo("SpacePointCheater")
321  << "SpacePointCheater statistics:\n"
322  << " Number of events = " << fNumEvent << "\n"
323  << " Number of 2-view space points created = " << fNumSpt2 << "\n"
324  << " Number of 3-view space points created = " << fNumSpt3;
325  }
326 }
void reserve(size_type n)
Definition: PtrVector.h:337
bool enableW() const noexcept
bool enableV() const noexcept
ProductID getProductID(std::string const &instance_name="") const
Definition: DataViewImpl.h:338
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Planes which measure V.
Definition: geo_types.h:130
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
struct vector vector
Planes which measure Z direction.
Definition: geo_types.h:132
int minViews() const noexcept
const art::PtrVector< recob::Hit > & getAssociatedHits(const recob::SpacePoint &spt) const
art framework interface to geometry description
void makeMCTruthSpacePoints(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, const art::PtrVector< recob::Hit > &hits, std::vector< recob::SpacePoint > &spts) const
bool isValid() const noexcept
Definition: Handle.h:191
Planes which measure U.
Definition: geo_types.h:129
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:435
def move(depos, offset)
Definition: depos.py:107
EDProductGetter const * productGetter(ProductID const pid) const
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
geo::View_t View() const
Returns the view for this cluster.
Definition: Cluster.h:741
Declaration of signal hit object.
bool enableU() const noexcept
static constexpr double fm
Definition: Units.h:75
TCEvent evt
Definition: DataStructs.cxx:7
SpacePointCheater(fhicl::ParameterSet const &pset)
Algorithm for generating space points from hits.
void produce(art::Event &evt) override