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  virtual void beginRun(const edm::Run&, const edm::EventSetup&) override;
23  virtual void produce(edm::Event&, const edm::EventSetup&) override;
24  virtual 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 
53  // What is being produced
54  produces<std::vector<reco::Muon> >();
55 
56  // Input products
57  muonToken_ = 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 }
63 
66 }
67 
71  iSetup.get<MuScleFitDBobjectRcd>().get(dbScaleLabel_,dbObject1);
72  momCorrector_.reset(new MomentumScaleCorrector(dbObject1.product()));
73 
74  LogTrace("") << ">>> Using database for momentum scale corrections !!";
75 
77  iSetup.get<MuScleFitDBobjectRcd>().get(dbDataResolutionLabel_, dbObject2);
78  momResolutionData_.reset(new ResolutionFunction(dbObject2.product()));
79 
81  iSetup.get<MuScleFitDBobjectRcd>().get(dbMCResolutionLabel_, dbObject3);
82  momResolutionMC_.reset(new ResolutionFunction(dbObject3.product()));
83 
84  LogTrace("") << ">>> Using database for momentum resolution corrections !!";
85 }
86 
89 }
90 
93 
94  if (ev.isRealData()) return;
95 
96  // Muon collection
98  if (!ev.getByToken(muonToken_, muonCollection)) {
99  edm::LogError("") << ">>> Muon collection does not exist !!!";
100  return;
101  }
102  unsigned int muonCollectionSize = muonCollection->size();
103 
104  std::unique_ptr<reco::MuonCollection> newmuons (new reco::MuonCollection);
105 
106  for (unsigned int i=0; i<muonCollectionSize; i++) {
107  edm::RefToBase<reco::Muon> mu = muonCollection->refAt(i);
108 
109  // Set shift
110  double shift = (*momCorrector_)(*mu) - mu->pt();
111  LogTrace("") << "\tmomentumScaleShift= " << shift << " [GeV]";
112 
113  // Set resolutions
114  double sigma = pow(momResolutionData_->sigmaPt(*mu),2) -
115  pow(momResolutionMC_->sigmaPt(*mu),2);
116  if (sigma>0.) sigma = sqrt(sigma); else sigma = 0.;
117  LogTrace("") << "\tPt additional smearing= " << sigma << " [GeV]";
118 
119  // Gaussian Random number for smearing
120  double rndg = CLHEP::RandGauss::shoot();
121 
122  // New muon
123  double ptmu = mu->pt();
124  ptmu += shift + sigma*rndg;
125  reco::Muon* newmu = mu->clone();
126  newmu->setP4 (
128  ptmu, mu->eta(), mu->phi(), mu->mass()
129  )
130  );
131  newmuons->push_back(*newmu);
132 
133  }
134 
135  ev.put(std::move(newmuons));
136 }
137 
virtual double pt() const final
transverse momentum
std::unique_ptr< ResolutionFunction > momResolutionData_
virtual 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:122
virtual double mass() const final
mass
virtual double eta() const final
momentum pseudorapidity
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool ev
bool isRealData() const
Definition: EventBase.h:64
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
virtual double phi() const final
momentum azimuthal angle
T sqrt(T t)
Definition: SSEVec.h:18
const int mu
Definition: Constants.h:22
virtual void beginRun(const edm::Run &, const edm::EventSetup &) override
std::unique_ptr< MomentumScaleCorrector > momCorrector_
#define LogTrace(id)
const T & get() const
Definition: EventSetup.h:55
virtual void setP4(const LorentzVector &p4) final
set 4-momentum
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:23
static unsigned int const shift
DistortedMuonProducerFromDB(const edm::ParameterSet &)
T const * product() const
Definition: ESHandle.h:86
Muon * clone() const
create a clone
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
def move(src, dest)
Definition: eostools.py:510
Definition: Run.h:42
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_