00001 #include <string.h>
00002 #include "ModulesReader.h"
00003 #include "ModulesFactory.h"
00004 #include "CommandLine.h"
00005 #include <stdio.h>
00006 #include <string>
00007 #include <sstream>
00008 #include <cstdlib>
00009 #include <iostream>
00010 #include <stdexcept>
00011
00012 void help_command_line(const char* my_name)
00013 {
00014 std::cerr << "\nUsage: " << my_name << " [options]\n"
00015 << " Positional arguments: None\n\n"
00016 << "Valid options are:\n"
00017 << " -i <filename> Input root tree file.\n"
00018 << " -o <filename> Output root tree file.\n"
00019 << " -n <count> Analyze only <count> events.\n"
00020 << " -n <first> <last> Analyze only events from "
00021 << "<first> to <last>.\n"
00022 << " -r <PSI run number> Run number specification for the shrubs.\n"
00023 << " -s <correction file> Name of the correction file to be used.\n"
00024 << " -m <modules file> Name of the MODULES file to be used.\n"
00025 << " -c Tell the setup navigator that this is a calibration\n"
00026 << " run, and it might not find all of the tables it needs\n"
00027 << " in the calibration database\nbut to continue all the\n"
00028 << " same."
00029 << std::endl;
00030 return;
00031 }
00032
00033
00034
00035
00036 bool isNumber(const char* c)
00037 {
00038 for ( std::size_t i=0; i<strlen(c); i++){
00039 if(!isdigit(c[i])) return false;
00040 }
00041 return true;
00042 }
00043
00044
00045 int GetRunNumber(const std::string& file_name)
00046 {
00052
00053
00054 std::string name = file_name.substr(0,file_name.find_last_of('.'));
00055
00056
00057 name = name.substr(name.find_last_not_of("01234566789")+1);
00058
00059
00060 int number (-1);
00061 std::stringstream(name) >> number;
00062 if (number == -1) {
00063 std::cerr << "Unable to deduce run number from \"" << name
00064 << "\" in \"" << file_name << "\"" << std::endl;
00065 throw std::invalid_argument("not a valid int");
00066 }
00067 return number;
00068 }
00069
00070
00071 int check_arguments(ARGUMENTS& arguments){
00072 if(arguments.stop > 0){
00073 if (arguments.stop <= arguments.start){
00074 std::cerr << "ERROR: Cannot process events from "
00075 << arguments.start << " to " << arguments.stop
00076 << " because " << arguments.start << ">=" << arguments.stop
00077 << std::endl;
00078 return 1;
00079 }
00080 }
00081
00082 if(arguments.infile.size() == 0){
00083 std::cerr << "ERROR: Empty input file name."
00084 <<" Did you specify the -i option?" << std::endl;
00085 return 2;
00086 }
00087
00088 if(arguments.outfile.size() == 0){
00089 std::cerr << "ERROR: Empty output file name."
00090 << " Did you specify the -o option?\n" << std::endl;
00091 return 3;
00092 }
00093
00094 if(arguments.run == -1){
00095
00096 arguments.run = GetRunNumber(arguments.infile);
00097 }
00098
00099 if(arguments.mod_file.size() == 0){
00100 arguments.mod_file = "testModule.txt";
00101 }
00102
00103 if(arguments.correction_file.size() == 0){
00104 std::stringstream buff("");
00105 buff << "wiremap_corrections/correct" << arguments.run << ".dat";
00106 arguments.correction_file=buff.str();
00107 }
00108 return 0;
00109 }
00110
00111
00112 void PrintLocation()
00113 {
00114 static int count=0;
00115 std::cout << "At location" << count++ << std::endl;
00116 }
00117
00118 int analyze_command_line (int argc, char **argv, ARGUMENTS& arguments)
00119 {
00120
00121 arguments.infile="";
00122 arguments.outfile="";
00123 arguments.correction_file="";
00124 arguments.start=0;
00125 arguments.stop=0;
00126 arguments.run=-1;
00127 arguments.calib=false;
00128
00129
00130
00131
00132
00133 if(argc < 7 || std::string(argv[1]) == "--help") {
00134 help_command_line(argv[0]);
00135 return 1;
00136 }
00137
00138 for(int i=1; i<argc; ){
00139 if(argv[i][0] != '-'){
00140 std::cerr << "ERROR: Wrong argument " << argv[i] << std::endl;
00141 help_command_line(argv[0]);
00142 return 1;
00143 }
00144
00145 if(strlen(&argv[i][1]) != 1){
00146 std::cerr << "ERROR: All options must be single characters, "
00147 << "separated with a space and prefixed with a '-'"
00148 << std::endl;
00149 help_command_line(argv[0]);
00150 return 1;
00151 }
00152
00153 switch(argv[i][1]){
00154 case 'm':
00155 if(i+1 < argc){
00156 arguments.mod_file = argv[i+1];
00157 i+=2;
00158 }
00159 else {
00160 std::cerr << "ERROR: No argument for module file specified\n";
00161 help_command_line(argv[0]); return 1;
00162 }
00163 break;
00164
00165
00166 case 'i':
00167 if(i+1 < argc){
00168 arguments.infile = argv[i+1];
00169 i+=2;
00170 }
00171 else{
00172 std::cerr << "ERROR: No argument for input file specified\n";
00173 help_command_line(argv[0]); return 1;
00174 }
00175 break;
00176
00177
00178 case 'o':
00179 if(i+1 < argc){
00180 arguments.outfile = argv[i+1];
00181 i+=2;
00182 }
00183 else{
00184 std::cerr << "ERROR: No argument for input file specified\n";
00185 help_command_line(argv[0]); return 1;
00186 }
00187 break;
00188
00189
00190 case 's':
00191 if(i+1 < argc){
00192 arguments.correction_file = argv[i+1];
00193 i+=2;
00194 }
00195 else{
00196 std::cerr << "ERROR: No argument for TSetupData correction file specified\n";
00197 help_command_line(argv[0]); return 1;
00198 }
00199 break;
00200
00201
00202 case 'n':
00203 {
00204
00205 int nArgs = 0;
00206 for (int j = 1; j < argc-i; ++j) {
00207 if ( !isNumber(argv[i+j]) ) break;
00208 ++nArgs;
00209 }
00210 if ( nArgs > 2 || nArgs < 1) {
00211 std::cerr << "ERROR: " << nArgs
00212 << " non-negative integer arguments passed to"
00213 << " -n option, which accepts only one or two";
00214 help_command_line(argv[0]); return 1;
00215 }
00216 arguments.start = (nArgs==2) ? atoi(argv[i+1]) : 0;
00217 arguments.stop = (nArgs==2) ? atoi(argv[i+2]) : atoi(argv[i+1]);
00218 i += (nArgs+1);
00219 }
00220 break;
00221
00222 case 'r':
00223 if(i+1 < argc){
00224 if(isNumber(argv[i+1])){
00225 arguments.run = atoi(argv[i+1]);
00226 i+=2;
00227 }
00228 else{
00229 std::cerr << "ERROR: Argument " << argv[i+1]
00230 << " for option -r is not a number\n";
00231 help_command_line(argv[0]); return 1;
00232 }
00233 }
00234 break;
00235 case 'c':
00236 arguments.calib = true;
00237 i+=1;
00238 break;
00239
00240 default:
00241 std::cerr << "ERROR: Argument " << argv[i] << " not recognized\n";
00242 help_command_line(argv[0]); return 1;
00243 }
00244 }
00245
00246
00247 return check_arguments(arguments);
00248 }
00249
00250
00251 int load_config_file(const char* filename){
00252 modules::reader modules_file;
00253 modules_file.ReadFile(filename);
00254 modules_file.PrintAllOptions();
00255
00256 modules::factory* mgr = modules::factory::Instance();
00257 size_t num_modules = modules_file.GetNumModules();
00258 std::cout << "number of modules requested: " << num_modules << std::endl;
00259 std::string name;
00260 modules::options* opts;
00261 modules::BaseModule** mods=NULL;
00262 for(unsigned i = 0; i < num_modules; i++){
00263 name = modules_file.GetModule(i);
00264 opts = modules_file.GetOptions(i);
00265 std::cout << "Creating module: " << name
00266 << "\nWith options:" << std::endl;
00267 opts->DumpOptions(" ");
00268 mods[i] = mgr->createModule(name,opts);
00269 }
00270
00271 return 0;
00272 }
00273
00274
00275 void print_arguments(const ARGUMENTS& args){
00276 std::cout << "ARGUMENTS struct:"
00277 << "\n infile:\t\t" << args.infile
00278 << "\n outfile:\t\t" << args.outfile
00279 << "\n correction file:\t" << args.correction_file
00280 << "\n mod file:\t\t" << args.mod_file
00281 << "\n start event:\t" << args.start
00282 << "\n stop event:\t\t" << args.stop
00283 << "\n run number:\t\t" << args.run
00284 << "\n calibration:\t " << args.calib
00285 << std::endl;
00286 }