This macro fits the source spectrum using the AWMI algorithm from the "TSpectrumFit" class ("TSpectrum" class is used to find peaks).
created -9.7 7.97885 2
created -9.1 15.9577 4
created -8.5 11.9683 3
created -7.9 27.926 7
created -7.3 11.9683 3
created -6.7 7.97885 2
created -6.1 35.9048 9
created -5.5 19.9471 5
created -4.9 23.9365 6
created -4.3 11.9683 3
created -3.7 23.9365 6
created -3.1 23.9365 6
created -2.5 15.9577 4
created -1.9 39.8942 10
created -1.3 3.98942 1
created -0.7 15.9577 4
created -0.1 3.98942 1
created 0.5 11.9683 3
created 1.1 27.926 7
created 1.7 23.9365 6
created 2.3 11.9683 3
created 2.9 39.8942 10
created 3.5 35.9048 9
created 4.1 39.8942 10
created 4.7 27.926 7
created 5.3 31.9154 8
created 5.9 19.9471 5
created 6.5 11.9683 3
created 7.1 11.9683 3
created 7.7 39.8942 10
created 8.3 31.9154 8
created 8.9 7.97885 2
created 9.5 3.98942 1
the total number of created peaks = 33 with sigma = 0.1
the total number of found peaks = 33 with sigma = 0.100002 (+-1.69915e-05)
fit chi^2 = 7.78303e-07
found -1.9 (+-0.000121725) 39.8937 (+-0.0481226) 10.0001 (+-0.000394918)
found 2.9 (+-0.000122338) 39.8941 (+-0.0481508) 10.0002 (+-0.000395149)
found 4.1 (+-0.000122657) 39.8943 (+-0.0481657) 10.0002 (+-0.000395271)
found 7.7 (+-0.000122284) 39.894 (+-0.048148) 10.0001 (+-0.000395127)
found -6.1 (+-0.00012864) 35.9045 (+-0.0456663) 9.00009 (+-0.00037476)
found 3.5 (+-0.000129635) 35.9051 (+-0.0457094) 9.00026 (+-0.000375114)
found 8.3 (+-0.000136901) 31.9154 (+-0.0430727) 8.00015 (+-0.000353476)
found 5.3 (+-0.000137065) 31.9154 (+-0.043078) 8.00015 (+-0.000353519)
found 4.7 (+-0.000147173) 27.9263 (+-0.0403184) 7.00023 (+-0.000330873)
found -7.9 (+-0.000145997) 27.9257 (+-0.0402779) 7.00008 (+-0.000330541)
found 1.1 (+-0.00014634) 27.9259 (+-0.0402895) 7.00011 (+-0.000330636)
found -4.9 (+-0.000158123) 23.9365 (+-0.0373024) 6.0001 (+-0.000306122)
found -3.7 (+-0.000158236) 23.9365 (+-0.0373058) 6.00012 (+-0.00030615)
found -3.1 (+-0.000158388) 23.9366 (+-0.0373102) 6.00013 (+-0.000306186)
found 1.7 (+-0.000158338) 23.9366 (+-0.0373089) 6.00013 (+-0.000306175)
found -5.5 (+-0.000174368) 19.9475 (+-0.0340813) 5.00019 (+-0.000279688)
found 5.9 (+-0.000173802) 19.9473 (+-0.0340671) 5.00014 (+-0.000279572)
found -2.5 (+-0.000195485) 15.9582 (+-0.0304946) 4.00021 (+-0.000250254)
found -9.1 (+-0.000193585) 15.9576 (+-0.0304558) 4.00006 (+-0.000249935)
found -0.7 (+-0.00019258) 15.9575 (+-0.0304371) 4.00003 (+-0.000249782)
found -7.3 (+-0.00022489) 11.9685 (+-0.0263964) 3.00012 (+-0.000216622)
found -4.3 (+-0.000225777) 11.9687 (+-0.0264098) 3.00016 (+-0.000216731)
found 2.3 (+-0.00022643) 11.9689 (+-0.0264205) 3.00021 (+-0.00021682)
found 6.5 (+-0.000224849) 11.9685 (+-0.0263953) 3.0001 (+-0.000216613)
found -8.5 (+-0.000225518) 11.9686 (+-0.0264058) 3.00014 (+-0.000216699)
found 0.500004 (+-0.000224404) 11.9684 (+-0.0263897) 3.0001 (+-0.000216567)
found 7.1 (+-0.000225701) 11.9687 (+-0.0264093) 3.00017 (+-0.000216728)
found -6.69999 (+-0.000277456) 7.97931 (+-0.0215742) 2.00016 (+-0.000177049)
found 8.89999 (+-0.000276044) 7.97916 (+-0.02156) 2.00012 (+-0.000176932)
found -9.7 (+-0.00027498) 7.9789 (+-0.0215462) 2.00005 (+-0.000176819)
found -1.30001 (+-0.000396977) 3.99007 (+-0.0152816) 1.00018 (+-0.000125408)
found -0.100002 (+-0.000393533) 3.98971 (+-0.0152613) 1.00009 (+-0.000125242)
found 9.5 (+-0.000385974) 3.9895 (+-0.0152253) 1.00004 (+-0.000124947)
#include <iostream>
TH1F *FitAwmi_Create_Spectrum(
void)
{
delete gROOT->FindObject(
"h");
npeaks++;
<< 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);
else
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");
d->SetNameTitle(
"d",
"");
for (i = 0; i < nbins; i++)
d->SetBinContent(i + 1, source[i]);
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;
}
if (pm) {
h->GetListOfFunctions()->Remove(pm);
delete pm;
}
h->GetListOfFunctions()->Add(pm);
delete pfit;
delete[] Amp;
delete[] FixAmp;
delete[] FixPos;
delete s;
delete[] source;
return;
}
bool Bool_t
Boolean (0=false, 1=true) (bool)
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 dest
Option_t Option_t TPoint TPoint const char x1
R__EXTERN TRandom * gRandom
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
void Clear(Option_t *option="") override
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.
Int_t SearchHighRes(Double_t *source, Double_t *destVector, Int_t ssize, Double_t sigma, Double_t threshold, bool backgroundRemove, Int_t deconIterations, bool markov, Int_t averWindow)
One-dimensional high-resolution peak search function.
Double_t * GetPositionX() const
constexpr Double_t Sqrt2()
Double_t Sqrt(Double_t x)
Returns the square root of x.
constexpr Double_t TwoPi()