How to set Title when superimposing two histograms?

I superimpose two histograms:

 TH1F *data1 = new TH1F("without_KF", "without_KF",100,0,400 );
    t1->Project("without_KF", "pi0pi0");
    //e_data->Scale(1./scale_e);
    data1->Draw();
    c->Update();
    TH1F *data2 = new TH1F("with_KF", "with_KF",100,0,400);
    t2->Project("with_KF", "pi0pi0");
    data2->SetLineColor(kRed);
    data2->Draw("same");
    c->BuildLegend();

Next I want to set Title in result picture. By default it is the name of the 1st drew hist. And I cannot change it. How to set Title in this case properly?

The simplest is to change the title of the histogram data1, either at creation time or using SetTitle on data1.

It is bad because I set Legend and if I change title of the hist data1 then in legend it will be too.

{
   auto h1 = new TH1D("h1","h1",100,-2.,2.);
   h1->FillRandom("gaus", 100000);
   h1->Draw();

   auto h2 = new TH1D("h2","h2",100,-2.,2.);
   h2->FillRandom("gaus", 50000);
   h2->Draw("same");
   gPad->Update();

   TPaveText* pt  = (TPaveText*)gPad->FindObject("title");

   TPaveText* ptn = new TPaveText(pt->GetX1(),pt->GetY1(),
                                  pt->GetX2(),pt->GetY2(),"");
   ptn->AddText("New Title");
   ptn->SetBorderSize(0);
   ptn->SetFillColor(0);
   ptn->SetTextFont(42);
   ptn->Draw();

   gPad->BuildLegend();
}
1 Like

Though it looks like not the way it works and I do not have any alternative. Thank you.

One more. I want to enlarge text. But if I try ptn->SetTextSize(number) I get no text at all.
How to set text size?

Sorry it seems like number should be less than zero? Sorry.

To enlarge the title you should specify the ton box larger. X1 smaller and X2 bigger.

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