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 
12  residualType(X_Y_RESIDUALS)
13 {
14  memset(residuals_, 0, sizeof(residuals_));
15 }
16 
18  residualType(type)
19 {
20  memset(residuals_, 0, sizeof(residuals_));
21 }
22 
24 {
26 }
27 
28 void TrackResiduals::setResidualXY (int idx, double residualX, double residualY)
29 {
30  assert(residualType == X_Y_RESIDUALS);
31  if (idx >= numResiduals) {
32  edm::LogWarning("TrackResiduals") << " setting residual over the array size.";
33  return;
34  }
35  residuals_[idx] = (pack_residual(residualX) << 4) | pack_residual(residualY);
36 }
37 
38 double TrackResiduals::residualX (int i) const
39 {
40  switch (residualType) {
41  case X_Y_RESIDUALS:
42  return unpack_residual(residuals_[i] >> 4);
43  case X_Y_PULLS:
44  return unpack_pull(residuals_[i] >> 4);
45  default:
46  assert(0);
47  }
48  return 0;
49 }
50 
51 double TrackResiduals::residualY (int i) const
52 {
53  switch (residualType) {
54  case X_Y_RESIDUALS:
55  return unpack_residual(residuals_[i] & 0x0f);
56  case X_Y_PULLS:
57  return unpack_pull(residuals_[i] & 0x0f);
58  default:
59  assert(0);
60  }
61  return 0;
62 }
63 
64 static int index_to_hitpattern (int i_hitpattern, const HitPattern &h)
65 {
66  int i_residuals = 0;
67  assert(i_hitpattern < h.numberOfHits(HitPattern::TRACK_HITS));
68  if (!h.validHitFilter(h.getHitPattern(HitPattern::TRACK_HITS, i_hitpattern))) {
69  // asking for residual of invalid hit...
70  return -999;
71  }
72 
73  for (int i = 0; i <= i_hitpattern; i++) {
75  i_residuals++;
76  }
77  }
78 
79  assert(i_residuals > 0);
80  return i_residuals - 1;
81 }
82 
83 double TrackResiduals::residualX (int i, const HitPattern &h) const
84 {
85  int idx = index_to_hitpattern(i, h);
86  if (idx == -999) {
87  return -999;
88  }
89 
90  return residualX(idx);
91 }
92 
93 double TrackResiduals::residualY (int i, const HitPattern &h) const
94 {
95  int idx = index_to_hitpattern(i, h);
96  if (idx == -999) {
97  return -999;
98  }
99 
100  return residualY(idx);
101 }
102 
103 void TrackResiduals::setPullXY (int idx, double pullX, double pullY)
104 {
105  assert(residualType == X_Y_PULLS);
106  if (idx >= numResiduals) {
107  edm::LogWarning("TrackResiduals") << " setting pulls over the array size.";
108  return;
109  }
110 
111  residuals_[idx] = (pack_pull(pullX) << 4) | pack_pull(pullY);
112 }
113 
114 static const double pull_char_to_double[8][2] = {
115  { 0, 0.5 },
116  { 0.5, 1 },
117  { 1, 1.5 },
118  { 1.5, 2 },
119  { 2, 2.5 },
120  { 2.5, 3.5 },
121  { 3.5, 4.5 },
122  { 4.5, 5.5 },
123 };
124 
125 double TrackResiduals::unpack_pull (unsigned char pull)
126 {
127  int sgn = 1 - 2 * ((pull & 0x08) >> 3);
128  unsigned char mag = pull & 0x07;
129  return sgn * (pull_char_to_double[mag][0] + pull_char_to_double[mag][1]) / 2;
130 }
131 
132 unsigned char TrackResiduals::pack_pull (double pull)
133 {
134  unsigned char sgn = (pull < 0) * 0x08; // 1xxx is -abs(0xxx)
135  int mag = -1;
136  while (++mag < 8 && pull_char_to_double[mag][1] < fabs(pull));
137  return sgn + mag;
138 }
139 
140 
141 double TrackResiduals::unpack_residual (unsigned char pull)
142 {
143  int sgn = 1 - 2 * ((pull & 0x08) >> 3);
144  unsigned char mag = pull & 0x07;
145  return sgn * (pull_char_to_double[mag][0] + pull_char_to_double[mag][1]) / 2;
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  // just counting
154  ;
155  }
156  return sgn + mag;
157 }
158 
159 void TrackResiduals::print (std::ostream &stream) const
160 {
161  stream << "TrackResiduals" << std::endl;
162  std::ios_base::fmtflags flags = stream.flags();
163  stream.setf (std::ios_base::hex, std::ios_base::basefield);
164  stream.setf (std::ios_base::showbase);
165 
166  for (int i = 0; i < numResiduals; i++) {
167  unsigned char residual = residuals_[i];
168  printf("0x%x\n", residual);
169  //stream << residual << std::endl;
170  }
171 
172  stream.flags(flags);
173 }
174 
175 void TrackResiduals::print (const HitPattern &h, std::ostream &stream) const
176 {
177  stream << "TrackResiduals" << std::endl;
178  for (int i = 0; i < h.numberOfHits(HitPattern::TRACK_HITS); i++) {
179  stream << (h.validHitFilter(h.getHitPattern(HitPattern::TRACK_HITS, i)) ? "valid hit: " : "invalid hit: ")
180  << "( " << residualX(i, h) << " , " << residualY(i, h) << " )"
181  << std::endl;
182  }
183 }
184 
type
Definition: HCALResponse.h:21
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 bool validHitFilter(uint16_t pattern)
Definition: HitPattern.h:701
static int index_to_hitpattern(int i_hitpattern, const HitPattern &h)
static const double pull_char_to_double[8][2]
void setResidualType(enum ResidualType)
unsigned char residuals_[numResiduals]
residuals, bitpacked two hits to a char
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
double residualX(int i, const HitPattern &) const
static unsigned char pack_residual(double)
void print(std::ostream &stream=std::cout) const
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
void setResidualXY(int idx, double residualX, double residualY)
uint16_t getHitPattern(HitCategory category, int position) const
Definition: HitPattern.h:448
static double unpack_pull(unsigned char)
int numberOfHits(HitCategory category) const
Definition: HitPattern.h:721
static unsigned char pack_pull(double)