Ratio plot

Hi, I used this example root.cern.ch/root/html/tutorial … lot.C.html to draw 2 th1f and their ratio. The macro is in the attachment; Unfortunatly…there are some problems because:

  1. It looks like to me that the ratio isn’t right in the ranges, because in the range 20-30 Km I think it must be greather than the values plotted by root…
    2)i t doesn’t the plot the legend:
  2. it doesn’t write the numbers of y axis of first graph
  3. the number “15” of y axis of the second graph is cutted.

thanks
hfirstint.c (4.65 KB)

I can have a look at your problem but I just want to mention that we have developed a new class call TRatioPlot to deal with all these Issues. It has just been put in the ROOT master:

root.cern.ch/doc/master/classTRatioPlot.html

I am missing your ROOT files in order to run your macro.

hi couet, i copied this code

void ratioplot1() { gStyle->SetOptStat(0); auto c1 = new TCanvas("c1", "A ratio example"); auto h1 = new TH1D("h1", "h1", 50, 0, 10); auto h2 = new TH1D("h2", "h2", 50, 0, 10); auto f1 = new TF1("f1", "exp(- x/[0] )"); f1->SetParameter(0, 3); h1->FillRandom("f1", 1900); h2->FillRandom("f1", 2000); h1->Sumw2(); h2->Scale(1.9 / 2.); h1->GetXaxis()->SetTitle("x"); h1->GetYaxis()->SetTitle("y"); auto rp = new TRatioPlot(h1, h2); c1->SetTicks(0, 1); rp->Draw(); c1->Update(); }

to try the macro, but when I compile I’ve this error:

mybe, doesn’t it work using ROOT 5.34.36?

EDIT:
Here the link to download the root files

drive.google.com/file/d/0B9Me0x … sp=sharing
drive.google.com/file/d/0B9Me0x … sp=sharing

as I said the TRatioPLot class is available only in ROOT 6.08… you are using ROOT 5 …

I will take our files.

[quote=“couet”]as I said the TRatioPLot class is available only in ROOT 6.08… you are using ROOT 5 …

I will take our files.[/quote]
Unfortunatly, using Windows, I can’t download ROOT 6.08…

In your macro the variable “hene” is not declared and not initialized … what is it supposed to be ?

Hi couet…copying the code by other my macro, i forgot to change the name from “hene” to hfirstint"…but…it is just for the control if there is the histogram in the root file…it doesn’t give problem iduring compiling

#include "TFile.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TGaxis.h"
#include "TRandom.h"
#include "TLegend.h"
#include "TPaveStats.h"
#include "TGraph.h"

void hfirstint()
{
   gROOT->SetBatch(1);

   TFile *g = TFile::Open("cors_plot-gammatot.root");
   if (g == 0) {
      // if we cannot open the file, print an error message and return immediatly
      printf("Error: cannot open cors_plot.root!\n");
      return;
   }

   TFile *p = TFile::Open("cors_plot-prottot.root");
   if (p == 0) {
      // if we cannot open the file, print an error message and return immediatly
      printf("Error: cannot open cors_plot.root!\n");
      return;
   }

   TH1F *hfirstintg = (TH1F *)g->Get("hfirstint");
   if (hfirstintg == 0) {
      printf("Error getting an histogram from the file!\n");
      return;
   }

   TH1F *hfirstintp = (TH1F *)p->Get("hfirstint");
   if (hfirstintp == 0) {
      printf("Error getting an histogram from the file!\n");
      return;
   }

   float offx=1.2;
   float offy=1;
   float marg=0.15;
   TCanvas *c2 = new TCanvas("c2","hists with different scales",1280,1024);
   TPad *pad1  = new TPad("pad1", "pad1", 0, 0.3, 1, 1.0);
   pad1->SetBottomMargin(0); // Upper and lower plot are joined
   pad1->SetGridx();         // Vertical grid
   pad1->Draw();             // Draw the upper pad: pad1
   pad1->cd();               // pad1 becomes the current pad

   hfirstintg->SetLineColor(kBlack);
   hfirstintg->SetTitle("Quota di prima interazione dei #gamma e #font[12]{p} primari");
   hfirstintg->GetXaxis()->SetTitle("Altezza (Km)");
   hfirstintg->GetYaxis()->SetTitle("N_{part}");
   hfirstintg->GetYaxis()->SetTitleOffset(offy);
   hfirstintg->SetMinimum(0.001);
   c2->SetLeftMargin(marg);
   hfirstintg->GetYaxis()->SetTitleSize(20);
   hfirstintg->GetYaxis()->SetTitleFont(43);
   hfirstintg->GetYaxis()->SetTitleOffset(1.55);
   hfirstintg->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
   hfirstintg->GetYaxis()->SetLabelSize(15);
   hfirstintg->Draw();
   hfirstintp->SetLineColor(kBlue);
   hfirstintp->Draw("sames");
   gPad->Update();

   // Define the ratio plot
   TLegend *leg = new TLegend(0.5,0.67,0.6,0.88,NULL,"brNDC");
   leg->AddEntry(hfirstintg, "#gamma", "l");
   leg->AddEntry(hfirstintp, "#font[12]{p}", "l");
   leg->Draw();

   c2->cd();          // Go back to the main canvas before defining pad2
   TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3);
   pad2->SetTopMargin(0);
   pad2->SetBottomMargin(0.2);
   pad2->SetGridx(); // vertical grid
   pad2->Draw();
   pad2->cd();       // pad2 becomes the current pad
   c2->Update();

   TPaveStats *stats1 = (TPaveStats*)hfirstintg->GetListOfFunctions()->FindObject("stats");
   TPaveStats *stats2 = (TPaveStats*)hfirstintp->GetListOfFunctions()->FindObject("stats");
   stats1->SetTextColor(kBlack);
   stats2->SetTextColor(kBlue);
   stats1->SetX1NDC(0.80); stats1->SetX2NDC(0.98);
   stats1->SetY1NDC(0.79); stats1->SetY2NDC(0.94);
   stats2->SetX1NDC(0.80); stats2->SetX2NDC(0.98);
   stats2->SetY1NDC(0.62); stats2->SetY2NDC(0.77);
   gPad->Modified();
   gPad->Update();

   TH1F *h3 = (TH1F*)hfirstintg->Clone("h3");
   h3->SetLineColor(kBlack);
   h3->SetMinimum(0);  // Define Y ..
   h3->SetMaximum(15.); // .. range
   h3->Sumw2();
   h3->SetStats(0);      // No statistics on lower plot
   h3->Divide(hfirstintp);
   h3->SetMarkerStyle(21);
   h3->SetMaximum(15.5);
   h3->SetTitle(""); // Remove the ratio title
   h3->Draw("ep");       // Draw the ratio plot

   // Y axis ratio plot settings
   h3->GetYaxis()->SetTitle("h_{#gamma}/h_{p} ");
   h3->GetYaxis()->SetNdivisions(505);
   h3->GetYaxis()->SetTitleSize(20);
   h3->GetYaxis()->SetTitleFont(43);
   h3->GetYaxis()->SetTitleOffset(1.55);
   h3->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
   h3->GetYaxis()->SetLabelSize(15);

   // X axis ratio plot settings
   h3->GetXaxis()->SetTitleSize(20);
   h3->GetXaxis()->SetTitleFont(43);
   h3->GetXaxis()->SetTitleOffset(4.);
   h3->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
   h3->GetXaxis()->SetLabelSize(15);

   c2->Print("c2.pdf");
}

Hi couet thanks! other question please… i wrote

h3->SetMarkerColor(kRed);

to have the ratio plot in red color…but i see there is something in black…what is it?

h3 is plotted with errors bars. The markers and the bars can have different colors.
To have all red you should do:

h3->SetMarkerColor(kRed);
h3->SetLineColor(kRed);
1 Like

Thanks couet!