# Fitting Peaks above a Function

Hi, I was wondering how you would go about doing a fit peak above a line that is called by a function. The purpose of this is to find the area of the upper portion of a peak in a histogram while ignoring the rest of the histogram. Thanks.

I am trying to use the example from http://root.cern.ch/root/html/examples/FittingDemo.C.html to help.

Because the histogram I am working with has many peaks, the thing I need help with is how do I set the range on the x-axis so I can find the peak for just one particular peak.

Using this script:

[code]#include “TH1.h”
#include “TF1.h”
#include “TLegend.h”
#include “TCanvas.h”

Double_t background(Double_t *x, Double_t *par) {
return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
}

// Lorenzian Peak function
Double_t lorentzianPeak(Double_t x, Double_t par) {
return (0.5
par[0]par[1]/TMath::Pi()) /
TMath::Max( 1.e-10,(x[0]-par[2])
(x[0]-par[2]) + .25
par[1]*par[1]);
}

// Sum of background and peak function
Double_t fitFunction(Double_t *x, Double_t *par) {
return background(x,par) + lorentzianPeak(x,&par[3]);
}

void FittingDemo() {
//Bevington Exercise by Peter Malzacher, modified by Rene Brun

TFile *f1 = TFile::Open(“his00048.root”);
TFolder folder1 = (TFolder) f1->Get(“histos”);
TH1F h1 = (TH1F) folder1->FindObjectAny(“I0C2_time”);

// create a TF1 with the range from 0 to 3 and 6 parameters
TF1 *fitFcn = new TF1(“fitFcn”,fitFunction,0,3,6);
fitFcn->SetNpx(500);
fitFcn->SetLineWidth(4);
fitFcn->SetLineColor(kMagenta);

// second try: set start values for some parameters
fitFcn->SetParameter(4,0.2);
fitFcn->SetParameter(5,1);

h1->Fit(“fitFcn”,“V+”,“ep”);

// improve the picture:
TF1 *backFcn = new TF1(“backFcn”,background,0,3,3);
backFcn->SetLineColor(kRed);
TF1 *signalFcn = new TF1(“signalFcn”,lorentzianPeak,0,3,3);
signalFcn->SetLineColor(kBlue);
signalFcn->SetNpx(500);
Double_t par[6];

// writes the fit results into the par array
fitFcn->GetParameters(par);

backFcn->SetParameters(par);
backFcn->Draw(“same”);

signalFcn->SetParameters(&par[3]);
signalFcn->Draw(“same”);

// draw the legend
TLegend *legend=new TLegend(0.6,0.65,0.88,0.85);
legend->SetTextFont(72);
legend->SetTextSize(0.04);