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.7 19.9471 5
created -9.1 15.9577 4
created -8.5 19.9471 5
created -7.9 31.9154 8
created -7.3 19.9471 5
created -6.7 39.8942 10
created -6.1 23.9365 6
created -5.5 15.9577 4
created -4.9 15.9577 4
created -4.3 19.9471 5
created -3.7 7.97885 2
created -3.1 31.9154 8
created -2.5 7.97885 2
created -1.9 3.98942 1
created -1.3 31.9154 8
created -0.7 27.926 7
created -0.1 35.9048 9
created 0.5 19.9471 5
created 1.1 35.9048 9
created 1.7 35.9048 9
created 2.3 15.9577 4
created 2.9 11.9683 3
created 3.5 23.9365 6
created 4.1 3.98942 1
created 4.7 23.9365 6
created 5.3 7.97885 2
created 5.9 27.926 7
created 6.5 3.98942 1
created 7.1 3.98942 1
created 7.7 11.9683 3
created 8.3 15.9577 4
created 8.9 27.926 7
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.75179e-05)
fit chi^2 = 7.83132e-07
found -6.7 (+-0.000122717) 39.894 (+-0.0482993) 10.0001 (+-0.000396368)
found -0.1 (+-0.000129517) 35.9047 (+-0.0458278) 9.00015 (+-0.000376086)
found 1.1 (+-0.000129643) 35.9048 (+-0.0458335) 9.00018 (+-0.000376132)
found 1.7 (+-0.000129552) 35.9048 (+-0.0458297) 9.00017 (+-0.000376101)
found -7.9 (+-0.000137321) 31.9153 (+-0.0432048) 8.00013 (+-0.00035456)
found -3.1 (+-0.000136597) 31.915 (+-0.0431779) 8.00005 (+-0.000354339)
found -1.3 (+-0.000136924) 31.9152 (+-0.0431911) 8.0001 (+-0.000354447)
found -0.7 (+-0.000147559) 27.9263 (+-0.0404407) 7.00022 (+-0.000331876)
found 5.9 (+-0.0001459) 27.9256 (+-0.0403854) 7.00004 (+-0.000331422)
found 8.9 (+-0.000146193) 27.9257 (+-0.040395) 7.00006 (+-0.000331501)
found -6.1 (+-0.000159239) 23.9368 (+-0.0374369) 6.00018 (+-0.000307226)
found 3.5 (+-0.000157887) 23.9363 (+-0.0373978) 6.00005 (+-0.000306905)
found 4.7 (+-0.000157702) 23.9362 (+-0.0373926) 6.00004 (+-0.000306862)
found 0.5 (+-0.00017524) 19.9477 (+-0.0341954) 5.00023 (+-0.000280625)
found -9.7 (+-0.000173748) 19.9469 (+-0.0341549) 5.00005 (+-0.000280292)
found -8.5 (+-0.00017452) 19.9473 (+-0.034177) 5.00016 (+-0.000280473)
found -7.3 (+-0.000175232) 19.9477 (+-0.0341953) 5.00023 (+-0.000280623)
found -4.3 (+-0.000173608) 19.947 (+-0.0341544) 5.00008 (+-0.000280288)
found -9.1 (+-0.000195253) 15.9579 (+-0.0305714) 4.00013 (+-0.000250884)
found -5.5 (+-0.00019523) 15.9579 (+-0.030571) 4.00013 (+-0.00025088)
found 2.3 (+-0.000195409) 15.958 (+-0.0305752) 4.00016 (+-0.000250915)
found -4.9 (+-0.000195069) 15.9578 (+-0.0305676) 4.00012 (+-0.000250853)
found 8.3 (+-0.000195157) 15.9579 (+-0.0305697) 4.00013 (+-0.00025087)
found 2.9 (+-0.000226031) 11.9686 (+-0.0264846) 3.00013 (+-0.000217345)
found 7.7 (+-0.00022448) 11.9683 (+-0.0264615) 3.00006 (+-0.000217156)
found -3.7 (+-0.000278836) 7.97936 (+-0.0216463) 2.00017 (+-0.00017764)
found -2.50001 (+-0.000276899) 7.97916 (+-0.0216268) 2.00012 (+-0.00017748)
found 5.3 (+-0.000278894) 7.97936 (+-0.0216469) 2.00017 (+-0.000177645)
found 4.1 (+-0.000397712) 3.98997 (+-0.0153255) 1.00016 (+-0.000125769)
found 6.49999 (+-0.000394136) 3.98976 (+-0.0153065) 1.00011 (+-0.000125612)
found 9.49999 (+-0.000390202) 3.98976 (+-0.0152905) 1.0001 (+-0.000125481)
found -1.89999 (+-0.000395836) 3.98987 (+-0.0153155) 1.00013 (+-0.000125687)
found 7.1 (+-0.000391889) 3.98956 (+-0.0152932) 1.00005 (+-0.000125504)
#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->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
sigma /= TMath::Sqrt2(); 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:101
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:734
1-Dim function class
Definition TF1.h:233
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
TObject * FindObject(const char *name) const override
Search object named name in the list of functions.
Definition TH1.cxx:3853
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:662
constexpr Double_t TwoPi()
Definition TMath.h:44
Author

Definition in file FitAwmi.C.