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.7 31.9154 8
created -9.1 23.9365 6
created -8.5 15.9577 4
created -7.9 3.98942 1
created -7.3 31.9154 8
created -6.7 15.9577 4
created -6.1 27.926 7
created -5.5 3.98942 1
created -4.9 19.9471 5
created -4.3 11.9683 3
created -3.7 23.9365 6
created -3.1 35.9048 9
created -2.5 19.9471 5
created -1.9 7.97885 2
created -1.3 27.926 7
created -0.7 3.98942 1
created -0.1 3.98942 1
created 0.5 23.9365 6
created 1.1 15.9577 4
created 1.7 27.926 7
created 2.3 11.9683 3
created 2.9 3.98942 1
created 3.5 35.9048 9
created 4.1 35.9048 9
created 4.7 19.9471 5
created 5.3 15.9577 4
created 5.9 7.97885 2
created 6.5 3.98942 1
created 7.1 19.9471 5
created 7.7 23.9365 6
created 8.3 27.926 7
created 8.9 39.8942 10
created 9.5 23.9365 6
the total number of created peaks = 33 with sigma = 0.1
the total number of found peaks = 33 with sigma = 0.100002 (+-4.03768e-05)
fit chi^2 = 4.1838e-06
found 8.9 (+-0.000283963) 39.8942 (+-0.111653) 10.0002 (+-0.000916277)
found -3.1 (+-0.000299195) 35.9047 (+-0.105918) 9.00014 (+-0.000869211)
found 3.5 (+-0.000298474) 35.9046 (+-0.105891) 9.00013 (+-0.000868992)
found 4.1 (+-0.000299653) 35.9048 (+-0.105938) 9.00018 (+-0.000869378)
found -9.7 (+-0.000317413) 31.9151 (+-0.0998546) 8.00008 (+-0.000819455)
found -7.3 (+-0.000315867) 31.915 (+-0.0998062) 8.00006 (+-0.000819059)
found -6.1 (+-0.000337905) 27.9257 (+-0.0933675) 7.00006 (+-0.000766219)
found -1.3 (+-0.000337228) 27.9256 (+-0.0933454) 7.00004 (+-0.000766037)
found 1.7 (+-0.000338803) 27.9258 (+-0.0933954) 7.00009 (+-0.000766448)
found 8.3 (+-0.000340825) 27.9263 (+-0.0934652) 7.00021 (+-0.000767021)
found 9.5 (+-0.000365011) 23.9369 (+-0.086457) 6.0002 (+-0.000709508)
found -9.1 (+-0.000367677) 23.9367 (+-0.0865184) 6.00015 (+-0.000710012)
found -3.7 (+-0.000367521) 23.9367 (+-0.0865145) 6.00016 (+-0.00070998)
found 0.500001 (+-0.000365281) 23.9363 (+-0.0864499) 6.00006 (+-0.00070945)
found 7.7 (+-0.000367763) 23.9367 (+-0.0865205) 6.00015 (+-0.000710029)
found -2.5 (+-0.000402678) 19.9473 (+-0.0789796) 5.00014 (+-0.000648145)
found 4.7 (+-0.00040361) 19.9474 (+-0.0790016) 5.00017 (+-0.000648326)
found -4.9 (+-0.000400157) 19.9469 (+-0.0789174) 5.00005 (+-0.000647635)
found 7.1 (+-0.000401214) 19.9471 (+-0.078944) 5.00009 (+-0.000647853)
found -8.5 (+-0.000449251) 15.9578 (+-0.070623) 4.00009 (+-0.000579567)
found -6.7 (+-0.000453031) 15.9582 (+-0.0706977) 4.00019 (+-0.00058018)
found 1.1 (+-0.000452387) 15.9581 (+-0.0706841) 4.00017 (+-0.000580068)
found 5.3 (+-0.000449749) 15.9578 (+-0.0706311) 4.00009 (+-0.000579633)
found -4.3 (+-0.000522989) 11.9686 (+-0.061224) 3.00014 (+-0.000502434)
found 2.3 (+-0.000520285) 11.9685 (+-0.0611851) 3.00011 (+-0.000502115)
found -1.9 (+-0.000643943) 7.97932 (+-0.0500263) 2.00016 (+-0.00041054)
found 5.9 (+-0.000637462) 7.97896 (+-0.0499588) 2.00007 (+-0.000409986)
found -5.5 (+-0.000919119) 3.98997 (+-0.0354222) 1.00016 (+-0.000290692)
found -7.89999 (+-0.000918689) 3.98997 (+-0.0354201) 1.00016 (+-0.000290675)
found -0.700013 (+-0.00091099) 3.98977 (+-0.0353788) 1.00011 (+-0.000290336)
found 2.90001 (+-0.000917904) 3.98998 (+-0.0354164) 1.00016 (+-0.000290644)
found 6.50001 (+-0.000911712) 3.98971 (+-0.0353804) 1.00009 (+-0.000290349)
found -0.0999894 (+-0.00090994) 3.98972 (+-0.0353724) 1.00009 (+-0.000290283)
#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
const 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
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
#define gROOT
Definition: TROOT.h:404
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:23
void Clear(Option_t *option="") override
Remove all primitives from the canvas.
Definition: TCanvas.cxx:725
1-Dim function class
Definition: TF1.h:213
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:574
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:608
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:672
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.
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:
@ kFitTaylorOrderFirst
Definition: TSpectrumFit.h:82
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:86
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition: TMath.h:660
constexpr Double_t TwoPi()
Definition: TMath.h:44
Author

Definition in file FitAwmi.C.