Logo ROOT   6.14/05
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 \ingroup tree
14 
15 A branch containing and managing a TRefTable for TRef autoloading.
16 It loads the TBranch containing a referenced object when requested
17 by TRef::GetObject(), so the reference can be resolved. The
18 information which branch to load is stored by TRefTable. Once a
19 TBranch has read the TBranchRef's current entry it will not be told
20 to re-read, in case the use has changed objects read from the
21 branch.
22 
23 ### LIMITATION :
24 Note that this does NOT allow for autoloading of references spanning
25 different entries. The TBranchRef's current entry has to correspond
26 to the entry of the TBranch containing the referenced object.
27 
28 The TRef cannot be stored in a top-level branch which is a
29 TBranchObject for the auto-loading to work. E.g. you cannot store
30 the TRefs in TObjArray, and create a top-level branch storing this
31 TObjArray.
32 */
33 
34 #include "TBranchRef.h"
35 #include "TBuffer.h"
36 #include "TTree.h"
37 #include "TBasket.h"
38 #include "TFile.h"
39 #include "TFriendElement.h"
40 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Default constructor.
45 
46 TBranchRef::TBranchRef(): TBranch(), fRequestedEntry(-1), fRefTable(0)
47 {
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Main constructor called by TTree::BranchRef.
54 
56  : TBranch(), fRequestedEntry(-1), fRefTable(0)
57 {
58  if (!tree) return;
59  SetName("TRefTable");
60  SetTitle("List of branch numbers with referenced objects");
61  fRefTable = new TRefTable(this,100);
62 
63  fCompress = 1;
64  fBasketSize = 32000;
65  fAddress = 0;
69 
70  for (Int_t i=0;i<fMaxBaskets;i++) {
71  fBasketBytes[i] = 0;
72  fBasketEntry[i] = 0;
73  fBasketSeek[i] = 0;
74  }
75 
76  fTree = tree;
77  fMother = this;
79  fFileName = "";
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Typical destructor.
86 
88 {
89  delete fRefTable;
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Clear entries in the TRefTable.
94 
96 {
97  if (fRefTable) fRefTable->Clear(option);
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Fill the branch basket with the referenced objects parent numbers.
102 
104 {
105  Int_t nbytes = TBranch::FillImpl(imtHelper);
106  return nbytes;
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// This function is called by TRefTable::Notify, itself called by
111 /// TRef::GetObject.
112 /// The function reads the branch containing the object referenced
113 /// by the TRef.
114 
116 {
117  if (!fRefTable) fRefTable = new TRefTable(this,100);
118  UInt_t uid = fRefTable->GetUID();
119  TProcessID* context = fRefTable->GetUIDContext();
120  if (fReadEntry != fRequestedEntry) {
121  // Load the RefTable if we need to.
123  }
124  TBranch *branch = (TBranch*)fRefTable->GetParent(uid, context);
125  if (branch) {
126  // don't re-read, the user might have changed some object
127  if (branch->GetReadEntry() != fRequestedEntry)
128  branch->GetEntry(fRequestedEntry);
129  } else {
130  //scan the TRefTable of possible friend Trees
131  TList *friends = fTree->GetListOfFriends();
132  if (!friends) return kTRUE;
133  TObjLink *lnk = friends->FirstLink();
134  while (lnk) {
135  TFriendElement* elem = (TFriendElement*)lnk->GetObject();
136  TTree *tree = elem->GetTree();
137  TBranchRef *bref = tree->GetBranchRef();
138  if (bref) {
139  if (bref->GetReadEntry() != fRequestedEntry) {
140  bref->GetEntry(fRequestedEntry);
141  }
142  branch = (TBranch*)bref->GetRefTable()->GetParent(uid, context);
143  if (branch) {
144  // don't re-read, the user might have changed some object
145  if (branch->GetReadEntry() != fRequestedEntry)
146  branch->GetEntry(fRequestedEntry);
147  return kTRUE;
148  }
149  }
150  lnk = lnk->Next();
151  }
152  }
153  return kTRUE;
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Print the TRefTable branch.
158 
159 void TBranchRef::Print(Option_t *option) const
160 {
161  TBranch::Print(option);
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// This function called by TBranch::GetEntry overloads TBranch::ReadLeaves.
166 
168 {
169  if (!fRefTable) fRefTable = new TRefTable(this,100);
170  fRefTable->ReadBuffer(b);
171 }
172 
173 ////////////////////////////////////////////////////////////////////////////////
174 /// This function called by TBranch::Fill overloads TBranch::FillLeaves.
175 
177 {
178  if (!fRefTable) fRefTable = new TRefTable(this,100);
179  fRefTable->FillBuffer(b);
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// - Existing buffers are deleted
184 /// - Entries, max and min are reset
185 /// - TRefTable is cleared.
186 
188 {
189  TBranch::Reset(option);
190  if (!fRefTable) fRefTable = new TRefTable(this,100);
191  fRefTable->Reset();
192 }
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Reset a Branch after a Merge operation (drop data but keep customizations)
196 /// TRefTable is cleared.
197 
199 {
201  if (!fRefTable) fRefTable = new TRefTable(this,100);
202  fRefTable->Reset();
203 }
204 
205 ////////////////////////////////////////////////////////////////////////////////
206 /// Set the current parent branch.
207 ///
208 /// This function is called by TBranchElement::GetEntry()
209 /// and TBranchElement::Fill() when reading or writing
210 /// branches that may contain referenced objects.
211 
212 Int_t TBranchRef::SetParent(const TObject* object, Int_t branchID)
213 {
214  if (!fRefTable) {
215  fRefTable = new TRefTable(this, 100);
216  }
218  return fRefTable->SetParent(object, branchID);
219 }
220 
virtual void ResetAfterMerge(TFileMergeInfo *)
Reset a Branch after a Merge operation (drop data but keep customizations) TRefTable is cleared...
Definition: TBranchRef.cxx:198
virtual Int_t FillImpl(ROOT::Internal::TBranchIMTHelper *)
Loop on all leaves of this branch to fill Basket buffer.
Definition: TBranch.cxx:818
virtual TTree * GetTree()
Return pointer to friend TTree.
long long Long64_t
Definition: RtypesCore.h:69
TRefTable * GetRefTable() const
Definition: TBranchRef.h:44
const char Option_t
Definition: RtypesCore.h:62
void ReadLeavesImpl(TBuffer &b)
This function called by TBranch::GetEntry overloads TBranch::ReadLeaves.
Definition: TBranchRef.cxx:167
ReadLeaves_t fReadLeaves
! Pointer to the ReadLeaves implementation to use.
Definition: TBranch.h:125
TBranchRef()
Default constructor.
Definition: TBranchRef.cxx:46
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
virtual TList * GetListOfFriends() const
Definition: TTree.h:411
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
UInt_t GetUID() const
Definition: TRefTable.h:80
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:115
Long64_t * fBasketSeek
[fMaxBaskets] Addresses of baskets on file
Definition: TBranch.h:108
virtual ~TBranchRef()
Typical destructor.
Definition: TBranchRef.cxx:87
virtual void Clear(Option_t *="")
Clear all entries in the table.
Definition: TRefTable.cxx:145
virtual void Reset(Option_t *option="")
Definition: TBranchRef.cxx:187
Int_t * fBasketBytes
[fMaxBaskets] Length of baskets on file
Definition: TBranch.h:106
virtual void Print(Option_t *option="") const
Print the TRefTable branch.
Definition: TBranchRef.cxx:159
static void SetRefTable(TRefTable *table)
Static function setting the current TRefTable.
Definition: TRefTable.cxx:383
Int_t fMaxBaskets
Maximum number of Baskets so far.
Definition: TBranch.h:90
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition: TProcessID.h:69
A branch containing and managing a TRefTable for TRef autoloading.
Definition: TBranchRef.h:29
A doubly linked list.
Definition: TList.h:44
virtual void ResetAfterMerge(TFileMergeInfo *)
Reset a Branch.
Definition: TBranch.cxx:2174
void(TBranch::* ReadLeaves_t)(TBuffer &b)
Definition: TBranch.h:124
virtual void Reset(Option_t *="")
Clear all entries in the table.
Definition: TRefTable.cxx:345
Int_t fBasketSize
Initial Size of Basket Buffer.
Definition: TBranch.h:84
void FillLeavesImpl(TBuffer &b)
This function called by TBranch::Fill overloads TBranch::FillLeaves.
Definition: TBranchRef.cxx:176
virtual void ReadBuffer(TBuffer &b)
Fill buffer b with the fN elements in fParentdIDs.
Definition: TRefTable.cxx:308
FillLeaves_t fFillLeaves
! Pointer to the FillLeaves implementation to use.
Definition: TBranch.h:127
unsigned int UInt_t
Definition: RtypesCore.h:42
Long64_t fRequestedEntry
! Cursor indicating which entry is being requested.
Definition: TBranchRef.h:31
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:1310
virtual TBranchRef * GetBranchRef() const
Definition: TTree.h:371
virtual TObjLink * FirstLink() const
Definition: TList.h:108
TRefTable * fRefTable
pointer to the TRefTable
Definition: TBranchRef.h:34
#define ClassImp(name)
Definition: Rtypes.h:359
virtual void Clear(Option_t *option="")
Clear entries in the TRefTable.
Definition: TBranchRef.cxx:95
TDirectory * GetDirectory() const
Definition: TTree.h:383
virtual Int_t SetParent(const TObject *parent, Int_t branchID)
– Set current parent object, typically a branch of a tree.
Definition: TRefTable.cxx:357
A TRefTable maintains the association between a referenced object and the parent object supporting th...
Definition: TRefTable.h:35
virtual Int_t SetParent(const TObject *obj, Int_t branchID)
Set the current parent branch.
Definition: TBranchRef.cxx:212
TTree * fTree
! Pointer to Tree header
Definition: TBranch.h:109
TDirectory * fDirectory
! Pointer to directory where this branch buffers are stored
Definition: TBranch.h:113
virtual void FillBuffer(TBuffer &b)
Fill buffer b with the fN elements in fParentdIDs.
Definition: TRefTable.cxx:225
TProcessID * GetUIDContext() const
Definition: TRefTable.h:81
Mother of all ROOT objects.
Definition: TObject.h:37
Long64_t * fBasketEntry
[fMaxBaskets] Table of first entry in each basket
Definition: TBranch.h:107
virtual void Reset(Option_t *option="")
Reset a Branch.
Definition: TBranch.cxx:2133
A TFriendElement TF describes a TTree object TF in a file.
void(TBranch::* FillLeaves_t)(TBuffer &b)
Definition: TBranch.h:126
Long64_t fReadEntry
! Current entry number when reading
Definition: TBranch.h:95
Long64_t GetReadEntry() const
Definition: TBranch.h:192
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
TBranch * fMother
! Pointer to top-level parent branch in the tree.
Definition: TBranch.h:110
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:70
A TTree is a list of TBranches.
Definition: TBranch.h:62
Int_t fCompress
Compression level and algorithm.
Definition: TBranch.h:83
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
virtual Int_t FillImpl(ROOT::Internal::TBranchIMTHelper *)
Fill the branch basket with the referenced objects parent numbers.
Definition: TBranchRef.cxx:103
virtual void Print(Option_t *option="") const
Print TBranch parameters.
Definition: TBranch.cxx:1917
TString fFileName
Name of file where buffers are stored ("" if in same file as Tree header)
Definition: TBranch.h:114
const Bool_t kTRUE
Definition: RtypesCore.h:87
TObject * GetParent(Int_t uid, TProcessID *context=0) const
Return object corresponding to uid.
Definition: TRefTable.cxx:249
char * fAddress
! Address of 1st leaf (variable or object)
Definition: TBranch.h:112