Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
FitAwmi.C File Reference

Detailed Description

View in nbviewer Open in SWAN
This macro fits the source spectrum using the AWMI algorithm from the "TSpectrumFit" class ("TSpectrum" class is used to find peaks).

To try this macro, in a ROOT (5 or 6) prompt, do:

root > .x FitAwmi.C

or:

root > .x FitAwmi.C++
root > FitAwmi(); // re-run with another random set of peaks
created -9.76 44.881 9
created -9.28 9.97356 2
created -8.8 49.8678 10
created -8.32 9.97356 2
created -7.84 49.8678 10
created -7.36 14.9603 3
created -6.88 9.97356 2
created -6.4 24.9339 5
created -5.92 19.9471 4
created -5.44 34.9074 7
created -4.96 24.9339 5
created -4.48 9.97356 2
created -4 29.9207 6
created -3.52 49.8678 10
created -3.04 4.98678 1
created -2.56 49.8678 10
created -2.08 24.9339 5
created -1.6 19.9471 4
created -1.12 34.9074 7
created -0.64 29.9207 6
created -0.16 19.9471 4
created 0.32 34.9074 7
created 0.8 44.881 9
created 1.28 39.8942 8
created 1.76 14.9603 3
created 2.24 4.98678 1
created 2.72 29.9207 6
created 3.2 19.9471 4
created 3.68 4.98678 1
created 4.16 49.8678 10
created 4.64 29.9207 6
created 5.12 24.9339 5
created 5.6 24.9339 5
created 6.08 29.9207 6
created 6.56 49.8678 10
created 7.04 49.8678 10
created 7.52 49.8678 10
created 8 29.9207 6
created 8.48 14.9603 3
created 8.96 14.9603 3
created 9.44 9.97356 2
the total number of created peaks = 41 with sigma = 0.08
the total number of found peaks = 41 with sigma = 0.0800011 (+-1.44686e-05)
fit chi^2 = 1.2004e-06
found -8.8 (+-0.000119845) 49.8673 (+-0.0740576) 10 (+-0.000490423)
found -7.84 (+-0.000119961) 49.8674 (+-0.0740656) 10 (+-0.000490476)
found -3.52 (+-0.000120046) 49.8675 (+-0.0740731) 10.0001 (+-0.000490526)
found -2.56 (+-0.000119975) 49.8674 (+-0.0740678) 10.0001 (+-0.00049049)
found 4.16 (+-0.000120046) 49.8675 (+-0.0740731) 10.0001 (+-0.000490526)
found 6.56 (+-0.000120799) 49.8679 (+-0.0741273) 10.0002 (+-0.000490884)
found 7.04 (+-0.000121029) 49.8681 (+-0.0741449) 10.0002 (+-0.000491001)
found 7.52 (+-0.000120799) 49.8679 (+-0.0741273) 10.0002 (+-0.000490884)
found -9.76 (+-0.000126675) 44.8805 (+-0.0702736) 9.00002 (+-0.000465365)
found 0.8 (+-0.000127391) 44.8812 (+-0.0703271) 9.00015 (+-0.000465719)
found 1.28 (+-0.000134927) 39.8943 (+-0.0662941) 8.00012 (+-0.000439012)
found -5.44 (+-0.000144174) 34.9074 (+-0.062008) 7.00009 (+-0.000410628)
found -1.12 (+-0.000144271) 34.9075 (+-0.0620133) 7.0001 (+-0.000410663)
found 0.320001 (+-0.000144513) 34.9076 (+-0.0620267) 7.00013 (+-0.000410752)
found -0.640001 (+-0.000156108) 29.9208 (+-0.0574261) 6.00011 (+-0.000380286)
found 4.64 (+-0.000156491) 29.921 (+-0.0574443) 6.00015 (+-0.000380407)
found 8 (+-0.000156212) 29.9209 (+-0.0574318) 6.00013 (+-0.000380324)
found -4 (+-0.000156025) 29.9209 (+-0.0574238) 6.00012 (+-0.000380271)
found 2.72 (+-0.000155181) 29.9205 (+-0.0573851) 6.00005 (+-0.000380014)
found 6.08 (+-0.000156491) 29.921 (+-0.0574443) 6.00015 (+-0.000380407)
found -4.96 (+-0.000170865) 24.934 (+-0.0524179) 5.00009 (+-0.000347121)
found -2.08 (+-0.000171557) 24.9343 (+-0.0524448) 5.00014 (+-0.000347299)
found 5.12 (+-0.000171292) 24.9341 (+-0.0524335) 5.00011 (+-0.000347224)
found -6.4 (+-0.000170471) 24.9339 (+-0.0524022) 5.00006 (+-0.000347017)
found 5.6 (+-0.000171292) 24.9341 (+-0.0524335) 5.00011 (+-0.000347224)
found -0.16 (+-0.000192187) 19.9475 (+-0.0469198) 4.00013 (+-0.000310711)
found 3.2 (+-0.000190854) 19.9472 (+-0.0468793) 4.00007 (+-0.000310443)
found -5.92 (+-0.000192028) 19.9475 (+-0.0469146) 4.00012 (+-0.000310677)
found -1.6 (+-0.000192028) 19.9475 (+-0.0469146) 4.00012 (+-0.000310677)
found -7.36 (+-0.000221966) 14.9607 (+-0.0406367) 3.00012 (+-0.000269104)
found 1.76 (+-0.000221195) 14.9606 (+-0.0406187) 3.00009 (+-0.000268984)
found 8.48 (+-0.000221673) 14.9606 (+-0.040628) 3.00009 (+-0.000269046)
found 8.96 (+-0.000220629) 14.9604 (+-0.040603) 3.00005 (+-0.00026888)
found -9.28 (+-0.00027522) 9.97439 (+-0.0332363) 2.00019 (+-0.000220097)
found -8.32 (+-0.000275421) 9.97444 (+-0.0332399) 2.0002 (+-0.00022012)
found -6.88 (+-0.000272305) 9.97383 (+-0.0331859) 2.00008 (+-0.000219763)
found -4.48 (+-0.000273311) 9.97399 (+-0.0332027) 2.00011 (+-0.000219875)
found 9.44 (+-0.000268451) 9.97368 (+-0.0331318) 2.00005 (+-0.000219404)
found -3.04 (+-0.00039378) 4.98774 (+-0.023544) 1.00021 (+-0.000155912)
found 3.68001 (+-0.000391024) 4.98743 (+-0.0235188) 1.00014 (+-0.000155745)
found 2.24 (+-0.00038872) 4.98717 (+-0.0234974) 1.00009 (+-0.000155604)
#include "TROOT.h"
#include "TMath.h"
#include "TRandom.h"
#include "TH1.h"
#include "TF1.h"
#include "TCanvas.h"
#include "TSpectrum.h"
#include "TSpectrumFit.h"
#include "TPolyMarker.h"
#include "TList.h"
#include <iostream>
TH1F *FitAwmi_Create_Spectrum(void)
{
Int_t nbins = 1000;
Double_t xmin = -10., xmax = 10.;
delete gROOT->FindObject("h"); // prevent "memory leak"
TH1F *h = new TH1F("h", "simulated spectrum", nbins, xmin, xmax);
h->SetStats(kFALSE);
TF1 f("f", "TMath::Gaus(x, [0], [1], 1)", xmin, xmax);
// f.SetParNames("mean", "sigma");
gRandom->SetSeed(0); // make it really random
// create well separated peaks with exactly known means and areas
// note: TSpectrumFit assumes that all peaks have the same sigma
Double_t sigma = (xmax - xmin) / Double_t(nbins) * Int_t(gRandom->Uniform(2., 6.));
Int_t npeaks = 0;
while (xmax > (xmin + 6. * sigma)) {
npeaks++;
xmin += 3. * sigma; // "mean"
f.SetParameters(xmin, sigma);
Double_t area = 1. * Int_t(gRandom->Uniform(1., 11.));
h->Add(&f, area, ""); // "" ... or ... "I"
std::cout << "created " << xmin << " " << (area / sigma / TMath::Sqrt(TMath::TwoPi())) << " " << area
<< std::endl;
xmin += 3. * sigma;
}
std::cout << "the total number of created peaks = " << npeaks << " with sigma = " << sigma << std::endl;
return h;
}
void FitAwmi(void)
{
TH1F *h = FitAwmi_Create_Spectrum();
TCanvas *cFit = ((TCanvas *)(gROOT->GetListOfCanvases()->FindObject("cFit")));
if (!cFit)
cFit = new TCanvas("cFit", "cFit", 10, 10, 1000, 700);
else
cFit->Clear();
h->Draw("L");
Int_t i, nfound, bin;
Int_t nbins = h->GetNbinsX();
Double_t *source = new Double_t[nbins];
Double_t *dest = new Double_t[nbins];
for (i = 0; i < nbins; i++)
source[i] = h->GetBinContent(i + 1);
TSpectrum *s = new TSpectrum(); // note: default maxpositions = 100
// searching for candidate peaks positions
nfound = s->SearchHighRes(source, dest, nbins, 2., 2., kFALSE, 10000, kFALSE, 0);
// filling in the initial estimates of the input parameters
Bool_t *FixPos = new Bool_t[nfound];
Bool_t *FixAmp = new Bool_t[nfound];
for (i = 0; i < nfound; i++)
FixAmp[i] = FixPos[i] = kFALSE;
Double_t *Pos, *Amp = new Double_t[nfound]; // ROOT 6
Pos = s->GetPositionX(); // 0 ... (nbins - 1)
for (i = 0; i < nfound; i++) {
bin = 1 + Int_t(Pos[i] + 0.5); // the "nearest" bin
Amp[i] = h->GetBinContent(bin);
}
TSpectrumFit *pfit = new TSpectrumFit(nfound);
pfit->SetFitParameters(0, (nbins - 1), 1000, 0.1, pfit->kFitOptimChiCounts, pfit->kFitAlphaHalving, pfit->kFitPower2,
pfit->SetPeakParameters(2., kFALSE, Pos, FixPos, Amp, FixAmp);
// pfit->SetBackgroundParameters(source[0], kFALSE, 0., kFALSE, 0., kFALSE);
pfit->FitAwmi(source);
Double_t *Positions = pfit->GetPositions();
Double_t *PositionsErrors = pfit->GetPositionsErrors();
Double_t *Amplitudes = pfit->GetAmplitudes();
Double_t *AmplitudesErrors = pfit->GetAmplitudesErrors();
Double_t *Areas = pfit->GetAreas();
Double_t *AreasErrors = pfit->GetAreasErrors();
delete gROOT->FindObject("d"); // prevent "memory leak"
TH1F *d = new TH1F(*h);
d->SetNameTitle("d", "");
d->Reset("M");
for (i = 0; i < nbins; i++)
d->SetBinContent(i + 1, source[i]);
Double_t x1 = d->GetBinCenter(1), dx = d->GetBinWidth(1);
Double_t sigma, sigmaErr;
pfit->GetSigma(sigma, sigmaErr);
// current TSpectrumFit needs a sqrt(2) correction factor for sigma
sigmaErr /= TMath::Sqrt2();
// convert "bin numbers" into "x-axis values"
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); // the "nearest" bin
Pos[i] = d->GetBinCenter(bin);
Amp[i] = d->GetBinContent(bin);
// convert "bin numbers" into "x-axis values"
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);
d->Draw("SAME L");
TPolyMarker *pm = ((TPolyMarker *)(h->GetListOfFunctions()->FindObject("TPolyMarker")));
if (pm) {
h->GetListOfFunctions()->Remove(pm);
delete pm;
}
pm = new TPolyMarker(nfound, Pos, Amp);
h->GetListOfFunctions()->Add(pm);
pm->SetMarkerStyle(23);
pm->SetMarkerSize(1);
// cleanup
delete pfit;
delete[] Amp;
delete[] FixAmp;
delete[] FixPos;
delete s;
delete[] dest;
delete[] source;
return;
}
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
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
float xmin
float xmax
#define gROOT
Definition TROOT.h:406
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:45
The Canvas class.
Definition TCanvas.h:23
void Clear(Option_t *option="") override
Remove all primitives from the canvas.
Definition TCanvas.cxx:737
1-Dim function class
Definition TF1.h:233
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:623
TObject * FindObject(const char *name) const override
Search object named name in the list of functions.
Definition TH1.cxx:3865
A PolyMarker is defined by an array on N points in a 2-D space.
Definition TPolyMarker.h:31
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:615
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:682
Advanced 1-dimensional spectra fitting functions.
Double_t GetChi() const
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.
Definition TSpectrum.h:18
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
Definition TSpectrum.h:58
const Double_t sigma
constexpr Double_t Sqrt2()
Definition TMath.h:86
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
constexpr Double_t TwoPi()
Definition TMath.h:44
Author

Definition in file FitAwmi.C.