Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:06

0001 #include <stdlib.h>
0002 
0003 #include <iostream>
0004 #include <fstream>
0005 #include <vector>
0006 #include <map>
0007 #include <algorithm>
0008 #include <string>
0009 
0010 #include "DataFormats/Common/interface/Timestamp.h"
0011 #include "CondCore/IOVService/interface/IOV.h"
0012 #include "CondTools/Hcal/interface/HcalDbTool.h"
0013 
0014 namespace {
0015   typedef HcalDbTool::IOVRun IOVRun;
0016   typedef std::map<IOVRun,std::string> IOVCollection;
0017   typedef std::pair<IOVRun,IOVRun> IntervalOV;
0018 
0019   std::vector <IntervalOV> allIOV (const cond::IOV& fIOV) {
0020     std::vector <IntervalOV> result;
0021     IOVRun iovMin = 0;
0022     for (IOVCollection::const_iterator iovi = fIOV.iov.begin (); iovi != fIOV.iov.end (); iovi++) {
0023       IOVRun iovMax = iovi->first;
0024       result.push_back (std::make_pair (iovMin, iovMax));
0025       iovMin = iovMax;
0026     }
0027     return result;
0028   }
0029 }
0030 
0031 class Args {
0032  public:
0033   Args () {};
0034   ~Args () {};
0035   void defineOption (const std::string& fOption, const std::string& fComment = "");
0036   void defineParameter (const std::string& fParameter, const std::string& fComment = "");
0037   void parse (int nArgs, char* fArgs []);
0038   void printOptionsHelp () const;
0039   std::string command () const;
0040   std::vector<std::string> arguments () const;
0041   bool optionIsSet (const std::string& fOption) const;
0042   std::string getParameter (const std::string& fKey);
0043  private:
0044   std::string mProgramName;
0045   std::vector <std::string> mOptions;
0046   std::vector <std::string> mParameters;
0047   std::vector <std::string> mArgs;
0048   std::map <std::string, std::string> mParsed;
0049   std::map <std::string, std::string> mComments;
0050 };
0051 
0052 void printHelp (const Args& args) {
0053   char buffer [1024];
0054   std::cout << "Tool to convert RAW HCAL conditions into standard offline accessible ones" << std::endl;
0055   std::cout << "    feedback -> ratnikov@fnal.gov" << std::endl;
0056   std::cout << "Use:" << std::endl;
0057   sprintf (buffer, " %s <what> <options> <parameters>\n", args.command ().c_str());
0058   std::cout << buffer;
0059   std::cout << "  where <what> is: \n    pedestals\n    gains\n    pwidths\n    gwidths\n    emap\n    qie\n    calibqie" << std::endl;
0060   args.printOptionsHelp ();
0061 }
0062 
0063 
0064 bool publishObjects (const std::string& fInputDb, const std::string& fInputTag, 
0065          const std::string& fOutputDb, const std::string& fOutputTag,
0066          bool fVerbose) {
0067   HcalDbTool db (fInputDb, fVerbose);
0068   cond::IOV inputIov;
0069   cond::IOV outputIov;
0070   std::vector<std::string> allTags = db.metadataAllTags ();
0071   for (unsigned i = 0; i < allTags.size(); i++) {
0072     if (allTags[i] == fInputTag) {
0073       if (!db.getObject (&inputIov, fInputTag)) {
0074     std::cerr << "copyObject-> Can not get IOV for input tags " << fInputTag << std::endl;
0075     return false;
0076       }
0077     }
0078     if (allTags[i] == fOutputTag) {
0079       if (!db.getObject (&outputIov, fOutputTag)) {
0080     std::cerr << "copyObject-> Can not get IOV for output tags " << fOutputTag << std::endl;
0081     return false;
0082       }
0083     }
0084   }
0085   if (fVerbose) {
0086     std::vector <IntervalOV> allIOVs = allIOV (inputIov);
0087     std::cout << " all IOVs available for input tag " << fInputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
0088     for (unsigned i = 0; i < allIOVs.size(); i++) {
0089       std::cout << "[ " << allIOVs[i].first << " .. " << allIOVs[i].second << " )  " << inputIov.iov [allIOVs[i].second] << std::endl;
0090     }
0091     allIOVs = allIOV (outputIov);
0092     std::cout << "\n all IOVs available for output tag " << fOutputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
0093     for (unsigned i = 0; i < allIOVs.size(); i++) {
0094       std::cout << "[ " << allIOVs[i].first << " .. " << allIOVs[i].second << " )  " << outputIov.iov [allIOVs[i].second] << std::endl;
0095     }
0096   }
0097   
0098   // first check that all objects of output are contained in input
0099   IOVCollection::const_iterator iovIn = inputIov.iov.begin ();
0100   if (iovIn == inputIov.iov.end ()) {
0101     std::cerr << "Input IOV is empty - nothing to do" << std::endl;
0102     return true;
0103   }
0104   std::string inputToken = iovIn->second;
0105   iovIn++;
0106   IOVCollection::const_iterator iovOut = outputIov.iov.begin ();
0107   for (; ; iovOut++, iovIn++) {
0108     if (iovIn == inputIov.iov.end ()) {
0109       if (++iovOut == outputIov.iov.end ()) {
0110     std::cerr << "Nothing to update" << std::endl;
0111     return true;
0112       }
0113       else {
0114     std::cerr << "List of input IOV is too short" << std::endl;
0115     return false;
0116       }
0117     }
0118     if (iovOut == outputIov.iov.end ()) { // empty output
0119       outputIov.iov [iovIn->first] = inputToken;
0120       inputToken = iovIn->second;
0121       break;
0122     }
0123     if (inputToken != iovOut->second) {
0124       std::cerr << "Mismatched tokens: \n  in: " << iovIn->second << "\n out: " << iovOut->second << std::endl;
0125       return false;
0126     }
0127     
0128     // is it the open end?
0129     IOVCollection::const_iterator iovOut2 = iovOut;
0130     if (++iovOut2 == outputIov.iov.end ()) {
0131       outputIov.iov.erase (iovOut->first);
0132       outputIov.iov [iovIn->first] = inputToken;
0133       inputToken = iovIn->second;
0134       break;
0135     }
0136     if (iovIn->first != iovOut->first) {
0137       std::cerr << "Mismatched runs: in: " << iovIn->first << ", out: " << iovOut->first << std::endl;
0138       return false;
0139     }
0140     
0141     inputToken = iovIn->second;
0142   }
0143   std::cout << "Good! Input and output does match" << std::endl;
0144   
0145   for (iovIn++; iovIn != inputIov.iov.end (); iovIn++) {
0146     IOVRun run = iovIn->first;
0147     outputIov.iov [run] = inputToken;
0148     inputToken = iovIn->second;
0149   }
0150   // last open token
0151   outputIov.iov [edm::Timestamp::endOfTime().value()] = inputToken;
0152   
0153   if (fVerbose) {
0154     std::vector <IntervalOV> allIOVs = allIOV (outputIov);
0155     std::cout << "\n Done! All IOVs available for output tag " << fOutputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
0156     for (unsigned i = 0; i < allIOVs.size(); i++) {
0157       std::cout << "[ " << allIOVs[i].first << " ... " << allIOVs[i].second << " )  " << outputIov.iov [allIOVs[i].second] << std::endl;
0158     }
0159   }
0160   return db.putObject (&outputIov, fOutputTag);
0161 }
0162 
0163 
0164 int main (int argn, char* argv []) {
0165 
0166   Args args;
0167   args.defineParameter ("-input", "DB connection string, POOL format, i.e. oracle://devdb10/CMS_COND_HCAL");
0168   args.defineParameter ("-inputtag", "tag for raw conditions");
0169   args.defineParameter ("-output", "DB connection string, POOL format, i.e. oracle://devdb10/CMS_COND_HCAL");
0170   args.defineParameter ("-outputtag", "tag for production conditions");
0171   args.defineOption ("-help", "this help");
0172   args.defineOption ("-verbose", "verbose");
0173   
0174   args.parse (argn, argv);
0175   
0176   std::vector<std::string> arguments = args.arguments ();
0177 
0178   if (arguments.size () < 1 || args.optionIsSet ("-help")) {
0179     printHelp (args);
0180     return -1;
0181   }
0182 
0183   std::string inputdb = args.getParameter ("-input");
0184   std::string inputtag = args.getParameter ("-inputtag");
0185   std::string outputdb = args.getParameter ("-output");
0186   std::string outputtag = args.getParameter ("-outputtag");
0187   bool verbose = args.optionIsSet ("-verbose");
0188 
0189   publishObjects (inputdb, inputtag, outputdb, outputtag, verbose);
0190 }
0191 
0192 
0193 //==================== Args ===== BEGIN ==============================
0194 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
0195   mOptions.push_back (fOption);
0196   mComments [fOption] = fComment;
0197 }
0198 
0199 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {
0200   mParameters.push_back (fParameter);
0201   mComments [fParameter] = fComment;
0202 }
0203 
0204 void Args::parse (int nArgs, char* fArgs []) {
0205   if (nArgs <= 0) return;
0206   mProgramName = std::string (fArgs [0]);
0207   int iarg = 0;
0208   while (++iarg < nArgs) {
0209     std::string arg (fArgs [iarg]);
0210     if (arg [0] != '-') mArgs.push_back (arg);
0211     else {
0212       if (std::find (mOptions.begin(), mOptions.end (), arg) !=  mOptions.end ()) {
0213     mParsed [arg] = "";
0214       }
0215       if (std::find (mParameters.begin(), mParameters.end (), arg) !=  mParameters.end ()) {
0216     if (iarg >= nArgs) {
0217       std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
0218     }
0219     else {
0220       mParsed [arg] = std::string (fArgs [++iarg]);
0221     }
0222       }
0223     }
0224   }
0225 }
0226 
0227 void Args::printOptionsHelp () const {
0228   char buffer [1024];
0229   std::cout << "Parameters:" << std::endl;
0230   for (unsigned i = 0; i < mParameters.size (); i++) {
0231     std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
0232     std::string comment = it != mComments.end () ? it->second : "uncommented";
0233     sprintf (buffer, "  %-8s <value> : %s", (mParameters [i]).c_str(),  comment.c_str());
0234     std::cout << buffer << std::endl;
0235   }
0236   std::cout << "Options:" << std::endl;
0237   for (unsigned i = 0; i < mOptions.size (); i++) {
0238     std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
0239     std::string comment = it != mComments.end () ? it->second : "uncommented";
0240     sprintf (buffer, "  %-8s  : %s", (mOptions [i]).c_str(),  comment.c_str());
0241     std::cout << buffer << std::endl;
0242   }
0243 }
0244 
0245 std::string Args::command () const {
0246   int ipos = mProgramName.rfind ('/');
0247   return std::string (mProgramName, ipos+1);
0248 }
0249 
0250 std::vector<std::string> Args::arguments () const {return mArgs;}
0251 
0252 bool Args::optionIsSet (const std::string& fOption) const {
0253   return mParsed.find (fOption) != mParsed.end ();
0254 }
0255 
0256 std::string Args::getParameter (const std::string& fKey) {
0257   if (optionIsSet (fKey)) return mParsed [fKey];
0258   return "";
0259 }
0260 //==================== Args ===== END ==============================