Go to the documentation of this file.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 #include <memory>
00127 #include <string>
00128 #include <cstdlib>
00129
00130
00131
00132 #include "FWCore/Framework/interface/Frameworkfwd.h"
00133 #include "FWCore/Framework/interface/EDAnalyzer.h"
00134 #include "FWCore/Framework/interface/Run.h"
00135 #include "CondCore/DBCommon/interface/Time.h"
00136 #include "FWCore/Framework/interface/ESHandle.h"
00137 #include "FWCore/ServiceRegistry/interface/Service.h"
00138 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00139
00140 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00141 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00142
00143 #include "FWCore/Utilities/interface/Exception.h"
00144
00145
00146 #include "FWCore/Framework/interface/Event.h"
00147 #include "DataFormats/Common/interface/Handle.h"
00148
00149
00150 template< class T >
00151 class ConditionDBWriter : public edm::EDAnalyzer {
00152
00153 public:
00154
00155 explicit ConditionDBWriter(const edm::ParameterSet& iConfig) : LumiBlockMode_(false), RunMode_(false), JobMode_(false), AlgoDrivenMode_(false), Time_(0), setSinceTime_(false), firstRun_(true)
00156 {
00157 edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
00158 SinceAppendMode_=iConfig.getParameter<bool>("SinceAppendMode");
00159 std::string IOVMode=iConfig.getParameter<std::string>("IOVMode");
00160 if (IOVMode==std::string("Job")) JobMode_=true;
00161 else if (IOVMode==std::string("Run")) RunMode_=true;
00162 else if (IOVMode==std::string("LumiBlock")) LumiBlockMode_=true;
00163 else if (IOVMode==std::string("AlgoDriven")) AlgoDrivenMode_=true;
00164 else edm::LogError("ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything on the DB") << std::endl;
00165 Record_=iConfig.getParameter<std::string>("Record");
00166 doStore_=iConfig.getParameter<bool>("doStoreOnDB");
00167
00168 if(! SinceAppendMode_ )
00169 edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
00170 }
00171
00172 virtual ~ConditionDBWriter()
00173 {
00174 edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl;
00175 }
00176
00177 private:
00178
00179
00180
00181 virtual T * getNewObject()=0;
00182
00183
00184
00185
00186
00187 virtual void algoBeginJob(const edm::EventSetup&){};
00188
00189 virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
00190
00191 virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00192
00193 virtual void algoAnalyze(const edm::Event&, const edm::EventSetup&){};
00194
00195 virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
00196
00197 virtual void algoEndJob(){};
00198
00199 void beginJob() {}
00200
00201 void beginRun(const edm::Run & run, const edm::EventSetup & es)
00202 {
00203 if( firstRun_ ) {
00204 edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
00205 if( (JobMode_ || AlgoDrivenMode_) && SinceAppendMode_) setSinceTime_=true;
00206 algoBeginJob(es);
00207 firstRun_ = false;
00208 }
00209 edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
00210 if(RunMode_ && SinceAppendMode_) setSinceTime_=true;
00211 algoBeginRun(run,es);
00212 }
00213
00214 void beginLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & iSetup)
00215 {
00216 edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
00217 if(LumiBlockMode_ && SinceAppendMode_) setSinceTime_=true;
00218 algoBeginLuminosityBlock(lumiBlock, iSetup);
00219 }
00220
00221 void analyze(const edm::Event& event, const edm::EventSetup& iSetup)
00222 {
00223 if(setSinceTime_ ){
00224 setTime();
00225 setSinceTime_=false;
00226 }
00227 algoAnalyze(event, iSetup);
00228 }
00229
00230 void endLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & es)
00231 {
00232 edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
00233 algoEndLuminosityBlock(lumiBlock, es);
00234
00235 if(LumiBlockMode_){
00236
00237 T * objPointer = getNewObject();
00238
00239 if(objPointer ){
00240 storeOnDb(objPointer);
00241 }
00242 else {
00243 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;
00244 }
00245 }
00246 }
00247
00248 virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00249
00250 void endRun(const edm::Run & run, const edm::EventSetup & es)
00251 {
00252 edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
00253
00254 algoEndRun(run, es);
00255
00256 if(RunMode_){
00257
00258 T * objPointer = getNewObject();
00259
00260 if(objPointer ){
00261 storeOnDb(objPointer);
00262 }
00263 else {
00264 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;
00265 }
00266 }
00267 }
00268
00269 void endJob()
00270 {
00271 edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
00272
00273 algoEndJob();
00274
00275 if(JobMode_){
00276
00277 T * objPointer = getNewObject();
00278
00279 if( objPointer ){
00280 storeOnDb(objPointer);
00281 }
00282
00283 else {
00284
00285 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;
00286 }
00287 }
00288 }
00289
00290 void storeOnDb(T * objPointer)
00291 {
00292 edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
00293
00294 setSinceTime_=true;
00295
00296 if(! objPointer) {
00297 edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB") ;
00298 return;
00299 }
00300
00301
00302 if( !doStore_ ) return;
00303 edm::Service<cond::service::PoolDBOutputService> mydbservice;
00304 if (! mydbservice.isAvailable() ) {
00305 edm::LogError("ConditionDBWriter")<<"PoolDBOutputService is unavailable"<<std::endl;
00306 return;
00307 }
00308
00309 cond::Time_t since =
00310 ( mydbservice->isNewTagRequest(Record_) ) ? mydbservice->beginOfTime() : Time_;
00311
00312 edm::LogInfo("ConditionDBWriter") << "appending a new object to tag "
00313 <<Record_ <<" in since mode " << std::endl;
00314 mydbservice->writeOne<T>(objPointer, since, Record_);
00315 }
00316
00317 void setTime()
00318 {
00319 edm::Service<cond::service::PoolDBOutputService> mydbservice;
00320
00321 if( mydbservice.isAvailable() ){
00322 Time_ = mydbservice->currentTime();
00323 edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
00324 }
00325 else{
00326 edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time") << std::endl;
00327 }
00328 }
00329
00330 protected:
00331
00332
00333
00334 void storeOnDbNow()
00335 {
00336 T * objPointer = 0;
00337
00338 if(AlgoDrivenMode_){
00339
00340 setSinceTime_=true;
00341
00342 objPointer = getNewObject();
00343
00344 if (!objPointer ) {
00345 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;
00346 return;
00347 }
00348 else {storeOnDb(objPointer);}
00349
00350 }
00351 else {
00352
00353 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;
00354 return;
00355 }
00356 }
00357
00358
00359
00360 cond::Time_t timeOfLastIOV(){return Time_;}
00361
00362 private:
00363
00364 bool SinceAppendMode_;
00365
00366 bool LumiBlockMode_;
00367 bool RunMode_;
00368 bool JobMode_;
00369 bool AlgoDrivenMode_;
00370 bool doStore_;
00371
00372 std::string Record_;
00373 cond::Time_t Time_;
00374
00375 bool setSinceTime_;
00376
00377 bool firstRun_;
00378 };
00379
00380 #endif