ROOT  6.06/09
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 A TFriendElement TF describes a TTree object TF in a file.
14 When a TFriendElement TF is added to the the list of friends of an
15 existing TTree T, any variable from TF can be referenced in a query
16 to T.
17 
18 To add a TFriendElement to an existing TTree T, do:
19 ~~~ {.cpp}
20  T.AddFriend("friendTreename","friendTreeFile");
21 ~~~
22 See TTree::AddFriend for more information.
23 */
24 
25 #include "TTree.h"
26 #include "TFriendElement.h"
27 #include "TFile.h"
28 #include "TROOT.h"
29 
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 /// Default constructor for a friend element.
34 
36 {
37  fFile = 0;
38  fTree = 0;
39  fOwnFile = kFALSE;
40  fParentTree = 0;
41 }
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Create a friend element.
45 ///
46 /// If treename is of the form "a=b", an alias called "a" is created for
47 /// treename = "b" by default the alias name is the name of the tree.
48 
49 TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
50  :TNamed(treename,filename)
51 {
52  fFile = 0;
53  fTree = 0;
54  fOwnFile = kTRUE;
55  fParentTree = tree;
56  fTreeName = treename;
57  if (treename && strchr(treename,'=')) {
58  char *temp = Compress(treename);
59  char *equal = strchr(temp,'=');
60  if (!equal) return;;
61  *equal=0;
62  fTreeName = equal+1;
63  SetName(temp);
64  delete [] temp;
65  }
66 
67  Connect();
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Create a friend element.
72 ///
73 /// If treename is of the form "a=b", an alias called "a" is created for
74 /// treename = "b" by default the alias name is the name of the tree.
75 /// The passed TFile is managed by the user (i.e. user must delete the TFile).
76 
77 TFriendElement::TFriendElement(TTree *tree, const char *treename, TFile *file)
78  :TNamed(treename,file?file->GetName():"")
79 {
80  fFile = file;
81  fTree = 0;
82  fOwnFile = kFALSE;
83  fParentTree = tree;
84  fTreeName = treename;
86  && fParentTree->GetDirectory()->GetFile() == fFile) {
87  // The friend and the TTree are in the same file, let's not record
88  // the filename.
89  SetTitle("");
90  }
91  if (treename && strchr(treename,'=')) {
92  char *temp = Compress(treename);
93  char *equal = strchr(temp,'=');
94  if (!equal) return;;
95  *equal=0;
96  fTreeName = equal+1;
97  SetName(temp);
98  delete [] temp;
99  }
100 
101  Connect();
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Create a friend element.
106 
107 TFriendElement::TFriendElement(TTree *tree, TTree* friendtree, const char *alias)
108  : TNamed(friendtree?friendtree->GetName():"",
109  friendtree
110  ? ( friendtree->GetDirectory()
111  ? ( friendtree->GetDirectory()->GetFile()
112  ? friendtree->GetDirectory()->GetFile()->GetName()
113  : "")
114  : "")
115  : "")
116 {
117  fTree = friendtree;
118  fTreeName = "";
119  fFile = 0;
120  fOwnFile = kFALSE;
121  fParentTree = tree;
122  if (fTree) {
123  fTreeName = fTree->GetName();
126  && fParentTree->GetDirectory()->GetFile() == fFile) {
127  // The friend and the TTree are in the same file, let's not record
128  // the filename.
129  SetTitle("");
130  }
131  } else {
132  MakeZombie(); // ROOT-7007
133  }
134  if (alias && strlen(alias)) {
135  char *temp = Compress(alias);
136  SetName(temp);
137  delete [] temp;
138  }
139 
140  // No need to Connect.
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Copy constructor
145 
147  TNamed(tfe),
148  fParentTree(tfe.fParentTree),
149  fTree(tfe.fTree),
150  fFile(tfe.fFile),
151  fTreeName(tfe.fTreeName),
152  fOwnFile(tfe.fOwnFile)
153 {
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Equal operator
158 
160 {
161  if(this!=&tfe) {
162  TNamed::operator=(tfe);
164  fTree=tfe.fTree;
165  fFile=tfe.fFile;
166  fTreeName=tfe.fTreeName;
167  fOwnFile=tfe.fOwnFile;
168  } return *this;
169 }
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Destructor. Disconnect from the owning tree if needed.
173 
175 {
176  DisConnect();
177 }
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Connect file and return TTree.
181 
183 {
184  GetFile();
185  auto treePtr = GetTree();
186  if (!treePtr) MakeZombie(); // ROOT-7007
187  return treePtr;
188 }
189 
190 ////////////////////////////////////////////////////////////////////////////////
191 /// DisConnect file and TTree.
192 
194 {
195  if (fOwnFile) delete fFile;
196  fFile = 0;
197  fTree = 0;
198  return 0;
199 }
200 
201 ////////////////////////////////////////////////////////////////////////////////
202 /// Return pointer to TFile containing this friend TTree.
203 
205 {
206  if (fFile || IsZombie()) return fFile;
207 
208  if (strlen(GetTitle())) {
211  fOwnFile = kTRUE;
212  } else {
214  if (dir) {
215  fFile = dir->GetFile();
216  fOwnFile = kFALSE;
217  }
218  }
219  if (fFile && fFile->IsZombie()) {
220  MakeZombie();
221  delete fFile;
222  fFile = 0;
223  }
224  return fFile;
225 }
226 
227 ////////////////////////////////////////////////////////////////////////////////
228 /// Return pointer to friend TTree.
229 
231 {
232  if (fTree) return fTree;
233 
234  if (GetFile()) {
236  if (fTree) return fTree;
237  }
238 
239  // This could be a memory tree or chain
240  fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
241 
242  return fTree;
243 }
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 /// List this friend element.
247 
249 {
250  printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
251 }
TTree * fParentTree
TTree * fTree
pointer to the parent TTree
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual TTree * GetTree()
Return pointer to friend TTree.
char * Compress(const char *str)
Remove all blanks from the string str.
Definition: TString.cxx:2527
const char Option_t
Definition: RtypesCore.h:62
bool equal(double d1, double d2, double stol=10000)
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
static const char * filename()
#define gROOT
Definition: TROOT.h:340
Bool_t IsZombie() const
Definition: TObject.h:141
const Bool_t kFALSE
Definition: Rtypes.h:92
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3851
virtual const char * GetTreeName() const
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
TString fTreeName
pointer to the file containing the friend TTree
void GetObject(const char *namecycle, T *&ptr)
virtual TTree * Connect()
Connect file and return TTree.
virtual TFile * GetFile() const
Definition: TDirectory.h:152
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:40
TFriendElement & operator=(const TFriendElement &)
Equal operator.
TDirectory * GetDirectory() const
Definition: TTree.h:381
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
Describe directory structure in memory.
Definition: TDirectory.h:41
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
virtual ~TFriendElement()
Destructor. Disconnect from the owning tree if needed.
TFile * fFile
pointer to the TTree described by this element
virtual TTree * DisConnect()
DisConnect file and TTree.
virtual void ls(Option_t *option="") const
List this friend element.
virtual TFile * GetFile()
Return pointer to TFile containing this friend TTree.
A TFriendElement TF describes a TTree object TF in a file.
void MakeZombie()
Definition: TObject.h:68
A TTree object has a header with a name and a title.
Definition: TTree.h:94
ClassImp(TFriendElement) TFriendElement
Default constructor for a friend element.
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152