InputArguments -- RooBinning::binEdges ERROR

Dear experts,

I get the error when running the following code, which is obtained by modifying the tutorial macro rf102_dataimport.C

Please tell me what’s wrong with the code.

THANKS

[#0] ERROR:InputArguments -- RooBinning::binEdges ERROR: bin number must be in range (0,0)
[#0] ERROR:InputArguments -- RooBinning::binEdges ERROR: bin number must be in range (0,0)
[#0] ERROR:InputArguments -- RooAbsRealLValue::frame(x) ERROR: empty fit range, must specify plot range
[#0] ERROR:InputArguments -- RooDataHist::dh:plotOn: frame is null
TH1* makeTH1() ;

void gaus()
{
  TH1* hh = makeTH1() ;
  RooRealVar x("x","x",-1,-8.,8.) ;
  x.getBinning(nullptr).Print("");
  RooDataHist dh("dh","dh",x,Import(*hh)) ;
  RooPlot* frame = x.frame() ;
  dh.plotOn(frame) ;
  RooRealVar mean("mean","mean",0,-5,5) ;
  RooRealVar sigma("sigma","sigma",1,0.1,4) ;
  RooGaussian gauss("gauss","gauss",x,mean,sigma) ;
  gauss.fitTo(dh);
  gauss.plotOn(frame) ;
  frame->Draw() ;
}

TH1* makeTH1()
{
  TFile f1("dedx_p.root");
  TH1F *hh=f1.Get("dedx_chi_p_mc_2009_0.20");;
  return hh ;
}

Hi @YULianzhou! The problem in you code is that since you create the TFile on the stack, it gets deleted when the makeTH1 function is finished. In addition, all the objects it owns get deleted too! Since you Get the histogram from the file, it will be owned by the file and hence deleted at the end of makeTH1.

To avoid this, you can reset the directory for the histogram with SetDirectory(0). Like this, it doesn’t get deleted for you anymore (so remember to delete it at some point if you care about memory leaks).

  TFile f1("dedx_p.root");
  TH1F *hh=f1.Get("dedx_chi_p_mc_2009_0.20");
  hh->SetDirectory(0);
  return hh ;

You can read more about object ownership in Chapter 10 of the ROOT users guide.

I hope this works for your! If it doesn’t of if you have any other questions, please let me know.

Jonas

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