11 #include "CommonTools/Utils/interface/PtComparator.h"
13 #include "DataFormats/Common/interface/ValueMap.h"
14 #include "DataFormats/Math/interface/LorentzVector.h"
16 #include "FWCore/Framework/interface/Frameworkfwd.h"
17 #include "FWCore/Framework/interface/stream/EDProducer.h"
18 #include "FWCore/Framework/interface/Event.h"
19 #include "FWCore/Framework/interface/ConsumesCollector.h"
20 #include "FWCore/ParameterSet/interface/ParameterSet.h"
21 #include "FWCore/Utilities/interface/InputTag.h"
22 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
23 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
25 #include <cp3_llbb/Framework/interface/Rochester.h>
26 #include <KaMuCa/Calibration/interface/KalmanMuonCalibrator.h>
30 #include <boost/filesystem.hpp>
37 std::string tag = cfg.getParameter<std::string>(
"tag");
38 corrector.reset(
new KalmanMuonCalibrator(tag));
42 T correct(
const edm::Event& event,
const T& muon) {
43 auto corrected_pt = corrector->getCorrectedPt(muon.pt(), muon.eta(), muon.phi(), muon.charge());
44 if (! event.isRealData())
45 corrected_pt = corrector->smear(corrected_pt, muon.eta());
47 double ratio = corrected_pt / muon.pt();
49 T corrected_muon = muon;
50 corrected_muon.setP4(corrected_muon.p4() * ratio);
52 return corrected_muon;
56 std::unique_ptr<KalmanMuonCalibrator> corrector;
62 random_generator(42), random_distribution(0, 1) {
63 auto tag = cfg.getParameter<edm::FileInPath>(
"input");
68 boost::filesystem::path p(tag.fullPath());
69 corrector.reset(
new RoccoR(p.parent_path().native()));
73 T correct(
const edm::Event& event,
const T& muon) {
75 float scale_factor = 1.;
77 if (event.isRealData()) {
78 scale_factor = corrector->kScaleDT(muon.charge(), muon.pt(), muon.eta(), muon.phi(), 0 , 0 );
81 if (!muon.innerTrack().isNull())
82 n_tracks = muon.innerTrack()->hitPattern().trackerLayersWithMeasurement();
84 auto gen_particle = muon.genParticle();
86 scale_factor = corrector->kScaleFromGenMC(muon.charge(), muon.pt(), muon.eta(), muon.phi(), n_tracks, gen_particle->pt(), random_distribution(random_generator), 0 , 0 );
88 scale_factor = corrector->kScaleAndSmearMC(muon.charge(), muon.pt(), muon.eta(), muon.phi(), n_tracks, random_distribution(random_generator), random_distribution(random_generator), 0 , 0 );
91 if (std::isnan(scale_factor)) {
95 T corrected_muon = muon;
96 corrected_muon.setP4(muon.p4() * scale_factor);
98 return corrected_muon;
102 std::unique_ptr<RoccoR> corrector;
103 std::mt19937 random_generator;
104 std::uniform_real_distribution<double> random_distribution;
108 template <
typename T,
class C>
111 using MuonCollection = std::vector<T>;
116 enabled = cfg.getParameter<
bool>(
"enabled");
117 muons_token = consumes<MuonCollection>(cfg.getParameter<edm::InputTag>(
"src"));
120 corrector.reset(
new C(cfg));
123 produces<MuonCollection>();
124 produces<edm::ValueMap<float>>(
"correctionFactors");
127 virtual void produce(edm::Event& event,
const edm::EventSetup& setup)
override {
129 edm::Handle<MuonCollection> muons_collection;
130 event.getByToken(muons_token, muons_collection);
132 const MuonCollection& muons = *muons_collection;
134 std::unique_ptr<MuonCollection> corrected_muons(
new MuonCollection());
135 std::vector<float> correction_factors;
137 for (
const auto& muon: muons) {
138 if ((! enabled) || muon.pt() == 0) {
139 corrected_muons->push_back(muon);
140 correction_factors.push_back(1);
145 T corrected_muon = corrector->template correct<T>(event, muon);
147 double ratio = corrected_muon.pt() / muon.pt();
149 corrected_muons->push_back(corrected_muon);
150 correction_factors.push_back(ratio);
153 std::unique_ptr<edm::ValueMap<float>> correction_factors_map(
new edm::ValueMap<float>());
154 edm::ValueMap<float>::Filler filler(*correction_factors_map);
155 filler.insert(muons_collection, correction_factors.begin(), correction_factors.end());
158 std::sort(corrected_muons->begin(), corrected_muons->end(), muon_pt_comparator);
160 event.put(std::move(corrected_muons));
161 event.put(std::move(correction_factors_map),
"correctionFactors");
165 edm::EDGetTokenT<MuonCollection> muons_token;
168 std::unique_ptr<C> corrector;
170 GreaterByPt<T> muon_pt_comparator;
Definition: CorrectedMuonProducerT.h:109
Definition: Rochester.h:197
Definition: CorrectedMuonProducerT.h:34
Definition: CorrectedMuonProducerT.h:59