ROOT logo
// @(#)root/tree:$Id: TFriendElement.cxx 25057 2008-08-05 18:34:34Z pcanal $
// Author: Rene Brun   07/04/2001

/*************************************************************************
 * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TFriendElement                                                       //
//                                                                      //
// A TFriendElement TF describes a TTree object TF in a file.           //
// When a TFriendElement TF is added to the the list of friends of an   //
// existing TTree T, any variable from TF can be referenced in a query  //
// to T.                                                                //
//                                                                      //
// To add a TFriendElement to an existing TTree T, do:                  //
//       T.AddFriend("friendTreename","friendTreeFile");                //
//                                                                      //
//  See TTree::AddFriend for more information.                          //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TTree.h"
#include "TFriendElement.h"
#include "TFile.h"
#include "TROOT.h"

R__EXTERN TTree *gTree;

ClassImp(TFriendElement)

//______________________________________________________________________________
TFriendElement::TFriendElement() : TNamed()
{
//*-*-*-*-*-*Default constructor for a friend element*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*        =======================================

   fFile       = 0;
   fTree       = 0;
   fOwnFile    = kFALSE;
   fParentTree = gTree;
}

//______________________________________________________________________________
TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
    :TNamed(treename,filename)
{
//*-*-*-*-*-*-*-*-*-*-*-*-*Create a friend element*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ======================
//
// If treename is of the form "a=b", an alias called "a" is created for
// treename = "b" by default the alias name is the name of the tree.

   fFile       = 0;
   fTree       = 0;
   fOwnFile    = kTRUE;
   fParentTree = tree;
   fTreeName   = treename;
   if (strchr(treename,'=')) {
      char *temp = Compress(treename);
      char *equal = strchr(temp,'=');
      *equal=0;
      fTreeName = equal+1;
      SetName(temp);
      delete [] temp;
   }

   Connect();
}

//______________________________________________________________________________
TFriendElement::TFriendElement(TTree *tree, const char *treename, TFile *file)
    :TNamed(treename,file?file->GetName():"")
{
//*-*-*-*-*-*-*-*-*-*-*-*-*Create a friend element*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ======================
//
// If treename is of the form "a=b", an alias called "a" is created for
// treename = "b" by default the alias name is the name of the tree.
// The passed TFile is managed by the user (i.e. user must delete the TFile).

   fFile       = file;
   fTree       = 0;
   fOwnFile    = kFALSE;
   fParentTree = tree;
   fTreeName   = treename;
   if (fParentTree && fParentTree->GetDirectory() 
       && fParentTree->GetDirectory()->GetFile() == fFile) {
      // The friend and the TTree are in the same file, let's not record
      // the filename.
      SetTitle("");
   }
   if (strchr(treename,'=')) {
      char *temp = Compress(treename);
      char *equal = strchr(temp,'=');
      *equal=0;
      fTreeName = equal+1;
      SetName(temp);
      delete [] temp;
   }

   Connect();
}

//______________________________________________________________________________
TFriendElement::TFriendElement(TTree *tree, TTree* friendtree, const char *alias)
   : TNamed(friendtree?friendtree->GetName():"",
            friendtree
            ? (   friendtree->GetDirectory()
                  ? (    friendtree->GetDirectory()->GetFile()
                         ? friendtree->GetDirectory()->GetFile()->GetName()
                         :  "")
                  : "")
            :  "")
{
   // Create a friend element.

   fTree       = friendtree;
   fTreeName   = "";
   fFile       = 0;
   fOwnFile    = kFALSE;
   fParentTree = tree;
   if (fTree) {
      fTreeName   = fTree->GetName();
      if (fTree->GetDirectory()) fFile = fTree->GetDirectory()->GetFile();
      if (fParentTree && fParentTree->GetDirectory() 
          && fParentTree->GetDirectory()->GetFile() == fFile) {
         // The friend and the TTree are in the same file, let's not record
         // the filename.
         SetTitle("");
      }
   }
   if (alias && strlen(alias)) {
      char *temp = Compress(alias);
      SetName(temp);
      delete [] temp;
   }

   // No need to Connect.
}

//______________________________________________________________________________
TFriendElement::TFriendElement(const TFriendElement& tfe) : 
   TNamed(tfe),
   fParentTree(tfe.fParentTree),
   fTree(tfe.fTree),
   fFile(tfe.fFile),
   fTreeName(tfe.fTreeName),
   fOwnFile(tfe.fOwnFile)
{ 
   // Copy constructor
}

//______________________________________________________________________________
TFriendElement& TFriendElement::operator=(const TFriendElement& tfe) 
{
   // Equal operator
   if(this!=&tfe) {
      TNamed::operator=(tfe);
      fParentTree=tfe.fParentTree;
      fTree=tfe.fTree;
      fFile=tfe.fFile;
      fTreeName=tfe.fTreeName;
      fOwnFile=tfe.fOwnFile;
   } return *this;
}

//______________________________________________________________________________
TFriendElement::~TFriendElement()
{
   // Destructor.  Disconnect from the owning tree if needed.

   DisConnect();
}

//_______________________________________________________________________
TTree *TFriendElement::Connect()
{
   // Connect file and return TTree.

   GetFile();
   return GetTree();
}

//_______________________________________________________________________
TTree *TFriendElement::DisConnect()
{
   // DisConnect file and TTree.

   if (fOwnFile) delete fFile;
   fFile = 0;
   fTree = 0;
   return 0;
}

//_______________________________________________________________________
TFile *TFriendElement::GetFile()
{
   // Return pointer to TFile containing this friend TTree.

   if (fFile || IsZombie()) return fFile;


   if (strlen(GetTitle())) {
      TDirectory::TContext ctxt(gDirectory, 0);
      fFile = TFile::Open(GetTitle());
      fOwnFile = kTRUE;
   } else {
      TDirectory *dir = fParentTree->GetDirectory();
      if (dir) {
         fFile = dir->GetFile();
         fOwnFile = kFALSE;
      }
   }
   if (fFile && fFile->IsZombie()) {
      MakeZombie();
      delete fFile;
      fFile = 0;
   }
   return fFile;
}

//_______________________________________________________________________
TTree *TFriendElement::GetTree()
{
   // Return pointer to friend TTree.

   if (fTree) return fTree;

   if (GetFile()) {
      fFile->GetObject(GetTreeName(),fTree);
      if (fTree) return fTree;
   }

   // This could be a memory tree or chain
   fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );

   return fTree;
}

//_______________________________________________________________________
void TFriendElement::ls(Option_t *) const
{
   // List this friend element.

   printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
}
 TFriendElement.cxx:1
 TFriendElement.cxx:2
 TFriendElement.cxx:3
 TFriendElement.cxx:4
 TFriendElement.cxx:5
 TFriendElement.cxx:6
 TFriendElement.cxx:7
 TFriendElement.cxx:8
 TFriendElement.cxx:9
 TFriendElement.cxx:10
 TFriendElement.cxx:11
 TFriendElement.cxx:12
 TFriendElement.cxx:13
 TFriendElement.cxx:14
 TFriendElement.cxx:15
 TFriendElement.cxx:16
 TFriendElement.cxx:17
 TFriendElement.cxx:18
 TFriendElement.cxx:19
 TFriendElement.cxx:20
 TFriendElement.cxx:21
 TFriendElement.cxx:22
 TFriendElement.cxx:23
 TFriendElement.cxx:24
 TFriendElement.cxx:25
 TFriendElement.cxx:26
 TFriendElement.cxx:27
 TFriendElement.cxx:28
 TFriendElement.cxx:29
 TFriendElement.cxx:30
 TFriendElement.cxx:31
 TFriendElement.cxx:32
 TFriendElement.cxx:33
 TFriendElement.cxx:34
 TFriendElement.cxx:35
 TFriendElement.cxx:36
 TFriendElement.cxx:37
 TFriendElement.cxx:38
 TFriendElement.cxx:39
 TFriendElement.cxx:40
 TFriendElement.cxx:41
 TFriendElement.cxx:42
 TFriendElement.cxx:43
 TFriendElement.cxx:44
 TFriendElement.cxx:45
 TFriendElement.cxx:46
 TFriendElement.cxx:47
 TFriendElement.cxx:48
 TFriendElement.cxx:49
 TFriendElement.cxx:50
 TFriendElement.cxx:51
 TFriendElement.cxx:52
 TFriendElement.cxx:53
 TFriendElement.cxx:54
 TFriendElement.cxx:55
 TFriendElement.cxx:56
 TFriendElement.cxx:57
 TFriendElement.cxx:58
 TFriendElement.cxx:59
 TFriendElement.cxx:60
 TFriendElement.cxx:61
 TFriendElement.cxx:62
 TFriendElement.cxx:63
 TFriendElement.cxx:64
 TFriendElement.cxx:65
 TFriendElement.cxx:66
 TFriendElement.cxx:67
 TFriendElement.cxx:68
 TFriendElement.cxx:69
 TFriendElement.cxx:70
 TFriendElement.cxx:71
 TFriendElement.cxx:72
 TFriendElement.cxx:73
 TFriendElement.cxx:74
 TFriendElement.cxx:75
 TFriendElement.cxx:76
 TFriendElement.cxx:77
 TFriendElement.cxx:78
 TFriendElement.cxx:79
 TFriendElement.cxx:80
 TFriendElement.cxx:81
 TFriendElement.cxx:82
 TFriendElement.cxx:83
 TFriendElement.cxx:84
 TFriendElement.cxx:85
 TFriendElement.cxx:86
 TFriendElement.cxx:87
 TFriendElement.cxx:88
 TFriendElement.cxx:89
 TFriendElement.cxx:90
 TFriendElement.cxx:91
 TFriendElement.cxx:92
 TFriendElement.cxx:93
 TFriendElement.cxx:94
 TFriendElement.cxx:95
 TFriendElement.cxx:96
 TFriendElement.cxx:97
 TFriendElement.cxx:98
 TFriendElement.cxx:99
 TFriendElement.cxx:100
 TFriendElement.cxx:101
 TFriendElement.cxx:102
 TFriendElement.cxx:103
 TFriendElement.cxx:104
 TFriendElement.cxx:105
 TFriendElement.cxx:106
 TFriendElement.cxx:107
 TFriendElement.cxx:108
 TFriendElement.cxx:109
 TFriendElement.cxx:110
 TFriendElement.cxx:111
 TFriendElement.cxx:112
 TFriendElement.cxx:113
 TFriendElement.cxx:114
 TFriendElement.cxx:115
 TFriendElement.cxx:116
 TFriendElement.cxx:117
 TFriendElement.cxx:118
 TFriendElement.cxx:119
 TFriendElement.cxx:120
 TFriendElement.cxx:121
 TFriendElement.cxx:122
 TFriendElement.cxx:123
 TFriendElement.cxx:124
 TFriendElement.cxx:125
 TFriendElement.cxx:126
 TFriendElement.cxx:127
 TFriendElement.cxx:128
 TFriendElement.cxx:129
 TFriendElement.cxx:130
 TFriendElement.cxx:131
 TFriendElement.cxx:132
 TFriendElement.cxx:133
 TFriendElement.cxx:134
 TFriendElement.cxx:135
 TFriendElement.cxx:136
 TFriendElement.cxx:137
 TFriendElement.cxx:138
 TFriendElement.cxx:139
 TFriendElement.cxx:140
 TFriendElement.cxx:141
 TFriendElement.cxx:142
 TFriendElement.cxx:143
 TFriendElement.cxx:144
 TFriendElement.cxx:145
 TFriendElement.cxx:146
 TFriendElement.cxx:147
 TFriendElement.cxx:148
 TFriendElement.cxx:149
 TFriendElement.cxx:150
 TFriendElement.cxx:151
 TFriendElement.cxx:152
 TFriendElement.cxx:153
 TFriendElement.cxx:154
 TFriendElement.cxx:155
 TFriendElement.cxx:156
 TFriendElement.cxx:157
 TFriendElement.cxx:158
 TFriendElement.cxx:159
 TFriendElement.cxx:160
 TFriendElement.cxx:161
 TFriendElement.cxx:162
 TFriendElement.cxx:163
 TFriendElement.cxx:164
 TFriendElement.cxx:165
 TFriendElement.cxx:166
 TFriendElement.cxx:167
 TFriendElement.cxx:168
 TFriendElement.cxx:169
 TFriendElement.cxx:170
 TFriendElement.cxx:171
 TFriendElement.cxx:172
 TFriendElement.cxx:173
 TFriendElement.cxx:174
 TFriendElement.cxx:175
 TFriendElement.cxx:176
 TFriendElement.cxx:177
 TFriendElement.cxx:178
 TFriendElement.cxx:179
 TFriendElement.cxx:180
 TFriendElement.cxx:181
 TFriendElement.cxx:182
 TFriendElement.cxx:183
 TFriendElement.cxx:184
 TFriendElement.cxx:185
 TFriendElement.cxx:186
 TFriendElement.cxx:187
 TFriendElement.cxx:188
 TFriendElement.cxx:189
 TFriendElement.cxx:190
 TFriendElement.cxx:191
 TFriendElement.cxx:192
 TFriendElement.cxx:193
 TFriendElement.cxx:194
 TFriendElement.cxx:195
 TFriendElement.cxx:196
 TFriendElement.cxx:197
 TFriendElement.cxx:198
 TFriendElement.cxx:199
 TFriendElement.cxx:200
 TFriendElement.cxx:201
 TFriendElement.cxx:202
 TFriendElement.cxx:203
 TFriendElement.cxx:204
 TFriendElement.cxx:205
 TFriendElement.cxx:206
 TFriendElement.cxx:207
 TFriendElement.cxx:208
 TFriendElement.cxx:209
 TFriendElement.cxx:210
 TFriendElement.cxx:211
 TFriendElement.cxx:212
 TFriendElement.cxx:213
 TFriendElement.cxx:214
 TFriendElement.cxx:215
 TFriendElement.cxx:216
 TFriendElement.cxx:217
 TFriendElement.cxx:218
 TFriendElement.cxx:219
 TFriendElement.cxx:220
 TFriendElement.cxx:221
 TFriendElement.cxx:222
 TFriendElement.cxx:223
 TFriendElement.cxx:224
 TFriendElement.cxx:225
 TFriendElement.cxx:226
 TFriendElement.cxx:227
 TFriendElement.cxx:228
 TFriendElement.cxx:229
 TFriendElement.cxx:230
 TFriendElement.cxx:231
 TFriendElement.cxx:232
 TFriendElement.cxx:233
 TFriendElement.cxx:234
 TFriendElement.cxx:235
 TFriendElement.cxx:236
 TFriendElement.cxx:237
 TFriendElement.cxx:238
 TFriendElement.cxx:239
 TFriendElement.cxx:240
 TFriendElement.cxx:241
 TFriendElement.cxx:242
 TFriendElement.cxx:243
 TFriendElement.cxx:244
 TFriendElement.cxx:245
 TFriendElement.cxx:246
 TFriendElement.cxx:247
 TFriendElement.cxx:248
 TFriendElement.cxx:249
 TFriendElement.cxx:250
 TFriendElement.cxx:251
 TFriendElement.cxx:252