Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
peaks.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_spectrum
3/// Illustrates how to find peaks in histograms.
4///
5/// This script generates a random number of gaussian peaks
6/// on top of a linear background.
7/// The position of the peaks is found via TSpectrum and injected
8/// as initial values of parameters to make a global fit.
9/// The background is computed and drawn on top of the original histogram.
10///
11/// This script can fit "peaks' heights" or "peaks' areas" (comment out
12/// or uncomment the line which defines `__PEAKS_C_FIT_AREAS__`).
13///
14/// To execute this example, do (in ROOT 5 or ROOT 6):
15///
16/// ~~~{.cpp}
17/// root > .x peaks.C (generate 10 peaks by default)
18/// root > .x peaks.C++ (use the compiler)
19/// root > .x peaks.C++(30) (generates 30 peaks)
20/// ~~~
21///
22/// To execute only the first part of the script (without fitting)
23/// specify a negative value for the number of peaks, eg
24///
25/// ~~~{.cpp}
26/// root > .x peaks.C(-20)
27/// ~~~
28///
29/// \macro_output
30/// \macro_image
31/// \macro_code
32///
33/// \author Rene Brun
34
35#include "TCanvas.h"
36#include "TMath.h"
37#include "TH1.h"
38#include "TF1.h"
39#include "TRandom.h"
40#include "TSpectrum.h"
41#include "TVirtualFitter.h"
42
43//
44// Comment out the line below, if you want "peaks' heights".
45// Uncomment the line below, if you want "peaks' areas".
46//
47// #define __PEAKS_C_FIT_AREAS__ 1 /* fit peaks' areas */
48
49Int_t npeaks = 30;
51{
52 Double_t result = par[0] + par[1] * x[0];
53 for (Int_t p = 0; p < npeaks; p++) {
54 Double_t norm = par[3 * p + 2]; // "height" or "area"
55 Double_t mean = par[3 * p + 3];
56 Double_t sigma = par[3 * p + 4];
57#if defined(__PEAKS_C_FIT_AREAS__)
58 norm /= sigma * (TMath::Sqrt(TMath::TwoPi())); // "area"
59#endif /* defined(__PEAKS_C_FIT_AREAS__) */
60 result += norm * TMath::Gaus(x[0], mean, sigma);
61 }
62 return result;
63}
64void peaks(Int_t np = 10)
65{
67 TH1F *h = new TH1F("h", "test", 500, 0, 1000);
68 // Generate n peaks at random
69 Double_t par[3000];
70 par[0] = 0.8;
71 par[1] = -0.6 / 1000;
72 Int_t p;
73 for (p = 0; p < npeaks; p++) {
74 par[3 * p + 2] = 1; // "height"
75 par[3 * p + 3] = 10 + gRandom->Rndm() * 980; // "mean"
76 par[3 * p + 4] = 3 + 2 * gRandom->Rndm(); // "sigma"
77#if defined(__PEAKS_C_FIT_AREAS__)
78 par[3 * p + 2] *= par[3 * p + 4] * (TMath::Sqrt(TMath::TwoPi())); // "area"
79#endif /* defined(__PEAKS_C_FIT_AREAS__) */
80 }
81 TF1 *f = new TF1("f", fpeaks, 0, 1000, 2 + 3 * npeaks);
82 f->SetNpx(1000);
83 f->SetParameters(par);
84 TCanvas *c1 = new TCanvas("c1", "c1", 10, 10, 1000, 900);
85 c1->Divide(1, 2);
86 c1->cd(1);
87 h->FillRandom("f", 200000);
88 h->Draw();
89 TH1F *h2 = (TH1F *)h->Clone("h2");
90 // Use TSpectrum to find the peak candidates
91 TSpectrum *s = new TSpectrum(2 * npeaks);
92 Int_t nfound = s->Search(h, 2, "", 0.10);
93 printf("Found %d candidate peaks to fit\n", nfound);
94 // Estimate background using TSpectrum::Background
95 TH1 *hb = s->Background(h, 20, "same");
96 if (hb)
97 c1->Update();
98 if (np < 0)
99 return;
100
101 // estimate linear background using a fitting method
102 c1->cd(2);
103 TF1 *fline = new TF1("fline", "pol1", 0, 1000);
104 h->Fit("fline", "qn");
105 // Loop on all found peaks. Eliminate peaks at the background level
106 par[0] = fline->GetParameter(0);
107 par[1] = fline->GetParameter(1);
108 npeaks = 0;
110 xpeaks = s->GetPositionX();
111 for (p = 0; p < nfound; p++) {
112 Double_t xp = xpeaks[p];
113 Int_t bin = h->GetXaxis()->FindBin(xp);
114 Double_t yp = h->GetBinContent(bin);
115 if (yp - TMath::Sqrt(yp) < fline->Eval(xp))
116 continue;
117 par[3 * npeaks + 2] = yp; // "height"
118 par[3 * npeaks + 3] = xp; // "mean"
119 par[3 * npeaks + 4] = 3; // "sigma"
120#if defined(__PEAKS_C_FIT_AREAS__)
121 par[3 * npeaks + 2] *= par[3 * npeaks + 4] * (TMath::Sqrt(TMath::TwoPi())); // "area"
122#endif /* defined(__PEAKS_C_FIT_AREAS__) */
123 npeaks++;
124 }
125 printf("Found %d useful peaks to fit\n", npeaks);
126 printf("Now fitting: Be patient\n");
127 TF1 *fit = new TF1("fit", fpeaks, 0, 1000, 2 + 3 * npeaks);
128 // We may have more than the default 25 parameters
129 TVirtualFitter::Fitter(h2, 10 + 3 * npeaks);
130 fit->SetParameters(par);
131 fit->SetNpx(1000);
132 h2->Fit("fit");
133}
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:233
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:645
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
Advanced Spectra Processing.
Definition TSpectrum.h:18
virtual TH1 * Background(const TH1 *hist, Int_t niter=20, Option_t *option="")
One-dimensional background estimation function.
virtual Int_t Search(const TH1 *hist, Double_t sigma=2, Option_t *option="", Double_t threshold=0.05)
One-dimensional peak search function.
Double_t * GetPositionX() const
Definition TSpectrum.h:58
static TVirtualFitter * Fitter(TObject *obj, Int_t maxpar=25)
Static function returning a pointer to the current fitter.
const Double_t sigma
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
fit(model, train_loader, val_loader, num_epochs, batch_size, optimizer, criterion, save_best, scheduler)
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculates a gaussian function with mean and sigma.
Definition TMath.cxx:471
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
constexpr Double_t TwoPi()
Definition TMath.h:44