00001 #include <stdlib.h>
00002
00003 #include <iostream>
00004 #include <fstream>
00005 #include <vector>
00006 #include <map>
00007 #include <algorithm>
00008 #include <string>
00009
00010
00011 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00012 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
00013 #include "DataFormats/HcalDetId/interface/HcalElectronicsId.h"
00014 #include "Geometry/CaloTopology/interface/HcalTopology.h"
00015
00016
00017
00018 #include "PluginManager/PluginManager.h"
00019 #include "POOLCore/POOLContext.h"
00020 #include "POOLCore/Token.h"
00021 #include "FileCatalog/URIParser.h"
00022 #include "FileCatalog/IFileCatalog.h"
00023 #include "StorageSvc/DbType.h"
00024 #include "PersistencySvc/DatabaseConnectionPolicy.h"
00025 #include "PersistencySvc/ISession.h"
00026 #include "PersistencySvc/ITransaction.h"
00027 #include "PersistencySvc/IDatabase.h"
00028 #include "PersistencySvc/Placement.h"
00029 #include "DataSvc/DataSvcFactory.h"
00030 #include "DataSvc/IDataSvc.h"
00031 #include "DataSvc/ICacheSvc.h"
00032 #include "DataSvc/Ref.h"
00033 #include "RelationalAccess/SchemaException.h"
00034 #include "Collection/Collection.h"
00035 #include "CoralBase/AttributeList.h"
00036 #include "FileCatalog/FCSystemTools.h"
00037 #include "FileCatalog/FCException.h"
00038 #include "FileCatalog/IFCAction.h"
00039
00040
00041 #include "CondCore/IOVService/interface/IOV.h"
00042 #include "CondCore/MetaDataService/interface/MetaData.h"
00043
00044
00045 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbHardcode.h"
00046 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbASCIIIO.h"
00047 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbXml.h"
00048 #include "CondFormats/HcalObjects/interface/HcalPedestals.h"
00049 #include "CondFormats/HcalObjects/interface/HcalPedestalWidths.h"
00050 #include "CondFormats/HcalObjects/interface/HcalGains.h"
00051 #include "CondFormats/HcalObjects/interface/HcalGainWidths.h"
00052 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
00053 #include "CondFormats/HcalObjects/interface/HcalQIEShape.h"
00054 #include "CondFormats/HcalObjects/interface/HcalChannelQuality.h"
00055 #include "CondFormats/HcalObjects/interface/HcalQIEData.h"
00056
00057
00058
00059 class Args {
00060 public:
00061 Args () {};
00062 ~Args () {};
00063 void defineOption (const std::string& fOption, const std::string& fComment = "");
00064 void defineParameter (const std::string& fParameter, const std::string& fComment = "");
00065 void parse (int nArgs, char* fArgs []);
00066 void printOptionsHelp () const;
00067 std::string command () const;
00068 std::vector<std::string> arguments () const;
00069 bool optionIsSet (const std::string& fOption) const;
00070 std::string getParameter (const std::string& fKey);
00071 private:
00072 std::string mProgramName;
00073 std::vector <std::string> mOptions;
00074 std::vector <std::string> mParameters;
00075 std::vector <std::string> mArgs;
00076 std::map <std::string, std::string> mParsed;
00077 std::map <std::string, std::string> mComments;
00078 };
00079
00080 class PoolData {
00081 public:
00082 PoolData (const std::string& fConnect);
00083 ~PoolData ();
00084
00085 template <class T>
00086 bool storeObject (T* fObject, const std::string& fContainer, pool::Ref<T>* fObject);
00087
00088 template <class T>
00089 bool updateObject (T* fObject, pool::Ref<T>* fUpdate);
00090
00091 template <class T>
00092 bool updateObject (pool::Ref<T>* fUpdate);
00093
00094 template <class T>
00095 bool storeIOV (const pool::Ref<T>& fObject, unsigned fMaxRun, pool::Ref<cond::IOV>* fIov);
00096
00097 template <class T>
00098 bool getObject (const pool::Ref<cond::IOV>& fIOV, unsigned fRun, pool::Ref<T>* fObject);
00099
00100 template <class T>
00101 bool getObject (const std::string& fToken, pool::Ref<T>* fObject);
00102
00103 pool::IDataSvc* service () {return mService;}
00104 std::string connect () {return mConnect;}
00105 private:
00106 std::string mConnect;
00107 pool::IFileCatalog mCatalog;
00108 pool::IDataSvc* mService;
00109 pool::Placement mPlacement;
00110 };
00111
00112 template <class T>
00113 std::vector<HcalDetId> undefinedCells (const T& fData) {
00114 static std::vector<HcalDetId> result;
00115 if (result.size () <= 0) {
00116 HcalTopology topology;
00117 for (int eta = -50; eta < 50; eta++) {
00118 for (int phi = 0; phi < 100; phi++) {
00119 for (int depth = 1; depth < 5; depth++) {
00120 for (int det = 1; det < 5; det++) {
00121 HcalDetId cell ((HcalSubdetector) det, eta, phi, depth);
00122 if (topology.valid(cell) && !fData.getValues (cell.rawId())) result.push_back (cell);
00123 }
00124 }
00125 }
00126 }
00127 }
00128 return result;
00129 }
00130
00131 void fillDefaults (HcalPedestals* fPedestals) {
00132 std::vector<HcalDetId> cells = undefinedCells (*fPedestals);
00133 for (std::vector <HcalDetId>::const_iterator cell = cells.begin (); cell != cells.end (); cell++) {
00134 HcalPedestal item = HcalDbHardcode::makePedestal (*cell);
00135 fPedestals->addValue (*cell, item.getValues ());
00136 }
00137 fPedestals->sort ();
00138 }
00139
00140 void fillDefaults (HcalGains* fGains) {
00141 std::vector<HcalDetId> cells = undefinedCells (*fGains);
00142 for (std::vector <HcalDetId>::const_iterator cell = cells.begin (); cell != cells.end (); cell++) {
00143 HcalGain item = HcalDbHardcode::makeGain (*cell);
00144 fGains->addValue (*cell, item.getValues ());
00145 }
00146 }
00147
00148 void fillDefaults (HcalElectronicsMap* fMap) {
00149 std::cerr << "ERROR: fillDefaults (HcalElectronicsMap* fMap) is not implemented. Ignore." << std::endl;
00150 }
00151
00152 void printHelp (const Args& args) {
00153 char buffer [1024];
00154 std::cout << "Tool to manipulate by Hcal Calibrations" << std::endl;
00155 std::cout << " feedback -> ratnikov@fnal.gov" << std::endl;
00156 std::cout << "Use:" << std::endl;
00157 sprintf (buffer, " %s dump <what> <options> <parameters>\n", args.command ().c_str());
00158 std::cout << buffer;
00159 sprintf (buffer, " %s fill <what> <options> <parameters>\n", args.command ().c_str());
00160 std::cout << buffer;
00161 sprintf (buffer, " %s add <what> <options> <parameters>\n", args.command ().c_str());
00162 std::cout << buffer;
00163 std::cout << " where <what> is: \n pedestals\n gains\n emap\n" << std::endl;
00164 args.printOptionsHelp ();
00165 }
00166
00167
00168
00169 int main (int argn, char* argv []) {
00170
00171 Args args;
00172 args.defineParameter ("-connect", "DB connection string, POOL format");
00173 args.defineParameter ("-input", "input file where to get constants from");
00174 args.defineParameter ("-output", "output file where to dump constants to");
00175 args.defineParameter ("-run", "run # for which constands should be dumped");
00176 args.defineParameter ("-tag", "tag for the constants set");
00177 args.defineOption ("-help", "this help");
00178 args.defineOption ("-defaults", "fill default values for not specifyed cells");
00179 args.defineOption ("-xml", "dump constants in XML format suitable for feeling online DB");
00180
00181 args.parse (argn, argv);
00182
00183 std::vector<std::string> arguments = args.arguments ();
00184
00185 if (arguments.size () < 2 || args.optionIsSet ("-help")) {
00186 printHelp (args);
00187 return -1;
00188 }
00189
00190 bool dump = arguments [0] == "dump";
00191 bool fill = arguments [0] == "fill";
00192 bool add = arguments [0] == "add";
00193 bool getPedestals = arguments [1] == "pedestals";
00194 bool getGains = arguments [1] == "gains";
00195 bool emap = arguments [1] == "emap";
00196 bool defaults = args.optionIsSet ("-defaults");
00197 bool xml = args.optionIsSet ("-xml");
00198
00199 std::string what = arguments [1];
00200
00201 std::string connect = args.getParameter ("-connect");
00202 if (connect.empty ()) {
00203 std::cerr << "ERROR: -connect is mandatory parameter" << std::endl;
00204 return -1;
00205 }
00206 std::string tag = args.getParameter ("-tag");
00207 if (tag.empty ()) {
00208 std::cerr << "ERROR: -tag is mandatory parameter" << std::endl;
00209 return -1;
00210 }
00211 std::string runStr = args.getParameter ("-run");
00212 if (runStr.empty ()) runStr = "0";
00213 unsigned run = atoi (runStr.c_str());
00214
00215 std::string metadataToken;
00216
00217 if (dump || add) {
00218 cond::MetaData md (connect);
00219 metadataToken = md.getToken (tag);
00220 if (metadataToken.empty ()) {
00221 std::cerr << "ERROR: can not find metadata for tag " << tag << std::cerr;
00222 return 2;
00223 }
00224 }
00225 {
00226 PoolData db (connect);
00227
00228
00229 pool::Ref<cond::IOV> iov;
00230 if (!metadataToken.empty ()) {
00231 db.getObject (metadataToken, &iov);
00232 if (iov.isNull ()) {
00233 std::cerr << "ERROR: can not find IOV for token " << metadataToken << std::endl;;
00234 return 2;
00235 }
00236 }
00237
00238 if (dump) {
00239 std::string output = args.getParameter ("-output");
00240 if (output.empty ()) output = "/dev/null";
00241 std::ofstream outStream (output.c_str ());
00242 if (getPedestals) {
00243 pool::Ref<HcalPedestals> ref;
00244 if (db.getObject (iov, run, &ref)) {
00245 if (xml) {
00246 HcalPedestalWidths widths; widths.sort ();
00247 HcalDbXml::dumpObject (outStream, run, tag, *ref, widths);
00248 }
00249 else {
00250 HcalDbASCIIIO::dumpObject (outStream, *ref);
00251 }
00252 }
00253 }
00254 else if (getGains) {
00255 pool::Ref<HcalGains> ref;
00256 if (db.getObject (iov, run, &ref)) {
00257 if (xml) {
00258 HcalGainWidths widths; widths.sort ();
00259 HcalDbXml::dumpObject (outStream, run, tag, *ref, widths);
00260 }
00261 else {
00262 HcalDbASCIIIO::dumpObject (outStream, *ref);
00263 }
00264 }
00265 }
00266 else if (emap) {
00267 pool::Ref<HcalElectronicsMap> ref;
00268 if (db.getObject (iov, run, &ref)) {
00269 HcalDbASCIIIO::dumpObject (outStream, *ref);
00270 }
00271 }
00272 else {
00273 std::cerr << "ERROR object " << what << " is not supported" << std::endl;
00274 }
00275 }
00276 else if (fill || add) {
00277 std::string input = args.getParameter ("-input");
00278 if (input.empty ()) input = "/dev/null";
00279 std::ifstream inStream (input.c_str ());
00280
00281 if (getPedestals) {
00282 HcalPedestals* obj = new HcalPedestals ();
00283 HcalDbASCIIIO::getObject (inStream, obj);
00284 if (defaults) fillDefaults (obj);
00285 pool::Ref<HcalPedestals> ref;
00286 if (!db.storeObject (obj, "HcalPedestals", &ref) ||
00287 !db.storeIOV (ref, run, &iov)) {
00288 std::cerr << "ERROR: failed to store object or its IOV" << std::endl;
00289 return 1;
00290 }
00291 }
00292 else if (getGains) {
00293 HcalGains* obj = new HcalGains ();
00294 HcalDbASCIIIO::getObject (inStream, obj);
00295 if (defaults) fillDefaults (obj);
00296 pool::Ref<HcalGains> ref;
00297 if (!db.storeObject (obj, "HcalGains", &ref) ||
00298 !db.storeIOV (ref, run, &iov)) {
00299 std::cerr << "ERROR: failed to store object or its IOV" << std::endl;
00300 return 1;
00301 }
00302 }
00303 else if (emap) {
00304 HcalElectronicsMap* obj = new HcalElectronicsMap ();
00305 HcalDbASCIIIO::getObject (inStream, obj);
00306 pool::Ref<HcalElectronicsMap> ref;
00307 if (defaults) fillDefaults (obj);
00308 if (!db.storeObject (obj, "HcalElectronicsMap", &ref) ||
00309 !db.storeIOV (ref, run, &iov)) {
00310 std::cerr << "ERROR: failed to store object or its IOV" << std::endl;
00311 return 1;
00312 }
00313 }
00314 metadataToken = iov.toString ();
00315 }
00316 else {
00317 std::cerr << "Unknown option. Try -help option for more details" << std::endl;
00318 }
00319 }
00320
00321 if (fill) {
00322 cond::MetaData md (connect);
00323 md.addMapping (tag, metadataToken);
00324 }
00325 return 0;
00326 }
00327
00328
00329
00330
00331 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
00332 mOptions.push_back (fOption);
00333 mComments [fOption] = fComment;
00334 }
00335
00336 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {
00337 mParameters.push_back (fParameter);
00338 mComments [fParameter] = fComment;
00339 }
00340
00341 void Args::parse (int nArgs, char* fArgs []) {
00342 if (nArgs <= 0) return;
00343 mProgramName = std::string (fArgs [0]);
00344 int iarg = 0;
00345 while (++iarg < nArgs) {
00346 std::string arg (fArgs [iarg]);
00347 if (arg [0] != '-') mArgs.push_back (arg);
00348 else {
00349 if (std::find (mOptions.begin(), mOptions.end (), arg) != mOptions.end ()) {
00350 mParsed [arg] = "";
00351 }
00352 if (std::find (mParameters.begin(), mParameters.end (), arg) != mParameters.end ()) {
00353 if (iarg >= nArgs) {
00354 std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
00355 }
00356 else {
00357 mParsed [arg] = std::string (fArgs [++iarg]);
00358 }
00359 }
00360 }
00361 }
00362 }
00363
00364 void Args::printOptionsHelp () const {
00365 char buffer [1024];
00366 std::cout << "Parameters:" << std::endl;
00367 for (unsigned i = 0; i < mParameters.size (); i++) {
00368 std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
00369 std::string comment = it != mComments.end () ? it->second : "uncommented";
00370 sprintf (buffer, " %-8s <value> : %s", (mParameters [i]).c_str(), comment.c_str());
00371 std::cout << buffer << std::endl;
00372 }
00373 std::cout << "Options:" << std::endl;
00374 for (unsigned i = 0; i < mOptions.size (); i++) {
00375 std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
00376 std::string comment = it != mComments.end () ? it->second : "uncommented";
00377 sprintf (buffer, " %-8s : %s", (mOptions [i]).c_str(), comment.c_str());
00378 std::cout << buffer << std::endl;
00379 }
00380 }
00381
00382 std::string Args::command () const {
00383 int ipos = mProgramName.rfind ('/');
00384 return std::string (mProgramName, ipos+1);
00385 }
00386
00387 std::vector<std::string> Args::arguments () const {return mArgs;}
00388
00389 bool Args::optionIsSet (const std::string& fOption) const {
00390 return mParsed.find (fOption) != mParsed.end ();
00391 }
00392
00393 std::string Args::getParameter (const std::string& fKey) {
00394 if (optionIsSet (fKey)) return mParsed [fKey];
00395 return "";
00396 }
00397
00398
00399
00400 PoolData::PoolData (const std::string& fConnect)
00401 : mConnect (fConnect)
00402 {
00403 std::cout << "PoolData-> Using DB connection: " << fConnect << std::endl;
00404 seal::PluginManager::get()->initialise();
00405 pool::POOLContext::loadComponent( "SEAL/Services/MessageService" );
00406 pool::POOLContext::loadComponent( "POOL/Services/EnvironmentAuthenticationService" );
00407
00408 pool::URIParser parser;
00409 parser.parse();
00410
00411
00412 mCatalog.setWriteCatalog(parser.contactstring());
00413 mCatalog.connect();
00414 mCatalog.start();
00415
00416 mService = pool::DataSvcFactory::create (&mCatalog);
00417
00418 pool::DatabaseConnectionPolicy policy;
00419 policy.setWriteModeForNonExisting(pool::DatabaseConnectionPolicy::CREATE);
00420 policy.setWriteModeForExisting(pool::DatabaseConnectionPolicy::UPDATE);
00421 mService->session().setDefaultConnectionPolicy(policy);
00422 mPlacement.setDatabase(mConnect, pool::DatabaseSpecification::PFN);
00423 mPlacement.setTechnology(pool::POOL_RDBMS_StorageType.type());
00424 }
00425
00426 PoolData::~PoolData () {
00427 mService->session().disconnectAll();
00428 mCatalog.commit ();
00429 mCatalog.disconnect ();
00430 delete mService;
00431 }
00432
00433 template <class T>
00434 bool PoolData::storeObject (T* fObject, const std::string& fContainer, pool::Ref<T>* fRef) {
00435 if (!fRef->isNull ()) {
00436 std::cerr << "PoolData::storeObject-> Ref is not empty. Ignore." << std::endl;
00437 return false;
00438 }
00439 try {
00440 mService->transaction().start(pool::ITransaction::UPDATE);
00441 *fRef = pool::Ref <T> (mService, fObject);
00442 mPlacement.setContainerName (fContainer);
00443 fRef->markWrite (mPlacement);
00444 mService->transaction().commit();
00445 }
00446 catch (...) {
00447 std::cerr << "PoolData::storeObject error " << std::endl;
00448 return false;
00449 }
00450 return true;
00451 }
00452
00453 template <class T>
00454 bool PoolData::updateObject (T* fObject, pool::Ref<T>* fUpdate) {
00455 try {
00456 mService->transaction().start(pool::ITransaction::UPDATE);
00457 if (fObject) *(fUpdate->ptr ()) = *fObject;
00458 fUpdate->markUpdate();
00459 mService->transaction().commit();
00460 }
00461 catch (...) {
00462 std::cerr << "PoolData::updateObject error " << std::endl;
00463 return false;
00464 }
00465 return true;
00466 }
00467
00468 template <class T>
00469 bool PoolData::updateObject (pool::Ref<T>* fUpdate) {
00470 return updateObject ((T*)0, fUpdate);
00471 }
00472
00473 template <class T>
00474 bool PoolData::storeIOV (const pool::Ref<T>& fObject, unsigned fMaxRun, pool::Ref<cond::IOV>* fIov) {
00475 unsigned maxRun = fMaxRun == 0 ? 0xffffffff : fMaxRun;
00476 if (fIov->isNull ()) {
00477 cond::IOV* newIov = new cond::IOV ();
00478 newIov->iov.insert (std::make_pair (maxRun, fObject.toString ()));
00479 return storeObject (newIov, "IOV", fIov);
00480 }
00481 else {
00482 (*fIov)->iov.insert (std::make_pair (maxRun, fObject.toString ()));
00483 return updateObject (fIov);
00484 }
00485 }
00486
00487 template <class T>
00488 bool PoolData::getObject (const pool::Ref<cond::IOV>& fIOV, unsigned fRun, pool::Ref<T>* fObject) {
00489 if (!fIOV.isNull ()) {
00490
00491 for (std::map<unsigned long,std::string>::iterator iovi = fIOV->iov.begin (); iovi != fIOV->iov.end (); iovi++) {
00492 std::cout << "PoolData::getObject-> size: " << fIOV->iov.size () << " run:" << iovi->first << std::endl;
00493 if (fRun <= iovi->first) {
00494 std::string token = iovi->second;
00495 return getObject (token, fObject);
00496 }
00497 }
00498 std::cerr << "PoolData::getObject-> no object for run " << fRun << " is found" << std::endl;
00499 }
00500 else {
00501 std::cerr << "PoolData::getObject-> IOV reference is not set" << std::endl;
00502 }
00503 return false;
00504 }
00505
00506 template <class T>
00507 bool PoolData::getObject (const std::string& fToken, pool::Ref<T>* fObject) {
00508 try {
00509 *fObject = pool::Ref <T> (service (), fToken);
00510 service ()->transaction().start(pool::ITransaction::READ);
00511 fObject->isNull ();
00512 mService->transaction().commit();
00513 }
00514 catch(const coral::TableNotExistingException& e) {
00515 std::cerr << "PoolData::getObject-> coral::TableNotExisting Exception" << std::endl;
00516 }
00517 catch (const seal::Exception& e) {
00518 std::cerr<<"PoolData::getObject-> seal exception: " << e.what() << std::endl;
00519 }
00520 catch(...){
00521 std::cerr << "PoolData::getObject-> Funny error" << std::endl;
00522 }
00523 return !(fObject->isNull ());
00524 }
00525
00526