Strange behaviour of CloneTree(0)

Good morning,
I’m working on a BaBar analysis and I realize that my code has a strange bug.

I have to read some files, each with a TTree. Then I split the input file in 4 parts, according with a int variable (PID).
My code is the following

[code]int main(int argc, char* argv[]) {

const int nchannels = 5;
if (argc < 3) return 0;
const int run = atoi(argv[1]);
string type = samplename(argv[2]);
if (type == “badname”) return 0;
TChain* intree = new TChain(“BtnSel”);
stringstream infiles;
infiles << “/data1/babardata/Btaunu/SubSkimTuple/”
<< type << “_” << “Run" << run << “.root”;
intree->Add(infiles.str().c_str());
cout << "Adding file " << infiles.str() << endl;
if (type==“Data” && run ==5) {
stringstream infiles2;
infiles2 << “/data1/babardata/Btaunu/SubSkimTuple/”
<< type << "
” << “_Run” << run << “1.root";
intree->Add(infiles2.str().c_str());
cout << "Adding file " << infiles2.str() << endl;
}
Float_t PID;
intree->SetBranchAddress(“RecTauMode”,&PID);
stringstream outname[nchannels];
vector<TFile*> outfiles;
vector<TTree*> outtrees;
string ch[] = {“e”,“mu”,“pi”,“rho”,“a1”};
for (int i=0; i < nchannels; i++) {
outname[i] << “/data1/babardata/Btaunu/SubSkimTuple/”
<< type << "
” << ch[i] << “_Run” << run << “.root”;
outfiles.push_back(new TFile(outname[i].str().c_str(),“RECREATE”));
outfiles[i]->cd();
outtrees.push_back(intree->CloneTree(0));
}
int nev = intree->GetEntries();
for (int j=0; j<nev; j++) {
intree->GetEntry(j);
if (j%500==0)
cout << "\rEvent n. " << j << “/” << nev << flush;
int idx = (int)PID - 1;
if (idx<0 || idx >4) {
cout << "Event "<< j << " BAD PID! " << PID << " ----> " << idx << endl;
continue;
}
outtrees[idx]->Fill();
}
cout << endl;
for (int i=0; i<nchannels; i++) {
outfiles[i]->Write();
}
delete intree;
return 0;
}[/code]

I realized that the variable eextragammaPhoton_ecal, that is an array of float, became 0 in all array values. It happened only in a single file “Sig__Run4”.

I tried to eliminate the vector structure fr TFile and TTree wiriting this:

[code]string ch[] = {“e”,“mu”,“pi”,“rho”};
for (int i=0; i < nchannels; i++) {
outname[i] << “/data1/babardata/Btaunu/SubSkimTuple/”
<< type << “_” << ch[i] << “_Run” << run << “.root”;
}

TFile* efile = new TFile(outname[0].str().c_str(),“RECREATE”);
efile->cd();
TTree* etree = intree->CloneTree(0);
TFile* mufile = new TFile(outname[1].str().c_str(),“RECREATE”);
mufile->cd();
TTree* mutree = intree->CloneTree(0);
TFile* pifile = new TFile(outname[2].str().c_str(),“RECREATE”);
pifile->cd();
TTree* pitree = intree->CloneTree(0);
TFile* rhofile = new TFile(outname[3].str().c_str(),“RECREATE”);
rhofile->cd();
TTree* rhotree = intree->CloneTree(0);[/code]

Now the same error appeared in another file (Sig__Run2).

I avoid this bug making the application read the incriminate variable in the input file, like this:

intree->SetBranchAddress("neextragamma",&ngamma); intree->SetBranchAddress("eextragammaPhoton_ecal",energy);

but I still have no idea of why this happened.

Can anybody help me, please?

Cheers
Giovanni Onorato - INFN Naples - Italy

Hi,

Your code looks fine as it is and I am not seeing any obvious error. The problem is likely in subtle differences between your files. To help any further I would need a complete running example (with data file) showing the problem (including a description of what you expected vs what you actually see).

Cheers,
Philippe.