I am trying to read data in from a Cern ROOT TTree file. I have not used root before and I am struggling a bit with this. I am familiar with C++, so can sort the array side of things, but I have been through several tutorial pages online and haven't got anywhere.
How do I read data from a TTree file? I assume it would be done by iterating over the tree's nodes (again, I'm not exactly sure how the file is organized?) inside a for loop?
Here is an example of the sort of reference I have been trying to follow.
https://root.cern.ch/drupal/content/using-macro-read-ttree
As I understand it this is a generic question, but TTree's are not generic? (Since they could contain different variable names, I think?)
So, further information, which I think is important is:
By opening the root object browser TBrowser b
- and browsing to the '.root' file, (and double clicking it) I can see that there are 12 variables in the file. For example, one is called 'mass', 'charge', etc...
Hope that's enough info? If not I can provide more.
This page seems to show nicely how this can be done: https://root.cern.ch/root/htmldoc/TTreeReader.html
The shortest usage example for your case would be:
TFile file("filename.root");
TTreeReader reader("treename", &file);
TTreeReaderValue<float> mass(reader, "mass"); // template type must match datatype
TTreeReaderValue<float> charge(reader, "charge"); // name must match branchname
...
while (reader.Next()) {
// use *mass, *charge, ...
}
In the olden days, there used to be a more manual way of doing the same thing. You had to redirect the branches of the tree to your local variables. This method looks like this:
TTree* tree = (TTree*) file.Get("treename");
float mass, charge, ...;
tree->SetBranchAddress("mass", &mass);
tree->SetBranchAddress("charge", &charge);
...
for (int i = 0, N = tree->GetEntries(); i < N; ++i) {
tree->GetEntry(i);
// use mass, charge
}
From the TBrowser you can read off the names of the branches that you need to supply as the second paramter to TTreeReaderValue
or SetBranchAddress
.
Basically you should think of TTree
as a collection of entries (classical trees). Each of the entries is made up of branches (nodes). This is how you read it.