test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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::auto_ptr<MomentumScaleCorrector> momCorrector_;
33  std::auto_ptr<ResolutionFunction> momResolutionData_;
34  std::auto_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
97  edm::Handle<edm::View<reco::Muon> > muonCollection;
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::auto_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(newmuons);
136 }
137 
virtual void produce(edm::Event &, const edm::EventSetup &) override
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
std::auto_ptr< MomentumScaleCorrector > momCorrector_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::auto_ptr< ResolutionFunction > momResolutionData_
virtual double phi() const final
momentum azimuthal angle
bool ev
bool isRealData() const
Definition: EventBase.h:63
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
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
virtual double mass() const final
mass
#define LogTrace(id)
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
virtual void setP4(const LorentzVector &p4) final
set 4-momentum
std::auto_ptr< ResolutionFunction > momResolutionMC_
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:23
static unsigned int const shift
DistortedMuonProducerFromDB(const edm::ParameterSet &)
virtual double eta() const final
momentum pseudorapidity
Muon * clone() const
create a clone
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Definition: Run.h:43
virtual double pt() const final
transverse momentum
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_