CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/CondTools/Hcal/bin/hcalIovTool.cc

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 #include <vector>
00006 #include <map>
00007 #include <algorithm>
00008 #include <string>
00009 
00010 #include "CondCore/IOVService/interface/IOV.h"
00011 #include "CondTools/Hcal/interface/HcalDbTool.h"
00012 #include "CondTools/Hcal/interface/HcalDbOnline.h"
00013 
00014 namespace {
00015   typedef HcalDbTool::IOVRun IOVRun;
00016   typedef std::map<IOVRun,std::string> IOVCollection;
00017   typedef std::pair<IOVRun,IOVRun> IntervalOV;
00018 
00019   std::vector <IntervalOV> allIOV (const cond::IOV& fIOV) {
00020     std::vector <IntervalOV> result;
00021     IOVRun iovMin = 0;
00022     for (IOVCollection::const_iterator iovi = fIOV.iov.begin (); iovi != fIOV.iov.end (); iovi++) {
00023       IOVRun iovMax = iovi->first;
00024       result.push_back (std::make_pair (iovMin, iovMax));
00025       iovMin = iovMax;
00026     }
00027     return result;
00028   }
00029 
00030 bool dbFile (const std::string fParam) {
00031   return fParam.find (':') != std::string::npos;
00032 }
00033 
00034 bool onlineFile (const std::string fParam) {
00035   return fParam.find ('@') != std::string::npos &&
00036     fParam.find ("cms_val_lb") == std::string::npos;
00037 }
00038 
00039 }
00040 
00041 class Args {
00042  public:
00043   Args () {};
00044   ~Args () {};
00045   void defineOption (const std::string& fOption, const std::string& fComment = "");
00046   void defineParameter (const std::string& fParameter, const std::string& fComment = "");
00047   void parse (int nArgs, char* fArgs []);
00048   void printOptionsHelp () const;
00049   std::string command () const;
00050   std::vector<std::string> arguments () const;
00051   bool optionIsSet (const std::string& fOption) const;
00052   std::string getParameter (const std::string& fKey);
00053  private:
00054   std::string mProgramName;
00055   std::vector <std::string> mOptions;
00056   std::vector <std::string> mParameters;
00057   std::vector <std::string> mArgs;
00058   std::map <std::string, std::string> mParsed;
00059   std::map <std::string, std::string> mComments;
00060 };
00061 
00062 void printHelp (const Args& args) {
00063   char buffer [1024];
00064   std::cout << "Tool to manipulate by IOV of Hcal Calibrations" << std::endl;
00065   std::cout << "    feedback -> ratnikov@fnal.gov" << std::endl;
00066   std::cout << "Use:" << std::endl;
00067   sprintf (buffer, " %s <what> <options> <parameters>\n", args.command ().c_str());
00068   std::cout << buffer;
00069   std::cout << "  where <what> is: \n    tag\n    iov" << std::endl;
00070   args.printOptionsHelp ();
00071 }
00072 
00073 void printTags (const std::string& fDb, bool fVerbose) {
00074   std::vector<std::string> allTags;
00075   if (dbFile (fDb)) {
00076     HcalDbTool poolDb (fDb, fVerbose);
00077     allTags = poolDb.metadataAllTags ();
00078     std::cout << "Tags available in CMS POOL DB instance: " << fDb << std::endl;
00079   }
00080   if (onlineFile (fDb)) {
00081     HcalDbOnline onlineDb (fDb, fVerbose);
00082     allTags = onlineDb.metadataAllTags ();
00083     std::cout << "Tags available in HCAL master DB instance: " << fDb << std::endl;
00084   }
00085   for (unsigned i = 0; i < allTags.size(); i++) {
00086     std::cout << allTags[i] << std::endl;
00087   }
00088 }
00089 
00090 void printRuns (const std::string& fDb, const std::string fTag, bool fVerbose) {
00091   std::vector <IntervalOV> allIOVs;
00092   if (dbFile (fDb)) {
00093     HcalDbTool poolDb (fDb, fVerbose);
00094     cond::IOV iov;
00095     if (poolDb.getObject (&iov, fTag)) {
00096       allIOVs = allIOV (iov);
00097       std::cout << "IOVs available for tag " << fTag << " in CMS POOL DB instance: " << fDb << std::endl;
00098     }
00099     else {
00100       std::cerr << "printRuns-> can not find IOV for tag " << fTag << std::endl;
00101     }
00102   }
00103   if (onlineFile (fDb)) {
00104     HcalDbOnline onlineDb (fDb, fVerbose);
00105     allIOVs = onlineDb.getIOVs (fTag);
00106   }
00107   for (unsigned i = 0; i < allIOVs.size(); i++) {
00108     std::cout << "[ " << allIOVs[i].first << " ... " << allIOVs[i].second << " )" << std::endl;
00109   }
00110 }
00111 
00112 int main (int argn, char* argv []) {
00113 
00114   Args args;
00115   args.defineParameter ("-db", "DB connection string, POOL format, or online format");
00116   args.defineParameter ("-tag", "tag specifyer");
00117   args.defineOption ("-help", "this help");
00118   args.defineOption ("-verbose", "this help");
00119   
00120   args.parse (argn, argv);
00121   
00122   std::vector<std::string> arguments = args.arguments ();
00123 
00124   if (arguments.size () < 1 || args.optionIsSet ("-help")) {
00125     printHelp (args);
00126     return -1;
00127   }
00128 
00129   std::string db = args.getParameter ("-db");
00130   std::string tag = args.getParameter ("-tag");
00131   bool verbose = args.optionIsSet ("-verbose");
00132 
00133   std::string what = arguments [0];
00134 
00135   if (what == "tag") {
00136     printTags (db, verbose);
00137   }
00138   else if (what == "iov") {
00139     printRuns (db, tag, verbose);
00140   }
00141   std::cout << "Program has completed successfully" << std::endl;
00142   return 0;
00143 }
00144 
00145 
00146 //==================== Args ===== BEGIN ==============================
00147 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
00148   mOptions.push_back (fOption);
00149   mComments [fOption] = fComment;
00150 }
00151 
00152 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {
00153   mParameters.push_back (fParameter);
00154   mComments [fParameter] = fComment;
00155 }
00156 
00157 void Args::parse (int nArgs, char* fArgs []) {
00158   if (nArgs <= 0) return;
00159   mProgramName = std::string (fArgs [0]);
00160   int iarg = 0;
00161   while (++iarg < nArgs) {
00162     std::string arg (fArgs [iarg]);
00163     if (arg [0] != '-') mArgs.push_back (arg);
00164     else {
00165       if (std::find (mOptions.begin(), mOptions.end (), arg) !=  mOptions.end ()) {
00166         mParsed [arg] = "";
00167       }
00168       if (std::find (mParameters.begin(), mParameters.end (), arg) !=  mParameters.end ()) {
00169         if (iarg >= nArgs) {
00170           std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
00171         }
00172         else {
00173           mParsed [arg] = std::string (fArgs [++iarg]);
00174         }
00175       }
00176     }
00177   }
00178 }
00179 
00180 void Args::printOptionsHelp () const {
00181   char buffer [1024];
00182   std::cout << "Parameters:" << std::endl;
00183   for (unsigned i = 0; i < mParameters.size (); i++) {
00184     std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
00185     std::string comment = it != mComments.end () ? it->second : "uncommented";
00186     sprintf (buffer, "  %-8s <value> : %s", (mParameters [i]).c_str(),  comment.c_str());
00187     std::cout << buffer << std::endl;
00188   }
00189   std::cout << "Options:" << std::endl;
00190   for (unsigned i = 0; i < mOptions.size (); i++) {
00191     std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
00192     std::string comment = it != mComments.end () ? it->second : "uncommented";
00193     sprintf (buffer, "  %-8s  : %s", (mOptions [i]).c_str(),  comment.c_str());
00194     std::cout << buffer << std::endl;
00195   }
00196 }
00197 
00198 std::string Args::command () const {
00199   int ipos = mProgramName.rfind ('/');
00200   return std::string (mProgramName, ipos+1);
00201 }
00202 
00203 std::vector<std::string> Args::arguments () const {return mArgs;}
00204 
00205 bool Args::optionIsSet (const std::string& fOption) const {
00206   return mParsed.find (fOption) != mParsed.end ();
00207 }
00208 
00209 std::string Args::getParameter (const std::string& fKey) {
00210   if (optionIsSet (fKey)) return mParsed [fKey];
00211   return "";
00212 }
00213 //==================== Args ===== END ==============================