CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/CondTools/Hcal/bin/hcalCalibrationsCopy.cc

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 #include <sstream>
00006 #include <vector>
00007 #include <map>
00008 #include <algorithm>
00009 #include <string>
00010 
00011 // other
00012 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00013 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
00014 #include "DataFormats/HcalDetId/interface/HcalElectronicsId.h"
00015 #include "Geometry/CaloTopology/interface/HcalTopology.h"
00016 
00017 
00018 #include "CondCore/IOVService/interface/IOV.h"
00019 
00020 // Hcal calibrations
00021 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbHardcode.h"
00022 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbASCIIIO.h"
00023 #include "CondTools/Hcal/interface/HcalDbXml.h"
00024 #include "CondTools/Hcal/interface/HcalDbOnline.h"
00025 #include "CondTools/Hcal/interface/HcalDbTool.h"
00026 //#include "CondTools/Hcal/interface/HcalDbPoolOCCI.h"
00027 #include "CondFormats/HcalObjects/interface/HcalPedestals.h"
00028 #include "CondFormats/HcalObjects/interface/HcalPedestalWidths.h"
00029 #include "CondFormats/HcalObjects/interface/HcalGains.h"
00030 #include "CondFormats/HcalObjects/interface/HcalGainWidths.h"
00031 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
00032 #include "CondFormats/HcalObjects/interface/HcalChannelQuality.h"
00033 #include "CondFormats/HcalObjects/interface/HcalQIEData.h"
00034 #include "CondFormats/HcalObjects/interface/HcalCalibrationQIEData.h"
00035 
00036 // CMSSW Message service
00037 #include "FWCore/MessageService/interface/MessageServicePresence.h"
00038 
00039 //using namespace cms;
00040 
00041 
00042 typedef HcalDbTool::IOVRun IOVRun;
00043 typedef std::map<IOVRun,std::string> IOVCollection;
00044 
00045 
00046 class Args {
00047  public:
00048   Args () {};
00049   ~Args () {};
00050   void defineOption (const std::string& fOption, const std::string& fComment = "");
00051   void defineParameter (const std::string& fParameter, const std::string& fComment = "");
00052   void parse (int nArgs, char* fArgs []);
00053   void printOptionsHelp () const;
00054   std::string command () const;
00055   std::vector<std::string> arguments () const;
00056   bool optionIsSet (const std::string& fOption) const;
00057   std::string getParameter (const std::string& fKey);
00058  private:
00059   std::string mProgramName;
00060   std::vector <std::string> mOptions;
00061   std::vector <std::string> mParameters;
00062   std::vector <std::string> mArgs;
00063   std::map <std::string, std::string> mParsed;
00064   std::map <std::string, std::string> mComments;
00065 };
00066 
00067 template <class T>
00068 std::vector<HcalDetId> undefinedCells (const T& fData) {
00069   static std::vector<HcalDetId> result;
00070   if (result.size () <= 0) {
00071     HcalTopology topology;
00072     for (int eta = -63; eta < 64; eta++) {
00073       for (int phi = 0; phi < 128; phi++) {
00074         for (int depth = 1; depth < 5; depth++) {
00075           for (int det = 1; det < 5; det++) {
00076             HcalDetId cell ((HcalSubdetector) det, eta, phi, depth);
00077             try {
00078               fData.getValues (cell);
00079             }
00080             catch (...) {
00081               if (topology.valid(cell)) result.push_back (cell);
00082             }
00083           }
00084         }
00085       }
00086     }
00087   }
00088   return result;
00089 }
00090 
00091 void fillDefaults (HcalPedestals*& fPedestals) {
00092   if (!fPedestals) {
00093     fPedestals = new HcalPedestals;
00094     fPedestals->sort ();
00095   }
00096   std::vector<HcalDetId> cells = undefinedCells (*fPedestals);
00097   for (std::vector <HcalDetId>::const_iterator cell = cells.begin (); cell != cells.end (); cell++) {
00098     HcalPedestal item = HcalDbHardcode::makePedestal (*cell, false); // do not smear
00099     fPedestals->addValue (*cell, item.getValues ());
00100   }
00101   fPedestals->sort ();
00102 }
00103 
00104 void fillDefaults (HcalPedestalWidths*& fPedestals) {
00105   if (!fPedestals) {
00106     fPedestals = new HcalPedestalWidths;
00107     fPedestals->sort ();
00108   }
00109   std::vector<HcalDetId> cells = undefinedCells (*fPedestals);
00110   for (std::vector <HcalDetId>::const_iterator cell = cells.begin (); cell != cells.end (); cell++) {
00111     HcalPedestalWidth item = HcalDbHardcode::makePedestalWidth (*cell);
00112     fPedestals->setWidth (item);
00113   }
00114   fPedestals->sort ();
00115 }
00116 
00117 void fillDefaults (HcalGains*& fGains) {
00118   if (!fGains) {
00119     fGains = new HcalGains;
00120     fGains->sort ();
00121   }
00122   std::vector<HcalDetId> cells = undefinedCells (*fGains);
00123   for (std::vector <HcalDetId>::const_iterator cell = cells.begin (); cell != cells.end (); cell++) {
00124     HcalGain item = HcalDbHardcode::makeGain (*cell, false); // do not smear
00125     fGains->addValue (*cell, item.getValues ());
00126   }
00127   fGains->sort ();
00128 }
00129 
00130 void fillDefaults (HcalGainWidths*& fGains) {
00131   if (!fGains) {
00132     fGains = new HcalGainWidths;
00133     fGains->sort ();
00134   }
00135   std::vector<HcalDetId> cells = undefinedCells (*fGains);
00136   for (std::vector <HcalDetId>::const_iterator cell = cells.begin (); cell != cells.end (); cell++) {
00137     HcalGainWidth item = HcalDbHardcode::makeGainWidth (*cell);
00138     fGains->addValue (*cell, item.getValues ());
00139   }
00140   fGains->sort ();
00141 }
00142 
00143 void fillDefaults (HcalElectronicsMap*& fMap) {
00144   if (!fMap) {
00145     fMap = new HcalElectronicsMap;
00146     fMap->sort ();
00147   }
00148   std::cerr << "Warning: fillDefaults (HcalElectronicsMap* fMap) is not implemented. Ignore." << std::endl;
00149 }
00150 
00151 void fillDefaults (HcalQIEData*& fObject) {
00152   if (!fObject) {
00153     fObject = new HcalQIEData;
00154     fObject->sort ();
00155   }
00156   HcalTopology topology;
00157   for (int eta = -63; eta < 64; eta++) {
00158     for (int phi = 0; phi < 128; phi++) {
00159       for (int depth = 1; depth < 5; depth++) {
00160         for (int det = 1; det < 5; det++) {
00161           HcalDetId cell ((HcalSubdetector) det, eta, phi, depth);
00162           if (topology.valid(cell)) {
00163             HcalQIECoder item = HcalDbHardcode::makeQIECoder (cell); 
00164             fObject->addCoder (item);
00165           }
00166         }
00167       }
00168     }
00169   }
00170   fObject->sort ();
00171 }
00172 
00173 void fillDefaults (HcalCalibrationQIEData*& fObject) {
00174   if (!fObject) {
00175     fObject = new HcalCalibrationQIEData;
00176     fObject->sort ();
00177   }
00178   HcalTopology topology;
00179   for (int eta = -63; eta < 64; eta++) {
00180     for (int phi = 0; phi < 128; phi++) {
00181       for (int depth = 1; depth < 5; depth++) {
00182         for (int det = 1; det < 5; det++) {
00183           HcalDetId cell ((HcalSubdetector) det, eta, phi, depth);
00184           if (topology.valid(cell)) {
00185             HcalCalibrationQIECoder item = HcalDbHardcode::makeCalibrationQIECoder (cell); 
00186             fObject->addCoder (item);
00187           }
00188         }
00189       }
00190     }
00191   }
00192   fObject->sort ();
00193 }
00194 
00195 void printHelp (const Args& args) {
00196   char buffer [1024];
00197   std::cout << "Tool to manipulate by Hcal Calibrations" << std::endl;
00198   std::cout << "    feedback -> ratnikov@fnal.gov" << std::endl;
00199   std::cout << "Use:" << std::endl;
00200   sprintf (buffer, " %s <what> <options> <parameters>\n", args.command ().c_str());
00201   std::cout << buffer;
00202   std::cout << "  where <what> is: \n    pedestals\n    gains\n    pwidths\n    gwidths\n    emap\n    qie\n    calibqie" << std::endl;
00203   args.printOptionsHelp ();
00204 }
00205 
00206 bool defaultsFile (const std::string fParam) {
00207   return fParam == "defaults";
00208 }
00209 
00210 bool asciiFile (const std::string fParam) {
00211   return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".txt";
00212 }
00213 
00214 bool xmlFile (const std::string fParam) {
00215   return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".xml";
00216 }
00217 
00218 bool dbFile (const std::string fParam) {
00219   return fParam.find (':') != std::string::npos;
00220 }
00221 
00222 bool occiFile (const std::string fParam) {
00223   return fParam.find ("cms_val_lb.cern.ch") != std::string::npos &&
00224     fParam.find (':') == std::string::npos;
00225 }
00226 
00227 bool onlineFile (const std::string fParam) {
00228   return fParam.find ('@') != std::string::npos &&
00229     fParam.find ("cms_val_lb") == std::string::npos;
00230 }
00231 
00232 template <class T> bool copyObject (T* fObject, 
00233                                     const std::string& fInput, const std::string& fInputTag, HcalDbTool::IOVRun fInputRun,
00234                                     const std::string& fOutput, const std::string& fOutputTag, HcalDbTool::IOVRun fOutputRun,
00235                                     bool fAppend,
00236                                     unsigned long long fIovgmtbegin, unsigned long long fIovgmtend,
00237                                     unsigned fNread, unsigned fNwrite, unsigned fNtrace,
00238                                     bool fVerbose,
00239                                     const char* fInputCatalog, const char* fOutputCatalog, bool fXmlAuth
00240                                     ) {
00241   typedef std::vector <std::pair<HcalDbTool::IOVRun, T*> > Objects;
00242 
00243   bool result = false;
00244   time_t t0 = time (0);
00245   time_t t1 = t0;
00246   unsigned traceCounter = 0;
00247   HcalDbTool* poolDb = 0;
00248   HcalDbOnline* onlineDb = 0;
00249   //  HcalDbToolOCCI* occiDb = 0;
00250   Objects allInstances;
00251   while (traceCounter < fNread) {
00252     delete fObject;
00253     // get input
00254     if (defaultsFile (fInput)) {
00255       if (!traceCounter) std::cout << "USE INPUT: defaults" << std::endl;
00256       fillDefaults (fObject);
00257       result = true;
00258     }
00259     else if (asciiFile (fInput)) {
00260       if (!traceCounter) std::cout << "USE INPUT: ASCII: " << fInput << std::endl;
00261       std::ifstream inStream (fInput.c_str ());
00262       fObject = new T;
00263       HcalDbASCIIIO::getObject (inStream, fObject); 
00264       result = true;
00265     }
00266     else if (dbFile (fInput)) {
00267       if (!traceCounter) std::cout << "USE INPUT: Pool: " << fInput << "/" << fInputRun << std::endl;
00268       if (!poolDb) poolDb = new HcalDbTool (fInput, fVerbose, fXmlAuth, fInputCatalog);
00269       if (fInputRun > 0) {
00270         fObject = new T;
00271         result = poolDb->getObject (fObject, fInputTag, fInputRun);
00272       }
00273       else { // copy all instances
00274         std::cout << "Copy all instances... " << std::endl;
00275         cond::IOV iov;
00276         if (poolDb->getObject (&iov, fInputTag)) {
00277           IOVCollection::const_iterator iovi = iov.iov.begin ();
00278           for (; iovi != iov.iov.end (); iovi++) {
00279             IOVRun iovMax = iovi->first;
00280             if (fVerbose) {
00281               std::cout << "fetching object for run " << iovMax << std::endl;
00282             }
00283             T* object = new T;
00284             if (!poolDb->getObject (object, fInputTag, iovMax)) {
00285               std::cerr << "Failed to fetch object..." << std::endl;
00286               result = false;
00287               delete object;
00288               break;
00289             }
00290             allInstances.push_back (std::make_pair (iovMax, object));
00291             std::cerr << '.';
00292           }
00293           if (iovi == iov.iov.end ()) result = true;
00294         }
00295         else {
00296           std::cerr << "can not find IOV for tag " << fInputTag << std::endl;
00297           result = false;
00298         }
00299       }
00300     }
00301     else if (onlineFile (fInput)) {
00302       if (!traceCounter) std::cout << "USE INPUT: Online: " << fInput << std::endl;
00303       if (!onlineDb) onlineDb = new HcalDbOnline (fInput, fVerbose);
00304       if (fInputRun > 0) {
00305         fObject = new T;
00306         result = onlineDb->getObject (fObject, fInputTag, fInputRun);
00307       }
00308       else { // copy all instances
00309         std::cout << "Copy all instances... " << std::endl;
00310         std::vector<HcalDbOnline::IntervalOV> iovs = onlineDb->getIOVs (fInputTag);
00311         for (unsigned i = 0; i < iovs.size ();i++) {
00312           IOVRun iovMin = iovs[i].first;
00313           if (fVerbose) {
00314             std::cout << "fetching object for run " << iovMin << std::endl;
00315           }
00316           T* object = new T;
00317           if (!onlineDb->getObject (object, fInputTag, iovMin)) {
00318             std::cerr << "Failed to fetch object..." << std::endl;
00319             result = false;
00320             delete object;
00321             break;
00322           }
00323           allInstances.push_back (std::make_pair (iovMin, object));
00324           std::cerr << '.';
00325         }
00326         result = true;
00327       }
00328     }
00329  //    else if (occiFile (fInput)) {
00330 //       if (!traceCounter) std::cout << "USE INPUT: OCCI" << std::endl;
00331 //       if (!occiDb) occiDb = new HcalDbPoolOCCI (fInput);
00332 //       fObject = new T;
00333 //       result = occiDb->getObject (fObject, fInputTag, fInputRun);
00334 //     }
00335     traceCounter++;
00336     fInputRun++;
00337     if (fNtrace && !(traceCounter % fNtrace)) {
00338       time_t t = time (0);
00339       std::cout << "read transaction: " << traceCounter << " time: " << t - t0 << " dtime: " << t - t1 << std::endl;
00340       t1 = t;
00341     }
00342   }
00343   delete poolDb;
00344   delete onlineDb;
00345   poolDb = 0;
00346   onlineDb = 0;
00347   if (result) {
00348     t0 = time (0);
00349     t1 = t0;
00350     traceCounter = 0;
00351     T* object = 0;
00352     while (traceCounter < fNwrite) {
00353       delete object;
00354       object = fObject ? new T (*fObject) : 0; // copy original object
00355       if (asciiFile (fOutput)) {
00356         if (!traceCounter) std::cout << "USE OUTPUT: ASCII: " << fOutput << std::endl;
00357         if (fObject && allInstances.empty ()) {
00358           std::ofstream outStream (fOutput.c_str ());
00359           HcalDbASCIIIO::dumpObject (outStream, *object);
00360         }
00361         else {
00362           for (unsigned i = 0; i < allInstances.size (); i++) {
00363             if (fVerbose) {
00364               std::cout << "Storing object for run " << allInstances[i].first << std::endl;
00365             }
00366             std::ostringstream outName;
00367             unsigned ipos = fOutput.find (".txt");
00368             if (ipos == std::string::npos) {
00369               outName << fOutput << "_" << allInstances[i].first;
00370             }
00371             else {
00372               outName << std::string (fOutput, 0, ipos) << "_" << allInstances[i].first << ".txt";
00373             }
00374             std::ofstream outStream (outName.str().c_str ());
00375             object = allInstances[i].second;
00376             HcalDbASCIIIO::dumpObject (outStream, *object);
00377             delete object;
00378             allInstances[i].second = 0;
00379             std::cerr << '.';
00380           }
00381         }
00382       }
00383       else if (xmlFile (fOutput)) {
00384         if (!traceCounter) std::cout << "USE OUTPUT: XML: " << fOutput << std::endl;
00385         std::ofstream outStream (fOutput.c_str ());
00386         HcalDbXml::dumpObject (outStream, fOutputRun, fIovgmtbegin, fIovgmtend, fOutputTag, *object);
00387         outStream.close ();
00388         std::cout << "close file\n";
00389       }
00390       else if (dbFile (fOutput)) { //POOL
00391         if (!traceCounter) std::cout << "USE OUTPUT: Pool: " << fOutput << '/' << fOutputRun << std::endl;
00392         if (!poolDb) poolDb = new HcalDbTool (fOutput, fVerbose, fXmlAuth, fOutputCatalog);
00393         if (fOutputRun > 0) {
00394           poolDb->putObject (object, fOutputTag, fOutputRun, fAppend);
00395           object = 0; // owned by POOL
00396         }
00397         else {
00398           for (unsigned i = 0; i < allInstances.size (); i++) {
00399             if (fVerbose) {
00400               std::cout << "Storing object for run " << allInstances[i].first << std::endl;
00401             }
00402             poolDb->putObject (allInstances[i].second, fOutputTag, allInstances[i].first, fAppend);
00403             allInstances[i].second = 0;
00404             std::cerr << '.';
00405           }
00406         }
00407       }
00408       traceCounter++;
00409       fOutputRun++;
00410       if (fNtrace && !(traceCounter % fNtrace)) {
00411         time_t t = time (0);
00412         std::cout << "write transaction: " << traceCounter << " time: " << t - t0 << " dtime: " << t - t1 << std::endl;
00413         t1 = t;
00414       }
00415     }
00416     delete poolDb;
00417     poolDb = 0;
00418   }
00419   return result;
00420 }
00421 
00422 int main (int argn, char* argv []) {
00423   // start message service
00424   edm::service::MessageServicePresence my_message_service;
00425 
00426   Args args;
00427   args.defineParameter ("-input", "DB connection string, POOL format, or .txt file, or defaults");
00428   args.defineParameter ("-output", "DB connection string, POOL format, or .txt, or .xml file");
00429   args.defineParameter ("-inputrun", "run # for which constands should be made");
00430   args.defineParameter ("-inputtag", "tag for the input constants set");
00431   args.defineParameter ("-inputcatalog", "catalog for POOL DB <$POOL_CATALOG>");
00432   args.defineParameter ("-outputrun", "run # for which constands should be dumped");
00433   args.defineParameter ("-outputtag", "tag for the output constants set");
00434   args.defineParameter ("-outputcatalog", "catalog for POOL DB <$POOL_CATALOG>");
00435   args.defineParameter ("-iovgmtbegin", "start time for online IOV <outputrun>");
00436   args.defineParameter ("-iovgmtend", "end time for online IOV <0>");
00437   args.defineParameter ("-nread", "repeat input that many times with increasing run# <1>");
00438   args.defineParameter ("-nwrite", "repeat output that many times with increasing run# <1>");
00439   args.defineParameter ("-trace", "trace time every that many operations <false>");
00440   args.defineOption ("-help", "this help");
00441   args.defineOption ("-online", "interpret input DB as an online DB");
00442   args.defineOption ("-xmlauth", "use XML authorization <false>");
00443   args.defineOption ("-append", "Strip previous IOV, make this IOV open (POOL DB) <false>");
00444   args.defineOption ("-verbose", "makes program verbose <false>");
00445   
00446   args.parse (argn, argv);
00447   
00448   std::vector<std::string> arguments = args.arguments ();
00449 
00450   if (arguments.size () < 1 || args.optionIsSet ("-help")) {
00451     printHelp (args);
00452     return -1;
00453   }
00454 
00455   std::string input = args.getParameter ("-input");
00456   std::string output = args.getParameter ("-output");
00457   
00458   HcalDbTool::IOVRun inputRun = args.getParameter ("-inputrun").empty () ? 0 : strtoull (args.getParameter ("-inputrun").c_str (), 0, 0);
00459   HcalDbTool::IOVRun outputRun = args.getParameter ("-outputrun").empty () ? 0 : strtoll (args.getParameter ("-outputrun").c_str (), 0, 0);
00460   std::string inputTag = args.getParameter ("-inputtag");
00461   std::string outputTag = args.getParameter ("-outputtag");
00462 
00463   unsigned long long iovgmtbegin = args.getParameter ("-iovgmtbegin").empty () ? outputRun : strtoull (args.getParameter ("-iovgmtbegin").c_str (), 0, 0);
00464   unsigned long long iovgmtend = args.getParameter ("-iovgmtend").empty () ? 0 : strtoull (args.getParameter ("-iovgmtend").c_str (), 0, 0);
00465 
00466   unsigned nread = args.getParameter ("-nread").empty () ? 1 : atoi (args.getParameter ("-nread").c_str ());
00467   unsigned nwrite = args.getParameter ("-nwrite").empty () ? 1 : atoi (args.getParameter ("-nwrite").c_str ());
00468   unsigned trace = args.getParameter ("-trace").empty () ? 0 : atoi (args.getParameter ("-trace").c_str ());
00469 
00470   const char* inputCatalog = args.getParameter ("-inputcatalog").empty () ? 0 : args.getParameter ("-inputcatalog").c_str();
00471   const char* outputCatalog = args.getParameter ("-outputcatalog").empty () ? 0 : args.getParameter ("-outputcatalog").c_str();
00472 
00473   bool xmlAuth = args.optionIsSet ("-xmlauth");
00474   bool append = args.optionIsSet ("-append");
00475 
00476   bool verbose = args.optionIsSet ("-verbose");
00477 
00478 
00479   std::string what = arguments [0];
00480 
00481   if (what == "pedestals") {
00482     HcalPedestals* object = 0;
00483     copyObject (object, input, inputTag, inputRun, output, outputTag, outputRun, append, iovgmtbegin, iovgmtend, nread, nwrite, trace, verbose, inputCatalog, outputCatalog, xmlAuth);
00484   }
00485   else if (what == "gains") {
00486     HcalGains* object = 0;
00487     copyObject (object, input, inputTag, inputRun, output, outputTag, outputRun, append, iovgmtbegin, iovgmtend, nread, nwrite, trace, verbose, inputCatalog, outputCatalog, xmlAuth);
00488   }
00489   else if (what == "pwidths") {
00490     HcalPedestalWidths* object = 0;
00491     copyObject (object, input, inputTag, inputRun, output, outputTag, outputRun, append, iovgmtbegin, iovgmtend, nread, nwrite, trace, verbose, inputCatalog, outputCatalog, xmlAuth);
00492   }
00493   else if (what == "gwidths") {
00494     HcalGainWidths* object = 0;
00495     copyObject (object, input, inputTag, inputRun, output, outputTag, outputRun, append, iovgmtbegin, iovgmtend, nread, nwrite, trace, verbose, inputCatalog, outputCatalog, xmlAuth);
00496   }
00497   else if (what == "emap") {
00498     HcalElectronicsMap* object = 0;
00499     copyObject (object, input, inputTag, inputRun, output, outputTag, outputRun, append, iovgmtbegin, iovgmtend, nread, nwrite, trace, verbose, inputCatalog, outputCatalog, xmlAuth);
00500   }
00501   else if (what == "qie") {
00502     HcalQIEData* object = 0;
00503     copyObject (object, input, inputTag, inputRun, output, outputTag, outputRun, append, iovgmtbegin, iovgmtend, nread, nwrite, trace, verbose, inputCatalog, outputCatalog, xmlAuth);
00504   }
00505   else if (what == "calibqie") {
00506     HcalCalibrationQIEData* object = 0;
00507     copyObject (object, input, inputTag, inputRun, output, outputTag, outputRun, append, iovgmtbegin, iovgmtend, nread, nwrite, trace, verbose, inputCatalog, outputCatalog, xmlAuth);
00508   }
00509 }
00510 
00511 
00512 //==================== Args ===== BEGIN ==============================
00513 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
00514   mOptions.push_back (fOption);
00515   mComments [fOption] = fComment;
00516 }
00517 
00518 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {
00519   mParameters.push_back (fParameter);
00520   mComments [fParameter] = fComment;
00521 }
00522 
00523 void Args::parse (int nArgs, char* fArgs []) {
00524   if (nArgs <= 0) return;
00525   mProgramName = std::string (fArgs [0]);
00526   int iarg = 0;
00527   while (++iarg < nArgs) {
00528     std::string arg (fArgs [iarg]);
00529     if (arg [0] != '-') mArgs.push_back (arg);
00530     else {
00531       if (std::find (mOptions.begin(), mOptions.end (), arg) !=  mOptions.end ()) {
00532         mParsed [arg] = "";
00533       }
00534       if (std::find (mParameters.begin(), mParameters.end (), arg) !=  mParameters.end ()) {
00535         if (iarg >= nArgs) {
00536           std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
00537         }
00538         else {
00539           mParsed [arg] = std::string (fArgs [++iarg]);
00540         }
00541       }
00542     }
00543   }
00544 }
00545 
00546 void Args::printOptionsHelp () const {
00547   char buffer [1024];
00548   std::cout << "Parameters:" << std::endl;
00549   for (unsigned i = 0; i < mParameters.size (); i++) {
00550     std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
00551     std::string comment = it != mComments.end () ? it->second : "uncommented";
00552     sprintf (buffer, "  %-8s <value> : %s", (mParameters [i]).c_str(),  comment.c_str());
00553     std::cout << buffer << std::endl;
00554   }
00555   std::cout << "Options:" << std::endl;
00556   for (unsigned i = 0; i < mOptions.size (); i++) {
00557     std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
00558     std::string comment = it != mComments.end () ? it->second : "uncommented";
00559     sprintf (buffer, "  %-8s  : %s", (mOptions [i]).c_str(),  comment.c_str());
00560     std::cout << buffer << std::endl;
00561   }
00562 }
00563 
00564 std::string Args::command () const {
00565   int ipos = mProgramName.rfind ('/');
00566   return std::string (mProgramName, ipos+1);
00567 }
00568 
00569 std::vector<std::string> Args::arguments () const {return mArgs;}
00570 
00571 bool Args::optionIsSet (const std::string& fOption) const {
00572   return mParsed.find (fOption) != mParsed.end ();
00573 }
00574 
00575 std::string Args::getParameter (const std::string& fKey) {
00576   if (optionIsSet (fKey)) return mParsed [fKey];
00577   return "";
00578 }
00579 //==================== Args ===== END ==============================