119 #include "StFileIter.h"
127 , fEventName("event"), fRunNumber(UInt_t(-1)),fEventNumber(UInt_t(-1))
128 , fCursorPosition(-1), fOwnTFile(kFALSE)
135 StFileIter::StFileIter(TDirectory *directory) : fFileBackUp(0),fDirectoryBackUp(0), fNestedIterator(0)
136 , fRootFile(directory)
137 , fEventName(
"event"), fRunNumber(UInt_t(-1)),fEventNumber(UInt_t(-1))
138 , fCursorPosition(-1), fOwnTFile(kFALSE)
144 StFileIter::StFileIter(
const char *name, Option_t *option,
const char *ftitle
145 , Int_t compress, Int_t ) :fNestedIterator(0),fRootFile (0)
149 if (name && name[0]) {
155 fRootFile = TFile::Open(MapName(name),option,ftitle,compress);
161 StFileIter::StFileIter(
const StFileIter &dst) : TListIter()
162 ,fFileBackUp(0), fDirectoryBackUp(0), fNestedIterator(0)
163 ,fRootFile(dst.fRootFile),fEventName(dst.fEventName), fRunNumber(dst.fRunNumber)
164 ,fEventNumber(dst.fRunNumber),
165 fCursorPosition(-1), fOwnTFile(dst.fOwnTFile)
168 assert(!fRootFile->IsWritable());
169 if (fRootFile && fOwnTFile && !fRootFile->IsWritable()) {
171 if (fRootFile->InheritsFrom(TFile::Class()))
173 TFile *thisFile = (TFile *)fRootFile;
174 fRootFile = TFile::Open(MapName(fRootFile->GetName())
175 ,fRootFile->GetOption()
176 ,fRootFile->GetTitle()
177 ,thisFile->GetCompressionLevel());
183 SkipObjects(dst.fCursorPosition);
186 StFileIter::~StFileIter()
189 StFileIter *deleteit = fNestedIterator; fNestedIterator = 0;
191 if (fRootFile && fOwnTFile ) {
192 if (fRootFile->IsWritable()) fRootFile->Write();
200 void StFileIter::Initialize()
204 fDirection = kIterForward;
205 if (IsOpen()) Reset();
207 if (fRootFile && fOwnTFile )
delete fRootFile;
213 Bool_t StFileIter::IsOpen()
const
215 Bool_t iOpen = kFALSE;
216 if (fRootFile && !fRootFile->IsZombie() ) {
218 if (fRootFile->InheritsFrom(TFile::Class()) && !((TFile*)fRootFile)->IsOpen())
225 TKey *StFileIter::GetCurrentKey()
const
232 Int_t StFileIter::GetDepth()
const
236 return fNestedIterator ? fNestedIterator->GetDepth()+1 : 0;
240 const char *StFileIter::GetKeyName()
const
243 const char *name = 0;
244 TKey *key = GetCurrentKey();
245 if (key) name = key->GetName();
249 TObject *StFileIter::GetObject()
const
259 return ReadObj(GetCurrentKey());
262 Int_t StFileIter::GetObjlen()
const
266 TKey *key = GetCurrentKey();
267 if (key) lenObj = ((TKey *)key)->GetObjlen();
271 Int_t StFileIter::TotalKeys()
const
280 if(fList) size += fList->GetSize();
284 TObject *StFileIter::Next(Int_t nSkip)
295 void StFileIter::PurgeKeys(TList *listOfKeys) {
301 TObjLink *lnk = listOfKeys->FirstLink();
303 TKey *key = (TKey *)lnk->GetObject();
304 Short_t cycle = key->GetCycle();
305 const char *keyName = key->GetName();
310 TObjLink *lnkThis = lnk;
312 && (nextkey = (TKey *)lnk->GetObject())
313 && !strcmp(nextkey->GetName(), keyName)
316 Short_t nextCycle = nextkey->GetCycle() ;
319 assert(cycle != nextCycle);
320 TObjLink *lnkNext = lnk->Next();
321 if (cycle > nextCycle ) {
322 delete listOfKeys->Remove(lnk);
324 delete listOfKeys->Remove(lnkThis);
336 void AssignPointer(TObjLink* cursor, TObjLink* ptr) { cursor = ptr; }
337 void AssignPointer(std::shared_ptr<TObjLink> cursor, TObjLink* ptr) { cursor.reset(ptr); }
340 void StFileIter::Reset()
343 if (fNestedIterator) {
349 if (!fRootFile->IsWritable()) {
350 TList *listOfKeys = fRootFile->GetListOfKeys();
352 if (!listOfKeys->IsSorted()) PurgeKeys(listOfKeys);
354 if (fDirection == kIterForward) {
356 AssignPointer(fCurCursor, fList->FirstLink());
357 if (fCurCursor) AssignPointer(fCursor, fCurCursor->Next());
359 fCursorPosition = fList->GetSize()-1;
360 AssignPointer(fCurCursor, fList->LastLink());
361 if (fCurCursor) AssignPointer(fCursor, fCurCursor->Prev());
367 void StFileIter::SetCursorPosition(
const char *keyNameToFind)
371 while( (*
this != keyNameToFind) && SkipObjects() );
374 TKey *StFileIter::SkipObjects(Int_t nSkip)
386 TKey *nextObject = fNestedIterator ? fNestedIterator->SkipObjects(nSkip): 0;
388 if (fNestedIterator) {
393 Int_t collectionSize = 0;
394 if (fList && (collectionSize = fList->GetSize()) ) {
395 if (fDirection !=kIterForward) nSkip = -nSkip;
396 Int_t newPos = fCursorPosition + nSkip;
397 if (0 <= newPos && newPos < collectionSize) {
399 if (fCursorPosition < newPos) {
401 fCurCursor = fCursor;
402 AssignPointer(fCursor, fCursor->Next());
403 }
else if (fCursorPosition > newPos) {
405 fCurCursor = fCursor;
406 AssignPointer(fCursor, fCursor->Prev());
408 }
while (fCursorPosition != newPos);
409 if (fCurCursor) nextObject =
dynamic_cast<TKey *
>(fCurCursor->GetObject());
411 fCurCursor = fCursor = 0;
413 fCursorPosition = -1;
414 if (fList) AssignPointer(fCursor, fList->FirstLink());
416 fCursorPosition = collectionSize;
417 if (fList) AssignPointer(fCursor, fList->LastLink());
425 TKey *StFileIter::NextEventKey(UInt_t eventNumber, UInt_t runNumber,
const char *name)
433 Bool_t reset = kFALSE;
434 if (name && name[0] && name[0] !=
'*') {
if (fEventName > name) reset = kTRUE; fEventName = name; }
435 if (runNumber !=UInt_t(-1) ) {
if (fRunNumber > runNumber) reset = kTRUE; fRunNumber = runNumber;}
436 if (eventNumber !=UInt_t(-1) ) {
if (fEventNumber > eventNumber) reset = kTRUE; fEventNumber = eventNumber;}
442 while ( (key = SkipObjects()) ) {
443 if (fDirection==kIterForward) fCursorPosition++;
444 else fCursorPosition--;
445 if ( strcmp(name,
"*") ) {
446 thisKey.
SetKey(key->GetName());
447 if (thisKey.GetName() < name)
continue;
448 if (thisKey.GetName() > name) { key = 0;
break; }
451 if (runNumber != UInt_t(-1)) {
452 UInt_t thisRunNumber = thisKey.RunNumber();
453 if (thisRunNumber < runNumber)
continue;
454 if (thisRunNumber < runNumber) { key = 0;
break; }
457 if (eventNumber != UInt_t(-1)) {
458 UInt_t thisEventNumber = thisKey.EventNumber();
459 if (thisEventNumber < eventNumber)
continue;
460 if (thisEventNumber > eventNumber) {key = 0;
break; }
467 TObject *StFileIter::NextEventGet(UInt_t eventNumber, UInt_t runNumber,
const char *name)
475 return ReadObj(NextEventKey(eventNumber,runNumber,name));
479 TObject *StFileIter::ReadObj(
const TKey *key)
const
483 if (fNestedIterator) obj = fNestedIterator->ReadObj(key);
485 obj = ((TKey *)key)->ReadObj();
486 if (obj && obj->InheritsFrom(TDirectory::Class()) )
489 assert(!fNestedIterator);
498 Int_t StFileIter::NextEventPut(TObject *obj, UInt_t eventNum, UInt_t runNumber
504 if (obj && IsOpen() && fRootFile->IsWritable()) {
505 TDsKey thisKey(runNumber,eventNum);
507 thisKey.SetName(name);
509 thisKey.SetName(obj->GetName());
511 if (fRootFile != gDirectory) {
515 wBytes = obj->Write(thisKey.
GetKey());
516 if (fRootFile->InheritsFrom(TFile::Class())) ((TFile*)fRootFile)->Flush();
517 if (fRootFile != gDirectory) RestoreFileScope();
522 TString StFileIter::MapName(
const char *name,
const char *localSystemKey,
const char *mountedFileSystemKey)
545 if ( !localSystemKey) localSystemKey = GetLocalFileNameKey();
546 if ( !mountedFileSystemKey) mountedFileSystemKey = GetForeignFileSystemKey();
547 TString newName = name;
548 TString fileMap = gEnv->GetValue(GetResourceName(),GetDefaultMapFileName());
549 const char *localName = 0;
550 const char *foreignName = 0;
551 if ( gSystem->AccessPathName(fileMap) == 0 ){
552 TEnv myMapResource(fileMap);
553 localName = myMapResource.Defined(localSystemKey) ?
554 myMapResource.GetValue(localSystemKey,
"") : 0;
555 foreignName = myMapResource.Defined(mountedFileSystemKey) ?
556 myMapResource.GetValue(mountedFileSystemKey,
""):0;
558 localName =
"/castor";
559 foreignName =
"rfio:/castor";
561 if (localName && localName[0]
564 && newName.BeginsWith(localName) )
565 newName.Replace(0,strlen(localName),foreignName);
virtual TString GetKey() const
to be documented
virtual void SetKey(const char *key)
to be documented