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