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