CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MultiTrajectoryStateMode.cc
Go to the documentation of this file.
3 
5 
11 
12 #include <iostream>
13 
14 namespace multiTrajectoryStateMode {
15 
17  //
18  // clear result vector and check validity of the TSOS
19  //
20  momentum = GlobalVector(0., 0., 0.);
21  if (!tsos.isValid()) {
22  edm::LogInfo("multiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
23  return false;
24  }
25  //
26  // 1D mode computation for px, py and pz
27  //
28  GetComponents comps(tsos);
29  auto const& components = comps();
30  auto numb = components.size();
31  // vectors of components in x, y and z
32  std::vector<SingleGaussianState1D> pxStates;
33  pxStates.reserve(numb);
34  std::vector<SingleGaussianState1D> pyStates;
35  pyStates.reserve(numb);
36  std::vector<SingleGaussianState1D> pzStates;
37  pzStates.reserve(numb);
38  // iteration over components
39  for (std::vector<TrajectoryStateOnSurface>::const_iterator ic = components.begin(); ic != components.end(); ++ic) {
40  // extraction of parameters and variances
41  GlobalVector mom(ic->globalMomentum());
42  AlgebraicSymMatrix66 cov(ic->cartesianError().matrix());
43  pxStates.push_back(SingleGaussianState1D(mom.x(), cov(3, 3), ic->weight()));
44  pyStates.push_back(SingleGaussianState1D(mom.y(), cov(4, 4), ic->weight()));
45  pzStates.push_back(SingleGaussianState1D(mom.z(), cov(5, 5), ic->weight()));
46  }
47  //
48  // transformation in 1D multi-states and creation of utility classes
49  //
50  MultiGaussianState1D pxState(pxStates);
51  MultiGaussianState1D pyState(pyStates);
52  MultiGaussianState1D pzState(pzStates);
53  GaussianSumUtilities1D pxUtils(pxState);
54  GaussianSumUtilities1D pyUtils(pyState);
55  GaussianSumUtilities1D pzUtils(pzState);
56  //
57  // cartesian momentum vector from modes
58  //
59  momentum = GlobalVector(pxUtils.mode().mean(), pyUtils.mode().mean(), pzUtils.mode().mean());
60  return true;
61  }
62 
64  //
65  // clear result vector and check validity of the TSOS
66  //
67  position = GlobalPoint(0., 0., 0.);
68  if (!tsos.isValid()) {
69  edm::LogInfo("multiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
70  return false;
71  }
72  //
73  // 1D mode computation for x, y and z
74  //
75 
76  GetComponents comps(tsos);
77  auto const& components = comps();
78  auto numb = components.size();
79  // vectors of components in x, y and z
80  std::vector<SingleGaussianState1D> xStates;
81  xStates.reserve(numb);
82  std::vector<SingleGaussianState1D> yStates;
83  yStates.reserve(numb);
84  std::vector<SingleGaussianState1D> zStates;
85  zStates.reserve(numb);
86  // iteration over components
87  for (std::vector<TrajectoryStateOnSurface>::const_iterator ic = components.begin(); ic != components.end(); ++ic) {
88  // extraction of parameters and variances
89  GlobalPoint pos(ic->globalPosition());
90  AlgebraicSymMatrix66 cov(ic->cartesianError().matrix());
91  xStates.push_back(SingleGaussianState1D(pos.x(), cov(0, 0), ic->weight()));
92  yStates.push_back(SingleGaussianState1D(pos.y(), cov(1, 1), ic->weight()));
93  zStates.push_back(SingleGaussianState1D(pos.z(), cov(2, 2), ic->weight()));
94  }
95  //
96  // transformation in 1D multi-states and creation of utility classes
97  //
98  MultiGaussianState1D xState(xStates);
99  MultiGaussianState1D yState(yStates);
100  MultiGaussianState1D zState(zStates);
101  GaussianSumUtilities1D xUtils(xState);
102  GaussianSumUtilities1D yUtils(yState);
103  GaussianSumUtilities1D zUtils(zState);
104  //
105  // cartesian position vector from modes
106  //
107  position = GlobalPoint(xUtils.mode().mean(), yUtils.mode().mean(), zUtils.mode().mean());
108  return true;
109  }
110 
112  //
113  // clear result vector and check validity of the TSOS
114  //
115  momentum = GlobalVector(0., 0., 0.);
116  if (!tsos.isValid()) {
117  edm::LogInfo("multiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
118  return false;
119  }
120  //
121  // mode computation for local co-ordinates q/p, dx/dz, dy/dz
122  //
123  double qpMode(0);
124  double dxdzMode(0);
125  double dydzMode(0);
126  //
127  // first 3 elements of local parameters = q/p, dx/dz, dy/dz
128  //
129  for (unsigned int iv = 0; iv < 3; ++iv) {
130  // extraction of multi-state using helper class
132  GaussianSumUtilities1D utils(state1D);
133  // mode (in case of failure: mean)
134  double result = utils.mode().mean();
135  if (!utils.modeIsValid())
136  result = utils.mean();
137  if (iv == 0)
138  qpMode = result;
139  else if (iv == 1)
140  dxdzMode = result;
141  else
142  dydzMode = result;
143  }
144  // local momentum vector from dx/dz, dy/dz and q/p + sign of local pz
145  LocalVector localP(dxdzMode, dydzMode, 1.);
146  localP *= tsos.localParameters().pzSign() / fabs(qpMode) / sqrt(dxdzMode * dxdzMode + dydzMode * dydzMode + 1.);
147  // conversion to global coordinates
148  momentum = tsos.surface().toGlobal(localP);
149  return true;
150  }
151 
152  bool momentumFromModeQP(TrajectoryStateOnSurface const& tsos, double& momentum) {
153  //
154  // clear result vector and check validity of the TSOS
155  //
156  momentum = 0.;
157  if (!tsos.isValid()) {
158  edm::LogInfo("multiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
159  return false;
160  }
161  //
162  // mode computation for local co-ordinates q/p, dx/dz, dy/dz
163  //
164  double qpMode(0);
165  //
166  // first element of local parameters = q/p
167  //
168  // extraction of multi-state using helper class
170  GaussianSumUtilities1D utils(state1D);
171  // mode (in case of failure: mean)
172  qpMode = utils.mode().mean();
173  if (!utils.modeIsValid())
174  qpMode = utils.mean();
175 
176  momentum = 1. / fabs(qpMode);
177  return true;
178  }
179 
180  bool momentumFromModeP(TrajectoryStateOnSurface const& tsos, double& momentum) {
181  //
182  // clear result vector and check validity of the TSOS
183  //
184  momentum = 0.;
185  if (!tsos.isValid()) {
186  edm::LogInfo("multiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
187  return false;
188  }
189  //
190  // first element of local parameters = q/p
191  //
192  // extraction of multi-state using helper class
194  std::vector<SingleGaussianState1D> states(qpMultiState.components());
195  // transform from q/p to p
196  for (unsigned int i = 0; i < states.size(); ++i) {
197  SingleGaussianState1D& qpState = states[i];
198  double wgt = qpState.weight();
199  double qp = qpState.mean();
200  double varQp = qpState.variance();
201  double p = 1. / fabs(qp);
202  double varP = p * p * p * p * varQp;
203  states[i] = SingleGaussianState1D(p, varP, wgt);
204  }
205  MultiGaussianState1D pMultiState(states);
206  GaussianSumUtilities1D utils(pMultiState);
207  // mode (in case of failure: mean)
208  momentum = utils.mode().mean();
209  if (!utils.modeIsValid())
210  momentum = utils.mean();
211 
212  return true;
213  }
214 
216  //
217  // clear result vector and check validity of the TSOS
218  //
219  position = GlobalPoint(0., 0., 0.);
220  if (!tsos.isValid()) {
221  edm::LogInfo("multiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
222  return false;
223  }
224  //
225  // mode computation for local co-ordinates x, y
226  //
227  double xMode(0);
228  double yMode(0);
229  //
230  // last 2 elements of local parameters = x, y
231  //
232  for (unsigned int iv = 3; iv < 5; ++iv) {
233  // extraction of multi-state using helper class
235  GaussianSumUtilities1D utils(state1D);
236  // mode (in case of failure: mean)
237  double result = utils.mode().mean();
238  if (!utils.modeIsValid())
239  result = utils.mean();
240  if (iv == 3)
241  xMode = result;
242  else
243  yMode = result;
244  }
245  // local position vector from x, y
246  LocalPoint localP(xMode, yMode, 0.);
247  // conversion to global coordinates
248  position = tsos.surface().toGlobal(localP);
249  return true;
250  }
251 
253  //
254  // clear result vector and check validity of the TSOS
255  //
256  momentum = GlobalVector(0., 0., 0.);
257  if (!tsos.isValid()) {
258  edm::LogInfo("multiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
259  return false;
260  }
261  //
262  // 1D mode computation for p, phi, eta
263  //
264  GetComponents comps(tsos);
265  auto const& components = comps();
266  auto numb = components.size();
267  // vectors of components in p, phi and eta
268  std::vector<SingleGaussianState1D> pStates;
269  pStates.reserve(numb);
270  std::vector<SingleGaussianState1D> phiStates;
271  phiStates.reserve(numb);
272  std::vector<SingleGaussianState1D> etaStates;
273  etaStates.reserve(numb);
274  // covariances in cartesian and p-phi-eta and jacobian
275  AlgebraicMatrix33 jacobian;
276  AlgebraicSymMatrix33 covCart;
277  AlgebraicSymMatrix33 covPPhiEta;
278  // iteration over components
279  for (std::vector<TrajectoryStateOnSurface>::const_iterator ic = components.begin(); ic != components.end(); ++ic) {
280  // parameters
281  GlobalVector mom(ic->globalMomentum());
282  auto px = mom.x();
283  auto py = mom.y();
284  auto pz = mom.z();
285  auto op = 1. / mom.mag();
286  auto opt2 = 1. / mom.perp2();
287  auto phi = mom.phi();
288  auto eta = mom.eta();
289  // jacobian
290  jacobian(0, 0) = px * op;
291  jacobian(0, 1) = py * op;
292  jacobian(0, 2) = pz * op;
293  jacobian(1, 0) = py * opt2;
294  jacobian(1, 1) = -px * opt2;
295  jacobian(1, 2) = 0;
296  jacobian(2, 0) = px * pz * opt2 * op;
297  jacobian(2, 1) = py * pz * opt2 * op;
298  jacobian(2, 2) = -op;
299  // extraction of the momentum part from the 6x6 cartesian error matrix
300  // and conversion to p-phi-eta
301  covCart = ic->cartesianError().matrix().Sub<AlgebraicSymMatrix33>(3, 3);
302  covPPhiEta = ROOT::Math::Similarity(jacobian, covCart);
303  pStates.push_back(SingleGaussianState1D(1 / op, covPPhiEta(0, 0), ic->weight()));
304  phiStates.push_back(SingleGaussianState1D(phi, covPPhiEta(1, 1), ic->weight()));
305  etaStates.push_back(SingleGaussianState1D(eta, covPPhiEta(2, 2), ic->weight()));
306  }
307  //
308  // transformation in 1D multi-states and creation of utility classes
309  //
310  MultiGaussianState1D pState(pStates);
311  MultiGaussianState1D phiState(phiStates);
312  MultiGaussianState1D etaState(etaStates);
313  GaussianSumUtilities1D pUtils(pState);
314  GaussianSumUtilities1D phiUtils(phiState);
315  GaussianSumUtilities1D etaUtils(etaState);
316  //
317  // parameters from mode (in case of failure: mean)
318  //
319  auto p = pUtils.modeIsValid() ? pUtils.mode().mean() : pUtils.mean();
320  auto phi = phiUtils.modeIsValid() ? phiUtils.mode().mean() : phiUtils.mean();
321  auto eta = etaUtils.modeIsValid() ? etaUtils.mode().mean() : etaUtils.mean();
322  // double theta = 2*atan(exp(-eta));
323  auto tanth2 = std::exp(-eta);
324  auto pt = p * 2 * tanth2 / (1 + tanth2 * tanth2); // p*sin(theta)
325  auto pz = p * (1 - tanth2 * tanth2) / (1 + tanth2 * tanth2); // p*cos(theta)
326  // conversion to a cartesian momentum vector
327  momentum = GlobalVector(pt * cos(phi), pt * sin(phi), pz);
328  return true;
329  }
330 
332  //
333  // clear result vector and check validity of the TSOS
334  //
335  if (!tsos.isValid()) {
336  edm::LogInfo("multiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
337  return 0;
338  }
339  //
340  // mode computation for local co-ordinates q/p
341  // extraction of multi-state using helper class
343  GaussianSumUtilities1D utils(state1D);
344  // mode (in case of failure: mean)
345  double result = utils.mode().mean();
346  if (!utils.modeIsValid())
347  result = utils.mean();
348 
349  return result > 0. ? 1 : -1;
350  }
351 
352 } // namespace multiTrajectoryStateMode
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
Definition: Surface.h:79
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepStd< double, 3, 3 > > AlgebraicMatrix33
int32_t *__restrict__ iv
const LocalTrajectoryParameters & localParameters() const
bool positionFromModeLocal(TrajectoryStateOnSurface const &tsos, GlobalPoint &position)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
int chargeFromMode(TrajectoryStateOnSurface const &tsos)
bool momentumFromModeP(TrajectoryStateOnSurface const &tsos, double &momentum)
Exp< T >::type exp(const T &t)
Definition: Exp.h:22
tuple result
Definition: mps_fire.py:311
const SurfaceType & surface() const
MultiGaussianState1D multiState1D(const std::vector< MultiGaussianState< N >::Vector > &, const std::vector< MultiGaussianState< N >::Matrix > &, const std::vector< double > &, unsigned int)
bool positionFromModeCartesian(TrajectoryStateOnSurface const &tsos, GlobalPoint &position)
double mean() const
parameter vector
T sqrt(T t)
Definition: SSEVec.h:19
double variance() const
variance
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
const SingleGaussianState1D & mode() const
ROOT::Math::SMatrix< double, 6, 6, ROOT::Math::MatRepSym< double, 6 > > AlgebraicSymMatrix66
Log< level::Info, false > LogInfo
double weight() const
weight
double mean(unsigned int i) const
mean value of a component
const SingleState1dContainer & components() const
access to components
bool momentumFromModeLocal(TrajectoryStateOnSurface const &tsos, GlobalVector &momentum)
bool momentumFromModePPhiEta(TrajectoryStateOnSurface const &tsos, GlobalVector &momentum)
static int position[264][3]
Definition: ReadPGInfo.cc:289
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepSym< double, 3 > > AlgebraicSymMatrix33
bool momentumFromModeQP(TrajectoryStateOnSurface const &tsos, double &momentum)
bool momentumFromModeCartesian(TrajectoryStateOnSurface const &tsos, GlobalVector &momentum)
float pzSign() const
Sign of the z-component of the momentum in the local frame.
T x() const
Definition: PV3DBase.h:59
bool modeIsValid() const
mode status
Global3DVector GlobalVector
Definition: GlobalVector.h:10