Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00070
00071
00072
00073
00074
00075
00076
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 }
00095
00096
00097 namespace hitfit {
00098
00099
00100
00101
00102
00107 class Defaults_Textrep
00108
00109
00110
00111 {
00112 public:
00113
00114
00122 Defaults_Textrep (string file);
00132 Defaults_Textrep (string file, int argc, char** argv);
00133
00134
00135
00140 std::map<std::string,std::string> _map;
00141
00142
00149 string get_val (string name) const;
00150
00151
00152 private:
00153
00159 void read_file (string file);
00160
00161
00162
00169 void process_args (int argc, char** argv);
00170
00171
00176 void doline (string l);
00177 };
00178
00179
00180 Defaults_Textrep::Defaults_Textrep (string file)
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 {
00191 read_file (file);
00192 }
00193
00194
00195 Defaults_Textrep::Defaults_Textrep (string file, int argc, char** argv)
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 {
00208 read_file (file);
00209 process_args (argc, argv);
00210 }
00211
00212
00213 void Defaults_Textrep::read_file (string file)
00214
00215
00216
00217
00218
00219
00220 {
00221
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
00243
00244
00245
00246
00247
00248
00249
00250 {
00251
00252 for (int i=1; i < argc; i++) {
00253 if (argv[i][0] == '-' && argv[i][1] == '-') {
00254
00255
00256 string l;
00257 if (strchr (argv[i], '=') != 0)
00258
00259 l = argv[i] + 2;
00260 else if (argv[i][2] == 'n' && argv[i][3] == 'o') {
00261
00262 l = argv[i] + 4;
00263 l += "=0";
00264 }
00265 else {
00266
00267 l = argv[i] + 2;
00268 l += "=1";
00269 }
00270
00271
00272 doline (l);
00273 }
00274 }
00275 }
00276
00277
00278 string Defaults_Textrep::get_val (string name) const
00279
00280
00281
00282
00283
00284
00285
00286
00287
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
00308
00309
00310
00311
00312 {
00313
00314 l = strip (l);
00315 if (l.size() == 0)
00316 return;
00317
00318
00319 string::size_type pos = l.find ('=');
00320 if (pos == string::npos) {
00321 cerr << "bad defaults line " << l << "\n";
00322 abort ();
00323 }
00324
00325
00326 std::string name = strip (l.substr (0, pos));
00327 std::string val = strip (l.substr (pos+1));
00328
00329
00330 _map[name] = val;
00331
00332 }
00333
00334
00335
00336
00337
00338 Defaults_Text::Defaults_Text (std::string def_file)
00339
00340
00341
00342
00343
00344
00345
00346
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
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 : _rep (new Defaults_Textrep (def_file, argc, argv))
00366 {
00367 }
00368
00369 Defaults_Text::~Defaults_Text ()
00370
00371
00372
00373 {
00374 delete _rep;
00375 }
00376
00377
00378 bool Defaults_Text::exists (std::string name) const
00379
00380
00381
00382
00383
00384
00385
00386
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
00398
00399
00400
00401
00402
00403
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
00413
00414
00415
00416
00417
00418
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
00428
00429
00430
00431
00432
00433
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
00448
00449
00450
00451
00452
00453
00454
00455 {
00456 return _rep->get_val (name);
00457 }
00458
00459
00460 std::ostream& operator<< (std::ostream& s, const Defaults_Text& def)
00461
00462
00463
00464
00465
00466
00467
00468
00469
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 }