File indexing completed on 2024-11-28 03:54:26
0001 #ifndef TOPDQMHELPERS
0002 #define TOPDQMHELPERS
0003
0004 #include <string>
0005 #include <vector>
0006 #include <iostream>
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Common/interface/TriggerNames.h"
0009 #include "DataFormats/Common/interface/TriggerResults.h"
0010 #include "DataFormats/VertexReco/interface/Vertex.h"
0011 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0012 #include <DataFormats/METReco/interface/PFMET.h>
0013 #include "FWCore/Framework/interface/ConsumesCollector.h"
0014 #include "FWCore/Framework/interface/EDConsumerBase.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016
0017 #include "DataFormats/PatCandidates/interface/Muon.h"
0018 #include "DataFormats/PatCandidates/interface/Electron.h"
0019 #include "DataFormats/PatCandidates/interface/Jet.h"
0020 #include "DataFormats/PatCandidates/interface/MET.h"
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 inline bool accept(const edm::Event& event, const edm::TriggerResults& triggerTable, const std::string& triggerPath) {
0032 bool passed = false;
0033 const edm::TriggerNames& triggerNames = event.triggerNames(triggerTable);
0034 for (unsigned int i = 0; i < triggerNames.triggerNames().size(); ++i) {
0035 if (triggerNames.triggerNames()[i] == triggerPath) {
0036 if (triggerTable.accept(i)) {
0037 passed = true;
0038 break;
0039 }
0040 }
0041 }
0042 return passed;
0043 }
0044
0045 inline bool accept(const edm::Event& event,
0046 const edm::TriggerResults& triggerTable,
0047 const std::vector<std::string>& triggerPaths) {
0048 bool passed = false;
0049 for (unsigned int j = 0; j < triggerPaths.size(); ++j) {
0050 if (accept(event, triggerTable, triggerPaths[j])) {
0051 passed = true;
0052 break;
0053 }
0054 }
0055 return passed;
0056 }
0057
0058 #include "DataFormats/JetReco/interface/Jet.h"
0059 #include "FWCore/Framework/interface/EventSetup.h"
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 class Calculate_miniAOD {
0072 public:
0073
0074 Calculate_miniAOD(int maxNJets, double wMass);
0075
0076 ~Calculate_miniAOD() {}
0077
0078
0079 double massWBoson(const std::vector<pat::Jet>& jets);
0080
0081 double massTopQuark(const std::vector<pat::Jet>& jets);
0082
0083
0084
0085 double massBTopQuark(const std::vector<pat::Jet>& jets, std::vector<double> VbtagWP, double btagWP_);
0086
0087
0088 double tmassWBoson(pat::Muon* lep, const pat::MET& met, const pat::Jet& b);
0089
0090 double tmassTopQuark(pat::Muon* lep, const pat::MET& met, const pat::Jet& b);
0091
0092
0093 double tmassWBoson(pat::Electron* lep, const pat::MET& met, const pat::Jet& b);
0094
0095 double tmassTopQuark(pat::Electron* lep, const pat::MET& met, const pat::Jet& b);
0096
0097 private:
0098
0099
0100
0101 void operator()(const std::vector<pat::Jet>& jets);
0102
0103 void operator2(const std::vector<pat::Jet>&, std::vector<double>, double);
0104
0105 void operator()(const pat::Jet& bJet, pat::Electron* lepton, const pat::MET& met);
0106
0107 void operator()(const pat::Jet& bJet, pat::Muon* lepton, const pat::MET& met);
0108
0109 private:
0110
0111 bool failed_;
0112
0113 int maxNJets_;
0114
0115 double wMass_;
0116
0117 double massWBoson_;
0118
0119 double massTopQuark_;
0120
0121 double massBTopQuark_;
0122
0123 double tmassWBoson_;
0124
0125 double tmassTopQuark_;
0126 };
0127
0128 class Calculate {
0129 public:
0130
0131 Calculate(int maxNJets, double wMass);
0132
0133 ~Calculate() {}
0134
0135
0136 double massWBoson(const std::vector<reco::Jet>& jets);
0137
0138 double massTopQuark(const std::vector<reco::Jet>& jets);
0139
0140
0141
0142 double massBTopQuark(const std::vector<reco::Jet>& jets, std::vector<double> VbtagWP, double btagWP_);
0143
0144
0145 double tmassWBoson(reco::RecoCandidate* lep, const reco::MET& met, const reco::Jet& b);
0146
0147 double tmassTopQuark(reco::RecoCandidate* lep, const reco::MET& met, const reco::Jet& b);
0148
0149 private:
0150
0151
0152
0153 void operator()(const std::vector<reco::Jet>& jets);
0154
0155 void operator2(const std::vector<reco::Jet>&, std::vector<double>, double);
0156
0157 void operator()(const reco::Jet& bJet, reco::RecoCandidate* lepton, const reco::MET& met);
0158
0159 private:
0160
0161 bool failed_;
0162
0163 int maxNJets_;
0164
0165 double wMass_;
0166
0167 double massWBoson_;
0168
0169 double massTopQuark_;
0170
0171 double massBTopQuark_;
0172
0173 double tmassWBoson_;
0174
0175 double tmassTopQuark_;
0176 };
0177
0178 #include "DataFormats/JetReco/interface/JetID.h"
0179 #include "DataFormats/JetReco/interface/PFJet.h"
0180 #include "DataFormats/JetReco/interface/CaloJet.h"
0181 #include "DataFormats/BTauReco/interface/JetTag.h"
0182 #include "DataFormats/Common/interface/Handle.h"
0183 #include "DataFormats/Common/interface/ValueMap.h"
0184 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0185 #include "JetMETCorrections/JetCorrector/interface/JetCorrector.h"
0186 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0187 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0188 #include "FWCore/Utilities/interface/EDGetToken.h"
0189 #include "FWCore/Utilities/interface/InputTag.h"
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239 template <typename Object>
0240 class SelectionStep {
0241 public:
0242
0243 SelectionStep(const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC);
0244
0245 ~SelectionStep() {}
0246
0247
0248 bool select(const edm::Event& event);
0249 bool select(const edm::Event& event, const std::string& type);
0250
0251 bool select(const edm::Event& event, const edm::EventSetup& setup);
0252 bool selectVertex(const edm::Event& event);
0253
0254 private:
0255
0256 edm::EDGetTokenT<edm::View<Object> > src_;
0257
0258 int min_, max_;
0259
0260 edm::EDGetTokenT<edm::ValueMap<float> > electronId_;
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272 double eidCutValue_;
0273
0274 edm::EDGetTokenT<reco::JetCorrector> jetCorrector_;
0275
0276 edm::EDGetTokenT<reco::JetTagCollection> btagLabel_;
0277
0278 double btagWorkingPoint_;
0279
0280 edm::EDGetTokenT<reco::JetIDValueMap> jetIDLabel_;
0281
0282 edm::EDGetTokenT<edm::View<reco::Vertex> > pvs_;
0283 edm::EDGetTokenT<edm::View<reco::GsfElectron> > gsfEs_;
0284
0285 StringCutObjectSelector<Object> select_;
0286
0287 StringCutObjectSelector<reco::JetID>* jetIDSelect_;
0288 };
0289
0290
0291 template <typename Object>
0292 SelectionStep<Object>::SelectionStep(const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC)
0293 : select_(cfg.getParameter<std::string>("select")), jetIDSelect_(nullptr) {
0294 src_ = iC.consumes<edm::View<Object> >(cfg.getParameter<edm::InputTag>("src"));
0295
0296 cfg.exists("min") ? min_ = cfg.getParameter<int>("min") : min_ = -1;
0297 cfg.exists("max") ? max_ = cfg.getParameter<int>("max") : max_ = -1;
0298 std::string mygSF = "gedGsfElectrons";
0299 gsfEs_ = iC.consumes<edm::View<reco::GsfElectron> >(cfg.getUntrackedParameter<edm::InputTag>("myGSF", mygSF));
0300 if (cfg.existsAs<edm::ParameterSet>("electronId")) {
0301 edm::ParameterSet elecId = cfg.getParameter<edm::ParameterSet>("electronId");
0302 electronId_ = iC.consumes<edm::ValueMap<float> >(elecId.getParameter<edm::InputTag>("src"));
0303 eidCutValue_ = elecId.getParameter<double>("cutValue");
0304 }
0305 if (cfg.exists("jetCorrector")) {
0306 jetCorrector_ = iC.consumes<reco::JetCorrector>(edm::InputTag(cfg.getParameter<std::string>("jetCorrector")));
0307 }
0308 if (cfg.existsAs<edm::ParameterSet>("jetBTagger")) {
0309 edm::ParameterSet jetBTagger = cfg.getParameter<edm::ParameterSet>("jetBTagger");
0310 btagLabel_ = iC.consumes<reco::JetTagCollection>(jetBTagger.getParameter<edm::InputTag>("label"));
0311 btagWorkingPoint_ = jetBTagger.getParameter<double>("workingPoint");
0312 }
0313 if (cfg.existsAs<edm::ParameterSet>("jetID")) {
0314 edm::ParameterSet jetID = cfg.getParameter<edm::ParameterSet>("jetID");
0315 jetIDLabel_ = iC.consumes<reco::JetIDValueMap>(jetID.getParameter<edm::InputTag>("label"));
0316 jetIDSelect_ = new StringCutObjectSelector<reco::JetID>(jetID.getParameter<std::string>("select"));
0317 }
0318 }
0319
0320
0321 template <typename Object>
0322 bool SelectionStep<Object>::select(const edm::Event& event) {
0323
0324 edm::Handle<edm::View<Object> > src;
0325 if (!event.getByToken(src_, src))
0326 return false;
0327
0328
0329 edm::Handle<edm::ValueMap<float> > electronId;
0330 if (!electronId_.isUninitialized()) {
0331 if (!event.getByToken(electronId_, electronId))
0332 return false;
0333 }
0334
0335
0336 int n = 0;
0337 for (typename edm::View<Object>::const_iterator obj = src->begin(); obj != src->end(); ++obj) {
0338
0339 if (dynamic_cast<const reco::GsfElectron*>(&*obj)) {
0340 unsigned int idx = obj - src->begin();
0341 if (electronId_.isUninitialized() ? true : ((double)(*electronId)[src->refAt(idx)] >= eidCutValue_)) {
0342 if (select_(*obj))
0343 ++n;
0344 }
0345 }
0346
0347 else {
0348 if (select_(*obj))
0349 ++n;
0350 }
0351 }
0352 bool accept = (min_ >= 0 ? n >= min_ : true) && (max_ >= 0 ? n <= max_ : true);
0353 return (min_ < 0 && max_ < 0) ? (n > 0) : accept;
0354 }
0355
0356
0357 template <typename Object>
0358 bool SelectionStep<Object>::select(const edm::Event& event, const std::string& type) {
0359
0360 edm::Handle<edm::View<Object> > src;
0361 if (!event.getByToken(src_, src))
0362 return false;
0363
0364
0365 edm::Handle<edm::View<reco::GsfElectron> > elecs_gsf;
0366
0367
0368 edm::Handle<edm::ValueMap<float> > electronId;
0369 if (!electronId_.isUninitialized()) {
0370 if (!event.getByToken(electronId_, electronId))
0371 return false;
0372 }
0373
0374
0375 int n = 0;
0376 for (typename edm::View<Object>::const_iterator obj = src->begin(); obj != src->end(); ++obj) {
0377
0378 if (dynamic_cast<const reco::PFCandidate*>(&*obj)) {
0379 reco::PFCandidate objtmp = dynamic_cast<const reco::PFCandidate&>(*obj);
0380
0381 if (type == "muon") {
0382 if (select_(*obj)) {
0383 ++n;
0384 }
0385 } else if (type == "electron") {
0386 if (select_(*obj)) {
0387 ++n;
0388 }
0389
0390 }
0391 }
0392
0393
0394 else if (dynamic_cast<const reco::GsfElectron*>(&*obj)) {
0395 unsigned int idx = obj - src->begin();
0396 if (electronId_.isUninitialized() ? true : ((double)(*electronId)[src->refAt(idx)] >= eidCutValue_)) {
0397 if (select_(*obj))
0398 ++n;
0399 }
0400 }
0401
0402
0403 else {
0404 if (select_(*obj))
0405 ++n;
0406 }
0407 }
0408 bool accept = (min_ >= 0 ? n >= min_ : true) && (max_ >= 0 ? n <= max_ : true);
0409 return (min_ < 0 && max_ < 0) ? (n > 0) : accept;
0410 }
0411
0412 template <typename Object>
0413 bool SelectionStep<Object>::selectVertex(const edm::Event& event) {
0414
0415 edm::Handle<edm::View<Object> > src;
0416 if (!event.getByToken(src_, src))
0417 return false;
0418
0419
0420 edm::Handle<edm::ValueMap<float> > electronId;
0421 if (!electronId_.isUninitialized()) {
0422 if (!event.getByToken(electronId_, electronId))
0423 return false;
0424 }
0425
0426
0427 int n = 0;
0428 for (typename edm::View<Object>::const_iterator obj = src->begin(); obj != src->end(); ++obj) {
0429 if (select_(*obj))
0430 ++n;
0431 }
0432 bool accept = (min_ >= 0 ? n >= min_ : true) && (max_ >= 0 ? n <= max_ : true);
0433 return (min_ < 0 && max_ < 0) ? (n > 0) : accept;
0434 }
0435
0436
0437 template <typename Object>
0438 bool SelectionStep<Object>::select(const edm::Event& event, const edm::EventSetup& setup) {
0439
0440 edm::Handle<edm::View<Object> > src;
0441 if (!event.getByToken(src_, src))
0442 return false;
0443
0444
0445
0446
0447
0448 edm::Handle<edm::View<reco::Jet> > bjets;
0449 edm::Handle<reco::JetTagCollection> btagger;
0450 edm::Handle<edm::View<reco::Vertex> > pvertex;
0451 if (!btagLabel_.isUninitialized()) {
0452 if (!event.getByToken(src_, bjets))
0453 return false;
0454 if (!event.getByToken(btagLabel_, btagger))
0455 return false;
0456 if (!event.getByToken(pvs_, pvertex))
0457 return false;
0458 }
0459
0460
0461 edm::Handle<reco::JetIDValueMap> jetID;
0462 if (jetIDSelect_) {
0463 if (!event.getByToken(jetIDLabel_, jetID))
0464 return false;
0465 }
0466
0467
0468 const reco::JetCorrector* corrector = nullptr;
0469 if (!jetCorrector_.isUninitialized()) {
0470
0471 edm::Handle<reco::JetCorrector> correctorHandle = event.getHandle(jetCorrector_);
0472 if (correctorHandle.isValid()) {
0473 corrector = correctorHandle.product();
0474 } else {
0475 edm::LogVerbatim("TopDQMHelpers") << "\n"
0476 << "---------------------------------------------------------------\n"
0477 << " No reco::JetCorrrector available from Event:\n"
0478 << " - Jets will not be corrected.\n"
0479 << "---------------------------------------------------------------"
0480 "\n";
0481 }
0482 }
0483
0484 int n = 0;
0485 for (typename edm::View<Object>::const_iterator obj = src->begin(); obj != src->end(); ++obj) {
0486
0487
0488 unsigned int idx = obj - src->begin();
0489 if (btagLabel_.isUninitialized() ? true : (*btagger)[bjets->refAt(idx)] > btagWorkingPoint_) {
0490 bool passedJetID = true;
0491
0492 if (jetIDSelect_ && dynamic_cast<const reco::CaloJet*>(src->refAt(idx).get())) {
0493 passedJetID = (*jetIDSelect_)((*jetID)[src->refAt(idx)]);
0494 }
0495 if (passedJetID) {
0496
0497 Object jet = *obj;
0498 jet.scaleEnergy(corrector ? corrector->correction(*obj) : 1.);
0499 if (select_(jet))
0500 ++n;
0501 }
0502 }
0503 }
0504 bool accept = (min_ >= 0 ? n >= min_ : true) && (max_ >= 0 ? n <= max_ : true);
0505 return (min_ < 0 && max_ < 0) ? (n > 0) : accept;
0506 }
0507
0508 #endif
0509
0510
0511
0512
0513