I am trying to make a fit of five spectra. I create a kind of array that will contain each espectrum with:
TH1 *h_hLcPt[5]
and then I fill them with
h_hLcPt[0] = new TH1F("h_hLcPt[0]","1 spectrum",125, 2.15, 2.65);
and so on for the rest 4 … Later I use a loop to access to every index, but fist as I want them in a canvas to see the five of them:
TCanvas *cx=new TCanvas("cx","hLcPt1,2,3,4,5 plot",600,900);
cx->Divide(2,3);
Int_t j;
Then the loop
for (j=0;j<5;j++) {
cx->cd(j+1);
h_hLcPt[j]->SetLineColor(8);
h_hLcPt[j]->Draw();
And plot the spectra. So, now I acces to every index in a for a nested loop. I started with the j=0
if (j==0){
par = fillArray(6);// Array for the parameters of the fit i.e the sum of the functions
//Fit function
TF1 *fitFcn1 = new TF1("fitFcn1",fitFunction_GN_GNBkg,2.2,2.4,6);
fitFcn1->SetParameters(96700,2.29,0.006,93600,2.27,0.5);
fitFcn1->SetParNames("Norm1","Mean1","Sigma1","Norm2","Mean2", "Sigma2");
fitFcn1->SetLineColor(kRed);
h_hLcPtClones[0]->Fit("fitFcn1","R");
//As I want to see the bakgraund function (gauss normalized)
//and the signal function(gauss normalized)
TF1 *backFcn1 = new TF1("backFcn1", GausBkg1,2.22,2.4,3);
backFcn1->SetLineColor(7);//aqua
backFcn1->SetParameters(93600,2.27,0.5);
TF1 *signalFcn1 = new TF1("signalFcn1",gaussNFunction, 2.25,2.32,3);
signalFcn1->SetLineColor(1); //kBlue//6 hot pink//5 yellow
fitFcn1->GetParameters(par);
//And add it to the plot
cx->cd(1);
backFcn1->Draw("same");
signalFcn1->Draw("same");
}//Close j=0
Then I run the code and the result are the backgound a flat line over the x axis and the signal is a flat line at the level of the plot. BTW, the definition of the functions used are
- Fit functions
Double_t gaussNFunction(Double_t *x,Double_t *par){
return ( par[0]/(TMath::Sqrt(2.*TMath::Pi())*par[2]) )*TMath::Exp(
-(x[0]-par[1])*(x[0]-par[1])/(2.*par[2]*par[2]) );
}
//The same
Double_t GausBkg1(Double_t *x, Double_t *par) {
return ( par[0]/(TMath::Sqrt(2.*TMath::Pi())*par[2]) )*TMath::Exp(
-(x[0]-par[1])*(x[0]-par[1])/(2.*par[2]*par[2]) );
}
//Fit
Double_t fitFunction_GN_GNBkg(Double_t *x, Double_t *par) {
return gaussNFunction(x,par) - GausBkg1(x,&par[3]) ;
}
And the parameters
Double_t * fillArray(Int_t n) {
// create a double array of length n, fill it with random numbers return a pointer to it
Double_t *x = new Double_t[n];
//TRandom generator(0);
for(Int_t i=0; i<n; i++) x[i] = 0;/*generator.Rndm();*/
return x;
}
Double_t *par;// with *
I even used the fit panel to get the initial parameters for the fit. But does not work.