Ratio of two Tprofile histograms

I am trying to get the ratio of 2 TProfile histograms. Can someone help me with this? I have n idea about the TRatioplot. I tried dividing the two histograms but I obtain a blank histogram with no entries. I have tried various other process that I found online but I don’t get any results.

n=2, S = 0 + S = 1.root|attachment (4.3 KB)
n=3, S = 0 + S = 1.root|attachment (4.3 KB)
Ratio.C (1.2 KB)

Welcome to the ROOT forum.

The histograms A and B are empty:

void Ratio()
   TFile* fa = new TFile("n=2, S = 0 + S = 1.root", "READ");
   TProfile* A = (TProfile*) fa->Get("hprof5");

   TFile* fb = new TFile("n=3, S = 0 + S = 1.root", "READ");
   TProfile* B = (TProfile*) fb->Get("hprof6");

   printf("%f %f\n",A->GetEntries(),B->GetEntries());


root [0] .x Ratio.C
0.000000 0.000000
root [1] 

Actually I have 4 different histograms. In A histogram I combined the first 2 histograms and in B the other two. Then I want to find the ratio between A and B histogram.

Ok, but A and B are empty. Try to plot them.

The thing is I need to put A and B in one Histogram. And C and D in another single histogram. Then find the ratio between these two. So what do you suggest I do?

Using S.C I was combining A and B into one root file. And C and D into another root file. Then I was trying to plot the ratio using Ratio.C. But as you said A and B are empty.

Yes, so your initial macro cannot work as it relaies on A and B, which are empty.

Can you check S.C please? Like why the entries are empty for A and B?

You create hprof6:

   TProfile *hprof6 = new TProfile("hprof6", "Eccentricity vs b for n = 3 for Shadowing = 0 and 1 ", 30, 0, 15);

but you never fill it.

I just created hprof6 to plot A and B in the same histogram. Is there a better way to do it? Like I have all 4 histograms in the root files. I need to combine the 2_S0 with 2_s1 and 3_S0 with 3_S1. And after that plot the ratio between them.

hprof6 is just an empt frame you are using to draw the axis. You draw C and D on top of it (or A and B) . But that’s simply graphics when you save hprof6 you save an empty frame.

Okay I understood your point. So what do you suggest? How do I solve this problem?

Use the histogram having data. Not an empty frame.

1 Like

Okay so I decided to remove the hprof6(empty histogram). So how do I extract the histogram after Drawing A and B in the same canvas by using Draw(“SAMES”);?

I am not sure I understand what you mean by “extract” … you have two histogram A and B … you plot them. Fine that’s graphics… Use them directly in TRatioPlot for instance. It depends what you want to do.

1 Like

Okay so I have 4 histograms A B C and D. I want A+B as one histogram, like in one canvas. And I want C+D as one histogram, in one canvas. Lets say A+B = F histogram. And C+D = G histogram. Now I want the ratio between F and G histograms.

auto F = new TProfile();
1 Like

Processing S.C…
Error in TProfileHelper::Add: Attempt to add profiles with different number of bins

I am getting this error. But all the TProfiles that I have created are by using the following command:
TProfile *hprof1 = new TProfile(“hprof1”, “Eccentricity vs b for n = 2 for S = 0”, 40, 0, 20);

It seems your profile histo do not have the same number of bins. pease check.

1 Like

Okay. I have reached my limit for replies.

TProfile *hprof1 = new TProfile(“hprof1”, “Eccentricity vs b for n = 2 for S = 0”, 40, 0, 20);
TProfile *hprof2 = new TProfile(“hprof2”, “Eccentricity vs b for n =2 for S = 1”, 40, 0, 20);
TProfile *sum = new TProfile(“sum”, “sum”, 40, 0, 20);
This solution that you gave it works. Thank you very much and I really appreciate it.

But is there a way to change the color of the entries of hprof1 and hprof2 in the sum histogram. Because in sum all the entries are in same color. So is it possible to distinguish between them?