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++) {
179  edm::RefToBase<reco::Muon> mu = muonCollection->refAt(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 
T getUntrackedParameter(std::string const &, T const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
TrackRef track() const override
reference to a Track
Definition: Muon.h:46
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
double eta() const final
momentum pseudorapidity
bool isStandAloneMuon() const override
Definition: Muon.h:300
std::vector< double > relativeUncertaintyOnPt_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
edm::EDGetTokenT< edm::View< reco::PFCandidate > > pfToken_
double pt() const final
transverse momentum
bool ev
key_type key() const
Accessor for product key.
Definition: Ref.h:250
bool isRealData() const
Definition: EventBase.h:62
bool isTrackerMuon() const override
Definition: Muon.h:299
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< reco::GenParticleMatch > genMatchMapToken_
bool isGlobalMuon() const override
Definition: Muon.h:298
std::vector< double > uncertaintyOnOneOverPt_
size_t key() const
Definition: RefToBase.h:219
bool isNull() const
Checks for null.
Definition: Ref.h:235
#define LogTrace(id)
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:421
PFCandidate * clone() const override
return a clone
Definition: PFCandidate.cc:197
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_
std::vector< double > efficiencyRatioOverMC_
void produce(edm::Event &, const edm::EventSetup &) override
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:23
virtual ParticleType particleId() const
Definition: PFCandidate.h:366
std::vector< double > relativeShiftOnPt_
std::vector< double > shiftOnOneOverPt_
double phi() const final
momentum azimuthal angle
void setP4(const LorentzVector &p4) final
set 4-momentum
def move(src, dest)
Definition: eostools.py:511
std::vector< double > etaBinEdges_
double mass() const final
mass
DistortedPFCandProducer(const edm::ParameterSet &)