Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-04 00:29:49

0001 // system include files
0002 #include <cstdio>
0003 #include <iostream>
0004 #include <memory>
0005 #include <string>
0006 #include <sys/time.h>
0007 #include <vector>
0008 
0009 // user include files
0010 #include "CondFormats/DataRecord/interface/MuScleFitDBobjectRcd.h"
0011 #include "CondFormats/RecoMuonObjects/interface/MuScleFitDBobject.h"
0012 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0013 #include "FWCore/Framework/interface/ESHandle.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "FWCore/ServiceRegistry/interface/Service.h"
0020 #include "MuonAnalysis/MomentumScaleCalibration/interface/BackgroundFunction.h"
0021 #include "MuonAnalysis/MomentumScaleCalibration/interface/MomentumScaleCorrector.h"
0022 #include "MuonAnalysis/MomentumScaleCalibration/interface/ResolutionFunction.h"
0023 
0024 class DBReader : public edm::one::EDAnalyzer<> {
0025 public:
0026   explicit DBReader(const edm::ParameterSet&);
0027   ~DBReader() override;
0028   void initialize(const edm::EventSetup& iSetup);
0029   void analyze(const edm::Event&, const edm::EventSetup&) override;
0030 
0031 private:
0032   template <typename T>
0033   void printParameters(const T& functionPtr) {
0034     // Looping directly on it does not work, because it is returned by value
0035     // and the iterator gets invalidated on the next line. Save it to a temporary object
0036     // and iterate on it.
0037     std::vector<double> parVecVec(functionPtr->parameters());
0038     std::vector<double>::const_iterator parVec = parVecVec.begin();
0039     std::vector<int> functionId(functionPtr->identifiers());
0040     std::vector<int>::const_iterator id = functionId.begin();
0041     edm::LogPrint("DBReader") << "total number of parameters read from database = parVecVec.size() = "
0042                               << parVecVec.size() << std::endl;
0043     int iFunc = 0;
0044     for (; id != functionId.end(); ++id, ++iFunc) {
0045       int parNum = functionPtr->function(iFunc)->parNum();
0046       edm::LogPrint("DBReader") << "For function id = " << *id << ", with " << parNum << " parameters: " << std::endl;
0047       for (int par = 0; par < parNum; ++par) {
0048         edm::LogPrint("DBReader") << "par[" << par << "] = " << *parVec << std::endl;
0049         ++parVec;
0050       }
0051     }
0052   }
0053 
0054   //  uint32_t printdebug_;
0055   const edm::ESGetToken<MuScleFitDBobject, MuScleFitDBobjectRcd> muToken_;
0056   const std::string type_;
0057   //std::unique_ptr<BaseFunction> corrector_;
0058   std::shared_ptr<MomentumScaleCorrector> corrector_;
0059   std::shared_ptr<ResolutionFunction> resolution_;
0060   std::shared_ptr<BackgroundFunction> background_;
0061 };
0062 
0063 DBReader::DBReader(const edm::ParameterSet& iConfig)
0064     : muToken_(esConsumes()), type_(iConfig.getUntrackedParameter<std::string>("Type")) {}
0065 
0066 void DBReader::initialize(const edm::EventSetup& iSetup) {
0067   const MuScleFitDBobject* dbObject = &iSetup.getData(muToken_);
0068   edm::LogInfo("DBReader") << "[DBReader::analyze] End Reading MuScleFitDBobjectRcd" << std::endl;
0069   edm::LogPrint("DBReader") << "identifiers size from dbObject = " << dbObject->identifiers.size() << std::endl;
0070   edm::LogPrint("DBReader") << "parameters size from dbObject = " << dbObject->parameters.size() << std::endl;
0071 
0072   // This string is one of: scale, resolution, background.
0073   // Create the corrector and set the parameters
0074   if (type_ == "scale")
0075     corrector_ = std::make_shared<MomentumScaleCorrector>(dbObject);
0076   else if (type_ == "resolution")
0077     resolution_ = std::make_shared<ResolutionFunction>(dbObject);
0078   else if (type_ == "background")
0079     background_ = std::make_shared<BackgroundFunction>(dbObject);
0080   else {
0081     edm::LogPrint("DBReader") << "Error: unrecognized type. Use one of those: 'scale', 'resolution', 'background'"
0082                               << std::endl;
0083     exit(1);
0084   }
0085   // cout << "pointer = " << corrector_.get() << endl;
0086 }
0087 
0088 //:  printdebug_(iConfig.getUntrackedParameter<uint32_t>("printDebug",1)){}
0089 
0090 DBReader::~DBReader() = default;
0091 
0092 void DBReader::analyze(const edm::Event& e, const edm::EventSetup& iSetup) {
0093   initialize(iSetup);
0094   if (type_ == "scale")
0095     printParameters(corrector_);
0096   else if (type_ == "resolution")
0097     printParameters(resolution_);
0098   else if (type_ == "background")
0099     printParameters(background_);
0100 }
0101 
0102 #include "FWCore/PluginManager/interface/ModuleDef.h"
0103 #include "FWCore/Framework/interface/MakerMacros.h"
0104 
0105 DEFINE_FWK_MODULE(DBReader);