#include "TEntryListFromFile.h"
#include "TObjArray.h"
#include "TFile.h"
#include "TKey.h"
ClassImp(TEntryListFromFile)
TEntryListFromFile::TEntryListFromFile():TEntryList()
{
fListFileName ="";
fNFiles = 0;
fFile = 0;
}
TEntryListFromFile::TEntryListFromFile(const char *filename, const char *listname, Int_t nfiles):TEntryList()
{
fListFileName = filename;
fListName = listname;
fNFiles = nfiles;
fListOffset = new Long64_t[fNFiles+1];
fListOffset[0]=0;
for (Int_t i=1; i<fNFiles+1; i++){
fListOffset[i]=kBigNumber;
}
fN = kBigNumber;
fFile = 0;
}
TEntryListFromFile::~TEntryListFromFile()
{
delete [] fListOffset;
fListOffset = 0;
delete fFile;
fFile = 0;
}
Long64_t TEntryListFromFile::GetEntry(Int_t index)
{
if (index<0) return -1;
if (index > fListOffset[fNFiles] && fListOffset[fNFiles]!=kBigNumber){
Error("GetEntry", "Index value is too large\n");
return -1;
}
if (index==fLastIndexQueried+1)
return Next();
Int_t itree =0;
while (!fCurrent && itree<fNFiles){
LoadList(itree);
itree++;
}
if (itree == fNFiles){
Error("GetEntry", "All lists are empty\n");
return -1;
}
if (index < fListOffset[fTreeNumber]) {
itree=0;
for (itree=0; itree<fTreeNumber; itree++){
if (index >= fListOffset[itree] && fListOffset[itree]!=fListOffset[itree+1])
break;
}
LoadList(itree);
}
else if (index >= fListOffset[fTreeNumber+1]){
itree = fTreeNumber;
while (itree < fNFiles){
itree++;
if (fListOffset[itree+1]==kBigNumber){
LoadList(itree);
}
if (index < fListOffset[itree+1]){
break;
}
}
if (fTreeNumber == fNFiles){
Error("GetEntry", "Entry number is too big\n");
return -1;
}
if (fTreeNumber!=itree)
LoadList(itree);
}
Long64_t localentry = index - fListOffset[fTreeNumber];
Long64_t retentry = fCurrent->GetEntry(localentry);
fLastIndexQueried = index;
fLastIndexReturned = retentry;
return retentry;
}
Long64_t TEntryListFromFile::GetEntryAndTree(Int_t index, Int_t &treenum)
{
Long64_t result = GetEntry(index);
treenum = fTreeNumber;
return result;
}
Long64_t TEntryListFromFile::GetEntries()
{
if (fN==kBigNumber){
for (Int_t i=0; i<fNFiles; i++){
if (fListOffset[i+1]==kBigNumber){
LoadList(i);
}
}
}
fN = fListOffset[fNFiles];
fLastIndexQueried = -3;
return fN;
}
Long64_t TEntryListFromFile::Next()
{
Int_t itree =0;
while (!fCurrent && itree<fNFiles){
LoadList(itree);
itree++;
}
if (itree == fNFiles){
Error("Next", "All lists are empty\n");
return -1;
}
Long64_t retentry = fCurrent->Next();
if (retentry<0){
if (fLastIndexQueried == fListOffset[fTreeNumber+1]-1){
if (fTreeNumber == fNFiles -1){
return -1;
}
do{
fTreeNumber++;
LoadList(fTreeNumber);
} while (fListOffset[fTreeNumber+1]==fListOffset[fTreeNumber] && fTreeNumber<fNFiles-1);
if (fTreeNumber == fNFiles -1 && fListOffset[fTreeNumber+1]==fListOffset[fTreeNumber]){
return -1;
}
retentry = fCurrent->Next();
} else {
Error("Next", "Something wrong with reading the current list, even though thefile #%d and the list exist\n", fTreeNumber);
return -1;
}
}
fLastIndexQueried++;
fLastIndexReturned = retentry;
return retentry;
}
Int_t TEntryListFromFile::LoadList(Int_t listnumber)
{
if (fCurrent){
if (fFile) {
delete fFile;
fFile = 0;
fCurrent = 0;
}
}
TNamed *nametitle = (TNamed*)fFileNames->At(listnumber);
TString filename_short = nametitle->GetTitle();
if (filename_short.Contains(".root")){
filename_short.Remove(filename_short.Length()-5, 5);
}
if (!strcmp(fListFileName.Data(), "")){
filename_short.Append("_elist.root");
fFile = TFile::Open(filename_short.Data());
} else {
TString filename = fListFileName;
filename.ReplaceAll("$", filename_short);
fFile = TFile::Open(filename.Data());
}
if (!fFile || fFile->IsZombie()){
if (fFile) {
delete fFile;
fFile = 0;
}
fCurrent = 0;
fListOffset[listnumber+1]=fListOffset[listnumber];
return -1;
}
if (!strcmp(fListName.Data(), "")){
TKey *key;
TIter nextkey(fFile->GetListOfKeys());
while ((key=(TKey*)nextkey())){
if (strcmp("TEntryList", key->GetClassName())==0){
fCurrent = (TEntryList*)key->ReadObj();
}
}
} else {
fCurrent = (TEntryList*)fFile->Get(fListName.Data());
}
if (!fCurrent){
Error("LoadList", "List %s not found in the file\n", fListName.Data());
fCurrent = 0;
fListOffset[listnumber+1]=fListOffset[listnumber];
return -1;
}
fTreeNumber = listnumber;
Long64_t nentries = fCurrent->GetN();
if (fListOffset[fTreeNumber+1] != (fListOffset[fTreeNumber] + nentries)) {
fListOffset[fTreeNumber+1] = fListOffset[fTreeNumber] + nentries;
fN = fListOffset[fNFiles];
}
return 1;
}
void TEntryListFromFile::Print(const Option_t* option) const
{
printf("total number of files: %d\n", fNFiles);
TFile *f;
TEntryList *el=0;
for (Int_t listnumber=0; listnumber<fNFiles; listnumber++){
TNamed *nametitle = (TNamed*)fFileNames->At(listnumber);
TString filename_short = nametitle->GetTitle();
if (filename_short.Contains(".root")){
filename_short.Remove(filename_short.Length()-5, 5);
}
if (!strcmp(fListFileName.Data(), "")){
filename_short.Append("_elist.root");
f = TFile::Open(filename_short.Data());
} else {
TString filename = fListFileName;
filename.ReplaceAll("$", filename_short);
f = TFile::Open(filename.Data());
}
if (f && !f->IsZombie()){
if (!strcmp(fListName.Data(), "")){
TKey *key;
TIter nextkey(f->GetListOfKeys());
while ((key=(TKey*)nextkey())){
if (strcmp("TEntryList", key->GetClassName())==0){
el = (TEntryList*)key->ReadObj();
}
}
} else {
el = (TEntryList*)f->Get(fListName.Data());
}
if (el)
el->Print(option);
}
}
}
Last change: Wed Jun 25 08:36:25 2008
Last generated: 2008-06-25 08:36
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.