CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ObjectEnergyScale.h
Go to the documentation of this file.
1 //
2 // $Id: ObjectEnergyScale.h,v 1.3 2010/10/20 23:09:25 wmtan Exp $
3 //
4 
5 #ifndef PhysicsTools_PatAlgos_ObjectEnergyScale_h
6 #define PhysicsTools_PatAlgos_ObjectEnergyScale_h
7 
27 
30 #include "CLHEP/Random/RandGaussQ.h"
31 
32 
33 namespace pat {
34 
35 
36  template<class T>
38 
39  public:
40 
41  explicit ObjectEnergyScale(const edm::ParameterSet& iConfig);
43 
44  private:
45 
46  virtual void produce( edm::Event& iEvent, const edm::EventSetup& iSetup);
47 
48  float getSmearing(T& object);
49  void setScale(T& object);
50 
52  float factor_,
54  iniRes_,
55  worsenRes_;
60 
61  CLHEP::RandGaussQ* gaussian_;
62 
63  };
64 
65 
66 }
67 
68 
69 template<class T>
71 {
72  objects_ = iConfig.getParameter<edm::InputTag>("scaledObject");
73  useFixedMass_ = iConfig.getParameter<bool> ("fixMass");
74  shiftFactor_ = iConfig.getParameter<double> ("shiftFactor");
75  useDefaultIniRes_ = iConfig.getParameter<bool> ("useDefaultInitialResolution");
76  iniRes_ = iConfig.getParameter<double> ("initialResolution");
77  useIniResByFraction_ = iConfig.getParameter<bool> ("initialResolutionByFraction");
78  worsenRes_ = iConfig.getParameter<double> ("worsenResolution");
79  useWorsenResByFactor_ = iConfig.getParameter<bool> ("worsenResolutionByFactor");
80 
82  CLHEP::HepRandomEngine& engine = rng->getEngine();
83  gaussian_ = new CLHEP::RandGaussQ(engine);
84 
85  produces<std::vector<T> >();
86 }
87 
88 
89 template<class T>
91 {
92  delete gaussian_;
93 }
94 
95 
96 template<class T>
98 {
99  edm::Handle<std::vector<T> > objectsHandle;
100  iEvent.getByLabel(objects_, objectsHandle);
101  std::vector<T> objects = *objectsHandle;
102  std::auto_ptr<std::vector<T> > objectsVector(new std::vector<T>);
103  objectsVector->reserve(objectsHandle->size());
104 
105  for ( unsigned int i = 0; i < objects.size(); i++ ) {
106  factor_ = shiftFactor_ * ( objects[i].energy() > 0. ?
107  getSmearing(objects[i]) :
108  0.);
109  setScale(objects[i]);
110  objectsVector->push_back(objects[i]);
111  }
112  iEvent.put(objectsVector);
113 }
114 
115 
119 template<class T>
121 {
122  // overwrite config file parameter 'initialResolution' if required
123  if ( useDefaultIniRes_ ) {
124  // get initial resolution from input object (and calculate relative initial resolution from absolute value)
125  iniRes_ = (1. / sin(object.theta()) * object.resolutionEt() - object.et() * cos(object.theta()) / std::pow(sin(object.theta()),2) * object.resolutionTheta()) / object.energy(); // conversion of resEt and resTheta into energy resolution
126  } else if ( ! useIniResByFraction_ ) {
127  // calculate relative initial resolution from absolute value
128  iniRes_ = iniRes_ / object.energy();
129  }
130  // Is 'worsenResolution' a factor or a summand?
131  float finalRes = useWorsenResByFactor_ ?
132  (1.+fabs(1.-fabs(worsenRes_))) * fabs(iniRes_) :
133  fabs(worsenRes_)/object.energy() + fabs(iniRes_); // conversion as protection from "finalRes_<iniRes_"
134  // return smearing factor
135  return std::max( gaussian_->fire(1., sqrt(std::pow(finalRes,2)-std::pow(iniRes_,2))), 0. ); // protection from negative smearing factors
136 }
137 
138 
141 template<class T>
143 {
144  if ( factor_ < 0. ) {
145  factor_ = 0.;
146  }
147  // calculate the momentum factor for fixed or not fixed mass
148  float factorMomentum = useFixedMass_ && object.p() > 0. ?
149  sqrt(std::pow(factor_*object.energy(),2)-object.massSqr()) / object.p() :
150  factor_;
151  // set shifted & smeared new 4-vector
152  object.setP4(reco::Particle::LorentzVector(factorMomentum*object.px(),
153  factorMomentum*object.py(),
154  factorMomentum*object.pz(),
155  factor_ *object.energy()));
156 }
157 
158 
159 #endif
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Geom::Theta< T > theta() const
Energy scale shifting and smearing module.
int iEvent
Definition: GenABIO.cc:243
const T & max(const T &a, const T &b)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
T sqrt(T t)
Definition: SSEVec.h:46
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
ObjectEnergyScale(const edm::ParameterSet &iConfig)
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
CLHEP::RandGaussQ * gaussian_
float getSmearing(T &object)
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup)
long double T
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:25
struct factor_s factor_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40