119 #include "TFileIter.h"
129 , fEventName(
"event"), fRunNumber(UInt_t(-1)),fEventNumber(UInt_t(-1))
130 , fCursorPosition(-1), fOwnTFile(kFALSE)
139 , fRootFile(directory)
140 , fEventName(
"event"), fRunNumber(UInt_t(-1)),fEventNumber(UInt_t(-1))
141 , fCursorPosition(-1), fOwnTFile(kFALSE)
150 , Int_t compress, Int_t ) : fNestedIterator(0)
152 ,fEventName(
"event"), fRunNumber(UInt_t(-1)) ,fEventNumber(UInt_t(-1))
153 ,fCursorPosition(-1), fOwnTFile(kFALSE)
155 if (name && name[0]) {
161 fRootFile = TFile::Open(
MapName(name),option,ftitle,compress);
173 ,fRootFile(dst.fRootFile),fEventName(dst.fEventName), fRunNumber(dst.fRunNumber)
174 ,fEventNumber(dst.fRunNumber),
175 fCursorPosition(-1), fOwnTFile(dst.fOwnTFile)
177 if (fRootFile && fOwnTFile) {
179 if (fRootFile->InheritsFrom(TFile::Class()))
181 TFile *thisFile = (TFile *)fRootFile;
182 fRootFile = TFile::Open(
MapName(fRootFile->GetName())
183 ,fRootFile->GetOption()
184 ,fRootFile->GetTitle()
185 ,thisFile->GetCompressionSettings());
198 TFileIter *deleteit = fNestedIterator; fNestedIterator = 0;
200 if (fRootFile && fOwnTFile ) {
201 if (fRootFile->IsWritable()) fRootFile->Write();
214 fDirection = kIterForward;
217 if (fRootFile && fOwnTFile )
delete fRootFile;
228 Bool_t iOpen = kFALSE;
229 if (fRootFile && !fRootFile->IsZombie() ) {
231 if (fRootFile->InheritsFrom(TFile::Class()) && !((TFile*)fRootFile)->IsOpen())
242 return ((
TFileIter*)
this)->SkipObjects(0);
250 return fNestedIterator ? fNestedIterator->
GetDepth()+1 : 0;
258 const char *name = 0;
260 if (key) name = key->GetName();
284 if (key) lenObj = ((TKey *)key)->GetObjlen();
297 if(fList) size += fList->GetSize();
305 TObject *TFileIter::Next(Int_t nSkip)
320 TObjLink *lnk = listOfKeys->FirstLink();
322 TKey *key = (TKey *)lnk->GetObject();
323 Short_t cycle = key->GetCycle();
324 const char *keyName = key->GetName();
329 TObjLink *lnkThis = lnk;
331 && (nextkey = (TKey *)lnk->GetObject())
332 && !strcmp(nextkey->GetName(), keyName)
335 Short_t nextCycle = nextkey->GetCycle() ;
338 assert(cycle != nextCycle);
339 TObjLink *lnkNext = lnk->Next();
340 if (cycle > nextCycle ) {
341 delete listOfKeys->Remove(lnk);
343 delete listOfKeys->Remove(lnkThis);
358 if (fNestedIterator) {
364 if (!fRootFile->IsWritable()) {
365 TList *listOfKeys = fRootFile->GetListOfKeys();
367 if (!listOfKeys->IsSorted())
PurgeKeys(listOfKeys);
369 if (fDirection == kIterForward) {
371 fCurCursor = fList->FirstLink()->shared_from_this();
372 if (fCurCursor) fCursor = fCurCursor->NextSP();
374 fCursorPosition = fList->GetSize()-1;
375 fCurCursor = fList->LastLink()->shared_from_this();
376 if (fCurCursor) fCursor = fCurCursor->PrevSP();
384 void TFileIter::SetCursorPosition(
const char *keyNameToFind)
387 while( (*
this != keyNameToFind) &&
SkipObjects() ) {;}
403 TKey *nextObject = fNestedIterator ? fNestedIterator->
SkipObjects(nSkip): 0;
405 if (fNestedIterator) {
410 Int_t collectionSize = 0;
411 if (fList && (collectionSize = fList->GetSize()) ) {
412 if (fDirection !=kIterForward) nSkip = -nSkip;
413 Int_t newPos = fCursorPosition + nSkip;
414 if (0 <= newPos && newPos < collectionSize) {
416 if (fCursorPosition < newPos) {
418 fCurCursor = fCursor;
419 fCursor = fCursor->NextSP();
420 }
else if (fCursorPosition > newPos) {
422 fCurCursor = fCursor;
423 fCursor = fCursor->PrevSP();
425 }
while (fCursorPosition != newPos);
426 if (fCurCursor) nextObject =
dynamic_cast<TKey *
>(fCurCursor->GetObject());
428 fCurCursor = fCursor = 0;
430 fCursorPosition = -1;
431 if (fList) fCursor = fList->FirstLink()->shared_from_this();
433 fCursorPosition = collectionSize;
434 if (fList) fCursor = fList->LastLink()->shared_from_this();
443 TKey *TFileIter::NextEventKey(UInt_t eventNumber, UInt_t runNumber,
const char *name)
447 Bool_t reset = kFALSE;
448 if (name && name[0] && name[0] !=
'*') {
if (fEventName > name) reset = kTRUE; fEventName = name; }
449 if (runNumber !=UInt_t(-1) ) {
if (fRunNumber > runNumber) reset = kTRUE; fRunNumber = runNumber;}
450 if (eventNumber !=UInt_t(-1) ) {
if (fEventNumber > eventNumber) reset = kTRUE; fEventNumber = eventNumber;}
457 if (fDirection==kIterForward) fCursorPosition++;
458 else fCursorPosition--;
459 if ( name && name[0] !=
'*') {
460 thisKey.
SetKey(key->GetName());
461 if (thisKey.GetName() < name)
continue;
462 if (thisKey.GetName() > name) { key = 0;
break; }
465 if (runNumber != UInt_t(-1)) {
466 UInt_t thisRunNumber = thisKey.RunNumber();
467 if (thisRunNumber < runNumber)
continue;
468 if (thisRunNumber > runNumber) { key = 0;
break; }
471 if (eventNumber != UInt_t(-1)) {
472 UInt_t thisEventNumber = thisKey.EventNumber();
473 if (thisEventNumber < eventNumber)
continue;
474 if (thisEventNumber > eventNumber) {key = 0;
break; }
489 return ReadObj(NextEventKey(eventNumber,runNumber,name));
498 if (fNestedIterator) obj = fNestedIterator->
ReadObj(key);
500 obj = ((TKey *)key)->ReadObj();
501 if (obj && obj->InheritsFrom(TDirectory::Class()) )
504 assert(!fNestedIterator);
519 if (obj &&
IsOpen() && fRootFile->IsWritable()) {
520 TDsKey thisKey(runNumber,eventNum);
522 thisKey.SetName(name);
524 thisKey.SetName(obj->GetName());
526 TDirectory::TContext ctxt(fRootFile);
528 wBytes = obj->Write(thisKey.
GetKey());
529 if (fRootFile->InheritsFrom(TFile::Class())) ((TFile*)fRootFile)->Flush();
547 TString
TFileIter::MapName(
const char *name,
const char *localSystemKey,
const char *mountedFileSystemKey)
557 if ( !localSystemKey) localSystemKey = GetLocalFileNameKey();
558 if ( !mountedFileSystemKey) mountedFileSystemKey = GetForeignFileSystemKey();
559 TString newName = name;
560 TString fileMap = gEnv->GetValue(GetResourceName(),GetDefaultMapFileName());
561 const char *localName = 0;
562 const char *foreignName = 0;
563 if ( gSystem->AccessPathName(fileMap) == 0 ){
564 TEnv myMapResource(fileMap);
565 localName = myMapResource.Defined(localSystemKey) ?
566 myMapResource.GetValue(localSystemKey,
"") : 0;
567 foreignName = myMapResource.Defined(mountedFileSystemKey) ?
568 myMapResource.GetValue(mountedFileSystemKey,
""):0;
570 localName =
"/castor";
571 foreignName =
"rfio:/castor";
573 if (localName && localName[0]
576 && newName.BeginsWith(localName) )
577 newName.Replace(0,strlen(localName),foreignName);
virtual ~TFileIter()
TFileIter dtor.
virtual Int_t GetDepth() const
const char * GetKeyName() const
return the name of the current TKey
TKey * GetCurrentKey() const
return the pointer to the current TKey
TObject * ReadObj(const TKey *key) const
Read the next TObject from for the TDirectory by TKey provided.
virtual TObject * NextEventGet(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*")
static TString MapName(const char *name, const char *localSystemKey=0, const char *mountedFileSystemKey=0)
virtual Int_t NextEventPut(TObject *obj, UInt_t eventNum, UInt_t runNumber, const char *name=0)
Create a special TKey name with obj provided and write it out.
virtual TObject * GetObject() const
virtual TString GetKey() const
to be documented
void Initialize()
to be documented
virtual Bool_t IsOpen() const
virtual Int_t TotalKeys() const
virtual void Reset()
Reset the status of the iterator.
virtual TKey * SkipObjects(Int_t nSkip=1)
TFileIter(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
static void PurgeKeys(TList *listOfKeys)
Int_t GetObjlen() const
Returns the uncompressed length of the current object.
virtual void SetKey(const char *key)
to be documented