Accessing histos with identical names from different files

hi,

i attached two .root files with different histograms of the same name and a script which draws both histograms.

here’s what the script looks like:

[code]{
TFile* DataDstarFile = TFile::Open(“testInput1.root”);
TFile* McDstarFile = TFile::Open(“testInput2.root”);

TH1D* firstHisto = (TH1D*) DataDstarFile->Get("ptKaon");

// TH1D* histoClone = (TH1D*) firstHisto->Clone();
TH1D* secondHisto = (TH1D*) McDstarFile->Get(“ptKaon”);

TCanvas canvas;
canvas.Divide(2,1);

canvas.cd(1);
firstHisto->Draw();

canvas.cd(2);
secondHisto->Draw();

// TFile file(“testOutput.root”,“recreate”);
// firstHisto->Write();
// secondHisto->Write();
// file.Close();

}[/code]

when i run the script (root 5.18) without cloning the histogram in line 7 everything’s fine, both histograms are drawn correctly.

the problem arises when i uncomment line 7:

TH1D* histoClone = (TH1D*) firstHisto->Clone();

if i clone the first histogram before drawing the second, then the plots that are drawn are identical, only the first histogram is drawn.

the same thing happens when i write the histograms to another file. if i clone the first histogram before writing the second to file only two copies of the first will be written to the output file (see the last few uncommented lines).

what is the reason for this? how can i clone (or copy) a histogram and still afterwards access another histogram of the same name from a different file?

cheers, and thanks in advance for help and comments,
axel.
testInput2.root (3.74 KB)
testInput1.root (3.73 KB)
testHistoSameName.C (510 Bytes)

Hi,

When you do: TFile* McDstarFile = TFile::Open("testInput2.root"); ... TH1D* histoClone = (TH1D*) firstHisto->Clone();The new histogram is ‘attached’ to the current directory, hence it is attach to McDstarFile. Then TH1D* secondHisto = (TH1D*) McDstarFile->Get("ptKaon");return the histogram name ‘ptKaon’ which is in memory in the directory ‘McDstarFile’ … so it returns the clone you just made!.

It is unlikely you need to attach the cloned histogram to your second input file. So simply use: TH1D* histoClone = (TH1D*) firstHisto->Clone(); histoClone->SetDirectory(0); // Detach the histoor gROOT->cd(); // Make gROOT the current directory TH1D* histoClone = (TH1D*) firstHisto->Clone();

Cheers,
Philippe.

hi philippe,

thanks very much for your help, gROOT->cd() solved my problem.

cheers,
axel.