This macro fits the source spectrum using the AWMI algorithm from the "TSpectrumFit" class ("TSpectrum" class is used to find peaks).
created -9.7 31.9154 8
created -9.1 19.9471 5
created -8.5 39.8942 10
created -7.9 23.9365 6
created -7.3 27.926 7
created -6.7 3.98942 1
created -6.1 7.97885 2
created -5.5 31.9154 8
created -4.9 35.9048 9
created -4.3 23.9365 6
created -3.7 11.9683 3
created -3.1 11.9683 3
created -2.5 31.9154 8
created -1.9 3.98942 1
created -1.3 3.98942 1
created -0.7 15.9577 4
created -0.1 39.8942 10
created 0.5 3.98942 1
created 1.1 15.9577 4
created 1.7 7.97885 2
created 2.3 35.9048 9
created 2.9 39.8942 10
created 3.5 27.926 7
created 4.1 19.9471 5
created 4.7 7.97885 2
created 5.3 15.9577 4
created 5.9 19.9471 5
created 6.5 15.9577 4
created 7.1 19.9471 5
created 7.7 23.9365 6
created 8.3 35.9048 9
created 8.9 23.9365 6
created 9.5 15.9577 4
the total number of created peaks = 33 with sigma = 0.1
the total number of found peaks = 33 with sigma = 0.100002 (+-3.20086e-05)
fit chi^2 = 2.82272e-06
found -8.5 (+-0.000232981) 39.894 (+-0.0916976) 10.0001 (+-0.000752515)
found -0.100001 (+-0.000231813) 39.8937 (+-0.091645) 10.0001 (+-0.000752083)
found 2.9 (+-0.000233588) 39.8943 (+-0.091727) 10.0002 (+-0.000752757)
found -4.9 (+-0.000246169) 35.9048 (+-0.0870175) 9.00018 (+-0.000714108)
found 2.3 (+-0.000245612) 35.9047 (+-0.0869954) 9.00015 (+-0.000713927)
found 8.3 (+-0.000245907) 35.9047 (+-0.0870059) 9.00015 (+-0.000714013)
found -9.7 (+-0.000260551) 31.915 (+-0.0820128) 8.00007 (+-0.000673037)
found -5.5 (+-0.000260597) 31.9153 (+-0.0820231) 8.00014 (+-0.000673122)
found -2.5 (+-0.000259228) 31.915 (+-0.0819713) 8.00005 (+-0.000672697)
found 3.5 (+-0.000279759) 27.9262 (+-0.0767646) 7.00019 (+-0.000629968)
found -7.3 (+-0.000277949) 27.9258 (+-0.0767047) 7.00009 (+-0.000629476)
found -7.9 (+-0.000302982) 23.937 (+-0.0710946) 6.00022 (+-0.000583437)
found -4.3 (+-0.000301877) 23.9367 (+-0.0710619) 6.00016 (+-0.000583169)
found 8.9 (+-0.000302168) 23.9368 (+-0.0710702) 6.00017 (+-0.000583237)
found 7.7 (+-0.000302416) 23.9368 (+-0.0710774) 6.00018 (+-0.000583296)
found -9.1 (+-0.000332683) 19.9477 (+-0.0649206) 5.00023 (+-0.000532771)
found 4.1 (+-0.000330361) 19.9472 (+-0.0648624) 5.00012 (+-0.000532293)
found 5.9 (+-0.000330356) 19.9471 (+-0.0648611) 5.0001 (+-0.000532282)
found 7.1 (+-0.000330896) 19.9473 (+-0.0648747) 5.00013 (+-0.000532393)
found 6.5 (+-0.000370693) 15.9579 (+-0.0580405) 4.00013 (+-0.000476309)
found 9.5 (+-0.000367016) 15.9579 (+-0.0579793) 4.00013 (+-0.000475807)
found -0.699995 (+-0.000369965) 15.958 (+-0.0580297) 4.00014 (+-0.00047622)
found 1.1 (+-0.000367452) 15.9575 (+-0.0579775) 4.00004 (+-0.000475792)
found 5.3 (+-0.000369418) 15.9577 (+-0.0580155) 4.00009 (+-0.000476103)
found -3.7 (+-0.000428595) 11.9685 (+-0.0502736) 3.00012 (+-0.00041257)
found -3.1 (+-0.000429263) 11.9686 (+-0.0502845) 3.00014 (+-0.000412659)
found 1.70001 (+-0.000529149) 7.97937 (+-0.041094) 2.00017 (+-0.000337238)
found -6.09999 (+-0.000525699) 7.97917 (+-0.0410591) 2.00012 (+-0.000336951)
found 4.7 (+-0.000527239) 7.97916 (+-0.0410725) 2.00012 (+-0.000337061)
found 0.499987 (+-0.000756005) 3.99008 (+-0.0291024) 1.00018 (+-0.000238828)
found -1.90002 (+-0.000749057) 3.98982 (+-0.0290646) 1.00012 (+-0.000238518)
found -6.70001 (+-0.000750717) 3.98982 (+-0.0290721) 1.00012 (+-0.00023858)
found -1.29999 (+-0.000745333) 3.98961 (+-0.0290422) 1.00007 (+-0.000238334)
#include <iostream>
TH1F *FitAwmi_Create_Spectrum(
void) {
delete gROOT->FindObject(
"h");
npeaks++;
std::cout << "created "
<< area << std::endl;
}
std::cout << "the total number of created peaks = " << npeaks
<<
" with sigma = " <<
sigma << std::endl;
}
void FitAwmi(void) {
TH1F *
h = FitAwmi_Create_Spectrum();
if (!cFit) cFit =
new TCanvas(
"cFit",
"cFit", 10, 10, 1000, 700);
for (i = 0; i < nbins; i++) source[i] =
h->GetBinContent(i + 1);
for(i = 0; i < nfound; i++) FixAmp[i] = FixPos[i] =
kFALSE;
for (i = 0; i < nfound; i++) {
bin = 1 +
Int_t(Pos[i] + 0.5);
Amp[i] =
h->GetBinContent(bin);
}
delete gROOT->FindObject(
"d");
TH1F *
d =
new TH1F(*
h);
d->SetNameTitle(
"d",
"");
d->Reset(
"M");
for (i = 0; i < nbins; i++)
d->SetBinContent(i + 1, source[i]);
sigma *= dx; sigmaErr *= dx;
std::cout << "the total number of found peaks = " << nfound
<<
" with sigma = " <<
sigma <<
" (+-" << sigmaErr <<
")"
<< std::endl;
std::cout <<
"fit chi^2 = " << pfit->
GetChi() << std::endl;
for (i = 0; i < nfound; i++) {
bin = 1 +
Int_t(Positions[i] + 0.5);
Pos[i] =
d->GetBinCenter(bin);
Amp[i] =
d->GetBinContent(bin);
Positions[i] =
x1 + Positions[i] * dx;
PositionsErrors[i] *= dx;
Areas[i] *= dx;
AreasErrors[i] *= dx;
std::cout << "found "
<< Positions[i] << " (+-" << PositionsErrors[i] << ") "
<< Amplitudes[i] << " (+-" << AmplitudesErrors[i] << ") "
<< Areas[i] << " (+-" << AreasErrors[i] << ")"
<< std::endl;
}
d->SetLineColor(
kRed);
d->SetLineWidth(1);
if (pm) {
h->GetListOfFunctions()->Remove(pm);
delete pm;
}
h->GetListOfFunctions()->Add(pm);
delete pfit;
delete [] Amp;
delete [] FixAmp;
delete [] FixPos;
delete [] source;
return;
}
static const double x1[5]
R__EXTERN TRandom * gRandom
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
void Clear(Option_t *option="")
Remove all primitives from the canvas.
1-D histogram with a float per channel (see TH1 documentation)}
A PolyMarker is defined by an array on N points in a 2-D space.
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Advanced 1-dimensional spectra fitting functions.
void SetPeakParameters(Double_t sigma, Bool_t fixSigma, const Double_t *positionInit, const Bool_t *fixPosition, const Double_t *ampInit, const Bool_t *fixAmp)
This function sets the following fitting parameters of peaks:
Double_t * GetAmplitudesErrors() const
void FitAwmi(Double_t *source)
This function fits the source spectrum.
Double_t * GetAreasErrors() const
void GetSigma(Double_t &sigma, Double_t &sigmaErr)
This function gets the sigma parameter and its error.
Double_t * GetAreas() const
Double_t * GetAmplitudes() const
void SetFitParameters(Int_t xmin, Int_t xmax, Int_t numberIterations, Double_t alpha, Int_t statisticType, Int_t alphaOptim, Int_t power, Int_t fitTaylor)
This function sets the following fitting parameters:
Double_t * GetPositionsErrors() const
Double_t * GetPositions() const
Advanced Spectra Processing.
static constexpr double s
constexpr Double_t Sqrt2()
Double_t Sqrt(Double_t x)
constexpr Double_t TwoPi()
#define dest(otri, vertexptr)