Logo ROOT  
Reference Guide
TFriendElement.cxx
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Rene Brun 07/04/2001
3
4/*************************************************************************
5 * Copyright (C) 1995-2001, 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 TFriendElement
13\ingroup tree
14
15A TFriendElement TF describes a TTree object TF in a file.
16When a TFriendElement TF is added to the the list of friends of an
17existing TTree T, any variable from TF can be referenced in a query
18to T.
19
20To add a TFriendElement to an existing TTree T, do:
21~~~ {.cpp}
22 T.AddFriend("friendTreename","friendTreeFile");
23~~~
24See TTree::AddFriend for more information.
25*/
26
27#include "TFriendElement.h"
28#include "TBuffer.h"
29#include "TTree.h"
30#include "TFile.h"
31#include "TROOT.h"
32#include "TChain.h"
33
35
36////////////////////////////////////////////////////////////////////////////////
37/// Default constructor for a friend element.
38
40{
41 fFile = 0;
42 fTree = 0;
44 fParentTree = 0;
45}
46
47////////////////////////////////////////////////////////////////////////////////
48/// Create a friend element.
49///
50/// If treename is of the form "a=b", an alias called "a" is created for
51/// treename = "b" by default the alias name is the name of the tree.
52
53TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
54 :TNamed(treename,filename)
55{
56 fFile = 0;
57 fTree = 0;
60 fTreeName = treename;
61 if (treename && strchr(treename,'=')) {
62 char *temp = Compress(treename);
63 char *equal = strchr(temp,'=');
64 if (!equal) return;;
65 *equal=0;
66 fTreeName = equal+1;
67 SetName(temp);
68 delete [] temp;
69 }
70
71 Connect();
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// Create a friend element.
76///
77/// If treename is of the form "a=b", an alias called "a" is created for
78/// treename = "b" by default the alias name is the name of the tree.
79/// The passed TFile is managed by the user (i.e. user must delete the TFile).
80
82 :TNamed(treename,file?file->GetName():"")
83{
84 fFile = file;
85 fTree = 0;
88 fTreeName = treename;
91 // The friend and the TTree are in the same file, let's not record
92 // the filename.
93 SetTitle("");
94 }
95 if (treename && strchr(treename,'=')) {
96 char *temp = Compress(treename);
97 char *equal = strchr(temp,'=');
98 if (!equal) return;;
99 *equal=0;
100 fTreeName = equal+1;
101 SetName(temp);
102 delete [] temp;
103 }
104
105 Connect();
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// Create a friend element.
110
111TFriendElement::TFriendElement(TTree *tree, TTree* friendtree, const char *alias)
112 : TNamed(friendtree?friendtree->GetName():"",
113 friendtree
114 ? ( friendtree->GetDirectory()
115 ? ( friendtree->GetDirectory()->GetFile()
116 ? friendtree->GetDirectory()->GetFile()->GetName()
117 : "")
118 : "")
119 : "")
120{
121 fTree = friendtree;
122 fTreeName = "";
123 fFile = 0;
126 if (fTree) {
131 // The friend and the TTree are in the same file, let's not record
132 // the filename.
133 SetTitle("");
134 }
135 } else {
136 MakeZombie(); // ROOT-7007
137 }
138 if (alias && strlen(alias)) {
139 char *temp = Compress(alias);
140 SetName(temp);
141 delete [] temp;
142 }
143
144 if (fTree)
146 // No need to Connect.
147}
148
149////////////////////////////////////////////////////////////////////////////////
150/// Destructor. Disconnect from the owning tree if needed.
151
153{
154 DisConnect();
155}
156
157////////////////////////////////////////////////////////////////////////////////
158/// Connect file and return TTree.
159
161{
162 GetFile();
163 auto treePtr = GetTree();
164 if (!treePtr) MakeZombie(); // ROOT-7007
165 return treePtr;
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// DisConnect file and TTree.
170
172{
173 // At this point, if the condition is not meant, fTree is usually already
174 // deleted (hence the need for a local bit describing fTree)
175 if (fTree)
177 if (fOwnFile) delete fFile;
178 fFile = 0;
179 fTree = 0;
180 return 0;
181}
182
183////////////////////////////////////////////////////////////////////////////////
184/// Return pointer to TFile containing this friend TTree.
185
187{
188 if (fFile || IsZombie()) return fFile;
189
190 if (strlen(GetTitle())) {
193 fOwnFile = kTRUE;
194 } else {
196 if (dir) {
197 fFile = dir->GetFile();
199 }
200 }
201 if (fFile && fFile->IsZombie()) {
202 MakeZombie();
203 delete fFile;
204 fFile = 0;
205 }
206 return fFile;
207}
208
209////////////////////////////////////////////////////////////////////////////////
210/// Return pointer to friend TTree.
211
213{
214 if (fTree) {
215 // In cases where the friend was added by the owning chain and the friend is
216 // a chain we recorded the address of the chain but we need to return the address
217 // of the underlying current TTree.
218 if (TestBit(kFromChain))
219 return fTree->GetTree();
220 else
221 return fTree;
222 }
223
224 if (GetFile()) {
226 } else {
227 // This could be a memory tree or chain
228 fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
229 }
230
231 if (fTree)
233
234 return fTree;
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// List this friend element.
239
241{
242 printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
243}
244
245////////////////////////////////////////////////////////////////////////////////
246/// Forget deleted elements.
247
249{
250 if (delobj == fTree)
251 fTree = nullptr;
252 if (delobj == fFile)
253 fFile = nullptr;
254}
const Bool_t kFALSE
Definition: RtypesCore.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
#define gROOT
Definition: TROOT.h:406
char * Compress(const char *str)
Remove all blanks from the string str.
Definition: TString.cxx:2504
Small helper to keep current directory context.
Definition: TDirectory.h:47
Describe directory structure in memory.
Definition: TDirectory.h:40
virtual TFile * GetFile() const
Definition: TDirectory.h:163
void GetObject(const char *namecycle, T *&ptr)
Definition: TDirectory.h:155
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3942
A TFriendElement TF describes a TTree object TF in a file.
TFriendElement()
Default constructor for a friend element.
virtual void ls(Option_t *option="") const
List this friend element.
TFile * fFile
! pointer to the file containing the friend TTree
virtual const char * GetTreeName() const
Bool_t fOwnFile
true if file is managed by this class
TTree * fTree
! pointer to the TTree described by this element
virtual TTree * GetTree()
Return pointer to friend TTree.
virtual TFile * GetFile()
Return pointer to TFile containing this friend TTree.
virtual TTree * Connect()
Connect file and return TTree.
virtual void RecursiveRemove(TObject *obj)
Forget deleted elements.
virtual TTree * DisConnect()
DisConnect file and TTree.
TTree * fParentTree
! pointer to the parent TTree
virtual ~TFriendElement()
Destructor. Disconnect from the owning tree if needed.
TString fTreeName
name of the friend TTree
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:187
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:149
void MakeZombie()
Definition: TObject.h:49
A TTree represents a columnar dataset.
Definition: TTree.h:78
TDirectory * GetDirectory() const
Definition: TTree.h:456
virtual TTree * GetTree() const
Definition: TTree.h:511
virtual void RegisterExternalFriend(TFriendElement *)
Record a TFriendElement that we need to warn when the chain switches to a new file (typically this is...
Definition: TTree.cxx:7851
virtual void RemoveExternalFriend(TFriendElement *)
Removes external friend.
Definition: TTree.cxx:7862
Definition: file.py:1
Definition: tree.py:1