Hi PyROOTers,
I’ve defined a C++ class, TTest that inherits from TNamed, that contains a Float_t and std::vector<Float_t>. In PyROOT I was able to initialize this object, place it into a TTree, and save to a TFile. However, I want to later reopen the TFile, get the TTree, and append another TTest object as another branch. The PyROOT manual describes this with Python builtins, and the Users’ Manual mentions how to do this in C++ with TObject::kOverwrite (although I did see that AddFriend is recommended). Is there any way to accomplish this with PyROOT? I’ve attached all the scripts. For what it’s worth, the errors that I get are due to the objects not having their addresses set, and I’m not sure if / how I should AddressOf() here.
Error in <TBranchElement::Fill>: attempt to fill branch Event_0 while addresss is not set
Error in <TBranchElement::Fill>: attempt to fill branch Event_1 while addresss is not set
Thanks!
Chris
My C++ class, TTest.C:
#include "TNamed.h"
#include <iostream>
#include <vector>
class TTest : public TNamed
{
private:
Float_t myfloat;
std::vector<Float_t> myvec;
public:
TTest() : myfloat(-1.0), myvec(-1.0, 4) {}
TTest(const char* name) : myfloat(-1.0), myvec(-1.0, 4) {this->SetName(name); }
Float_t Get() const { return myfloat; }
std::vector<Float_t> getVec() { return myvec; }
void Set(Float_t num) { myfloat = num; }
void setVec(std::vector<Float_t> newVec) { myvec = newVec; }
ClassDef(TTest, 1)
};
#if !defined(__CINT__)
ClassImp(TTest);
#endif
Create TTest object and save to a TTree in a ROOT file, testTTest.py:
#!/usr/bin/python
# Compile class with `echo .L TTest.C+ | root -b`
from ROOT import gSystem, std, TTree, TFile
gSystem.Load('TTest_C')
from ROOT import TTest
object = TTest("test_obj")
object.Set(3.0)
STLvec = std.vector(float)()
for i in xrange(3):
STLvec.push_back(i)
object.setVec(STLvec)
f = TFile("test.root", "recreate")
mytree = TTree("mytree", "testing tree appending")
mytree.Branch("Event_0", object)
mytree.Fill()
f.WriteTObject(mytree)
f.Close()
Trying to append to the already-existing TTree, testAppend.py:
#!/usr/bin/python
from ROOT import gSystem, std, TTree, TFile
gSystem.Load('TTest_C')
from ROOT import TTest
object2 = TTest("test_obj2")
object2.Set(8.0)
STLvec = std.vector(float)()
for i in xrange(3):
STLvec.push_back(3.3*i)
object2.setVec(STLvec)
f = TFile("test.root", "update")
tree = f.Get("mytree")
# around here I don't know what to do...
tree.Branch("Event_1", object2)
tree.Fill()
f.WriteTObject(tree)
f.Close()
testTTest.py (417 Bytes)
TTest.C (559 Bytes)
testAppend.py (432 Bytes)