Writing Text over TH1D or TH2D hist without writing over TCanvas

Hi ROOT experts!

I want to write a simple text over hist (TH1D/TH2D) without TCanvas. There are many histograms in a root file and I want to write text over some of the histograms available in the root file. Pls, suggest any hint. Thanks in advance.

_ROOT Version:_6.28/04
Platform: linuxx8664gcc
_Compiler:_c++ (GCC) 12.2.0


Hi,
I am not sure I fully understand your problem. The text will be shown when you plot the histogram, so it will be in a ROOT Canvas. You would like to append text in the class that is automatically visible when displaying the histogram ?

Lorenzo

I got solution. It is in this way …

#include "TH1D.h"
#include "TPaveText.h"
#include "TList.h"

#include "Riostream.h"
#include "TRandom.h"

#include "TFile.h"

void Text(){
    TFile* file = new TFile("file.root","recreate");
    TH1D* h1=new TH1D("h1","h1",10,-5,5);
    TRandom* rng =new TRandom();
    for(int i=0;i<50;i++){
        h1->Fill(rng->Gaus());
    }
    TPaveText* msg1 = new TPaveText(0.1,0.8,0.2,0.9,"NDC"); // check option "br","NDC"
    h1->GetListOfFunctions()->Add(msg1);
    // msg1->Clear();
    msg1->AddText("Gaussian Dist 1");
    msg1->SetFillColor(kGreen);
    // msg1->SetTextSize(0.1);

    file->Write();
    file->Close();
}

Thanks for reply.

1 Like

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