Hey,
I am opening a root file, getting a histogram and then fitting it. I then want to save the histogram to a new root file. However, when I save the histogram it seems to only save the global fit, rather than the global, background and signal fits.
Here is my script below:
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TChain.h>
//Declare a user defined functions for fitting:
// Quadratic background function
Double_t background(Double_t *x, Double_t *par) {
return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
}
//Gaussian
Double_t Gaussian1(Double_t *x, Double_t *par){
return par[0]/sqrt(2*M_PI)/(par[2])*(par[3])*exp(-0.5*pow(((x[0]-par[1])/par[2]),2)); // p3 is p0 ///par 3 is new!!! //Par[2] here is the bin width
}
//Sum of Background and Guassian
Double_t FitFunction(Double_t *x, Double_t *par){
return background(x,par) + Gaussian1(x,&par[3]);
}
void Fits()
{ //OPENING BRACE
//#################################################################
// OPEN UP ROOT FILE AND OBTAIN HISTOGRAM
//#################################################################
TFile *file = new TFile("My_Saved_Plots_user_jwalder.root", "READ")
file->ls();
TH1D *h1 = (TH1D*)file->Get("One_J_One_P");
//#################################################################
// FITTING
//#################################################################
h1->Rebin(3);
//Fit Function for h1
TF1 *FitFcn = new TF1("FitFcn", FitFunction, 2700.0, 3500.0, 7);
FitFcn->SetNpx(500);
FitFcn->SetLineWidth(4);
FitFcn->SetLineColor(kMagenta);
// second try: set start values for some parameters
FitFcn->SetParameter(5,50.0); // width //index 5 of the parameters
FitFcn->SetParameter(4, 3100.0 ); // peak //index 4 of params
FitFcn->SetParameter(3, 19000); //some other normalization
FitFcn->FixParameter(6, h1->GetBinWidth(1)); // bin width
FitFcn->SetParameter(0, 200); //index 0, constant term in background
FitFcn->SetParameter(1, -0.05); //index 1, coeff. x term in background
FitFcn->SetParameter(2, 0); //index 2, coeff. x^2 term in background
FitFcn->FixParameter(2,0);
//FitFcn->FixParameter(4,3100.0);
h1->Fit("FitFcn"," " , "ep");
h1->Fit("FitFcn"," " , "ep");
cout << h1->GetBinWidth(1) << " Bin Width" << std::endl; //MeV
// improve the picture:
TF1 *backFcn = new TF1("backFcn",background,2700.0, 3500.0, 3);
backFcn->SetLineColor(kRed);
TF1 *signalFcn = new TF1("signalFcn",Gaussian1, 2700.0 ,3500.0 , 4);
signalFcn->SetLineColor(kBlue);
signalFcn->SetNpx(500);
Double_t par[7];
// 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.10,0.70,0.38,0.90);
legend->SetTextFont(72);
legend->SetTextSize(0.04);
legend->AddEntry(h1,"Data","lpe");
legend->AddEntry(backFcn,"Background fit","l");
legend->AddEntry(signalFcn,"Signal fit","l");
legend->AddEntry(FitFcn,"Global Fit","l");
legend->Draw();
gStyle->SetOptStat(1111111);
gStyle->SetOptFit(1111);
TFile *file2 = new TFile("Fit_Plots.root", "RECREATE");
file2->cd();
h1->Write();
}// CLOSING BRACE