Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TTree.h"
29#include "TFile.h"
30#include "TROOT.h"
31#include "TChain.h"
32
34
35////////////////////////////////////////////////////////////////////////////////
36/// Default constructor for a friend element.
37
39{
40 fFile = 0;
41 fTree = 0;
43 fParentTree = 0;
44}
45
46////////////////////////////////////////////////////////////////////////////////
47/// Create a friend element.
48///
49/// If treename is of the form "a=b", an alias called "a" is created for
50/// treename = "b" by default the alias name is the name of the tree.
51
52TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
53 :TNamed(treename,filename)
54{
55 fFile = 0;
56 fTree = 0;
59 fTreeName = treename;
60 if (treename && strchr(treename,'=')) {
61 char *temp = Compress(treename);
62 char *equal = strchr(temp,'=');
63 if (!equal) return;;
64 *equal=0;
65 fTreeName = equal+1;
66 SetName(temp);
67 delete [] temp;
68 }
69
70 Connect();
71}
72
73////////////////////////////////////////////////////////////////////////////////
74/// Create a friend element.
75///
76/// If treename is of the form "a=b", an alias called "a" is created for
77/// treename = "b" by default the alias name is the name of the tree.
78/// The passed TFile is managed by the user (i.e. user must delete the TFile).
79
81 :TNamed(treename,file?file->GetName():"")
82{
83 fFile = file;
84 fTree = 0;
87 fTreeName = treename;
90 // The friend and the TTree are in the same file, let's not record
91 // the filename.
92 SetTitle("");
93 }
94 if (treename && strchr(treename,'=')) {
95 char *temp = Compress(treename);
96 char *equal = strchr(temp,'=');
97 if (!equal) return;;
98 *equal=0;
99 fTreeName = equal+1;
100 SetName(temp);
101 delete [] temp;
102 }
103
104 Connect();
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Create a friend element.
109
110TFriendElement::TFriendElement(TTree *tree, TTree* friendtree, const char *alias)
111 : TNamed(friendtree?friendtree->GetName():"",
112 friendtree
113 ? ( friendtree->GetDirectory()
114 ? ( friendtree->GetDirectory()->GetFile()
115 ? friendtree->GetDirectory()->GetFile()->GetName()
116 : "")
117 : "")
118 : "")
119{
120 fTree = friendtree;
121 fTreeName = "";
122 fFile = 0;
125 if (fTree) {
130 // The friend and the TTree are in the same file, let's not record
131 // the filename.
132 SetTitle("");
133 }
134 } else {
135 MakeZombie(); // ROOT-7007
136 }
137 if (alias && strlen(alias)) {
138 char *temp = Compress(alias);
139 SetName(temp);
140 delete [] temp;
141 }
142
143 if (fTree)
145 // No need to Connect.
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Destructor. Disconnect from the owning tree if needed.
150
152{
153 DisConnect();
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// Connect file and return TTree.
158
160{
161 GetFile();
162 auto treePtr = GetTree();
163 if (!treePtr) MakeZombie(); // ROOT-7007
164 return treePtr;
165}
166
167////////////////////////////////////////////////////////////////////////////////
168/// DisConnect file and TTree.
169
171{
172 // At this point, if the condition is not meant, fTree is usually already
173 // deleted (hence the need for a local bit describing fTree)
174 if (fTree)
176 if (fOwnFile) delete fFile;
177 fFile = 0;
178 fTree = 0;
179 return 0;
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Return pointer to TFile containing this friend TTree.
184
186{
187 if (fFile || IsZombie()) return fFile;
188
189 if (strlen(GetTitle())) {
192 fOwnFile = kTRUE;
193 } else {
195 if (dir) {
196 fFile = dir->GetFile();
198 }
199 }
200 if (fFile && fFile->IsZombie()) {
201 MakeZombie();
202 delete fFile;
203 fFile = 0;
204 }
205 return fFile;
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// Return pointer to friend TTree.
210
212{
213 if (fTree) {
214 // In cases where the friend was added by the owning chain and the friend is
215 // a chain we recorded the address of the chain but we need to return the address
216 // of the underlying current TTree.
217 if (TestBit(kFromChain))
218 return fTree->GetTree();
219 else
220 return fTree;
221 }
222
223 if (GetFile()) {
225 } else {
226 // This could be a memory tree or chain
227 fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
228 }
229
230 if (fTree)
232
233 return fTree;
234}
235
236////////////////////////////////////////////////////////////////////////////////
237/// List this friend element.
238
240{
241 printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
242}
243
244////////////////////////////////////////////////////////////////////////////////
245/// Forget deleted elements.
246
248{
249 if (delobj == fTree)
250 fTree = nullptr;
251 if (delobj == fFile)
252 fFile = nullptr;
253}
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
#define gROOT
Definition TROOT.h:406
char * Compress(const char *str)
Remove all blanks from the string str.
Definition TString.cxx:2524
Small helper to keep current directory context.
Definition TDirectory.h:52
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TFile * GetFile() const
Definition TDirectory.h:174
void GetObject(const char *namecycle, T *&ptr)
Get an object with proper type checking.
Definition TDirectory.h:166
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
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:3997
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
Get the actual TTree name of the friend.
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:79
TDirectory * GetDirectory() const
Definition TTree.h:459
virtual TTree * GetTree() const
Definition TTree.h:514
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:7929
virtual void RemoveExternalFriend(TFriendElement *)
Removes external friend.
Definition TTree.cxx:7940
Definition file.py:1
Definition tree.py:1