Manipulating Histograms in Different Files

Dear ROOTers,

I am using ROOT 4.00/04 in a Linux environment, and am experiencing difficulties manipulating the histograms that I have generated with a compiled program that I have written.

The program creates a uniquely named histogram in a uniquely named file (both the histogram name and the file name are generated based on the data being sorted, so that I do not confuse myself). The problem is that I want to then compare these histograms (mostly simple addition and subtraction using TH1F::Add(const TH1* h1, Double_t c1 = 1)), but I am unable to figure out the syntax for working with histograms within different files. When I try to compare them using the interactive ROOT interface (either directly or with short scripts) the program tells me that any histogram that is not withing the current file “is not defined within the current scope”. I attempted to circumvent this with pointers, but I am unable to find a function within TFile that will return a pointer to a member histogram.

If anyone can provide a syntax example of opening two files which each contain a single TH1F, adding them, and then saving the result to a third file, I would appreciate it. My goal is to have a function that accepts several filenames and/or histogram names and adds and subtracts the appropriate histograms and saves output to a newly created file.

Thank you in advance,

Andrew Knox

To easily retrieve an histogram independently of the current ‘directory’ use:


Thank you for the expeditious response!

in attempting to write my own script utilizing that function I ran a couple of quick tests in the interactive interface, one of which follows:

(note that gammatimehistogram is the name of a histogram within the file 199.1879.root)

root [0] TFile myfile ("199.1879.root") root [1] TH1F * testogram root [2] myfile (class TFile)145542248 root [3] testogram (class TH1F*)0x0 root [4] myfile.GetObject("gammatimehistogram", testogram) Error: Can't call TFile::GetObject("gammatimehistogram",testogram) in current scope FILE:(tmpfile) LINE:1 Possible candidates are... filename line:size busy function type and name (in TFile) filename line:size busy function type and name (in TDirectory) filename line:size busy function type and name (in TNamed) filename line:size busy function type and name (in TObject) Error: Symbol myfile is not defined in current scope FILE:(tmpfile) LINE:1 Error: Failed to evaluate myfile.GetObject("gammatimehistogram",testogram)Possible candidates are... filename line:size busy function type and name *** Interpreter error recovered ***

Did I entirely miss the point?

Andrew Knox

TDirectory::GetObject appeared in ROOT 4.00/08.
In ROOT 4.00/04 use:


thank you very much, the code snippet works perfectly.

Andrew Knox

Hi everyone,

I have recently run into the following problem. I wrote one method to fill basic histos and write them to file separately for several simulations subsets. In a second method I open several TFiles at the same time to access these histograms that have always the same names. I do this the following way:

TFile *Fth234 = new TFile(“Th234_OC_MSC_histo.root”);
TFile *… (several more of these)


TH1F *hEtER_th234;
TH1F *hEtmsc_th234;
TH1F *hEtfvc_th234;

TH1F *hEtZoom_th234;
TH1F *hEtZmsc_th234;
TH1F *hEtZfvc_th234;



What I now do not understand is that it works fine for the first three histograms, but when I simply try to plot any of the last three, I get the following error message.

Error: illegal pointer to class object hEtZoom_th234 0x0 193 FILE:XeBGana.C LINE:284

I checked for spelling mistakes and/or typos, there is no, but the error message remains.

What do I do wrong and how can I solve this problem.

Any help is greatly appreciated!

Greets Joerg

TFile *Fth234 = new TFile("Th234_OC_MSC_histo.root"); Fth234->GetObject("hEtzoom;1",hEtZoom_th234); Error: illegal pointer to class object hEtZoom_th234 0x0 193 FILE:XeBGana.C LINE:284This means that ROOT thinks that in the top directory of the file Th234_OC_MSC_histo.root, there is no TH1F named hEtzoom (either there is no object or the object is not a TH1F, … maybe it is a TH2F?)
If you are sure that there is a TH1F named hEtzoom stored in the top level directory of Th234_OC_MSC_histo.root. please send me the file so that I can try to reproduce the problem.


Thanks for your fast reply.

It works out that there was just a problem about a CAPITAL and LOWER case ‘Z’ that I used in the macro different then in the actual name of the histogram.

TH1F *hEtzoom = new TH1F(“hEtZoom”,“Total Energy(keV) <=50 keV”,100,0.25,50.25);

I use hEtzoom, but then write hEtZoom to file!

Greets Joerg