# Fitting of mutiple peaks in the case of unknown number of peaks

ROOT Version: 6.14
Platform: linux
Compiler: Not Provided

How the following code can be modified for gaussian fitting of mutiple peaks in the case of unknown number of peaks in histogram generated from .tex file (for example: 1.tex)?

``````TF1 *fpol;
Int_t npol;

const Int_t npeaks = 5;
Double_t fpeaks(Double_t *x, Double_t *par) {
Double_t result = fpol->EvalPar(x,par);
for (Int_t p=0;p<npeaks;p++) {
Double_t norm  = par[3*p+npol];
Double_t mean  = par[3*p+npol+1];
Double_t sigma = par[3*p+npol+2];
result += norm*TMath::Gaus(x[0],mean,sigma);
}
return result;
}
void fitg() {
TTree *t = new TTree("t", "my tree");
TH1D *h = new TH1D("h", "Histo", , ., .);
//fit background with a polynomial
fpol = new TF1("fpol","pol3",60,550);
npol = fpol->GetNpar();
h->Fit("fpol","0");
Double_t xpeak[npeaks] = {200, , , , };
Double_t sigma[npeaks] = {2, , , , };
Double_t amp[npeaks]   = {100,100,100,100,100};
Double_t par[100];
for (Int_t ip=0;ip<npol;ip++) par[ip] = fpol->GetParameter(ip);
TF1 *fp = new TF1("fp",fpeaks,60,550,npol+3*npeaks);
for (Int_t i=0;i<npeaks;i++) {
fp->SetParameter(3*i+npol,1000*amp[i]);
fp->SetParameter(3*i+npol+1, xpeak[i]);
fp->SetParLimits(3*i+npol+1, xpeak[i]-5,xpeak[i]+5);
fp->SetParameter(3*i+npol+2, sigma[i]);
fp->SetParLimits(3*i+npol+2, 0.5,3*sigma[i]);
}
fp->SetNpx(5000);
TVirtualFitter::SetMaxIterations(10000);
h->Fit(fp,"rw");

}
``````