Re: TRef between Branch

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Mon, 12 Oct 2009 09:00:08 -0500


Hi Stefano,

In your example, you do a branch 'fill' operation for each vertex and track by doing that you
lose (well the TTree loses) all concept of which track belongs with with vertex.

For any of the TTree automated tools (TTree::Draw, TTree::Process, Branch reference, etc.)
to work properly you must have only one TTree entry (and hence one TBranch entry and
hence one Fill) per semantic event.

So you need to create branches that contains a container of Track and Vertex rather than
directly the objects. (The container could be a TClonesArray or a std::vector).

In almost all cases, you should __not__ call TBranch::Fill directly but should just call
TTree::Fill.

> Clearly the number of entries in the brach containing the vertex is
> smaller then the number of entries in the track branch.

It should not. The number of entries should be the same but each entry should contain a variable number of track and vertex.

> but zero entry for the TRefTable

This is because you never fill this branch. It needs to be filled via a call to TTree::Fill.

Cheers,
Philippe.

Stefano Dusini wrote:
> Hi
>
> I would like to write a tree with two branch one containg the vertecies
> (Vertex class) and the other the tracks of the event (Track class). Clarly the
> number of entries in the brach containing the vertex is smaler then the number
> of entries in the track branch.
>
> To link the tracks to the verticies I use TRefArray.
>
> here below an example where I create some dummy vertexes and tracks.
>
> TFile *hFile = TFile::Open("myFile.root","recreate");
> TTree *myTree = new TTree("myTree","myTree");
> myTree->BranchRef();
> Vertex *vertex = new Vertex();
> Track *track = new Track();
> TBranch *vertexBranch =
> myTree->Branch("VertexBranch","Vertex",&vertex,16000,2);
> TBranch *trackBranch = myTree->Branch("TrackBranch","Track",&track,16000,2);
>
> for(int iv=0;iv<3;iv++) {
> vertex->SetID(iv); // I set the id of the vertex
> int itt=0;
> for(int it=0;it<iv+1;it++) {
> track->SetID(itt); //I set the id of the Track
> vertex->AddTrack(track); //This metod add the pointer track to a TRefArray of
> the class VERTEX
> trackBranch->Fill();
> track->Clear();
> }
> vertexBranch->Fill();
> vertex->Clear();
> }
> hFile->Write();
> myTree->Print();
> hFile->Close();
>
> The code to read the file is
>
> TFile *hFile = TFile::Open("emuHist.root");
> TTree *myTree = (TTree*) hFile->Get("myTree");
> myTree->BranchRef();
> myTree->Print();
>
> Vertex *vertex = new Vertex();
> Track *track = new Track();
> TBranch *vertexBranch = myTree->GetBranch("VertexBranch");
> TBranch *trackBranch = myTree->GetBranch("TrackBranch");
>
> vertexBranch->SetAddress(&vertex);
> trackBranch->SetAddress(&track);
>
> int nv = vertexBranch->GetEntries();
> int nt = trackBranch->GetEntries();
>
> for(int iv=0;iv<nv;iv++) {
> vertexBranch->GetEntry(iv);
> cout << "Vertex [" << iv << "] = " << vertex->GetID() << endl;
> TRefArray *aTrk = vertex->GetTrackArray();
> cout << "Number of tracks at the vertex " << aTrk->GetEntries() << endl;
> for(int it=0;it<aTrk->GetEntries();it++) {
> Track *track = vertex->GetTrack(it); //return (Track*)(eTrackArray->At(index))
> cout << "Track [" << it << "] ID " << track->GetID() << endl;
> }
> vertex->Clear();
> }
> }
>
> I'm able to read the vertexes but the pointers at the track attached to the
> vertex, i.e. the elements of the TRefArray aTrk are NULL pointers.
>
> In the TTree::Print() of the tree that I have create is I see the correct
> number of entries for the Vertex and Track branch but zero entry for the
> TRefTable. Do you have any explanation?
>
> Thanks a lot
> Stefano
>
> ******************************************************************************
> *Tree :myTree : myTree *
> *Entries : 0 : Total = 15243 bytes File Size = 2890 *
> * : : Tree compression factor = 1.00 *
> ******************************************************************************
> *Branch :VertexBranch *
> *Entries : 3 : BranchElement (see below) *
> *............................................................................*
> *Br 0 :fUniqueID : *
> *Entries : 3 : Total Size= 670 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 1 :fBits : *
> *Entries : 3 : Total Size= 662 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 2 :eID : *
> *Entries : 3 : Total Size= 634 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 3 :eTrackArray : *
> *Entries : 3 : Total Size= 891 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 4 :eAttributeList : *
> *Entries : 3 : Total Size= 716 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Branch :TrackBranch *
> *Entries : 6 : BranchElement (see below) *
> *............................................................................*
> *Br 5 :fUniqueID : *
> *Entries : 6 : Total Size= 682 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 6 :fBits : *
> *Entries : 6 : Total Size= 698 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 7 :eID : *
> *Entries : 6 : Total Size= 646 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 8 :eDigitList : *
> *Entries : 6 : Total Size= 740 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 9 :eTrackKinematic : *
> *Entries : 6 : Total Size= 746 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 10 :eVtUp : *
> *Entries : 6 : Total Size= 734 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 11 :eVtDown : *
> *Entries : 6 : Total Size= 746 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 12 :eVtUpID : *
> *Entries : 6 : Total Size= 670 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 13 :eVtDownID : *
> *Entries : 6 : Total Size= 682 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 14 :eIsKinematicOwner : *
> *Entries : 6 : Total Size= 740 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 *
> *............................................................................*
> *Br 15 :TRefTable : List of branch numbers with referenced objects *
> *Entries : 0 : Total Size= 600 bytes One basket in memory *
> *Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
> *............................................................................*
>
>
>
> --
> Stefano Dusin
> INFN - sezione di Padova
> via Marzolo 8
> Office: +39-049-8277323
>
>
Received on Mon Oct 12 2009 - 16:00:21 CEST

This archive was generated by hypermail 2.2.0 : Mon Oct 12 2009 - 17:50:04 CEST