ROOT  6.06/09
Reference Guide
TBranchRef.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 19/08/2004
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TBranchRef
13 A branch containing and managing a TRefTable for TRef autoloading.
14 It loads the TBranch containing a referenced object when requested
15 by TRef::GetObject(), so the reference can be resolved. The
16 information which branch to load is stored by TRefTable. Once a
17 TBranch has read the TBranchRef's current entry it will not be told
18 to re-read, in case the use has changed objects read from the
19 branch.
20 
21 *** LIMITATION ***
22 Note that this does NOT allow for autoloading of references spanning
23 different entries. The TBranchRef's current entry has to correspond
24 to the entry of the TBranch containing the referenced object.
25 
26 The TRef cannot be stored in a top-level branch which is a
27 TBranchObject for the auto-loading to work. E.g. you cannot store
28 the TRefs in TObjArray, and create a top-level branch storing this
29 TObjArray.
30 */
31 
32 #include "TBranchRef.h"
33 #include "TTree.h"
34 #include "TBasket.h"
35 #include "TFile.h"
36 #include "TFriendElement.h"
37 
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Default constructor.
42 
43 TBranchRef::TBranchRef(): TBranch(), fRequestedEntry(-1), fRefTable(0)
44 {
45  fReadLeaves = (ReadLeaves_t)&TBranchRef::ReadLeavesImpl;
46  fFillLeaves = (FillLeaves_t)&TBranchRef::FillLeavesImpl;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Main constructor called by TTree::BranchRef.
51 
53  : TBranch(), fRequestedEntry(-1), fRefTable(0)
54 {
55  if (!tree) return;
56  SetName("TRefTable");
57  SetTitle("List of branch numbers with referenced objects");
58  fRefTable = new TRefTable(this,100);
59 
60  fCompress = 1;
61  fBasketSize = 32000;
62  fAddress = 0;
66 
67  for (Int_t i=0;i<fMaxBaskets;i++) {
68  fBasketBytes[i] = 0;
69  fBasketEntry[i] = 0;
70  fBasketSeek[i] = 0;
71  }
72 
73  fTree = tree;
74  fMother = this;
76  fFileName = "";
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Typical destructor.
83 
85 {
86  delete fRefTable;
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Clear entries in the TRefTable.
91 
93 {
94  if (fRefTable) fRefTable->Clear(option);
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Fill the branch basket with the referenced objects parent numbers.
99 
101 {
102  Int_t nbytes = TBranch::Fill();
103  return nbytes;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// This function is called by TRefTable::Notify, itself called by
108 /// TRef::GetObject.
109 /// The function reads the branch containing the object referenced
110 /// by the TRef.
111 
113 {
114  if (!fRefTable) fRefTable = new TRefTable(this,100);
115  UInt_t uid = fRefTable->GetUID();
116  TProcessID* context = fRefTable->GetUIDContext();
117  if (fReadEntry != fRequestedEntry) {
118  // Load the RefTable if we need to.
120  }
121  TBranch *branch = (TBranch*)fRefTable->GetParent(uid, context);
122  if (branch) {
123  // don't re-read, the user might have changed some object
124  if (branch->GetReadEntry() != fRequestedEntry)
125  branch->GetEntry(fRequestedEntry);
126  } else {
127  //scan the TRefTable of possible friend Trees
128  TList *friends = fTree->GetListOfFriends();
129  if (!friends) return kTRUE;
130  TObjLink *lnk = friends->FirstLink();
131  while (lnk) {
132  TFriendElement* elem = (TFriendElement*)lnk->GetObject();
133  TTree *tree = elem->GetTree();
134  TBranchRef *bref = tree->GetBranchRef();
135  if (bref) {
136  if (bref->GetReadEntry() != fRequestedEntry) {
137  bref->GetEntry(fRequestedEntry);
138  }
139  branch = (TBranch*)bref->GetRefTable()->GetParent(uid, context);
140  if (branch) {
141  // don't re-read, the user might have changed some object
142  if (branch->GetReadEntry() != fRequestedEntry)
143  branch->GetEntry(fRequestedEntry);
144  return kTRUE;
145  }
146  }
147  lnk = lnk->Next();
148  }
149  }
150  return kTRUE;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Print the TRefTable branch.
155 
156 void TBranchRef::Print(Option_t *option) const
157 {
158  TBranch::Print(option);
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// This function called by TBranch::GetEntry overloads TBranch::ReadLeaves.
163 
165 {
166  if (!fRefTable) fRefTable = new TRefTable(this,100);
167  fRefTable->ReadBuffer(b);
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// This function called by TBranch::Fill overloads TBranch::FillLeaves.
172 
174 {
175  if (!fRefTable) fRefTable = new TRefTable(this,100);
176  fRefTable->FillBuffer(b);
177 }
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// - Existing buffers are deleted
181 /// - Entries, max and min are reset
182 /// - TRefTable is cleared.
183 
185 {
186  TBranch::Reset(option);
187  if (!fRefTable) fRefTable = new TRefTable(this,100);
188  fRefTable->Reset();
189 }
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 /// Reset a Branch after a Merge operation (drop data but keep customizations)
193 /// TRefTable is cleared.
194 
196 {
198  if (!fRefTable) fRefTable = new TRefTable(this,100);
199  fRefTable->Reset();
200 }
201 
202 ////////////////////////////////////////////////////////////////////////////////
203 /// Set the current parent branch.
204 ///
205 /// This function is called by TBranchElement::GetEntry()
206 /// and TBranchElement::Fill() when reading or writing
207 /// branches that may contain referenced objects.
208 
209 Int_t TBranchRef::SetParent(const TObject* object, Int_t branchID)
210 {
211  if (!fRefTable) {
212  fRefTable = new TRefTable(this, 100);
213  }
215  return fRefTable->SetParent(object, branchID);
216 }
217 
Long64_t GetReadEntry() const
Definition: TBranch.h:169
virtual void ResetAfterMerge(TFileMergeInfo *)
Reset a Branch after a Merge operation (drop data but keep customizations) TRefTable is cleared...
Definition: TBranchRef.cxx:195
virtual TTree * GetTree()
Return pointer to friend TTree.
long long Long64_t
Definition: RtypesCore.h:69
const char Option_t
Definition: RtypesCore.h:62
TObject * GetParent(Int_t uid, TProcessID *context=0) const
Return object corresponding to uid.
Definition: TRefTable.cxx:247
void ReadLeavesImpl(TBuffer &b)
This function called by TBranch::GetEntry overloads TBranch::ReadLeaves.
Definition: TBranchRef.cxx:164
ReadLeaves_t fReadLeaves
Definition: TBranch.h:106
TProcessID * GetUIDContext() const
Definition: TRefTable.h:83
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
virtual void Print(Option_t *option="") const
Print TBranch parameters.
Definition: TBranch.cxx:1726
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
TRefTable * GetRefTable() const
Definition: TBranchRef.h:49
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual Bool_t Notify()
This function is called by TRefTable::Notify, itself called by TRef::GetObject.
Definition: TBranchRef.cxx:112
Long64_t * fBasketSeek
Definition: TBranch.h:93
virtual ~TBranchRef()
Typical destructor.
Definition: TBranchRef.cxx:84
virtual void Clear(Option_t *="")
Clear all entries in the table.
Definition: TRefTable.cxx:143
virtual void Reset(Option_t *option="")
Definition: TBranchRef.cxx:184
Int_t * fBasketBytes
Definition: TBranch.h:91
static void SetRefTable(TRefTable *table)
Static function setting the current TRefTable.
Definition: TRefTable.cxx:381
virtual TList * GetListOfFriends() const
Definition: TTree.h:407
Int_t fMaxBaskets
Definition: TBranch.h:75
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition: TProcessID.h:34
A branch containing and managing a TRefTable for TRef autoloading.
Definition: TBranchRef.h:33
A doubly linked list.
Definition: TList.h:47
virtual void ResetAfterMerge(TFileMergeInfo *)
Reset a Branch.
Definition: TBranch.cxx:1964
virtual Int_t Fill()
Loop on all leaves of this branch to fill Basket buffer.
Definition: TBranch.cxx:723
void(TBranch::* ReadLeaves_t)(TBuffer &b)
After being read, the buffer will not be unziped.
Definition: TBranch.h:105
virtual void Reset(Option_t *="")
Clear all entries in the table.
Definition: TRefTable.cxx:343
Int_t fBasketSize
Definition: TBranch.h:70
void FillLeavesImpl(TBuffer &b)
This function called by TBranch::Fill overloads TBranch::FillLeaves.
Definition: TBranchRef.cxx:173
TDirectory * GetDirectory() const
Definition: TTree.h:381
virtual void ReadBuffer(TBuffer &b)
Fill buffer b with the fN elements in fParentdIDs.
Definition: TRefTable.cxx:306
FillLeaves_t fFillLeaves
Definition: TBranch.h:108
unsigned int UInt_t
Definition: RtypesCore.h:42
Long64_t fRequestedEntry
Definition: TBranchRef.h:35
virtual TBranchRef * GetBranchRef() const
Definition: TTree.h:370
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1198
virtual TObjLink * FirstLink() const
Definition: TList.h:101
virtual Int_t Fill()
Fill the branch basket with the referenced objects parent numbers.
Definition: TBranchRef.cxx:100
TRefTable * fRefTable
Cursor indicating which entry is being requested.
Definition: TBranchRef.h:38
virtual void Clear(Option_t *option="")
Clear entries in the TRefTable.
Definition: TBranchRef.cxx:92
virtual Int_t SetParent(const TObject *parent, Int_t branchID)
– Set current parent object, typically a branch of a tree.
Definition: TRefTable.cxx:355
A TRefTable maintains the association between a referenced object and the parent object supporting th...
Definition: TRefTable.h:37
virtual Int_t SetParent(const TObject *obj, Int_t branchID)
Set the current parent branch.
Definition: TBranchRef.cxx:209
TTree * fTree
Definition: TBranch.h:94
TDirectory * fDirectory
Address of 1st leaf (variable or object)
Definition: TBranch.h:98
virtual void FillBuffer(TBuffer &b)
Fill buffer b with the fN elements in fParentdIDs.
Definition: TRefTable.cxx:223
Mother of all ROOT objects.
Definition: TObject.h:58
ClassImp(TBranchRef) TBranchRef
Default constructor.
Definition: TBranchRef.cxx:38
Long64_t * fBasketEntry
Definition: TBranch.h:92
virtual void Reset(Option_t *option="")
Reset a Branch.
Definition: TBranch.cxx:1923
A TFriendElement TF describes a TTree object TF in a file.
void(TBranch::* FillLeaves_t)(TBuffer &b)
Pointer to the ReadLeaves implementation to use.
Definition: TBranch.h:107
Long64_t fReadEntry
Current basket number when reading.
Definition: TBranch.h:80
TBranch * fMother
Pointer to Tree header.
Definition: TBranch.h:95
A TTree object has a header with a name and a title.
Definition: TTree.h:94
A TTree is a list of TBranches.
Definition: TBranch.h:58
UInt_t GetUID() const
Definition: TRefTable.h:82
Int_t fCompress
branch counter
Definition: TBranch.h:69
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
TString fFileName
Pointer to directory where this branch buffers are stored.
Definition: TBranch.h:99
virtual void Print(Option_t *option="") const
Print the TRefTable branch.
Definition: TBranchRef.cxx:156
char * fAddress
Pointer to parent branch.
Definition: TBranch.h:97