Logo ROOT   6.10/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 \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 
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// Default constructor for a friend element.
37 
39 {
40  fFile = 0;
41  fTree = 0;
42  fOwnFile = kFALSE;
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 
52 TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
53  :TNamed(treename,filename)
54 {
55  fFile = 0;
56  fTree = 0;
57  fOwnFile = kTRUE;
58  fParentTree = tree;
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;
85  fOwnFile = kFALSE;
86  fParentTree = tree;
87  fTreeName = treename;
89  && fParentTree->GetDirectory()->GetFile() == fFile) {
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 
110 TFriendElement::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;
123  fOwnFile = kFALSE;
124  fParentTree = tree;
125  if (fTree) {
126  fTreeName = fTree->GetName();
129  && fParentTree->GetDirectory()->GetFile() == fFile) {
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  // No need to Connect.
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Copy constructor
148 
150  TNamed(tfe),
152  fTree(tfe.fTree),
153  fFile(tfe.fFile),
154  fTreeName(tfe.fTreeName),
155  fOwnFile(tfe.fOwnFile)
156 {
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Equal operator
161 
163 {
164  if(this!=&tfe) {
165  TNamed::operator=(tfe);
167  fTree=tfe.fTree;
168  fFile=tfe.fFile;
169  fTreeName=tfe.fTreeName;
170  fOwnFile=tfe.fOwnFile;
171  } return *this;
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// Destructor. Disconnect from the owning tree if needed.
176 
178 {
179  DisConnect();
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// Connect file and return TTree.
184 
186 {
187  GetFile();
188  auto treePtr = GetTree();
189  if (!treePtr) MakeZombie(); // ROOT-7007
190  return treePtr;
191 }
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 /// DisConnect file and TTree.
195 
197 {
198  if (fOwnFile) delete fFile;
199  fFile = 0;
200  fTree = 0;
201  return 0;
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Return pointer to TFile containing this friend TTree.
206 
208 {
209  if (fFile || IsZombie()) return fFile;
210 
211  if (strlen(GetTitle())) {
214  fOwnFile = kTRUE;
215  } else {
217  if (dir) {
218  fFile = dir->GetFile();
219  fOwnFile = kFALSE;
220  }
221  }
222  if (fFile && fFile->IsZombie()) {
223  MakeZombie();
224  delete fFile;
225  fFile = 0;
226  }
227  return fFile;
228 }
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 /// Return pointer to friend TTree.
232 
234 {
235  if (fTree) return fTree;
236 
237  if (GetFile()) {
239  if (fTree) return fTree;
240  }
241 
242  // This could be a memory tree or chain
243  fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
244 
245  return fTree;
246 }
247 
248 ////////////////////////////////////////////////////////////////////////////////
249 /// List this friend element.
250 
252 {
253  printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
254 }
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
virtual TTree * GetTree()
Return pointer to friend TTree.
char * Compress(const char *str)
Remove all blanks from the string str.
Definition: TString.cxx:2538
const char Option_t
Definition: RtypesCore.h:62
bool equal(double d1, double d2, double stol=10000)
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
#define gROOT
Definition: TROOT.h:375
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:3909
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:145
void GetObject(const char *namecycle, T *&ptr)
virtual TTree * Connect()
Connect file and return TTree.
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:42
TFriendElement & operator=(const TFriendElement &)
Equal operator.
virtual const char * GetTreeName() const
const Bool_t kFALSE
Definition: RtypesCore.h:92
virtual void ls(Option_t *option="") const
List this friend element.
#define ClassImp(name)
Definition: Rtypes.h:336
Bool_t IsZombie() const
Definition: TObject.h:122
Describe directory structure in memory.
Definition: TDirectory.h:34
TDirectory * GetDirectory() const
Definition: TTree.h:380
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.
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 object has a header with a name and a title.
Definition: TTree.h:78
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48