CMS 3D CMS Logo

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   if (!::getenv("CORAL_AUTH_USER")) ::putenv("CORAL_AUTH_USER=blaaah");
00096   if (!::getenv("CORAL_AUTH_PASSWORD")) ::putenv("CORAL_AUTH_PASSWORD=blaaah"); 
00097 
00098   Args args;
00099   args.defineParameter ("-p", "raw pedestals");
00100   args.defineParameter ("-w", "raw widths");
00101   args.defineParameter ("-run", "current run number <0>");
00102   args.defineParameter ("-ptag", "raw pedestal tag <NULL>");
00103   args.defineParameter ("-wtag", "raw width tag <ptag>");
00104   args.defineParameter ("-pref", "reference pedestals");
00105   args.defineParameter ("-wref", "reference widths");
00106   args.defineParameter ("-ptagref", "reference pedestal tag <NULL>");
00107   args.defineParameter ("-wtagref", "reference width tag <ptagref>");
00108   args.defineParameter ("-pval", "validated pedestals");
00109   args.defineParameter ("-wval", "validated widths");
00110   args.defineOption ("-help", "this help");
00111 
00112   args.parse (argn, argv);
00113   std::vector<std::string> arguments = args.arguments ();
00114   if (args.optionIsSet ("-help")) {
00115     args.printOptionsHelp ();
00116     return -1;
00117   }
00118 
00119 // read parameters from command line
00120   std::string RawPedSource = args.getParameter("-p");
00121   std::string RawPedWidSource = args.getParameter("-w");
00122   std::string RawPedTag = args.getParameter("-ptag").empty() ? "" : args.getParameter("-ptag");
00123   std::string RawPedWidTag = args.getParameter("-wtag").empty() ? RawPedTag : args.getParameter("-wtag");
00124   int RawPedRun = args.getParameter("-run").empty() ? 0 : (int)strtoll (args.getParameter("-run").c_str(),0,0);
00125   int RawPedWidRun = RawPedRun;
00126   std::string RefPedSource = args.getParameter("-pref");
00127   std::string RefPedWidSource = args.getParameter("-wref");
00128   std::string RefPedTag = args.getParameter("-ptagref").empty() ? "" : args.getParameter("-ptagref");
00129   std::string RefPedWidTag = args.getParameter("-wtagref").empty() ? RefPedTag : args.getParameter("-wtagref");
00130   int RefPedRun = RawPedRun;
00131   int RefPedWidRun = RefPedRun;
00132   std::string outputPedDest = args.getParameter("-pval");
00133   std::string outputPedWidDest = args.getParameter("-wval");
00134   std::string outputPedTag = "";
00135   std::string outputPedWidTag = "";
00136   int outputPedRun = RawPedRun;
00137   int outputPedWidRun = outputPedRun;
00138 
00139 // get reference objects
00140   HcalPedestals* RefPeds = 0;
00141   RefPeds = new HcalPedestals ();
00142   if (!getObject (RefPeds, RefPedSource, RefPedTag, RefPedRun)) {
00143     std::cerr << "HcalPedestalValidator-> Failed to get reference Pedestals" << std::endl;
00144     return 1;
00145   }
00146   HcalPedestalWidths* RefPedWids = 0;
00147   RefPedWids = new HcalPedestalWidths ();
00148   if (!getObject (RefPedWids, RefPedWidSource, RefPedWidTag, RefPedWidRun)) {
00149     std::cerr << "HcalPedestalValidator-> Failed to get reference PedestalWidths" << std::endl;
00150     return 2;
00151   }
00152 
00153 // get input raw objects
00154   HcalPedestals* RawPeds = 0;
00155   RawPeds = new HcalPedestals ();
00156   if (!getObject (RawPeds, RawPedSource, RawPedTag, RawPedRun)) {
00157     std::cerr << "HcalPedestalValidator-> Failed to get raw Pedestals" << std::endl;
00158     return 3;
00159   }
00160   HcalPedestalWidths* RawPedWids = 0;
00161   RawPedWids = new HcalPedestalWidths ();
00162   if (!getObject (RawPedWids, RawPedWidSource, RawPedWidTag, RawPedWidRun)) {
00163     std::cerr << "HcalPedestalValidator-> Failed to get raw PedestalWidths" << std::endl;
00164     return 4;
00165   }
00166 
00167 // make output objects
00168   HcalPedestals* outputPeds = 0;
00169   outputPeds = new HcalPedestals ();
00170   HcalPedestalWidths* outputPedWids = 0;
00171   outputPedWids = new HcalPedestalWidths ();
00172 
00173 // run algorithm
00174   int nstat[4]={2500,2500,2500,2500};
00175   int Flag=HcalPedestalAnalysis::HcalPedVal(nstat,RefPeds,RefPedWids,RawPeds,RawPedWids,outputPeds,outputPedWids);
00176 
00177   delete RefPeds;
00178   delete RefPedWids;
00179   delete RawPeds;
00180   delete RawPedWids;
00181 
00182 
00183 // store new objects if necessary
00184   if (Flag%100000>0) {
00185     if (outputPeds) {
00186       if (!putObject (&outputPeds, outputPedDest, outputPedTag, outputPedRun)) {
00187           std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals" << std::endl;
00188           return 5;
00189         }
00190     }
00191     if (outputPedWids) {
00192         if (!putObject (&outputPedWids, outputPedWidDest, outputPedWidTag, outputPedWidRun)) {
00193         std::cerr << "HcalPedestalAnalyzer-> Failed to put output PedestalWidths" << std::endl;
00194         return 6;
00195       }
00196     }
00197   }
00198   delete outputPeds;
00199   delete outputPedWids;
00200 
00201 return 0;
00202 }
00203 
00204 //==================== Args ===== BEGIN ==============================
00205 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
00206   mOptions.push_back (fOption);
00207   mComments [fOption] = fComment;
00208 }
00209 
00210 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {   mParameters.push_back (fParameter);
00211   mComments [fParameter] = fComment;
00212 }
00213 
00214 void Args::parse (int nArgs, char* fArgs []) {
00215   if (nArgs <= 0) return;
00216   mProgramName = std::string (fArgs [0]);
00217   int iarg = 0;
00218   while (++iarg < nArgs) {
00219     std::string arg (fArgs [iarg]);
00220     if (arg [0] != '-') mArgs.push_back (arg);
00221     else {
00222       if (std::find (mOptions.begin(), mOptions.end (), arg) !=  mOptions.end ()) {
00223         mParsed [arg] = "";
00224       }
00225       if (std::find (mParameters.begin(), mParameters.end (), arg) !=  mParameters.end ()) {
00226         if (iarg >= nArgs) {
00227           std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
00228         }
00229         else {
00230           mParsed [arg] = std::string (fArgs [++iarg]);
00231         }
00232       }
00233     }
00234   }
00235 }
00236 
00237 void Args::printOptionsHelp () const {
00238   char buffer [1024];
00239   std::cout << "Parameters:" << std::endl;
00240   for (unsigned i = 0; i < mParameters.size (); i++) {
00241     std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
00242     std::string comment = it != mComments.end () ? it->second : "uncommented";
00243     sprintf (buffer, "  %-8s <value> : %s", (mParameters [i]).c_str(),  comment.c_str());
00244     std::cout << buffer << std::endl;
00245   }
00246   std::cout << "Options:" << std::endl;
00247   for (unsigned i = 0; i < mOptions.size (); i++) {
00248     std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
00249     std::string comment = it != mComments.end () ? it->second : "uncommented";
00250     sprintf (buffer, "  %-8s  : %s", (mOptions [i]).c_str(),  comment.c_str());
00251     std::cout << buffer << std::endl;
00252   }
00253 }
00254 
00255 std::string Args::command () const {
00256   int ipos = mProgramName.rfind ('/');
00257   return std::string (mProgramName, ipos+1);
00258 }
00259 
00260 std::vector<std::string> Args::arguments () const {return mArgs;}
00261 
00262 bool Args::optionIsSet (const std::string& fOption) const {
00263   return mParsed.find (fOption) != mParsed.end ();
00264 }
00265 
00266 std::string Args::getParameter (const std::string& fKey) {
00267   if (optionIsSet (fKey)) return mParsed [fKey];
00268   return "";
00269 }
00270 

Generated on Tue Jun 9 17:25:20 2009 for CMSSW by  doxygen 1.5.4