I have a code snippet that accesses data from a root file.
The code snippet runs and returns the right value when run in the interpreter.
It runs and returns a wrong value (a value from another branch) when run in compiled mode. My question is now: looking at how I access the data,
do you consider this a bug, or is your point that I simply use ROOT incorrectly (Given that I do not employ any of the ::SetAddress methods)?
Here’s the code:
TTree * Events = (TTree *) f.Get(“Events”);
TBranch * rec=(TBranch *) Events->GetListOfBranches()->At(1);
TBranch * obj=(TBranch *) rec->GetListOfBranches()->At(2);
TBranch * chi2=(TBranch *) obj->GetListOfBranches()->At(0);
TLeaf * leaf = (TLeaf *) chi2->GetListOfLeaves()->At(0);
cout << “v=” << v << endl;
So, my question, again, in other words: is this a “legal” and supported way of using ROOT? (Of course all objects do exist … ).
Or do I always have to meddle with the internal memory management,
even if, ultimately, I want a simple “double”?
[ Surely, returning a value from the wrong branch is pretty nasty, in any case. ]
P.S. The data was written with ROOT 5.14, and read with ROOT 5.15 and 5.17.
In your implementation, you are assuming that branches and leaves
are at a fixed and known position. This not safe and not readable.
I suggest changing your code to something like:
TTree * Events = (TTree *) f.Get("Events");
TLeaf *chi2 = Events->GetLeaf("chi2"); //or whatever is the leaf name
cout << "v=" << v << endl;
thank you very much for your answer. Unfortunately my question is not answered yet. The code snippet is just sample code, my actual use case is very different. (I actually want to write something that makes very few assumptions on the content of the ROOT file. I want sth that works on all ROOT files. So I “browse” through the brachnames and leafnames). That’s why I use the “At” statements.
Calling ::GetLeaf(leafname) after all the ::At statements does not mend the problem, by the way. So I want to repeat the question.
Assuming of course that the ROOT file really is the right way, should
the above code work - do the ROOT developers consider this a bug, or
do I have to live with the fact that things “just dont behave like that”?[/code]
It is difficult to answer your question without understanding what are your goals.
One thing is clear: nobody is programming your way to read ROOT Trees.
You should tell us:
-if you just want to read one branch (or a few branches)
-if you just want to browse leaves or do some operations with
-if you are converting from a ROOt format to some other format, etc
You can use ROOT Trees typically in two ways
-creating branches/leaves with basic types or arrays (rdbms model)
-creating one (or few) top level branches starting from an object
If you use the second approach (that’s what we all try to do), then you must think a bit how you are going to read back this object: into an object of the same class?
or in reading its leaves one by one by making assumptions on the object layout.
Oops, I think I was a little too quick with my last answer.
This thing that you did:
getting the leaves, then saying
and then asking for the value seems to fix my problem!
Thank you very much for these most instructive three lines!
(And yes, I still do not fully understand the logic of it ).
And no, I am not surprised that nobody reads ROOT trees that way
Oh, and, as a final remark. Yes, I write (have written) a tool that converts between several formats, one of them being ROOT.