Ratio plot axis titles root 6.38

This code:

TRatioPlot* rp = new TRatioPlot(hData,hStack);
rp->Draw();
rp->GetUpperPad()->cd();

((THStack*)rp->GetUpperRefObject())->GetXaxis()->SetTitle(hData->GetXaxis()->GetTitle());
((THStack*)rp->GetUpperRefObject())->GetYaxis()->SetTitle(hData->GetYaxis()->GetTitle());

worked fine in root 6.36 but fails at runtime in root 6.38, giving a bus error when I try to do GetXaxis(). Can anyone tell me the correct way to set axis titles of a ratio plot in 6.38?

@Danilo Looks like an incompatible change in ROOT.
The GetUpperRefObject (correctly) returns hData in ROOT 6.38 and (incorrectly) hStack in ROOT 6.36 (and ROOT 6.34).

@bethlong06 Try with (it should work with any ROOT version):
TRatioPlot *rp = new TRatioPlot(hStack, hData);

In fact TRatioPlot *rp = new TRatioPlot(hStack,hData); solves the problem

The ctor hData,hStack was not in the initial version of the TRatioPlot class in 2016. It was introduced two years ago.

Then a fix was done beginning of september last year

ROOT 6.36.08 (an LTS release branch) is still missing this fix.

I made a complete example. With master (3.39) it fails. I will try with 6.36.

void TRatioPlotStack()
{
   auto C = new TCanvas("C", "A ratio example");

   auto h1 = new TH1D("h1", "TRatioPlot Example; x; y", 50, 0, 10);
   auto h2 = new TH1D("h2", "h2", 50, 0, 10);
   auto h3 = new TH1D("h3", "h3", 50, 0, 10);

  auto f1 = new TF1("f1", "exp(- x/[0] )");
  f1->SetParameter(0, 3);

  for(int ii=0; ii<h1->GetXaxis()->GetNbins(); ii++) h1->SetBinContent(ii+1,150);
  h2->FillRandom("f1", 2000);
  h3->FillRandom("f1", 2000);

  THStack* st = new THStack("st","");
  st->Add(h2);
  st->Add(h3);

  auto rp = new TRatioPlot(h1, st); // Fail
//   auto rp = new TRatioPlot(st, h1); // Work
   rp->Draw();

   rp->GetUpperPad()->cd();
   ((THStack*)rp->GetUpperRefObject())->GetXaxis()->SetTitle(h1->GetXaxis()->GetTitle());
}

Indeed the fix: Fix the (TH1*, THStack*) ctor (#19824) · root-project/root@e520baf · GitHub make the macro I posted crash..
This fix was done to fix this issue reported by @bethlong06 .

Investigating…

From what I understand, for the “TRatioPlot(first, second)”, the “GetUpperRefObject” should always return the “first” (and NOT the “second”, regardless of the types of the “first” and “second”).

I am on it

If you do:

((TH1D*)rp->GetUpperRefObject())->GetXaxis()->SetTitle(hData->GetXaxis()->GetTitle());
((TH1D*)rp->GetUpperRefObject())->GetYaxis()->SetTitle(hData->GetYaxis()->GetTitle());  

It does not crash

For the time being, it depends on which ROOT version you use with “TRatioPlot(hData, hStack)”. :wink:

Right after “rp->Draw();”, add:
rp->GetUpperRefObject()->Print();

PR here: Backport ratioplot fixes by couet · Pull Request #21247 · root-project/root · GitHub

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