#include "TTree.h"
#include "TFriendElement.h"
#include "TFile.h"
#include "TROOT.h"
R__EXTERN TTree *gTree;
ClassImp(TFriendElement)
TFriendElement::TFriendElement() : TNamed()
{
fFile = 0;
fTree = 0;
fOwnFile = kFALSE;
fParentTree = gTree;
}
TFriendElement::TFriendElement(TTree *tree, const char *treename, const char *filename)
:TNamed(treename,filename)
{
fFile = 0;
fTree = 0;
fOwnFile = kTRUE;
fParentTree = tree;
fTreeName = treename;
if (treename && strchr(treename,'=')) {
char *temp = Compress(treename);
char *equal = strchr(temp,'=');
if (!equal) return;;
*equal=0;
fTreeName = equal+1;
SetName(temp);
delete [] temp;
}
Connect();
}
TFriendElement::TFriendElement(TTree *tree, const char *treename, TFile *file)
:TNamed(treename,file?file->GetName():"")
{
fFile = file;
fTree = 0;
fOwnFile = kFALSE;
fParentTree = tree;
fTreeName = treename;
if (fParentTree && fParentTree->GetDirectory()
&& fParentTree->GetDirectory()->GetFile() == fFile) {
SetTitle("");
}
if (treename && strchr(treename,'=')) {
char *temp = Compress(treename);
char *equal = strchr(temp,'=');
if (!equal) return;;
*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()
: "")
: "")
: "")
{
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) {
SetTitle("");
}
}
if (alias && strlen(alias)) {
char *temp = Compress(alias);
SetName(temp);
delete [] temp;
}
}
TFriendElement::TFriendElement(const TFriendElement& tfe) :
TNamed(tfe),
fParentTree(tfe.fParentTree),
fTree(tfe.fTree),
fFile(tfe.fFile),
fTreeName(tfe.fTreeName),
fOwnFile(tfe.fOwnFile)
{
}
TFriendElement& TFriendElement::operator=(const TFriendElement& tfe)
{
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()
{
DisConnect();
}
TTree *TFriendElement::Connect()
{
GetFile();
return GetTree();
}
TTree *TFriendElement::DisConnect()
{
if (fOwnFile) delete fFile;
fFile = 0;
fTree = 0;
return 0;
}
TFile *TFriendElement::GetFile()
{
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()
{
if (fTree) return fTree;
if (GetFile()) {
fFile->GetObject(GetTreeName(),fTree);
if (fTree) return fTree;
}
fTree = dynamic_cast<TTree*>( gROOT->FindObject(GetTreeName()) );
return fTree;
}
void TFriendElement::ls(Option_t *) const
{
printf(" Friend Tree: %s in file: %s\n",GetName(),GetTitle());
}