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

Detailed Description

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 15.9577 4
created -9.1 23.9365 6
created -8.5 35.9048 9
created -7.9 7.97885 2
created -7.3 11.9683 3
created -6.7 3.98942 1
created -6.1 7.97885 2
created -5.5 31.9154 8
created -4.9 15.9577 4
created -4.3 39.8942 10
created -3.7 15.9577 4
created -3.1 19.9471 5
created -2.5 39.8942 10
created -1.9 27.926 7
created -1.3 27.926 7
created -0.7 39.8942 10
created -0.1 3.98942 1
created 0.5 3.98942 1
created 1.1 11.9683 3
created 1.7 15.9577 4
created 2.3 31.9154 8
created 2.9 23.9365 6
created 3.5 31.9154 8
created 4.1 39.8942 10
created 4.7 23.9365 6
created 5.3 15.9577 4
created 5.9 39.8942 10
created 6.5 11.9683 3
created 7.1 39.8942 10
created 7.7 39.8942 10
created 8.3 19.9471 5
created 8.9 3.98942 1
created 9.5 31.9154 8
the total number of created peaks = 33 with sigma = 0.1
the total number of found peaks = 33 with sigma = 0.100002 (+-4.30368e-05)
fit chi^2 = 5.54738e-06
found -4.3 (+-0.000325978) 39.8939 (+-0.128519) 10.0001 (+-0.00105469)
found -2.5 (+-0.000326785) 39.8941 (+-0.128557) 10.0002 (+-0.001055)
found -0.700001 (+-0.000325555) 39.8939 (+-0.128503) 10.0001 (+-0.00105456)
found 4.1 (+-0.000327139) 39.8942 (+-0.128574) 10.0002 (+-0.00105514)
found 5.9 (+-0.000325722) 39.8938 (+-0.128507) 10.0001 (+-0.00105459)
found 7.1 (+-0.000326748) 39.8941 (+-0.128557) 10.0002 (+-0.001055)
found 7.7 (+-0.000327228) 39.8942 (+-0.128579) 10.0002 (+-0.00105518)
found -8.5 (+-0.000343646) 35.9045 (+-0.121927) 9.0001 (+-0.00100059)
found -5.5 (+-0.000364247) 31.9151 (+-0.114944) 8.00008 (+-0.000943284)
found 2.3 (+-0.000365448) 31.9153 (+-0.114989) 8.00013 (+-0.000943653)
found 3.5 (+-0.000366704) 31.9156 (+-0.115038) 8.00021 (+-0.00094406)
found 9.50001 (+-0.000361094) 31.9152 (+-0.114841) 8.00011 (+-0.000942445)
found -1.9 (+-0.000392696) 27.9263 (+-0.107632) 7.00022 (+-0.000883282)
found -1.3 (+-0.000392696) 27.9263 (+-0.107632) 7.00022 (+-0.000883282)
found 4.7 (+-0.000423815) 23.9368 (+-0.0996385) 6.00018 (+-0.000817682)
found -9.1 (+-0.000423603) 23.9368 (+-0.0996318) 6.00017 (+-0.000817627)
found 2.9 (+-0.00042455) 23.9369 (+-0.0996598) 6.00021 (+-0.000817857)
found 8.3 (+-0.000463101) 19.9473 (+-0.0909323) 5.00014 (+-0.000746235)
found -3.1 (+-0.000464999) 19.9475 (+-0.0909759) 5.00018 (+-0.000746593)
found -4.9 (+-0.00052264) 15.9583 (+-0.0814284) 4.00023 (+-0.000668241)
found -3.7 (+-0.000521459) 15.9582 (+-0.0814039) 4.0002 (+-0.00066804)
found 5.3 (+-0.000521895) 15.9582 (+-0.0814128) 4.00021 (+-0.000668113)
found -9.7 (+-0.000518375) 15.9577 (+-0.0813342) 4.00008 (+-0.000667468)
found 1.7 (+-0.000519759) 15.958 (+-0.0813689) 4.00014 (+-0.000667753)
found 6.5 (+-0.000606269) 11.9691 (+-0.0705648) 3.00026 (+-0.000579089)
found -7.3 (+-0.000595816) 11.9682 (+-0.0704024) 3.00004 (+-0.000577756)
found 1.1 (+-0.000597455) 11.9683 (+-0.0704272) 3.00006 (+-0.00057796)
found -7.90001 (+-0.000740737) 7.97932 (+-0.0575977) 2.00016 (+-0.000472675)
found -6.09999 (+-0.000736966) 7.97916 (+-0.0575598) 2.00012 (+-0.000472364)
found -0.10002 (+-0.00105201) 3.98993 (+-0.0407571) 1.00015 (+-0.000334473)
found 8.90001 (+-0.00105947) 3.99003 (+-0.0407949) 1.00017 (+-0.000334783)
found -6.7 (+-0.00104638) 3.98961 (+-0.0407203) 1.00007 (+-0.00033417)
found 0.500004 (+-0.00104301) 3.98956 (+-0.040703) 1.00005 (+-0.000334029)
#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
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
@ kRed
Definition Rtypes.h:67
float xmin
float xmax
#define gROOT
Definition TROOT.h:417
externTRandom * gRandom
Definition TRandom.h:62
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:41
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:43
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:48
The Canvas class.
Definition TCanvas.h:23
void Clear(Option_t *option="") override
Remove all primitives from the canvas.
Definition TCanvas.cxx:734
Definition TF1.h:182
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
A PolyMarker is defined by an array on N points in a 2-D space.
Definition TPolyMarker.h:31
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)
Double_t * GetPositionX() const
Definition TSpectrum.h:58
const Double_t sigma
constexpr Double_t Sqrt2()
Definition TMath.h:89
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673
constexpr Double_t TwoPi()
Definition TMath.h:47
Author

Definition in file FitAwmi.C.