HitCreator.h
Go to the documentation of this file.
1 /** ****************************************************************************
2  * @file HitCreator.h
3  * @brief Helper functions to create a hit
4  * @date December 18, 2014
5  * @author Gianluca Petrillo (petrillo@fnal.gov)
6  * @see Hit.h HitCreator.cxx
7  *
8  * ****************************************************************************/
9 
10 #ifndef LARDATA_ARTDATAHELPERS_HITCREATOR_H
11 #define LARDATA_ARTDATAHELPERS_HITCREATOR_H
12 
13 // LArSoft libraries
17 
18 // framework libraries
25 
26 // C/C++ standard library
27 #include <utility> // std::move()
28 #include <vector>
29 #include <string>
30 
31 namespace geo { struct WireID; }
32 namespace raw { class RawDigit; }
33 namespace art {
34  class ProducesCollector;
35  class Event;
36 }
37 
38 /// Reconstruction base classes
39 namespace recob {
40 
41  /** **************************************************************************
42  * @brief Class managing the creation of a new `recob::Hit` object.
43  *
44  * In order to be as simple as possible (Plain Old Data), data products like
45  * `recob::Hit` need to be stripped of most of their functions, including the
46  * ability to communicate whether a value we try to store is invalid
47  * (that would require a art::Exception` -- art -- or at least a message on
48  * the screen -- MessageFacility) and the ability to read things from event,
49  * services (e.g. geometry) etc.
50  *
51  * A Creator is a class that creates a temporary data product, and at the
52  * end it yields it to the caller for storage.
53  * This last step should be by move construction, although a copy method is
54  * also provided.
55  *
56  * An example of creating a `recob::Hit` object (assuming all the relevant
57  * variables have been assigned proper values):
58  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
59  * recob::HitCreator hit(
60  * wire, wireID,
61  * start_tick, end_tick, rms,
62  * peak_time, sigma_peak_time, peak_amplitude, sigma_peak_amplitude,
63  * hit_integral, hit_sigma_integral, summedADC,
64  * multiplicity, local_index, goodness_of_fit, dof
65  * );
66  * hit.push_back(hit.move()); // hit content is not valid any more
67  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68  *
69  * This is a one-step creation object: the hit is constructed at the same
70  * time the HitCreator is, and no facility is offered to modify the
71  * constructed hit, or to create another one.
72  *
73  * The constructors currently provided are:
74  * 1. from RawDigit (extracts channel, view and signal type [CVS] thanks to
75  * geometry)
76  * 2. from `recob::Wire`, [CVS]
77  * 3. from `recob::Wire`, [CVS], `summedADC` is automatically computed from
78  * wire
79  * 4. from `recob::Wire`, [CVS], start and stop time from a region of interest
80  * 5. from `recob::Wire`, [CVS], start and stop time from index of region of
81  * interest
82  */
83  class HitCreator {
84  public:
85  /// Type of one region of interest.
86  using RegionOfInterest_t = recob::Wire::RegionsOfInterest_t::datarange_t;
87 
88  // destructor, copy and move constructor and assignment as default
89 
90  /**
91  * @brief Constructor: extracts some information from raw digit.
92  * @param digits a pointer to a `raw::RawDigit` (for channel, view, signal
93  * type)
94  * @param wireID ID of the wire the hit is on
95  * @param start_tick first tick in the region the hit was extracted from
96  * @param end_tick first tick after the region the hit was extracted from
97  * @param rms RMS of the signal hit, in TDC time units
98  * @param peak_time time at peak of the signal, in TDC time units
99  * @param sigma_peak_time uncertainty on time at peak, in TDC time units
100  * @param peak_amplitude amplitude of the signal at peak, in ADC units
101  * @param sigma_peak_amplitude uncertainty on amplitude at peak
102  * @param hit_integral total charge integrated under the hit signal
103  * @param hit_sigma_integral uncertainty on the total hit charge
104  * @param summedADC total ADC count in the region assigned to the hit
105  * @param multiplicity number of hits in the region it was extracted from
106  * @param local_index index of this hit in the region it was extracted
107  * from
108  * @param goodness_of_fit quality parameter for the hit
109  * @param dof degrees of freedom in the definition of the hit shape
110  *
111  * The information used from the raw digit is the channel ID; view and
112  * signal type are obtained from geometry.
113  */
114  HitCreator(
115  raw::RawDigit const& digits,
116  geo::WireID const& wireID,
117  raw::TDCtick_t start_tick,
118  raw::TDCtick_t end_tick,
119  float rms,
120  float peak_time,
121  float sigma_peak_time,
122  float peak_amplitude,
123  float sigma_peak_amplitude,
124  float hit_integral,
125  float hit_sigma_integral,
126  float summedADC,
127  short int multiplicity,
128  short int local_index,
129  float goodness_of_fit,
130  int dof
131  );
132 
133 
134  /**
135  * @brief Constructor: extracts some information from wire.
136  * @param wire a pointer to a `recob::Wire` (for channel, view, signal
137  * type)
138  * @param wireID ID of the wire the hit is on
139  * @param start_tick first tick in the region the hit was extracted from
140  * @param end_tick first tick after the region the hit was extracted from
141  * @param rms RMS of the signal hit, in TDC time units
142  * @param peak_time time at peak of the signal, in TDC time units
143  * @param sigma_peak_time uncertainty on time at peak, in TDC time units
144  * @param peak_amplitude amplitude of the signal at peak, in ADC units
145  * @param sigma_peak_amplitude uncertainty on amplitude at peak
146  * @param hit_integral total charge integrated under the hit signal
147  * @param hit_sigma_integral uncertainty on the total hit charge
148  * @param summedADC total ADC count in the region assigned to the hit
149  * @param multiplicity number of hits in the region it was extracted from
150  * @param local_index index of this hit in the region it was extracted
151  * from
152  * @param goodness_of_fit quality parameter for the hit
153  * @param dof degrees of freedom in the definition of the hit shape
154  *
155  * The information used from the wire are the channel ID and view;
156  * the signal type is obtained from geometry.
157  */
158  HitCreator(
159  recob::Wire const& wire,
160  geo::WireID const& wireID,
161  raw::TDCtick_t start_tick,
162  raw::TDCtick_t end_tick,
163  float rms,
164  float peak_time,
165  float sigma_peak_time,
166  float peak_amplitude,
167  float sigma_peak_amplitude,
168  float hit_integral,
169  float hit_sigma_integral,
170  float summedADC,
171  short int multiplicity,
172  short int local_index,
173  float goodness_of_fit,
174  int dof
175  );
176 
177 
178  /**
179  * @brief Constructor: computes sum of ADC from wire.
180  * @param wire a pointer to a `recob::Wire` (for channel, view, signal
181  * type)
182  * @param wireID ID of the wire the hit is on
183  * @param start_tick first tick in the region the hit was extracted from
184  * @param end_tick first tick after the region the hit was extracted from
185  * @param rms RMS of the signal hit, in TDC time units
186  * @param peak_time time at peak of the signal, in TDC time units
187  * @param sigma_peak_time uncertainty on time at peak, in TDC time units
188  * @param peak_amplitude amplitude of the signal at peak, in ADC units
189  * @param sigma_peak_amplitude uncertainty on amplitude at peak
190  * @param hit_integral total charge integrated under the hit signal
191  * @param hit_sigma_integral uncertainty on the total hit charge
192  * @param multiplicity number of hits in the region it was extracted from
193  * @param local_index index of this hit in the region it was extracted from
194  * @param goodness_of_fit quality parameter for the hit
195  * @param dof degrees of freedom in the definition of the hit shape
196  *
197  * The information used from the wire are the channel ID, view;
198  * the signal type is obtained from geometry.
199  *
200  * The sum of ADC counts is automatically computed over the whole range
201  * of the wire signal between `start_tick` and `end_tick`
202  * (the latter excluded).
203  */
204  HitCreator(
205  recob::Wire const& wire,
206  geo::WireID const& wireID,
207  raw::TDCtick_t start_tick,
208  raw::TDCtick_t end_tick,
209  float rms,
210  float peak_time,
211  float sigma_peak_time,
212  float peak_amplitude,
213  float sigma_peak_amplitude,
214  float hit_integral,
215  float hit_sigma_integral,
216  short int multiplicity,
217  short int local_index,
218  float goodness_of_fit,
219  int dof
220  );
221 
222 
223  /**
224  * @brief Constructor: uses region of interest specified by index.
225  * @param wire a pointer to a `recob::Wire` (for channel, view, signal
226  * type)
227  * @param wireID ID of the wire the hit is on
228  * @param rms RMS of the signal hit, in TDC time units
229  * @param peak_time time at peak of the signal, in TDC time units
230  * @param sigma_peak_time uncertainty on time at peak, in TDC time units
231  * @param peak_amplitude amplitude of the signal at peak, in ADC units
232  * @param sigma_peak_amplitude uncertainty on amplitude at peak
233  * @param hit_integral total charge integrated under the hit signal
234  * @param hit_sigma_integral uncertainty on the total hit charge
235  * @param summedADC total ADC count in the region assigned to the hit
236  * @param multiplicity number of hits in the region it was extracted from
237  * @param local_index index of this hit in the region it was extracted
238  * from
239  * @param goodness_of_fit quality parameter for the hit
240  * @param dof degrees of freedom in the definition of the hit shape
241  * @param signal the signal region the hit was extracted from
242  *
243  * The information used from the wire are the channel ID, view
244  * and the region of interest; the signal type is obtained from
245  * geometry.
246  *
247  * Signal start and end ticks are extracted from the region of interest.
248  */
249  HitCreator(
250  recob::Wire const& wire,
251  geo::WireID const& wireID,
252  float rms,
253  float peak_time,
254  float sigma_peak_time,
255  float peak_amplitude,
256  float sigma_peak_amplitude,
257  float hit_integral,
258  float hit_sigma_integral,
259  float summedADC,
260  short int multiplicity,
261  short int local_index,
262  float goodness_of_fit,
263  int dof,
264  RegionOfInterest_t const& signal
265  );
266 
267 
268  /**
269  * @brief Constructor: uses region of interest specified by index.
270  * @param wire a pointer to a `recob::Wire` (for channel, view, signal
271  * type)
272  * @param wireID ID of the wire the hit is on
273  * @param rms RMS of the signal hit, in TDC time units
274  * @param peak_time time at peak of the signal, in TDC time units
275  * @param sigma_peak_time uncertainty on time at peak, in TDC time units
276  * @param peak_amplitude amplitude of the signal at peak, in ADC units
277  * @param sigma_peak_amplitude uncertainty on amplitude at peak
278  * @param hit_integral total charge integrated under the hit signal
279  * @param hit_sigma_integral uncertainty on the total hit charge
280  * @param summedADC total ADC count in the region assigned to the hit
281  * @param multiplicity number of hits in the region it was extracted from
282  * @param local_index index of this hit in the region it was extracted from
283  * @param goodness_of_fit quality parameter for the hit
284  * @param dof degrees of freedom in the definition of the hit shape
285  * @param iSignalRoI index in the wire of the signal region the hit was
286  * extracted from
287  *
288  * The information used from the wire are the channel ID, view
289  * and the region of interest; the signal type is obtained from
290  * geometry.
291  *
292  * Signal start and end ticks are extracted from the region of interest.
293  */
294  HitCreator(
295  recob::Wire const& wire,
296  geo::WireID const& wireID,
297  float rms,
298  float peak_time,
299  float sigma_peak_time,
300  float peak_amplitude,
301  float sigma_peak_amplitude,
302  float hit_integral,
303  float hit_sigma_integral,
304  float summedADC,
305  short int multiplicity,
306  short int local_index,
307  float goodness_of_fit,
308  int dof,
309  size_t iSignalRoI
310  );
311 
312 
313  /**
314  * @brief Constructor: copies from an existing hit.
315  * @param from the original hit
316  */
317  HitCreator(recob::Hit const& from);
318 
319 
320  /**
321  * @brief Constructor: copies from an existing hit, changing wire ID.
322  * @param from the original hit
323  * @param wireID ID of the new wire the hit is on
324  */
325  HitCreator(recob::Hit const& from, geo::WireID const& wireID);
326 
327 
328  /**
329  * @brief Prepares the constructed hit to be moved away.
330  * @return a right-value reference to the constructed hit
331  *
332  * Despite the name, no move happens in this function.
333  * Move takes place in the caller code as proper; for example:
334  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
335  * // be hit a HitCreator instance:
336  * std::vector<recob::Hit> Hits;
337  * hit.move(); // nothing happens
338  * Hits.push_back(hit.move()); // here the copy happens
339  * recob::Hit single_hit(hit.move()); // wrong! hit is empty now
340  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
341  *
342  */
343  recob::Hit&& move() { return std::move(hit); }
344 
345 
346  /**
347  * @brief Returns the constructed wire
348  * @return a constant reference to the constructed wire
349  *
350  * Despite the name, no copy happens in this function.
351  * Copy takes place in the caller code as proper; for example:
352  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
353  * // be Hit a HitCreator instance:
354  * std::vector<recob::Hit> Hits;
355  * hit.copy(); // nothing happens
356  * Hits.push_back(hit.copy()); // here a copy happens
357  * recob::Hit single_hit(hit.copy()); // hit is copied again
358  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
359  *
360  */
361  recob::Hit const& copy() const { return hit; }
362 
363  protected:
364 
365  recob::Hit hit; ///< Local instance of the hit being constructed.
366 
367  }; // class HitCreator
368 
369 
370 
371  /** **************************************************************************
372  * @brief Base class handling a collection of hits and its associations.
373  *
374  * Instead of creating a collection of hits, one for its association with
375  * wires and one for its association with raw digits, one can use a class
376  * derived from this one:
377  * - `HitCollectionCreator`: push new hits one by one
378  * - `HitCollectionAssociator`: push a complete collection of hits
379  * - `HitRefinerAssociator`: push a complete collection of hits deriving their
380  * associations from other hits
381  * Using `put_into()` will transfer into the event the data.
382  *
383  * The typical usage is to have the constructor of the module call the static
384  * function
385  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
386  * HitAndAssociationsWriterBase::declare_products(*this);
387  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
388  *
389  * (this example declares a collection with empty instance name and that we
390  * want associations to both wires and raw digits), and then in `produce()`:
391  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
392  * HitAndAssociationsWriterDerived hcol(*this, event);
393  *
394  * // ... fill hcol in the proper way ...
395  *
396  * hcol.put_into(); // calls art::Event::put()
397  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
398  *
399  */
401  public:
402 
403  // no public constructor: use one of the derived classes!
404  // destructor, copy and move constructors and assignment are default
405 
406  /// Returns the number of hits currently in the collection.
407  size_t size() const { return hits? hits->size(): 0; }
408 
409 
410  /**
411  * @brief Moves the data into the event.
412  *
413  * The calling module must have already declared the production of these
414  * products with the proper instance name.
415  * After the move, the collections in this object are empty.
416  *
417  * @deprecated Use the version with no arguments instead.
418  */
420 
421  /**
422  * @brief Moves the data into the event.
423  *
424  * The calling module must have already declared the production of these
425  * products with the proper instance name.
426  * After the move, the collections in this object are empty.
427  */
428  void put_into();
429 
430 
431  /// Returns a read-only reference to the current list of hits.
432  std::vector<recob::Hit> const& peek() const { return *hits; }
433 
434 
435  /**
436  * @brief Declares the hit products we are going to fill.
437  * @tparam ModuleType type of producing module (`EDProducer` or `EDFilter`)
438  * @param producer the module producing the data products
439  * @param instance_name name of the instance for all data products
440  * @param doWireAssns whether to enable associations to wires
441  * @param doRawDigitAssns whether to enable associations to raw digits
442  *
443  * This declaration must be given in the constructor of producer.
444  * It is equivalent to manually declare the relevant among these products:
445  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
446  * produces<std::vector<recob::Hit>>(prod_instance);
447  * produces<art::Assns<recob::Wire, recob::Hit>>(prod_instance);
448  * produces<art::Assns<raw::RawDigit, recob::Hit>>(prod_instance);
449  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
450  * in the producer constructor.
451  * All the data products (hit collection and associations) will have the
452  * specified product instance name.
453  */
454  static void declare_products(
455  art::ProducesCollector& collector, std::string instance_name = "",
456  bool doWireAssns = true, bool doRawDigitAssns = true
457  );
458 
459  protected:
460  using HitPtr_t = art::Ptr<recob::Hit>; ///< Type of art pointer to Hit.
461 
462  std::string prod_instance; ///< Tame of the instance for data products.
463 
464  /// Collection of hits.
465  std::unique_ptr<std::vector<recob::Hit>> hits;
466  /// Associations with wires.
467  std::unique_ptr<art::Assns<recob::Wire, recob::Hit>> WireAssns;
468  /// Associations with raw digits.
469  std::unique_ptr<art::Assns<raw::RawDigit, recob::Hit>> RawDigitAssns;
470 
471  art::Event* event = nullptr; ///< Pointer to the event we are using.
472 
473  art::PtrMaker<recob::Hit> hitPtrMaker; ///< Tool to create hit pointers,
474 
475 
476  /**
477  * @brief Constructor: sets instance name and whether to build associations.
478  * @param event the event the products are going to be put into
479  * @param instance_name name of the instance for all data products
480  * @param doWireAssns whether to enable associations to wires
481  * @param doRawDigitAssns whether to enable associations to raw digits
482  *
483  * All the data products (hit collection and associations) will have the
484  * specified product instance name.
485  */
487  art::Event& event,
488  std::string instance_name,
489  bool doWireAssns, bool doRawDigitAssns
490  );
491 
492 
493  /// Creates an art pointer to the hit with the specified index.
494  HitPtr_t CreatePtr(size_t index) const { return hitPtrMaker(index); }
495 
496  }; // class HitAndAssociationsWriterBase
497 
498 
499 
500 
501  /** **************************************************************************
502  * @brief A class handling a collection of hits and its associations.
503  *
504  * Instead of creating a collection of hits, one for its association with
505  * wires and one for its association with raw digits, one can push hits into
506  * this object, and then move it into the event.
507  */
509  public:
510 
511  /// @name Constructors
512  /// @{
513  /**
514  * @brief Constructor: sets instance name and whether to build associations.
515  * @param event the event the products are going to be put into
516  * @param instance_name name of the instance for all data products
517  * @param doWireAssns whether to enable associations to wires
518  * @param doRawDigitAssns whether to enable associations to raw digits
519  *
520  * All the data products (hit collection and associations) will have the
521  * specified product instance name.
522  */
524  art::Event& event,
525  std::string instance_name = "",
526  bool doWireAssns = true, bool doRawDigitAssns = true
527  );
528 
529 
530  /**
531  * @brief Constructor: no product instance name.
532  * @param event the event the products are going to be put into
533  * @param doWireAssns whether to enable associations to wires
534  * @param doRawDigitAssns whether to enable associations to raw digits
535  */
537  art::Event& event,
538  bool doWireAssns, bool doRawDigitAssns
539  ):
540  HitCollectionCreator(event, "", doWireAssns, doRawDigitAssns)
541  {}
542 
543  /// @}
544 
545  // destructor, copy and move constructors and assignment are default
546 
547 
548  /// @name Addition of hits
549  /// @{
550  /**
551  * @brief Adds the specified hit to the data collection.
552  * @param hit the hit that will be moved into the collection
553  * @param wire art pointer to the wire to be associated to this hit
554  * @param digits art pointer to the raw digits to be associated to this hit
555  *
556  * After this call, hit will be invalid.
557  * If a art pointer is not valid, that association will not be stored.
558  */
559  void emplace_back(
560  recob::Hit&& hit,
563  );
564 
565 
566  /**
567  * @brief Adds the specified hit to the data collection.
568  * @param hit the hit that will be copied into the collection
569  * @param wire art pointer to the wire to be associated to this hit
570  * @param digits art pointer to the raw digits to be associated to this hit
571  *
572  * If a art pointer is not valid, that association will not be stored.
573  */
574  void emplace_back(
575  recob::Hit const& hit,
578  );
579 
580 
581  /**
582  * @brief Adds the specified hit to the data collection.
583  * @param hit the HitCreator object containing the hit
584  * @param wire art pointer to the wire to be associated to this hit
585  * @param digits art pointer to the raw digits to be associated to this hit
586  *
587  * After this call, the hit creator will be empty.
588  * If a art pointer is not valid, that association will not be stored.
589  */
591  HitCreator&& hit,
594  )
595  { emplace_back(hit.move(), wire, digits); }
596 
597 
598  /**
599  * @brief Adds the specified hit to the data collection.
600  * @param hit the hit that will be moved into the collection
601  * @param digits art pointer to the raw digits to be associated to this hit
602  *
603  * After this call, hit will be invalid.
604  * If the digit pointer is not valid, its association will not be stored.
605  */
607  { emplace_back(std::move(hit), art::Ptr<recob::Wire>(), digits); }
608 
609 
610  /**
611  * @brief Adds the specified hit to the data collection.
612  * @param hit the HitCreator object containing the hit
613  * @param digits art pointer to the raw digits to be associated to this hit
614  *
615  * After this call, the hit creator will be empty.
616  * If the digit pointer is not valid, its association will not be stored.
617  */
619  { emplace_back(std::move(hit), art::Ptr<recob::Wire>(), digits); }
620 
621 
622  /**
623  * @brief Adds the specified hit to the data collection.
624  * @param hit the HitCreator object containing the hit
625  * @param digits art pointer to the raw digits to be associated to this hit
626  *
627  * If the digit pointer is not valid, its association will not be stored.
628  */
629  void emplace_back
630  (HitCreator const& hit, art::Ptr<raw::RawDigit> const& digits)
631  { emplace_back(std::move(hit.copy()), art::Ptr<recob::Wire>(), digits); }
632  /// @}
633 
634 
635  /// Returns the number of hits currently in the collection.
636  size_t size() const { return hits->size(); }
637 
638 
639  /// Prepares the collection to host at least `new_size` hits.
640  void reserve(size_t new_size) { if (hits) hits->reserve(new_size); }
641 
642 
643  /**
644  * @brief Moves the data into an event.
645  *
646  * The calling module must have already declared the production of these
647  * products with the proper instance name.
648  * After the move, the collections in this object are empty.
649  *
650  * @deprecated Use the version with no arguments instead.
651  */
653 
654  /**
655  * @brief Moves the data into the event.
656  *
657  * The calling module must have already declared the production of these
658  * products with the proper instance name.
659  * After the move, the collections in this object are empty.
660  */
661  void put_into();
662 
663 
664  /// Returns a read-only reference to the current list of hits.
665  std::vector<recob::Hit> const& peek() const { return *hits; }
666 
667 
668  protected:
670 
671  /// Creates an art pointer to the hit with the last index.
673  { return hits->empty()? HitPtr_t(): CreatePtr(hits->size() - 1); }
674 
675  /// Creates associations between the last hit and the specified pointers.
676  void CreateAssociationsToLastHit(
677  art::Ptr<recob::Wire> const& wire, art::Ptr<raw::RawDigit> const& digits
678  );
679 
680  }; // class HitCollectionCreator
681 
682 
683 
684 
685  /** **************************************************************************
686  * @brief A class handling a collection of hits and its associations.
687  *
688  * Use this object if you already have a collection of `recob::Hit` and you
689  * simply want the hits associated to the wire and digit with the same
690  * channel.
691  */
693  public:
694  /// @name Constructors
695  /// @{
696  /**
697  * @brief Constructor: sets instance name and whether to build associations.
698  * @param event the event the products are going to be put into
699  * @param instance_name name of the instance for all data products
700  * @param WireModuleLabel label of the module used to create wires
701  * @param RawDigitModuleLabel label of the module used to create raw digits
702  *
703  * All the data products (hit collection and associations) will have the
704  * specified product instance name.
705  *
706  * If a label is empty, the corresponding association will not be produced.
707  */
709  art::Event& event,
710  std::string instance_name,
711  art::InputTag const& WireModuleLabel,
712  art::InputTag const& RawDigitModuleLabel
713  );
714 
715  /**
716  * @brief Constructor: sets instance name and whether to build associations.
717  * @param event the event the products are going to be put into
718  * @param WireModuleLabel label of the module used to create wires
719  * @param RawDigitModuleLabel label of the module used to create raw digits
720  *
721  * All the data products (hit collection and associations) will have a
722  * default, empty product instance name.
723  *
724  * If a label is empty, the corresponding association will not be produced.
725  */
727  art::Event& event,
728  art::InputTag const& WireModuleLabel,
729  art::InputTag const& RawDigitModuleLabel
730  ):
732  (event, "", WireModuleLabel, RawDigitModuleLabel)
733  {}
734 
735  /**
736  * @brief Constructor: sets instance name and whether to build associations.
737  * @param event the event the products are going to be put into
738  * @param instance_name name of the instance for all data products
739  * @param WireModuleLabel label of the module used to create wires
740  * @param doRawDigitAssns whether to write associations with raw digits
741  *
742  * All the data products (hit collection and associations) will have the
743  * specified product instance name.
744  *
745  * The raw digit association is built out of their existing associations
746  * with wires, rather than by directly using the raw digits data product.
747  */
749  art::Event& event,
750  std::string instance_name,
751  art::InputTag const& WireModuleLabel,
752  bool doRawDigitAssns
753  );
754 
755  /**
756  * @brief Constructor: sets instance name and whether to build associations.
757  * @param event the event the products are going to be put into
758  * @param WireModuleLabel label of the module used to create wires
759  * @param doRawDigitAssns whether to write associations with raw digits
760  *
761  * All the data products (hit collection and associations) will have the
762  * default, empty product instance name.
763  *
764  * The raw digit association is built out of their existing associations
765  * with wires, rather than by directly using the raw digits data product.
766  */
768  art::Event& event,
769  art::InputTag const& WireModuleLabel,
770  bool doRawDigitAssns
771  ):
773  (event, "", WireModuleLabel, doRawDigitAssns)
774  {}
775 
776  /// @}
777 
778  // destructor, copy and move constructors and assignment are default
779 
780  /**
781  * @brief Uses the specified collection as data product.
782  * @param srchits the collection to be used as data product
783  *
784  * The very same collection is put into the event.
785  * This object will temporary own the collection until the hits are put into
786  * the event.
787  * If there were previous hits in the object, they are lost.
788  */
789  void use_hits(std::unique_ptr<std::vector<recob::Hit>>&& srchits);
790 
791 
792  /**
793  * @brief Moves the data into the event.
794  *
795  * The calling module must have already declared the production of these
796  * products with the proper instance name.
797  * After the move, the collections in this object are empty.
798  *
799  * @deprecated Use the version with no arguments instead.
800  */
802 
803  /**
804  * @brief Moves the data into the event.
805  *
806  * The calling module must have already declared the production of these
807  * products with the proper instance name.
808  * After the move, the collections in this object are empty.
809  */
810  void put_into();
811 
812 
813  protected:
814  /// Label of the collection of wires to associate.
816  /// Label of raw digits collection to associate.
818 
819  /// Finds out the associations for the specified hits.
820  void prepare_associations(std::vector<recob::Hit> const& srchits);
821 
822  /// Finds out the associations for the current hits.
824 
825  }; // class HitCollectionAssociator
826 
827 
828  /** **************************************************************************
829  * @brief A class handling a collection of hits and its associations.
830  *
831  * Use this object if you already have a `recob::Hit` data product and
832  * another collection that is going to become a data product, and you
833  * simply want the new hits associated to the wire and digit with the same
834  * channel.
835  * No hit-to-hit association is attempted (that would be, incidentally, not
836  * supported by art): the data product is used to get all the associated
837  * wires and digits, then they are associated to the new hits by channel ID.
838  * If a channel is not available, a warning is produced. If different hits
839  * on the same channel are associated to different wires or raw digits, an
840  * exception is thrown.
841  */
843  public:
844 
845  /// @name Constructors
846  /// @{
847  /**
848  * @brief Constructor: sets instance name and whether to build associations.
849  * @param event the event the products are going to be put into
850  * @param HitModuleLabel label of the module used to create hits
851  * @param instance_name name of the instance for all data products
852  * @param doWireAssns whether to enable associations to wires
853  * @param doRawDigitAssns whether to enable associations to raw digits
854  *
855  * All the data products (hit collection and associations) will have the
856  * specified product instance name.
857  */
859  art::InputTag const& HitModuleLabel,
860  std::string instance_name = "",
861  bool doWireAssns = true, bool doRawDigitAssns = true
862  );
863 
864  /**
865  * @brief Constructor: sets instance name and whether to build associations.
866  * @param event the event the products are going to be put into
867  * @param HitModuleLabel label of the module used to create hits
868  * @param doWireAssns whether to enable associations to wires
869  * @param doRawDigitAssns whether to enable associations to raw digits
870  *
871  * All the data products (hit collection and associations) will have an
872  * empty product instance name.
873  */
875  art::InputTag const& HitModuleLabel,
876  bool doWireAssns, bool doRawDigitAssns = true
877  ):
879  (event, HitModuleLabel, "", doWireAssns, doRawDigitAssns)
880  {}
881 
882  /// @}
883 
884  // destructor, copy and move constructors and assignment are default
885 
886  /**
887  * @brief Uses the specified collection as data product.
888  * @param srchits the collection to be used as data product
889  *
890  * The very same collection is put into the event.
891  * This object will temporary own the collection until the hits are put into
892  * the event.
893  * If there were previous hits in the object, they are lost.
894  */
895  void use_hits(std::unique_ptr<std::vector<recob::Hit>>&& srchits);
896 
897 
898  /**
899  * @brief Moves the data into the event.
900  *
901  * The calling module must have already declared the production of these
902  * products with the proper instance name.
903  * After the move, the collections in this object are empty.
904  *
905  * @deprecated Use the version with no arguments instead.
906  *
907  */
909 
910  /**
911  * @brief Moves the data into the event.
912  *
913  * The calling module must have already declared the production of these
914  * products with the proper instance name.
915  * After the move, the collections in this object are empty.
916  */
917  void put_into();
918 
919 
920  protected:
921  art::InputTag hits_label; ///< Label of the collection of hits.
922 
923  /// Finds out the associations for the specified hits.
924  void prepare_associations(std::vector<recob::Hit> const& srchits);
925 
926  /// Finds out the associations for the current hits.
928 
929  }; // class HitRefinerAssociator
930 
931 
932  // ---------------------------------------------------------------------------
933  /**
934  * @brief A helper to centralise creation of a hit collection data product.
935  * @tparam Writer writer class to manage
936  * @tparam ModuleType owning module: `art::EDProducer` or `art::EDFilter`
937  *
938  * This class adds an indirection layer to the model relying on
939  * `HitAndAssociationsWriter`. In that one, two different steps are required,
940  * one in the constructor of the module, where data products are declared, and
941  * one in the place where hits are actually assembled.
942  * These two steps need consistent setup, but they are separate and
943  * formally independent. The "manager" approach consists of an object
944  * performing the first step directly, and delivering an already configured
945  * object for the second step.
946  *
947  * An example of usage in a module:
948  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
949  * class MyHitProducer: public art::EDProducer {
950  *
951  * recob::HitAndAssociationsWriterManager<recob::HitCollectionCreator>
952  * hitCollCreator;
953  *
954  * public:
955  *
956  * MyHitProducer(fhicl::ParameterSet const& pset)
957  * : EDProducer{pset}
958  * , hitCollCreator(*this, pset.get<std::string>("instanceName", ""))
959  * {}
960  *
961  * void produce(art::Event& event)
962  * {
963  * auto hitCollWriter = hitCollCreator.collectionWriter(event);
964  *
965  * for (recob::Wire const& wire: Wires) {
966  * // create hits...
967  * hitCollWriter.emplace_back(hit, wire, digit);
968  * }
969  * hitCollWriter.put_into();
970  * }
971  *
972  * }; // class MyHitProducer
973  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
974  *
975  */
976  template <typename Writer>
978 
979  public:
980  using Writer_t = Writer; ///< Type of managed hit collection writer.
981 
982  /**
983  * @brief Constructor: does not declare anything.
984  *
985  * This constructor does not declare products. Calling `declare_products()`
986  * explicitly is then required in the module constructor.
987  *
988  */
990 
991  /**
992  * @brief Declares the hit products we are going to fill.
993  * @param collector the module this manager is bound to
994  * @param instanceName name of the instance for all data products
995  * @param doWireAssns whether to enable associations to wires
996  * @param doRawDigitAssns whether to enable associations to raw digits
997  *
998  * This constructor calls `declareProducts()`.
999  */
1001  art::ProducesCollector& collector, std::string instanceName = "",
1002  bool doWireAssns = true, bool doRawDigitAssns = true
1003  );
1004 
1005  /**
1006  * @brief Declares the hit products we are going to fill.
1007  * @param collector the module this manager is bound to
1008  * @param instanceName name of the instance for all data products
1009  * @param doWireAssns whether to enable associations to wires
1010  * @param doRawDigitAssns whether to enable associations to raw digits
1011  *
1012  * This declaration must be made in the constructor of producer.
1013  * It is equivalent to manually declare the relevant among these products:
1014  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
1015  * produces<std::vector<recob::Hit>>(prod_instance);
1016  * produces<art::Assns<recob::Wire, recob::Hit>>(prod_instance);
1017  * produces<art::Assns<raw::RawDigit, recob::Hit>>(prod_instance);
1018  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1019  * in the producer constructor.
1020  * All the data products (hit collection and associations) will have the
1021  * specified product instance name.
1022  */
1023  void declareProducts(
1024  art::ProducesCollector& collector, std::string instanceName = "",
1025  bool doWireAssns = true, bool doRawDigitAssns = true
1026  );
1027 
1028  /// Returns a new writer already configured.
1029  Writer_t collectionWriter(art::Event& event) const;
1030 
1031 
1032  /// Returns the configured product instance name.
1033  std::string instanceName() const { return prodInstance; }
1034 
1035  /// Returns whether the class is fully configured.
1036  bool ready() const noexcept { return collector_p != nullptr; }
1037 
1038  protected:
1039  art::ProducesCollector* collector_p = nullptr; ///< Collector this manager is bound to.
1040 
1041  std::string prodInstance; ///< Tame of the instance for data products.
1042 
1043  /// Whether we produce hit-digit associations.
1044  bool hasRawDigitAssns = true;
1045 
1046  /// Whether we produce hit-wire associations.
1047  bool hasWireAssns = true;
1048 
1049 
1050  }; // class HitAndAssociationsWriterManager
1051 
1052  /// A manager for `recob::HitCollectionCreator` writer class.
1054 
1055 } // namespace recob
1056 
1057 
1058 //------------------------------------------------------------------------------
1059 //--- template implementation
1060 //------------------------------------------------------------------------------
1061 //--- recob::HitAndAssociationsWriterBase
1062 //---
1063 //------------------------------------------------------------------------------
1064 //--- recob::HitAndAssociationsWriterManager
1065 //---
1066 template <typename Writer>
1069  (
1070  art::ProducesCollector& collector, std::string instanceName /* = "" */,
1071  bool doWireAssns /* = true */, bool doRawDigitAssns /* = true */
1072  )
1073 {
1074  declareProducts
1075  (collector, instanceName, doWireAssns, doRawDigitAssns);
1076 } // recob::HitAndAssociationsWriterManager::HitAndAssociationsWriterManager()
1077 
1078 
1079 //------------------------------------------------------------------------------
1080 template <typename Writer>
1082  (
1083  art::ProducesCollector& collector, std::string instanceName /* = "" */,
1084  bool doWireAssns /* = true */, bool doRawDigitAssns /* = true */
1085  )
1086 {
1087  if (collector_p) {
1088  // this means you already called to declaredProducts()
1089  // or used the wrong constructor (which did that for you):
1091  << "HitAndAssociationsWriter<> has already declared its products.";
1092  }
1093  collector_p = &collector;
1094  prodInstance = instanceName;
1095  hasWireAssns = doWireAssns;
1096  hasRawDigitAssns = doRawDigitAssns;
1097  HitAndAssociationsWriterBase::declare_products
1098  (collector, prodInstance, hasWireAssns, hasRawDigitAssns);
1099 } // recob::HitAndAssociationsWriterManager::declareProducts()
1100 
1101 
1102 //------------------------------------------------------------------------------
1103 template <typename Writer>
1106  (art::Event& event) const
1107 {
1108  if (!collector_p) {
1109  // this means you forgot to code a call to declaredProducts()
1110  // or used the wrong constructor:
1112  << "HitAndAssociationsWriter<>::collectionWriter() called"
1113  " before products are declared.";
1114  }
1115  return
1116  { event, prodInstance, hasWireAssns, hasRawDigitAssns };
1117 } // recob::HitAndAssociationsWriterManager::collectionWriter()
1118 
1119 
1120 //------------------------------------------------------------------------------
1121 
1122 #endif // LARDATA_ARTDATAHELPERS_HITCREATOR_H
size_t size() const
Returns the number of hits currently in the collection.
Definition: HitCreator.h:407
HitPtr_t CreatePtrToLastHit() const
Creates an art pointer to the hit with the last index.
Definition: HitCreator.h:672
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
Reconstruction base classes.
void put_into(art::Event &)
Moves the data into the event.
Definition: HitCreator.h:419
recob::Hit const & copy() const
Returns the constructed wire.
Definition: HitCreator.h:361
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:40
void prepare_associations()
Finds out the associations for the current hits.
Definition: HitCreator.h:823
std::string string
Definition: nybbler.cc:12
std::string prod_instance
Tame of the instance for data products.
Definition: HitCreator.h:462
art::PtrMaker< recob::Hit > hitPtrMaker
Tool to create hit pointers,.
Definition: HitCreator.h:473
Writer_t collectionWriter(art::Event &event) const
Returns a new writer already configured.
Definition: HitCreator.h:1106
art::InputTag wires_label
Label of the collection of wires to associate.
Definition: HitCreator.h:815
A helper to centralise creation of a hit collection data product.
Definition: HitCreator.h:977
std::unique_ptr< art::Assns< recob::Wire, recob::Hit > > WireAssns
Associations with wires.
Definition: HitCreator.h:467
Raw data description.
recob::Hit hit
Local instance of the hit being constructed.
Definition: HitCreator.h:365
int TDCtick_t
Type representing a TDC tick.
Definition: RawTypes.h:25
HitRefinerAssociator(art::Event &event, art::InputTag const &HitModuleLabel, bool doWireAssns, bool doRawDigitAssns=true)
Constructor: sets instance name and whether to build associations.
Definition: HitCreator.h:874
Class managing the creation of a new recob::Hit object.
Definition: HitCreator.h:83
bool ready() const noexcept
Returns whether the class is fully configured.
Definition: HitCreator.h:1036
void emplace_back(HitCreator &&hit, art::Ptr< raw::RawDigit > const &digits)
Adds the specified hit to the data collection.
Definition: HitCreator.h:618
A class handling a collection of hits and its associations.
Definition: HitCreator.h:508
IDparameter< geo::WireID > WireID
Member type of validated geo::WireID parameter.
void prepare_associations()
Finds out the associations for the current hits.
Definition: HitCreator.h:927
void reserve(size_t new_size)
Prepares the collection to host at least new_size hits.
Definition: HitCreator.h:640
void put_into(art::Event &)
Moves the data into the event.
Definition: HitCreator.h:908
def move(depos, offset)
Definition: depos.py:107
void declareProducts(art::ProducesCollector &collector, std::string instanceName="", bool doWireAssns=true, bool doRawDigitAssns=true)
Declares the hit products we are going to fill.
Definition: HitCreator.h:1082
A class handling a collection of hits and its associations.
Definition: HitCreator.h:692
HitPtr_t CreatePtr(size_t index) const
Creates an art pointer to the hit with the specified index.
Definition: HitCreator.h:494
A class handling a collection of hits and its associations.
Definition: HitCreator.h:842
HitCollectionCreator(art::Event &event, bool doWireAssns, bool doRawDigitAssns)
Constructor: no product instance name.
Definition: HitCreator.h:536
Writer Writer_t
Type of managed hit collection writer.
Definition: HitCreator.h:980
void emplace_back(HitCreator &&hit, art::Ptr< recob::Wire > const &wire=art::Ptr< recob::Wire >(), art::Ptr< raw::RawDigit > const &digits=art::Ptr< raw::RawDigit >())
Adds the specified hit to the data collection.
Definition: HitCreator.h:590
void put_into(art::Event &)
Moves the data into an event.
Definition: HitCreator.h:652
Detector simulation of raw signals on wires.
HitCollectionAssociator(art::Event &event, art::InputTag const &WireModuleLabel, bool doRawDigitAssns)
Constructor: sets instance name and whether to build associations.
Definition: HitCreator.h:767
art::InputTag hits_label
Label of the collection of hits.
Definition: HitCreator.h:921
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
size_t size() const
Returns the number of hits currently in the collection.
Definition: HitCreator.h:636
Declaration of signal hit object.
Definition: types.h:32
void put_into(art::Event &)
Moves the data into the event.
Definition: HitCreator.h:801
Class holding the regions of interest of signal from a channel.
Definition: Wire.h:118
art::InputTag digits_label
Label of raw digits collection to associate.
Definition: HitCreator.h:817
std::vector< recob::Hit > const & peek() const
Returns a read-only reference to the current list of hits.
Definition: HitCreator.h:432
Declaration of basic channel signal object.
std::unique_ptr< art::Assns< raw::RawDigit, recob::Hit > > RawDigitAssns
Associations with raw digits.
Definition: HitCreator.h:469
HitCollectionAssociator(art::Event &event, art::InputTag const &WireModuleLabel, art::InputTag const &RawDigitModuleLabel)
Constructor: sets instance name and whether to build associations.
Definition: HitCreator.h:726
std::string prodInstance
Tame of the instance for data products.
Definition: HitCreator.h:1041
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
std::vector< recob::Hit > const & peek() const
Returns a read-only reference to the current list of hits.
Definition: HitCreator.h:665
recob::Wire::RegionsOfInterest_t::datarange_t RegionOfInterest_t
Type of one region of interest.
Definition: HitCreator.h:86
std::unique_ptr< std::vector< recob::Hit > > hits
Collection of hits.
Definition: HitCreator.h:465
LArSoft geometry interface.
Definition: ChannelGeo.h:16
std::string instanceName() const
Returns the configured product instance name.
Definition: HitCreator.h:1033
HitAndAssociationsWriterManager()=default
Constructor: does not declare anything.
recob::Hit && move()
Prepares the constructed hit to be moved away.
Definition: HitCreator.h:343
Base class handling a collection of hits and its associations.
Definition: HitCreator.h:400
void emplace_back(recob::Hit &&hit, art::Ptr< raw::RawDigit > const &digits)
Adds the specified hit to the data collection.
Definition: HitCreator.h:606
Event finding and building.