Get the name of histogram

[quote=“tayyebb”]No, for example we have this code :

{
TFile *f = new TFile(“myfile.root”);

TH1F * h1 = new TH1F(“h1”,“h1 title” , 100, 0, 4);
h1 = (TH1F*)f.Get([color=#FF0000]hist1[/color]);
h1->Draw();
}
I want to retrieve the name of the histogram that exists in the root file ?[/quote]

Start by getting the list of objects from the file

then iterate over the list and check if the objects are histograms

string name; for(int i=0;i < list->Getsize();i++) { TObject *obj = (TObject*)list->At(i); if(obj->ClassName() == "TH1") { TH1 *h = (TH1*)obj; name = h->GetName(); } else if(obj->ClassName() == "TH2") { TH2 *h = (TH2*)obj; name = h->GetName(); } }
which should give you the names. See root.cern.ch/root/roottalk/roottalk00/2885.html for the full example.

2 Likes