00001
00002
00003
00004
00005
00006
00008 #include <iostream>
00009 #include <cstring>
00010 #include <ctime>
00011 #include <climits>
00012 #include <list>
00013 #include <string>
00014 #include "TSystem.h"
00015 #include "TProcessID.h"
00016 #include "EventDataModel/EventHandle.h"
00017 #include "EventDataModel/EventHeader.h"
00018 #include "IoModules/ReadWriteModule.h"
00019 #include "JobControl/Job.h"
00020 #include "JobControl/CmdLine.h"
00021 #include "JobControl/Exception.h"
00022 using namespace jobc;
00023
00024 const char* tstamp()
00025 {
00026
00027
00028
00029 static char tbuff[32];
00030 time_t t;
00031 t = time(0);
00032 strcpy(tbuff, ctime(&t));
00033 tbuff[24] = '\0';
00034 return tbuff;
00035 }
00036
00037
00038
00039 Job::Job(const char* name) :
00040 Sequence (name ),
00041 fInit (false),
00042 fCurrentRun (-1 ),
00043 fCurrentSubrun(-1 ),
00044 fCurrentFile ("" )
00045 { }
00046
00047
00048
00049 int Job::Run(int nevent)
00050 {
00051
00052
00053
00054
00055 int nMemReport = 0;
00056 bool printFlag = (nevent!=0);
00057
00058
00059 if (printFlag) {
00060 std::cout << "** Start job '" << this->Name() << "' "
00061 << tstamp() << " **" << std::endl;
00062 }
00063
00066
00067
00068
00069 int nevt = nevent;
00070 CmdLine& opt = CmdLine::Instance();
00071
00072 io::ReadWriteModule iomod;
00073 opt.SetupIoModule(iomod, &nevt);
00074 bool outfile = !(opt.OutFileName()==std::string(""));
00075 if (printFlag) iomod.Report();
00076
00077
00078 if (nevt==0) {
00079 edm::EventHandle& evt = iomod.GetEvent();
00080 this->CheckStatus(iomod.CurrentFile(), &evt);
00081 this->Exec(evt);
00082 }
00083 else {
00084 int n = 0;
00085 nMemReport = opt.NmemReport();
00086 for (; (iomod.ReadOK() && n<nevt); iomod.Advance()) {
00087 edm::EventHandle& evt = iomod.GetEvent();
00088 try {
00089 this->CheckStatus(iomod.CurrentFile(), &evt);
00090 bool passed = this->Exec(evt);
00091 if (passed && outfile) iomod.WriteEvent();
00092 }
00093 catch(Exception jce) {
00094 std::cout << "Job assertion '" << jce.fExpression
00095 << "' failed:\n";
00096 if (!jce.fMessage.empty()) std::cout<<" | "<<jce.fMessage<<"\n";
00097 std::cout << " | in " << jce.fFile << ":" << jce.fLine
00098 << "\n |- Run "<<iomod.RunNumber() << " event "
00099 << iomod.EventNumber() << ": " << iomod.CurrentFile()
00100 << std::endl;
00101 if (opt.DumpOnException()) break;
00102 }
00103 ++n;
00104 if (n>0 && nMemReport>0 && n%nMemReport==0) {
00105 this->ResourceReport(true);
00106 }
00107 if (n%1000==0) {
00108 std::cout << "[" << n << "] "
00109 << tstamp() << " /"
00110 << iomod.RunNumber() << ":" << iomod.EventNumber()
00111 << "/ " << iomod.CurrentFile()
00112 << std::endl;
00113 }
00114
00115 if (CmdLine::Instance().HaveSIGHUP()==true) {
00116 std::cout << "** Job ending on SIGHUP " << std::endl;
00117 break;
00118 }
00119 }
00120 }
00121 this->CheckStatus(std::string(""),0);
00122
00123
00124 if (printFlag) {
00125 std::cout << "** " << tstamp() << " End job " << this->Name()
00126 << " **" << std::endl;
00127 this->Print();
00128 }
00129 if (nMemReport>0) this->ResourceReport(true);
00130
00131 return 1;
00132 }
00133
00134
00135
00136 void Job::CheckStatus(const std::string& file,
00137 const edm::EventHandle* evt)
00138 {
00139
00140
00141
00142
00143 int run = -1;
00144 int subrun = -1;
00145
00146 if (evt) {
00147 const edm::EventHeader& head = evt->Header();
00148 run = head.Run();
00149 subrun = head.Subrun();
00150 }
00151
00152
00153
00154 switch (fInit) {
00155
00156 case false:
00157
00158 if (run>=0 && subrun>=0) {
00159 this->NewRun(run, subrun);
00160 this->NewSubrun(run, subrun);
00161 this->NewFile(file.c_str());
00162 }
00163 break;
00164
00165 case true:
00166
00167
00168 if (fCurrentSubrun != subrun) {
00169 if (fCurrentSubrun!=-1) this->EndSubrun(fCurrentRun, fCurrentSubrun);
00170 if (subrun!=-1) this->NewSubrun(run, subrun);
00171 }
00172 if (fCurrentRun != run) {
00173 if (fCurrentRun!=-1) this->EndRun(fCurrentRun, fCurrentSubrun);
00174 if (run!=-1) this->NewRun(run, subrun);
00175 }
00176 if (fCurrentFile != file) {
00177 if (fCurrentFile!="") this->EndFile(fCurrentFile.c_str());
00178 if (file!="") this->NewFile(file.c_str());
00179 }
00180 break;
00181 }
00182 fCurrentRun = run;
00183 fCurrentSubrun = subrun;
00184 fCurrentFile = file;
00185 fInit = true;
00186 }
00187