CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ObjectSpatialResolution.h
Go to the documentation of this file.
1 //
2 // $Id: ObjectSpatialResolution.h,v 1.3 2010/10/20 23:09:25 wmtan Exp $
3 //
4 
5 #ifndef PhysicsTools_PatAlgos_ObjectSpatialResolution_h
6 #define PhysicsTools_PatAlgos_ObjectSpatialResolution_h
7 
22 #include <cmath>
23 
29 
32 #include "CLHEP/Random/RandGaussQ.h"
34 
35 #include <cmath>
36 
37 
38 namespace pat {
39 
40 
41  template<class T>
43 
44  public:
45 
46  explicit ObjectSpatialResolution(const edm::ParameterSet& iConfig);
48 
49  private:
50 
51  virtual void produce(edm::Event& iEvent, const edm::EventSetup& iSetup);
52 
53  void smearAngles(T& object);
54 
56  double iniResPolar_,
58  iniResPhi_,
64 
65  CLHEP::RandGaussQ* gaussian_;
66 
67  };
68 
69 
70 }
71 
72 
73 template<class T>
75 {
76  objects_ = iConfig.getParameter<edm::InputTag>("movedObject");
77  useDefaultIniRes_ = iConfig.getParameter<bool> ("useDefaultInitialResolutions");
78  iniResPhi_ = iConfig.getParameter<double> ("initialResolutionPhi");
79  worsenResPhi_ = iConfig.getParameter<double> ("worsenResolutionPhi");
80  useWorsenResPhiByFactor_ = iConfig.getParameter<bool> ("worsenResolutionPhiByFactor");
81  usePolarTheta_ = iConfig.getParameter<bool> ("usePolarTheta");
82  iniResPolar_ = iConfig.getParameter<double> ("initialResolutionPolar");
83  worsenResPolar_ = iConfig.getParameter<double> ("worsenResolutionPolar");
84  useWorsenResPolarByFactor_ = iConfig.getParameter<bool> ("worsenResolutionPolarByFactor");
85 
87  CLHEP::HepRandomEngine& engine = rng->getEngine();
88  gaussian_ = new CLHEP::RandGaussQ(engine);
89 
90  produces<std::vector<T> >();
91 }
92 
93 
94 template<class T>
96 {
97  delete gaussian_;
98 }
99 
100 
101 template<class T>
103 {
104  edm::Handle<std::vector<T> > objectsHandle;
105  iEvent.getByLabel(objects_, objectsHandle);
106  std::vector<T> objects = *objectsHandle;
107  std::auto_ptr<std::vector<T> > objectsVector(new std::vector<T>);
108  objectsVector->reserve(objectsHandle->size());
109 
110  for ( unsigned int i = 0; i < objects.size(); i++ ) {
111  smearAngles(objects[i]);
112  objectsVector->push_back(objects[i]);
113  }
114  iEvent.put(objectsVector);
115 }
116 
117 
120 template<class T>
122 {
123  // overwrite config file parameters 'initialResolution...' if required
124  if ( useDefaultIniRes_ ) {
125  // get initial resolutions from input object
126  iniResPhi_ = object.resolutionPhi(); // overwrites config file parameter "initialResolution"
127  iniResPolar_ = usePolarTheta_ ?
128  object.resolutionTheta():
129  object.resolutionEta(); // overwrites config file parameter "initialResolution"
130  }
131  // smear phi
132  double finalResPhi = useWorsenResPhiByFactor_ ?
133  (1.+fabs(1.-fabs(worsenResPhi_))) * fabs(iniResPhi_):
134  fabs(worsenResPhi_) + fabs(iniResPhi_); // conversions as protection from "finalRes_<iniRes_"
135  double smearedPhi = normalizedPhi( gaussian_->fire(object.phi(), sqrt(std::pow(finalResPhi,2)-std::pow(iniResPhi_,2))) );
136  double finalResPolar = useWorsenResPolarByFactor_ ?
137  (1.+fabs(1.-fabs(worsenResPolar_))) * fabs(iniResPolar_):
138  fabs(worsenResPolar_) + fabs(iniResPolar_); // conversions as protection from "finalRes_<iniRes_"
139  // smear theta/eta
140  const double thetaMin = 2.*atan(exp(-ROOT::Math::etaMax<double>())); // to be on the safe side; however, etaMax=22765 ==> thetaMin=0 within double precision
141  double smearedTheta,
142  smearedEta;
143  if ( usePolarTheta_ ) {
144  smearedTheta = gaussian_->fire(object.theta(), sqrt(std::pow(finalResPolar,2)-std::pow(iniResPolar_,2)));
145  // 0<theta<Pi needs to be assured to have proper calculation of eta
146  while ( fabs(smearedTheta) > M_PI ) smearedTheta = smearedTheta < 0. ?
147  smearedTheta + 2.*M_PI:
148  smearedTheta - 2.*M_PI;
149  if ( smearedTheta < 0. ) {
150  smearedTheta = -smearedTheta;
151  smearedPhi = normalizedPhi(smearedPhi+M_PI);
152  }
153  smearedEta = smearedTheta < thetaMin ?
154  ROOT::Math::etaMax<double>() :
155  ( smearedTheta > M_PI-thetaMin ?
156  -ROOT::Math::etaMax<double>():
157  -log(tan(smearedTheta/2.)) ); // eta not calculable for theta=0,Pi, which could occur
158  } else {
159  smearedEta = gaussian_->fire(object.eta(), sqrt(std::pow(finalResPolar,2)-std::pow(iniResPolar_,2)));
160  if ( fabs(smearedEta) > ROOT::Math::etaMax<double>() ) smearedEta = smearedEta < 0. ?
161  -ROOT::Math::etaMax<double>():
162  ROOT::Math::etaMax<double>();
163  smearedTheta = 2. * atan(exp(-smearedEta)); // since exp(x)>0 && atan() returns solution closest to 0, 0<theta<Pi should be assured.
164  }
165  // set smeared new 4-vector
166  math::PtEtaPhiELorentzVector newLorentzVector(object.p()*sin(smearedTheta),
167  smearedEta,
168  smearedPhi,
169  object.energy());
170  object.setP4(reco::Particle::LorentzVector(newLorentzVector.Px(),
171  newLorentzVector.Py(),
172  newLorentzVector.Pz(),
173  newLorentzVector.E() ));
174 }
175 
176 
177 #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
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup)
T eta() const
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
T sqrt(T t)
Definition: SSEVec.h:46
Energy scale shifting and smearing module.
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
ObjectSpatialResolution(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
#define M_PI
Definition: BFit3D.cc:3
PtEtaPhiELorentzVectorD PtEtaPhiELorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:28
double normalizedPhi(double phi)
Definition: normalizedPhi.cc:5
long double T
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:25
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40