Pointer to the object in TChain

Hi,

I have a simple question. I have root files containing histograms. I want to get the pointer to the objects (histograms) in root files. For one root file I can do

    TFile f("test.root");
    TH2D *h2 = (TH2D*)f.Get("h2");

I want to do the same thing with a number of similar root files. So, I put them in a chain.

    TChain *chain = new TChain();
    chain->AddFile("test1.root");
    chain->AddFile("test2.root");
    ..................................

Now, How can I get the pointer to the histogram h2?

Thanks,
Manoj

I assume that somewhere you are looping on entries of the chain.
in this loop, you can do

TH2 *h2 = (TH2*)chain.GetCurrentFile()->Get("h2");
Rene

Thanks Rene. Simply, I want to add root files in chain and plot the histograms. There is no loop over the number of entries. There is no tree.

With chain->GetEntries(), I got following error:
Error in TChain::LoadTree: Cannot find tree with name in file test1.root
Error in TChain::LoadTree: Cannot find tree with name in file test2.root

With TH2 h2 = (TH2)chain.GetCurrentFile()->Get(“h2”), I got following error:

Error: illegal pointer to class object GetCurrentFile() 0x0 77 plot.C:16:

Manoj.

If you just want to add histograms in ROOT files, do

hadd -f result.root file1.root file2.root .. fileN.root where hadd is in $ROOTSYS/bin/hadd

Rene

It followed the same way and showed me the same problem

Error in TChain::LoadTree: Cannot find tree with name in file 2.root
Error in TChain::LoadTree: Cannot find tree with name in file 3.root

please help me
I have three files (1.root, 2.root ,3.root ) and I want to draw them in one drawing I followed the method
But did not good work
TChain *chain = new TChain();
chain->AddFile(“test1.root”);
chain->AddFile(“test2.root”);

How to use this method --> hadd -f result.root file1.root file2.root … fileN.root

It is most likely that you do not have the correct path to your ROOT files. Perhaps you need to add the directory information?

You basically have the hadd arguments correct, but for your case it should read:

hadd result.root test1.root test2.root

This needs to be executed in the directory where test1.root and test2.root are or the paths updated to point at those files.