Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Job.cxx

Go to the documentation of this file.
00001 
00002 // $Id: Job.cxx,v 1.1 2007/02/09 04:44:13 fmwk Exp $
00003 //
00004 // The basic unit of running MIPP analysis and reconstruction
00005 //
00006 // messier@indina.edu
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 // Provide a nicely formatted, current, time stamp string
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 // Process the next nevents in the queue through this job Use
00053 // nevent=-1 to process all events
00054 //======================================================================
00055   int  nMemReport = 0;
00056   bool printFlag = (nevent!=0);
00057 
00058   // Print begin run status
00059   if (printFlag) {
00060     std::cout << "** Start job '" << this->Name() << "' "
00061               << tstamp() << " **" << std::endl;
00062   }
00063   
00066   //
00067   // Setup the I/O module
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   // Event loop
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       // Check for sighup signal
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   // Print end run status
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 // Check the run status looking for things like new files, new run
00141 // numbers, etc.
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   // Very first time we should issue a begin file, run, and subrun in
00153   // that order. All other times we need to check the status.
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     // Check status changes in order of most frequent to least frequent
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 

Generated on Thu Sep 4 02:05:27 2008 for NOvA Offline by doxygen 1.3.5