ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
peaks.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_spectrum
3 /// Getting Contours From TH2D.
4 /// Illustrates how to find peaks in histograms.
5 ///
6 /// This script generates a random number of gaussian peaks
7 /// on top of a linear background.
8 /// The position of the peaks is found via TSpectrum and injected
9 /// as initial values of parameters to make a global fit.
10 /// The background is computed and drawn on top of the original histogram.
11 ///
12 /// To execute this example, do:
13 ///
14 /// ~~~ {.cpp}
15 /// root > .x peaks.C (generate 10 peaks by default)
16 /// root > .x peaks.C++ (use the compiler)
17 /// root > .x peaks.C++(30) (generates 30 peaks)
18 /// ~~~
19 ///
20 /// To execute only the first part of the script (without fitting)
21 /// specify a negative value for the number of peaks, eg
22 ///
23 /// ~~~ {.cpp}
24 /// root > .x peaks.C(-20)
25 /// ~~~
26 ///
27 /// \macro_image
28 /// \macro_code
29 ///
30 /// \author Rene Brun
31 
32 #include "TCanvas.h"
33 #include "TMath.h"
34 #include "TH1.h"
35 #include "TF1.h"
36 #include "TRandom.h"
37 #include "TSpectrum.h"
38 #include "TVirtualFitter.h"
39 
40 Int_t npeaks = 30;
41 Double_t fpeaks(Double_t *x, Double_t *par) {
42  Double_t result = par[0] + par[1]*x[0];
43  for (Int_t p=0;p<npeaks;p++) {
44  Double_t norm = par[3*p+2];
45  Double_t mean = par[3*p+3];
46  Double_t sigma = par[3*p+4];
47  result += norm*TMath::Gaus(x[0],mean,sigma);
48  }
49  return result;
50 }
51 void peaks(Int_t np=10) {
52  npeaks = TMath::Abs(np);
53  TH1F *h = new TH1F("h","test",500,0,1000);
54  //generate n peaks at random
55  Double_t par[3000];
56  par[0] = 0.8;
57  par[1] = -0.6/1000;
58  Int_t p;
59  for (p=0;p<npeaks;p++) {
60  par[3*p+2] = 1;
61  par[3*p+3] = 10+gRandom->Rndm()*980;
62  par[3*p+4] = 3+2*gRandom->Rndm();
63  }
64  TF1 *f = new TF1("f",fpeaks,0,1000,2+3*npeaks);
65  f->SetNpx(1000);
66  f->SetParameters(par);
67  TCanvas *c1 = new TCanvas("c1","c1",10,10,1000,900);
68  c1->Divide(1,2);
69  c1->cd(1);
70  h->FillRandom("f",200000);
71  h->Draw();
72  TH1F *h2 = (TH1F*)h->Clone("h2");
73  //Use TSpectrum to find the peak candidates
74  TSpectrum *s = new TSpectrum(2*npeaks);
75  Int_t nfound = s->Search(h,2,"",0.10);
76  printf("Found %d candidate peaks to fit\n",nfound);
77  //Estimate background using TSpectrum::Background
78  TH1 *hb = s->Background(h,20,"same");
79  if (hb) c1->Update();
80  if (np <0) return;
81 
82  //estimate linear background using a fitting method
83  c1->cd(2);
84  TF1 *fline = new TF1("fline","pol1",0,1000);
85  h->Fit("fline","qn");
86  //Loop on all found peaks. Eliminate peaks at the background level
87  par[0] = fline->GetParameter(0);
88  par[1] = fline->GetParameter(1);
89  npeaks = 0;
90  Double_t *xpeaks = s->GetPositionX();
91  for (p=0;p<nfound;p++) {
92  Double_t xp = xpeaks[p];
93  Int_t bin = h->GetXaxis()->FindBin(xp);
94  Double_t yp = h->GetBinContent(bin);
95  if (yp-TMath::Sqrt(yp) < fline->Eval(xp)) continue;
96  par[3*npeaks+2] = yp;
97  par[3*npeaks+3] = xp;
98  par[3*npeaks+4] = 3;
99  npeaks++;
100  }
101  printf("Found %d useful peaks to fit\n",npeaks);
102  printf("Now fitting: Be patient\n");
103  TF1 *fit = new TF1("fit",fpeaks,0,1000,2+3*npeaks);
104  //we may have more than the default 25 parameters
105  TVirtualFitter::Fitter(h2,10+3*npeaks);
106  fit->SetParameters(par);
107  fit->SetNpx(1000);
108  h2->Fit("fit");
109 }
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition: TF1.cxx:3116
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4629
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2565
tuple np
Definition: multifit.py:30
const Double_t sigma
virtual Int_t Search(const TH1 *hist, Double_t sigma=2, Option_t *option="", Double_t threshold=0.05)
One-dimensional peak search function.
Definition: TSpectrum.cxx:262
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculate a gaussian function with mean and sigma.
Definition: TMath.cxx:453
Double_t * GetPositionX() const
Definition: TSpectrum.h:60
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:264
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
virtual TH1 * Background(const TH1 *hist, Int_t niter=20, Option_t *option="")
One-dimensional background estimation function.
Definition: TSpectrum.cxx:148
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
The TH1 histogram class.
Definition: TH1.h:80
Advanced Spectra Processing.
Definition: TSpectrum.h:20
virtual Double_t GetParameter(Int_t ipar) const
Definition: TF1.h:352
static TVirtualFitter * Fitter(TObject *obj, Int_t maxpar=25)
Static function returning a pointer to the current fitter.
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
1-Dim function class
Definition: TF1.h:149
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
Definition: TF1.cxx:1162
double result[121]
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3607
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
TAxis * GetXaxis()
Definition: TH1.h:319