CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/CondTools/Hcal/bin/hcalPublishConditions.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 "DataFormats/Common/interface/Timestamp.h"
00011 #include "CondCore/IOVService/interface/IOV.h"
00012 #include "CondTools/Hcal/interface/HcalDbTool.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 
00031 class Args {
00032  public:
00033   Args () {};
00034   ~Args () {};
00035   void defineOption (const std::string& fOption, const std::string& fComment = "");
00036   void defineParameter (const std::string& fParameter, const std::string& fComment = "");
00037   void parse (int nArgs, char* fArgs []);
00038   void printOptionsHelp () const;
00039   std::string command () const;
00040   std::vector<std::string> arguments () const;
00041   bool optionIsSet (const std::string& fOption) const;
00042   std::string getParameter (const std::string& fKey);
00043  private:
00044   std::string mProgramName;
00045   std::vector <std::string> mOptions;
00046   std::vector <std::string> mParameters;
00047   std::vector <std::string> mArgs;
00048   std::map <std::string, std::string> mParsed;
00049   std::map <std::string, std::string> mComments;
00050 };
00051 
00052 void printHelp (const Args& args) {
00053   char buffer [1024];
00054   std::cout << "Tool to convert RAW HCAL conditions into standard offline accessible ones" << std::endl;
00055   std::cout << "    feedback -> ratnikov@fnal.gov" << std::endl;
00056   std::cout << "Use:" << std::endl;
00057   sprintf (buffer, " %s <what> <options> <parameters>\n", args.command ().c_str());
00058   std::cout << buffer;
00059   std::cout << "  where <what> is: \n    pedestals\n    gains\n    pwidths\n    gwidths\n    emap\n    qie\n    calibqie" << std::endl;
00060   args.printOptionsHelp ();
00061 }
00062 
00063 
00064 bool publishObjects (const std::string& fInputDb, const std::string& fInputTag, 
00065                  const std::string& fOutputDb, const std::string& fOutputTag,
00066                  bool fVerbose) {
00067   HcalDbTool db (fInputDb, fVerbose);
00068   cond::IOV inputIov;
00069   cond::IOV outputIov;
00070   std::vector<std::string> allTags = db.metadataAllTags ();
00071   for (unsigned i = 0; i < allTags.size(); i++) {
00072     if (allTags[i] == fInputTag) {
00073       if (!db.getObject (&inputIov, fInputTag)) {
00074         std::cerr << "copyObject-> Can not get IOV for input tags " << fInputTag << std::endl;
00075         return false;
00076       }
00077     }
00078     if (allTags[i] == fOutputTag) {
00079       if (!db.getObject (&outputIov, fOutputTag)) {
00080         std::cerr << "copyObject-> Can not get IOV for output tags " << fOutputTag << std::endl;
00081         return false;
00082       }
00083     }
00084   }
00085   if (fVerbose) {
00086     std::vector <IntervalOV> allIOVs = allIOV (inputIov);
00087     std::cout << " all IOVs available for input tag " << fInputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
00088     for (unsigned i = 0; i < allIOVs.size(); i++) {
00089       std::cout << "[ " << allIOVs[i].first << " .. " << allIOVs[i].second << " )  " << inputIov.iov [allIOVs[i].second] << std::endl;
00090     }
00091     allIOVs = allIOV (outputIov);
00092     std::cout << "\n all IOVs available for output tag " << fOutputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
00093     for (unsigned i = 0; i < allIOVs.size(); i++) {
00094       std::cout << "[ " << allIOVs[i].first << " .. " << allIOVs[i].second << " )  " << outputIov.iov [allIOVs[i].second] << std::endl;
00095     }
00096   }
00097   
00098   // first check that all objects of output are contained in input
00099   IOVCollection::const_iterator iovIn = inputIov.iov.begin ();
00100   if (iovIn == inputIov.iov.end ()) {
00101     std::cerr << "Input IOV is empty - nothing to do" << std::endl;
00102     return true;
00103   }
00104   std::string inputToken = iovIn->second;
00105   iovIn++;
00106   IOVCollection::const_iterator iovOut = outputIov.iov.begin ();
00107   for (; ; iovOut++, iovIn++) {
00108     if (iovIn == inputIov.iov.end ()) {
00109       if (++iovOut == outputIov.iov.end ()) {
00110         std::cerr << "Nothing to update" << std::endl;
00111         return true;
00112       }
00113       else {
00114         std::cerr << "List of input IOV is too short" << std::endl;
00115         return false;
00116       }
00117     }
00118     if (iovOut == outputIov.iov.end ()) { // empty output
00119       outputIov.iov [iovIn->first] = inputToken;
00120       inputToken = iovIn->second;
00121       break;
00122     }
00123     if (inputToken != iovOut->second) {
00124       std::cerr << "Mismatched tokens: \n  in: " << iovIn->second << "\n out: " << iovOut->second << std::endl;
00125       return false;
00126     }
00127     
00128     // is it the open end?
00129     IOVCollection::const_iterator iovOut2 = iovOut;
00130     if (++iovOut2 == outputIov.iov.end ()) {
00131       outputIov.iov.erase (iovOut->first);
00132       outputIov.iov [iovIn->first] = inputToken;
00133       inputToken = iovIn->second;
00134       break;
00135     }
00136     if (iovIn->first != iovOut->first) {
00137       std::cerr << "Mismatched runs: in: " << iovIn->first << ", out: " << iovOut->first << std::endl;
00138       return false;
00139     }
00140     
00141     inputToken = iovIn->second;
00142   }
00143   std::cout << "Good! Input and output does match" << std::endl;
00144   
00145   for (iovIn++; iovIn != inputIov.iov.end (); iovIn++) {
00146     IOVRun run = iovIn->first;
00147     outputIov.iov [run] = inputToken;
00148     inputToken = iovIn->second;
00149   }
00150   // last open token
00151   outputIov.iov [edm::Timestamp::endOfTime().value()] = inputToken;
00152   
00153   if (fVerbose) {
00154     std::vector <IntervalOV> allIOVs = allIOV (outputIov);
00155     std::cout << "\n Done! All IOVs available for output tag " << fOutputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
00156     for (unsigned i = 0; i < allIOVs.size(); i++) {
00157       std::cout << "[ " << allIOVs[i].first << " ... " << allIOVs[i].second << " )  " << outputIov.iov [allIOVs[i].second] << std::endl;
00158     }
00159   }
00160   return db.putObject (&outputIov, fOutputTag);
00161 }
00162 
00163 
00164 int main (int argn, char* argv []) {
00165 
00166   Args args;
00167   args.defineParameter ("-input", "DB connection string, POOL format, i.e. oracle://devdb10/CMS_COND_HCAL");
00168   args.defineParameter ("-inputtag", "tag for raw conditions");
00169   args.defineParameter ("-output", "DB connection string, POOL format, i.e. oracle://devdb10/CMS_COND_HCAL");
00170   args.defineParameter ("-outputtag", "tag for production conditions");
00171   args.defineOption ("-help", "this help");
00172   args.defineOption ("-verbose", "verbose");
00173   
00174   args.parse (argn, argv);
00175   
00176   std::vector<std::string> arguments = args.arguments ();
00177 
00178   if (arguments.size () < 1 || args.optionIsSet ("-help")) {
00179     printHelp (args);
00180     return -1;
00181   }
00182 
00183   std::string inputdb = args.getParameter ("-input");
00184   std::string inputtag = args.getParameter ("-inputtag");
00185   std::string outputdb = args.getParameter ("-output");
00186   std::string outputtag = args.getParameter ("-outputtag");
00187   bool verbose = args.optionIsSet ("-verbose");
00188 
00189   publishObjects (inputdb, inputtag, outputdb, outputtag, verbose);
00190 }
00191 
00192 
00193 //==================== Args ===== BEGIN ==============================
00194 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
00195   mOptions.push_back (fOption);
00196   mComments [fOption] = fComment;
00197 }
00198 
00199 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {
00200   mParameters.push_back (fParameter);
00201   mComments [fParameter] = fComment;
00202 }
00203 
00204 void Args::parse (int nArgs, char* fArgs []) {
00205   if (nArgs <= 0) return;
00206   mProgramName = std::string (fArgs [0]);
00207   int iarg = 0;
00208   while (++iarg < nArgs) {
00209     std::string arg (fArgs [iarg]);
00210     if (arg [0] != '-') mArgs.push_back (arg);
00211     else {
00212       if (std::find (mOptions.begin(), mOptions.end (), arg) !=  mOptions.end ()) {
00213         mParsed [arg] = "";
00214       }
00215       if (std::find (mParameters.begin(), mParameters.end (), arg) !=  mParameters.end ()) {
00216         if (iarg >= nArgs) {
00217           std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
00218         }
00219         else {
00220           mParsed [arg] = std::string (fArgs [++iarg]);
00221         }
00222       }
00223     }
00224   }
00225 }
00226 
00227 void Args::printOptionsHelp () const {
00228   char buffer [1024];
00229   std::cout << "Parameters:" << std::endl;
00230   for (unsigned i = 0; i < mParameters.size (); i++) {
00231     std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
00232     std::string comment = it != mComments.end () ? it->second : "uncommented";
00233     sprintf (buffer, "  %-8s <value> : %s", (mParameters [i]).c_str(),  comment.c_str());
00234     std::cout << buffer << std::endl;
00235   }
00236   std::cout << "Options:" << std::endl;
00237   for (unsigned i = 0; i < mOptions.size (); i++) {
00238     std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
00239     std::string comment = it != mComments.end () ? it->second : "uncommented";
00240     sprintf (buffer, "  %-8s  : %s", (mOptions [i]).c_str(),  comment.c_str());
00241     std::cout << buffer << std::endl;
00242   }
00243 }
00244 
00245 std::string Args::command () const {
00246   int ipos = mProgramName.rfind ('/');
00247   return std::string (mProgramName, ipos+1);
00248 }
00249 
00250 std::vector<std::string> Args::arguments () const {return mArgs;}
00251 
00252 bool Args::optionIsSet (const std::string& fOption) const {
00253   return mParsed.find (fOption) != mParsed.end ();
00254 }
00255 
00256 std::string Args::getParameter (const std::string& fKey) {
00257   if (optionIsSet (fKey)) return mParsed [fKey];
00258   return "";
00259 }
00260 //==================== Args ===== END ==============================