00001 #include "StCloseFileOnTerminate.h"
00002
00003 #include "TROOT.h"
00004 #include "TSystem.h"
00005 #include "TApplication.h"
00006 #include "TFile.h"
00007
00008 StCloseFileOnTerminate *StCloseFileOnTerminate::fgCloseFileOnTerminate = 0;
00009
00010 StCloseFileOnTerminate:: StCloseFileOnTerminate() : TSignalHandler(kSigTermination, kFALSE)
00011 {
00012 Add();
00013 }
00014
00015 StCloseFileOnTerminate &StCloseFileOnTerminate::Instantiate()
00016 {
00017
00018 if (! fgCloseFileOnTerminate )
00019 fgCloseFileOnTerminate = new StCloseFileOnTerminate;
00020 return *fgCloseFileOnTerminate;
00021 }
00022
00023 Bool_t StCloseFileOnTerminate::Notify() {
00024
00025 #if 1
00026 Error(__FUNCTION__," Closing all TFiles . . . . ");
00027 TSeqCollection *files = gROOT->GetListOfFiles();
00028 int count = 0;
00029 if (files && files->GetSize() >0 ) {
00030 TIter next(files);
00031 while( TFile *f = (TFile *) next() ) {
00032 if ( f-> IsWritable() ) {
00033 Error(__FUNCTION__, "file %s will be closed", f->GetName());
00034 f->Write();
00035 f->Close(); ++count;
00036 Error(__FUNCTION__, "file %s has been closed", f->GetName());
00037 }
00038 }
00039 files->Delete();
00040 }
00041 if (count) Error(__FUNCTION__, "%d files have been closed", count);
00042 else Print(" There was no open file to close");
00043 #else
00044 Error(__FUNCTION__,"Close any files or sockets before emptying CINT . . . . ");
00045 gROOT->CloseFiles();
00046 #endif
00047 Error(__FUNCTION__,"Terminating . . . . ");
00048 gApplication->Terminate(15);
00049 return kTRUE;
00050 }
00051
00052 StTerminateNotified::StTerminateNotified() : fTerminateHandler()
00053 {
00054 fTerminateHandler = new StNotifyOnTerminate(*this);
00055
00056 if (StCloseFileOnTerminate::Exists()) StCloseFileOnTerminate::Instantiate().DeActivate();
00057 }
00058
00059 StTerminateNotified::~StTerminateNotified()
00060 {
00061 fTerminateHandler->DeActivate();
00062
00063 if (StCloseFileOnTerminate::Exists() && ((&StCloseFileOnTerminate::Instantiate()) !=fTerminateHandler))
00064 StCloseFileOnTerminate::Instantiate().Activate();
00065 delete fTerminateHandler; fTerminateHandler = 0;
00066 }
00067
00068
00069 Bool_t StNotifyOnTerminate::Notify()
00070 {
00071 fNotificator.SetNotifiedCallBack();
00072 Error(__FUNCTION__," Job will be terminated soon by the external signal . . . . ");
00073 return kTRUE;
00074 }