CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/TopQuarkAnalysis/TopHitFit/src/Defaults_Text.cc

Go to the documentation of this file.
00001 //
00002 // $Id: Defaults_Text.cc,v 1.1 2011/05/26 09:46:59 mseidel Exp $
00003 //
00004 // File: src/Defaults_Text.cc
00005 // Purpose: A lightweight implementation of the Defaults interface
00006 //          that uses simple text files.
00007 // Created: Jul, 2000, sss.
00008 //
00009 // CMSSW File      : src/Defaults_Text.cc
00010 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
00011 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
00012 //
00013 
00037 #include "TopQuarkAnalysis/TopHitFit/interface/Defaults_Text.h"
00038 #include <cassert>
00039 #include <cstdlib>
00040 #include <fstream>
00041 #include <iostream>
00042 #include <cctype>
00043 #include <cstring>
00044 #include <map>
00045 
00046 using std::cerr;
00047 using std::string;
00048 using std::ifstream;
00049 using std::getline;
00050 using std::isspace;
00051 using std::tolower;
00052 using std::atoi;
00053 using std::atof;
00054 using std::abort;
00055 using std::strchr;
00056 using std::map;
00057 
00058 namespace {
00059 
00067 string strip (string s)
00068 //
00069 // Purpose: Remove comments (text starting with `;' or `#') and leading
00070 //          and trailing spaces from S.
00071 //
00072 // Inputs:
00073 //   s -           The string to strip.
00074 //
00075 // Returns:
00076 //  The stripped string.
00077 //
00078 {
00079   string::size_type j = s.find_first_of (";#");
00080   if (j == string::npos)
00081     j = s.size();
00082 
00083   while (j > 0 && isspace (s[j-1]))
00084     --j;
00085 
00086   string::size_type i = 0;
00087   while (i < j && isspace (s[i]))
00088     ++i;
00089 
00090   return string (s, i, j-i);
00091 }
00092 
00093 
00094 } // unnamed namespace
00095 
00096 
00097 namespace hitfit {
00098 
00099 
00100 //***************************************************************************
00101 
00102 
00107 class Defaults_Textrep
00108 //
00109 // Purpose: The internal representation for a Defaults_Text object.
00110 //
00111 {
00112 public:
00113   // Constructor.
00114 
00122   Defaults_Textrep (string file);
00132   Defaults_Textrep (string file, int argc, char** argv);
00133 
00134   // The data.  Maps from parameter names to values (which are stored
00135   // as strings).
00140   std::map<std::string,std::string> _map;
00141 
00142   // Look up parameter NAME and return its value.
00149   string get_val (string name) const;
00150 
00151 
00152 private:
00153   // Read parameters from FILE and add them to our data.
00159   void read_file (string file);
00160 
00161   // Look for additional parameter settings in the argument list
00162   // ARGC, ARGV and add them to our data.
00169   void process_args (int argc, char** argv);
00170 
00171   // Helper to process a line defining a single parameter.
00176   void doline (string l);
00177 };
00178 
00179 
00180 Defaults_Textrep::Defaults_Textrep (string file)
00181 //
00182 // Purpose: Constructor.
00183 //
00184 // Inputs:
00185 //   file -        The name of the defaults file to read.
00186 //                 See the comments in the header for a description
00187 //                 of the format for this and for the argument list.
00188 //                 Pass an empty string to skip reading a file.
00189 //
00190 {
00191   read_file (file);
00192 }
00193 
00194 
00195 Defaults_Textrep::Defaults_Textrep (string file, int argc, char** argv)
00196 //
00197 // Purpose: Constructor.
00198 //
00199 // Inputs:
00200 //   file -        The name of the defaults file to read.
00201 //                 See the comments in the header for a description
00202 //                 of the format for this and for the argument list.
00203 //                 Pass an empty string to skip reading a file.
00204 //   argc -        The arglist length.
00205 //   argv -        The arglist.
00206 //
00207 {
00208   read_file (file);
00209   process_args (argc, argv);
00210 }
00211 
00212 
00213 void Defaults_Textrep::read_file (string file)
00214 //
00215 // Purpose: Read parameters from FILE and add them to our data.
00216 //
00217 // Inputs:
00218 //   s -           The name of the file to read.
00219 //
00220 {
00221   // Just return if we weren't given a file.
00222   if (file.size() == 0)
00223     return;
00224 
00225   ifstream f (file.c_str());
00226   if (!f.good()) {
00227     cerr << "Can't open " << file << "\n";
00228     abort ();
00229   }
00230 
00231   string l;
00232   while (getline (f, l)) {
00233     doline (l);
00234   }
00235 
00236   f.close ();
00237 }
00238 
00239 
00240 void Defaults_Textrep::process_args (int argc, char** argv)
00241 //
00242 // Purpose: Process the argument list ARGC, ARGV and add additional
00243 //          parameters from it to our data (possibly overriding
00244 //          existing settings).  See the header file for more details.
00245 //
00246 // Inputs:
00247 //   argc -        The arglist length.
00248 //   argv -        The arglist.
00249 //
00250 {
00251   // Look for arguments starting with `--'.
00252   for (int i=1; i < argc; i++) {
00253     if (argv[i][0] == '-' && argv[i][1] == '-') {
00254 
00255       // Found one. 
00256       string l;
00257       if (strchr (argv[i], '=') != 0)
00258         // It was of the form `--NAME=VALUE'.  Change to `NAME=VALUE'.
00259         l = argv[i] + 2;
00260       else if (argv[i][2] == 'n' && argv[i][3] == 'o') {
00261         // It was of the form `--noNAME'.  Change to `NAME=0'.
00262         l = argv[i] + 4;
00263         l += "=0";
00264       }
00265       else {
00266         // It was of the form `--NAME'.  Change to `NAME=1'. 
00267         l = argv[i] + 2;
00268         l += "=1";
00269       }
00270 
00271       // Process it like a line we read from a file.
00272       doline (l);
00273     }
00274   }
00275 }
00276 
00277 
00278 string Defaults_Textrep::get_val (string name) const
00279 //
00280 // Purpose: Look up parameter NAME and return its value.
00281 //          The parameter must exist.
00282 //
00283 // Inputs:
00284 //   name -        The name of the parameter.
00285 //
00286 // Returns:
00287 //   The value of the parameter.
00288 //
00289 {
00290 
00291     std::string val;
00292 
00293     if (_map.find(name) == _map.end()) {
00294     cerr << "can't find default for " << name << "\n";
00295     abort ();
00296     } else {
00297     std::map<string,string>::const_iterator it = _map.find(name);
00298     val = it->second;
00299     }
00300 
00301     return val;
00302 }
00303 
00304 
00305 void Defaults_Textrep::doline (string l)
00306 //
00307 // Purpose: Helper to process a line defining a single parameter.
00308 //
00309 // Inputs:
00310 //   l -           The line to process.
00311 //
00312 {
00313   // Strip spaces from the line and ignore it if it's blank.
00314   l = strip (l);
00315   if (l.size() == 0)
00316     return;
00317 
00318   // It must contain a `=' character.
00319   string::size_type pos = l.find ('=');
00320   if (pos == string::npos) {
00321     cerr << "bad defaults line " << l << "\n";
00322     abort ();
00323   }
00324 
00325   // Split off name and value parts.
00326   std::string name = strip (l.substr (0, pos));
00327   std::string val = strip (l.substr (pos+1));
00328 
00329   // Add it to the map.
00330   _map[name] = val;
00331 
00332 }
00333 
00334 
00335 //***************************************************************************
00336 
00337 
00338 Defaults_Text::Defaults_Text (std::string def_file)
00339 //
00340 // Purpose: Constructor.
00341 //
00342 // Inputs:
00343 //   def_file -    The name of the defaults file to read.
00344 //                 See the comments in the header for a description
00345 //                 of the format for this and for the argument list.
00346 //                 Pass an empty string to skip reading a file.
00347 //
00348   : _rep (new Defaults_Textrep (def_file))
00349 {
00350 }
00351 
00352 
00353         Defaults_Text::Defaults_Text (std::string def_file, int argc, char** argv)
00354 //
00355 // Purpose: Constructor.
00356 //
00357 // Inputs:
00358 //   def_file -    The name of the defaults file to read.
00359 //                 See the comments in the header for a description
00360 //                 of the format for this and for the argument list.
00361 //                 Pass an empty string to skip reading a file.
00362 //   argc -        The arglist length.
00363 //   argv -        The arglist.
00364 //
00365   : _rep (new Defaults_Textrep (def_file, argc, argv))
00366 {
00367 }
00368 
00369 Defaults_Text::~Defaults_Text ()
00370 //
00371 // Purpose: Destructor.
00372 //
00373 {
00374   delete _rep;
00375 }
00376 
00377 
00378 bool Defaults_Text::exists (std::string name) const
00379 //
00380 // Purpose: Test to see if parameter NAME exists.
00381 //
00382 // Inputs:
00383 //   name -        The name of the parameter to look up.
00384 //
00385 // Returns:
00386 //   True if NAME exists.
00387 //
00388 {
00389   std::string val;
00390   return (_rep->_map.find(name) != _rep->_map.end());
00391 
00392 }
00393 
00394 
00395 int Defaults_Text::get_int (std::string name) const
00396 //
00397 // Purpose: Get the value of NAME as an integer.
00398 //
00399 // Inputs:
00400 //   name -        The name of the parameter to look up.
00401 //
00402 // Returns:
00403 //   The parameter's value as an integer.
00404 //
00405 {
00406   return atoi (_rep->get_val (name).c_str());
00407 }
00408 
00409 
00410 double Defaults_Text::get_float (std::string name) const
00411 //
00412 // Purpose: Get the value of NAME as a float.
00413 //
00414 // Inputs:
00415 //   name -        The name of the parameter to look up.
00416 //
00417 // Returns:
00418 //   The parameter's value as a float.
00419 //
00420 {
00421   return atof (_rep->get_val (name).c_str());
00422 }
00423 
00424 
00425 bool Defaults_Text::get_bool (std::string name) const
00426 //
00427 // Purpose: Get the value of NAME as a bool.
00428 //
00429 // Inputs:
00430 //   name -        The name of the parameter to look up.
00431 //
00432 // Returns:
00433 //   The parameter's value as a bool.
00434 //
00435 {
00436   string val = _rep->get_val (name);
00437   if (tolower (val[0]) == 't' || tolower (val[0]) == 'y')
00438     return true;
00439   else if (tolower (val[0]) == 'f' || tolower (val[0]) == 'n')
00440     return false;
00441   return !!get_int (name);
00442 }
00443 
00444 
00445 string Defaults_Text::get_string (std::string name) const
00446 //
00447 // Purpose: Get the value of NAME as a string.
00448 //
00449 // Inputs:
00450 //   name -        The name of the parameter to look up.
00451 //
00452 // Returns:
00453 //   The parameter's value as a string.
00454 //
00455 {
00456   return _rep->get_val (name);
00457 }
00458 
00459 
00460 std::ostream& operator<< (std::ostream& s, const Defaults_Text& def)
00461 //
00462 // Purpose: Dump out all parameter settings.
00463 //
00464 // Inputs:
00465 //   s -           The stream to which we're writing.
00466 //   def -         The instance to dump.
00467 //
00468 // Returns:
00469 //   The stream S.
00470 //
00471 {
00472 
00473     for (std::map<std::string,std::string>::const_iterator it = def._rep->_map.begin() ;
00474      it != def._rep->_map.end() ;
00475      it++) {
00476          s << "[" << it->first << "] = [" << it->second << "]\n";
00477     }
00478 
00479   return s;
00480 }
00481 
00482 
00483 } // namespace hitfit