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 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
33
34////////////////////////////////////////////////////////////////////////////////
35/// Default constructor for a friend element.
36
38{
39 fFile = nullptr;
40 fTree = nullptr;
41 fOwnFile = false;
42 fParentTree = nullptr;
43}
44
45////////////////////////////////////////////////////////////////////////////////
46/// Create a friend element.
47///
48/// If treename is of the form "a=b", an alias called "a" is created for
49/// treename = "b" by default the alias name is the name of the tree.
50
51TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
52 :TNamed(treename,filename)
53{
54 fFile = nullptr;
55 fTree = nullptr;
56 fOwnFile = true;
57 fParentTree = tree;
58 fTreeName = treename;
59 if (treename && strchr(treename,'=')) {
60 char *temp = Compress(treename);
61 char *equal = strchr(temp,'=');
62 if (!equal) {
63 delete [] temp;
64 return;
65 }
66 *equal=0;
67 fTreeName = equal+1;
68 SetName(temp);
69 delete [] temp;
70 }
71
72 Connect();
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// Create a friend element.
77///
78/// If treename is of the form "a=b", an alias called "a" is created for
79/// treename = "b" by default the alias name is the name of the tree.
80/// The passed TFile is managed by the user (i.e. user must delete the TFile).
81
82TFriendElement::TFriendElement(TTree *tree, const char *treename, TFile *file)
83 :TNamed(treename,file?file->GetName():"")
84{
85 fFile = file;
86 fTree = nullptr;
87 fOwnFile = false;
88 fParentTree = tree;
89 fTreeName = treename;
90 if (fParentTree && fParentTree->GetDirectory()
91 && fParentTree->GetDirectory()->GetFile() == fFile) {
92 // The friend and the TTree are in the same file, let's not record
93 // the filename.
94 SetTitle("");
95 }
96 if (treename && strchr(treename,'=')) {
97 char *temp = Compress(treename);
98 char *equal = strchr(temp,'=');
99 if (!equal) {
100 delete [] temp;
101 return;
102 }
103 *equal=0;
104 fTreeName = equal+1;
105 SetName(temp);
106 delete [] temp;
107 }
108
109 Connect();
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Create a friend element.
114
115TFriendElement::TFriendElement(TTree *tree, TTree* friendtree, const char *alias)
116 : TNamed(friendtree?friendtree->GetName():"",
117 friendtree
118 ? ( friendtree->GetDirectory()
119 ? ( friendtree->GetDirectory()->GetFile()
120 ? friendtree->GetDirectory()->GetFile()->GetName()
121 : "")
122 : "")
123 : "")
124{
125 fTree = friendtree;
126 fTreeName = "";
127 fFile = nullptr;
128 fOwnFile = false;
129 fParentTree = tree;
130 if (fTree) {
131 fTreeName = fTree->GetName();
132 if (fTree->GetDirectory()) fFile = fTree->GetDirectory()->GetFile();
133 if (fParentTree && fParentTree->GetDirectory()
134 && fParentTree->GetDirectory()->GetFile() == fFile) {
135 // The friend and the TTree are in the same file, let's not record
136 // the filename.
137 SetTitle("");
138 }
139 } else {
140 MakeZombie(); // ROOT-7007
141 }
142 if (alias && strlen(alias)) {
143 char *temp = Compress(alias);
144 SetName(temp);
145 delete [] temp;
146 }
147
148 if (fTree)
149 fTree->RegisterExternalFriend(this);
150 // No need to Connect.
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Destructor. Disconnect from the owning tree if needed.
155
160
161////////////////////////////////////////////////////////////////////////////////
162/// Connect file and return TTree.
163
165{
166 GetFile();
167 auto treePtr = GetTree();
168 if (!treePtr) MakeZombie(); // ROOT-7007
169 return treePtr;
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// DisConnect file and TTree.
174
176{
177 // At this point, if the condition is not meant, fTree is usually already
178 // deleted (hence the need for a local bit describing fTree)
179 if (fTree)
180 fTree->RemoveExternalFriend(this);
181 if (fOwnFile) delete fFile;
182 fFile = nullptr;
183 fTree = nullptr;
184 return nullptr;
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// Return pointer to TFile containing this friend TTree.
189
191{
192 if (fFile || IsZombie()) return fFile;
193
194 if (strlen(GetTitle())) {
197 fOwnFile = true;
198 } else {
199 TDirectory *dir = fParentTree->GetDirectory();
200 if (dir) {
201 fFile = dir->GetFile();
202 fOwnFile = false;
203 }
204 }
205 if (fFile && fFile->IsZombie()) {
206 MakeZombie();
207 delete fFile;
208 fFile = nullptr;
209 }
210 return fFile;
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// Return pointer to friend TTree.
215
217{
218 if (fTree) {
219 // In cases where the friend was added by the owning chain and the friend is
220 // a chain we recorded the address of the chain but we need to return the address
221 // of the underlying current TTree.
222 if (TestBit(kFromChain))
223 return fTree->GetTree();
224 else
225 return fTree;
226 }
227
228 if (GetFile()) {
229 fFile->GetObject(GetTreeName(),fTree);
230 } else {
231 // This could be a memory tree or chain
232 fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
233 }
234
235 if (fTree)
236 fTree->RegisterExternalFriend(this);
237
238 return fTree;
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// List this friend element.
243
245{
246 printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
247}
248
249////////////////////////////////////////////////////////////////////////////////
250/// Forget deleted elements.
251
253{
254 if (delobj == fTree)
255 fTree = nullptr;
256 if (delobj == fFile)
257 fFile = nullptr;
258}
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
if(name) objname
#define gROOT
Definition TROOT.h:417
char * Compress(const char *str)
Remove all blanks from the string str.
Definition TString.cxx:2579
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TFile * GetFile() const
Definition TDirectory.h:221
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
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:3787
TFriendElement()
Default constructor for a friend element.
void ls(Option_t *option="") const override
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.
TTree * fTree
! pointer to the TTree described by this element
virtual TTree * GetTree()
Return pointer to friend TTree.
void RecursiveRemove(TObject *obj) override
Forget deleted elements.
virtual TFile * GetFile()
Return pointer to TFile containing this friend TTree.
virtual TTree * Connect()
Connect file and return TTree.
~TFriendElement() override
Destructor. Disconnect from the owning tree if needed.
virtual TTree * DisConnect()
DisConnect file and TTree.
TTree * fParentTree
! pointer to the parent TTree
bool fOwnFile
true if file is managed by this class
TString fTreeName
name of the friend TTree
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TNamed()
Definition TNamed.h:38
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
void MakeZombie()
Definition TObject.h:55
TObject()
TObject constructor.
Definition TObject.h:259
Bool_t IsZombie() const
Definition TObject.h:161
A TTree represents a columnar dataset.
Definition TTree.h:89