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 19.9471 2
created -9.4 49.8678 5
created -9.16 39.8942 4
created -8.92 39.8942 4
created -8.68 9.97356 1
created -8.44 79.7885 8
created -8.2 29.9207 3
created -7.96 89.762 9
created -7.72 49.8678 5
created -7.48 9.97356 1
created -7.24 69.8149 7
created -7 49.8678 5
created -6.76 89.762 9
created -6.52 99.7356 10
created -6.28 79.7885 8
created -6.04 49.8678 5
created -5.8 59.8413 6
created -5.56 39.8942 4
created -5.32 89.762 9
created -5.08 69.8149 7
created -4.84 79.7885 8
created -4.6 79.7885 8
created -4.36 19.9471 2
created -4.12 49.8678 5
created -3.88 99.7356 10
created -3.64 59.8413 6
created -3.4 39.8942 4
created -3.16 29.9207 3
created -2.92 69.8149 7
created -2.68 59.8413 6
created -2.44 39.8942 4
created -2.2 99.7356 10
created -1.96 59.8413 6
created -1.72 19.9471 2
created -1.48 79.7885 8
created -1.24 29.9207 3
created -1 19.9471 2
created -0.76 49.8678 5
created -0.52 59.8413 6
created -0.28 79.7885 8
created -0.04 99.7356 10
created 0.2 69.8149 7
created 0.44 79.7885 8
created 0.68 89.762 9
created 0.92 69.8149 7
created 1.16 99.7356 10
created 1.4 9.97356 1
created 1.64 99.7356 10
created 1.88 19.9471 2
created 2.12 9.97356 1
created 2.36 49.8678 5
created 2.6 39.8942 4
created 2.84 9.97356 1
created 3.08 99.7356 10
created 3.32 29.9207 3
created 3.56 59.8413 6
created 3.8 9.97356 1
created 4.04 79.7885 8
created 4.28 29.9207 3
created 4.52 49.8678 5
created 4.76 29.9207 3
created 5 89.762 9
created 5.24 49.8678 5
created 5.48 19.9471 2
created 5.72 89.762 9
created 5.96 29.9207 3
created 6.2 59.8413 6
created 6.44 49.8678 5
created 6.68 59.8413 6
created 6.92 99.7356 10
created 7.16 29.9207 3
created 7.4 69.8149 7
created 7.64 39.8942 4
created 7.88 99.7356 10
created 8.12 89.762 9
created 8.36 79.7885 8
created 8.6 69.8149 7
created 8.84 89.762 9
created 9.08 69.8149 7
created 9.32 79.7885 8
created 9.56 99.7356 10
created 9.8 29.9207 3
the total number of created peaks = 83 with sigma = 0.04
the total number of found peaks = 83 with sigma = 0.0400021 (+-1.05442e-05)
fit chi^2 = 5.92128e-06
found -6.52 (+-0.000127917) 99.7349 (+-0.313834) 10.0004 (+-0.00109031)
found -0.0400002 (+-0.0001278) 99.7344 (+-0.313798) 10.0004 (+-0.00109018)
found 1.16 (+-0.000127119) 99.7326 (+-0.313605) 10.0002 (+-0.00108951)
found 6.92 (+-0.000127353) 99.7328 (+-0.313664) 10.0002 (+-0.00108972)
found 9.56 (+-0.000127478) 99.7334 (+-0.313703) 10.0003 (+-0.00108985)
found -3.88 (+-0.000127535) 99.7333 (+-0.313717) 10.0003 (+-0.0010899)
found -2.2 (+-0.000127452) 99.7331 (+-0.313692) 10.0003 (+-0.00108982)
found 1.64 (+-0.00012668) 99.7312 (+-0.313476) 10.0001 (+-0.00108907)
found 3.08 (+-0.000126803) 99.7314 (+-0.31351) 10.0001 (+-0.00108919)
found 7.88 (+-0.000127633) 99.7339 (+-0.313749) 10.0003 (+-0.00109001)
found 0.68 (+-0.000134816) 89.7614 (+-0.297723) 9.0004 (+-0.00103434)
found 8.12 (+-0.000135002) 89.7622 (+-0.297776) 9.00048 (+-0.00103452)
found 8.84 (+-0.000134749) 89.7611 (+-0.297705) 9.00037 (+-0.00103427)
found -7.96 (+-0.000134246) 89.7595 (+-0.297569) 9.00021 (+-0.0010338)
found -6.76 (+-0.000134782) 89.7614 (+-0.297716) 9.0004 (+-0.00103431)
found -5.32 (+-0.000134506) 89.7603 (+-0.297638) 9.00029 (+-0.00103404)
found 5 (+-0.000134246) 89.7595 (+-0.297569) 9.00021 (+-0.0010338)
found 5.72 (+-0.000133913) 89.7587 (+-0.297483) 9.00013 (+-0.0010335)
found -6.28 (+-0.000143084) 79.7883 (+-0.280721) 8.0004 (+-0.000975269)
found -4.84 (+-0.000143121) 79.7883 (+-0.280728) 8.0004 (+-0.000975295)
found -4.6 (+-0.00014258) 79.787 (+-0.280601) 8.00026 (+-0.000974852)
found -0.279999 (+-0.000143173) 79.7886 (+-0.280743) 8.00043 (+-0.000975345)
found 0.44 (+-0.00014319) 79.7886 (+-0.280746) 8.00043 (+-0.000975357)
found 8.36 (+-0.00014319) 79.7886 (+-0.280746) 8.00043 (+-0.000975357)
found 9.32 (+-0.000143254) 79.7889 (+-0.280763) 8.00045 (+-0.000975415)
found -8.44 (+-0.00014191) 79.7854 (+-0.280443) 8.0001 (+-0.000974304)
found -1.48 (+-0.000142122) 79.7857 (+-0.280488) 8.00013 (+-0.000974461)
found 4.04 (+-0.00014191) 79.7854 (+-0.280443) 8.0001 (+-0.000974304)
found -5.08 (+-0.000153327) 69.8158 (+-0.26267) 7.00045 (+-0.000912558)
found 0.199999 (+-0.0001534) 69.8161 (+-0.262687) 7.00048 (+-0.000912616)
found 0.92 (+-0.000153479) 69.8164 (+-0.262705) 7.0005 (+-0.000912678)
found 8.6 (+-0.000153327) 69.8158 (+-0.26267) 7.00045 (+-0.000912558)
found 9.08 (+-0.000153327) 69.8158 (+-0.26267) 7.00045 (+-0.000912558)
found -7.24 (+-0.000152048) 69.8129 (+-0.262401) 7.00016 (+-0.000911623)
found -2.92 (+-0.000152553) 69.8137 (+-0.262501) 7.00024 (+-0.00091197)
found 7.4 (+-0.00015234) 69.8132 (+-0.262455) 7.00019 (+-0.00091181)
found -3.64 (+-0.000165475) 59.842 (+-0.243162) 6.00037 (+-0.000844782)
found -1.96 (+-0.000165129) 59.8415 (+-0.243101) 6.00032 (+-0.000844571)
found -5.8 (+-0.000164986) 59.8407 (+-0.243066) 6.00024 (+-0.000844451)
found -2.68 (+-0.000165206) 59.8412 (+-0.243108) 6.00029 (+-0.000844597)
found -0.519999 (+-0.000165433) 59.8417 (+-0.243151) 6.00035 (+-0.000844746)
found 3.56 (+-0.000164093) 59.8394 (+-0.24291) 6.00011 (+-0.000843907)
found 6.2 (+-0.000164834) 59.8404 (+-0.243039) 6.00021 (+-0.000844355)
found 6.68 (+-0.000165606) 59.8423 (+-0.243186) 6.0004 (+-0.000844866)
found -7.72 (+-0.00018073) 49.8679 (+-0.2219) 5.00027 (+-0.000770917)
found 5.24 (+-0.00018105) 49.8682 (+-0.221944) 5.00029 (+-0.00077107)
found -7 (+-0.000181872) 49.8695 (+-0.222071) 5.00043 (+-0.00077151)
found -6.04 (+-0.000181641) 49.8689 (+-0.222033) 5.00037 (+-0.000771379)
found 4.52 (+-0.000180465) 49.8668 (+-0.221847) 5.00016 (+-0.000770731)
found 6.44 (+-0.000181402) 49.8684 (+-0.221994) 5.00032 (+-0.000771243)
found -9.4 (+-0.000180422) 49.8668 (+-0.221841) 5.00016 (+-0.000770713)
found -4.12 (+-0.000181148) 49.8684 (+-0.221961) 5.00032 (+-0.000771128)
found -0.759998 (+-0.000180709) 49.8674 (+-0.221888) 5.00021 (+-0.000770873)
found 2.36 (+-0.000180105) 49.8665 (+-0.221798) 5.00013 (+-0.00077056)
found -9.16 (+-0.000202697) 39.8946 (+-0.198543) 4.00024 (+-0.000689768)
found -8.92 (+-0.000201639) 39.8935 (+-0.198415) 4.00014 (+-0.000689325)
found -5.56 (+-0.000203645) 39.8962 (+-0.198668) 4.0004 (+-0.000690203)
found -3.4 (+-0.000202645) 39.8946 (+-0.198537) 4.00024 (+-0.000689749)
found -2.44 (+-0.000203766) 39.8964 (+-0.198684) 4.00043 (+-0.00069026)
found 2.6 (+-0.000201825) 39.8938 (+-0.198439) 4.00016 (+-0.000689409)
found 7.64 (+-0.00020392) 39.8967 (+-0.198704) 4.00045 (+-0.00069033)
found -9.88 (+-0.000201479) 39.8927 (+-0.198374) 4.00006 (+-0.000689183)
found -8.2 (+-0.000236267) 29.9237 (+-0.172164) 3.00045 (+-0.000598126)
found -1.24 (+-0.000234607) 29.9218 (+-0.172002) 3.00027 (+-0.000597562)
found 3.32 (+-0.000236039) 29.9234 (+-0.172142) 3.00043 (+-0.000598048)
found 4.28 (+-0.000235498) 29.9226 (+-0.172086) 3.00035 (+-0.000597856)
found 5.96 (+-0.000235885) 29.9231 (+-0.172126) 3.0004 (+-0.000597992)
found 7.16 (+-0.000236239) 29.9237 (+-0.172162) 3.00045 (+-0.000598118)
found 9.8 (+-0.000232967) 29.9226 (+-0.17189) 3.00035 (+-0.000597172)
found 4.76 (+-0.000235664) 29.9229 (+-0.172104) 3.00037 (+-0.000597916)
found -3.16 (+-0.000235069) 29.9221 (+-0.172043) 3.00029 (+-0.000597706)
found -4.36 (+-0.00028979) 19.9496 (+-0.140602) 2.00035 (+-0.000488472)
found 1.87999 (+-0.000288223) 19.9491 (+-0.140506) 2.0003 (+-0.000488141)
found -1.72 (+-0.000290106) 19.9498 (+-0.140623) 2.00037 (+-0.000488547)
found 5.48 (+-0.00029002) 19.9498 (+-0.140618) 2.00037 (+-0.00048853)
found -9.64 (+-0.000288586) 19.9485 (+-0.140518) 2.00024 (+-0.000488184)
found -0.999998 (+-0.000288169) 19.9482 (+-0.140491) 2.00021 (+-0.000488088)
found 1.4 (+-0.000416716) 9.97838 (+-0.0996731) 1.00053 (+-0.00034628)
found 3.8 (+-0.000414298) 9.97678 (+-0.099581) 1.00037 (+-0.00034596)
found -8.67999 (+-0.000413107) 9.97625 (+-0.0995386) 1.00032 (+-0.000345813)
found -7.48 (+-0.00041332) 9.97625 (+-0.0995449) 1.00032 (+-0.000345835)
found 2.84001 (+-0.000413846) 9.97679 (+-0.0995676) 1.00038 (+-0.000345914)
found 2.12001 (+-0.000409939) 9.97493 (+-0.0994258) 1.00019 (+-0.000345421)
#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:407
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:737
1-Dim function class
Definition TF1.h:233
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:622
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.