Illustrates how to find peaks in histograms.
This script generates a random number of gaussian peaks on top of a linear background. The position of the peaks is found via TSpectrum and injected as initial values of parameters to make a global fit. The background is computed and drawn on top of the original histogram.
This script can fit "peaks' heights" or "peaks' areas" (comment out or uncomment the line which defines __PEAKS_C_FIT_AREAS__).
To execute only the first part of the script (without fitting) specify a negative value for the number of peaks, eg
{
for (
Int_t p = 0; p < npeaks; p++) {
#if defined(__PEAKS_C_FIT_AREAS__)
#endif
}
}
{
TH1F *
h =
new TH1F(
"h",
"test", 500, 0, 1000);
par[0] = 0.8;
par[1] = -0.6 / 1000;
for (p = 0; p < npeaks; p++) {
par[3 * p + 2] = 1;
par[3 * p + 3] = 10 +
gRandom->Rndm() * 980;
par[3 * p + 4] = 3 + 2 *
gRandom->Rndm();
#if defined(__PEAKS_C_FIT_AREAS__)
#endif
}
TF1 *
f =
new TF1(
"f", fpeaks, 0, 1000, 2 + 3 * npeaks);
h->FillRandom(
"f", 200000);
printf("Found %d candidate peaks to fit\n", nfound);
if (hb)
return;
TF1 *fline =
new TF1(
"fline",
"pol1", 0, 1000);
npeaks = 0;
for (p = 0; p < nfound; p++) {
Int_t bin =
h->GetXaxis()->FindBin(xp);
continue;
par[3 * npeaks + 2] = yp;
par[3 * npeaks + 3] = xp;
par[3 * npeaks + 4] = 3;
#if defined(__PEAKS_C_FIT_AREAS__)
#endif
npeaks++;
}
printf("Found %d useful peaks to fit\n", npeaks);
printf("Now fitting: Be patient\n");
TF1 *fit =
new TF1(
"fit", fpeaks, 0, 1000, 2 + 3 * npeaks);
}
int Int_t
Signed integer 4 bytes (int).
double Double_t
Double 8 bytes.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
virtual void SetNpx(Int_t npx=100)
virtual void SetParameters(const Double_t *params)
virtual Double_t GetParameter(Int_t ipar) const
1-D histogram with a float per channel (see TH1 documentation)
TH1 is the base class of all histogram classes in ROOT.
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Advanced Spectra Processing.
virtual Int_t Search(const TH1 *hist, Double_t sigma=2, Option_t *option="", Double_t threshold=0.05)
One-dimensional peak search function.
Double_t * GetPositionX() const
virtual TH1 * Background(const TH1 *hist, Int_t nIter=20, Option_t *option="")
One-dimensional background estimation function.
static TVirtualFitter * Fitter(TObject *obj, Int_t maxpar=25)
Static function returning a pointer to the current fitter.
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculates a gaussian function with mean and sigma.
Double_t Sqrt(Double_t x)
Returns the square root of x.
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
constexpr Double_t TwoPi()