Error Creating Ratio Plot

Dear experts,
After spending a lot of time trying to create a ration plot from my histograms and getting the following error:
Error in TRatioPlot::GetUpperRefObject: No upper ref object of TH1 or THStack type found

I decided to first go through the tutorial: ratioplot1.C (ROOT: tutorials/hist/ratioplot1.C File Reference).
However, I received the same error message.
Please let me know where I am going wrong.
As always, thank you for the help.

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

What operating system and what ROOT version?
What do you get when you run: root -l ratioplot1.C

Hi @Wile_E_Coyote
Thank you for your quick response.
I am using RHEL 7.9 and my root version is 6.26/06.
I successfully ran “root -l ratioplot1.C” and I could see the plots. But when I try to implement ratio plots in a similar way for my other histograms, I get the error.

Attach your macro for inspection.

Hi @Wile_E_Coyote ,
I guessed I figured out what the problem was. After I saved the created canvases in a root file, I would close the file. If I comment out the command to close the file :
everything works as expected. However, my question now is how to close the file without any problems?
Thanks again

Again, it would be nice to see your macro to inspect it.

Hi @couet
How can I upload my file? can I just copy and paste it here?
It is really long (593 lines) and if I remove similar sections like other histograms then the problem seems to go away. However, I could not find the problem by removing the other histograms and parts of the code one by one and running it. It seems like it happens when I have many histograms and I try to save it to a root file and then close the file.
Please let me know how I can share it,

yes, you can post it here. copy/paste or click the upload icon in the text window where you type your messages.

Hi @couet
Here are the code and the header file I use. (33.7 KB)
MCResultComparator.h (12.0 KB)

A brief description about the code:
The code reads in two MC samples and creates several plots. Then, it normalizes the plots related to second MC sample. Using the two sets of plots , it creates ratio plots and saves to the file (which is located in a directory made by the code with the name of two samples). At the very end, it closes the file.
Thanks a lot for your help.

How do you execute your code ?

Instead of “some_histo->Draw(...);” use “some_histo->DrawCopy(...);”.

BTW. Instead of “some_file->Close();” use “delete some_file;”;

I run it as in the following:
MCResultComparator MCRC(“path to first MC”,“path to second MC”,“First Decay Mode”,“Second Decay Mode”)

@Wile_E_Coyote ,
I used DrawCopy() for histograms only, and also used “delete outputfile” instead of “outputfile->Close()” but I still get the same error:
Error in TRatioPlot::GetUpperRefObject: No upper ref object of TH1 or THStack type found
also immediately “segmentation violation”

Change how you manage the output file (do not use “DrawCopy” as it will not be needed).

Instead of “OutputRootF = new TFile(...);”, put:

gROOT->cd(); // newly created histograms should go here

Then, in the end of your routine:

OutputRootF = TFile::Open(OutputFileName.c_str(), "RECREATE");
// ...
delete OutputRootF;

Hi @Wile_E_Coyote ,
Thank you very much for all your help.
I made the changes you mentioned and my code is working perfectly.
Thank you again

The error message suggests that there might be an issue with how you’re specifying the input histograms or THStacks when creating the ratio plot. Double-check your data and input parameters for correctness. If the issue persists, consult ROOT’s documentation or community support for more specific help.