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.88 39.8942 4
created -9.64 49.8678 5
created -9.4 99.7356 10
created -9.16 79.7885 8
created -8.92 9.97356 1
created -8.68 39.8942 4
created -8.44 99.7356 10
created -8.2 19.9471 2
created -7.96 9.97356 1
created -7.72 29.9207 3
created -7.48 49.8678 5
created -7.24 49.8678 5
created -7 59.8413 6
created -6.76 19.9471 2
created -6.52 79.7885 8
created -6.28 49.8678 5
created -6.04 99.7356 10
created -5.8 19.9471 2
created -5.56 49.8678 5
created -5.32 89.762 9
created -5.08 89.762 9
created -4.84 39.8942 4
created -4.6 19.9471 2
created -4.36 39.8942 4
created -4.12 59.8413 6
created -3.88 29.9207 3
created -3.64 69.8149 7
created -3.4 39.8942 4
created -3.16 49.8678 5
created -2.92 59.8413 6
created -2.68 59.8413 6
created -2.44 49.8678 5
created -2.2 39.8942 4
created -1.96 69.8149 7
created -1.72 39.8942 4
created -1.48 79.7885 8
created -1.24 49.8678 5
created -1 19.9471 2
created -0.76 19.9471 2
created -0.52 89.762 9
created -0.28 39.8942 4
created -0.04 69.8149 7
created 0.2 79.7885 8
created 0.44 29.9207 3
created 0.68 59.8413 6
created 0.92 69.8149 7
created 1.16 99.7356 10
created 1.4 99.7356 10
created 1.64 79.7885 8
created 1.88 49.8678 5
created 2.12 59.8413 6
created 2.36 69.8149 7
created 2.6 59.8413 6
created 2.84 79.7885 8
created 3.08 39.8942 4
created 3.32 49.8678 5
created 3.56 79.7885 8
created 3.8 39.8942 4
created 4.04 59.8413 6
created 4.28 49.8678 5
created 4.52 89.762 9
created 4.76 69.8149 7
created 5 29.9207 3
created 5.24 49.8678 5
created 5.48 39.8942 4
created 5.72 19.9471 2
created 5.96 99.7356 10
created 6.2 39.8942 4
created 6.44 79.7885 8
created 6.68 89.762 9
created 6.92 79.7885 8
created 7.16 9.97356 1
created 7.4 29.9207 3
created 7.64 99.7356 10
created 7.88 49.8678 5
created 8.12 29.9207 3
created 8.36 19.9471 2
created 8.6 69.8149 7
created 8.84 29.9207 3
created 9.08 99.7356 10
created 9.32 89.762 9
created 9.56 39.8942 4
created 9.8 99.7356 10
the total number of created peaks = 83 with sigma = 0.04
the total number of found peaks = 83 with sigma = 0.040002 (+-1.798e-05)
fit chi^2 = 1.67531e-05
found 1.16 (+-0.000215149) 99.735 (+-0.527883) 10.0005 (+-0.00183395)
found 1.4 (+-0.000215251) 99.7353 (+-0.527914) 10.0005 (+-0.00183406)
found -9.4 (+-0.000214733) 99.7339 (+-0.527754) 10.0003 (+-0.0018335)
found -8.44 (+-0.000213745) 99.732 (+-0.527466) 10.0002 (+-0.0018325)
found -6.04 (+-0.000213884) 99.7323 (+-0.527507) 10.0002 (+-0.00183264)
found 5.96 (+-0.000213745) 99.7321 (+-0.527466) 10.0002 (+-0.0018325)
found 7.64 (+-0.000214093) 99.7326 (+-0.527564) 10.0002 (+-0.00183284)
found 9.08 (+-0.000214519) 99.7337 (+-0.527696) 10.0003 (+-0.0018333)
found 9.8 (+-0.000212701) 99.7342 (+-0.527226) 10.0004 (+-0.00183166)
found -5.08 (+-0.00022646) 89.7609 (+-0.500705) 9.00034 (+-0.00173953)
found 6.68 (+-0.000226879) 89.7617 (+-0.500819) 9.00042 (+-0.00173992)
found 9.32 (+-0.000226556) 89.7612 (+-0.500733) 9.00037 (+-0.00173963)
found -5.32 (+-0.000226614) 89.7611 (+-0.500746) 9.00037 (+-0.00173967)
found -0.52 (+-0.00022543) 89.759 (+-0.500429) 9.00016 (+-0.00173857)
found 4.52 (+-0.000226399) 89.7606 (+-0.500685) 9.00032 (+-0.00173946)
found -9.16 (+-0.000239689) 79.7873 (+-0.471968) 8.00029 (+-0.00163969)
found 0.199999 (+-0.000239957) 79.787 (+-0.472011) 8.00026 (+-0.00163984)
found 1.64 (+-0.000240675) 79.7884 (+-0.472188) 8.0004 (+-0.00164046)
found 2.84 (+-0.000240023) 79.7871 (+-0.472024) 8.00026 (+-0.00163989)
found 6.92 (+-0.000239582) 79.787 (+-0.47194) 8.00026 (+-0.00163959)
found -6.52 (+-0.000239423) 79.7863 (+-0.471884) 8.00019 (+-0.0016394)
found -1.48 (+-0.000239874) 79.7868 (+-0.471987) 8.00024 (+-0.00163976)
found 3.56 (+-0.000239874) 79.7868 (+-0.471987) 8.00024 (+-0.00163976)
found 6.44 (+-0.000240396) 79.7879 (+-0.472119) 8.00035 (+-0.00164022)
found 0.920001 (+-0.000257733) 69.8156 (+-0.441789) 7.00043 (+-0.00153484)
found 2.36 (+-0.000257186) 69.8145 (+-0.441665) 7.00032 (+-0.00153441)
found 4.76 (+-0.000257023) 69.8145 (+-0.441636) 7.00032 (+-0.00153431)
found -3.64 (+-0.000256245) 69.8132 (+-0.441464) 7.00019 (+-0.00153371)
found -1.96 (+-0.000256469) 69.8135 (+-0.44151) 7.00021 (+-0.00153388)
found -0.039999 (+-0.000257119) 69.8145 (+-0.441653) 7.00032 (+-0.00153437)
found 8.6 (+-0.000255742) 69.8127 (+-0.441361) 7.00013 (+-0.00153336)
found -7 (+-0.00027694) 59.8402 (+-0.408749) 6.00019 (+-0.00142006)
found -4.12 (+-0.000277042) 59.8402 (+-0.408764) 6.00019 (+-0.00142011)
found -2.92 (+-0.000277929) 59.8412 (+-0.408928) 6.00029 (+-0.00142068)
found -2.68 (+-0.000277929) 59.8412 (+-0.408928) 6.00029 (+-0.00142068)
found 0.680001 (+-0.000277628) 59.841 (+-0.408875) 6.00027 (+-0.0014205)
found 2.12 (+-0.000278105) 59.8415 (+-0.408962) 6.00032 (+-0.0014208)
found 2.6 (+-0.000278639) 59.8423 (+-0.409065) 6.0004 (+-0.00142116)
found 4.04 (+-0.000277516) 59.8407 (+-0.408851) 6.00024 (+-0.00142041)
found 7.88 (+-0.000305078) 49.8687 (+-0.373406) 5.00035 (+-0.00129727)
found -9.64 (+-0.000305382) 49.869 (+-0.373452) 5.00037 (+-0.00129743)
found -7.48 (+-0.000304109) 49.8674 (+-0.373246) 5.00021 (+-0.00129671)
found -7.24 (+-0.000304899) 49.8682 (+-0.373369) 5.00029 (+-0.00129714)
found -6.28 (+-0.000306279) 49.87 (+-0.373596) 5.00048 (+-0.00129793)
found -3.16 (+-0.000304639) 49.8679 (+-0.373329) 5.00027 (+-0.001297)
found -2.44 (+-0.000304639) 49.8679 (+-0.373329) 5.00027 (+-0.001297)
found -1.24 (+-0.00030436) 49.8679 (+-0.373293) 5.00027 (+-0.00129688)
found 1.88 (+-0.00030553) 49.869 (+-0.373472) 5.00037 (+-0.0012975)
found 3.32 (+-0.000305039) 49.8684 (+-0.373394) 5.00032 (+-0.00129723)
found 4.28 (+-0.000305708) 49.8692 (+-0.373502) 5.0004 (+-0.0012976)
found 5.24 (+-0.000303852) 49.8671 (+-0.373205) 5.00019 (+-0.00129657)
found -5.56 (+-0.000304536) 49.8682 (+-0.373323) 5.0003 (+-0.00129698)
found -4.84 (+-0.000341106) 39.8951 (+-0.333991) 4.00029 (+-0.00116034)
found -0.280001 (+-0.000342801) 39.8965 (+-0.334204) 4.00043 (+-0.00116108)
found 6.2 (+-0.000343242) 39.897 (+-0.334263) 4.00048 (+-0.00116128)
found 9.56 (+-0.000343462) 39.8973 (+-0.334293) 4.00051 (+-0.00116139)
found -3.4 (+-0.000341804) 39.8954 (+-0.334072) 4.00032 (+-0.00116062)
found -2.2 (+-0.000341804) 39.8954 (+-0.334072) 4.00032 (+-0.00116062)
found -1.72 (+-0.000342582) 39.8962 (+-0.334174) 4.0004 (+-0.00116097)
found 3.08 (+-0.000342039) 39.8957 (+-0.334103) 4.00035 (+-0.00116073)
found 3.8 (+-0.000342324) 39.8959 (+-0.33414) 4.00037 (+-0.00116086)
found 5.48 (+-0.000340124) 39.8941 (+-0.333858) 4.00019 (+-0.00115988)
found -9.88 (+-0.000340028) 39.8936 (+-0.333818) 4.00014 (+-0.00115974)
found -8.68 (+-0.000340657) 39.8952 (+-0.333945) 4.0003 (+-0.00116018)
found -4.36 (+-0.000340404) 39.8943 (+-0.333895) 4.00021 (+-0.00116001)
found 0.439999 (+-0.000396491) 29.9229 (+-0.289496) 3.00037 (+-0.00100575)
found -3.88 (+-0.000396187) 29.9226 (+-0.289465) 3.00035 (+-0.00100565)
found 5 (+-0.000395816) 29.9224 (+-0.289428) 3.00032 (+-0.00100552)
found 8.12 (+-0.000393628) 29.921 (+-0.289215) 3.00019 (+-0.00100478)
found 8.84 (+-0.000397367) 29.9237 (+-0.289586) 3.00045 (+-0.00100607)
found -7.72 (+-0.000392806) 29.9208 (+-0.289143) 3.00016 (+-0.00100453)
found 7.40001 (+-0.000394323) 29.9221 (+-0.289301) 3.0003 (+-0.00100508)
found -8.20001 (+-0.000484807) 19.9491 (+-0.236339) 2.0003 (+-0.00082108)
found -5.8 (+-0.000488187) 19.9501 (+-0.236553) 2.0004 (+-0.000821824)
found -6.76 (+-0.000487974) 19.9498 (+-0.236536) 2.00037 (+-0.000821763)
found -1 (+-0.000483857) 19.948 (+-0.236258) 2.00019 (+-0.000820798)
found -4.6 (+-0.000484819) 19.9482 (+-0.236319) 2.00021 (+-0.00082101)
found -0.759993 (+-0.000485646) 19.9491 (+-0.236385) 2.0003 (+-0.000821239)
found 5.72001 (+-0.000487579) 19.9498 (+-0.236513) 2.00037 (+-0.000821682)
found 8.36 (+-0.000485706) 19.9488 (+-0.236382) 2.00027 (+-0.00082123)
found -8.92001 (+-0.000694867) 9.97627 (+-0.167429) 1.00032 (+-0.000581676)
found 7.15999 (+-0.000693571) 9.97601 (+-0.167385) 1.0003 (+-0.000581521)
found -7.96 (+-0.000687227) 9.9744 (+-0.167157) 1.00014 (+-0.000580731)
#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:3857
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.