TFriendElement.cxx

Go to the documentation of this file.
00001 // @(#)root/tree:$Id: TFriendElement.cxx 35378 2010-09-17 13:55:42Z brun $
00002 // Author: Rene Brun   07/04/2001
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TFriendElement                                                       //
00015 //                                                                      //
00016 // A TFriendElement TF describes a TTree object TF in a file.           //
00017 // When a TFriendElement TF is added to the the list of friends of an   //
00018 // existing TTree T, any variable from TF can be referenced in a query  //
00019 // to T.                                                                //
00020 //                                                                      //
00021 // To add a TFriendElement to an existing TTree T, do:                  //
00022 //       T.AddFriend("friendTreename","friendTreeFile");                //
00023 //                                                                      //
00024 //  See TTree::AddFriend for more information.                          //
00025 //                                                                      //
00026 //////////////////////////////////////////////////////////////////////////
00027 
00028 #include "TTree.h"
00029 #include "TFriendElement.h"
00030 #include "TFile.h"
00031 #include "TROOT.h"
00032 
00033 R__EXTERN TTree *gTree;
00034 
00035 ClassImp(TFriendElement)
00036 
00037 //______________________________________________________________________________
00038 TFriendElement::TFriendElement() : TNamed()
00039 {
00040 //*-*-*-*-*-*Default constructor for a friend element*-*-*-*-*-*-*-*-*-*-*-*-*
00041 //*-*        =======================================
00042 
00043    fFile       = 0;
00044    fTree       = 0;
00045    fOwnFile    = kFALSE;
00046    fParentTree = gTree;
00047 }
00048 
00049 //______________________________________________________________________________
00050 TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
00051     :TNamed(treename,filename)
00052 {
00053 //*-*-*-*-*-*-*-*-*-*-*-*-*Create a friend element*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00054 //*-*                      ======================
00055 //
00056 // If treename is of the form "a=b", an alias called "a" is created for
00057 // treename = "b" by default the alias name is the name of the tree.
00058 
00059    fFile       = 0;
00060    fTree       = 0;
00061    fOwnFile    = kTRUE;
00062    fParentTree = tree;
00063    fTreeName   = treename;
00064    if (strchr(treename,'=')) {
00065       char *temp = Compress(treename);
00066       char *equal = strchr(temp,'=');
00067       if (!equal) return;;
00068       *equal=0;
00069       fTreeName = equal+1;
00070       SetName(temp);
00071       delete [] temp;
00072    }
00073 
00074    Connect();
00075 }
00076 
00077 //______________________________________________________________________________
00078 TFriendElement::TFriendElement(TTree *tree, const char *treename, TFile *file)
00079     :TNamed(treename,file?file->GetName():"")
00080 {
00081 //*-*-*-*-*-*-*-*-*-*-*-*-*Create a friend element*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00082 //*-*                      ======================
00083 //
00084 // If treename is of the form "a=b", an alias called "a" is created for
00085 // treename = "b" by default the alias name is the name of the tree.
00086 // The passed TFile is managed by the user (i.e. user must delete the TFile).
00087 
00088    fFile       = file;
00089    fTree       = 0;
00090    fOwnFile    = kFALSE;
00091    fParentTree = tree;
00092    fTreeName   = treename;
00093    if (fParentTree && fParentTree->GetDirectory() 
00094        && fParentTree->GetDirectory()->GetFile() == fFile) {
00095       // The friend and the TTree are in the same file, let's not record
00096       // the filename.
00097       SetTitle("");
00098    }
00099    if (strchr(treename,'=')) {
00100       char *temp = Compress(treename);
00101       char *equal = strchr(temp,'=');
00102       if (!equal) return;;
00103       *equal=0;
00104       fTreeName = equal+1;
00105       SetName(temp);
00106       delete [] temp;
00107    }
00108 
00109    Connect();
00110 }
00111 
00112 //______________________________________________________________________________
00113 TFriendElement::TFriendElement(TTree *tree, TTree* friendtree, const char *alias)
00114    : TNamed(friendtree?friendtree->GetName():"",
00115             friendtree
00116             ? (   friendtree->GetDirectory()
00117                   ? (    friendtree->GetDirectory()->GetFile()
00118                          ? friendtree->GetDirectory()->GetFile()->GetName()
00119                          :  "")
00120                   : "")
00121             :  "")
00122 {
00123    // Create a friend element.
00124 
00125    fTree       = friendtree;
00126    fTreeName   = "";
00127    fFile       = 0;
00128    fOwnFile    = kFALSE;
00129    fParentTree = tree;
00130    if (fTree) {
00131       fTreeName   = fTree->GetName();
00132       if (fTree->GetDirectory()) fFile = fTree->GetDirectory()->GetFile();
00133       if (fParentTree && fParentTree->GetDirectory() 
00134           && fParentTree->GetDirectory()->GetFile() == fFile) {
00135          // The friend and the TTree are in the same file, let's not record
00136          // the filename.
00137          SetTitle("");
00138       }
00139    }
00140    if (alias && strlen(alias)) {
00141       char *temp = Compress(alias);
00142       SetName(temp);
00143       delete [] temp;
00144    }
00145 
00146    // No need to Connect.
00147 }
00148 
00149 //______________________________________________________________________________
00150 TFriendElement::TFriendElement(const TFriendElement& tfe) : 
00151    TNamed(tfe),
00152    fParentTree(tfe.fParentTree),
00153    fTree(tfe.fTree),
00154    fFile(tfe.fFile),
00155    fTreeName(tfe.fTreeName),
00156    fOwnFile(tfe.fOwnFile)
00157 { 
00158    // Copy constructor
00159 }
00160 
00161 //______________________________________________________________________________
00162 TFriendElement& TFriendElement::operator=(const TFriendElement& tfe) 
00163 {
00164    // Equal operator
00165    if(this!=&tfe) {
00166       TNamed::operator=(tfe);
00167       fParentTree=tfe.fParentTree;
00168       fTree=tfe.fTree;
00169       fFile=tfe.fFile;
00170       fTreeName=tfe.fTreeName;
00171       fOwnFile=tfe.fOwnFile;
00172    } return *this;
00173 }
00174 
00175 //______________________________________________________________________________
00176 TFriendElement::~TFriendElement()
00177 {
00178    // Destructor.  Disconnect from the owning tree if needed.
00179 
00180    DisConnect();
00181 }
00182 
00183 //_______________________________________________________________________
00184 TTree *TFriendElement::Connect()
00185 {
00186    // Connect file and return TTree.
00187 
00188    GetFile();
00189    return GetTree();
00190 }
00191 
00192 //_______________________________________________________________________
00193 TTree *TFriendElement::DisConnect()
00194 {
00195    // DisConnect file and TTree.
00196 
00197    if (fOwnFile) delete fFile;
00198    fFile = 0;
00199    fTree = 0;
00200    return 0;
00201 }
00202 
00203 //_______________________________________________________________________
00204 TFile *TFriendElement::GetFile()
00205 {
00206    // Return pointer to TFile containing this friend TTree.
00207 
00208    if (fFile || IsZombie()) return fFile;
00209 
00210 
00211    if (strlen(GetTitle())) {
00212       TDirectory::TContext ctxt(gDirectory, 0);
00213       fFile = TFile::Open(GetTitle());
00214       fOwnFile = kTRUE;
00215    } else {
00216       TDirectory *dir = fParentTree->GetDirectory();
00217       if (dir) {
00218          fFile = dir->GetFile();
00219          fOwnFile = kFALSE;
00220       }
00221    }
00222    if (fFile && fFile->IsZombie()) {
00223       MakeZombie();
00224       delete fFile;
00225       fFile = 0;
00226    }
00227    return fFile;
00228 }
00229 
00230 //_______________________________________________________________________
00231 TTree *TFriendElement::GetTree()
00232 {
00233    // Return pointer to friend TTree.
00234 
00235    if (fTree) return fTree;
00236 
00237    if (GetFile()) {
00238       fFile->GetObject(GetTreeName(),fTree);
00239       if (fTree) return fTree;
00240    }
00241 
00242    // This could be a memory tree or chain
00243    fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
00244 
00245    return fTree;
00246 }
00247 
00248 //_______________________________________________________________________
00249 void TFriendElement::ls(Option_t *) const
00250 {
00251    // List this friend element.
00252 
00253    printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
00254 }

Generated on Tue Jul 5 15:34:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1