Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:54

0001 /**
0002   \class    pat::PATMETSlimmer PATMETSlimmer.h "PhysicsTools/PatAlgos/interface/PATMETSlimmer.h"
0003   \brief    Slimmer of PAT METs 
0004 */
0005 
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/Framework/interface/global/EDProducer.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 
0011 #include "DataFormats/Common/interface/View.h"
0012 #include "FWCore/Framework/interface/ConsumesCollector.h"
0013 
0014 #include "DataFormats/PatCandidates/interface/MET.h"
0015 
0016 namespace pat {
0017 
0018   class PATMETSlimmer : public edm::global::EDProducer<> {
0019   public:
0020     explicit PATMETSlimmer(const edm::ParameterSet &iConfig);
0021     ~PATMETSlimmer() override {}
0022 
0023     void produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override;
0024 
0025   private:
0026     class OneMETShift {
0027     public:
0028       OneMETShift()
0029           : shift(pat::MET::NoShift),
0030             level(pat::MET::None),
0031             t0FromMiniAOD(false),
0032             corShift(false),
0033             uncShift(false),
0034             isSmeared(false) {}
0035       OneMETShift(pat::MET::METUncertainty shift_,
0036                   pat::MET::METCorrectionType level_,
0037                   const edm::InputTag &baseTag,
0038                   edm::ConsumesCollector &&cc,
0039                   bool t0FromMiniAOD_,
0040                   bool corShift_,
0041                   bool uncShift_,
0042                   bool isSmeared_ = false);
0043       void readAndSet(const edm::Event &ev, pat::MET &met) const;
0044 
0045     private:
0046       const pat::MET::METUncertainty shift;
0047       const pat::MET::METCorrectionType level;
0048       edm::EDGetTokenT<pat::METCollection> token;
0049       const bool t0FromMiniAOD;
0050       const bool corShift;
0051       const bool uncShift;
0052       const bool isSmeared;
0053     };
0054     void maybeReadShifts(const edm::ParameterSet &basePSet,
0055                          const std::string &name,
0056                          pat::MET::METCorrectionType level,
0057                          bool readFromMiniAOD = false);
0058 
0059     const edm::EDGetTokenT<pat::METCollection> src_;
0060     std::vector<OneMETShift> shifts_;
0061 
0062     const bool onMiniAOD_;
0063   };
0064 
0065 }  // namespace pat
0066 
0067 pat::PATMETSlimmer::PATMETSlimmer(const edm::ParameterSet &iConfig)
0068     : src_(consumes<pat::METCollection>(iConfig.getParameter<edm::InputTag>("src"))),
0069       onMiniAOD_(iConfig.existsAs<bool>("runningOnMiniAOD") ? iConfig.getParameter<bool>("runningOnMiniAOD") : false) {
0070   maybeReadShifts(iConfig, "rawVariation", pat::MET::None);
0071   maybeReadShifts(iConfig, "t1Uncertainties", pat::MET::T1);
0072   maybeReadShifts(iConfig, "t01Variation", pat::MET::T0, onMiniAOD_);
0073   maybeReadShifts(iConfig, "t1SmearedVarsAndUncs", pat::MET::Smear);
0074 
0075   maybeReadShifts(iConfig, "tXYUncForRaw", pat::MET::TXYForRaw);
0076   maybeReadShifts(iConfig, "tXYUncForT1", pat::MET::TXY);
0077   maybeReadShifts(iConfig, "tXYUncForT01", pat::MET::TXYForT01);
0078   maybeReadShifts(iConfig, "tXYUncForT1Smear", pat::MET::TXYForT1Smear);
0079   maybeReadShifts(iConfig, "tXYUncForT01Smear", pat::MET::TXYForT01Smear);
0080   maybeReadShifts(iConfig, "caloMET", pat::MET::Calo);
0081   maybeReadShifts(iConfig, "chsMET", pat::MET::Chs);
0082   maybeReadShifts(iConfig, "trkMET", pat::MET::Trk);
0083   if (iConfig.getParameter<bool>("addDeepMETs")) {
0084     maybeReadShifts(iConfig, "deepMETResolutionTune", pat::MET::DeepResolutionTune);
0085     maybeReadShifts(iConfig, "deepMETResponseTune", pat::MET::DeepResponseTune);
0086   }
0087 
0088   produces<std::vector<pat::MET>>();
0089 }
0090 
0091 void pat::PATMETSlimmer::maybeReadShifts(const edm::ParameterSet &basePSet,
0092                                          const std::string &name,
0093                                          pat::MET::METCorrectionType level,
0094                                          bool readFromMiniAOD) {
0095   if (basePSet.existsAs<edm::ParameterSet>(name)) {
0096     throw cms::Exception("Unsupported", "Reading PSets not supported, for now just use input tag");
0097   } else if (basePSet.existsAs<edm::InputTag>(name)) {
0098     const edm::InputTag &baseTag = basePSet.getParameter<edm::InputTag>(name);
0099 
0100     if (level == pat::MET::T1) {
0101       shifts_.push_back(
0102           OneMETShift(pat::MET::NoShift, level, baseTag, consumesCollector(), readFromMiniAOD, true, false, false));
0103       shifts_.push_back(
0104           OneMETShift(pat::MET::NoShift, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0105       shifts_.push_back(
0106           OneMETShift(pat::MET::JetResUp, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0107       shifts_.push_back(
0108           OneMETShift(pat::MET::JetResDown, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0109       shifts_.push_back(
0110           OneMETShift(pat::MET::JetEnUp, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0111       shifts_.push_back(
0112           OneMETShift(pat::MET::JetEnDown, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0113       shifts_.push_back(
0114           OneMETShift(pat::MET::MuonEnUp, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0115       shifts_.push_back(
0116           OneMETShift(pat::MET::MuonEnDown, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0117       shifts_.push_back(
0118           OneMETShift(pat::MET::ElectronEnUp, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0119       shifts_.push_back(
0120           OneMETShift(pat::MET::ElectronEnDown, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0121       shifts_.push_back(
0122           OneMETShift(pat::MET::PhotonEnUp, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0123       shifts_.push_back(
0124           OneMETShift(pat::MET::PhotonEnDown, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0125       shifts_.push_back(
0126           OneMETShift(pat::MET::TauEnUp, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0127       shifts_.push_back(
0128           OneMETShift(pat::MET::TauEnDown, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0129       shifts_.push_back(
0130           OneMETShift(pat::MET::UnclusteredEnUp, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0131       shifts_.push_back(
0132           OneMETShift(pat::MET::UnclusteredEnDown, level, baseTag, consumesCollector(), readFromMiniAOD, false, true));
0133     } else if (level == pat::MET::Smear) {
0134       shifts_.push_back(
0135           OneMETShift(pat::MET::NoShift, level, baseTag, consumesCollector(), readFromMiniAOD, true, false, true));
0136       shifts_.push_back(
0137           OneMETShift(pat::MET::JetResUp, level, baseTag, consumesCollector(), readFromMiniAOD, false, true, true));
0138       shifts_.push_back(
0139           OneMETShift(pat::MET::JetResDown, level, baseTag, consumesCollector(), readFromMiniAOD, false, true, true));
0140     } else {
0141       shifts_.push_back(
0142           OneMETShift(pat::MET::NoShift, level, baseTag, consumesCollector(), readFromMiniAOD, true, false));
0143     }
0144   }
0145 }
0146 
0147 pat::PATMETSlimmer::OneMETShift::OneMETShift(pat::MET::METUncertainty shift_,
0148                                              pat::MET::METCorrectionType level_,
0149                                              const edm::InputTag &baseTag,
0150                                              edm::ConsumesCollector &&cc,
0151                                              bool t0FromMiniAOD_,
0152                                              bool corShift_,
0153                                              bool uncShift_,
0154                                              bool isSmeared)
0155     : shift(shift_),
0156       level(level_),
0157       t0FromMiniAOD(t0FromMiniAOD_),
0158       corShift(corShift_),
0159       uncShift(uncShift_),
0160       isSmeared(isSmeared) {
0161   std::string baseTagStr = baseTag.encode();
0162   char buff[1024];
0163   switch (shift) {
0164     case pat::MET::NoShift:
0165       snprintf(buff, 1023, baseTagStr.c_str(), "");
0166       break;
0167     case pat::MET::JetEnUp:
0168       snprintf(buff, 1023, baseTagStr.c_str(), "JetEnUp");
0169       break;
0170     case pat::MET::JetEnDown:
0171       snprintf(buff, 1023, baseTagStr.c_str(), "JetEnDown");
0172       break;
0173     case pat::MET::JetResUp:
0174       snprintf(buff, 1023, baseTagStr.c_str(), "JetResUp");
0175       break;
0176     case pat::MET::JetResDown:
0177       snprintf(buff, 1023, baseTagStr.c_str(), "JetResDown");
0178       break;
0179     case pat::MET::MuonEnUp:
0180       snprintf(buff, 1023, baseTagStr.c_str(), "MuonEnUp");
0181       break;
0182     case pat::MET::MuonEnDown:
0183       snprintf(buff, 1023, baseTagStr.c_str(), "MuonEnDown");
0184       break;
0185     case pat::MET::ElectronEnUp:
0186       snprintf(buff, 1023, baseTagStr.c_str(), "ElectronEnUp");
0187       break;
0188     case pat::MET::ElectronEnDown:
0189       snprintf(buff, 1023, baseTagStr.c_str(), "ElectronEnDown");
0190       break;
0191     case pat::MET::PhotonEnUp:
0192       snprintf(buff, 1023, baseTagStr.c_str(), "PhotonEnUp");
0193       break;
0194     case pat::MET::PhotonEnDown:
0195       snprintf(buff, 1023, baseTagStr.c_str(), "PhotonEnDown");
0196       break;
0197     case pat::MET::TauEnUp:
0198       snprintf(buff, 1023, baseTagStr.c_str(), "TauEnUp");
0199       break;
0200     case pat::MET::TauEnDown:
0201       snprintf(buff, 1023, baseTagStr.c_str(), "TauEnDown");
0202       break;
0203     case pat::MET::UnclusteredEnUp:
0204       snprintf(buff, 1023, baseTagStr.c_str(), "UnclusteredEnUp");
0205       break;
0206     case pat::MET::UnclusteredEnDown:
0207       snprintf(buff, 1023, baseTagStr.c_str(), "UnclusteredEnDown");
0208       break;
0209     default:
0210       throw cms::Exception("LogicError", "OneMETShift constructor called with bogus shift");
0211   }
0212   token = cc.consumes<pat::METCollection>(edm::InputTag(buff));
0213 }
0214 
0215 void pat::PATMETSlimmer::produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const {
0216   using namespace edm;
0217   using namespace std;
0218 
0219   Handle<pat::METCollection> src;
0220   iEvent.getByToken(src_, src);
0221   if (src->size() != 1)
0222     throw cms::Exception("CorruptData", "More than one MET in the collection");
0223 
0224   auto out = std::make_unique<std::vector<pat::MET>>(1, src->front());
0225   pat::MET &met = out->back();
0226 
0227   for (const OneMETShift &shift : shifts_) {
0228     shift.readAndSet(iEvent, met);
0229   }
0230 
0231   iEvent.put(std::move(out));
0232 }
0233 
0234 void
0235 
0236 pat::PATMETSlimmer::OneMETShift::readAndSet(const edm::Event &ev, pat::MET &met) const {
0237   edm::Handle<pat::METCollection> src;
0238   ev.getByToken(token, src);
0239 
0240   if (src->size() != 1)
0241     throw cms::Exception("CorruptData", "More than one MET in the shifted collection");
0242   const pat::MET &met2 = src->front();
0243 
0244   if (t0FromMiniAOD) {
0245     if (uncShift)
0246       met.setUncShift(met2.shiftedPx(shift, pat::MET::Type01),
0247                       met2.shiftedPy(shift, pat::MET::Type01),
0248                       met2.shiftedSumEt(shift, pat::MET::Type01),
0249                       shift,
0250                       isSmeared);
0251     if (corShift)
0252       met.setCorShift(
0253           met2.corPx(pat::MET::Type01), met2.corPy(pat::MET::Type01), met2.corSumEt(pat::MET::Type01), level);
0254   } else {
0255     if (uncShift)
0256       met.setUncShift(met2.px(), met2.py(), met2.sumEt(), shift, isSmeared);
0257     if (corShift)
0258       met.setCorShift(met2.px(), met2.py(), met2.sumEt(), level);
0259   }
0260 }
0261 
0262 #include "FWCore/Framework/interface/MakerMacros.h"
0263 using namespace pat;
0264 DEFINE_FWK_MODULE(PATMETSlimmer);