Hey,
I am trying to fit a quadratic background and Gaussian to a plot of J/Psi masses. I have been using FittingDemo.C as an example to copy. However I get the warning that Warning in : Fit data is empty and not sure how to rectify this.
My plot as it currently looks is attached and below is my code:
#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "TTree.h"
#include "TFile.h"
#include "TLorentzVector.h"
#include "TH1.h"
#include "TF1.h"
#include "TLegend.h"
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <math.h>
// use this order for safety on library loading
using namespace RooFit ;
using namespace RooStats ;
using namespace std;
#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]*exp(pow((-0.5*(x[0]-par[1])/par[2]),2));
}
//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_version2");
if (!h1) {std::cout << "Error: c30 NOT found!" << std::endl;}
h1->Draw();
//#################################################################
// FITTING
//#################################################################
TH1D *h2 = new TH1D("h2","One DiLepton and One Photon bin: DiLepton Masses" , 100, 2900, 3300);
h2->SetMarkerStyle(21);
h2->SetMarkerSize(0.8);
h2->SetStats(0);
TF1 *FitFcn = new TF1("FitFcn", FitFunction, 2950.0, 3250.0, 6.0);
FitFcn->SetNpx(500);
FitFcn->SetLineWidth(4);
FitFcn->SetLineColor(kMagenta);
FitFcn->SetParameters(1,1,1,1,1,1);
h2->Fit("FitFcn", "QMWR");
// second try: set start values for some parameters
FitFcn->SetParameter(100.0,200.0); // width
FitFcn->SetParameter(40.0,100.0); // peak
h2->Fit("FitFcn","V+","ep");
// improve the picture:
TF1 *backFcn = new TF1("backFcn",background,0,-10,10);
backFcn->SetLineColor(kRed);
TF1 *signalFcn = new TF1("signalFcn",Gaussian1,1.001,2.5);
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);
legend->AddEntry(h2,"Data","lpe");
legend->AddEntry(backFcn,"Background fit","l");
legend->AddEntry(signalFcn,"Signal fit","l");
legend->AddEntry(FitFcn,"Global Fit","l");
legend->Draw()
}// CLOSING BRACE