All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CCHitFinderAlg.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // ClusterCrawlerAlg.h
3 //
4 // ClusterCrawlerAlg class
5 //
6 // Bruce Baller
7 //
8 ///////////////////////////////////////////////////////////////////////
9 #ifndef CCHITFINDERALG_H
10 #define CCHITFINDERALG_H
11 
12 // C/C++ standard libraries
13 #include <array>
14 #include <memory> // std::unique_ptr<>
15 #include <ostream> // std::endl
16 #include <vector>
17 
18 // framework libraries
19 #include "art/Framework/Services/Registry/ServiceHandle.h"
20 namespace fhicl { class ParameterSet; }
21 
22 // LArSoft libraries
28 
29 
30 namespace hit {
31 
32  /**
33  * @brief Hit finder algorithm designed to work with Cluster Crawler
34  *
35  * This algorithm used to store hits in a proprietary `CCHit` data structure.
36  * It has now been changed to use `recob::Hit` class directly.
37  * It is possible to translate the former into the latter, with one exception,
38  * as follows:
39  *
40  * // this is the original CCHit definition
41  * struct CCHit {
42  * float Charge; // recob::Hit::Integral()
43  * float ChargeErr; // recob::Hit::SigmaIntegral()
44  * float Amplitude; // recob::Hit::PeakAmplitude()
45  * float AmplitudeErr; // recob::Hit::SigmaPeakAmplitude()
46  * float Time; // recob::Hit::PeakTime()
47  * float TimeErr; // recob::Hit::SigmaPeakTime()
48  * float RMS; // recob::Hit::RMS()
49  * float RMSErr; // dropped
50  * float ChiDOF; // recob::Hit::GoodnessOfFit()
51  * int DOF; // recob::Hit::DegreesOfFreedom()
52  * float ADCSum; // recob::Hit::SummedADC()
53  * unsigned short WireNum; // recob::Hit::WireID().Wire
54  * unsigned short numHits; // recob::Hit::Multiplicity()
55  * unsigned int LoHitID; // see below
56  * float LoTime; // recob::Hit::StartTick()
57  * float HiTime; // recob::Hit::EndTick()
58  * short InClus; // dropped; see below
59  * geo::WireID WirID; // recob::Hit::WireID()
60  * recob::Wire const* Wire; // dropped; see below
61  * };
62  *
63  * The uncertainty on RMS has been dropped for good.
64  *
65  * The `LoHitID` member used to mean the index of the first hit in the "hit
66  * train" (that is the set of hits extracted from the same region of
67  * interest). That is a concept that is not portable. If your hit list is
68  * still the original one as produced by this algorithm, or if at least the
69  * hits from the same train are stored sorted and contiguously, for a hit with
70  * index `iHit`, the equivalent value of `LoHitID` is
71  * `iHit - hit.LocalIndex()`.
72  *
73  * There is no pointer to the wire any more in `recob::Hit`. The wire can be
74  * obtained through associations, that are typically produced by the art
75  * module that runs CCHitFinderAlg (e.g. `CCHitFinder`). The channel ID is
76  * also directly available as `recob::Hit::Channel()`.
77  */
78 
80 
81  public:
82 
83  std::vector<recob::Hit> allhits;
84 
85  CCHitFinderAlg(fhicl::ParameterSet const& pset);
86  virtual ~CCHitFinderAlg() = default;
87 
88  virtual void reconfigure(fhicl::ParameterSet const& pset);
89 
90  void RunCCHitFinder(std::vector<recob::Wire> const& Wires);
91 
92  /// Returns (and loses) the collection of reconstructed hits
93  std::vector<recob::Hit>&& YieldHits() { return std::move(allhits); }
94 
95  /// Print the fit statistics
96  template <typename Stream>
97  void PrintStats(Stream& out) const;
98 
99  private:
100 
101  std::vector<float> fMinPeak;
102  std::vector<float> fMinRMS;
103  unsigned short fMaxBumps; // make a crude hit if > MaxBumps are found in the RAT
104  unsigned short fMaxXtraHits; // max num of hits in Region Above Threshold
105  float fChiSplit; ///<Estimated noise error on the Signal
106  // float ChgNorm; // Area norm for the wire we are working on
107 
108  std::vector<float> fChiNorms;
109  std::vector<float> fTimeOffsets;
110  std::vector<float> fChgNorms;
111 
113  unsigned short theWireNum;
114  unsigned short thePlane;
115 
116  float chinorm;
117  // float timeoff;
118  static constexpr float Sqrt2Pi = 2.5066;
119  static constexpr float SqrtPi = 1.7725;
120 
122 
123 // bool prt;
124 
125  art::ServiceHandle<geo::Geometry const> geom;
126 
127  // fit n Gaussians possibly with bounds setting (parmin, parmax)
128  void FitNG(unsigned short nGaus, unsigned short npt, float *ticks,
129  float *signl);
130  // parameters, errors, lower limit, upper limits for FitNG
131  std::vector<double> par;
132  std::vector<double> parerr;
133  std::vector<double> parmin;
134  std::vector<double> parmax;
135  float chidof;
136  int dof;
137  std::vector<unsigned short> bumps;
138 
139  /// exchange data about the originating wire
141  public:
145 
147  (recob::Wire const* w, geo::WireID wid, geo::Geometry const& geom);
148  }; // HitChannelInfo_t
149 
150  // make a cruddy hit if fitting fails
151  void MakeCrudeHit(unsigned short npt, float *ticks, float *signl);
152  // store the hits
153  void StoreHits(unsigned short TStart, unsigned short npt,
154  HitChannelInfo_t info, float adcsum
155  );
156 
157  // study hit finding and fitting
159  std::vector< short > fUWireRange, fUTickRange;
160  std::vector< short > fVWireRange, fVTickRange;
161  std::vector< short > fWWireRange, fWTickRange;
162  void StudyHits(unsigned short flag, unsigned short npt = 0,
163  float *ticks = 0, float *signl = 0, unsigned short tstart = 0);
164  std::vector<int> bumpCnt;
165  std::vector<int> RATCnt;
166  std::vector<float> bumpChi;
167  std::vector<float> bumpRMS;
168  std::vector<int> hitCnt;
169  std::vector<float> hitRMS;
170  // use to determine the slope of protons
171  std::vector<float> loWire;
172  std::vector<float> loTime;
173  std::vector<float> hiWire;
174  std::vector<float> hiTime;
175  bool SelRAT; // set true if a Region Above Threshold should be studied
176 
177  bool fUseFastFit; ///< whether to attempt using a fast fit on single gauss.
178 
179  std::unique_ptr<GausFitCache> FitCache; ///< a set of functions ready to be used
180 
181 
182  typedef struct {
183  unsigned int FastFits; ///< count of single-Gaussian fast fits
184  std::vector<unsigned int> MultiGausFits; ///< multi-Gaussian stats
185 
186  void Reset(unsigned int nGaus);
187 
188  void AddMultiGaus(unsigned int nGaus);
189 
190  void AddFast() { ++FastFits; }
191 
192  } FitStats_t;
193 
194  FitStats_t FinalFitStats; ///< counts of the good fits
195  FitStats_t TriedFitStats; ///< counts of the tried fits
196 
197  /**
198  * @brief Performs a "fast" fit
199  * @param npt number of points to be fitted
200  * @param ticks tick coordinates
201  * @param signl signal amplitude
202  * @param params an array where the fit parameters will be stored
203  * @param paramerrors an array where the fit parameter errors will be stored
204  * @param chidof a variable where to store chi^2 over degrees of freedom
205  * @return whether the fit was successful or not
206  *
207  * Note that the fit will bail out and rteurn false if any of the input
208  * signal amplitudes is zero or negative.
209  *
210  * Also note that currently the chi^2 is not the one from comparing the
211  * Gaussian to the signal, but from comparing a fitted parabola to the
212  * logarithm of the signal.
213  */
214  static bool FastGaussianFit(
215  unsigned short npt, float const*ticks, float const*signl,
216  std::array<double, 3>& params,
217  std::array<double, 3>& paramerrors,
218  float& chidof
219  );
220 
221  static constexpr unsigned int MaxGaussians = 20;
222 
223  }; // class CCHitFinderAlg
224 
225 } // namespace hit
226 
227 
228 //==============================================================================
229 //=== Template implementation
230 //===
231 template <typename Stream>
233 
234  out << "CCHitFinderAlg fit statistics:";
235  if (fUseFastFit) {
236  out << "\n fast 1-Gaussian fits: " << FinalFitStats.FastFits
237  << " succeeded (" << TriedFitStats.FastFits << " tried)";
238  }
239  else
240  out << "\n fast 1-Gaussian fits: disabled";
241 
242  for (unsigned int nGaus = 1; nGaus < MaxGaussians; ++nGaus) {
243  if (TriedFitStats.MultiGausFits[nGaus-1] == 0) continue;
244  out << "\n " << nGaus << "-Gaussian fits: "
245  << FinalFitStats.MultiGausFits[nGaus-1]
246  << " accepted (" << TriedFitStats.MultiGausFits[nGaus-1] << " tried)";
247  } // for nGaus
248  if (TriedFitStats.MultiGausFits.back() > 0) {
249  out << "\n " << FinalFitStats.MultiGausFits.size()
250  << "-Gaussian fits or higher: " << FinalFitStats.MultiGausFits.back()
251  << " accepted (" << TriedFitStats.MultiGausFits.back() << " tried)";
252  }
253  out << std::endl;
254 
255 } // CCHitFinderAlg::FitStats_t::Print()
256 
257 
258 
259 /////////////////////////////////////////
260 
261 #endif // ifndef CCHITFINDERALG_H
unsigned short fMaxBumps
void RunCCHitFinder(std::vector< recob::Wire > const &Wires)
float fChiSplit
Estimated noise error on the Signal.
static constexpr float SqrtPi
art::ServiceHandle< geo::Geometry const > geom
void PrintStats(Stream &out) const
Print the fit statistics.
void FitNG(unsigned short nGaus, unsigned short npt, float *ticks, float *signl)
exchange data about the originating wire
std::vector< short > fWTickRange
std::vector< short > fWWireRange
virtual void reconfigure(fhicl::ParameterSet const &pset)
raw::ChannelID_t theChannel
then echo unknown compiler flag
std::vector< float > loWire
std::vector< double > parmax
void StoreHits(unsigned short TStart, unsigned short npt, HitChannelInfo_t info, float adcsum)
void StudyHits(unsigned short flag, unsigned short npt=0, float *ticks=0, float *signl=0, unsigned short tstart=0)
Declaration of signal hit object.
std::vector< float > hiWire
std::vector< unsigned short > bumps
std::vector< float > fChiNorms
std::vector< float > bumpRMS
std::vector< short > fUWireRange
bool fUseFastFit
whether to attempt using a fast fit on single gauss.
std::vector< short > fVWireRange
CCHitFinderAlg(fhicl::ParameterSet const &pset)
std::vector< float > fChgNorms
std::vector< short > fVTickRange
FitStats_t TriedFitStats
counts of the tried fits
process_name hit
Definition: cheaterreco.fcl:51
unsigned int FastFits
count of single-Gaussian fast fits
tick ticks
Alias for common language habits.
Definition: electronics.h:78
Hit finder algorithm designed to work with Cluster Crawler.
std::vector< float > fTimeOffsets
std::vector< recob::Hit > allhits
unsigned short thePlane
std::unique_ptr< GausFitCache > FitCache
a set of functions ready to be used
enum geo::_plane_sigtype SigType_t
virtual ~CCHitFinderAlg()=default
std::vector< float > hiTime
The geometry of one entire detector, as served by art.
Definition: Geometry.h:181
Definition of data types for geometry description.
std::vector< int > bumpCnt
std::vector< float > loTime
static bool FastGaussianFit(unsigned short npt, float const *ticks, float const *signl, std::array< double, 3 > &params, std::array< double, 3 > &paramerrors, float &chidof)
Performs a &quot;fast&quot; fit.
unsigned short theWireNum
std::vector< unsigned int > MultiGausFits
multi-Gaussian stats
std::vector< float > fMinPeak
Class holding the regions of interest of signal from a channel.
Definition: Wire.h:118
static constexpr unsigned int MaxGaussians
std::vector< int > hitCnt
Provide caches for TF1 functions to be used with ROOT fitters.
static constexpr float Sqrt2Pi
Declaration of basic channel signal object.
unsigned short fMaxXtraHits
std::vector< short > fUTickRange
std::vector< recob::Hit > && YieldHits()
Returns (and loses) the collection of reconstructed hits.
void MakeCrudeHit(unsigned short npt, float *ticks, float *signl)
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
std::vector< float > hitRMS
std::vector< double > parmin
std::vector< int > RATCnt
FitStats_t FinalFitStats
counts of the good fits
std::vector< double > parerr
HitChannelInfo_t(recob::Wire const *w, geo::WireID wid, geo::Geometry const &geom)
std::vector< float > fMinRMS
std::vector< float > bumpChi
std::vector< double > par
art framework interface to geometry description
bnb BNB Stream