GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TPeakFitter.h
Go to the documentation of this file.
1#ifndef TPEAKFITTER_H
2#define TPEAKFITTER_H
3
4/** \addtogroup Fitting Fitting & Analysis
5 * @{
6 */
7
8#include <string>
9#include <algorithm>
10#include <vector>
11#include <cstdarg>
12
13#include "TF1.h"
14#include "TFitResultPtr.h"
15#include "TFitResult.h"
16#include "TGraph.h"
17
18#include "TGRSIFunctions.h"
19#include "TSinglePeak.h"
20
21/////////////////////////////////////////////////////////////////
22///
23/// \class TPeakFitter
24///
25/// This class is used to fit things that resemble "peaks" in data
26///
27/////////////////////////////////////////////////////////////////
28class TSinglePeak;
29
30class TPeakFitter : public TObject {
31public:
32 // ctors and dtors
33 TPeakFitter() : TPeakFitter(0., 0.) {}
34 TPeakFitter(const Double_t& rangeLow, const Double_t& rangeHigh);
35 TPeakFitter(const TPeakFitter&) = default;
36 TPeakFitter(TPeakFitter&&) noexcept = default;
37 TPeakFitter& operator=(const TPeakFitter&) = default;
38 TPeakFitter& operator=(TPeakFitter&&) noexcept = default;
39 ~TPeakFitter();
40
41 void AddPeak(TSinglePeak* peak)
42 {
43 fPeaksToFit.push_back(peak);
45 }
47 {
48 fPeaksToFit.remove(peak);
50 }
52 {
53 fPeaksToFit.clear();
55 }
56 size_t NumberOfPeaks() { return fPeaksToFit.size(); }
57 std::list<TSinglePeak*>& Peaks() { return fPeaksToFit; }
58 TSinglePeak* Peak(const size_t& index)
59 {
60 if(index >= fPeaksToFit.size()) {
61 std::cerr << "Only " << fPeaksToFit.size() << " peaks in this peak fitter, can't access peak #" << index << std::endl;
62 return nullptr;
63 }
64 auto it = fPeaksToFit.begin();
65 std::advance(it, index);
66 return *it;
67 }
68
69 void SetBackground(TF1* bg_to_fit) { fBGToFit = bg_to_fit; }
70 void InitializeParameters(TH1* fit_hist);
71 void InitializeBackgroundParameters(TH1* fit_hist);
72
73 void Print(Option_t* opt = "") const override;
74 void PrintParameters() const;
75
76 TF1* GetBackground() const { return fBGToFit; }
77 TF1* GetFitFunction() const { return fTotalFitFunction; }
78 void SetRange(const Double_t& low, const Double_t& high);
79 Int_t GetNParameters() const;
80 TFitResultPtr Fit(TH1* fit_hist, Option_t* opt = "");
81 void DrawPeaks(Option_t* = "") const;
82
83 void ResetInitFlag() { fInitFlag = false; }
84
85 void SetColorIndex(const int& index) { fColorIndex = index; }
86
87 static void VerboseLevel(EVerbosity val) { fVerboseLevel = val; }
89
90private:
92 void UpdatePeakParameters(const TFitResultPtr& fit_res, TH1* fit_hist);
93 Double_t DefaultBackgroundFunction(Double_t* dim, Double_t* par);
95 {
96 delete fTotalFitFunction;
97 fTotalFitFunction = nullptr;
98 }
99
100 std::list<TSinglePeak*> fPeaksToFit;
101 TF1* fBGToFit{nullptr};
102
103 TF1* fTotalFitFunction{nullptr};
104
105 Double_t fRangeLow{0.};
106 Double_t fRangeHigh{0.};
107
108 Double_t FitFunction(Double_t* dim, Double_t* par);
109 Double_t BackgroundFunction(Double_t* dim, Double_t* par);
110
111 bool fInitFlag{false};
112
113 TH1* fLastHistFit{nullptr};
114
115 int fColorIndex{0}; ///< this index is added to the colors kRed for the total function and kMagenta for the individual peaks
116
117 static EVerbosity fVerboseLevel; ///< Changes verbosity of code.
118
119 /// \cond CLASSIMP
120 ClassDefOverride(TPeakFitter, 2) // NOLINT(readability-else-after-return)
121 /// \endcond
122};
123/*! @} */
124#endif
EVerbosity
Definition Globals.h:143
static void VerboseLevel(EVerbosity val)
Definition TPeakFitter.h:87
void UpdatePeakParameters(const TFitResultPtr &fit_res, TH1 *fit_hist)
void RemovePeak(TSinglePeak *peak)
Definition TPeakFitter.h:46
TF1 * fTotalFitFunction
void RemoveAllPeaks()
Definition TPeakFitter.h:51
void PrintParameters() const
TSinglePeak * Peak(const size_t &index)
Definition TPeakFitter.h:58
void SetRange(const Double_t &low, const Double_t &high)
std::list< TSinglePeak * > & Peaks()
Definition TPeakFitter.h:57
void Print(Option_t *opt="") const override
Int_t GetNParameters() const
void InitializeBackgroundParameters(TH1 *fit_hist)
void SetColorIndex(const int &index)
Definition TPeakFitter.h:85
Double_t FitFunction(Double_t *dim, Double_t *par)
Double_t fRangeLow
void InitializeParameters(TH1 *fit_hist)
int fColorIndex
this index is added to the colors kRed for the total function and kMagenta for the individual peaks
TF1 * GetBackground() const
Definition TPeakFitter.h:76
TPeakFitter(TPeakFitter &&) noexcept=default
void DrawPeaks(Option_t *="") const
static EVerbosity VerboseLevel()
Definition TPeakFitter.h:88
std::list< TSinglePeak * > fPeaksToFit
void ResetInitFlag()
Definition TPeakFitter.h:83
Double_t BackgroundFunction(Double_t *dim, Double_t *par)
void SetBackground(TF1 *bg_to_fit)
Definition TPeakFitter.h:69
void ResetTotalFitFunction()
Definition TPeakFitter.h:94
TF1 * GetFitFunction() const
Definition TPeakFitter.h:77
Double_t fRangeHigh
void AddPeak(TSinglePeak *peak)
Definition TPeakFitter.h:41
static EVerbosity fVerboseLevel
Changes verbosity of code.
TFitResultPtr Fit(TH1 *fit_hist, Option_t *opt="")
size_t NumberOfPeaks()
Definition TPeakFitter.h:56
TPeakFitter(const TPeakFitter &)=default
Double_t DefaultBackgroundFunction(Double_t *dim, Double_t *par)
TH1 * fLastHistFit
void UpdateFitterParameters()