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 
10 //
11 // class declaration
12 //
14  public:
17 
18  private:
19  virtual void beginRun(const edm::Run&, const edm::EventSetup&) override;
20  virtual void produce(edm::Event&, const edm::EventSetup&) override;
21  virtual void endJob() override ;
22 
25 
29 
30  std::auto_ptr<MomentumScaleCorrector> momCorrector_;
31  std::auto_ptr<ResolutionFunction> momResolutionData_;
32  std::auto_ptr<ResolutionFunction> momResolutionMC_;
33 };
34 
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  muonTag_ = 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.getByLabel(muonTag_, 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_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::auto_ptr< ResolutionFunction > momResolutionData_
bool isRealData() const
Definition: EventBase.h:60
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
virtual float phi() const GCC11_FINAL
momentum azimuthal angle
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
T sqrt(T t)
Definition: SSEVec.h:48
const int mu
Definition: Constants.h:22
virtual void beginRun(const edm::Run &, const edm::EventSetup &) override
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
#define LogTrace(id)
virtual float eta() const GCC11_FINAL
momentum pseudorapidity
Muon * clone() const
create a clone
Definition: Muon.cc:44
virtual float mass() const GCC11_FINAL
mass
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
std::auto_ptr< ResolutionFunction > momResolutionMC_
virtual void setP4(const LorentzVector &p4) GCC11_FINAL
set 4-momentum
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:29
static unsigned int const shift
DistortedMuonProducerFromDB(const edm::ParameterSet &)
virtual float pt() const GCC11_FINAL
transverse momentum
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Definition: Run.h:41