rootana/src/framework/CommandLine.h File Reference

#include <string>

Go to the source code of this file.

Classes

struct  ARGUMENTS

Functions

void help_command_line (const char *my_name)
void print_arguments (const ARGUMENTS &args)
bool isNumber (const char *c)
int GetRunNumber (const std::string &file_name)
 Obtains the run number from a file name.
int check_arguments (ARGUMENTS &arguments)
int analyze_command_line (int argc, char **argv, ARGUMENTS &arguments)
int load_config_file (const char *filename)

Function Documentation

int analyze_command_line ( int  argc,
char **  argv,
ARGUMENTS arguments 
)

Definition at line 118 of file CommandLine.cpp.

References ARGUMENTS::calib, check_arguments(), ARGUMENTS::correction_file, help_command_line(), ARGUMENTS::infile, isNumber(), ARGUMENTS::mod_file, ARGUMENTS::outfile, ARGUMENTS::run, ARGUMENTS::start, and ARGUMENTS::stop.

Referenced by main().

00119 {
00120   // Inialise the arguments to be consistently meaningless
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   // Now loop over all the arguments
00130   // There are a minimum of seven arguments:
00131   // -i, -m, -o, and corresponding files, and
00132   // program name.
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; /* incrementing of i done in the loop! */){
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        //detemine how many integer arguments are associated with the -n
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    } // End switch block
00244   } // End for loop over all args
00245 
00246   // Everything looks ok, so we now check the arguments
00247   return check_arguments(arguments);
00248 }

int check_arguments ( ARGUMENTS arguments  ) 

Definition at line 71 of file CommandLine.cpp.

References ARGUMENTS::correction_file, GetRunNumber(), ARGUMENTS::infile, ARGUMENTS::mod_file, ARGUMENTS::outfile, ARGUMENTS::run, ARGUMENTS::start, and ARGUMENTS::stop.

Referenced by analyze_command_line().

00071                                          {
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     // No run number has been set, obtain it from the filename
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; //success
00109 }

int GetRunNumber ( const std::string &  file_name  ) 

Obtains the run number from a file name.

Assumes the file name is of the format aaaaaaSnnnnnn.xxx where: 'a' is any character 'S' is any non-numeric character 'n' is the run number (includes '+' '-' '.') 'x' is any character excluding '.'

Assumes the file name is of the format aaaaaaSnnnnnn.xxx where 'a' is any character 'S' is any non-numeric character 'n' is the run number (includes '+' '-' '.') 'x' is any character excluding '.'

Definition at line 45 of file CommandLine.cpp.

Referenced by check_arguments().

00046 {
00052 
00053   //remove the extension
00054   std::string name = file_name.substr(0,file_name.find_last_of('.'));
00055 
00056   //find the last numbers
00057   name = name.substr(name.find_last_not_of("01234566789")+1);
00058 
00059   //Convert the rest
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 }

void help_command_line ( const char *  my_name  ) 

Definition at line 12 of file CommandLine.cpp.

Referenced by analyze_command_line().

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 }

bool isNumber ( const char *  c  ) 

Definition at line 36 of file CommandLine.cpp.

Referenced by analyze_command_line().

00037 {
00038   for ( std::size_t i=0; i<strlen(c); i++){
00039     if(!isdigit(c[i])) return false;
00040   }
00041   return true;
00042 }

int load_config_file ( const char *  filename  ) 

Definition at line 251 of file CommandLine.cpp.

References TemplateFactory< BaseModule, OptionsType >::createModule(), modules::options::DumpOptions(), modules::reader::GetModule(), modules::reader::GetNumModules(), modules::reader::GetOptions(), modules::factory::Instance(), modules::reader::PrintAllOptions(), and modules::reader::ReadFile().

00251                                           {
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 }

void print_arguments ( const ARGUMENTS args  ) 

Definition at line 275 of file CommandLine.cpp.

References ARGUMENTS::calib, ARGUMENTS::correction_file, ARGUMENTS::infile, ARGUMENTS::mod_file, ARGUMENTS::outfile, ARGUMENTS::run, ARGUMENTS::start, and ARGUMENTS::stop.

00275                                            {
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 }


Generated on 15 Jun 2016 for AlcapDAQ by  doxygen 1.6.1