00001 #ifndef CommonTools_ConditionDBWriter_ConditionDBWriter_h
00002 #define CommonTools_ConditionDBWriter_ConditionDBWriter_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 #include <memory>
00138 #include <string>
00139 #include <cstdlib>
00140
00141
00142
00143 #include "FWCore/Framework/interface/Frameworkfwd.h"
00144 #include "FWCore/Framework/interface/EDAnalyzer.h"
00145 #include "FWCore/Framework/interface/Run.h"
00146 #include "CondCore/DBCommon/interface/Time.h"
00147 #include "FWCore/Framework/interface/ESHandle.h"
00148 #include "FWCore/ServiceRegistry/interface/Service.h"
00149 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00150
00151 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00152 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00153
00154 #include "FWCore/Utilities/interface/Exception.h"
00155
00156
00157 #include "FWCore/Framework/interface/Event.h"
00158 #include "DataFormats/Common/interface/Handle.h"
00159
00160
00161 template< class T >
00162 class ConditionDBWriter : public edm::EDAnalyzer {
00163
00164 public:
00165
00166 explicit ConditionDBWriter(const edm::ParameterSet& iConfig) : LumiBlockMode_(false), RunMode_(false), JobMode_(false), AlgoDrivenMode_(false), Time_(0), setSinceTime_(false) {
00167
00168 edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
00169 SinceAppendMode_=iConfig.getParameter<bool>("SinceAppendMode");
00170 std::string IOVMode=iConfig.getParameter<std::string>("IOVMode");
00171 if (IOVMode==std::string("Job")) JobMode_=true;
00172 else if (IOVMode==std::string("Run")) RunMode_=true;
00173 else if (IOVMode==std::string("LumiBlock")) LumiBlockMode_=true;
00174 else if (IOVMode==std::string("AlgoDriven")) AlgoDrivenMode_=true;
00175 else edm::LogError("ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything on the DB") << std::endl;
00176 Record_=iConfig.getParameter<std::string>("Record");
00177 doStore_=iConfig.getParameter<bool>("doStoreOnDB");
00178
00179
00180 }
00181
00182
00183 virtual ~ConditionDBWriter(){
00184
00185 edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl;
00186
00187 }
00188
00189
00190 private:
00191
00192
00193
00194
00195 virtual T * getNewObject()=0;
00196
00197
00198
00199
00200
00201 virtual void algoBeginJob(const edm::EventSetup&){};
00202
00203 virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
00204
00205 virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00206
00207 virtual void algoAnalyze(const edm::Event&, const edm::EventSetup&){};
00208
00209 virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
00210
00211 virtual void algoEndJob(){};
00212
00213
00214
00215 void beginJob(const edm::EventSetup& iSetup){
00216
00217 edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
00218 if( (JobMode_ || AlgoDrivenMode_) && SinceAppendMode_) setSinceTime_=true;
00219 algoBeginJob(iSetup);
00220
00221 }
00222
00223
00224 void beginRun(const edm::Run & run, const edm::EventSetup & es){
00225
00226 edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
00227 if(RunMode_ && SinceAppendMode_) setSinceTime_=true;
00228 algoBeginRun(run,es);
00229
00230 }
00231
00232
00233
00234 void beginLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & iSetup){
00235
00236 edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
00237 if(LumiBlockMode_ && SinceAppendMode_) setSinceTime_=true;
00238 algoBeginLuminosityBlock(lumiBlock, iSetup);
00239
00240 }
00241
00242
00243 void analyze(const edm::Event& event, const edm::EventSetup& iSetup){
00244
00245 if(SinceAppendMode_ && setSinceTime_ ){
00246 setTime();
00247 setSinceTime_=false;
00248 }
00249
00250 algoAnalyze(event, iSetup);
00251
00252 }
00253
00254
00255
00256 void endLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & es){
00257
00258 edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
00259 algoEndLuminosityBlock(lumiBlock, es);
00260
00261 if(LumiBlockMode_){
00262
00263 T * objPointer = getNewObject();
00264
00265 if(objPointer ){
00266 storeOnDb(objPointer);
00267 }
00268 else {
00269 edm::LogError("ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, but received null pointer...will not store anything on the DB") << std::endl;
00270 }
00271
00272 }
00273
00274 }
00275
00276
00277 virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00278
00279
00280 void endRun(const edm::Run & run, const edm::EventSetup & es){
00281
00282 edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
00283
00284 algoEndRun(run, es);
00285
00286 if(RunMode_){
00287
00288 T * objPointer = getNewObject();
00289
00290 if(objPointer ){
00291 storeOnDb(objPointer);
00292 }
00293 else {
00294 edm::LogError("ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null pointer...will not store anything on the DB") << std::endl;
00295 }
00296
00297 }
00298
00299 }
00300
00301
00302
00303 void endJob(){
00304
00305 edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
00306
00307 algoEndJob();
00308
00309 if(JobMode_){
00310
00311 T * objPointer = getNewObject();
00312
00313 if( objPointer ){
00314 storeOnDb(objPointer);
00315 }
00316
00317 else {
00318
00319 edm::LogError("ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null pointer...will not store anything on the DB") << std::endl;
00320
00321 }
00322
00323 }
00324
00325 }
00326
00327
00328 void storeOnDb(T * objPointer){
00329
00330 edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
00331
00332 if(! SinceAppendMode_ ) setTime();
00333 else setSinceTime_=true;
00334
00335 if(! objPointer) {
00336 edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB") ;
00337 return;
00338 }
00339
00340
00341
00342 edm::Service<cond::service::PoolDBOutputService> mydbservice;
00343
00344 if( doStore_ && mydbservice.isAvailable() ){
00345
00346 try{
00347
00348 bool tillDone=false;
00349
00350
00351 if( mydbservice->isNewTagRequest(Record_) ){
00352
00353 edm::LogInfo("ConditionDBWriter") << "first request for storing objects with Record "<< Record_ << std::endl;
00354
00355 if(SinceAppendMode_) {
00356
00357
00358 edm::LogInfo("ConditionDBWriter") << "appending a new object to existing tag " <<Record_ <<" in since mode " << std::endl;
00359 mydbservice->createNewIOV<T>(objPointer, mydbservice->beginOfTime(), mydbservice->endOfTime(), Record_);
00360
00361
00362 }
00363 else{
00364 edm::LogInfo("ConditionDBWriter") << "appending a new object to new tag "<<Record_<< " in till mode " << std::endl;
00365 mydbservice->createNewIOV<T>(objPointer, mydbservice->beginOfTime(),Time_, Record_);
00366 tillDone=true;
00367 }
00368
00369 }
00370 else {
00371
00372 if(SinceAppendMode_){
00373 edm::LogInfo("ConditionDBWriter") << "appending a new object to existing tag " <<Record_ <<" in since mode " << std::endl;
00374 mydbservice->appendSinceTime<T>(objPointer, Time_, Record_);
00375 }
00376 else if(!tillDone){
00377 edm::LogInfo("ConditionDBWriter") << "appending a new object to existing tag "<<Record_ <<" in till mode." << std::endl;
00378
00379 mydbservice->appendTillTime<T>(objPointer, Time_, Record_);
00380 }
00381
00382 }
00383
00384 }catch(const cond::Exception& er){
00385 edm::LogError("ConditionDBWriter")<<er.what()<<std::endl;
00386 }catch(const std::exception& er){
00387 edm::LogError("ConditionDBWriter")<<"caught std::exception "<<er.what()<<std::endl;
00388 }catch(...){
00389 edm::LogError("ConditionDBWriter")<<"Funny error"<<std::endl;
00390 }
00391 }
00392 else if (! mydbservice.isAvailable() ) {
00393 edm::LogError("ConditionDBWriter")<<"PoolDBOutputService is unavailable"<<std::endl;
00394 }
00395
00396
00397 }
00398
00399
00400
00401 void setTime(){
00402
00403 edm::Service<cond::service::PoolDBOutputService> mydbservice;
00404
00405 if( mydbservice.isAvailable() ){
00406 Time_ = mydbservice->currentTime();
00407 edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
00408 }
00409 else{
00410 edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time") << std::endl;
00411 }
00412
00413 }
00414
00415
00416 protected:
00417
00418
00419
00420 void storeOnDbNow(){
00421
00422 T * objPointer = 0;
00423
00424 if(AlgoDrivenMode_){
00425
00426 setSinceTime_=true;
00427
00428 objPointer = getNewObject();
00429
00430 if (!objPointer ) {
00431 edm::LogError("ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is algo driven based IOV), but received NULL pointer...will not store anything on the DB") << std::endl;
00432 return;
00433 }
00434 else {storeOnDb(objPointer);}
00435
00436 }
00437 else {
00438
00439 edm::LogError("ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB a new object, but module configuration is not to store on DB on an algo driven based interval...will not store anything on the DB") << std::endl;
00440 return;
00441 }
00442
00443 }
00444
00445
00446
00447
00448 cond::Time_t timeOfLastIOV(){return Time_;}
00449
00450
00451
00452 private:
00453
00454 bool SinceAppendMode_;
00455
00456 bool LumiBlockMode_;
00457 bool RunMode_;
00458 bool JobMode_;
00459 bool AlgoDrivenMode_;
00460 bool doStore_;
00461
00462 std::string Record_;
00463 cond::Time_t Time_;
00464
00465 bool setSinceTime_;
00466
00467
00468 };
00469
00470 #endif