Adding Histograms Question

I am using this macro to combine histograms from different folders:

TH1 *h = 0 gROOT->GetObject("/his00052.root/histos/h1",h) TH1 *hadd = 0 gROOT->GetObject("/his00053.root/histos/h1",hadd) h->Add(hadd)
When I enter the “h->Add(hadd)” I get this error Error: illegal pointer to class object h 0x0 996 (tmpfile):1: *** Interpreter error recovered ***

I’m not sure what I’m doing wrong. Thanks for your help.

gROOT->GetObject will not open any ROOT file nor navigate through them. You should tryTFile *myfile = TFile::Open("his00052.root"); TH1 *h = 0; myfile->GetObject("histos/h1",h); TFile *myfile = TFile::Open("his00053.root"); TH1 *hadd = 0; myfile->GetObject("histos/h1",hadd); h->Add(hadd);

Cheers,
Philippe.

It still showed the same error message when I did the “h.Add(hadd)” command. Thanks.

Then either h or hadd is null because histos/h1 does not exist in your ROOT file and/or is of the wrong type.

Philippe.

The histograms are of TH2F type.
Can you do the “Add” command with TH2F type histograms? Thanks.

Hi,

[quote]Can you do the “Add” command with TH2F type histograms?[/quote]Yes and you should have been able to retrieve them via the command you gave. Can you post your root file so that I can check.

Thanks,
Philippe.

Thanks.

Hi,

My apologies, I see that you really meant to mean TFolder when you said ‘folders’ and not (as I unfortunately thought ) directory/TDirectory.

TDirectory::GetObject can not drill trough a TFolder (it can only drill through TDirectories) so you will need to retrieve the TFolder first and then retrieve the histogram from the TFolder.

Cheers,
Philippe.

Thanks, I got it to work using this macro: TFile *f1 = TFile::Open("his_____.root") TFolder *folder1 = (TFolder*) f1->Get("histos") TH2F *h1 = (TH2F*) folder1->FindObjectAny("histo_name") h1->Add(h2)