Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-12 03:09:35

0001 #ifndef __SCREGRESSIONCALCULATOR_H__
0002 #define __SCREGRESSIONCALCULATOR_H__
0003 
0004 #include "CondFormats/DataRecord/interface/GBRWrapperRcd.h"
0005 #include "CondFormats/GBRForest/interface/GBRForest.h"
0006 #include "FWCore/Framework/interface/ESHandle.h"
0007 
0008 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
0009 #include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
0010 
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 
0015 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
0016 
0017 #include <vector>
0018 #include <memory>
0019 
0020 template <class VarCalc>
0021 class SCRegressionCalculator {
0022 public:
0023   SCRegressionCalculator(const edm::ParameterSet&);
0024   void update(const edm::EventSetup&);
0025 
0026   std::unique_ptr<VarCalc>& varCalc() { return var_calc; }
0027 
0028   float getCorrection(const reco::SuperCluster&) const;
0029   std::pair<float, float> getCorrectionWithErrors(const reco::SuperCluster&) const;
0030 
0031 private:
0032   std::string eb_corr_name, ee_corr_name, eb_err_name, ee_err_name;
0033   const GBRWrapperRcd* gbr_record;
0034   edm::ESHandle<GBRForest> eb_corr, ee_corr, eb_err, ee_err;
0035   std::unique_ptr<VarCalc> var_calc;
0036 };
0037 
0038 template <class VarCalc>
0039 SCRegressionCalculator<VarCalc>::SCRegressionCalculator(const edm::ParameterSet& conf) : gbr_record(nullptr) {
0040   var_calc.reset(new VarCalc());
0041   eb_corr_name = conf.getParameter<std::string>("regressionKeyEB");
0042   ee_corr_name = conf.getParameter<std::string>("regressionKeyEE");
0043   if (conf.existsAs<std::string>("uncertaintyKeyEB"))
0044     eb_err_name = conf.getParameter<std::string>("uncertaintyKeyEB");
0045   if (conf.existsAs<std::string>("uncertaintyKeyEE"))
0046     ee_err_name = conf.getParameter<std::string>("uncertaintyKeyEE");
0047 }
0048 
0049 template <class VarCalc>
0050 void SCRegressionCalculator<VarCalc>::update(const edm::EventSetup& es) {
0051   var_calc->update(es);
0052   const GBRWrapperRcd& gbrfrom_es = es.get<GBRWrapperRcd>();
0053   if (!gbr_record || gbrfrom_es.cacheIdentifier() != gbr_record->cacheIdentifier()) {
0054     gbr_record = &gbrfrom_es;
0055     gbr_record->get(eb_corr_name.c_str(), eb_corr);
0056     gbr_record->get(ee_corr_name.c_str(), ee_corr);
0057     if (!eb_err_name.empty()) {
0058       gbr_record->get(eb_err_name.c_str(), eb_err);
0059     }
0060     if (!ee_err_name.empty()) {
0061       gbr_record->get(ee_err_name.c_str(), ee_err);
0062     }
0063   }
0064 }
0065 
0066 template <class VarCalc>
0067 float SCRegressionCalculator<VarCalc>::getCorrection(const reco::SuperCluster& sc) const {
0068   std::vector<float> inputs;
0069   var_calc->set(sc, inputs);
0070   switch (sc.seed()->seed().subdetId()) {
0071     case EcalSubdetector::EcalBarrel:
0072       return eb_corr->GetResponse(inputs.data());
0073       break;
0074     case EcalSubdetector::EcalEndcap:
0075       return ee_corr->GetResponse(inputs.data());
0076       break;
0077   }
0078   return -1.0f;
0079 }
0080 
0081 template <class VarCalc>
0082 std::pair<float, float> SCRegressionCalculator<VarCalc>::getCorrectionWithErrors(const reco::SuperCluster& sc) const {
0083   std::vector<float> inputs;
0084   var_calc->set(sc, inputs);
0085   switch (sc.seed()->seed().subdetId()) {
0086     case EcalSubdetector::EcalBarrel:
0087       return std::make_pair(eb_corr->GetResponse(inputs.data()), eb_err->GetResponse(inputs.data()));
0088       break;
0089     case EcalSubdetector::EcalEndcap:
0090       return std::make_pair(ee_corr->GetResponse(inputs.data()), ee_err->GetResponse(inputs.data()));
0091       break;
0092   }
0093   return std::make_pair(-1.0f, -1.0f);
0094 }
0095 
0096 #endif