Warning in <Fit>: Fit data is empty with a non empty histogram

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

Correct:

TF1 *FitFcn = new TF1("FitFcn", FitFunction, 2950.0, 3250.0, 6);
TF1 *backFcn = new TF1("backFcn", background, 2950.0, 3250.0, 3);
TF1 *signalFcn = new TF1("signalFcn", Gaussian1, 2950.0, 3250.0, 3);

See also: TF1::SetParameter

Then, inspect your “One_J_One_P_version2” histogram. In particular see if the “bin errors” are non-zero. If they are zero, try h2->Sumw2(kFALSE);. You can also try to add “W”: h2->Fit("FitFcn", "WV+", "ep");.

BTW. When you post “output” or “source code” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Thanks! I have a blue line appearing hugging the x axis, but at least it’s appearing on the plot. I will play around with the numbers and see what happens.Thank you.

I did h2->Print(); and it says that there is zero entries. Is this why the data isn’t being fitted?

I don’t know why you say that you get “zero entries” as the picture in your first post here clearly shows 87730.

It’s not about “bin contents” but about “bin errors” so try h2->Print("all"); or try for a single particular bin h2->GetBinError(h2->FindFixBin(3100.)) (I guess you should get something like 15 for this bin, which is around sqrt(230), which I get from the picture in your first post here).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.