CMS 3D CMS Logo

DistortedPFCandProducer.cc
Go to the documentation of this file.
1 #include <memory>
5 
10 
13 
14 //
15 // class declaration
16 //
18 public:
20  ~DistortedPFCandProducer() override;
21 
22 private:
23  void beginJob() override;
24  void produce(edm::Event&, const edm::EventSetup&) override;
25  void endJob() override;
26 
30  std::vector<double> etaBinEdges_;
31 
32  std::vector<double> shiftOnOneOverPt_; // in [1/GeV]
33  std::vector<double> relativeShiftOnPt_;
34  std::vector<double> uncertaintyOnOneOverPt_; // in [1/GeV]
35  std::vector<double> relativeUncertaintyOnPt_;
36 
37  std::vector<double> efficiencyRatioOverMC_;
38 };
39 
46 
47 #include <CLHEP/Random/RandFlat.h>
48 #include <CLHEP/Random/RandGauss.h>
49 
52  // What is being produced
53  produces<std::vector<reco::PFCandidate> >();
54 
55  // Input products
56  muonToken_ =
57  consumes<edm::View<reco::Muon> >(pset.getUntrackedParameter<edm::InputTag>("MuonTag", edm::InputTag("muons")));
58  genMatchMapToken_ = consumes<reco::GenParticleMatch>(
59  pset.getUntrackedParameter<edm::InputTag>("GenMatchMapTag", edm::InputTag("genMatchMap")));
60  pfToken_ = consumes<edm::View<reco::PFCandidate> >(
61  pset.getUntrackedParameter<edm::InputTag>("PFTag", edm::InputTag("particleFlow")));
62 
63  // Eta edges
64  std::vector<double> defEtaEdges;
65  defEtaEdges.push_back(-999999.);
66  defEtaEdges.push_back(999999.);
67  etaBinEdges_ = pset.getUntrackedParameter<std::vector<double> >("EtaBinEdges", defEtaEdges);
68  unsigned int ninputs_expected = etaBinEdges_.size() - 1;
69 
70  // Distortions in muon momentum
71  std::vector<double> defDistortion;
72  defDistortion.push_back(0.);
73 
75  pset.getUntrackedParameter<std::vector<double> >("ShiftOnOneOverPt", defDistortion); // in [1/GeV]
76  if (shiftOnOneOverPt_.size() == 1 && ninputs_expected > 1) {
77  for (unsigned int i = 1; i < ninputs_expected; i++) {
79  }
80  }
81 
82  relativeShiftOnPt_ = pset.getUntrackedParameter<std::vector<double> >("RelativeShiftOnPt", defDistortion);
83  if (relativeShiftOnPt_.size() == 1 && ninputs_expected > 1) {
84  for (unsigned int i = 1; i < ninputs_expected; i++) {
86  }
87  }
88 
90  pset.getUntrackedParameter<std::vector<double> >("UncertaintyOnOneOverPt", defDistortion); // in [1/GeV]
91  if (uncertaintyOnOneOverPt_.size() == 1 && ninputs_expected > 1) {
92  for (unsigned int i = 1; i < ninputs_expected; i++) {
94  }
95  }
96 
97  relativeUncertaintyOnPt_ = pset.getUntrackedParameter<std::vector<double> >("RelativeUncertaintyOnPt", defDistortion);
98  if (relativeUncertaintyOnPt_.size() == 1 && ninputs_expected > 1) {
99  for (unsigned int i = 1; i < ninputs_expected; i++) {
101  }
102  }
103 
104  // Data/MC efficiency ratios
105  std::vector<double> defEfficiencyRatio;
106  defEfficiencyRatio.push_back(1.);
108  pset.getUntrackedParameter<std::vector<double> >("EfficiencyRatioOverMC", defEfficiencyRatio);
109  if (efficiencyRatioOverMC_.size() == 1 && ninputs_expected > 1) {
110  for (unsigned int i = 1; i < ninputs_expected; i++) {
112  }
113  }
114 
115  // Send a warning if there are inconsistencies in vector sizes !!
116  bool effWrong = efficiencyRatioOverMC_.size() != ninputs_expected;
117  bool momWrong = shiftOnOneOverPt_.size() != ninputs_expected || relativeShiftOnPt_.size() != ninputs_expected ||
118  uncertaintyOnOneOverPt_.size() != ninputs_expected ||
119  relativeUncertaintyOnPt_.size() != ninputs_expected;
120  if (effWrong and momWrong) {
121  edm::LogError("")
122  << "WARNING: DistortedPFCandProducer : Size of some parameters do not match the EtaBinEdges vector!!";
123  }
124 }
125 
128 
131 
134 
137  if (ev.isRealData())
138  return;
139 
140  // Muon collection
142  if (!ev.getByToken(muonToken_, muonCollection)) {
143  edm::LogError("") << ">>> Muon collection does not exist !!!";
144  return;
145  }
146 
148  if (!ev.getByToken(genMatchMapToken_, genMatchMap)) {
149  edm::LogError("") << ">>> Muon-GenParticle match map does not exist !!!";
150  return;
151  }
152 
153  // Get PFCandidate collection
155  if (!ev.getByToken(pfToken_, pfCollection)) {
156  edm::LogError("") << ">>> PFCandidate collection does not exist !!!";
157  return;
158  }
159 
160  unsigned int muonCollectionSize = muonCollection->size();
161  unsigned int pfCollectionSize = pfCollection->size();
162 
163  if (pfCollectionSize < 1)
164  return;
165 
166  // Ask for PfMuon consistency
167  bool pfMuonFound = false;
168 
169  std::unique_ptr<reco::PFCandidateCollection> newmuons(new reco::PFCandidateCollection);
170 
171  // Loop on all PF candidates
172  for (unsigned int j = 0; j < pfCollectionSize; j++) {
173  edm::RefToBase<reco::PFCandidate> pf = pfCollection->refAt(j);
174 
175  // New PF muon
176  double ptmu = pf->pt();
177 
178  for (unsigned int i = 0; i < muonCollectionSize; i++) {
180 
181  // Check the muon is in the PF collection
182  if (pf->particleId() == reco::PFCandidate::mu) {
183  reco::MuonRef muref = pf->muonRef();
184  if (muref.isNonnull()) {
185  if (muref.key() == mu.key()) {
186  if (mu->isStandAloneMuon() && ptmu == muref->standAloneMuon()->pt() &&
187  ((!mu->isGlobalMuon() || (mu->isGlobalMuon() && ptmu != muref->combinedMuon()->pt())) &&
188  (!mu->isTrackerMuon() || (mu->isTrackerMuon() && ptmu != mu->track()->pt())))) {
189  pfMuonFound = false;
190  } else if (!mu->isTrackerMuon()) {
191  pfMuonFound = false;
192  } else {
193  pfMuonFound = true;
194  }
195  } else {
196  pfMuonFound = false;
197  }
198  }
199  }
200 
201  // do nothing if StandAlone muon
202  //const reco::Track& track = *trackRef;
203 
204  if (!pfMuonFound)
205  continue;
206 
207  double ptgen = pf->pt();
208  double etagen = pf->eta();
209 
210  reco::GenParticleRef gen = (*genMatchMap)[mu];
211  if (!gen.isNull()) {
212  ptgen = gen->pt();
213  etagen = gen->eta();
214  LogTrace("") << ">>> Muon-GenParticle match found; ptmu= " << pf->pt() << ", ptgen= " << ptgen;
215  } else {
216  LogTrace("") << ">>> MUON-GENPARTICLE MATCH NOT FOUND!!!";
217  }
218 
219  // Initialize parameters
220  double effRatio = 0.;
221  double shift1 = 0.;
222  double shift2 = 0.;
223  double sigma1 = 0.;
224  double sigma2 = 0.;
225 
226  // Find out which eta bin should be used
227  unsigned int nbins = etaBinEdges_.size() - 1;
228  unsigned int etaBin = nbins;
229  if (etagen > etaBinEdges_[0] && etagen < etaBinEdges_[nbins]) {
230  for (unsigned int j = 1; j <= nbins; ++j) {
231  if (etagen > etaBinEdges_[j])
232  continue;
233  etaBin = j - 1;
234  break;
235  }
236  }
237  if (etaBin < nbins) {
238  LogTrace("") << ">>> etaBin: " << etaBin << ", for etagen =" << etagen;
239  } else {
240  // Muon is rejected if outside the considered eta range
241  LogTrace("") << ">>> Muon outside eta range: reject it; etagen = " << etagen;
242  pfMuonFound = false;
243  continue;
244  }
245 
246  if (!pfMuonFound)
247  continue;
248 
249  // Set shifts
250  shift1 = shiftOnOneOverPt_[etaBin];
251  shift2 = relativeShiftOnPt_[etaBin];
252  LogTrace("") << "\tshiftOnOneOverPt= " << shift1 * 100 << " [%]";
253  LogTrace("") << "\trelativeShiftOnPt= " << shift2 * 100 << " [%]";
254 
255  // Set resolutions
258  LogTrace("") << "\tuncertaintyOnOneOverPt= " << sigma1 << " [1/GeV]";
259  LogTrace("") << "\trelativeUncertaintyOnPt= " << sigma2 * 100 << " [%]";
260 
261  // Set efficiency ratio
262  effRatio = efficiencyRatioOverMC_[etaBin];
263  LogTrace("") << "\tefficiencyRatioOverMC= " << effRatio;
264 
265  // Reject muons according to efficiency ratio
266  double rndf = CLHEP::RandFlat::shoot();
267  if (rndf > effRatio)
268  continue;
269 
270  // Gaussian Random numbers for smearing
271  double rndg1 = CLHEP::RandGauss::shoot();
272  double rndg2 = CLHEP::RandGauss::shoot();
273 
274  // change here the pt of the candidate, if it is a muon
275 
276  ptmu += ptgen * (shift1 * ptgen + shift2 + sigma1 * rndg1 * ptgen + sigma2 * rndg2);
277  pfMuonFound = false;
278  }
279 
280  reco::PFCandidate* newmu = pf->clone();
281  newmu->setP4(reco::Particle::PolarLorentzVector(ptmu, pf->eta(), pf->phi(), pf->mass()));
282 
283  newmuons->push_back(*newmu);
284  }
285 
286  ev.put(std::move(newmuons));
287 }
288 
DistortedPFCandProducer::DistortedPFCandProducer
DistortedPFCandProducer(const edm::ParameterSet &)
Definition: DistortedPFCandProducer.cc:51
MuonAlignmentFromReference_cff.newmuons
newmuons
Definition: MuonAlignmentFromReference_cff.py:26
Handle.h
mps_fire.i
i
Definition: mps_fire.py:428
Muon.h
MessageLogger.h
DistortedPFCandProducer::~DistortedPFCandProducer
~DistortedPFCandProducer() override
Definition: DistortedPFCandProducer.cc:127
EDProducer.h
etaBin
int etaBin(const l1t::HGCalMulticluster *cl)
Definition: L1EGammaEEProducer.cc:19
PFCandidate.h
amptDefaultParameters_cff.mu
mu
Definition: amptDefaultParameters_cff.py:16
edm::EDGetTokenT
Definition: EDGetToken.h:33
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89287
DistortedPFCandProducer::pfToken_
edm::EDGetTokenT< edm::View< reco::PFCandidate > > pfToken_
Definition: DistortedPFCandProducer.cc:29
edm::Handle
Definition: AssociativeIterator.h:50
edm::Ref< MuonCollection >
DistortedPFCandProducer
Definition: DistortedPFCandProducer.cc:17
GenParticle.h
MakerMacros.h
Track.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
reco::PFCandidate::mu
Definition: PFCandidate.h:48
MuonFwd.h
ResolutionFunction.h
LaserClient_cfi.nbins
nbins
Definition: LaserClient_cfi.py:51
DistortedPFCandProducer::endJob
void endJob() override
Definition: DistortedPFCandProducer.cc:133
reco::Particle::PolarLorentzVector
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:23
gen
Definition: PythiaDecays.h:13
DistortedPFCandProducer::uncertaintyOnOneOverPt_
std::vector< double > uncertaintyOnOneOverPt_
Definition: DistortedPFCandProducer.cc:34
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
DistortedPFCandProducer::genMatchMapToken_
edm::EDGetTokenT< reco::GenParticleMatch > genMatchMapToken_
Definition: DistortedPFCandProducer.cc:28
edm::Ref::isNonnull
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
edm::EventSetup
Definition: EventSetup.h:57
DistortedPFCandProducer::efficiencyRatioOverMC_
std::vector< double > efficiencyRatioOverMC_
Definition: DistortedPFCandProducer.cc:37
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
pdwgLeptonRecoSkim_cfi.muonCollection
muonCollection
Definition: pdwgLeptonRecoSkim_cfi.py:7
DistortedPFCandProducer::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: DistortedPFCandProducer.cc:136
reco::LeafCandidate::setP4
void setP4(const LorentzVector &p4) final
set 4-momentum
Definition: LeafCandidate.h:158
eostools.move
def move(src, dest)
Definition: eostools.py:511
DistortedPFCandProducer::shiftOnOneOverPt_
std::vector< double > shiftOnOneOverPt_
Definition: DistortedPFCandProducer.cc:32
Frameworkfwd.h
packedPFCandidateRefMixer_cfi.pf
pf
Definition: packedPFCandidateRefMixer_cfi.py:4
ev
bool ev
Definition: Hydjet2Hadronizer.cc:95
DistortedPFCandProducer::muonToken_
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_
Definition: DistortedPFCandProducer.cc:27
reco::PFCandidateCollection
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
Definition: PFCandidateFwd.h:12
DistortedPFCandProducer::etaBinEdges_
std::vector< double > etaBinEdges_
Definition: DistortedPFCandProducer.cc:30
edm::EDProducer
Definition: EDProducer.h:35
edm::RefToBase
Definition: AssociativeIterator.h:54
DistortedPFCandProducer::relativeShiftOnPt_
std::vector< double > relativeShiftOnPt_
Definition: DistortedPFCandProducer.cc:33
reco::PFCandidate
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
edm::Ref::key
key_type key() const
Accessor for product key.
Definition: Ref.h:250
LogTrace
#define LogTrace(id)
Definition: MessageLogger.h:224
View.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::Event
Definition: Event.h:73
MomentumScaleCorrector.h
edm::InputTag
Definition: InputTag.h:15
DistortedPFCandProducer::relativeUncertaintyOnPt_
std::vector< double > relativeUncertaintyOnPt_
Definition: DistortedPFCandProducer.cc:35
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
PFCandidateFwd.h
DistortedPFCandProducer::beginJob
void beginJob() override
Definition: DistortedPFCandProducer.cc:130