Hi all,
I am generating a ROOT file with 2306 histograms in it and afterwards, I want to read the ROOT file to access each histogram to make a fit to each one of them.
which is generating the output root file called myfile.root with all the histograms as TKeys instead of TH1Ds (as in the input file). Also, the histograms are not being displayed at all.
Can’t you put a if(hist-ClassName() == “TH1D”) in your while loop? I would use this to filter out the TObject you get and properly cast it to TH1D
Hope it helps
Note that I removed the Draw as I not sure ‘what’/‘how’ you want to draw. The original code did not even have time to refresh the screen with one histogram before it was replaced by the next one.
Using pcanal’s suggestion, I get the following warning and error which I don’t know how to solve
root_forum.cpp:24:29: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
while(key = (TKey*)iter()) {
^
root_forum.cpp:26:38: error: 'class TKey' has no member named 'ReadObject'
TH2D *hist = (TH2D*)key->ReadObject();
My code is in the attachment. root_forum.cxx (808 Bytes)
I think the only reason I put Draw() here was to check if the histogram is accessible or not.
Unfortunately, this also does not work. I don’t get any compilation error (except that hist->ClassName() == “TH1D”
didn’t work because ClassName() returns a const char and not a string) but the output file does not contain the histograms.
static TString classname("TH2D");
if (key->GetClassName() == classname) {
is completely different from
if (hist->ClassName() == “TH1D”)
The former, because ‘classname’ is a TString object will use a string based comparison (something like strcmp) while the later since both operand and char* will do a comparison of the pointer value and thus the equality will always be false.
Thank you for the correction! Yes, I noticed now in the ROOT documentation that ReadObj() and ReadObject() are different!
The parentheses warning is also solved but I don’t understand why they are required?
However, the output root file still does not contain any histograms.
I did not know that (abou the string comparison thing). Thanks for the info!
Thanks for the tip, couet. However, the root command line tools were released with ROOT 6.06 while mine is an older version. Besides, I think it doesn’t matter as far as my question is concerned.
FWIW, groot provides (about) the same command-line tools, are independant of the ROOT version you’ve installed and can thus work alongside your usual ROOT setup:
The (input) file that I am using is not empty. It contains the histograms that I want to access one-by-one. It is the output file which is being rendered empty.