ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "TFriendElement.h"
26 #include "TBuffer.h"
27 #include "TTree.h"
28 #include "TFile.h"
29 #include "TROOT.h"
30 
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// Default constructor for a friend element.
35 
37 {
38  fFile = 0;
39  fTree = 0;
40  fOwnFile = kFALSE;
41  fParentTree = 0;
42 }
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Create a friend element.
46 ///
47 /// If treename is of the form "a=b", an alias called "a" is created for
48 /// treename = "b" by default the alias name is the name of the tree.
49 
50 TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
51  :TNamed(treename,filename)
52 {
53  fFile = 0;
54  fTree = 0;
55  fOwnFile = kTRUE;
56  fParentTree = tree;
57  fTreeName = treename;
58  if (treename && strchr(treename,'=')) {
59  char *temp = Compress(treename);
60  char *equal = strchr(temp,'=');
61  if (!equal) return;;
62  *equal=0;
63  fTreeName = equal+1;
64  SetName(temp);
65  delete [] temp;
66  }
67 
68  Connect();
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Create a friend element.
73 ///
74 /// If treename is of the form "a=b", an alias called "a" is created for
75 /// treename = "b" by default the alias name is the name of the tree.
76 /// The passed TFile is managed by the user (i.e. user must delete the TFile).
77 
79  :TNamed(treename,file?file->GetName():"")
80 {
81  fFile = file;
82  fTree = 0;
83  fOwnFile = kFALSE;
84  fParentTree = tree;
85  fTreeName = treename;
87  && fParentTree->GetDirectory()->GetFile() == fFile) {
88  // The friend and the TTree are in the same file, let's not record
89  // the filename.
90  SetTitle("");
91  }
92  if (treename && strchr(treename,'=')) {
93  char *temp = Compress(treename);
94  char *equal = strchr(temp,'=');
95  if (!equal) return;;
96  *equal=0;
97  fTreeName = equal+1;
98  SetName(temp);
99  delete [] temp;
100  }
101 
102  Connect();
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Create a friend element.
107 
108 TFriendElement::TFriendElement(TTree *tree, TTree* friendtree, const char *alias)
109  : TNamed(friendtree?friendtree->GetName():"",
110  friendtree
111  ? ( friendtree->GetDirectory()
112  ? ( friendtree->GetDirectory()->GetFile()
113  ? friendtree->GetDirectory()->GetFile()->GetName()
114  : "")
115  : "")
116  : "")
117 {
118  fTree = friendtree;
119  fTreeName = "";
120  fFile = 0;
121  fOwnFile = kFALSE;
122  fParentTree = tree;
123  if (fTree) {
124  fTreeName = fTree->GetName();
127  && fParentTree->GetDirectory()->GetFile() == fFile) {
128  // The friend and the TTree are in the same file, let's not record
129  // the filename.
130  SetTitle("");
131  }
132  } else {
133  MakeZombie(); // ROOT-7007
134  }
135  if (alias && strlen(alias)) {
136  char *temp = Compress(alias);
137  SetName(temp);
138  delete [] temp;
139  }
140 
141  // No need to Connect.
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Copy constructor
146 
148  TNamed(tfe),
149  fParentTree(tfe.fParentTree),
150  fTree(tfe.fTree),
151  fFile(tfe.fFile),
152  fTreeName(tfe.fTreeName),
153  fOwnFile(tfe.fOwnFile)
154 {
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 /// Equal operator
159 
161 {
162  if(this!=&tfe) {
163  TNamed::operator=(tfe);
165  fTree=tfe.fTree;
166  fFile=tfe.fFile;
167  fTreeName=tfe.fTreeName;
168  fOwnFile=tfe.fOwnFile;
169  } return *this;
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Destructor. Disconnect from the owning tree if needed.
174 
176 {
177  DisConnect();
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Connect file and return TTree.
182 
184 {
185  GetFile();
186  auto treePtr = GetTree();
187  if (!treePtr) MakeZombie(); // ROOT-7007
188  return treePtr;
189 }
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 /// DisConnect file and TTree.
193 
195 {
196  if (fOwnFile) delete fFile;
197  fFile = 0;
198  fTree = 0;
199  return 0;
200 }
201 
202 ////////////////////////////////////////////////////////////////////////////////
203 /// Return pointer to TFile containing this friend TTree.
204 
206 {
207  if (fFile || IsZombie()) return fFile;
208 
209  if (strlen(GetTitle())) {
212  fOwnFile = kTRUE;
213  } else {
215  if (dir) {
216  fFile = dir->GetFile();
217  fOwnFile = kFALSE;
218  }
219  }
220  if (fFile && fFile->IsZombie()) {
221  MakeZombie();
222  delete fFile;
223  fFile = 0;
224  }
225  return fFile;
226 }
227 
228 ////////////////////////////////////////////////////////////////////////////////
229 /// Return pointer to friend TTree.
230 
232 {
233  if (fTree) return fTree;
234 
235  if (GetFile()) {
237  if (fTree) return fTree;
238  }
239 
240  // This could be a memory tree or chain
241  fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
242 
243  return fTree;
244 }
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 /// List this friend element.
248 
250 {
251  printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
252 }
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:2514
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:344
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:155
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:40
TFriendElement & operator=(const TFriendElement &)
Equal operator.
TDirectory * GetDirectory() const
Definition: TTree.h:385
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
tuple tree
Definition: tree.py:24
tuple file
Definition: fildir.py:20
Describe directory structure in memory.
Definition: TDirectory.h:44
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void dir(char *path=0)
Definition: rootalias.C:30
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:98
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