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,
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  TFile myfile ("199.1879.root")
root  TH1F * testogram
root  myfile
root  testogram
root  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?
TDirectory::GetObject appeared in ROOT 4.00/08.
In ROOT 4.00/04 use:
thank you very much, the code snippet works perfectly.
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)
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!
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!