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
34
35////////////////////////////////////////////////////////////////////////////////
36/// Default constructor for a friend element.
37
39{
40 fFile = nullptr;
41 fTree = nullptr;
42 fOwnFile = false;
43 fParentTree = nullptr;
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 = nullptr;
56 fTree = nullptr;
57 fOwnFile = true;
58 fParentTree = tree;
59 fTreeName = treename;
60 if (treename && strchr(treename,'=')) {
61 char *temp = Compress(treename);
62 char *equal = strchr(temp,'=');
63 if (!equal) {
64 delete [] temp;
65 return;
66 }
67 *equal=0;
68 fTreeName = equal+1;
69 SetName(temp);
70 delete [] temp;
71 }
72
73 Connect();
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// Create a friend element.
78///
79/// If treename is of the form "a=b", an alias called "a" is created for
80/// treename = "b" by default the alias name is the name of the tree.
81/// The passed TFile is managed by the user (i.e. user must delete the TFile).
82
83TFriendElement::TFriendElement(TTree *tree, const char *treename, TFile *file)
84 :TNamed(treename,file?file->GetName():"")
85{
86 fFile = file;
87 fTree = nullptr;
88 fOwnFile = false;
89 fParentTree = tree;
90 fTreeName = treename;
93 // The friend and the TTree are in the same file, let's not record
94 // the filename.
95 SetTitle("");
96 }
97 if (treename && strchr(treename,'=')) {
98 char *temp = Compress(treename);
99 char *equal = strchr(temp,'=');
100 if (!equal) {
101 delete [] temp;
102 return;
103 }
104 *equal=0;
105 fTreeName = equal+1;
106 SetName(temp);
107 delete [] temp;
108 }
109
110 Connect();
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Create a friend element.
115
116TFriendElement::TFriendElement(TTree *tree, TTree* friendtree, const char *alias)
117 : TNamed(friendtree?friendtree->GetName():"",
118 friendtree
119 ? ( friendtree->GetDirectory()
120 ? ( friendtree->GetDirectory()->GetFile()
121 ? friendtree->GetDirectory()->GetFile()->GetName()
122 : "")
123 : "")
124 : "")
125{
126 fTree = friendtree;
127 fTreeName = "";
128 fFile = nullptr;
129 fOwnFile = false;
130 fParentTree = tree;
131 if (fTree) {
136 // The friend and the TTree are in the same file, let's not record
137 // the filename.
138 SetTitle("");
139 }
140 } else {
141 MakeZombie(); // ROOT-7007
142 }
143 if (alias && strlen(alias)) {
144 char *temp = Compress(alias);
145 SetName(temp);
146 delete [] temp;
147 }
148
149 if (fTree)
151 // No need to Connect.
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Destructor. Disconnect from the owning tree if needed.
156
158{
159 DisConnect();
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// Connect file and return TTree.
164
166{
167 GetFile();
168 auto treePtr = GetTree();
169 if (!treePtr) MakeZombie(); // ROOT-7007
170 return treePtr;
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// DisConnect file and TTree.
175
177{
178 // At this point, if the condition is not meant, fTree is usually already
179 // deleted (hence the need for a local bit describing fTree)
180 if (fTree)
182 if (fOwnFile) delete fFile;
183 fFile = nullptr;
184 fTree = nullptr;
185 return nullptr;
186}
187
188////////////////////////////////////////////////////////////////////////////////
189/// Return pointer to TFile containing this friend TTree.
190
192{
193 if (fFile || IsZombie()) return fFile;
194
195 if (strlen(GetTitle())) {
198 fOwnFile = true;
199 } else {
201 if (dir) {
202 fFile = dir->GetFile();
203 fOwnFile = false;
204 }
205 }
206 if (fFile && fFile->IsZombie()) {
207 MakeZombie();
208 delete fFile;
209 fFile = nullptr;
210 }
211 return fFile;
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// Return pointer to friend TTree.
216
218{
219 if (fTree) {
220 // In cases where the friend was added by the owning chain and the friend is
221 // a chain we recorded the address of the chain but we need to return the address
222 // of the underlying current TTree.
223 if (TestBit(kFromChain))
224 return fTree->GetTree();
225 else
226 return fTree;
227 }
228
229 if (GetFile()) {
231 } else {
232 // This could be a memory tree or chain
233 fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
234 }
235
236 if (fTree)
238
239 return fTree;
240}
241
242////////////////////////////////////////////////////////////////////////////////
243/// List this friend element.
244
246{
247 printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
248}
249
250////////////////////////////////////////////////////////////////////////////////
251/// Forget deleted elements.
252
254{
255 if (delobj == fTree)
256 fTree = nullptr;
257 if (delobj == fFile)
258 fFile = nullptr;
259}
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
#define gROOT
Definition TROOT.h:406
char * Compress(const char *str)
Remove all blanks from the string str.
Definition TString.cxx:2572
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:220
void GetObject(const char *namecycle, T *&ptr)
Get an object with proper type checking.
Definition TDirectory.h:212
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
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:4086
A TFriendElement TF describes a TTree object TF in a file.
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
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
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition TObject.h:153
void MakeZombie()
Definition TObject.h:53
A TTree represents a columnar dataset.
Definition TTree.h:79
TDirectory * GetDirectory() const
Definition TTree.h:462
virtual TTree * GetTree() const
Definition TTree.h:557
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:7947
virtual void RemoveExternalFriend(TFriendElement *)
Removes external friend.
Definition TTree.cxx:7958