Hi,
I’m currently writing a script which merges two trees. Both trees contains mainly vectors; for ta
they are strictly of length 3, while for tm
they are of varying length > 3.
To actually do this, I’m using .SetBranchAddress
to get each entry of the two trees. When I write out the entries to the console, most of my entries appears to be garbage values:
When I inspect the resulting merged tree, however, everything appears to be as expected, with no trace of these weird values. The changes I make (if the condition index==-1
triggers, I’ve verified that it does) are apparently not carried through.
Can you guys point out what I am doing wrong? I’ve tried looking around the forum and documentation for similar issues without finding anything.
// open the root files
TFile *fa = TFile::Open(apath);
TFile *fm = TFile::Open(mpath);
TTree *ta = (TTree *)fa->Get("a");
TTree *tm = (TTree *)fm->Get("a101");
// define variables from analyzed tree
int mi[3], N;
ta->SetBranchAddress("N", &N);
ta->SetBranchAddress("mi", &mi);
// define variables from matched tree
// they all have length 100 to ensure there's space for all entries
double FT[100], BT[100], FI[100], BI[100], FE[100], BE[100], ID[100];
tm->SetBranchAddress("FI", &FI);
tm->SetBranchAddress("FT", &FT);
tm->SetBranchAddress("BI", &BI);
tm->SetBranchAddress("BT", &BT);
tm->SetBranchAddress("FE", &FE);
tm->SetBranchAddress("BE", &BE);
tm->SetBranchAddress("id", &ID);
// define destination tree and set its branches
double ft[3], bt[3], fi[3], bi[3], fe[3], be[3], id[3];
string dest = "merged/" + p.filename().string();
TFile f(dest.c_str(), "recreate");
TTree t("tree", "merged tree for TDC calibration");
t.Branch("FI", &fi, "FI[3]/I");
t.Branch("BI", &bi, "BI[3]/I");
t.Branch("FT", &ft, "FT[3]/I");
t.Branch("BT", &bt, "BT[3]/I");
t.Branch("FE", &fe, "FE[3]/D");
t.Branch("BE", &be, "BE[3]/D");
t.Branch("id", &id, "id[3]/I");
// loop over every event in the analyzed tree ta
for (double i = 0; i < ta->GetEntries(); i++) {
ta->GetEntry(i);
tm->GetEntry(N); // N is the corresponding index in tm for each event in ta
// loop over the three alpha particles
// we use the multiplicity index mi to get the correct values
int index;
bool flag = false;
for (int i = 0; i < 3; i++) {
index = mi[i];
if (index == -1) { // if it is a reconstructed event
c++;
ft[i] = 0;
bt[i] = 0;
fi[i] = -1;
bi[i] = -1;
fe[i] = 0;
be[i] = 0;
id[i] = -1;
flag = true;
}
else { // if it is a true event
ft[i] = FT[index];
bt[i] = BT[index];
fi[i] = FI[index];
bi[i] = BI[index];
fe[i] = FE[index];
be[i] = BE[index];
id[i] = ID[index];
cout << format("%1%, %2%, %3%, %4%, %5%, %6%, %7%") % FT[i] % BT[i] % FI[i] % BI[i] % FE[i] % BE[i] % ID[i] << endl;
}
}
t.Fill(); // fill the entries into the merged tree t
}
t.Write(); // write to disk
ROOT Version: 6.22/07