CMS 3D CMS Logo

FastLineRecognition.cc
Go to the documentation of this file.
1 /****************************************************************************
2 *
3 * This is a part of TOTEM offline software.
4 * Authors:
5 * Jan Kašpar (jan.kaspar@gmail.com)
6 *
7 ****************************************************************************/
8 
10 
12 
14 
15 #include <map>
16 #include <cmath>
17 #include <cstdio>
18 #include <algorithm>
19 
20 //#define CTPPS_DEBUG 1
21 
22 using namespace std;
23 using namespace edm;
24 
25 //----------------------------------------------------------------------------------------------------
26 
27 const double FastLineRecognition::sigma0 = 66E-3 / sqrt(12.);
28 
29 //----------------------------------------------------------------------------------------------------
30 
31 void FastLineRecognition::Cluster::add(const Point *p1, const Point *p2, double a, double b, double w) {
32  // which points to be added to contents?
33  bool add1 = true, add2 = true;
34  for (vector<const Point *>::const_iterator it = contents.begin(); it != contents.end() && (add1 || add2); ++it) {
35  if ((*it)->hit == p1->hit)
36  add1 = false;
37 
38  if ((*it)->hit == p2->hit)
39  add2 = false;
40  }
41 
42  // add the points
43  if (add1)
44  contents.push_back(p1);
45  if (add2)
46  contents.push_back(p2);
47 
48  // update sums
49  Saw += a * w;
50  Sbw += b * w;
51  Sw += w;
52  S1 += 1.;
53 }
54 
55 //----------------------------------------------------------------------------------------------------
56 //----------------------------------------------------------------------------------------------------
57 
59  : chw_a(cw_a / 2.), chw_b(cw_b / 2.), geometry(nullptr) {}
60 
61 //----------------------------------------------------------------------------------------------------
62 
64 
65 //----------------------------------------------------------------------------------------------------
66 
68  // result already buffered?
69  map<unsigned int, GeomData>::iterator it = geometryMap.find(id);
70  if (it != geometryMap.end())
71  return it->second;
72 
73  // calculate it
74  const auto &d = geometry->localToGlobalDirection(id, CTPPSGeometry::Vector(0., 1., 0.));
75  DetGeomDesc::Translation c = geometry->sensor(TotemRPDetId(id))->translation();
76  GeomData gd;
77  gd.z = c.z();
78  gd.s = d.x() * c.x() + d.y() * c.y();
79 
80  geometryMap[id] = gd;
81 
82  return gd;
83 }
84 
85 //----------------------------------------------------------------------------------------------------
86 
88  double z0,
89  double threshold,
90  DetSet<TotemRPUVPattern> &patterns) {
91  // build collection of points in the global coordinate system
92  std::vector<Point> points;
93  for (auto &ds : input) {
94  unsigned int detId = ds.detId();
95 
96  for (auto &h : ds) {
97  const TotemRPRecHit *hit = &h;
98  const GeomData &gd = getGeomData(detId);
99 
100  double p = hit->position() + gd.s;
101  double z = gd.z - z0;
102  double w = sigma0 / hit->sigma();
103 
104  points.push_back(Point(detId, hit, p, z, w));
105  }
106  }
107 
108 #if CTPPS_DEBUG > 0
109  printf(">> FastLineRecognition::getPatterns(z0 = %E)\n", z0);
110  printf(">>>>>>>>>>>>>>>>>>>\n");
111 #endif
112 
113  // reset output
114  patterns.clear();
115 
116  Cluster c;
117  while (getOneLine(points, threshold, c)) {
118  // convert cluster to pattern and save it
120  pattern.setA(c.Saw / c.Sw);
121  pattern.setB(c.Sbw / c.Sw);
122  pattern.setW(c.weight);
123 
124 #if CTPPS_DEBUG > 0
125  printf("\tpoints of the selected cluster: %lu\n", c.contents.size());
126 #endif
127 
128  for (auto &pit : c.contents) {
129 #if CTPPS_DEBUG > 0
130  printf("\t\t%.1f\n", pit->z);
131 #endif
132  pattern.addHit(pit->detId, *(pit->hit));
133  }
134 
135  patterns.push_back(pattern);
136 
137 #if CTPPS_DEBUG > 0
138  unsigned int u_points_b = 0;
139  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit)
140  if (dit->usable)
141  u_points_b++;
142  printf("\tusable points before: %u\n", u_points_b);
143 #endif
144 
145  // remove points belonging to the recognized line
146  for (vector<const Point *>::iterator hit = c.contents.begin(); hit != c.contents.end(); ++hit) {
147  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit) {
148  //printf("\t\t1: %.2f, %p vs. 2: %.2f, %p\n", (*hit)->z, (*hit)->hit, dit->z, dit->hit);
149  if ((*hit)->hit == dit->hit) {
150  dit->usable = false;
151  //points.erase(dit);
152  break;
153  }
154  }
155  }
156 
157 #if CTPPS_DEBUG > 0
158  unsigned int u_points_a = 0;
159  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit)
160  if (dit->usable)
161  u_points_a++;
162  printf("\tusable points after: %u\n", u_points_a);
163 #endif
164  }
165 
166 #if CTPPS_DEBUG > 0
167  printf("patterns at end: %lu\n", patterns.size());
168  printf("<<<<<<<<<<<<<<<<<<<\n");
169 #endif
170 }
171 
172 //----------------------------------------------------------------------------------------------------
173 
174 bool FastLineRecognition::getOneLine(const vector<FastLineRecognition::Point> &points,
175  double threshold,
177 #if CTPPS_DEBUG > 0
178  printf("\tFastLineRecognition::getOneLine\n");
179 #endif
180 
181  if (points.size() < 2)
182  return false;
183 
184  vector<Cluster> clusters;
185 
186  // go through all the combinations of measured points
187  for (vector<Point>::const_iterator it1 = points.begin(); it1 != points.end(); ++it1) {
188  if (!it1->usable)
189  continue;
190 
191  for (vector<Point>::const_iterator it2 = it1; it2 != points.end(); ++it2) {
192  if (!it2->usable)
193  continue;
194 
195  const double &z1 = it1->z;
196  const double &z2 = it2->z;
197 
198  if (z1 == z2)
199  continue;
200 
201  const double &p1 = it1->h;
202  const double &p2 = it2->h;
203 
204  const double &w1 = it1->w;
205  const double &w2 = it2->w;
206 
207  // calculate intersection
208  double a = (p2 - p1) / (z2 - z1);
209  double b = p1 - z1 * a;
210  double w = w1 + w2;
211 
212 #if CTPPS_DEBUG > 0
213  printf("\t\t\tz: 1=%+5.1f, 2=%+5.1f | U/V: 1=%+6.3f, 2=%+6.3f | a=%+6.3f rad, b=%+6.3f mm, w=%.1f\n",
214  z1,
215  z2,
216  p1,
217  p2,
218  a,
219  b,
220  w);
221 #endif
222 
223  // add it to the appropriate cluster
224  bool newCluster = true;
225  for (unsigned int k = 0; k < clusters.size(); k++) {
226  Cluster &c = clusters[k];
227  if (c.S1 < 1. || c.Sw <= 0.)
228  continue;
229 
230 #if CTPPS_DEBUG > 0
231  if (k < 10)
232  printf("\t\t\t\ttest cluster %u at a=%+6.3f, b=%+6.3f : %+6.3f, %+6.3f : %i, %i\n",
233  k,
234  c.Saw / c.Sw,
235  c.Sbw / c.Sw,
236  chw_a,
237  chw_b,
238  (std::abs(a - c.Saw / c.Sw) < chw_a),
239  (std::abs(b - c.Sbw / c.Sw) < chw_b));
240 #endif
241 
242  if ((std::abs(a - c.Saw / c.Sw) < chw_a) && (std::abs(b - c.Sbw / c.Sw) < chw_b)) {
243  newCluster = false;
244  clusters[k].add(&(*it1), &(*it2), a, b, w);
245 #if CTPPS_DEBUG > 0
246  printf("\t\t\t\t--> cluster %u\n", k);
247 #endif
248  break;
249  }
250  }
251 
252  // make new cluster
253  if (newCluster) {
254 #if CTPPS_DEBUG > 0
255  printf("\t\t\t\t--> new cluster %lu\n", clusters.size());
256 #endif
257  clusters.push_back(Cluster());
258  clusters.back().add(&(*it1), &(*it2), a, b, w);
259  }
260  }
261  }
262 
263 #if CTPPS_DEBUG > 0
264  printf("\t\tclusters: %lu\n", clusters.size());
265 #endif
266 
267  // find the cluster with highest weight
268  unsigned int mk = 0;
269  double mw = -1.;
270  for (unsigned int k = 0; k < clusters.size(); k++) {
271  double w = 0;
272  for (vector<const Point *>::iterator it = clusters[k].contents.begin(); it != clusters[k].contents.end(); ++it)
273  w += (*it)->w;
274  clusters[k].weight = w;
275 
276  if (w > mw) {
277  mw = w;
278  mk = k;
279  }
280  }
281 
282 #if CTPPS_DEBUG > 0
283  printf("\t\tmw = %.1f, mk = %u\n", mw, mk);
284 #endif
285 
286  // rerturn result
287  if (mw >= threshold) {
288  result = clusters[mk];
289 
290  return true;
291  } else
292  return false;
293 }
Detector ID class for TOTEM Si strip detectors.
Definition: TotemRPDetId.h:30
double chw_a
cluster half widths in a and b
void push_back(const T &t)
Definition: DetSet.h:66
common ppss p3p6s2 common epss epspn46 common const1 w2
Definition: inclppp.h:1
T w() const
double s
sensor&#39;s centre projected to its read-out direction
cluster of intersection points
void getPatterns(const edm::DetSetVector< TotemRPRecHit > &input, double _z0, double threshold, edm::DetSet< TotemRPUVPattern > &patterns)
DetGeomDesc::Translation Vector
Definition: CTPPSGeometry.h:36
static std::string const input
Definition: EdmProvDump.cc:50
bool getOneLine(const std::vector< Point > &points, double threshold, Cluster &result)
double threshold
weight threshold for accepting pattern candidates (clusters)
void add(const Point *p1, const Point *p2, double a, double b, double w)
double z0
"typical" z
std::map< unsigned int, GeomData > geometryMap
map: raw detector id –> GeomData
T sqrt(T t)
Definition: SSEVec.h:19
math::XYZPoint Point
Reconstructed hit in TOTEM RP.
Definition: TotemRPRecHit.h:18
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
d
Definition: ztail.py:151
A linear pattern in U or V projection. The intercept b is taken at the middle of a RP: (geometry->Get...
static const double sigma0
the uncertainty of 1-hit cluster, in mm
GeomData getGeomData(unsigned int id)
expects raw detector id
FastLineRecognition(double cw_a=0., double cw_b=0.)
double b
Definition: hdecay.h:120
double z
z position of a sensor (wrt. IP)
HLT enums.
double a
Definition: hdecay.h:121
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > Translation
Definition: DetGeomDesc.h:55
void clear()
Definition: DetSet.h:71
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
if(threadIdxLocalY==0 &&threadIdxLocalX==0)
size_type size() const
Definition: DetSet.h:61