StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StCloseFileOnTerminate.cxx
1 #include "StCloseFileOnTerminate.h"
2 
3 #include "TROOT.h"
4 #include "TSystem.h"
5 #include "TApplication.h"
6 #include "TFile.h"
7 #include "TError.h"
8 StCloseFileOnTerminate *StCloseFileOnTerminate::fgCloseFileOnTerminate = 0;
9 //_________________________________________________________
10 StCloseFileOnTerminate:: StCloseFileOnTerminate() : TSignalHandler(kSigTermination, kFALSE)
11 {
12  Add();
13 }
14 //_________________________________________________________
16 {
17  // Create Asynch signal handler
18  if (! fgCloseFileOnTerminate ) {
19  fgCloseFileOnTerminate = new StCloseFileOnTerminate;
20  ::Warning("StCloseFileOnTerminate::Instantiate","Asynch signal handler has been created");
21  }
22  return *fgCloseFileOnTerminate;
23 }
24 //_________________________________________________________
26  // close all TFile & Terminate
27 #if 1 /* ROOT_VERSION_CODE < ROOT_VERSION(5,31,1) */
28  Error(__FUNCTION__," Closing all TFiles . . . . ");
29  TSeqCollection *files = gROOT->GetListOfFiles();
30  int count = 0;
31  if (files && files->GetSize() >0 ) {
32  TIter next(files);
33  while( TFile *f = (TFile *) next() ) {
34  if ( f-> IsWritable() ) {
35  Error(__FUNCTION__, "file %s will be closed", f->GetName());
36  f->Write();
37  f->Close(); ++count;
38  Error(__FUNCTION__, "file %s has been closed", f->GetName());
39  }
40  }
41  files->Delete();
42  }
43  if (count) Error(__FUNCTION__, "%d files have been closed", count);
44  else Print(" There was no open file to close");
45 #else
46  Error(__FUNCTION__,"Close any files or sockets before emptying CINT . . . . ");
47  gROOT->CloseFiles();
48 #endif
49  Error(__FUNCTION__,"Terminating . . . . ");
50  gApplication->Terminate(15);
51  return kTRUE;
52 }
53 //_________________________________________________________
54 StTerminateNotified::StTerminateNotified() : fTerminateHandler()
55 {
56  fTerminateHandler = new StNotifyOnTerminate(*this);
57  // check the global handler
58  if (StCloseFileOnTerminate::Exists()) StCloseFileOnTerminate::Instantiate().DeActivate();
59 }
60 //_________________________________________________________
61 StTerminateNotified::~StTerminateNotified()
62 {
63  fTerminateHandler->DeActivate();
64  // restore the global handler if needed
65  if (StCloseFileOnTerminate::Exists() && ((&StCloseFileOnTerminate::Instantiate()) !=fTerminateHandler))
67  delete fTerminateHandler; fTerminateHandler = 0;
68 }
69 
70 //_________________________________________________________
72 {
73  fNotificator.SetNotifiedCallBack();
74  Error(__FUNCTION__," Job will be terminated soon by the external signal . . . . ");
75  return kTRUE;
76 }
Class StCloseFileOnTerminate - it is a singleton to catch the SIGTERM signal (sent by Condor for exam...
Definition: FJcore.h:367
virtual Bool_t Notify()
Close the open ROOT files upon SIGTERM sent by system to terminate the job.
virtual Bool_t Notify()
Close the open ROOT files upon SIGTERM sent by system to terminate the job.
static StCloseFileOnTerminate & Instantiate()
Create an instance of the signal handler (Should be called at once by code)