Objects in TTrees can't be retrieved

I’ve created a few custom root objects that descend from TObject. I have a TEvent object which stores some integers and two TObjArrays. Each TObjArray stores THits or TParticles. Everything works wonderfully. I can even write the TEvent object directly to a file and then retrieve it and everything is there. However, instead of writing the TEvent object directly to disk (for example with an event->Write()), I want to put the TEvent in a tree as a branch. I am also going to need to store multiple TEvents, each with its own branch in the tree. I’ve tried to do this just about every way I’ve seen, but no matter what I do when I retrieve my TEvent object from the tree it contains all default values. I haven’t been able to find any previous posts on these boards that have solved my problem either. Here are some examples:

root [0] .L librhits.so
root [1] TEvent * event = new TEvent()
root [2] event->initialize(1,2,3,4,5) //eventId, runId, sourceId, sequence, timeStamp
root [3] TParticle * particle = new TParticle()
root [4] THit * hit = new THit()
root [5] event->getEventId()
(const unsigned int)1
root [6] event->getRunId()
(const unsigned int)2
root [7] event->addHit(hit)
root [8] event->getHitCount()
(const unsigned int)1
root [9] event->addParticle(particle)
root [10] event->getParticleCount()
(const unsigned int)1
root [11] TFile f("output.root","RECREATE")
root [12] event->Write()
(Int_t)304
root [13] f.Close()
// Close Root, open new session
root [0] .L librhits.so
root [1] TFile f("output.root")
root [2] TEvent * event = (TEvent*)f.Get("TEvent")
root [3] event->getHitCount()
(const unsigned int)1
root [4] event->Dump()
==> Dumping object at: 0x08da8b40, name=TEvent, class=TEvent

m_eventId                     1
m_runId                         2
m_sourceId                    3
m_sequence                  4
m_timeStamp                5
*m_particleCol               ->8dabef8   ->
*m_HitCol                     ->8db6138   ->
fUniqueID                     0           object unique identifier
fBits                            0x03000000  bit field status word
root [5] f.Close()
// Now try to put the object in a tree.

root [8] TFile file("test.root","RECREATE")
root [9] TTree * tree = new TTree("tree","data")
root [10] tree->Branch("event","TEvent",&event,16000,99)
(class TBranch*)0x8d90020
root [11] tree->Fill()
(Int_t)232
root [12] file.Write()
(Int_t)1582
root [13] file.Close()

// Close root, start new session

root [0] .L librhits.so
root [1] TFile f("test.root")
root [2] TTree * tree = (TTree*)f.Get("tree")
root [3] tree->GetEntries()
(const Long64_t)1
root [4] TEvent * event = 0
root [5] tree->SetBranchAddress("event",&event)
root [6] event->Dump()
==> Dumping object at: 0x08c32e68, name=TEvent, class=TEvent

m_eventId                     0
m_runId                       0
m_sourceId                    0
m_sequence                    0
m_timeStamp                   0
*m_particleCol                ->0         ->
*m_HitCol                     ->0         ->
fUniqueID                     0           object unique identifier
fBits                         0x03000000  bit field status word

The TEvent object is full of the default values, so something went wrong. If anyone has any ideas or wants me to post more information, let me know. I can also post the three header files for the objects I’ve created if need be. I do have default constructors that initialize all values to 0 in each of them. Thanks in advance for any help.

Hi,

You need to specify and load the entry you are interested in:

root [] tree->SetBranchAddress("event",&event) root [] tree->GetEntry(0); root [] event->Dump();
Cheers,
Philippe.

Thank you very much. Works perfectly.