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 #include "CondFormats/HcalObjects/interface/HcalPedestals.h"
0015 #include "CondFormats/HcalObjects/interface/HcalPedestalWidths.h"
0016 #include "CondFormats/HcalObjects/interface/HcalGains.h"
0017 #include "CondFormats/HcalObjects/interface/HcalGainWidths.h"
0018 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
0019 #include "CondFormats/HcalObjects/interface/HcalChannelQuality.h"
0020 #include "CondFormats/HcalObjects/interface/HcalQIEData.h"
0021 #include "CondFormats/HcalObjects/interface/HcalCalibrationQIEData.h"
0022 
0023 
0024 namespace {
0025   typedef HcalDbTool::IOVRun IOVRun;
0026   typedef std::map<IOVRun,std::string> IOVCollection;
0027   typedef std::pair<IOVRun,IOVRun> IntervalOV;
0028 
0029 class Args {
0030  public:
0031   Args () {};
0032   ~Args () {};
0033   void defineOption (const std::string& fOption, const std::string& fComment = "");
0034   void defineParameter (const std::string& fParameter, const std::string& fComment = "");
0035   void parse (int nArgs, char* fArgs []);
0036   void printOptionsHelp () const;
0037   std::string command () const;
0038   std::vector<std::string> arguments () const;
0039   bool optionIsSet (const std::string& fOption) const;
0040   std::string getParameter (const std::string& fKey);
0041  private:
0042   std::string mProgramName;
0043   std::vector <std::string> mOptions;
0044   std::vector <std::string> mParameters;
0045   std::vector <std::string> mArgs;
0046   std::map <std::string, std::string> mParsed;
0047   std::map <std::string, std::string> mComments;
0048 };
0049 
0050 void printHelp (const Args& args) {
0051   char buffer [1024];
0052   std::cout << "Initialize POOL DB with dummy HCAL data" << std::endl;
0053   std::cout << "    feedback -> ratnikov@fnal.gov" << std::endl;
0054   std::cout << "Use:" << std::endl;
0055   sprintf (buffer, " %s <parameters>\n", args.command ().c_str());
0056   std::cout << buffer;
0057   args.printOptionsHelp ();
0058 }
0059 
0060 void initDb (const std::string& inputdb, bool verbose) {
0061   HcalDbTool db (inputdb, verbose);
0062   if (inputdb.find ("sqlite") != std::string::npos) {
0063     unsigned pos = inputdb.find (':');
0064     if (pos != std::string::npos) {
0065       std::string filename (inputdb, pos+1);
0066       std::cout << "initDb-> creating file " << filename << std::endl;
0067       std::ifstream file (filename.c_str()); // touch file
0068       file.close ();
0069     }
0070   }
0071   // make dummy metadata entry
0072   std::cout << "initDb-> initializing metadata" << std::endl;
0073   db.metadataSetTag ("dummy_tag", "dummy_token");
0074   // make dummy objects
0075   std::cout << "initDb-> initializing pedestals" << std::endl;
0076   HcalPedestals* peds = new HcalPedestals;
0077   db.putObject (peds, "dummy_pedestals", 1);
0078   std::cout << "initDb-> initializing pedestal widths" << std::endl;
0079   HcalPedestalWidths* pedws = new HcalPedestalWidths;
0080   db.putObject (pedws, "dummy_pedestalwidths", 1);
0081   std::cout << "initDb-> initializing gains" << std::endl;
0082   HcalGains* gains = new HcalGains;
0083   db.putObject (gains, "dummy_gains", 1);
0084   std::cout << "initDb-> initializing gain widths" << std::endl;
0085   HcalGainWidths* gainws = new HcalGainWidths;
0086   db.putObject (gainws, "dummy_gainwidths", 1);
0087   std::cout << "initDb-> initializing qie" << std::endl;
0088   HcalQIEData* qie = new HcalQIEData;
0089   db.putObject (qie, "dummy_qie", 1);
0090   std::cout << "initDb-> initializing electronics map" << std::endl;
0091   HcalElectronicsMap* map = new HcalElectronicsMap;
0092   db.putObject (map, "dummy_map", 1);
0093   std::cout << "initDb-> initializing channel quality" << std::endl;
0094   HcalChannelQuality* quality = new HcalChannelQuality;
0095   db.putObject (quality, "dummy_quality", 1);
0096 }
0097 
0098 } // namespace
0099 
0100 int main (int argn, char* argv []) {
0101 
0102   Args args;
0103   args.defineParameter ("-db", "DB connection string, POOL format, i.e. oracle://devdb10/CMS_COND_HCAL");
0104   args.defineOption ("-help", "this help");
0105   args.defineOption ("-verbose", "verbose");
0106   
0107   args.parse (argn, argv);
0108   
0109   std::vector<std::string> arguments = args.arguments ();
0110 
0111   if (args.getParameter ("-db").empty() || args.optionIsSet ("-help")) {
0112     printHelp (args);
0113     return -1;
0114   }
0115 
0116   std::string inputdb = args.getParameter ("-db");
0117   bool verbose = args.optionIsSet ("-verbose");
0118 
0119   initDb (inputdb, verbose);
0120   return 0;
0121 }
0122 
0123 
0124 //==================== Args ===== BEGIN ==============================
0125 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
0126   mOptions.push_back (fOption);
0127   mComments [fOption] = fComment;
0128 }
0129 
0130 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {
0131   mParameters.push_back (fParameter);
0132   mComments [fParameter] = fComment;
0133 }
0134 
0135 void Args::parse (int nArgs, char* fArgs []) {
0136   if (nArgs <= 0) return;
0137   mProgramName = std::string (fArgs [0]);
0138   int iarg = 0;
0139   while (++iarg < nArgs) {
0140     std::string arg (fArgs [iarg]);
0141     if (arg [0] != '-') mArgs.push_back (arg);
0142     else {
0143       if (std::find (mOptions.begin(), mOptions.end (), arg) !=  mOptions.end ()) {
0144     mParsed [arg] = "";
0145       }
0146       if (std::find (mParameters.begin(), mParameters.end (), arg) !=  mParameters.end ()) {
0147     if (iarg >= nArgs) {
0148       std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
0149     }
0150     else {
0151       mParsed [arg] = std::string (fArgs [++iarg]);
0152     }
0153       }
0154     }
0155   }
0156 }
0157 
0158 void Args::printOptionsHelp () const {
0159   char buffer [1024];
0160   std::cout << "Parameters:" << std::endl;
0161   for (unsigned i = 0; i < mParameters.size (); i++) {
0162     std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
0163     std::string comment = it != mComments.end () ? it->second : "uncommented";
0164     sprintf (buffer, "  %-8s <value> : %s", (mParameters [i]).c_str(),  comment.c_str());
0165     std::cout << buffer << std::endl;
0166   }
0167   std::cout << "Options:" << std::endl;
0168   for (unsigned i = 0; i < mOptions.size (); i++) {
0169     std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
0170     std::string comment = it != mComments.end () ? it->second : "uncommented";
0171     sprintf (buffer, "  %-8s  : %s", (mOptions [i]).c_str(),  comment.c_str());
0172     std::cout << buffer << std::endl;
0173   }
0174 }
0175 
0176 std::string Args::command () const {
0177   int ipos = mProgramName.rfind ('/');
0178   return std::string (mProgramName, ipos+1);
0179 }
0180 
0181 std::vector<std::string> Args::arguments () const {return mArgs;}
0182 
0183 bool Args::optionIsSet (const std::string& fOption) const {
0184   return mParsed.find (fOption) != mParsed.end ();
0185 }
0186 
0187 std::string Args::getParameter (const std::string& fKey) {
0188   if (optionIsSet (fKey)) return mParsed [fKey];
0189   return "";
0190 }
0191 //==================== Args ===== END ==============================