Logo ROOT  
Reference Guide
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 9.97356 1
created -9.64 99.7356 10
created -9.4 49.8678 5
created -9.16 99.7356 10
created -8.92 9.97356 1
created -8.68 59.8413 6
created -8.44 39.8942 4
created -8.2 19.9471 2
created -7.96 79.7885 8
created -7.72 49.8678 5
created -7.48 99.7356 10
created -7.24 49.8678 5
created -7 49.8678 5
created -6.76 69.8149 7
created -6.52 99.7356 10
created -6.28 99.7356 10
created -6.04 49.8678 5
created -5.8 9.97356 1
created -5.56 69.8149 7
created -5.32 79.7885 8
created -5.08 69.8149 7
created -4.84 19.9471 2
created -4.6 19.9471 2
created -4.36 89.762 9
created -4.12 29.9207 3
created -3.88 99.7356 10
created -3.64 69.8149 7
created -3.4 89.762 9
created -3.16 69.8149 7
created -2.92 99.7356 10
created -2.68 19.9471 2
created -2.44 39.8942 4
created -2.2 49.8678 5
created -1.96 19.9471 2
created -1.72 19.9471 2
created -1.48 39.8942 4
created -1.24 29.9207 3
created -1 79.7885 8
created -0.76 49.8678 5
created -0.52 99.7356 10
created -0.28 79.7885 8
created -0.04 19.9471 2
created 0.2 89.762 9
created 0.44 49.8678 5
created 0.68 19.9471 2
created 0.92 89.762 9
created 1.16 69.8149 7
created 1.4 59.8413 6
created 1.64 9.97356 1
created 1.88 59.8413 6
created 2.12 19.9471 2
created 2.36 19.9471 2
created 2.6 29.9207 3
created 2.84 19.9471 2
created 3.08 9.97356 1
created 3.32 39.8942 4
created 3.56 49.8678 5
created 3.8 89.762 9
created 4.04 29.9207 3
created 4.28 79.7885 8
created 4.52 79.7885 8
created 4.76 39.8942 4
created 5 29.9207 3
created 5.24 59.8413 6
created 5.48 49.8678 5
created 5.72 69.8149 7
created 5.96 79.7885 8
created 6.2 49.8678 5
created 6.44 29.9207 3
created 6.68 79.7885 8
created 6.92 69.8149 7
created 7.16 89.762 9
created 7.4 29.9207 3
created 7.64 19.9471 2
created 7.88 49.8678 5
created 8.12 29.9207 3
created 8.36 39.8942 4
created 8.6 9.97356 1
created 8.84 9.97356 1
created 9.08 39.8942 4
created 9.32 49.8678 5
created 9.56 9.97356 1
created 9.8 9.97356 1
the total number of created peaks = 83 with sigma = 0.04
the total number of found peaks = 83 with sigma = 0.0400021 (+-7.42016e-06)
fit chi^2 = 2.59904e-06
found -6.52 (+-8.47418e-05) 99.7349 (+-0.20792) 10.0004 (+-0.000722346)
found -6.28 (+-8.46494e-05) 99.7344 (+-0.207892) 10.0004 (+-0.00072225)
found -2.92 (+-8.43348e-05) 99.7328 (+-0.207799) 10.0002 (+-0.000721928)
found -9.64 (+-8.41284e-05) 99.7319 (+-0.207741) 10.0002 (+-0.000721727)
found -9.16 (+-8.41283e-05) 99.732 (+-0.207741) 10.0002 (+-0.000721727)
found -7.48 (+-8.44464e-05) 99.7331 (+-0.207829) 10.0003 (+-0.000722031)
found -3.88 (+-8.44173e-05) 99.7331 (+-0.207822) 10.0003 (+-0.000722007)
found -0.519999 (+-8.45779e-05) 99.7339 (+-0.207869) 10.0003 (+-0.000722171)
found -3.4 (+-8.92741e-05) 89.7611 (+-0.197235) 9.00037 (+-0.000685226)
found 7.16 (+-8.9041e-05) 89.76 (+-0.197173) 9.00026 (+-0.000685009)
found -4.36 (+-8.87203e-05) 89.7587 (+-0.197088) 9.00013 (+-0.000684715)
found 0.200001 (+-8.88509e-05) 89.7592 (+-0.197122) 9.00018 (+-0.000684835)
found 0.920001 (+-8.89509e-05) 89.7597 (+-0.19715) 9.00024 (+-0.000684931)
found 3.8 (+-8.89407e-05) 89.7595 (+-0.197145) 9.00021 (+-0.000684913)
found -5.32 (+-9.47715e-05) 79.788 (+-0.185976) 8.00037 (+-0.000646109)
found -0.280002 (+-9.45493e-05) 79.7875 (+-0.185926) 8.00032 (+-0.000645937)
found 4.52 (+-9.46409e-05) 79.7875 (+-0.185944) 8.00032 (+-0.000646)
found 5.96 (+-9.46592e-05) 79.7875 (+-0.185948) 8.00032 (+-0.000646012)
found -7.96 (+-9.43026e-05) 79.7862 (+-0.185863) 8.00018 (+-0.000645719)
found -1 (+-9.44017e-05) 79.7865 (+-0.185886) 8.00021 (+-0.000645796)
found 4.28 (+-9.45617e-05) 79.7873 (+-0.185926) 8.00029 (+-0.000645936)
found 6.68 (+-9.45132e-05) 79.787 (+-0.185913) 8.00027 (+-0.000645893)
found -5.08 (+-0.000101073) 69.8139 (+-0.173916) 7.00026 (+-0.000604211)
found -3.64 (+-0.000101682) 69.8163 (+-0.174047) 7.0005 (+-0.000604667)
found -3.16 (+-0.000101682) 69.8163 (+-0.174047) 7.0005 (+-0.000604667)
found 1.16 (+-0.000101466) 69.8153 (+-0.173998) 7.0004 (+-0.000604498)
found 6.92 (+-0.000101582) 69.8158 (+-0.174024) 7.00045 (+-0.000604588)
found -6.76 (+-0.000101448) 69.8153 (+-0.173995) 7.0004 (+-0.000604486)
found -5.56 (+-0.000100914) 69.8137 (+-0.173886) 7.00024 (+-0.000604108)
found 5.72 (+-0.000101348) 69.8147 (+-0.173972) 7.00035 (+-0.000604407)
found 1.4 (+-0.000109043) 59.8404 (+-0.160994) 6.00021 (+-0.00055932)
found 5.24 (+-0.000109206) 59.8404 (+-0.161018) 6.00021 (+-0.000559402)
found -8.68 (+-0.000108814) 59.8396 (+-0.16095) 6.00013 (+-0.000559168)
found 1.88 (+-0.000108591) 59.839 (+-0.16091) 6.00008 (+-0.000559029)
found -9.4 (+-0.000120773) 49.8705 (+-0.147173) 5.00053 (+-0.000511302)
found -7.24 (+-0.000120385) 49.8692 (+-0.147109) 5.0004 (+-0.000511082)
found -6.04 (+-0.000119802) 49.8682 (+-0.147024) 5.0003 (+-0.000510786)
found 0.439997 (+-0.000119949) 49.8682 (+-0.147042) 5.00029 (+-0.000510849)
found -7.72 (+-0.000120636) 49.87 (+-0.14715) 5.00048 (+-0.000511222)
found -7 (+-0.000120174) 49.8684 (+-0.147074) 5.00032 (+-0.000510959)
found -2.2 (+-0.000119533) 49.8668 (+-0.146974) 5.00016 (+-0.000510612)
found -0.759999 (+-0.000120636) 49.87 (+-0.14715) 5.00048 (+-0.000511222)
found 3.56 (+-0.000120217) 49.8687 (+-0.147082) 5.00035 (+-0.000510988)
found 5.48 (+-0.000120265) 49.8687 (+-0.147089) 5.00035 (+-0.00051101)
found 6.2 (+-0.000120028) 49.8682 (+-0.147052) 5.00029 (+-0.000510884)
found 9.32 (+-0.000119323) 49.8665 (+-0.146945) 5.00013 (+-0.000510511)
found 7.88 (+-0.000119415) 49.8665 (+-0.146956) 5.00013 (+-0.000510549)
found -8.44 (+-0.000134077) 39.8943 (+-0.131513) 4.00021 (+-0.000456897)
found 4.76 (+-0.000134449) 39.8951 (+-0.13156) 4.00029 (+-0.000457062)
found 8.36 (+-0.000133447) 39.8932 (+-0.131435) 4.00011 (+-0.000456628)
found -2.44 (+-0.000133966) 39.894 (+-0.131498) 4.00019 (+-0.000456847)
found -1.48 (+-0.000133699) 39.8935 (+-0.131464) 4.00013 (+-0.000456727)
found 3.32 (+-0.000133713) 39.8938 (+-0.13147) 4.00016 (+-0.000456747)
found 9.08 (+-0.000133713) 39.8937 (+-0.13147) 4.00016 (+-0.000456747)
found -4.12 (+-0.000156745) 29.9242 (+-0.114084) 3.00051 (+-0.000396347)
found 4.04 (+-0.000156531) 29.9237 (+-0.114062) 3.00045 (+-0.00039627)
found 7.4 (+-0.000155541) 29.9221 (+-0.113966) 3.0003 (+-0.000395936)
found 6.44 (+-0.000156022) 29.9226 (+-0.114011) 3.00035 (+-0.000396091)
found 8.12 (+-0.000155462) 29.9215 (+-0.113954) 3.00024 (+-0.000395895)
found -1.24 (+-0.000155857) 29.9223 (+-0.113994) 3.00032 (+-0.000396035)
found 2.6 (+-0.000154462) 29.9202 (+-0.113858) 3.00011 (+-0.000395562)
found 5 (+-0.000155607) 29.9218 (+-0.113969) 3.00027 (+-0.000395946)
found -2.68001 (+-0.000192045) 19.9498 (+-0.0931564) 2.00038 (+-0.00032364)
found -0.0399991 (+-0.000192711) 19.9506 (+-0.0932013) 2.00045 (+-0.000323796)
found -4.84 (+-0.000190967) 19.9485 (+-0.0930833) 2.00024 (+-0.000323386)
found -1.96 (+-0.000190579) 19.948 (+-0.0930562) 2.00019 (+-0.000323292)
found 0.680004 (+-0.000192144) 19.9498 (+-0.0931622) 2.00038 (+-0.00032366)
found 2.12 (+-0.000190785) 19.9482 (+-0.0930704) 2.00021 (+-0.000323342)
found -8.2 (+-0.000191753) 19.9493 (+-0.0931352) 2.00032 (+-0.000323567)
found -4.59999 (+-0.000191284) 19.949 (+-0.0931061) 2.0003 (+-0.000323466)
found -1.72 (+-0.000190346) 19.9477 (+-0.0930402) 2.00016 (+-0.000323237)
found 2.36 (+-0.000190073) 19.9474 (+-0.0930219) 2.00013 (+-0.000323173)
found 2.84 (+-0.000189613) 19.9472 (+-0.0929937) 2.00011 (+-0.000323075)
found 7.64 (+-0.000190917) 19.9482 (+-0.0930779) 2.00021 (+-0.000323367)
found -8.92001 (+-0.000274974) 9.97731 (+-0.0659936) 1.00043 (+-0.000229272)
found 1.64 (+-0.000273878) 9.97624 (+-0.0659518) 1.00032 (+-0.000229127)
found -5.8 (+-0.000273832) 9.97625 (+-0.0659504) 1.00032 (+-0.000229122)
found 8.59999 (+-0.000270302) 9.97439 (+-0.0658282) 1.00013 (+-0.000228698)
found 9.55999 (+-0.000270712) 9.97465 (+-0.0658432) 1.00016 (+-0.00022875)
found 3.08 (+-0.000271179) 9.97465 (+-0.0658566) 1.00016 (+-0.000228796)
found 8.84001 (+-0.000270302) 9.97439 (+-0.0658282) 1.00014 (+-0.000228698)
found 9.8 (+-0.000265727) 9.97358 (+-0.0656931) 1.00005 (+-0.000228228)
found -9.87998 (+-0.000271377) 9.97576 (+-0.0658702) 1.00027 (+-0.000228843)
#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
static const double x1[5]
int Int_t
Definition: RtypesCore.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:90
bool Bool_t
Definition: RtypesCore.h:61
double Double_t
Definition: RtypesCore.h:57
@ kRed
Definition: Rtypes.h:64
float xmin
Definition: THbookFile.cxx:93
float xmax
Definition: THbookFile.cxx:93
#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:41
The Canvas class.
Definition: TCanvas.h:27
void Clear(Option_t *option="")
Remove all primitives from the canvas.
Definition: TCanvas.cxx:720
1-Dim function class
Definition: TF1.h:210
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
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:597
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:635
Advanced 1-dimensional spectra fitting functions.
Definition: TSpectrumFit.h:18
Double_t GetChi() const
Definition: TSpectrumFit.h:121
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
Definition: TSpectrumFit.h:117
void FitAwmi(Double_t *source)
This function fits the source spectrum.
Double_t * GetAreasErrors() const
Definition: TSpectrumFit.h:119
void GetSigma(Double_t &sigma, Double_t &sigmaErr)
This function gets the sigma parameter and its error.
@ kFitTaylorOrderFirst
Definition: TSpectrumFit.h:82
Double_t * GetAreas() const
Definition: TSpectrumFit.h:118
Double_t * GetAmplitudes() const
Definition: TSpectrumFit.h:116
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
Definition: TSpectrumFit.h:123
Double_t * GetPositions() const
Definition: TSpectrumFit.h:122
Advanced Spectra Processing.
Definition: TSpectrum.h:18
const Double_t sigma
static constexpr double s
constexpr Double_t Sqrt2()
Definition: TMath.h:89
Double_t Sqrt(Double_t x)
Definition: TMath.h:681
constexpr Double_t TwoPi()
Definition: TMath.h:45
#define dest(otri, vertexptr)
Definition: triangle.c:1040
Author

Definition in file FitAwmi.C.