CMS 3D CMS Logo

fourvec.cc
Go to the documentation of this file.
1 //
2 //
3 // File: src/fourvec.cc
4 // Purpose: Define 3- and 4-vector types for the hitfit package, and
5 // supply a few additional operations.
6 // Created: Jul, 2000, sss, based on run 1 mass analysis code.
7 //
8 // CMSSW File : src/fourvec.cc
9 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
10 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
11 //
12 
36 #include <cmath>
37 
38 using std::atan;
39 using std::atan2;
40 using std::cos;
41 using std::exp;
42 using std::fabs;
43 using std::log;
44 using std::sin;
45 using std::sqrt;
46 using std::tan;
47 
48 namespace { // unnamed namespace
49 
50  double cal_th(double theta, double z)
51  //
52  // Purpose: Helper for deteta. Find `calorimeter theta' given
53  // physics theta and z-vertex. Copied from run 1 code.
54  //
55  // Inputs:
56  // theta - Physics theta.
57  // z - Z-vertex.
58  //
59  // Returns:
60  // Calorimeter theta.
61  //
62  {
63  const double R_CC = 91.6;
64  const double Z_EC = 178.9;
65  const double BIGG = 1e8;
66 
67  double tanth;
68  if (fabs(cos(theta)) < 1 / BIGG)
69  tanth = BIGG * sin(theta);
70  else
71  tanth = tan(theta);
72 
73  double z_cc = R_CC / tanth + z;
74 
75  if (fabs(z_cc) < Z_EC)
76  theta = atan2(R_CC, z_cc);
77 
78  else {
79  double zz = Z_EC;
80  if (tanth < 0)
81  zz = -zz;
82  double r_ec = (zz - z) * tanth;
83  theta = atan2(r_ec, zz);
84  }
85 
86  if (theta < 0)
87  theta += 2 * M_PI;
88  return theta;
89  }
90 
91 } // unnamed namespace
92 
93 namespace hitfit {
94 
96  //
97  // Purpose: Adjust the 3-vector part of V (leaving the energy unchanged)
98  // so that it has mass MASS. (Provided that is possible.)
99  //
100  // Inputs:
101  // v - The 4-vector to scale.
102  // mass - The desired mass of the 4-vector.
103  //
104  // Outputs:
105  // v - The scaled 4-vector.
106  //
107  {
108  CLHEP::Hep3Vector vect = v.vect();
109  double old_p2 = vect.mag2();
110  if (old_p2 == 0)
111  return;
112  double new_p2 = v.e() * v.e() - mass * mass;
113  if (new_p2 < 0)
114  new_p2 = 0;
115  vect *= sqrt(new_p2 / old_p2);
116  v.setVect(vect);
117  }
118 
120  //
121  // Purpose: Adjust the energy component of V (leaving the 3-vector part
122  // unchanged) so that it has mass MASS.
123  //
124  // Inputs:
125  // v - The 4-vector to scale.
126  // mass - The desired mass of the 4-vector.
127  //
128  // Outputs:
129  // v - The scaled 4-vector.
130  //
131  {
132  v.setE(sqrt(v.vect().mag2() + mass * mass));
133  }
134 
135  void rottheta(Fourvec& v, double theta)
136  //
137  // Purpose: Rotate V through polar angle THETA.
138  //
139  // Inputs:
140  // v - The 4-vector to rotate.
141  // theta - The rotation angle.
142  //
143  // Outputs:
144  // v - The rotated 4-vector.
145  //
146  {
147  double s = sin(theta), c = cos(theta);
148  double old_pt = v.perp();
149  double new_pt = old_pt * c - v.z() * s;
150  v.setZ(old_pt * s + v.z() * c);
151 
152  v.setX(v.x() * new_pt / old_pt);
153  v.setY(v.y() * new_pt / old_pt);
154  }
155 
156  void roteta(Fourvec& v, double eta)
157  //
158  // Purpose: Rotate a Fourvec through a polar angle such that
159  // its pseudorapidity changes by ETA.
160  //
161  // Inputs:
162  // v - The 4-vector to rotate.
163  // eta - The rotation angle.
164  //
165  // Outputs:
166  // v - The rotated 4-vector.
167  //
168  {
169  double theta1 = v.theta();
170  double eta1 = theta_to_eta(theta1);
171  double eta2 = eta1 + eta;
172  double theta2 = eta_to_theta(eta2);
173 
174  rottheta(v, theta1 - theta2);
175  }
176 
177  double eta_to_theta(double eta)
178  //
179  // Purpose: Convert psuedorapidity to polar angle.
180  //
181  // Inputs:
182  // eta - Pseudorapidity.
183  //
184  // Returns:
185  // Polar angle.
186  //
187  {
188  return 2 * atan(exp(-eta));
189  }
190 
191  double theta_to_eta(double theta)
192  //
193  // Purpose: Convert polar angle to psuedorapidity.
194  //
195  // Inputs:
196  // theta - Polar angle.
197  //
198  // Returns:
199  // Pseudorapidity.
200  //
201  {
202  return -log(tan(theta / 2));
203  }
204 
205  double deteta(const Fourvec& v, double zvert)
206  //
207  // Purpose: Get the detector eta (D0-specific).
208  //
209  // Inputs:
210  // v - Vector on which to operate.
211  // zvert - Z-vertex.
212  //
213  // Returns:
214  // Detector eta of V.
215  //
216  {
217  return theta_to_eta(cal_th(v.theta(), zvert));
218  }
219 
220  double phidiff(double phi)
221  //
222  // Purpose: Handle wraparound for a difference in azimuthal angles.
223  //
224  // Inputs:
225  // phi - Azimuthal angle.
226  //
227  // Returns:
228  // PHI normalized to the range -pi .. pi.
229  //
230  {
231  while (phi < -M_PI)
232  phi += 2 * M_PI;
233  while (phi > M_PI)
234  phi -= 2 * M_PI;
235  return phi;
236  }
237 
238  double delta_r(const Fourvec& a, const Fourvec& b)
239  //
240  // Purpose: Find the distance in R between two four-vectors.
241  //
242  // Inputs:
243  // a - First four-vector.
244  // b - Second four-vector.
245  //
246  // Returns:
247  // the distance in R between A and B.
248  //
249  {
250  double deta = a.pseudoRapidity() - b.pseudoRapidity();
251  double dphi = phidiff(a.phi() - b.phi());
252  return sqrt(deta * deta + dphi * dphi);
253  }
254 
255 } // namespace hitfit
Define three-vector and four-vector classes for the HitFit package, and supply a few additional opera...
void adjust_e_for_mass(Fourvec &v, double mass)
Adjust the energy component of four-vector v (leaving the three-vector part unchanged) so that the fo...
Definition: fourvec.cc:119
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
double phidiff(double phi)
Normalized difference in azimuthal angles to a range between .
Definition: fourvec.cc:220
double theta_to_eta(double theta)
Convert polar angle to pseudorapidity.
Definition: fourvec.cc:191
void roteta(Fourvec &v, double eta)
Rotate four-vector v through a polar angle such that the four-vector pseudorapidity changes by a desi...
Definition: fourvec.cc:156
T sqrt(T t)
Definition: SSEVec.h:23
double eta_to_theta(double eta)
Convert pseudorapidity to polar angle.
Definition: fourvec.cc:177
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
double delta_r(const Fourvec &a, const Fourvec &b)
Find the distance between two four-vectors in the two-dimensional space .
Definition: fourvec.cc:238
CLHEP::HepLorentzVector Fourvec
Typedef for a HepLorentzVector.
Definition: fourvec.h:55
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
double deteta(const Fourvec &v, double zvert)
NOT USED ANYMORE: Get the detector (D0-specific), requires z-vertex.
Definition: fourvec.cc:205
void adjust_p_for_mass(Fourvec &v, double mass)
Adjust the three-vector part of v, leaving the energy unchanged,.
Definition: fourvec.cc:95
#define M_PI
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
void rottheta(Fourvec &v, double theta)
Rotate four-vector v through a polar angle.
Definition: fourvec.cc:135
MPlex< T, D1, D2, N > atan2(const MPlex< T, D1, D2, N > &y, const MPlex< T, D1, D2, N > &x)
Definition: Matriplex.h:648