CMS 3D CMS Logo

DistortedMuonProducer.cc
Go to the documentation of this file.
1 #include <memory>
6 
10 
13 
14 //
15 // class declaration
16 //
18 public:
20  ~DistortedMuonProducer() override;
21 
22 private:
23  void beginJob() override;
24  void produce(edm::Event&, const edm::EventSetup&) override;
25  void endJob() override;
26 
29  std::vector<double> etaBinEdges_;
30 
31  std::vector<double> shiftOnOneOverPt_; // in [1/GeV]
32  std::vector<double> relativeShiftOnPt_;
33  std::vector<double> uncertaintyOnOneOverPt_; // in [1/GeV]
34  std::vector<double> relativeUncertaintyOnPt_;
35 
36  std::vector<double> efficiencyRatioOverMC_;
37 };
38 
43 
44 #include <CLHEP/Random/RandFlat.h>
45 #include <CLHEP/Random/RandGauss.h>
46 
49  // What is being produced
50  produces<std::vector<reco::Muon> >();
51 
52  // Input products
53  muonToken_ =
54  consumes<edm::View<reco::Muon> >(pset.getUntrackedParameter<edm::InputTag>("MuonTag", edm::InputTag("muons")));
55  genMatchMapToken_ = consumes<reco::GenParticleMatch>(
56  pset.getUntrackedParameter<edm::InputTag>("GenMatchMapTag", edm::InputTag("genMatchMap")));
57 
58  // Eta edges
59  std::vector<double> defEtaEdges;
60  defEtaEdges.push_back(-999999.);
61  defEtaEdges.push_back(999999.);
62  etaBinEdges_ = pset.getUntrackedParameter<std::vector<double> >("EtaBinEdges", defEtaEdges);
63  unsigned int ninputs_expected = etaBinEdges_.size() - 1;
64 
65  // Distortions in muon momentum
66  std::vector<double> defDistortion;
67  defDistortion.push_back(0.);
68 
70  pset.getUntrackedParameter<std::vector<double> >("ShiftOnOneOverPt", defDistortion); // in [1/GeV]
71  if (shiftOnOneOverPt_.size() == 1 && ninputs_expected > 1) {
72  for (unsigned int i = 1; i < ninputs_expected; i++) {
74  }
75  }
76 
77  relativeShiftOnPt_ = pset.getUntrackedParameter<std::vector<double> >("RelativeShiftOnPt", defDistortion);
78  if (relativeShiftOnPt_.size() == 1 && ninputs_expected > 1) {
79  for (unsigned int i = 1; i < ninputs_expected; i++) {
81  }
82  }
83 
85  pset.getUntrackedParameter<std::vector<double> >("UncertaintyOnOneOverPt", defDistortion); // in [1/GeV]
86  if (uncertaintyOnOneOverPt_.size() == 1 && ninputs_expected > 1) {
87  for (unsigned int i = 1; i < ninputs_expected; i++) {
89  }
90  }
91 
92  relativeUncertaintyOnPt_ = pset.getUntrackedParameter<std::vector<double> >("RelativeUncertaintyOnPt", defDistortion);
93  if (relativeUncertaintyOnPt_.size() == 1 && ninputs_expected > 1) {
94  for (unsigned int i = 1; i < ninputs_expected; i++) {
96  }
97  }
98 
99  // Data/MC efficiency ratios
100  std::vector<double> defEfficiencyRatio;
101  defEfficiencyRatio.push_back(1.);
103  pset.getUntrackedParameter<std::vector<double> >("EfficiencyRatioOverMC", defEfficiencyRatio);
104  if (efficiencyRatioOverMC_.size() == 1 && ninputs_expected > 1) {
105  for (unsigned int i = 1; i < ninputs_expected; i++) {
107  }
108  }
109 
110  // Send a warning if there are inconsistencies in vector sizes !!
111  bool effWrong = efficiencyRatioOverMC_.size() != ninputs_expected;
112  bool momWrong = shiftOnOneOverPt_.size() != ninputs_expected || relativeShiftOnPt_.size() != ninputs_expected ||
113  uncertaintyOnOneOverPt_.size() != ninputs_expected ||
114  relativeUncertaintyOnPt_.size() != ninputs_expected;
115  if (effWrong and momWrong) {
116  edm::LogError("")
117  << "WARNING: DistortedMuonProducer : Size of some parameters do not match the EtaBinEdges vector!!";
118  }
119 }
120 
123 
126 
129 
132  if (ev.isRealData())
133  return;
134 
135  // Muon collection
137  if (!ev.getByToken(muonToken_, muonCollection)) {
138  edm::LogError("") << ">>> Muon collection does not exist !!!";
139  return;
140  }
141 
143  if (!ev.getByToken(genMatchMapToken_, genMatchMap)) {
144  edm::LogError("") << ">>> Muon-GenParticle match map does not exist !!!";
145  return;
146  }
147 
148  unsigned int muonCollectionSize = muonCollection->size();
149 
150  std::unique_ptr<reco::MuonCollection> newmuons(new reco::MuonCollection);
151 
152  for (unsigned int i = 0; i < muonCollectionSize; i++) {
154 
155  double ptgen = mu->pt();
156  double etagen = mu->eta();
157  reco::GenParticleRef gen = (*genMatchMap)[mu];
158  if (!gen.isNull()) {
159  ptgen = gen->pt();
160  etagen = gen->eta();
161  LogTrace("") << ">>> Muon-GenParticle match found; ptmu= " << mu->pt() << ", ptgen= " << ptgen;
162  } else {
163  LogTrace("") << ">>> MUON-GENPARTICLE MATCH NOT FOUND!!!";
164  }
165 
166  // Initialize parameters
167  double effRatio = 0.;
168  double shift1 = 0.;
169  double shift2 = 0.;
170  double sigma1 = 0.;
171  double sigma2 = 0.;
172 
173  // Find out which eta bin should be used
174  unsigned int nbins = etaBinEdges_.size() - 1;
175  unsigned int etaBin = nbins;
176  if (etagen > etaBinEdges_[0] && etagen < etaBinEdges_[nbins]) {
177  for (unsigned int j = 1; j <= nbins; ++j) {
178  if (etagen > etaBinEdges_[j])
179  continue;
180  etaBin = j - 1;
181  break;
182  }
183  }
184  if (etaBin < nbins) {
185  LogTrace("") << ">>> etaBin: " << etaBin << ", for etagen =" << etagen;
186  } else {
187  // Muon is rejected if outside the considered eta range
188  LogTrace("") << ">>> Muon outside eta range: reject it; etagen = " << etagen;
189  continue;
190  }
191 
192  // Set shifts
193  shift1 = shiftOnOneOverPt_[etaBin];
194  shift2 = relativeShiftOnPt_[etaBin];
195  LogTrace("") << "\tshiftOnOneOverPt= " << shift1 * 100 << " [%]";
196  LogTrace("") << "\trelativeShiftOnPt= " << shift2 * 100 << " [%]";
197 
198  // Set resolutions
201  LogTrace("") << "\tuncertaintyOnOneOverPt= " << sigma1 << " [1/GeV]";
202  LogTrace("") << "\trelativeUncertaintyOnPt= " << sigma2 * 100 << " [%]";
203 
204  // Set efficiency ratio
205  effRatio = efficiencyRatioOverMC_[etaBin];
206  LogTrace("") << "\tefficiencyRatioOverMC= " << effRatio;
207 
208  // Reject muons according to efficiency ratio
209  double rndf = CLHEP::RandFlat::shoot();
210  if (rndf > effRatio)
211  continue;
212 
213  // Gaussian Random numbers for smearing
214  double rndg1 = CLHEP::RandGauss::shoot();
215  double rndg2 = CLHEP::RandGauss::shoot();
216 
217  // New muon
218  double ptmu = mu->pt();
219  ptmu += ptgen * (shift1 * ptgen + shift2 + sigma1 * rndg1 * ptgen + sigma2 * rndg2);
220  reco::Muon* newmu = mu->clone();
221  newmu->setP4(reco::Particle::PolarLorentzVector(ptmu, mu->eta(), mu->phi(), mu->mass()));
222  newmuons->push_back(*newmu);
223  }
224 
225  ev.put(std::move(newmuons));
226 }
227 
MuonAlignmentFromReference_cff.newmuons
newmuons
Definition: MuonAlignmentFromReference_cff.py:26
Handle.h
mps_fire.i
i
Definition: mps_fire.py:428
Muon.h
MessageLogger.h
EDProducer.h
etaBin
int etaBin(const l1t::HGCalMulticluster *cl)
Definition: L1EGammaEEProducer.cc:19
amptDefaultParameters_cff.mu
mu
Definition: amptDefaultParameters_cff.py:16
edm::EDGetTokenT
Definition: EDGetToken.h:33
DistortedMuonProducer::shiftOnOneOverPt_
std::vector< double > shiftOnOneOverPt_
Definition: DistortedMuonProducer.cc:31
DistortedMuonProducer::beginJob
void beginJob() override
Definition: DistortedMuonProducer.cc:125
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89287
DistortedMuonProducer::efficiencyRatioOverMC_
std::vector< double > efficiencyRatioOverMC_
Definition: DistortedMuonProducer.cc:36
DistortedMuonProducer::relativeShiftOnPt_
std::vector< double > relativeShiftOnPt_
Definition: DistortedMuonProducer.cc:32
edm::Handle
Definition: AssociativeIterator.h:50
DistortedMuonProducer::~DistortedMuonProducer
~DistortedMuonProducer() override
Definition: DistortedMuonProducer.cc:122
reco::Muon
Definition: Muon.h:27
edm::Ref< GenParticleCollection >
GenParticle.h
MakerMacros.h
Track.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
MuonFwd.h
reco::MuonCollection
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
ResolutionFunction.h
LaserClient_cfi.nbins
nbins
Definition: LaserClient_cfi.py:51
reco::Particle::PolarLorentzVector
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:23
gen
Definition: PythiaDecays.h:13
DistortedMuonProducer::etaBinEdges_
std::vector< double > etaBinEdges_
Definition: DistortedMuonProducer.cc:29
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
DistortedMuonProducer::muonToken_
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_
Definition: DistortedMuonProducer.cc:27
DistortedMuonProducer
Definition: DistortedMuonProducer.cc:17
edm::EventSetup
Definition: EventSetup.h:57
DistortedMuonProducer::uncertaintyOnOneOverPt_
std::vector< double > uncertaintyOnOneOverPt_
Definition: DistortedMuonProducer.cc:33
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
pdwgLeptonRecoSkim_cfi.muonCollection
muonCollection
Definition: pdwgLeptonRecoSkim_cfi.py:7
eostools.move
def move(src, dest)
Definition: eostools.py:511
DistortedMuonProducer::DistortedMuonProducer
DistortedMuonProducer(const edm::ParameterSet &)
Definition: DistortedMuonProducer.cc:48
DistortedMuonProducer::genMatchMapToken_
edm::EDGetTokenT< reco::GenParticleMatch > genMatchMapToken_
Definition: DistortedMuonProducer.cc:28
Frameworkfwd.h
ev
bool ev
Definition: Hydjet2Hadronizer.cc:95
edm::EDProducer
Definition: EDProducer.h:35
edm::RefToBase
Definition: AssociativeIterator.h:54
DistortedMuonProducer::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: DistortedMuonProducer.cc:131
LogTrace
#define LogTrace(id)
Definition: MessageLogger.h:224
View.h
DistortedMuonProducer::relativeUncertaintyOnPt_
std::vector< double > relativeUncertaintyOnPt_
Definition: DistortedMuonProducer.cc:34
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::Event
Definition: Event.h:73
MomentumScaleCorrector.h
edm::InputTag
Definition: InputTag.h:15
DistortedMuonProducer::endJob
void endJob() override
Definition: DistortedMuonProducer.cc:128
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27