File indexing completed on 2023-03-17 11:16:26
0001 #include "FWCore/Framework/interface/MakerMacros.h"
0002
0003 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Utilities/interface/InputTag.h"
0007
0008 #include "DataFormats/PatCandidates/interface/EventHypothesis.h"
0009 #include "DataFormats/PatCandidates/interface/EventHypothesisLooper.h"
0010 #include "DataFormats/Common/interface/ValueMap.h"
0011 #include "DataFormats/Math/interface/deltaR.h"
0012
0013 #include "DataFormats/JetReco/interface/CaloJet.h"
0014 #include "DataFormats/MuonReco/interface/Muon.h"
0015
0016 class TestEventHypothesisReader : public edm::stream::EDAnalyzer<> {
0017 public:
0018 TestEventHypothesisReader(const edm::ParameterSet &iConfig);
0019 void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override;
0020 void runTests(const pat::EventHypothesis &h);
0021
0022 private:
0023 edm::EDGetTokenT<std::vector<pat::EventHypothesis> > hypsToken_;
0024 edm::EDGetTokenT<edm::ValueMap<double> > deltaRsHToken_;
0025 };
0026
0027 TestEventHypothesisReader::TestEventHypothesisReader(const edm::ParameterSet &iConfig)
0028 : hypsToken_(consumes<std::vector<pat::EventHypothesis> >(iConfig.getParameter<edm::InputTag>("events"))),
0029 deltaRsHToken_(consumes<edm::ValueMap<double> >(
0030 edm::InputTag((iConfig.getParameter<edm::InputTag>("events")).label(), "deltaR"))) {}
0031
0032 void TestEventHypothesisReader::runTests(const pat::EventHypothesis &h) {
0033 using namespace std;
0034 using namespace pat::eventhypothesis;
0035 cout << "Test 1: Print the muon " << h["mu"]->pt() << endl;
0036
0037 for (size_t i = 0; i < h.count() - 2; ++i) {
0038 cout << "Test 2." << (i + 1) << ": Getting of the other jets: " << h.get("other jet", i)->et() << endl;
0039 }
0040
0041 cout << "Test 3: count: " << (h.count() - 2) << " vs " << h.count("other jet") << endl;
0042
0043 cout << "Test 4: regexp count: " << (h.count() - 1) << " vs " << h.count(".*jet") << endl;
0044
0045 cout << "Test 5.0: all with muon: " << h.all("mu").size() << endl;
0046 cout << "Test 5.1: all with muon: " << h.all("mu").front()->pt() << endl;
0047 cout << "Test 5.2: all with other jets: " << h.all("other jet").size() << endl;
0048 cout << "Test 5.3: all with regex: " << h.all(".*jet").size() << endl;
0049
0050 cout << "Test 6.0: get as : " << h.getAs<reco::CaloJet>("nearest jet")->maxEInHadTowers() << endl;
0051
0052 cout << "Loopers" << endl;
0053 cout << "Test 7.0: simple looper on all" << endl;
0054 for (CandLooper jet = h.loop(); jet; ++jet) {
0055 cout << "\titem " << jet.index() << ", role " << jet.role() << ": " << jet->et() << endl;
0056 }
0057 cout << "Test 7.1: simple looper on jets" << endl;
0058 for (CandLooper jet = h.loop(".*jet"); jet; ++jet) {
0059 cout << "\titem " << jet.index() << ", role " << jet.role() << ": " << jet->et() << endl;
0060 }
0061 cout << "Test 7.2: loopAs on jets" << endl;
0062 for (Looper<reco::CaloJet> jet = h.loopAs<reco::CaloJet>(".*jet"); jet; ++jet) {
0063 cout << "\titem " << jet.index() << ", role " << jet.role() << ": " << jet->maxEInHadTowers() << endl;
0064 }
0065 }
0066
0067 void TestEventHypothesisReader::analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) {
0068 using namespace edm;
0069 using namespace std;
0070 using reco::Candidate;
0071 using reco::CandidatePtr;
0072
0073 Handle<vector<pat::EventHypothesis> > hyps;
0074 iEvent.getByToken(hypsToken_, hyps);
0075
0076 Handle<ValueMap<double> > deltaRsH;
0077 iEvent.getByToken(deltaRsHToken_, deltaRsH);
0078 const ValueMap<double> &deltaRs = *deltaRsH;
0079
0080 for (size_t i = 0, n = hyps->size(); i < n; ++i) {
0081 const pat::EventHypothesis &h = (*hyps)[i];
0082
0083 std::cout << "Hypothesis " << (i + 1) << ": " << std::endl;
0084 CandidatePtr mu = h["mu"];
0085 std::cout << " muon : pt = " << mu->pt() << ", eta = " << mu->eta() << ", phi = " << mu->phi() << std::endl;
0086 CandidatePtr jet = h["nearest jet"];
0087 std::cout << " n jet: pt = " << jet->pt() << ", eta = " << jet->eta() << ", phi = " << jet->phi() << std::endl;
0088
0089 for (pat::EventHypothesis::CandLooper j2 = h.loop("other jet"); j2; ++j2) {
0090 std::cout << " 0 jet: pt = " << j2->pt() << ", eta = " << j2->eta() << ", phi = " << j2->phi() << std::endl;
0091 }
0092
0093 Ref<vector<pat::EventHypothesis> > key(hyps, i);
0094 std::cout << " deltaR: " << deltaRs[key] << "\n" << std::endl;
0095
0096 runTests(h);
0097 }
0098
0099 std::cout << "Found " << hyps->size() << " possible options"
0100 << "\n\n"
0101 << std::endl;
0102 }
0103
0104 DEFINE_FWK_MODULE(TestEventHypothesisReader);