TPaveText & TText don't show in Pads

Hi,rooters,

Four pads in a Canvas were drawn.
And TPave (*pt)was expected to be drawn in the righttop pad, but it didn’t work.

And the TTxet (*tv2, *tv3)were expected to be drawn same as TH1D in the lefttop pad and rightbottom pad, however they weren’t work.

The results is shown in figure:

And my code is:

#include "TF2.h"
#include "TH2.h"
#include "TCutG.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TStyle.h"
 
double fun2(double *x, double *par) {
   double r1 = double((x[0]-par[1])/par[2]);
   double r2 = double((x[1]-par[3])/par[4]);
   return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));
}

 
TCanvas *fourpad_gamma() {
   
   //gStyle->SetOptStat(true);
   //gStyle->SetOptFit(1111);
	//gStyle->SetPalette(57);

   Double_t xpmin = -1.0;
   Double_t xpmax = -0.5;
   Double_t ypmin = -0.01;
   Double_t ypmax = 0.01;
   const int npar = 5;
   double f2params[npar] = {1e6,-0.749,0.003,8*1e-6,0.003};
   auto f2 = new TF2("f2",fun2,xpmin,xpmax,ypmin,ypmax,npar);
   f2->SetParameters(f2params);
 
   //Create an histogram and fill it randomly with f2
   TFile *f = new TFile("photon_position.root");
   TH2D *h2;
   h2 = (TH2D *)f->Get("hp");

   f2->SetParameters(f2params);
   h2->Fit("f2","N");
   //h2->Fit("f2");

   TCanvas *c = new TCanvas("c1","c1",600,600);
   TPad *leftbottom = new TPad("leftbottom", "leftbottom",0.0,0.0,0.6,0.6);
   leftbottom->Draw();

   TPad *rightbottom = new TPad("rightbottom", "rightbottom",0.61,0.0,1.0,0.6);
   rightbottom->Draw();

   TPad *lefttop = new TPad("lefttop", "lefttop",0.0,0.4,0.6,1.0);
   lefttop->Draw();

   TPad *righttop = new TPad("righttop", "righttop",0.6,0.6,1.0,1.0);
   righttop->Draw();

   // TH1D build-process
   TH1D * projh2X = h2->ProjectionX();
   TH1D * projh2Y = h2->ProjectionY();
   
	 //========
   leftbottom->cd();
   gStyle->SetPalette(1);
   gStyle->SetOptStat(0);
	 //gStyle->SetOptFit();
	 //gStyle->SetStatY(0.6);
	 h2->Draw("lego2 0");
   h2->SetFillColor(38);
   f2->SetNpx(20);
   f2->SetNpy(20);
   //f2->Draw("surf1 same");
   f2->Draw("surf1 same");
	 
	 //========
   //righttop->cd();
	 //h2->Draw("lego2 0");
   //h2->SetFillColor(38);
 /*
 TLatex *t = new TLatex();
	 t->SetTextFont(42);
	 t->SetTextSize(0.08);
	 t->DrawLatex(0.5,0.80,"Fit result ");
	 t->DrawLatex(0.5,0.75,"Mean x      -0.749");
	 t->DrawLatex(0.5,0.70,"Mean y      5.551e-07");
	 t->DrawLatex(0.5,0.65,"Std Dev x   0.002961");
	 t->DrawLatex(0.5,0.60,"Std Dev y   0.002292");
	 t->DrawLatex(0.5,0.55,"p0          1.704e+5 #pm 69.99");
	 t->DrawLatex(0.5,0.50,"p1          -0.75 #pm 4.20372e-07");
	 t->DrawLatex(0.5,0.45,"p2          1.64458 #pm 5.29590e-07");
	 t->DrawLatex(0.5,0.40,"p3          7.41913e-07 #pm 3.9271e-07");
	 t->DrawLatex(0.5,0.35,"p4          1.54011e-03 #pm 5.58518e-07");
*/
   c->cd();
	 TPaveText pt(0.5,0.5,0.9,0.9);
	 pt.AddText("Mean x      -0.749");
	 pt.AddText("Mean y      5.551e-07");
	 pt.AddText("Std Dev x   0.002961");
	 pt.AddText("Std Dev y   0.002292");
	 pt.AddText("p0          1.704e+5 #pm 69.99");
	 pt.AddText("p1          -0.75 #pm 4.20372e-07");
	 pt.AddText("p2          1.64458 #pm 5.29590e-07");
	 pt.AddText("p3          7.41913e-07 #pm 3.9271e-07");
	 pt.AddText("p4          1.54011e-03 #pm 5.58518e-07");
   pt.SetLabel("Fit result");
	 pt.Draw("same");

	 //========
   lefttop->cd();
   //gStyle->SetOptStat(0000);
   projh2X->SetFillColor(kBlue+1);
   projh2X->Draw("bar");
	 projh2X->SetStats(0);
	 projh2Y->GetXaxis()->CenterTitle(1);
   
	 auto *tv2 = new TText(0.16,0.823,"Projection X");
	 tv2->SetTextAlign(13);
	 tv2->SetTextSize(0.12);
	 tv2->Draw("same");
	 
   //========
   rightbottom->cd();
   //gStyle->SetOptStat(0);
   projh2Y->SetFillColor(kBlue-2);
   projh2Y->Draw("hbar");
	 projh2Y->SetStats(0);
	 projh2Y->GetXaxis()->SetTitle("Y_{#gamma}");
	 projh2Y->GetXaxis()->CenterTitle(1);
	 //projh2Y->GetXaxis()->SetLabelOffset(0.05);
	 
   auto *tv3 = new TText(0.16,0.823,"Projection Y");
	 tv3->SetTextAlign(13);
	 tv3->SetTextSize(0.12);
	 tv3->Draw("same");

   return c;
}

photon_position.root (51.2 KB)

Best regards,
hurric…

     righttop->cd();
	 TPaveText *pt = new TPaveText(0.5,0.5,0.9,0.9);
	 pt->AddText("Mean x      -0.749");
	 pt->AddText("Mean y      5.551e-07");
	 pt->AddText("Std Dev x   0.002961");
	 pt->AddText("Std Dev y   0.002292");
	 pt->AddText("p0          1.704e+5 #pm 69.99");
	 pt->AddText("p1          -0.75 #pm 4.20372e-07");
	 pt->AddText("p2          1.64458 #pm 5.29590e-07");
	 pt->AddText("p3          7.41913e-07 #pm 3.9271e-07");
	 pt->AddText("p4          1.54011e-03 #pm 5.58518e-07");
     pt->SetLabel("Fit result");
	 pt->Draw();

Note that there is no “same” option, neither for TText->Draw().

The coordinates for TText in a pad should be in the same units as the axes of what is plotted in the pad. To use fractions from 0 to 1 you need to add

    tv2->SetNDC();  // same for tv3

before drawing.

1 Like

You elaborated in great detail.
It works well now!
Thank you so much!

Best regards,
hurric

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