TTree vs TChain

Hi,

The following code works fine,

TFile trainFile = new TFile(train_file,“READ”);
TTree trainTree = (TTree)trainFile->Get(“ana_nue”);
if(!trainTree) cout << “Couldn’t load training tree!” << std::endl;
NueRecord
trainRecord = 0;
TBranch* trainbranch = trainTree->GetBranch(“NueRecord”);
if(!trainbranch) cout << “Couldn’t find NueRecord train branch!” << std::endl;
trainbranch->SetAddress(&trainRecord);
.
.
.
Int_t train_nentries = (Int_t)trainTree->GetEntries();
for (Int_t i = 0; i < train_nentries; i++){
if(i%1000==0) cout<<"i = "<<i<<endl;

 trainTree->GetEntry(i,1);
cout<<trainRecord->shi.emenergy<<" "<<trainRecord->mctrue.nuEnergy<<" "<<trainRecord->mctrue.hadronicY<<" "<<trainRecord->mctrue.interactionType<<endl;

}

but when I try to use a TChain (so I can include more than one file) things break down and I get the default values for the quantities I request (trainRecord->shi.emenergy etc). What am I doing wrong?

TChain trainTree = new TChain(“ana_nue”); trainTree->Add("/data/AnaNue_Files/AnaNue-n13011051_0000_L010185N_D00.sntp.cedar_phy-TrkCuts.root");
trainTree->Add("/data/AnaNue_Files/AnaNue-n13011061_0000_L010185N_D00.sntp.cedar_phy-TrkCuts.root");
trainTree->Add("/data/AnaNue_Files/AnaNue-n13011143_0000_L010185N_D00.sntp.cedar_phy-TrkCuts.root");
trainTree->Add("/data/AnaNue_Files/AnaNue-n13011186_0000_L010185N_D00.sntp.cedar_phy-TrkCuts.root");
if(!trainTree) cout << “Couldn’t load training tree!” << std::endl;
NueRecord
trainRecord = 0;
TBranch* trainbranch = trainTree->GetBranch(“NueRecord”);
if(!trainbranch) cout << “Couldn’t find NueRecord train branch!” << std::endl;
trainbranch->SetAddress(&trainRecord);
.
.
.
Int_t train_nentries = (Int_t)trainTree->GetEntries();
for (Int_t i = 0; i < train_nentries; i++){
if(i%1000==0) cout<<"i = “<<i<<endl;
trainTree->GetEntry(i,1);
cout<shi.emenergy<<” “<mctrue.nuEnergy<<” “<mctrue.hadronicY<<” "<mctrue.interactionType<<endl;
}

Thanks for your help

Dan

Hi,

When writing code that should work both with a TTree and a TChain, you should not use the TBranch object directly. Tthis is because the TBranch object are specific to each TTree object and change overt time in the TChain. Any customization done on the TBranch level is lost after each file.

For example replace trainbranch->SetAddress(&trainRecord);with TBranch* trainbranch = 0; train->SetBranchAddress("NueRecord",&trainRecord,&trainbranch);

Cheers,
Philippe.