Hi,
I want create a function of normalization and I have a problem of “how to get all the name of every histogram in the root file ?”
void normalization(TString rootfile){
TFile file = new TFile(rootfile);
TString names[4]={“Interval1”,“Interval2”,“Interval3”,“Interval4”};
TH1D normalizedPlots[4];
TCanvasc2= new TCanvas(“normalizedPlots”, “normalizedPlots”,900, 900);
c2->Divide(2,2);
for(int i(0); i < 4; i++){
normalizedPlots[i] =new TH1D(“Normalized”+ names[i],“Normalized”+ names[i] +";Mass [GeV]; Entries per 0.1 GeV",2000,0,200);
[b]normalizedPlots[i] = (TH1D)file->Get([color=#FF0000]???[/color]);[/b]
normalizedPlots[i]->Scale(1./normalizedPlots[i]->GetEntries());
normalizedPlots[i]->SetLineColor(i+5);
c2->cd(i+1);
normalizedPlots[i]->Draw();
}
}
So what can we do to get the name of histogram automatically ?
Thank you
file->GetObject(names[i], normalizedPlots[i]);
if (!normalizedPlots[i]) continue; // " names[i]" TH1D not found in “file”
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=“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