I am working with root 303-09. I don’t want to use macro, but a compiled program.
Following problem: I want to chain several root files, each one containing a tree with several branches (one for each detector).
Chaining the files works, I get the expected number of entries by GetEntries. Then I do an event loop, while trying to read only one branch.
n_events = chain_root->GetEntries(); // fetch number of entries
TRICH *rich = new TRICH(); // storage for one RICH event
chain_root -> SetBranchAddress("rich", rich);
chain_root -> SetBranchStatus("*", 0);
chain_root -> SetBranchStatus("rich", 1); // only RICH event
for( i = 0; i < n_events; i++ )
{
retVal = chain_root -> GetEntry(i)
cout << retVal << " bytes read." << endl;
}
This doesn’t work for me. I tried several versions, some giving me only data from the first root file, some others crashing with reading the first event.
So my question is: how do I read data from several root file (chained by TChain) with an own event loop, by accessing some branches ?
TRICH *rich = new TRICH(); // storage for one RICH event
chain_root -> SetBranchAddress("rich", rich);
should be
TRICH *rich = new TRICH(); // storage for one RICH event
chain_root -> SetBranchAddress("rich", &rich);
(note the ‘&’ taking the address of the pointer).
Also if the branch is split, you might try
[quote=“pcanal”]Without more information from your side, it is difficult to guess what might be wrong in your setup.
[/quote]
So. Finally, I did step back in code history and managed to get a version which reproduces the error / unexpected behaviour (Root version 303-09).
The source code file is attached, it should be easy to understand.
What I do: I set up a chain of root files, containing the same tree (“tree2”), and I want to read some branches (in this case, only the RICH branch).
In the version attached I get all events from all files, as I set the BranchAddress for each new File which is accessed in the eventloop (but if this is needed, what is a chain then good for ?).
If I leave out the if-statement in line 141 - 145, but take in the line 133 (GetBranch statement), I get only data from the first event, and the last event of the first file is booked for all other events into the histogram (gives a sharp peak in all variables).
Any ideas on how I could get rid of my workarounds ?
Indeed, the TBranch Object belongs to the TTree objects and are recreated each time a new file is opened by the chain.
Hence you code
if( localentry == 0 )
{
cout << "Ho Ho Ho: I have read " << localmax << " entries." << endl;
br_rich = chain_root -> GetBranch("rich");
}
is mostly correctly (it assumes that you are using a TEventList and are always read all the entries of each tree … alternatively you could monitor the value of TChain::GetTreeNumber).
One alternative is to use SetBranchStatus/chain->GetEntry instead of GetBranch/branch->GetEntry. However this is not as efficient as what you are already doing.
Another alternative is to have the TChain object notify your object that it (the chain) has changed. This is the technic used by MakeClass/MakeSelector.