Trouble importing TH1 to a RooPlot frame

Hi,

I’ve started to learn about RooFit and trying to use it but I already ran into some trouble I don’t seem to understand. The TH1 histogram I’m importing onto the RooPlot frame in form of a RooDataHist isn’t showing properly on the frame.


When it usually looks like this:

Here is the code I wrote:

     using namespace RooFit;
 
     RooRealVar Obs(  "Obs"  , "Obs"         , expected_value - dE, expected_value + dE );                    
     RooDataHist histo_data1("histo_data1", "histo_data1", Obs, RooFit::Import(*histo, kTRUE));
 
     RooPlot *ObsFrame = Obs.frame(RooFit::Title("Title"));
     histo_data1.plotOn(ObsFrame);
 
     std::cout << histo->GetName() << std::endl;
 
     TFile *newfile = new TFile(".../Z_ROOFIT_TEST.root", "UPDATE"); 
     newfile->mkdir(Form("Gate %i", index+1));
     newfile->cd(Form("Gate %i", index+1));
 
     ObsFrame->RooPlot::Draw("same");
     ObsFrame->Write();
     newfile->Close();

I can’t seem to find the difference I made in comparison to some documentation I found online.
Thanks in advance for an answer
Ludwig

Welcome to the ROOT Forum!
I’m sure @jonas can help you

Hi, welcome to the forum!

I think the problem is this line here:

RooRealVar Obs("Obs", "Obs", expected_value - dE, expected_value + dE);                    

What is dE here? It looks like an error on the expected value, but the lower and upper uncertainties is not what the RooRealVar constructor expects. You need to pass the definition range for the variable, which should probably match with the histogram limits:

RooRealVar Obs("Obs", "Obs",
               histo->GetXaxis()->GetXmin(),
               histo->GetXaxis()->GetXmax());                    

Then it should work. Probably, your dE was very small, so the definition range was very narrow and all the values clipped to the first bin like you saw in the plot.

Cheers,
Jonas

edit: fixed copy-paste error, using GetXmin() for lower limit

Hi again, sorry for the late response.
First I want to thank you for your response.

I tried your approach but the values still clipps to the first bin ( I changed the first GetXmax into a GetXmin too ). It also appears that not all the values are stored in this bin and the width of the one bin doesn’t match the width of the bins of the original historgram (in the original it’s 10 units per bin; in the rooplot it has 350 units per bin). Do you have another idea eventually? :confused:

Here is by the definition of my function that I use to create the TFile (should have posted it earlier…)

void FitHistogram_ROOFIT(TH1 *histo, const double dE, const double expected_value, const uint8_t index )

Regards
Ludwig

Hello again,

I found the solution for my problem. I was using the Rootfile Viewer in VSCode to look at the RooFit frames (without a TCanvas that was defined in my code). This lead to the peculiar problem. Looking at the TBrowser, the data looks completely fine. So does it in the Rootfile Viewer with a defined TCanvas.

Regards
Ludwig

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