CMS 3D CMS Logo

DistortedMuonProducerFromDB.cc
Go to the documentation of this file.
1 #include <memory>
6 
9 
12 
13 //
14 // class declaration
15 //
17 public:
20 
21 private:
22  void beginRun(const edm::Run&, const edm::EventSetup&) override;
23  void produce(edm::Event&, const edm::EventSetup&) override;
24  void endJob() override;
25 
27 
31 
32  std::unique_ptr<MomentumScaleCorrector> momCorrector_;
33  std::unique_ptr<ResolutionFunction> momResolutionData_;
34  std::unique_ptr<ResolutionFunction> momResolutionMC_;
35 };
36 
40 
41 #include <CLHEP/Random/RandGauss.h>
42 
49 
52  // What is being produced
53  produces<std::vector<reco::Muon> >();
54 
55  // Input products
56  muonToken_ =
57  consumes<edm::View<reco::Muon> >(pset.getUntrackedParameter<edm::InputTag>("MuonTag", edm::InputTag("muons")));
58  dbScaleLabel_ = pset.getUntrackedParameter<std::string>("DBScaleLabel", "scale");
59  dbDataResolutionLabel_ = pset.getUntrackedParameter<std::string>("DBDataResolutionLabel", "datareso");
60  dbMCResolutionLabel_ = pset.getUntrackedParameter<std::string>("DBMCResolutionLabel", "mcreso");
61 }
62 
65 
69  iSetup.get<MuScleFitDBobjectRcd>().get(dbScaleLabel_, dbObject1);
70  momCorrector_.reset(new MomentumScaleCorrector(dbObject1.product()));
71 
72  LogTrace("") << ">>> Using database for momentum scale corrections !!";
73 
75  iSetup.get<MuScleFitDBobjectRcd>().get(dbDataResolutionLabel_, dbObject2);
76  momResolutionData_.reset(new ResolutionFunction(dbObject2.product()));
77 
79  iSetup.get<MuScleFitDBobjectRcd>().get(dbMCResolutionLabel_, dbObject3);
80  momResolutionMC_.reset(new ResolutionFunction(dbObject3.product()));
81 
82  LogTrace("") << ">>> Using database for momentum resolution corrections !!";
83 }
84 
87 
90  if (ev.isRealData())
91  return;
92 
93  // Muon collection
95  if (!ev.getByToken(muonToken_, muonCollection)) {
96  edm::LogError("") << ">>> Muon collection does not exist !!!";
97  return;
98  }
99  unsigned int muonCollectionSize = muonCollection->size();
100 
101  std::unique_ptr<reco::MuonCollection> newmuons(new reco::MuonCollection);
102 
103  for (unsigned int i = 0; i < muonCollectionSize; i++) {
104  edm::RefToBase<reco::Muon> mu = muonCollection->refAt(i);
105 
106  // Set shift
107  double shift = (*momCorrector_)(*mu) - mu->pt();
108  LogTrace("") << "\tmomentumScaleShift= " << shift << " [GeV]";
109 
110  // Set resolutions
111  double sigma = pow(momResolutionData_->sigmaPt(*mu), 2) - pow(momResolutionMC_->sigmaPt(*mu), 2);
112  if (sigma > 0.)
113  sigma = sqrt(sigma);
114  else
115  sigma = 0.;
116  LogTrace("") << "\tPt additional smearing= " << sigma << " [GeV]";
117 
118  // Gaussian Random number for smearing
119  double rndg = CLHEP::RandGauss::shoot();
120 
121  // New muon
122  double ptmu = mu->pt();
123  ptmu += shift + sigma * rndg;
124  reco::Muon* newmu = mu->clone();
125  newmu->setP4(reco::Particle::PolarLorentzVector(ptmu, mu->eta(), mu->phi(), mu->mass()));
126  newmuons->push_back(*newmu);
127  }
128 
129  ev.put(std::move(newmuons));
130 }
131 
std::unique_ptr< ResolutionFunction > momResolutionData_
void produce(edm::Event &, const edm::EventSetup &) override
T getUntrackedParameter(std::string const &, T const &) const
std::unique_ptr< ResolutionFunction > momResolutionMC_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
double eta() const final
momentum pseudorapidity
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
double pt() const final
transverse momentum
bool ev
bool isRealData() const
Definition: EventBase.h:62
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
T sqrt(T t)
Definition: SSEVec.h:19
void beginRun(const edm::Run &, const edm::EventSetup &) override
std::unique_ptr< MomentumScaleCorrector > momCorrector_
#define LogTrace(id)
T get() const
Definition: EventSetup.h:73
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:23
static unsigned int const shift
DistortedMuonProducerFromDB(const edm::ParameterSet &)
double phi() const final
momentum azimuthal angle
T const * product() const
Definition: ESHandle.h:86
void setP4(const LorentzVector &p4) final
set 4-momentum
Muon * clone() const override
create a clone
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
double mass() const final
mass
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_