00001
00002
00003
00005
00006
00008 #include "JobControl/CmdLine.h"
00009 #include <iostream>
00010 #include <climits>
00011 extern "C" {
00012 #include "getopt.h"
00013 }
00014 #include "IoModules/ReadWriteModule.h"
00015 #include "IoModules/EventFilter.h"
00016 using namespace jobc;
00017
00018 CmdLine* CmdLine::fInstance = 0;
00019
00020 void CmdLine::Usage() const
00021 {
00022 std::cerr <<
00023 "usage: ana [options] file1.root file2.root ...\n" <<
00024 "options are:\n" <<
00025 " -i file.root,--input=file.root : Add input data file\n"
00026 " -o file.root,--output=file.root : Set output data file \n"
00027 " -g file.root,--histo=file.root : Set output histogram file"
00028 " (0 for no file)\n"
00029 " -n #, --nevt=# : Set number of events to process\n"
00030 " -s #, --nskip=# : Skip number of events at start\n"
00031 " -r #, --run=# : Skip to run number at start\n"
00032 " -e #, --event=# : Skip to event number at start\n"
00033 " -x #, --xml=file.xml : Parse XML file\n"
00034 " -p #, --xmlpath=dir : Add directory to XML serach path\n"
00035 " -d #, --xmldir=dir : Read all XML files in directory\n"
00036 " -m #, --memory-report=# : Summarize resources used energy n events\n"
00037 " -l file.txt,--evtent-list=file : Read event list from text file\n"
00038 " -L #, --size-limit=# : Largest acceptable file size (in MB)\n"
00039 " -D #, --dumpcore : If 0 stop cleanly, if 1 dump core on assert_jobc's\n";
00040 }
00041
00042
00043
00044
00045 CmdLine::CmdLine() :
00046 fOutFileName(""),
00047 fFirstRun ( -1 ),
00048 fFirstEvent ( -1 ),
00049 fNevt ( INT_MAX ),
00050 fNskip ( 0 ),
00051 fNmemReport (-1 ),
00052 fSIGHUP (false),
00053 fDumpOnException(0),
00054 fSizeLimit ( 0 )
00055 { }
00056
00057
00058
00059 CmdLine& CmdLine::Instance()
00060 {
00061 if (fInstance==0) fInstance = new CmdLine();
00062 return *fInstance;
00063 }
00064
00065
00066
00067 void CmdLine::Parse(int argc, char** argv)
00068 {
00069 static const int kXMLDirOpt = 'd';
00070 static const int kEvtOpt = 'e';
00071 static const int kHFileOpt = 'g';
00072 static const int kHelpOpt = 'h';
00073 static const int kInputOpt = 'i';
00074 static const int kNevtOpt = 'n';
00075 static const int kOutputOpt = 'o';
00076 static const int kXMLPathOpt = 'p';
00077 static const int kRunOpt = 'r';
00078 static const int kNskipOpt = 's';
00079 static const int kXMLOpt = 'x';
00080 static const int kMemReportOpt = 'm';
00081 static const int kEvtListOpt = 'l';
00082 static const int kSizeLimitOpt = 'L';
00083 static const int kCoreDumpOpt = 'D';
00084 static struct option long_options[] = {
00085 {"input", 1, 0, kInputOpt},
00086 {"output", 1, 0, kOutputOpt},
00087 {"help", 0, 0, kHelpOpt},
00088 {"histo", 1, 0, kHFileOpt},
00089 {"nevt", 1, 0, kNevtOpt},
00090 {"nskip", 1, 0, kNskipOpt},
00091 {"run", 1, 0, kRunOpt},
00092 {"evt", 1, 0, kEvtOpt},
00093 {"xml", 1, 0, kXMLOpt},
00094 {"xmlpath", 1, 0, kXMLPathOpt},
00095 {"xmldir", 1, 0, kXMLDirOpt},
00096 {"memory-report",1, 0, kMemReportOpt},
00097 {"event-list", 1, 0, kEvtListOpt},
00098 {"size-limit", 1, 0, kSizeLimitOpt},
00099 {"dump", 1, 0, kCoreDumpOpt},
00100 {0,0,0,0}
00101 };
00102 while (1) {
00103 int c;
00104 int optindx;
00105 c = getopt_long(argc, argv, "hg:i:o:n:s:r:e:x:p:d:m:l:L:D:",
00106 long_options, &optindx);
00107 if (c==-1) break;
00108 switch (c) {
00109 case kInputOpt: fInFileList.push_back(std::string(optarg)); break;
00110 case kOutputOpt: fOutFileName = optarg; break;
00111 case kHelpOpt: this->Usage(); exit(0); break;
00112 case kHFileOpt: fHistoFileName = optarg; break;
00113 case kNevtOpt: fNevt = atoi(optarg); break;
00114 case kNskipOpt: fNskip = atoi(optarg); break;
00115 case kRunOpt: fFirstRun = atoi(optarg); break;
00116 case kEvtOpt: fFirstEvent = atoi(optarg); break;
00117 case kXMLOpt: fXMLFileList.push_back(std::string(optarg)); break;
00118 case kXMLPathOpt: fXMLPath = std::string(optarg); break;
00119 case kXMLDirOpt: fXMLDir.push_back(std::string(optarg)); break;
00120 case kMemReportOpt: fNmemReport = atoi(optarg); break;
00121 case kEvtListOpt: fEvtList.push_back(std::string(optarg)); break;
00122 case kSizeLimitOpt: fSizeLimit = atoi(optarg); break;
00123 case kCoreDumpOpt: fDumpOnException = atoi(optarg) + 1; break;
00124 default:
00125 std::cerr << "Unknown option: " << optind << argv[optind] << std::endl;
00126 this->Usage();
00127 exit(1);
00128 }
00129 }
00130 for (; optind<argc; ++optind) {
00131 fInFileList.push_back(std::string(argv[optind]));
00132 }
00133 };
00134
00135
00136
00137 void CmdLine::Print() const
00138 {
00139 std::cerr <<
00140 "Command line options:" << std::endl <<
00141 " fFirstRun = " << fFirstRun << std::endl <<
00142 " fFirstEvent = " << fFirstEvent << std::endl <<
00143 " fNevt = " << fNevt << std::endl <<
00144 " fNskip = " << fNskip << std::endl <<
00145 " fNmemReport = " << fNmemReport << std::endl;
00146
00147 std::list<std::string>::const_iterator itr;
00148 std::list<std::string>::const_iterator itrEnd;
00149
00150 std::cerr << " Input files = " << std::endl;
00151 itr = fInFileList.begin();
00152 itrEnd = fInFileList.end();
00153 for (; itr!=itrEnd; ++itr) std::cerr << " " << (*itr) << std::endl;
00154
00155 std::cerr << " Output file = " << fOutFileName << std::endl;
00156 std::cerr << " XML path = " << fXMLPath << std::endl;
00157
00158 std::cerr << " XML files = " << std::endl;
00159 itr = fXMLFileList.begin();
00160 itrEnd = fXMLFileList.end();
00161 for (; itr!=itrEnd; ++itr) std::cerr << " " << (*itr) << std::endl;
00162
00163 std::cerr << " XML dirs = " << std::endl;
00164 itr = fXMLDir.begin();
00165 itrEnd = fXMLDir.end();
00166 for (; itr!=itrEnd; ++itr) std::cerr << " " << (*itr) << std::endl;
00167 }
00168
00169
00170
00171 void CmdLine::SetupIoModule(io::ReadWriteModule& iomod, int* nevent) const
00172 {
00173
00174
00175
00176 const std::list<std::string>& fileList = this->InFileList();
00177 std::list<std::string>::const_iterator itr(fileList.begin());
00178 std::list<std::string>::const_iterator itrEnd(fileList.end());
00179 for (; itr!=itrEnd; ++itr) iomod.AddFile(itr->c_str());
00180
00181
00182 iomod.SetOutSizeLimit(fSizeLimit);
00183
00184
00185 const std::list<std::string>& evtList = this->EvtListFiles();
00186 itr = evtList.begin();
00187 itrEnd = evtList.end();
00188 for (; itr!=itrEnd; ++itr) {
00189 io::EventFilter* iof = new io::EventFilter();
00190 iof->ReadEventFile(itr->c_str());
00191 iomod.AdoptInputFilter(iof);
00192 }
00193
00194
00195 std::string outfile = this->OutFileName();
00196 if (outfile!="") iomod.SetOutputFileName(outfile.c_str());
00197
00198 int firstRun = this->FirstRun();
00199 int firstEvent = this->FirstEvent();
00200 int nevt = (*nevent);
00201 if (nevt==-1) nevt = this->Nevt();
00202 if (nevt==-1) nevt = INT_MAX;
00203 int nskip = this->NSkip();
00204 if (firstRun>=0) {
00205 if (firstEvent>=0) {
00206 iomod.GoTo(firstRun,firstEvent);
00207 }
00208 else {
00209 iomod.GoTo(firstRun,0);
00210 }
00211 }
00212 else {
00213 if (firstEvent>=0) {
00214 iomod.GoTo(iomod.RunNumber(),firstEvent);
00215 }
00216 }
00217 if (nskip>0) iomod.Advance(nskip);
00218 *nevent = nevt;
00219 }
00220
00221
00225 void CmdLine::SetSIGHUP()
00226 {
00227 if (fSIGHUP) {
00228 std::cout << "jobc::CmdLine: aborting anamipp!" << std::endl;
00229 abort();
00230 }
00231 std::cout << "Stopping job..."
00232 << "This may take a minute..." << std::endl;
00233 fSIGHUP = true;
00234 }
00235