Logo ROOT   master
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 
15 A TFriendElement TF describes a TTree object TF in a file.
16 When a TFriendElement TF is added to the the list of friends of an
17 existing TTree T, any variable from TF can be referenced in a query
18 to T.
19 
20 To add a TFriendElement to an existing TTree T, do:
21 ~~~ {.cpp}
22  T.AddFriend("friendTreename","friendTreeFile");
23 ~~~
24 See 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;
43  fOwnFile = kFALSE;
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 
53 TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
54  :TNamed(treename,filename)
55 {
56  fFile = 0;
57  fTree = 0;
58  fOwnFile = kTRUE;
59  fParentTree = tree;
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;
86  fOwnFile = kFALSE;
87  fParentTree = tree;
88  fTreeName = treename;
90  && fParentTree->GetDirectory()->GetFile() == fFile) {
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 
111 TFriendElement::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;
124  fOwnFile = kFALSE;
125  fParentTree = tree;
126  if (fTree) {
127  fTreeName = fTree->GetName();
130  && fParentTree->GetDirectory()->GetFile() == fFile) {
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  // No need to Connect.
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Destructor. Disconnect from the owning tree if needed.
149 
151 {
152  DisConnect();
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Connect file and return TTree.
157 
159 {
160  GetFile();
161  auto treePtr = GetTree();
162  if (!treePtr) MakeZombie(); // ROOT-7007
163  return treePtr;
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// DisConnect file and TTree.
168 
170 {
171  // At this point, if the condition is not meant, fTree is usually already
172  // deleted (hence the need for a local bit describing fTree)
173  if (fTree)
175  if (fOwnFile) delete fFile;
176  fFile = 0;
177  fTree = 0;
178  return 0;
179 }
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// Return pointer to TFile containing this friend TTree.
183 
185 {
186  if (fFile || IsZombie()) return fFile;
187 
188  if (strlen(GetTitle())) {
191  fOwnFile = kTRUE;
192  } else {
194  if (dir) {
195  fFile = dir->GetFile();
196  fOwnFile = kFALSE;
197  }
198  }
199  if (fFile && fFile->IsZombie()) {
200  MakeZombie();
201  delete fFile;
202  fFile = 0;
203  }
204  return fFile;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Return pointer to friend TTree.
209 
211 {
212  if (fTree) {
213  // In cases where the friend was added by the owning chain and the friend is
214  // a chain we recorded the address of the chain but we need to return the address
215  // of the underlying current TTree.
216  if (TestBit(kFromChain))
217  return fTree->GetTree();
218  else
219  return fTree;
220  }
221 
222  if (GetFile()) {
224  if (fTree) return fTree;
225  }
226 
227  // This could be a memory tree or chain
228  fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
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 }
TTree * fParentTree
! pointer to the parent TTree
TTree * fTree
! pointer to the TTree described by this element
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
std::string GetName(const std::string &scope_name)
Definition: Cppyy.cxx:150
virtual void RemoveExternalFriend(TFriendElement *)
Removes external friend.
Definition: TTree.cxx:7803
virtual TTree * GetTree()
Return pointer to friend TTree.
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:3944
char * Compress(const char *str)
Remove all blanks from the string str.
Definition: TString.cxx:2504
const char Option_t
Definition: RtypesCore.h:64
void GetObject(const char *namecycle, T *&ptr)
Get an object with proper type checking.
Definition: TDirectory.h:157
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:53
#define gROOT
Definition: TROOT.h:405
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
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:7792
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TString fTreeName
name of the friend TTree
TFriendElement()
Default constructor for a friend element.
virtual TFile * GetFile() const
Definition: TDirectory.h:165
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
virtual TTree * Connect()
Connect file and return TTree.
virtual TTree * GetTree() const
Definition: TTree.h:508
virtual const char * GetTreeName() const
const Bool_t kFALSE
Definition: RtypesCore.h:90
virtual void ls(Option_t *option="") const
List this friend element.
#define ClassImp(name)
Definition: Rtypes.h:361
Describe directory structure in memory.
Definition: TDirectory.h:40
TDirectory * GetDirectory() const
Definition: TTree.h:453
virtual ~TFriendElement()
Destructor. Disconnect from the owning tree if needed.
TFile * fFile
! pointer to the file containing the friend TTree
virtual TTree * DisConnect()
DisConnect file and TTree.
virtual TFile * GetFile()
Return pointer to TFile containing this friend TTree.
Small helper to keep current directory context.
Definition: TDirectory.h:47
Bool_t fOwnFile
true if file is managed by this class
Definition: file.py:1
A TFriendElement TF describes a TTree object TF in a file.
void MakeZombie()
Definition: TObject.h:49
Definition: tree.py:1
A TTree represents a columnar dataset.
Definition: TTree.h:78
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
const Bool_t kTRUE
Definition: RtypesCore.h:89
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48