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 this example, do (in ROOT 5 or ROOT 6):
root > .x peaks.C (generate 10 peaks by default)
root > .x peaks.C++ (use the compiler)
root > .
x peaks.C++(30) (generates 30 peaks)
To execute only the first part of the script (without fitting) specify a negative value for the number of peaks, eg
Found 9 candidate peaks to fit
Found 9 useful peaks to fit
Now fitting: Be patient
****************************************
Minimizer is Minuit2 / Migrad
Chi2 = 596.686
NDf = 471
Edm = 1.7299e-05
NCalls = 1747
p0 = 527.684 +/- 2.02282
p1 = -0.395029 +/- 0.00304651
p2 = 634.668 +/- 20.672
p3 = 519.331 +/- 0.111412
p4 = 3.49861 +/- 0.109353
p5 = 664.735 +/- 18.7022
p6 = 319.147 +/- 0.131874
p7 = 4.69145 +/- 0.126752
p8 = 670.916 +/- 17.6455
p9 = 754.806 +/- 0.108202
p10 = 4.29739 +/- 0.101204
p11 = 669.613 +/- 20.0806
p12 = 475.964 +/- 0.113649
p13 = 3.89314 +/- 0.110985
p14 = 648.09 +/- 18.199
p15 = 989.666 +/- 0.0884478
p16 = 3.34535 +/- 0.0786714
p17 = 662.552 +/- 17.8619
p18 = 539.268 +/- 0.122694
p19 = 4.56069 +/- 0.113882
p20 = 659.417 +/- 16.1804
p21 = 948.476 +/- 0.101982
p22 = 4.41156 +/- 0.091998
p23 = 753.529 +/- 15.2593
p24 = 232.585 +/- 0.151403
p25 = 6.95019 +/- 0.122555
p26 = 645.477 +/- 17.9858
p27 = 286.947 +/- 0.140814
p28 = 4.98705 +/- 0.133049
#if defined(__PEAKS_C_FIT_AREAS__)
#endif
}
}
par[0] = 0.8;
par[1] = -0.6/1000;
#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);
TF1 *fline =
new TF1(
"fline",
"pol1",0,1000);
npeaks = 0;
Int_t bin =
h->GetXaxis()->FindBin(xp);
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);
}
winID h TVirtualViewer3D TVirtualGLPainter p
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
R__EXTERN TRandom * gRandom
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
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.
Double_t Rndm() override
Machine independent random number generator.
Advanced Spectra Processing.
virtual TH1 * Background(const TH1 *hist, Int_t niter=20, Option_t *option="")
One-dimensional background estimation function.
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
static TVirtualFitter * Fitter(TObject *obj, Int_t maxpar=25)
Static function returning a pointer to the current fitter.
fit(model, train_loader, val_loader, num_epochs, batch_size, optimizer, criterion, save_best, scheduler)
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()
- Author
- Rene Brun
Definition in file peaks.C.