CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/CalibCalorimetry/HcalStandardModules/bin/HcalPedestalValidator.cc

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <vector>
00003 #include <map>
00004 #include <string>
00005 #include <sstream>
00006 #include <fstream>
00007 #include <iostream>
00008 
00009 #include "CondFormats/HcalObjects/interface/HcalPedestals.h"
00010 #include "CondFormats/HcalObjects/interface/HcalPedestalWidths.h"
00011 #include "CondTools/Hcal/interface/HcalDbOnline.h"
00012 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbASCIIIO.h"
00013 #include "CalibCalorimetry/HcalAlgos/interface/HcalPedestalAnalysis.h"
00014 
00015 namespace {
00016   bool defaultsFile (const std::string fParam) {
00017     return fParam == "defaults";
00018   }
00019 
00020   bool asciiFile (const std::string fParam) {
00021     return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".txt";
00022   }
00023   
00024   bool xmlFile (const std::string fParam) {
00025     return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".xml";
00026   }
00027   
00028   bool dbFile (const std::string fParam) {
00029     return fParam.find (':') != std::string::npos;
00030   }
00031 
00032   bool masterDb (const std::string fParam) {
00033     return fParam.find ('@') != std::string::npos;
00034   }
00035 
00036   template <class T> 
00037   bool getObject (T* fObject, const std::string& fDb, const std::string& fTag, int fRun) {
00038     if (!fObject) return false;
00039     if (fDb.empty ()) return false; 
00040     if (asciiFile (fDb)) {
00041       std::ifstream stream (fDb.c_str ());
00042       HcalDbASCIIIO::getObject (stream, fObject); 
00043       return true;
00044     }
00045     else if (masterDb (fDb)) {
00046       std::cout << "HcalPedestalValidator-> Use input: MasterDB " << fDb << std::endl;
00047       HcalDbOnline masterDb (fDb);
00048       return masterDb.getObject (fObject, fTag, fRun);
00049     }
00050     else {
00051       return false;
00052     }
00053   }
00054   
00055   template <class T>
00056   bool putObject (T** fObject, const std::string& fDb, const std::string& fTag, int fRun) {
00057     if (!fObject || !*fObject) return false;
00058     if (fDb.empty ()) return false;
00059     if (asciiFile (fDb)) {
00060       std::ofstream stream (fDb.c_str ());
00061       HcalDbASCIIIO::dumpObject (stream, **fObject);
00062       return true;
00063     }
00064     else {
00065       return false;
00066     }
00067   }
00068 }
00069 
00070 // Args is a copy-paste from Fedor's peds_txt2xml.cc
00071 class Args {
00072  public:
00073   Args () {};
00074   ~Args () {};
00075   void defineOption (const std::string& fOption, const std::string& fComment = "");
00076   void defineParameter (const std::string& fParameter, const std::string& fComment = "");
00077   void parse (int nArgs, char* fArgs []);
00078   void printOptionsHelp () const;
00079   std::string command () const;
00080   std::vector<std::string> arguments () const;
00081   bool optionIsSet (const std::string& fOption) const;
00082   std::string getParameter (const std::string& fKey);
00083  private:
00084   std::string mProgramName;
00085   std::vector <std::string> mOptions;
00086   std::vector <std::string> mParameters;
00087   std::vector <std::string> mArgs;
00088   std::map <std::string, std::string> mParsed;
00089   std::map <std::string, std::string> mComments;
00090 };
00091 
00092 int main (int argn, char* argv []) {
00093 
00094 // CORAL required variables to be set, even if not needed
00095   const char* foo1 = "CORAL_AUTH_USER=blaaah";
00096   const char* foo2 = "CORAL_AUTH_PASSWORD=blaaah";
00097   if (!::getenv("CORAL_AUTH_USER")) ::putenv(const_cast<char*>(foo1));
00098   if (!::getenv("CORAL_AUTH_PASSWORD")) ::putenv(const_cast<char*>(foo2)); 
00099 
00100   Args args;
00101   args.defineParameter ("-p", "raw pedestals");
00102   args.defineParameter ("-w", "raw widths");
00103   args.defineParameter ("-run", "current run number <0>");
00104   args.defineParameter ("-ptag", "raw pedestal tag <NULL>");
00105   args.defineParameter ("-wtag", "raw width tag <ptag>");
00106   args.defineParameter ("-pref", "reference pedestals");
00107   args.defineParameter ("-wref", "reference widths");
00108   args.defineParameter ("-ptagref", "reference pedestal tag <NULL>");
00109   args.defineParameter ("-wtagref", "reference width tag <ptagref>");
00110   args.defineParameter ("-pval", "validated pedestals");
00111   args.defineParameter ("-wval", "validated widths");
00112   args.defineOption ("-help", "this help");
00113 
00114   args.parse (argn, argv);
00115   std::vector<std::string> arguments = args.arguments ();
00116   if (args.optionIsSet ("-help")) {
00117     args.printOptionsHelp ();
00118     return -1;
00119   }
00120 
00121 // read parameters from command line
00122   std::string RawPedSource = args.getParameter("-p");
00123   std::string RawPedWidSource = args.getParameter("-w");
00124   std::string RawPedTag = args.getParameter("-ptag").empty() ? "" : args.getParameter("-ptag");
00125   std::string RawPedWidTag = args.getParameter("-wtag").empty() ? RawPedTag : args.getParameter("-wtag");
00126   int RawPedRun = args.getParameter("-run").empty() ? 0 : (int)strtoll (args.getParameter("-run").c_str(),0,0);
00127   int RawPedWidRun = RawPedRun;
00128   std::string RefPedSource = args.getParameter("-pref");
00129   std::string RefPedWidSource = args.getParameter("-wref");
00130   std::string RefPedTag = args.getParameter("-ptagref").empty() ? "" : args.getParameter("-ptagref");
00131   std::string RefPedWidTag = args.getParameter("-wtagref").empty() ? RefPedTag : args.getParameter("-wtagref");
00132   int RefPedRun = RawPedRun;
00133   int RefPedWidRun = RefPedRun;
00134   std::string outputPedDest = args.getParameter("-pval");
00135   std::string outputPedWidDest = args.getParameter("-wval");
00136   std::string outputPedTag = "";
00137   std::string outputPedWidTag = "";
00138   int outputPedRun = RawPedRun;
00139   int outputPedWidRun = outputPedRun;
00140 
00141 // get reference objects
00142   HcalPedestals* RefPeds = 0;
00143   RefPeds = new HcalPedestals ();
00144   if (!getObject (RefPeds, RefPedSource, RefPedTag, RefPedRun)) {
00145     std::cerr << "HcalPedestalValidator-> Failed to get reference Pedestals" << std::endl;
00146     return 1;
00147   }
00148   HcalPedestalWidths* RefPedWids = 0;
00149   RefPedWids = new HcalPedestalWidths ();
00150   if (!getObject (RefPedWids, RefPedWidSource, RefPedWidTag, RefPedWidRun)) {
00151     std::cerr << "HcalPedestalValidator-> Failed to get reference PedestalWidths" << std::endl;
00152     return 2;
00153   }
00154 
00155 // get input raw objects
00156   HcalPedestals* RawPeds = 0;
00157   RawPeds = new HcalPedestals ();
00158   if (!getObject (RawPeds, RawPedSource, RawPedTag, RawPedRun)) {
00159     std::cerr << "HcalPedestalValidator-> Failed to get raw Pedestals" << std::endl;
00160     return 3;
00161   }
00162   HcalPedestalWidths* RawPedWids = 0;
00163   RawPedWids = new HcalPedestalWidths ();
00164   if (!getObject (RawPedWids, RawPedWidSource, RawPedWidTag, RawPedWidRun)) {
00165     std::cerr << "HcalPedestalValidator-> Failed to get raw PedestalWidths" << std::endl;
00166     return 4;
00167   }
00168 
00169 // make output objects
00170   HcalPedestals* outputPeds = 0;
00171   outputPeds = new HcalPedestals ();
00172   HcalPedestalWidths* outputPedWids = 0;
00173   outputPedWids = new HcalPedestalWidths ();
00174 
00175 // run algorithm
00176   int nstat[4]={2500,2500,2500,2500};
00177   int Flag=HcalPedestalAnalysis::HcalPedVal(nstat,RefPeds,RefPedWids,RawPeds,RawPedWids,outputPeds,outputPedWids);
00178 
00179   delete RefPeds;
00180   delete RefPedWids;
00181   delete RawPeds;
00182   delete RawPedWids;
00183 
00184 
00185 // store new objects if necessary
00186   if (Flag%100000>0) {
00187     if (outputPeds) {
00188       if (!putObject (&outputPeds, outputPedDest, outputPedTag, outputPedRun)) {
00189           std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals" << std::endl;
00190           return 5;
00191         }
00192     }
00193     if (outputPedWids) {
00194         if (!putObject (&outputPedWids, outputPedWidDest, outputPedWidTag, outputPedWidRun)) {
00195         std::cerr << "HcalPedestalAnalyzer-> Failed to put output PedestalWidths" << std::endl;
00196         return 6;
00197       }
00198     }
00199   }
00200   delete outputPeds;
00201   delete outputPedWids;
00202 
00203 return 0;
00204 }
00205 
00206 //==================== Args ===== BEGIN ==============================
00207 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
00208   mOptions.push_back (fOption);
00209   mComments [fOption] = fComment;
00210 }
00211 
00212 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {   mParameters.push_back (fParameter);
00213   mComments [fParameter] = fComment;
00214 }
00215 
00216 void Args::parse (int nArgs, char* fArgs []) {
00217   if (nArgs <= 0) return;
00218   mProgramName = std::string (fArgs [0]);
00219   int iarg = 0;
00220   while (++iarg < nArgs) {
00221     std::string arg (fArgs [iarg]);
00222     if (arg [0] != '-') mArgs.push_back (arg);
00223     else {
00224       if (std::find (mOptions.begin(), mOptions.end (), arg) !=  mOptions.end ()) {
00225         mParsed [arg] = "";
00226       }
00227       if (std::find (mParameters.begin(), mParameters.end (), arg) !=  mParameters.end ()) {
00228         if (iarg >= nArgs) {
00229           std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
00230         }
00231         else {
00232           mParsed [arg] = std::string (fArgs [++iarg]);
00233         }
00234       }
00235     }
00236   }
00237 }
00238 
00239 void Args::printOptionsHelp () const {
00240   char buffer [1024];
00241   std::cout << "Parameters:" << std::endl;
00242   for (unsigned i = 0; i < mParameters.size (); i++) {
00243     std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
00244     std::string comment = it != mComments.end () ? it->second : "uncommented";
00245     sprintf (buffer, "  %-8s <value> : %s", (mParameters [i]).c_str(),  comment.c_str());
00246     std::cout << buffer << std::endl;
00247   }
00248   std::cout << "Options:" << std::endl;
00249   for (unsigned i = 0; i < mOptions.size (); i++) {
00250     std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
00251     std::string comment = it != mComments.end () ? it->second : "uncommented";
00252     sprintf (buffer, "  %-8s  : %s", (mOptions [i]).c_str(),  comment.c_str());
00253     std::cout << buffer << std::endl;
00254   }
00255 }
00256 
00257 std::string Args::command () const {
00258   int ipos = mProgramName.rfind ('/');
00259   return std::string (mProgramName, ipos+1);
00260 }
00261 
00262 std::vector<std::string> Args::arguments () const {return mArgs;}
00263 
00264 bool Args::optionIsSet (const std::string& fOption) const {
00265   return mParsed.find (fOption) != mParsed.end ();
00266 }
00267 
00268 std::string Args::getParameter (const std::string& fKey) {
00269   if (optionIsSet (fKey)) return mParsed [fKey];
00270   return "";
00271 }
00272