LinearEnergyAlg.h
Go to the documentation of this file.
1 /**
2  * @file LinearEnergyAlg.h
3  * @brief Algorithm(s) calculating energy
4  * @author Yun-Tse Tsai (yuntse@slac.stanford.edu) (adapting from the algorithm in LArLite)
5  * @date Feb 15, 2017
6  *
7  */
8 
9 #ifndef LARRECO_CALORIMETRY_LINEARENERGYALG_H
10 #define LARRECO_CALORIMETRY_LINEARENERGYALG_H
11 
12 
13 // LArSoft libraries
19 #include "larcoreobj/SimpleTypesAndConstants/PhysicalConstants.h" // util::kModBoxA ...
20 
21 // infrastructure and utilities
22 // #include "cetlib/exception.h"
25 #include "fhiclcpp/types/Comment.h"
26 #include "fhiclcpp/types/Name.h"
27 #include "fhiclcpp/types/Atom.h"
28 #include "fhiclcpp/types/Table.h"
29 #include "fhiclcpp/ParameterSet.h"
30 
31 // C/C++ standard libraries
32 #include <vector>
33 #include <utility> // std::forward()
34 #include <type_traits> // std::is_same, std::decay_t
35 
36 namespace calo {
37 
38  /**
39  * @brief Calibrates the energy of the clusters.
40  *
41  * Configuration
42  * --------------
43  *
44  * Example of configuration:
45  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46  * EnergyAlgo: {
47  * UseArea: true
48  * Recombination: {
49  * Model: Birks
50  * A3t: 0.8
51  * k3t: 0.0486
52  * }
53  * }
54  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55  *
56  * Parameters:
57  * * *UseArea* (flag, _mandatory): whether to use hit integral
58  * (`recob::Hit::Integral()`) instead of hit peak amplitude for charge
59  * estimation
60  * * *Recombination* (mandatory table): configures the recombination model
61  * to be used and its parameters:
62  * * *Model* (string, _mandatory_): specifies one of the
63  * supported recombination models: `Birks`, `ModBox` or `Constant`.
64  * Each requires its own specific configuration parameters.
65  * * `Birks`, Birks Law model
66  * (see S.Amoruso et al., NIM A 523 (2004) 275) requires
67  * configuration parameters:
68  * * *A3t* (real, default: `util::kRecombA`)
69  * * *k3t* (real, default: `util::kRecombk`) in kV/cm*(g/cm^2)/MeV
70  * * `ModBox`, box model requires configuration parameters:
71  * * *A* (real, default: `util::kModBoxA`)
72  * * *B* (real, default: `util::kModBoxB`) in kV/cm*(g/cm^2)/MeV
73  * * `Constant` recombination factor, requires configuration
74  * parameters:
75  * * *factor* (real, _mandatory_): the recombination factor,
76  * uniformly applied to all hits
77  *
78  */
80 
81  public:
82 
83  struct ModelName {
84  static const std::string ModBox;
85  static const std::string Birks;
86  static const std::string Constant;
87  };
88 
94  };
95 
96 
97  /// Configuration of parameters of the box model.
99 
100  using Name = fhicl::Name;
102 
103  bool modelIsBirks() const
104  { return Model() == ModelName::Birks; }
105  bool modelIsModBox() const
106  { return Model() == ModelName::ModBox; }
107  bool modelIsConstant() const
108  { return Model() == ModelName::Constant; }
109 
110 
112  Name("Model"),
113  Comment(std::string("Which recombination model to use: "
114  + ModelName::ModBox + ", "
115  + ModelName::Birks + ", "
116  + ModelName::Constant + ".").c_str()
117  )
118  };
119 
121  Name("A"),
122  Comment("Parameter \"A\" of box model."),
125  };
126 
128  Name("B"),
129  Comment("Parameter \"B\" of box model [kV/cm*(g/cm^2)/MeV]."),
132  };
133 
135  Name("A3t"),
136  Comment("Recombination parameter \"A\" of Birks model."),
139  };
140 
142  Name("k3t"),
143  Comment("Recombination parameter \"k\" of Birks model [kV/cm*(g/cm^2)/MeV]."),
146  };
147 
149  Name("factor"),
150  Comment("Constant recombination factor for \"constant\" model."),
152  };
153 
154  }; // RecombinationConfig
155 
156 
157  /// Algorithm configuration
158  struct Config {
159 
160  using Name = fhicl::Name;
162 
164  Name("UseArea"),
165  Comment("Whether to use the area of hit to count the deposited charges.")
166  };
167 
169  Name("Recombination"),
170  Comment("Parameters of the recombination model")
171  };
172 
173  }; // Config
174 
175 
176  /// @{
177  /// @name Construction and configuration
178 
179  /**
180  * @brief Constructor with configuration validation
181  * @param config configuration parameter structure
182  *
183  * For the configuration, see `LinearEnergyAlg` documentation.
184  */
185  LinearEnergyAlg(Config const& config);
186 
187 
188  /**
189  * @brief Constructor with configuration validation
190  * @param pset FHiCL configuration parameter set
191  * @see SpacePointIsolationAlg(Config const&)
192  *
193  * Translates the parameter set into a configuration object and uses the
194  * validating constructor to initialise the object.
195  *
196  * For the configuration, see `LinearEnergyAlg` documentation.
197  */
199  : LinearEnergyAlg(fhicl::Table<Config>(pset, {})())
200  {}
201 
202  /// @}
203 
204  /// @{
205  /// @name Set up
206 
207  /**
208  * @brief Sets up the algorithm
209  * @param geometry the geometry service provider
210  *
211  * Acquires the geometry description.
212  * This method must be called every time the geometry is changed.
213  */
214  void setup( detinfo::DetectorProperties const& detproperty, detinfo::DetectorClocks const& detclock, geo::GeometryCore const& geometry )
215  { detp = &detproperty; detc = &detclock; geom = &geometry; initialize(); }
216 
217  /**
218  * @brief Prints the current algorithm configuration.
219  * @tparam Stream type of output stream
220  * @param out output stream where to write the information
221  * @param indent indentation for all lines
222  * @param firstIndent special indentation for the first line
223  *
224  * The configuration parameters are printed in human-readable form.
225  * The output starts at the current line of the stream, and it terminates
226  * with a new line.
227  *
228  * This method does not require the algorithm to have been set up (via
229  * `Setup()`), since it just prints user configuration as given at
230  * construction time.
231  */
232  template <typename Stream>
233  void DumpConfiguration
234  (Stream&& out, std::string indent, std::string firstIndent) const;
235 
236  /**
237  * @brief Prints the current algorithm configuration.
238  * @tparam Stream type of output stream
239  * @param out output stream where to write the information
240  * @param indent (default: none) indentation for all lines (including the
241  * first one)
242  *
243  * The configuration parameters are printed in human-readable form.
244  * The output starts at the current line of the stream, and it terminates
245  * with a new line.
246  */
247  template <typename Stream>
248  void DumpConfiguration(Stream&& out, std::string indent = "") const
249  { DumpConfiguration(std::forward<Stream>(out), indent, indent); }
250 
251  /// @}
252 
253  /// @{
254  /// @name Operations
255 
256  /**
257  * @brief Calculates the energy of a single cluster.
258  * @tparam BeginHitIter type of iterator to the first associated hit
259  * @tparam EndHitIter type of iterator to the past-the-last associated hit
260  * @param cluster list of clusters we want the energy of
261  * @param hits pointers to all hits associated to the cluster
262  * @param beginHit iterator to the first hit associated to the cluster
263  * @param endHit iterator to past-the-last hit associated to the cluster
264  * @return the calibrated energy of the specified cluster [GeV]
265  *
266  * @todo Describe the algorithm
267  *
268  * The `hits` are stored as (constant) pointers.
269  *
270  */
271  template <typename BeginHitIter, typename EndHitIter>
273  (recob::Cluster const& cluster, BeginHitIter beginHit, EndHitIter endHit)
274  const;
275 
276  /**
277  * @brief Calculates the energy of a single cluster.
278  * @tparam Hits a range of hits associated with the cluster
279  * @param cluster list of clusters we want the energy of
280  * @param hits pointers to all hits associated to the cluster
281  * @return the calibrated energy of the specified cluster [GeV]
282  * @see CalculateClusterEnergy(recob::Cluster const&. BeginHitIter, EndHitIter)
283  *
284  * The `hits` are stored as (constant) pointers into a "range", that is any
285  * object (e.g. a STL vector) supporting `begin()` and `end()` iterators.
286  *
287  */
288  template <typename Hits>
290  (recob::Cluster const& cluster, Hits&& hits) const
291  {
292  using std::begin; using std::end;
293  return CalculateClusterEnergy(cluster, begin(hits), end(hits));
294  }
295 
296 
297  /**
298  * @brief Calculates the energy of the shower
299  * @param clusters list of clusters we want the energy of
300  * @param hitsPerCluster associations of all clusters to all hits
301  * @return a vector with one energy per input cluster [GeV]
302  * @see CalculateClusterEnergy()
303  *
304  * A vector is returned with a entry per plane in the TPC of the clusters.
305  * The energy has a very negative value
306  * (`std::numeric_limits<double>::lowest()`) for the planes with no cluster.
307  * See `CalculateClusterEnergy()` for the algorithm details.
308  */
309  std::vector<double> CalculateEnergy(
310  std::vector<art::Ptr<recob::Cluster>> const& clusters,
311  art::Assns<recob::Cluster, recob::Hit> const& hitsPerCluster
312  ) const;
313 
314  /// @}
315 
316  private:
317 
319  double A = util::kModBoxA;
320  double B = util::kModBoxB;
321  };
322 
324  double A = util::kRecombA;
325  double k = util::kRecombk;
326  };
327 
329  double factor = 1.0; // no sensible default value here
330  };
331 
332  /// Pointer to the geometry to be used
333  geo::GeometryCore const* geom = nullptr;
334 
335  /// Pointer to the detector property
337 
338  /// Pointer to the detector clock
339  detinfo::DetectorClocks const* detc = nullptr;
340 
341  bool fUseArea;
343 
344  /// Parameters for recombination box model; filled only when this model is selected.
346  /// Parameters for recombination Birks model; filled only when this model is selected.
348  /// Parameters for constant recombination factor; filled only when this model is selected.
350 
351  // TODO
352  // double fRecombA;
353  // double fRecombk;
354  // double fModBoxA;
355  // double fModBoxB;
356  // double fRecombFactor;
358  double fDeconNorm;
359 
360  static constexpr double kWion = 23.6e-9; ///< ionization potenial in LAr, 23.6 eV = 1e, Wion in GeV/e
361  // Conversion for energy deposited in GeV to number of ionization electrons produced
362  static constexpr double kRecombFactor = 0.62; ///< constant correction used in the current MicroBooNE shower reconstruction
363 
364  void initialize();
365 
366  /**
367  * @brief Returns the corrected energy from the hit.
368  * @param hit the hit to be corrected
369  * @return the corrected hit energy [GeV]
370  *
371  * @todo document the algorithm here
372  *
373  */
374  double CalculateHitEnergy(recob::Hit const& hit) const;
375 
376  double RecombinationCorrection( double dEdx ) const; ///< TODO: make it more flexible
377 
378  double ModBoxInverse( double dEdx ) const;
379 
380  double BirksInverse( double dEdx ) const;
381 
382  }; // class LinearEnergyAlg
383 
384 } // namespace calo
385 
386 
387 
388 //------------------------------------------------------------------------------
389 //--- template implementation
390 //---
391 template <typename BeginHitIter, typename EndHitIter>
393  (recob::Cluster const& cluster, BeginHitIter beginHit, EndHitIter endHit)
394  const
395 {
396  static_assert( // check the hits type
397  std::is_same<std::decay_t<decltype(**beginHit)>, recob::Hit>::value,
398  "The hits must be stored as recob::Hit pointers!" // any pointer will do
399  );
400 
401  double E = 0.0; // total cluster energy
402 
403  for (auto hitIter = beginHit; hitIter != endHit; ++hitIter) {
404 
405  E += CalculateHitEnergy(**hitIter);
406 
407  } // for
408 
409  return E;
410 
411 } // calo::LinearEnergyAlg::CalculateEnergy()
412 
413 
414 //------------------------------------------------------------------------------
415 template <typename Stream>
417  (Stream&& out, std::string indent, std::string firstIndent) const
418 {
419 
420  out << firstIndent << "LinearEnergyAlg configuration:"
421  << "\n" << indent << " use hit " << (fUseArea? "area": "peak amplitude")
422  << " for charge estimation"
423  << "\n" << indent << " recombination model: ";
424  switch ( fRecombModel ) {
425  case kModBox:
426  out << ModelName::ModBox
427  << "\n" << indent << " A = " << recombModBoxParams.A
428  << "\n" << indent << " B = " << recombModBoxParams.B;
429  break;
430  case kBirks:
431  out << ModelName::Birks
432  << "\n" << indent << " A = " << recombBirksParams.A
433  << "\n" << indent << " k = " << recombBirksParams.k;
434  break;
435  case kConstant:
436  out << ModelName::Constant
437  << "\n" << indent << " k = " << recombConstParams.factor;
438  break;
439  default:
440  out << "invalid (" << ((int) fRecombModel) << ")!!!";
441  } // switch
442  out << "\n";
443 
444 } // calo::LinearEnergyAlg::DumpConfiguration()
445 
446 
447 //------------------------------------------------------------------------------
448 
449 
450 
451 
452 
453 #endif // LARRECO_CALORIMETRY_LINEARENERGYALG_H
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
Calibrates the energy of the clusters.
static const std::string Constant
geo::GeometryCore const * geom
Pointer to the geometry to be used.
LinearEnergyAlg(fhicl::ParameterSet const &pset)
Constructor with configuration validation.
std::function< bool()> use_if(T *p, NullaryConfigPredicate_t< T > f)
std::string string
Definition: nybbler.cc:12
Declaration of signal hit object.
std::vector< double > CalculateEnergy(std::vector< art::Ptr< recob::Cluster >> const &clusters, art::Assns< recob::Cluster, recob::Hit > const &hitsPerCluster) const
Calculates the energy of the shower.
double RecombinationCorrection(double dEdx) const
TODO: make it more flexible.
struct vector vector
Set of hits with a 2D structure.
Definition: Cluster.h:71
Cluster finding and building.
pure virtual base interface for detector clocks
double BirksInverse(double dEdx) const
Algorithm configuration.
Access the description of detector geometry.
static const std::string Birks
double dEdx(float dqdx, float Efield)
Definition: doAna.cpp:21
void setup(detinfo::DetectorProperties const &detproperty, detinfo::DetectorClocks const &detclock, geo::GeometryCore const &geometry)
Sets up the algorithm.
static constexpr double kRecombFactor
constant correction used in the current MicroBooNE shower reconstruction
static Config * config
Definition: config.cpp:1054
constexpr double kModBoxB
Modified Box Beta in g/(MeV cm²)*kV/cm.
double CalculateClusterEnergy(recob::Cluster const &cluster, BeginHitIter beginHit, EndHitIter endHit) const
Calculates the energy of a single cluster.
double ModBoxInverse(double dEdx) const
detinfo::DetectorClocks const * detc
Pointer to the detector clock.
static constexpr double kWion
ionization potenial in LAr, 23.6 eV = 1e, Wion in GeV/e
void DumpConfiguration(Stream &&out, std::string indent="") const
Prints the current algorithm configuration.
Description of geometry of one entire detector.
Declaration of cluster object.
ModBoxParameters recombModBoxParams
Parameters for recombination box model; filled only when this model is selected.
detinfo::DetectorProperties const * detp
Pointer to the detector property.
Detector simulation of raw signals on wires.
Conversion of times between different formats and references.
void DumpConfiguration(Stream &&out, std::string indent, std::string firstIndent) const
Prints the current algorithm configuration.
AdcCodeMitigator::Name Name
art::PtrVector< recob::Hit > Hits
#define Comment
ConstantRecombParameters recombConstParams
Parameters for constant recombination factor; filled only when this model is selected.
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
E
Definition: 018_def.c:13
LinearEnergyAlg(Config const &config)
Constructor with configuration validation.
constexpr double kRecombk
BirksParameters recombBirksParams
Parameters for recombination Birks model; filled only when this model is selected.
Configuration of parameters of the box model.
static const std::string ModBox
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
constexpr double kRecombA
A constant.
constexpr double kModBoxA
Modified Box Alpha.
Collection of Physical constants used in LArSoft.
calorimetry
double CalculateHitEnergy(recob::Hit const &hit) const
Returns the corrected energy from the hit.