I am having trouble processing a TChain. It seems to know the first file in the chain, but then does not get any new events for the following files. I have read here and in other places that this is related to refreshing branch pointers, but how do I know when I need to do that? It seems very inefficient to do it on every event if it is only needed when encountering a new file.
I have pared this down to a simple test. I have tried it both as compiled code and in the root interpreter and get the same result. Below are three sets of code, one to make a few files to chain together (makeTree.cc), one to try to read the chain (readTree.cc), and finally a common .h definition file.
Thanks for your help,
Paul
makeTree.cc
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TChain.h>
#include <TRandom.h>
#include "myBranches.h"
void makeTree() {
TRandom* myRandom = new TRandom();
int maxEvt = 5;
int maxFile = 3;
for (int iFile = 1; iFile < maxFile; iFile++) {
char fileName[128];
snprintf(fileName, 128, "myfile%.3i.root", iFile);
TFile *myFile = new TFile(fileName, "create");
TTree *myTree = new TTree("testTree", "testTree");
myTree->Branch("branch1", &branch1, "iEvt:random_flat:one");
myTree->Branch("branch2", &branch2, "iEvt:random_gauss:two");
for (int iEvt = 0; iEvt < maxEvt; iEvt++) {
branch1.iEvt = iEvt;
branch1.random_flat = myRandom->Rndm();
branch1.one = 1.0;
branch2.iEvt = iEvt;
branch2.random_gauss = myRandom->Gaus();
branch2.two = 2.0;
std::cout << branch2.random_gauss << " " ;
myTree->Fill();
}
myTree->Print();
myFile->Write();
myFile->Close();
}
}
#ifndef __CINT__
int main() {
makeTree();
return 1;
}
#endif
readTree.cc
#include <iostream>
#include <unistd.h>
#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TChain.h>
#include <TRandom.h>
#include "myBranches.h"
void myTest() {
int maxFile = 3;
TChain* dataTree = new TChain("testTree");
for (int iFile = 1; iFile < maxFile; iFile++) {
char fileName[128];
snprintf(fileName, 128, "myfile%.3i.root", iFile);
dataTree->Add(fileName);
}
TBranch* b1_Branch = new TBranch();
dataTree->SetBranchAddress("branch1", &branch1, &(b1_Branch));
TBranch* b2_Branch = new TBranch();
dataTree->SetBranchAddress("branch2", &branch2, &(b2_Branch));
Long64_t nEvtMax = dataTree->GetEntries();
std::cout << "Total Entries seen in testTree = " << nEvtMax << std::endl;
Long64_t iEvt = 1;
int getEntryReturn = 0;
for (Long64_t iEvt = 0; iEvt <=nEvtMax; iEvt++ ) {
getEntryReturn = b1_Branch->GetEntry(iEvt);
std::cout << " branch 1 " << branch1.iEvt << " "
<< branch1.random_flat << " "
<< branch1.one;
getEntryReturn = b2_Branch->GetEntry(iEvt);
std::cout << " branch 2 " << branch2.iEvt << " "
<< branch2.random_gauss << " "
<< branch2.two << std::endl;
}
}
#ifndef __CINT__
int main() {
myTest();
return 1;
}
#endif
myBranches.h
#ifndef MYBRANCHES_H
#define MYBRANCHES_H
struct branch1Struct {
int iEvt;
float random_flat;
float one;
} branch1;
struct branch2Struct {
int iEvt;
float random_gauss;
float two;
} branch2;
#endif
| Welcome to ROOT 6.29/01 https://root.cern |
| (c) 1995-2022, The ROOT Team; conception: R. Brun, F. Rademakers |
| Built for macosx64 on Aug 20 2023, 16:04:00 |
| From tag , 21 December 2022 |
| With Apple clang version 14.0.0 (clang-1400.0.29.202) |
Try ‘.help’/‘.?’, ‘.demo’, ‘.license’, ‘.credits’, ‘.quit’/‘.q’ |
---|
ROOT Version: 6.29/01 |
Platform: macosx64 |
Compiler: Apple clang 14.0.0 |