CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackResiduals.cc
Go to the documentation of this file.
1 #include <math.h>
4 #include <cstdio>
5 #include <cstring>
6 
8 
9 using namespace reco;
10 
11 TrackResiduals::TrackResiduals () : residualType(X_Y_RESIDUALS)
12 {
13  memset(residuals_, 0, sizeof(residuals_));
14 }
15 
17 {
18  memset(residuals_, 0, sizeof(residuals_));
19 }
20 
22 {
24 }
25 
26 void TrackResiduals::setResidualXY (int idx, double residualX, double residualY)
27 {
28  assert(residualType == X_Y_RESIDUALS);
29  if (idx>=numResiduals) {
30  edm::LogWarning("TrackResiduals")<<" setting residual over the array size.";
31  return;}
32  residuals_[idx] = (pack_residual(residualX) << 4) | pack_residual(residualY);
33 }
34 
35 double TrackResiduals::residualX (int i) const
36 {
37  switch (residualType) {
38  case X_Y_RESIDUALS:
39  return unpack_residual(residuals_[i] >> 4);
40  case X_Y_PULLS:
41  return unpack_pull(residuals_[i] >> 4);
42  default:
43  assert(0);
44  }
45  return 0;
46 }
47 
48 double TrackResiduals::residualY (int i) const
49 {
50  switch (residualType) {
51  case X_Y_RESIDUALS:
52  return unpack_residual(residuals_[i] & 0x0f);
53  case X_Y_PULLS:
54  return unpack_pull(residuals_[i] & 0x0f);
55  default:
56  assert(0);
57  }
58  return 0;
59 }
60 
61 static int index_to_hitpattern (int i_hitpattern, const HitPattern &h)
62 {
63  int i_residuals = 0;
64  assert(i_hitpattern < h.numberOfHits());
65  if (!h.validHitFilter(h.getHitPattern(i_hitpattern)))
66  // asking for residual of invalid hit...
67  return -999;
68  for (int i = 0; i <= i_hitpattern; i++) {
69  if (h.validHitFilter(h.getHitPattern(i)))
70  i_residuals++;
71  }
72  assert(i_residuals > 0);
73  return i_residuals - 1;
74 }
75 
76 double TrackResiduals::residualX (int i, const HitPattern &h) const
77 {
78  int idx = index_to_hitpattern(i, h);
79  if (idx == -999)
80  return -999;
81  return residualX(idx);
82 }
83 
84 double TrackResiduals::residualY (int i, const HitPattern &h) const
85 {
86  int idx = index_to_hitpattern(i, h);
87  if (idx == -999)
88  return -999;
89  return residualY(idx);
90 }
91 
92 void TrackResiduals::setPullXY (int idx, double pullX, double pullY)
93 {
94  assert(residualType == X_Y_PULLS);
95  if (idx>=numResiduals) {
96  edm::LogWarning("TrackResiduals")<<" setting pulls over the array size.";
97  return;}
98 
99  residuals_[idx] = (pack_pull(pullX) << 4) | pack_pull(pullY);
100 }
101 
102 static const double pull_char_to_double[8][2] = {
103  { 0, 0.5 },
104  { 0.5, 1 },
105  { 1, 1.5 },
106  { 1.5, 2 },
107  { 2, 2.5 },
108  { 2.5, 3.5 },
109  { 3.5, 4.5 },
110  { 4.5, 5.5 },
111 };
112 
113 double TrackResiduals::unpack_pull (unsigned char pull)
114 {
115  int sgn = 1 - 2 * ((pull & 0x08) >> 3);
116  unsigned char mag = pull & 0x07;
117  return sgn *
119 }
120 
121 unsigned char TrackResiduals::pack_pull (double pull)
122 {
123  unsigned char sgn = (pull < 0) * 0x08; // 1xxx is -abs(0xxx)
124  int mag = -1;
125  while (++mag < 8 && pull_char_to_double[mag][1] < fabs(pull));
126  return sgn + mag;
127 }
128 
129 static const double residual_char_to_double[8][2] = {
130  { 0, 0.5 },
131  { 0.5, 1 },
132  { 1, 1.5 },
133  { 1.5, 2 },
134  { 2, 2.5 },
135  { 2.5, 3.5 },
136  { 3.5, 4.5 },
137  { 4.5, 5.5 },
138 };
139 
140 double TrackResiduals::unpack_residual (unsigned char pull)
141 {
142  int sgn = 1 - 2 * ((pull & 0x08) >> 3);
143  unsigned char mag = pull & 0x07;
144  return sgn *
146 }
147 
148 unsigned char TrackResiduals::pack_residual (double pull)
149 {
150  unsigned char sgn = (pull < 0) * 0x08; // 1xxx is -abs(0xxx)
151  int mag = -1;
152  while (++mag < 8 && pull_char_to_double[mag][1] < fabs(pull));
153  return sgn + mag;
154 }
155 
156 void TrackResiduals::print (std::ostream &stream) const
157 {
158  stream << "TrackResiduals" << std::endl;
159  std::ios_base::fmtflags flags = stream.flags();
160  stream.setf ( std::ios_base::hex, std::ios_base::basefield );
161  stream.setf ( std::ios_base::showbase );
162  for (int i = 0; i < numResiduals; i++) {
163  unsigned char residual = residuals_[i];
164  printf("0x%x\n", residual);
165 // stream << residual << std::endl;
166  }
167  stream.flags(flags);
168 }
169 
170 void TrackResiduals::print (const HitPattern &h, std::ostream &stream) const
171 {
172  stream << "TrackResiduals" << std::endl;
173  for (int i = 0; i < h.numberOfHits(); i++) {
174  stream << (h.validHitFilter(h.getHitPattern(i)) ?
175  "valid hit: " : "invalid hit: ") <<
176  "( " << residualX(i, h) << " , " << residualY(i, h) << " )" <<
177  std::endl;
178  }
179 }
type
Definition: HCALResponse.h:22
int i
Definition: DBlmapReader.cc:9
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
float sgn(float val)
Definition: FWPFMaths.cc:9
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
double residualY(int i, const HitPattern &) const
void setPullXY(int idx, double pullX, double pullY)
static double unpack_residual(unsigned char)
static int index_to_hitpattern(int i_hitpattern, const HitPattern &h)
static const double pull_char_to_double[8][2]
void setResidualType(enum ResidualType)
int numberOfHits() const
Definition: HitPattern.cc:312
unsigned char residuals_[numResiduals]
residuals, bitpacked two hits to a char
bool validHitFilter(uint32_t pattern) const
Definition: HitPattern.cc:264
static const double residual_char_to_double[8][2]
double residualX(int i, const HitPattern &) const
static unsigned char pack_residual(double)
void print(std::ostream &stream=std::cout) const
void setResidualXY(int idx, double residualX, double residualY)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
uint32_t getHitPattern(int position) const
Definition: HitPattern.cc:86
static double unpack_pull(unsigned char)
static unsigned char pack_pull(double)