I’m reading values from one TTree (in an existing file), computing a new value to write to another TTree (and save in a new file). The Fill() call on the second tree does not work (when done in PyROOT), but works fine in C++.
Here’s the C++ code:
// Read in the ROOT file, compute a quantity and write it to a "Friend" tree
TFile* f1 = new TFile("test.root");
TTree* t1 = (TTree*)f1->Get("t1");
t1->Scan();
double n;
double m;
t1->SetBranchAddress("val1", &n);
t1->SetBranchAddress("val2", &m);
TFile* f2 = new TFile("testfriend.root", "RECREATE");
TTree* t2 = new TTree("t1f", "a friend tree");
double w;
t2->Branch("val3", &w, "val3/D");
Int_t nn = t1->GetEntries();
for (int ii=0; ii<nn; ii++) {
t1->GetEvent(ii);
w = n+m;
cout << n << " + " << m << " = " << w << endl;
t2->Fill();
}
t2->Scan();
with output
************************************
* Row * val1 * val2 *
************************************
* 0 * 2 * 3 *
* 1 * 4 * 6 *
* 2 * 6 * 9 *
************************************
2 + 3 = 5
4 + 6 = 10
6 + 9 = 15
************************
* Row * val3 *
************************
* 0 * 5 *
* 1 * 10 *
* 2 * 15 *
************************
(notice that val3
has the expected contents: 5, 10, 15).
When I do the analagous thing in PyROOT, I get all zeros for the val3
entries in the new TTree
:
import ROOT
import numpy as np
f1 = ROOT.TFile('test.root')
t1 = f1.Get('t1')
t1.Scan()
f2 = ROOT.TFile('testfriend.root', 'RECREATE')
t2 = ROOT.TTree('t1f', 'a friend tree')
w = np.zeros(1, dtype='float')
t2.Branch('val3', w, 'val3/D')
for event in t1:
w = event.val1 + event.val2
print "%d + %d = %d" % (event.val1, event.val2, w)
t2.Fill()
t2.Scan()
gives
************************************
* Row * val1 * val2 *
************************************
* 0 * 2 * 3 *
* 1 * 4 * 6 *
* 2 * 6 * 9 *
************************************
2 + 3 = 5
4 + 6 = 10
6 + 9 = 15
************************
* Row * val3 *
************************
* 0 * 0 *
* 1 * 0 *
* 2 * 0 *
************************
Notice how the val3
entries in the new TTree
are all zero.