12 #include "PhysicsTools/FWLite/interface/dumpSTL.icc" 17 const std::string CommandLineParser::kSpaces =
" \t";
19 CommandLineParser::CommandLineParser(
const string &
usage,
unsigned int optionsType)
20 : m_argv0(
""), m_usageString(usage), m_printOptions(
true), m_optionsType(optionsType) {
24 addOption(
"section",
kInteger,
"This section (from 1..totalSections inclusive)", 0);
25 addOption(
"maxEvents",
kInteger,
"Maximum number of events to run over (0 for whole file)", 0);
26 addOption(
"jobID",
kInteger,
"jobID given by CRAB,etc. (-1 means append nothing)", -1);
27 addOption(
"outputEvery",
kInteger,
"Output something once every N events (0 for never)", 0);
30 addOption(
"storePrepend",
kString,
"Prepend location on files starting with '/store/'");
31 addOption(
"tag",
kString,
"A 'tag' to append to output file (e.g., 'v2', etc.)");
36 addOption(
"secondaryInputFiles",
kStringVector,
"List of secondary input files (a.k.a. two-file-solution");
37 addOption(
"orderedSecondaryFiles",
kBool,
"Are the secondary files ordered?",
false);
41 cerr <<
"CommandLineParser() Error: type '" << optionsType <<
"' is not understood. Aborting." << endl;
46 bool callHelp =
false;
54 if (string::npos != where) {
61 cerr <<
"Don't understand: " << arg << endl;
64 else if (arg.at(0) ==
'-') {
66 if (string::npos == where) {
68 cerr <<
"Don't understand: " << arg << endl;
73 char first = arg.at(where);
89 cerr <<
"Don't understand: " << arg << endl;
93 argsVec.push_back(arg);
95 cerr <<
"Don't understand: " << arg << endl;
108 cout <<
"--help - This screen" << endl
109 <<
"--noPrint - Do not print out all settings" << endl
110 <<
"--print - Print out all settings" << endl;
116 if (ignoreComments) {
123 char firstCh = line[start1];
124 if (
'#' == firstCh) {
132 while (string::npos != current) {
134 if (string::npos != last) {
139 string part = line.substr(pos, current - last - 1);
142 retval.push_back(part);
145 current = line.find_first_of(match, current + 1);
151 if (string::npos != location) {
153 line = line.substr(0, location - 1);
159 if (string::npos == pos) {
165 line = line.substr(pos);
167 pos = line.find_last_not_of(
kSpaces);
168 if (pos + 1 != line.length()) {
170 line = line.substr(0, pos + 1);
176 if (input.length() - ending.length() ==
position) {
178 return input.substr(0, position);
187 if (string::npos == nonspace) {
192 if (string::npos == space) {
194 command = line.substr(nonspace);
197 command = line.substr(nonspace, space - nonspace);
198 rest = line.substr(space + 1);
203 cout <<
"------------------------------------------------------------------" << left << endl;
206 cout << endl <<
"Integer options:" << endl;
210 cout <<
" " << setw(14) << iter->first <<
" = " << setw(14) << iter->second;
211 if (description.length()) {
219 cout << endl <<
"Double options:" << endl;
223 cout <<
" " << setw(14) << iter->first <<
" = " << setw(14) << iter->second;
224 if (description.length()) {
232 cout << endl <<
"Bool options:" << endl;
236 cout <<
" " << setw(14) << iter->first <<
" = " << setw(14);
242 if (description.length()) {
250 cout << endl <<
"String options:" << endl;
254 cout <<
" " << setw(14) << iter->first <<
" = ";
255 const string value =
"'" + iter->second +
"'";
256 cout << setw(14) <<
"";
257 if (description.length()) {
260 cout << endl <<
" " << value << endl;
265 cout << endl <<
"Integer Vector options:" << endl;
269 cout <<
" " << setw(14) << iter->first <<
" = ";
270 dumpSTL(iter->second);
272 if (description.length()) {
280 cout << endl <<
"Double Vector options:" << endl;
284 cout <<
" " << setw(14) << iter->first <<
" = ";
285 dumpSTL(iter->second);
287 if (description.length()) {
295 cout << endl <<
"String Vector options:" << endl;
301 cout <<
" " << setw(14) << iter->first <<
" = ";
302 if (description.length()) {
303 cout << setw(14) <<
"" 307 dumpSTLeachEndl(iter->second, 8);
310 cout <<
"------------------------------------------------------------------" << right << endl;
315 string varname = arg.substr(offset, where - offset);
316 string value = arg.substr(where + 1);
335 integerIter->second = (
int)atof(value.c_str());
343 doubleIter->second = atof(value.c_str());
351 stringIter->second =
value;
359 boolIter->second = 0 != atoi(value.c_str());
368 split(words, value,
",");
369 for (
SVecConstIter wordIter = words.begin(); words.end() != wordIter; ++wordIter) {
370 integerVecIter->second.push_back((
int)atof(wordIter->c_str()));
382 split(words, value,
",");
383 for (
SVecConstIter wordIter = words.begin(); words.end() != wordIter; ++wordIter) {
384 doubleVecIter->second.push_back(atof(wordIter->c_str()));
396 split(words, value,
",");
397 for (
SVecConstIter wordIter = words.begin(); words.end() != wordIter; ++wordIter) {
398 stringVecIter->second.push_back(*wordIter);
407 cerr <<
"CommandLineParser::SetVeriableFromString() Error: " 408 <<
"Unknown variable and internal fault. Aborting." << endl;
416 cerr <<
"file " << filename <<
"could not be opened" << endl;
420 while (getline(
source, line)) {
423 if (string::npos == where) {
427 char first = line.at(where);
431 where = line.find_first_not_of(
kSpaces, where + 1);
432 if (string::npos == where) {
439 string withspaces = line.substr(where);
446 }
else if (
' ' == ch ||
'\t' == ch) {
452 cerr <<
"Don't understand line" << endl
454 <<
"in options file '" << filename <<
"'. Aborting." << endl;
463 split(equalWords, arg,
"=");
464 if (2 != equalWords.size()) {
468 split(commandWords, equalWords.at(0),
"_");
469 if (2 != commandWords.size()) {
472 string &
command = commandWords.at(1);
474 if (command !=
"load" && command !=
"clear") {
477 const string &
key = commandWords.at(0);
480 cerr <<
"Command '" << command <<
"' only works on vectors." << endl;
487 if (
"clear" == command) {
492 }
else if (kStringVector == type) {
505 const string &
filename = equalWords.at(1);
508 cerr <<
"file " << filename <<
"could not be opened" << endl;
512 while (getline(
source, line)) {
515 if (string::npos == where) {
520 line = line.substr(where);
522 where = line.find_last_not_of(
kSpaces);
523 if (line.length() - 1 != where) {
524 line = line.substr(0, where + 1);
526 if (
'#' == line.at(0)) {
534 }
else if (kStringVector == type) {
547 assert(section > 0 && section <= totalSections);
563 int entries = (
int)inputList.size();
564 int perSection = entries / totalSections;
565 int extra = entries % totalSections;
567 int current = perSection *
section;
568 int num = perSection - 1;
569 if (section < extra) {
579 outputList.push_back(inputList.at(
loop));
603 const string &kStorePrepend =
stringValue(
"storePrepend");
604 if (kStorePrepend.length()) {
605 string match =
"/store/";
606 int matchLen = match.length();
609 for (
SVecConstIter iter = currentFiles.begin(); currentFiles.end() != iter; ++iter) {
611 if ((
int)filename.length() > matchLen && filename.substr(0, matchLen) ==
match) {
612 tempVec.push_back(kStorePrepend + filename);
614 tempVec.push_back(filename);
617 currentFiles = tempVec;
626 bool modifyOutputFile = (outputFile.length());
630 outputFile += Form(
"_maxevt%03d",
integerValue(
"maxEvents"));
633 outputFile += Form(
"_jobID%03d",
integerValue(
"jobID"));
643 cout << outputFile <<
".log" << endl;
646 outputFile +=
".root";
647 if (modifyOutputFile) {
bool _runVariableCommandFromString(const std::string &arg)
SIMap::const_iterator SIMapConstIter
void _getSectionFiles(const SVec &inputList, SVec &outputList, int section, int totalSection)
void _finishDefaultOptions(std::string tag="")
unsigned int m_optionsType
bool _setVariablesFromFile(const std::string &filename)
static std::string removeEnding(const std::string &input, const std::string &ending)
SDMap::const_iterator SDMapConstIter
SSMap::iterator SSMapIter
SBMap::iterator SBMapIter
std::string m_usageString
SIMap::iterator SIMapIter
std::string & stringValue(std::string key)
friend std::ostream & operator<<(std::ostream &o_stream, const CommandLineParser &rhs)
void parseArguments(int argc, char **argv, bool allowArgs=false)
static void removeLeadingAndTrailingSpaces(std::string &line)
static void split(SVec &retval, std::string line, std::string match=" \t", bool ignoreComments=true)
SBMap::const_iterator SBMapConstIter
SIVecMap::iterator SIVecMapIter
outputList
All other commands ##.
SSVecMap::const_iterator SSVecMapConstIter
std::vector< std::string > SVec
static std::string const input
int & integerValue(std::string key)
SSMap::const_iterator SSMapConstIter
static const std::string kSpaces
SIVecMap::const_iterator SIVecMapConstIter
DVec & doubleVector(std::string key)
SBMap m_variableModifiedMap
static void lowercaseString(std::string &arg)
SSVecMap::iterator SSVecMapIter
SDVecMap::iterator SDVecMapIter
bool _setVariableFromString(const std::string &arg, bool dontOverrideChange=false, int offset=0)
SSMap m_variableDescriptionMap
bool _valueHasBeenModified(const std::string &key)
static void removeComment(std::string &line)
IVec & integerVector(std::string key)
SVec & stringVector(std::string key)
static void findCommand(const std::string &line, std::string &command, std::string &rest)
bool & boolValue(std::string key)
static int position[264][3]
void addOption(std::string key, OptionType type, const std::string &description="")
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
SDMap::iterator SDMapIter
static std::string const source
SDVecMap::const_iterator SDVecMapConstIter
SVec::const_iterator SVecConstIter
OptionType hasOption(std::string key)