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