AdcPedestalFitter.h
Go to the documentation of this file.
1 // AdcPedestalFitter.h
2 
3 // David Adams
4 // August 2017
5 //
6 // Tool to fit ADC data and extract a pedestal. The pedestal is the mean of a Gaussian fit to
7 // the ADC distributions.
8 //
9 // If FitRmsMin < FitRmsMax, the the RMS is constrained to the range
10 // (FitRmsMin, FitRmsMax) in the fit.
11 //
12 // The starting mean and center of the histogram is the peak bin. If RemoveStickyCode is true, then
13 // one apparent sticky code may be removed when evaluating this peak.
14 //
15 // Configuration:
16 // LogLevel - 0=silent, 1=init, 2=each event, >2=more
17 // AdcRange - ADC values must less than this value, e.g. 4096 for 12 bits.
18 // FitOpt - 0: Use histogram mean (no fit)
19 // 1: Chi-square fit
20 // 2: Likelihood fit
21 // 3: Chi-square fit fillowed by likelihood fit if chi-square fit fails
22 // FitPrecision - Only used for chi-square fit? (TFitter::SetPrecision)
23 // SkipFlags - Samples with these flags are excluded
24 // AdcFitRange: Width [ADC counts] of the fit range.
25 // FitRmsMin: Lower limit for RMS fit range.
26 // FitRmsMax: Upper limit for RMS fit range.
27 // RemoveStickyCode: If true, an apparent sticky code may be removed.
28 // HistName: Name for the histogram.
29 // HistTitle: Title for the histogram.
30 // PlotFileName: If nonblank, histograms are displayed in this file.
31 // RootFileName: If nonblank, histogram is copied to this file.
32 // PlotSizeX, PlotSizeY: Size in pixels of the plot file.
33 // Root default (700x500?) is used if either is zero.
34 // PlotShowFit: Flag indicating how fit should be displayed.
35 // >= 1: Show final fit
36 // >= 2: Show starting fit function
37 // PlotSplitX: If this is nonzero, plots are created in updateMap (not update)
38 // and the drawing canvas is split NY x NX where NX = PlotSplitX.
39 // If PlotSplitX == 0, one canvas/plot is created in update.
40 // PlotSplitY: If PlotSplitY > 0, then the above NY = PlotSplitY. Otherwise
41 // NY = PlotSplitX. No effect if PlotSplitX == 0.
42 // and up to that many plots are shown on the screen.
43 //
44 // Tools:
45 // adcStringBuilder is used to make the following
46 // substitutions in the names and title:
47 // %RUN% --> run number
48 // %SUBRUN% --> subrun number
49 // %EVENT% --> event number
50 // %CHAN% --> channel number
51 //
52 // The updating methods add metadata to the ADC channel data:
53 //
54 // fitPedFractionLow - fraction of samples below the fit range
55 // fitPedFractionHigh - fraction of samples above the fit range
56 // fitPedestal - fitted pedestal (same as the assigned value)
57 // fitPedRms - Fit sigma of the pedestal
58 // fitPedChiSquare - Chi-square of the fit
59 // fitPedReducedChiSquare - An estimate of the chi-square/DOF using only the central
60 // (3-sigma) part of the pedestal distribution
61 // fitPedPeakBinFraction - Fraction of the pedestal distribution in the peak channel
62 // fitPedPeakBinExcess - Fraction above fit the peak channel
63 // fitPedNBinsRemoved - Number of sticky bins removed before fit
64 //
65 // The single-channel methods return a data map with the following:
66 // pedestal - pedestal histogram
67 // fitFractionLow - fraction of samples below the fit range
68 // fitFractionHigh - fraction of samples above the fit range
69 // fitPedestal - mean from the pedestal fit
70 // fitPedestalRms - sigma from the pedestal
71 // fitChiSquare - chi-square from the pedestal fit
72 // fitReducedChiSquare - extimate of central reduced chi-square
73 // fitPeakBinFraction - Fraction of the pedestal distribution in the peak channel
74 // fitPeakBinExcess - Fraction above fit the peak channel
75 // fitChannel - ADC channel number
76 // fitNBinsRemoved - # bins removed before the fit
77 
78 // The TpcDataTool methods all return a data map with the following:
79 //
80 // nPedFitGood - # channels with a successful pedestalfit
81 // nPedFitFail - # channels with a failed pedestal fit
82 // fitStats - fit status for each channel
83 // fitPedestals - fitted pecdestal for each channel
84 // fitPedestalRmss - fittend pedestal RMS (sigma) for each channel
85 
86 #ifndef AdcPedestalFitter_H
87 #define AdcPedestalFitter_H
88 
90 #include "fhiclcpp/ParameterSet.h"
92 #include <string>
93 #include <vector>
94 #include <set>
95 
96 class ParFormula;
97 class RunDataTool;
99 class TH1;
100 class TF1;
101 class TPadManipulator;
102 
104 : public TpcDataTool {
105 
106 public:
107 
108  using Index = unsigned int;
109  using IndexVector = std::vector<Index>;
110  using IndexSet = std::set<Index>;
111 
113 
114  DataMap view(const AdcChannelData& acd) const override;
115 
116  DataMap update(AdcChannelData& acd) const override;
117 
118  DataMap updateMap(AdcChannelDataMap& acds) const override;
119 
120  DataMap beginEvent(const DuneEventInfo&) const override;
121 
122 private:
123 
124  using Name = std::string;
125  using NameVector = std::vector<Name>;
126  using FormulaMap = std::map<Name, ParFormula*>;
127  using NameSet = std::set<Name>;
128  using NameSetMap = std::map<Name, NameSet>;
129 
130  // Configuration data.
149 
150  // ADC string tool.
152 
153  // Derived from config.
156  FormulaMap m_tfs; // Formulas for AdcRange, AdcFitRange, FitRmsMin, FitRmsMax
157  NameSetMap m_tfpars; // Formula parameters.
158  bool m_haveFormulaParams; // Do any formulas have parameters?
159  const RunDataTool* m_prdtool; // Run data tool.
160 
161  // State.
162  class State {
163  public:
165  TF1* pfitter = nullptr;
168  Index run = -1;
169  Index nevt = 0;
170  };
171  std::unique_ptr<State> m_pstate;
172  State& state() const { return *m_pstate; }
173 
174  // Make replacements in a name.
175  Name nameReplace(Name name, const AdcChannelData& acd, bool isTitle) const;
176 
177  // Find and return pedestal.
178  DataMap getPedestal(const AdcChannelData& acd) const;
179 
180  // Fill the pad for a channel.
181  // Histogram "pedestal" from dm is drawn.
182  int fillChannelPad(DataMap& dm, const AdcChannelData& acd, TPadManipulator* pman) const;
183 
184 };
185 
186 
187 #endif
static QCString name
Definition: declinfo.cpp:673
int fillChannelPad(DataMap &dm, const AdcChannelData &acd, TPadManipulator *pman) const
std::set< Index > IndexSet
unsigned int Index
std::string string
Definition: nybbler.cc:12
DataMap beginEvent(const DuneEventInfo &) const override
DataMap updateMap(AdcChannelDataMap &acds) const override
DataMap update(AdcChannelData &acd) const override
DataMap getPedestal(const AdcChannelData &acd) const
std::vector< Name > NameVector
std::map< Name, NameSet > NameSetMap
Name nameReplace(Name name, const AdcChannelData &acd, bool isTitle) const
std::map< Name, ParFormula * > FormulaMap
DataMap view(const AdcChannelData &acd) const override
const AdcChannelStringTool * m_adcStringBuilder
State & state() const
static constexpr double ps
Definition: Units.h:99
AdcPedestalFitter(fhicl::ParameterSet const &ps)
std::vector< Index > IndexVector
std::unique_ptr< State > m_pstate
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
std::set< Name > NameSet
const RunDataTool * m_prdtool