SISCone  2.0.5
examples/options.cpp
00001 
00002 // File: options.cpp                                                         //
00003 // Description: management of the cmdline options of the main program        //
00004 // This file is part of the SISCone project.                                 //
00005 // For more details, see http://projects.hepforge.org/siscone                //
00006 //                                                                           //
00007 // Copyright (c) 2006 Gavin Salam and Gregory Soyez                          //
00008 //                                                                           //
00009 // This program is free software; you can redistribute it and/or modify      //
00010 // it under the terms of the GNU General Public License as published by      //
00011 // the Free Software Foundation; either version 2 of the License, or         //
00012 // (at your option) any later version.                                       //
00013 //                                                                           //
00014 // This program is distributed in the hope that it will be useful,           //
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of            //
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             //
00017 // GNU General Public License for more details.                              //
00018 //                                                                           //
00019 // You should have received a copy of the GNU General Public License         //
00020 // along with this program; if not, write to the Free Software               //
00021 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA //
00022 //                                                                           //
00023 // $Revision:: 213                                                          $//
00024 // $Date:: 2008-03-17 17:05:59 +0100 (Mon, 17 Mar 2008)                     $//
00026 
00027 #include "options.h"
00028 #include <string.h>
00029 #include <getopt.h>
00030 #include <iostream>
00031 
00032 using namespace std;
00033 using namespace siscone;
00034 
00035 #define N_DEFAULT          -1
00036 #define R_DEFAULT          0.7
00037 #define THRESHOLD_DEFAULT  0.5
00038 #define PTMIN_DEFAULT      0.0
00039 #define NPASS_DEFAULT      0
00040 #define DEFAULT_EVENT      "events/single-event.dat"
00041 #define SM_DEFAULT         SM_pttilde
00042 
00043 /*******************************************
00044  * Coptions implementation                 *
00045  * options for the 'cone' sample           *
00046  *******************************************/
00047 
00048 // default ctor
00049 //--------------
00050 Coptions::Coptions(){
00051   // set default flags values
00052   help_flag=0;
00053   version_flag=0;
00054   verbose_flag=1;
00055 
00056   // set default options values
00057   N_stop = N_DEFAULT;
00058   R = R_DEFAULT;
00059   f = THRESHOLD_DEFAULT;
00060   npass = NPASS_DEFAULT;
00061   ev_name = NULL;
00062   SM_var = SM_DEFAULT;
00063 }
00064 
00065 
00066 // default dtor
00067 //--------------
00068 Coptions::~Coptions(){
00069   if (ev_name!=NULL)
00070     delete[] ev_name;
00071 }
00072 
00073 
00074 // parse oprions
00075 //  - argc  number of arguments from the command line
00076 //  - argv  arguments from the command line
00077 // return 1 on error, 0 on success
00078 //---------------------------------
00079 int Coptions::parse_options(int argc, char **argv){
00080   int opt_param;
00081   int option_index;
00082   bool stop=false;
00083 
00084   // browse the command-line options{
00085   static struct option siscone_options[]={
00086     // options that set a flag
00087     {"verbose",  no_argument, &verbose_flag, 1},
00088     {"quiet",    no_argument, &verbose_flag, 0},
00089     {"help",     no_argument, &help_flag   , 1},
00090     {"version",  no_argument, &version_flag, 1},
00091     // options setting parameters
00092     {"number",   required_argument, NULL, 'N'},
00093     {"radius",   required_argument, NULL, 'R'},
00094     {"fraction", required_argument, NULL, 'f'},
00095     {"ptmin",    required_argument, NULL, 'p'},
00096     {"npass",    required_argument, NULL, 'n'},
00097     {"event",    required_argument, NULL, 'e'},
00098     {"sm",       required_argument, NULL, 's'},
00099     {0,0,0,0}
00100   };
00101 
00102   
00103   do{
00104     // getopt_long stores the option index here.
00105     option_index=0;
00106 
00107     // retreive options
00108     opt_param = getopt_long(argc, argv, "hvqN:R:f:p:n:e:s:", 
00109                             siscone_options, &option_index);
00110 
00111     // Detect the end of the options.
00112     if (opt_param == -1)
00113       stop=true;
00114 
00115     // branch according to 'opt_param'
00116     switch (opt_param){
00117     case 'h': help_flag = 1;      break;  // help
00118     case 'v': verbose_flag = 1;   break;  // verbose
00119     case 'q': verbose_flag = 0;   break;  // quiet
00120     case 'N':  // max number of paprticles
00121       sscanf(optarg, "%d", &N_stop);
00122       if (N_stop<=0){
00123         cout << "Warning: the specified number of particles must be positive. Using default one" << endl;
00124         N_stop = N_DEFAULT;
00125       }
00126       break;
00127     case 'R':
00128       sscanf(optarg, "%lf", &R);
00129       if (R<=0){
00130         cout << "Warning: the specified cone radius must be positive. Using default one" << endl;
00131         R = R_DEFAULT;
00132       }      
00133       break;
00134     case 'f':
00135       sscanf(optarg, "%lf", &f);
00136       if ((f<0) || (f>1)){
00137         cout << "Warning: the specified split/merge threshold must be in [0,1]. Using default one" << endl;
00138         f = THRESHOLD_DEFAULT;
00139       }            
00140       break;
00141     case 'p':
00142       sscanf(optarg, "%lf", &ptmin);
00143       if (ptmin<0){
00144         cout << "Warning: the specified minimal pT must be non-negative. Using default one" << endl;
00145         ptmin = PTMIN_DEFAULT;
00146       }            
00147       break;
00148     case 'n':  // max number of paprticles
00149       sscanf(optarg, "%d", &npass);
00150       if (npass<0){
00151         cout << "Warning: the specified number of passes must be non negative. Using default one" << endl;
00152         npass = NPASS_DEFAULT;
00153       }
00154       break;
00155     case 'e':
00156       if (ev_name==NULL){
00157         ev_name = new char[strlen(optarg)+1];
00158         strcpy(ev_name, optarg);
00159       }
00160       break;
00161     case 's':
00162       char tmp[512];
00163       strcpy(tmp, optarg);
00164       if (strcmp(tmp, "pttilde")==0){
00165         SM_var = SM_pttilde;
00166       } else if (strcmp(tmp, "mt")==0){
00167         SM_var = SM_mt;
00168       } else if (strcmp(tmp, "pt")==0){
00169         SM_var = SM_pt;
00170       } else if (strcmp(tmp, "Et")==0){
00171         SM_var = SM_Et;
00172       } else {
00173         cout << "Warning: the specified varible for split--merge is not valid (should be pttilde, pt, mt or Et). Using pttilde as the default one." << endl;
00174         SM_var = SM_pttilde;
00175       }
00176       break;
00177     case 0:
00178     case -1:
00179       break;
00180     case '?':
00181       fprintf(stderr, "Giving up.\n");
00182       return 1;
00183       break;
00184     default:
00185       if (!help_flag){
00186         fprintf(stderr, "unrecognized option %c. Giving up.\n", opt_param);
00187         return 1;
00188       }
00189     }
00190   } while (!stop);
00191   
00192   if (ev_name==NULL){
00193     ev_name = new char[strlen(DEFAULT_EVENT)+1];
00194     strcpy(ev_name, DEFAULT_EVENT);
00195   }
00196   
00197   return 0;
00198 }
00199 
00200 
00201 // print the help message
00202 //------------------------
00203 int Coptions::print_help(){
00204   cout << siscone_package_name() << " " << siscone_version() << endl;
00205   cout << "Usage: " << siscone_package_name() << " <args>" << endl;
00206   cout << endl;
00207   cout << "Here is an exhaustive list of the arguments:" << endl;
00208   cout << "Parameters control (with default values):" << endl;
00209   cout << "  -n <val>, --number=<val>  : set the maximum number of particles allowed (all)" << endl;
00210   cout << "  -R <val>, --radius=<val>  : set the radius (" << R_DEFAULT << ")" << endl;
00211   cout << "  -f <val>, --fraction=<val>: set the overlap parameter (" << THRESHOLD_DEFAULT << ")" << endl;
00212   cout << "  -p <val>, --ptmin=<val>   : set the minimal pT for protojets (" << PTMIN_DEFAULT << ")" << endl;
00213   cout << "  -n <val>, --npass=<val>   : set the maximal number of passes (0 for no limit) (" << NPASS_DEFAULT << ")" << endl;
00214   cout << "  -e <val>, --event=<val>   : set the event filename (" << DEFAULT_EVENT << ")" << endl;
00215   cout << "  -s <val>, --sm=<val>      : variable for split--merge: pttilde, mt, pt or Et (pttilde)" << endl;
00216   cout << endl;
00217   cout << "Output flags" << endl;
00218   cout << "  --version    : show version information" << endl;
00219   cout << "  -h, --help   : show this message" << endl;
00220   cout << "  -v, --verbose: be verbose (on by default)" << endl;
00221   cout << "  -q, --quiet  : be quiet" << endl;
00222   cout << endl;
00223 
00224   return 0;
00225 }
00226 
00227 
00228 // print program version
00229 //-----------------------
00230 int Coptions::print_version(){
00231   cout << siscone_package_name() << " " << siscone_version() << endl;
00232   cout << "Copyright (C) 2006." << endl;
00233   cout << siscone_package_name() << " comes with NO WARRANTY," << endl;
00234   cout << "to the extent permitted by law." << endl;
00235   cout << "You may redistribute copies of " << siscone_package_name() << endl;
00236   cout << "under the terms of the GNU General Public License." << endl;
00237   cout << "For more information about these matters," << endl;
00238   cout << "see the files named COPYING." << endl;
00239   cout << "Please send bugs or comments to AUTHORS" << endl;
00240 
00241   return 0;
00242 }
The SISCone project has been developed by Gavin Salam and Gregory Soyez
Documentation generated on Mon Jun 4 2012 18:23:38 for SISCone by  Doxygen 1.7.6.1