Hi everyone !
I want to fit a histogram with a gaussian function. I specify the range in which I want the histogram to be fit, however The fit function is done and plot over the whole histogram. Could anyone help me solve this problem please ? Here is a copy of my code.
thanks a lot !
void histo_maker(){
TCanvas *fenetre = new TCanvas(“Angle 121”, “Etude Angle”, 700 , 500) ;
//fenetre->SetLogy();
fenetre->Draw (“C”);
gStyle->SetOptStat (1); // Axis normalisation
ifstream histo("data_voie4.txt", ios::in); // name of the file containing the experimental datas
float angle_detecteur = 121; // Angle of the detector
TH1* h1 = new TH1F("Histo1","histogramme channel 4", 33000, 0, 33000);
int counter = 0;
int i=0; // counts the number of datas
cout << "ready to enter the loop" << endl;
while (!histo.eof()){
i+=1;
int x;
histo>>x;
h1->Fill(x,i); // fill the bin i with the number x of counts
}
cout << " loop done " << endl;
histo.close();
int group_bins=1; // Groups the bins
int N= 500000; // Number of datas in the histogram
int Nbins = N/group_bins; // Number of bins
h1->Rebin(group_bins); // Histogram done with the new number of bins
// Axis parametrization
h1->GetXaxis ()->SetTitle ("bins");
h1->GetYaxis ()->SetTitle ("hits");
cout << "Histogram done" << endl;
h1->Draw("C");
h1->Draw();
// Area of interest
int xmin = 0;
int xmax = 9000;
// Shows the interval [xmin,xmax]
h1->GetXaxis ()->SetRange(xmin/group_bins,xmax/group_bins);
//fitting with a gaussian function a certain part of the graph
TF1 *fit = new TF1 ("gaussian","[2]*TMath::Gaus(x,[0],[1])", 7200,7800);
fit->SetParameters(7400,250,200e6);
fit->SetParName(0, "mean");
fit->SetParName(1, "std");
fit->SetParName(2, "amplitude");
gStyle->SetOptFit(1); // Shows the fitting parameters in the Canvas
fit->Draw();
h1->Fit(fit);
double err_mean = fit->GetParError(0);
double err_std = fit->GetParError(1);
double err_amp = fit->GetParError(2);
}