#include "TFileCollection.h"
#include "THashList.h"
#include "TFileInfo.h"
#include "TIterator.h"
#include "TObjString.h"
#include "TUrl.h"
#include "TSystem.h"
#include "Riostream.h"
#include "TRegexp.h"
ClassImp(TFileCollection)
TFileCollection::TFileCollection(const char *name, const char *title,
const char *textfile, Int_t nfiles, Int_t firstfile)
: TNamed(name, title), fList(0), fMetaDataList(0),
fTotalSize(0), fNFiles(0), fNStagedFiles(0), fNCorruptFiles(0)
{
fList = new THashList();
fList->SetOwner();
fMetaDataList = new TList;
fMetaDataList->SetOwner();
AddFromFile(textfile, nfiles, firstfile);
}
TFileCollection::~TFileCollection()
{
delete fList;
delete fMetaDataList;
}
Int_t TFileCollection::Add(TFileInfo *info)
{
if (fList && info) {
fList->Add(info);
return 1;
} else {
return 0;
}
}
Int_t TFileCollection::Add(TFileCollection *coll)
{
if (fList && coll && coll->GetList()) {
TList* list = coll->GetList();
for (Int_t i=0; i<list->GetEntries(); i++) {
fList->Add((TFileInfo*) list->At(i));
}
return 1;
} else {
return 0;
}
}
Int_t TFileCollection::AddFromFile(const char *textfile, Int_t nfiles, Int_t firstfile)
{
if (!fList)
return 0;
Int_t nf = 0;
if (textfile && *textfile) {
ifstream f;
f.open(gSystem->ExpandPathName(textfile));
if (f.is_open()) {
Bool_t all = (nfiles <= 0) ? kTRUE : kFALSE;
Int_t ff = (!all && (firstfile < 1)) ? 1 : firstfile;
Int_t nn = 0;
while (f.good() && (all || nf < nfiles)) {
TString line;
line.ReadToDelim(f);
if (!line.IsWhitespace() && !line.BeginsWith("#")) {
nn++;
if (all || nn >= ff) {
fList->Add(new TFileInfo(line));
nf++;
}
}
}
f.close();
Update();
} else
Error("AddFromFile", "unable to open file %s", textfile);
}
return nf;
}
Int_t TFileCollection::Add(const char *dir)
{
Int_t nf = 0;
if (!fList)
return nf;
if (!dir || !*dir) {
Error("AddFromDirectory", "input dir undefined");
return nf;
}
FileStat_t st;
FileStat_t tmp;
TString baseDir = gSystem->DirName(dir);
if (gSystem->GetPathInfo(dir, st) == 0 ||
gSystem->GetPathInfo(baseDir, tmp) == 0) {
if (R_ISREG(st.fMode)) {
TFileInfo *info = new TFileInfo(dir);
info->SetBit(TFileInfo::kStaged);
Add(info);
nf++;
Update();
return nf;
} else {
void *dataSetDir = gSystem->OpenDirectory(gSystem->DirName(dir));
if (!dataSetDir) {
Error("AddFromDirectory", "directory %s cannot be opened",
gSystem->DirName(dir));
} else {
const char *ent;
TString filesExp(gSystem->BaseName(dir));
filesExp.ReplaceAll("*",".*");
TRegexp rg(filesExp);
while ((ent = gSystem->GetDirEntry(dataSetDir))) {
TString entryString(ent);
if (entryString.Index(rg) != kNPOS) {
TString fn = gSystem->DirName(dir);
fn += "/";
fn += ent;
gSystem->GetPathInfo(fn, st);
if (R_ISREG(st.fMode)) {
TFileInfo *info = new TFileInfo(fn);
info->SetBit(TFileInfo::kStaged);
Add(info);
nf++;
}
}
}
gSystem->FreeDirectory(dataSetDir);
Update();
}
}
}
return nf;
}
TFileCollection *TFileCollection::GetStagedSubset()
{
if (!fList)
return 0;
TFileCollection *subset = new TFileCollection(GetName(), GetTitle());
TIter iter(fList);
TFileInfo *fileInfo = 0;
while ((fileInfo = dynamic_cast<TFileInfo*>(iter.Next()))) {
if (fileInfo->TestBit(TFileInfo::kStaged) && !fileInfo->TestBit(TFileInfo::kCorrupted))
subset->Add(fileInfo);
}
subset->Update();
return subset;
}
Int_t TFileCollection::Update(Long64_t avgsize)
{
if (!fList)
return -1;
Int_t rc = 0;
fTotalSize = 0;
fNStagedFiles = 0;
fNCorruptFiles = 0;
fMetaDataList->Clear();
fNFiles = fList->GetEntries();
TIter iter(fList);
TFileInfo *fileInfo = 0;
while ((fileInfo = dynamic_cast<TFileInfo*> (iter.Next()))) {
if (fileInfo->GetSize() > 0) {
fTotalSize += fileInfo->GetSize();
} else {
rc = 1;
if (avgsize > 0) {
rc = 2;
fTotalSize += avgsize;
}
}
if (fileInfo->TestBit(TFileInfo::kStaged) && !fileInfo->TestBit(TFileInfo::kCorrupted)) {
fNStagedFiles++;
if (fileInfo->GetMetaDataList()) {
TIter metaDataIter(fileInfo->GetMetaDataList());
TObject *obj = 0;
while ((obj = metaDataIter.Next())) {
TFileInfoMeta *metaData = dynamic_cast<TFileInfoMeta*>(obj);
if (!metaData)
continue;
if (!metaData->IsTree())
continue;
TFileInfoMeta *metaDataSum = dynamic_cast<TFileInfoMeta*>(fMetaDataList->FindObject(metaData->GetName()));
Bool_t newObj = kFALSE;
if (!metaDataSum) {
metaDataSum = new TFileInfoMeta(metaData->GetName(), metaData->GetTitle());
fMetaDataList->Add(metaDataSum);
newObj = kTRUE;
}
if (newObj)
metaDataSum->SetEntries(metaData->GetEntries());
else
metaDataSum->SetEntries(metaDataSum->GetEntries() + metaData->GetEntries());
}
}
}
if (fileInfo->TestBit(TFileInfo::kCorrupted))
fNCorruptFiles++;
}
return rc;
}
void TFileCollection::Print(Option_t *option) const
{
Printf("TFileCollection %s - %s contains: %lld files with a size of"
" %lld bytes, %.1f %% staged - default tree name: '%s'",
GetName(), GetTitle(), fNFiles, fTotalSize, GetStagedPercentage(),
GetDefaultTreeName());
if (TString(option).Contains("M", TString::kIgnoreCase)) {
Printf("The files contain the following trees:");
TIter metaDataIter(fMetaDataList);
TFileInfoMeta* metaData = 0;
while ((metaData = dynamic_cast<TFileInfoMeta*>(metaDataIter.Next()))) {
if (!metaData->IsTree())
continue;
Printf("Tree %s: %lld events", metaData->GetName(), metaData->GetEntries());
}
}
if (fList && TString(option).Contains("F", TString::kIgnoreCase)) {
Printf("The collection contains the following files:");
fList->Print();
}
}
void TFileCollection::SetAnchor(const char *anchor)
{
if (!fList)
return;
TIter iter(fList);
TFileInfo *fileInfo = 0;
while ((fileInfo = dynamic_cast<TFileInfo*>(iter.Next()))) {
fileInfo->ResetUrl();
TUrl *url = 0;
while ((url = fileInfo->NextUrl()))
url->SetAnchor(anchor);
fileInfo->ResetUrl();
}
}
void TFileCollection::SetBitAll(UInt_t f)
{
if (!fList)
return;
TIter iter(fList);
TFileInfo *fileInfo = 0;
while ((fileInfo = dynamic_cast<TFileInfo*>(iter.Next())))
fileInfo->SetBit(f);
}
void TFileCollection::ResetBitAll(UInt_t f)
{
if (!fList)
return;
TIter iter(fList);
TFileInfo *fileInfo = 0;
while ((fileInfo = dynamic_cast<TFileInfo*>(iter.Next())))
fileInfo->ResetBit(f);
}
const char *TFileCollection::GetDefaultTreeName() const
{
TIter metaDataIter(fMetaDataList);
TFileInfoMeta *metaData = 0;
while ((metaData = dynamic_cast<TFileInfoMeta*>(metaDataIter.Next()))) {
if (!metaData->IsTree())
continue;
return metaData->GetName();
}
return 0;
}
Long64_t TFileCollection::GetTotalEntries(const char *tree) const
{
if (!tree || !*tree) {
tree = GetDefaultTreeName();
if (!tree)
return -1;
}
TFileInfoMeta *metaData = dynamic_cast<TFileInfoMeta*>(fMetaDataList->FindObject(tree));
if (!metaData)
return -1;
return metaData->GetEntries();
}
TFileInfoMeta *TFileCollection::GetMetaData(const char *meta) const
{
if (!meta || !*meta)
return 0;
return dynamic_cast<TFileInfoMeta*>(fMetaDataList->FindObject(meta));
}
void TFileCollection::SetDefaultMetaData(const char *meta)
{
TFileInfoMeta *fim = GetMetaData(meta);
if (fim) {
fMetaDataList->Remove(fim);
fMetaDataList->AddFirst(fim);
}
}
void TFileCollection::RemoveMetaData(const char *meta)
{
if (fList) {
TIter iter(fList);
TFileInfo *fileInfo = 0;
while ((fileInfo = dynamic_cast<TFileInfo*>(iter.Next())))
fileInfo->RemoveMetaData(meta);
}
if (meta) {
TObject* obj = fMetaDataList->FindObject("meta");
if (obj) {
fMetaDataList->Remove(obj);
delete obj;
}
} else
fMetaDataList->Clear();
}
void TFileCollection::Sort()
{
if (!fList)
return;
fList->Sort();
}
TObjString *TFileCollection::ExportInfo(const char *name)
{
TString treeInfo;
if (GetDefaultTreeName()) {
treeInfo = Form(" %s ", GetDefaultTreeName());
if (treeInfo.Length() < 14)
treeInfo.Resize(14);
TFileInfoMeta* meta = GetMetaData(GetDefaultTreeName());
if (meta)
treeInfo += Form("| %8lld ", meta->GetEntries());
} else {
treeInfo = " N/A";
}
treeInfo.Resize(25);
const char *unit[4] = {"kB", "MB", "GB", "TB"};
Int_t k = 0;
Long64_t refsz = 1024;
Long64_t xsz = (Long64_t) (GetTotalSize() / refsz);
while (xsz > 1024 && k < 3) {
k++;
refsz *= 1024;
xsz = (Long64_t) (GetTotalSize() / refsz);
}
TString dsname(name);
if (dsname.IsNull()) dsname = GetName();
TObjString *outs =
new TObjString(Form("%s| %7lld |%s| %5lld %s | %3d %%", dsname.Data(),
GetNFiles(), treeInfo.Data(), xsz, unit[k],
(Int_t)GetStagedPercentage()));
return outs;
}
Last change: Tue Sep 30 12:40:38 2008
Last generated: 2008-09-30 12:40
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.