Hi PyROOTers,
I have recently started using PyROOT and love it thus far. I’m getting into a bind with using TClonesArray though. As an example, I have code to fill a branch in a TTree with a TClonesArray of TH2Fs. In the example below, I try to populate the first element of the TClonesArray, fill the TTree and repeat 3 times.
[code]import ROOT
ROOT.gROOT.Reset()
ROOT.gStyle.SetPalette(1)
fileName = "testTCA.root"
treeName = "MyTree"
branchName = “MyBranch”
def treeWrite():
f = ROOT.TFile(fileName, “RECREATE”)
t = ROOT.TTree(treeName, “my test tree”)
tca = ROOT.TClonesArray(“TH2F”, 5);
t.Branch(branchName, “TClonesArray”, ROOT.AddressOf(tca), 3200, 0)
print "putting data into tree"
for nn in range(3):
name = “image”+str(nn)
#name = "image"
img = ROOT.TH2F(name, name, 100, 0, 100, 100, 0, 100)
print name
for ii in range(100):
for jj in range(100):
img.SetBinContent(ii, jj, (nn+1)*(ii+jj))
print "assign"
tca[0] = img
print "fill"
t.Fill()
#print "tca.Delete()"
#tca.Delete()
#print "after"
print "f.Write()"
f.Write()
print "f.Close()"
f.Close()
if name == “main”:
treeWrite()
[/code]
When I run this code, I find that the first loop completes successfully, but I get a seg. fault when trying to populate the TClonesArray the second time:
[code]$ python useTCA.py
putting data into tree
image0
assign
fill
image1
assign
*** Break *** segmentation violation
/Users/jbattat/computing/root/pyROOT/5821: No such file or directory.
Attaching to process 5821.
Reading symbols for shared libraries . done
Reading symbols for shared libraries … done
0x95a68189 in wait4 ()
========== STACKS OF ALL THREADS ==========
… much more follows …
[/code]
The “No such file or directory” line always refers to the process number of the current python process (not sure if that’s a useful clue).
I did see Wim’s earlier post (http://root.cern.ch/phpBB2/viewtopic.php?t=2704&start=25) which was a great help for me to get this far, but I could not solve my current problem with the information there.
Any help you could provide would be greatly appreciated.
By the way, I made a corresponding example in C++, which works just fine:
[code]#include
#include “TCanvas.h”
#include “TFile.h”
#include “TTree.h”
#include “TH2F.h”
#include “TString.h”
#include “TClonesArray.h”
using namespace std;
void treeWrite() {
TFile* f = new TFile(“testTCAC.root”, “RECREATE”);
TTree* t = new TTree(“MyTree”, “my test tree”);
TClonesArray* tca = new TClonesArray(“TH2F”, 5);
t->Branch(“MyBranch”, “TClonesArray”, &tca, 3200, 0);
cout << “putting data into tree” << endl;
for (int nn=0; nn<3; nn++) {
TString name = “image”;
name += nn;
cout << name << endl;
TH2F* img = new TH2F(name, name, 100, 0, 100, 100, 0, 100);
for (int ii=0; ii<100; ii++) {
for (int jj=0; jj<100; jj++) {
img->SetBinContent(ii, jj, (nn+1)*(ii+jj));
}
}
new( (*tca)[0]) TH2F(*img);
t->Fill();
// this line causes a crash in pyROOT
tca->Delete();
delete img;
}
cout << “f->Write()” << endl;
f->Write();
cout << “f->Close()” << endl;
f->Close();
}
[/code][/url]