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