File indexing completed on 2024-04-06 12:19:25
0001 #ifndef JetMETCorrections_Type1MET_METCorrectionAlgorithm_h
0002 #define JetMETCorrections_Type1MET_METCorrectionAlgorithm_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/EventSetup.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/Utilities/interface/InputTag.h"
0024 #include "FWCore/Framework/interface/ConsumesCollector.h"
0025
0026 #include "DataFormats/METReco/interface/CorrMETData.h"
0027
0028 #include <TFormula.h>
0029
0030 #include <vector>
0031
0032 class METCorrectionAlgorithm {
0033 public:
0034 explicit METCorrectionAlgorithm(const edm::ParameterSet&, edm::ConsumesCollector&& iConsumesCollector);
0035 ~METCorrectionAlgorithm();
0036
0037 CorrMETData compMETCorrection(edm::Event&);
0038
0039 private:
0040 typedef std::vector<edm::InputTag> vInputTag;
0041
0042 std::vector<edm::EDGetTokenT<CorrMETData> > chsSumTokens_;
0043 std::vector<edm::EDGetTokenT<CorrMETData> > type1Tokens_;
0044
0045 bool applyType0Corrections_;
0046 bool applyType1Corrections_;
0047 bool applyType2Corrections_;
0048
0049 double type0Rsoft_;
0050 double type0Cuncl_;
0051
0052 typedef std::vector<std::string> vstring;
0053 struct type2BinningEntryType {
0054 type2BinningEntryType(const std::string& binCorrformula,
0055 const edm::ParameterSet& binCorrParameter,
0056 const vInputTag& srcUnclEnergySums,
0057 edm::ConsumesCollector& iConsumesCollector)
0058 : binLabel_(""), binCorrFormula_(nullptr) {
0059 for (vInputTag::const_iterator inputTag = srcUnclEnergySums.begin(); inputTag != srcUnclEnergySums.end();
0060 ++inputTag) {
0061 corrTokens_.push_back(iConsumesCollector.consumes<CorrMETData>(*inputTag));
0062 }
0063
0064 initialize(binCorrformula, binCorrParameter);
0065 }
0066 type2BinningEntryType(const edm::ParameterSet& cfg,
0067 const vInputTag& srcUnclEnergySums,
0068 edm::ConsumesCollector& iConsumesCollector)
0069 : binLabel_(cfg.getParameter<std::string>("binLabel")), binCorrFormula_(nullptr) {
0070 for (vInputTag::const_iterator srcUnclEnergySum = srcUnclEnergySums.begin();
0071 srcUnclEnergySum != srcUnclEnergySums.end();
0072 ++srcUnclEnergySum) {
0073 std::string instanceLabel = srcUnclEnergySum->instance();
0074 if (!instanceLabel.empty() && !binLabel_.empty())
0075 instanceLabel.append("#");
0076 instanceLabel.append(binLabel_);
0077 edm::InputTag inputTag(srcUnclEnergySum->label(), instanceLabel);
0078 corrTokens_.push_back(iConsumesCollector.consumes<CorrMETData>(inputTag));
0079 }
0080
0081 std::string binCorrFormula = cfg.getParameter<std::string>("binCorrFormula");
0082
0083 edm::ParameterSet binCorrParameter = cfg.getParameter<edm::ParameterSet>("binCorrParameter");
0084
0085 initialize(binCorrFormula, binCorrParameter);
0086 }
0087 void initialize(const std::string& binCorrFormula, const edm::ParameterSet& binCorrParameter) {
0088 TString formula = binCorrFormula;
0089
0090 vstring parNames = binCorrParameter.getParameterNamesForType<double>();
0091 int numParameter = parNames.size();
0092 binCorrParameter_.resize(numParameter);
0093 for (int parIndex = 0; parIndex < numParameter; ++parIndex) {
0094 const std::string& parName = parNames[parIndex];
0095
0096 double parValue = binCorrParameter.getParameter<double>(parName);
0097 binCorrParameter_[parIndex] = parValue;
0098
0099 TString parName_internal = Form("[%i]", parIndex);
0100 formula = formula.ReplaceAll(parName.data(), parName_internal);
0101 }
0102
0103 std::string binCorrFormulaName = std::string("binCorrFormula").append("_").append(binLabel_);
0104 binCorrFormula_ = new TFormula(binCorrFormulaName.data(), formula);
0105
0106
0107
0108 if (!(binCorrFormula_->GetNdim() <= 1 && binCorrFormula_->GetNpar() == numParameter))
0109 throw cms::Exception("METCorrectionAlgorithm")
0110 << "Formula for Type 2 correction has invalid syntax = " << formula << " !!\n";
0111
0112 for (int parIndex = 0; parIndex < numParameter; ++parIndex) {
0113 binCorrFormula_->SetParameter(parIndex, binCorrParameter_[parIndex]);
0114 binCorrFormula_->SetParName(parIndex, parNames[parIndex].data());
0115 }
0116 }
0117 ~type2BinningEntryType() { delete binCorrFormula_; }
0118 std::string binLabel_;
0119 std::vector<edm::EDGetTokenT<CorrMETData> > corrTokens_;
0120 TFormula* binCorrFormula_;
0121 std::vector<double> binCorrParameter_;
0122 };
0123 std::vector<type2BinningEntryType*> type2Binning_;
0124 };
0125
0126 #endif