Analysis-Utilities 26.9.9
C++/ROOT utilities for nuclear measurement data analysis
Loading...
Searching...
No Matches
FittingUtils.hpp
Go to the documentation of this file.
1#ifndef FITTINGUTILS_H
2#define FITTINGUTILS_H
3
4#include "PlottingUtils.hpp"
5#include <TCanvas.h>
6#include <TF1.h>
7#include <TFile.h>
8#include <TFitResult.h>
9#include <TH1.h>
10#include <TMath.h>
11#include <TPad.h>
12#include <TROOT.h>
13#include <TSystem.h>
14#include <TTree.h>
15#include <fstream>
16#include <iomanip>
17
32
33// Forward-declared so FittingUtils can launch the editor without pulling in the
34// GUI headers. Documented at its real declaration in InteractiveFitEditor.hpp —
35// a doxygen block here too would be merged with that one, doubling the @param
36// list for a single function.
37Bool_t LaunchInteractiveFitEditor(TH1 *hist, TF1 *fit_func, Double_t range_low,
38 Double_t range_high, Int_t num_peaks = 1,
39 const TString &info_label = "");
40
61Double_t Gaussian(Double_t *x, Double_t *par);
68Double_t LinearBackground(Double_t *x, Double_t *par);
76Double_t Step(Double_t *x, Double_t *par);
86Double_t LowTail(Double_t *x, Double_t *par);
95Double_t HighTail(Double_t *x, Double_t *par);
112Double_t PeakFunction(Double_t *x, Double_t *par);
124Double_t DoublePeakFunction(Double_t *x, Double_t *par);
135Double_t TriplePeakFunction(Double_t *x, Double_t *par);
136} // namespace FittingFunctions
137
171
182 std::string name;
183 Double_t value = 0;
184 Double_t error = 0;
185 Double_t lo = 0;
186 Double_t hi = 0;
187 Bool_t has_limits = kFALSE;
188 Bool_t near_lower = kFALSE;
189 Bool_t near_upper = kFALSE;
190 Bool_t near_limit = kFALSE;
191};
192
200struct FitResult {
201 std::vector<PeakFitResult> peaks;
202 Float_t bkg_constant = -1, bkg_constant_error = -1;
204 Float_t reduced_chi2 = -1;
207 Bool_t valid = kFALSE;
208
214 Int_t fit_status = -999;
217 Int_t cov_qual = -999;
218 Double_t edm = -1;
219 Double_t min_nll = -1;
222 Bool_t has_fit_diagnostics = kFALSE;
223 Int_t n_invalid_nll = -1;
225 std::vector<FitParameterDiagnostic> parameter_diagnostics;
227};
228
243private:
244 TF1 *fit_function_;
245 TH1 *working_hist_;
246 Float_t fit_range_low_;
247 Float_t fit_range_high_;
248
249 Bool_t use_flat_background_;
250 Bool_t use_step_;
251 Bool_t use_low_exp_tail_;
252 Bool_t use_low_lin_tail_;
253 Bool_t use_high_exp_tail_;
254
255 Bool_t use_manual_init_;
256 Bool_t interactive_;
257 Double_t tail_ratio_max_ = 100.0;
258 std::vector<Double_t> manual_params_;
259
260 Double_t EstimateBackground();
261 Double_t ClampToBounds(Int_t param_index, Double_t value);
262
263 void SaveInteractiveParams(const TString &input_name,
264 const TString &peak_name);
265 Bool_t LoadInteractiveParams(const TString &input_name,
266 const TString &peak_name);
267
268 void SortPeaksByMu(Int_t num_peaks);
269 void AppendPeakGraphs(std::vector<TGraph *> &components, Int_t param_offset,
270 Style_t line_style, TF1 *background, Int_t npts,
271 Double_t x_step);
272
273public:
288 FittingUtils(TH1 *working_hist, Float_t fit_range_low, Float_t fit_range_high,
289 Bool_t use_flat_background = kFALSE, Bool_t use_step = kFALSE,
290 Bool_t use_low_exp_tail = kFALSE,
291 Bool_t use_low_lin_tail = kFALSE,
292 Bool_t use_high_exp_tail = kFALSE);
295
298 void SetBackgroundModel(Bool_t use_flat_background) {
299 use_flat_background_ = use_flat_background;
300 }
301
303 void SetStep(Bool_t use_step = kTRUE) { use_step_ = use_step; }
306 void SetLowExpTail(Bool_t use_low_exp_tail = kTRUE) {
307 use_low_exp_tail_ = use_low_exp_tail;
308 }
309
311 void SetLowLinTail(Bool_t use_low_lin_tail = kTRUE) {
312 use_low_lin_tail_ = use_low_lin_tail;
313 }
314
316 void SetHighExpTail(Bool_t use_high_exp_tail = kTRUE) {
317 use_high_exp_tail_ = use_high_exp_tail;
318 }
319
334 void SetInteractive(Bool_t interactive = kTRUE) {
335 interactive_ = interactive;
336 }
337
340 void SetTailRatioMax(Double_t ratio_max) { tail_ratio_max_ = ratio_max; }
341
348 void SetManualParameters(const std::vector<Double_t> &params);
354 void SetManualParameter(Int_t index, Double_t value);
357 use_manual_init_ = kFALSE;
358 manual_params_.clear();
359 }
360
363 TF1 *GetFitFunction() { return fit_function_; }
371 void SetFitFunction(TF1 *func) { fit_function_ = func; }
372
379 void PlotFitSinglePeak(const TString input_name, const TString peak_name,
380 const TString label = "");
387 void PlotFitDoublePeak(const TString input_name, const TString peak_name,
388 const TString label = "");
395 void PlotFitTriplePeak(const TString input_name, const TString peak_name,
396 const TString label = "");
397
406 FitResult FitSinglePeak(const TString input_name, const TString peak_name);
415 FitResult FitDoublePeak(const TString input_name, const TString peak_name,
416 Double_t mu1_init, Double_t mu2_init);
429 FitResult FitDoublePeak(const TString input_name, const TString peak_name,
430 const PeakFitResult &constrained_peak,
431 Double_t mu2_init);
440 FitResult FitTriplePeak(const TString input_name, const TString peak_name,
441 const FitResult &constrained_peaks,
442 Double_t mu3_init);
443};
444
445#endif
Bool_t LaunchInteractiveFitEditor(TH1 *hist, TF1 *fit_func, Double_t range_low, Double_t range_high, Int_t num_peaks=1, const TString &info_label="")
void SetManualParameter(Int_t index, Double_t value)
Override one starting value.
void PlotFitDoublePeak(const TString input_name, const TString peak_name, const TString label="")
Draw and save the double-peak fit with its residual panel.
void SetFitFunction(TF1 *func)
Substitute a fit function of your own.
void PlotFitSinglePeak(const TString input_name, const TString peak_name, const TString label="")
Draw and save the single-peak fit with its residual panel.
void SetBackgroundModel(Bool_t use_flat_background)
Choose the background shape.
~FittingUtils()
Releases the fit function this object owns.
void SetStep(Bool_t use_step=kTRUE)
Offer the step shelf to the component search.
void SetTailRatioMax(Double_t ratio_max)
Cap the tail decay constants during fitting.
FitResult FitTriplePeak(const TString input_name, const TString peak_name, const FitResult &constrained_peaks, Double_t mu3_init)
Fit three peaks with the first two constrained by an earlier fit.
FitResult FitDoublePeak(const TString input_name, const TString peak_name, Double_t mu1_init, Double_t mu2_init)
Fit two peaks from centroid guesses.
void ClearManualParameters()
Discard manual starting values and return to automatic guesses.
FittingUtils(TH1 *working_hist, Float_t fit_range_low, Float_t fit_range_high, Bool_t use_flat_background=kFALSE, Bool_t use_step=kFALSE, Bool_t use_low_exp_tail=kFALSE, Bool_t use_low_lin_tail=kFALSE, Bool_t use_high_exp_tail=kFALSE)
Build a fitter for one histogram and range.
void SetInteractive(Bool_t interactive=kTRUE)
Open the GUI editor after the automated fit.
void SetLowLinTail(Bool_t use_low_lin_tail=kTRUE)
Offer the low-energy linear tail.
void PlotFitTriplePeak(const TString input_name, const TString peak_name, const TString label="")
Draw and save the triple-peak fit with its residual panel.
void SetManualParameters(const std::vector< Double_t > &params)
Supply explicit starting values for every parameter.
TF1 * GetFitFunction()
The fit function, for inspection or manual adjustment.
void SetLowExpTail(Bool_t use_low_exp_tail=kTRUE)
Offer the low-energy exponential tail.
FitResult FitSinglePeak(const TString input_name, const TString peak_name)
Fit one peak, pruning components that do not earn their place.
void SetHighExpTail(Bool_t use_high_exp_tail=kTRUE)
Offer the high-energy exponential tail.
Raw model functions in TF1 form: f(Double_t *x, Double_t *par).
Double_t LinearBackground(Double_t *x, Double_t *par)
Straight-line background.
Double_t Step(Double_t *x, Double_t *par)
Resolution-smeared step shelf below the peak.
Double_t DoublePeakFunction(Double_t *x, Double_t *par)
Two peaks sharing one linear background.
Double_t PeakFunction(Double_t *x, Double_t *par)
One peak with every optional component, plus a linear background.
Double_t TriplePeakFunction(Double_t *x, Double_t *par)
Three peaks sharing one linear background.
Double_t Gaussian(Double_t *x, Double_t *par)
Gaussian peak.
Double_t LowTail(Double_t *x, Double_t *par)
Combined exponential and linear tail below the peak.
Double_t HighTail(Double_t *x, Double_t *par)
Exponential tail above the peak, from pileup.
One fitted parameter's value, error and proximity to its limits.
Bool_t has_limits
Whether the parameter was bounded.
Double_t error
Fitted uncertainty.
std::string name
Parameter name as RooFit knows it.
Bool_t near_lower
Sits within a small margin of lo.
Bool_t near_limit
Either of the two above.
Bool_t near_upper
Sits within a small margin of hi.
Double_t lo
Lower bound, if has_limits.
Double_t value
Fitted value.
Double_t hi
Upper bound, if has_limits.
Result of one fit: peaks, background, quality, and diagnostics.
Float_t lin_bkg_slope
Int_t cov_qual
Covariance quality: 0 Exact, 1 NotPositiveDefinite, 2 Approximate, 3 External.
Int_t fit_status
Minuit migrad status; 0 means success.
Double_t edm
Estimated distance to the minimum.
Double_t min_nll
Minimised negative log-likelihood.
std::vector< PeakFitResult > peaks
One entry per peak; 1 to 3 supported.
Bool_t valid
In the RooFit backend this is computed post hoc against the display binning, and drives component pru...
Int_t n_invalid_nll
Invalid NLL evaluations; -1 if unknown.
std::vector< FitParameterDiagnostic > parameter_diagnostics
One entry per fitted RooRealVar, peak and background alike.
Float_t lin_bkg_slope_error
Background slope.
Float_t bkg_constant
Float_t reduced_chi2
Chi-squared per degree of freedom.
Float_t bkg_constant_error
Background offset.
Bool_t has_fit_diagnostics
Whether a RooFitResult was available, and hence whether the fields in this group mean anything.
Fitted parameters and errors for one peak.
Float_t mu_error
Centroid, in the histogram's x units.
Float_t high_exp_tail_amplitude
High-energy exponential tail scale.
Float_t low_lin_tail_amplitude
Low-energy linear tail scale.
Float_t high_exp_tail_ratio
High-energy exponential decay constant, in units of sigma.
Float_t low_exp_tail_amplitude
Low-energy exponential tail scale.
Float_t sigma_error
Gaussian resolution.
Float_t low_exp_tail_ratio
Low-energy exponential decay constant, in units of sigma.
Float_t low_lin_tail_slope
Slope of the linear tail factor.
Float_t gaus_amplitude
Float_t gaus_amplitude_error
Gaussian scale.
Float_t low_lin_tail_slope_error
Float_t low_exp_tail_ratio_error
Float_t step_amplitude_error
Step shelf scale.
Float_t low_lin_tail_amplitude_error
Float_t step_amplitude
Float_t high_exp_tail_amplitude_error
Float_t high_exp_tail_ratio_error
Float_t low_exp_tail_amplitude_error