Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-19 23:36:09

0001 // system include files
0002 #include <memory>
0003 #include <string>
0004 
0005 // user include files
0006 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0007 #include "CondFormats/RecoMuonObjects/interface/MuScleFitDBobject.h"
0008 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ServiceRegistry/interface/Service.h"
0014 #include "MuonAnalysis/MomentumScaleCalibration/interface/BackgroundFunction.h"
0015 #include "MuonAnalysis/MomentumScaleCalibration/interface/MomentumScaleCorrector.h"
0016 #include "MuonAnalysis/MomentumScaleCalibration/interface/ResolutionFunction.h"
0017 
0018 class DBWriter : public edm::one::EDAnalyzer<> {
0019 public:
0020   explicit DBWriter(const edm::ParameterSet&);
0021   ~DBWriter() override = default;
0022 
0023 private:
0024   void analyze(const edm::Event&, const edm::EventSetup&) override;
0025   std::unique_ptr<BaseFunction> corrector_;
0026 };
0027 
0028 DBWriter::DBWriter(const edm::ParameterSet& ps) {
0029   // This string is one of: scale, resolution, background.
0030   std::string type(ps.getUntrackedParameter<std::string>("Type"));
0031   // Create the corrector and set the parameters
0032   if (type == "scale")
0033     corrector_ =
0034         std::make_unique<MomentumScaleCorrector>(ps.getUntrackedParameter<std::string>("CorrectionsIdentifier"));
0035   else if (type == "resolution")
0036     corrector_ = std::make_unique<ResolutionFunction>(ps.getUntrackedParameter<std::string>("CorrectionsIdentifier"));
0037   else if (type == "background")
0038     corrector_ = std::make_unique<BackgroundFunction>(ps.getUntrackedParameter<std::string>("CorrectionsIdentifier"));
0039   else {
0040     edm::LogPrint("DBWriter") << "Error: unrecognized type. Use one of those: 'scale', 'resolution', 'background'"
0041                               << std::endl;
0042     exit(1);
0043   }
0044 }
0045 
0046 // ------------ method called to for each event  ------------
0047 void DBWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0048   MuScleFitDBobject dbObject;
0049 
0050   dbObject.identifiers = corrector_->identifiers();
0051   dbObject.parameters = corrector_->parameters();
0052 
0053   //   if( dbObject->identifiers.size() != dbObject->parameters.size() ) {
0054   //     edm::LogPrint("DBWriter") << "Error: size of parameters("<<dbObject->parameters.size()<<") and identifiers("<<dbObject->identifiers.size()<<") don't match" << std::endl;
0055   //     exit(1);
0056   //   }
0057 
0058   //   std::vector<std::vector<double> >::const_iterator parVec = dbObject->parameters.begin();
0059   //   std::vector<int>::const_iterator id = dbObject->identifiers.begin();
0060   //   for( ; id != dbObject->identifiers.end(); ++id, ++parVec ) {
0061   //     edm::LogPrint("DBWriter") << "id = " << *id << std::endl;
0062   //     std::vector<double>::const_iterator par = parVec->begin();
0063   //     int i=0;
0064   //     for( ; par != parVec->end(); ++par, ++i ) {
0065   //       edm::LogPrint("DBWriter") << "par["<<i<<"] = " << *par << std::endl;
0066   //     }
0067   //   }
0068 
0069   // Save the parameters to the db.
0070   edm::Service<cond::service::PoolDBOutputService> mydbservice;
0071   if (mydbservice.isAvailable()) {
0072     if (mydbservice->isNewTagRequest("MuScleFitDBobjectRcd")) {
0073       mydbservice->createOneIOV<MuScleFitDBobject>(dbObject, mydbservice->beginOfTime(), "MuScleFitDBobjectRcd");
0074     } else {
0075       mydbservice->appendOneIOV<MuScleFitDBobject>(dbObject, mydbservice->currentTime(), "MuScleFitDBobjectRcd");
0076     }
0077   } else {
0078     edm::LogError("DBWriter") << "Service is unavailable" << std::endl;
0079   }
0080 }
0081 
0082 //define this as a plug-in
0083 DEFINE_FWK_MODULE(DBWriter);