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 {
33  // which points to be added to contents?
34  bool add1 = true, add2 = true;
35  for (vector<const Point *>::const_iterator it = contents.begin(); it != contents.end() && (add1 || add2); ++it)
36  {
37  if ((*it)->hit == p1->hit)
38  add1 = false;
39 
40  if ((*it)->hit == p2->hit)
41  add2 = false;
42  }
43 
44  // add the points
45  if (add1)
46  contents.push_back(p1);
47  if (add2)
48  contents.push_back(p2);
49 
50  // update sums
51  Saw += a*w;
52  Sbw += b*w;
53  Sw += w;
54  S1 += 1.;
55 }
56 
57 //----------------------------------------------------------------------------------------------------
58 //----------------------------------------------------------------------------------------------------
59 
60 FastLineRecognition::FastLineRecognition(double cw_a, double cw_b) :
61  chw_a(cw_a/2.), chw_b(cw_b/2.), geometry(nullptr)
62 {
63 }
64 
65 //----------------------------------------------------------------------------------------------------
66 
68 {
69 }
70 
71 //----------------------------------------------------------------------------------------------------
72 
74 {
75  // result already buffered?
76  map<unsigned int, GeomData>::iterator it = geometryMap.find(id);
77  if (it != geometryMap.end())
78  return it->second;
79 
80  // calculate it
81  CLHEP::Hep3Vector d = geometry->localToGlobalDirection(id, CLHEP::Hep3Vector(0., 1., 0.));
82  DDTranslation c = geometry->getSensor(TotemRPDetId(id))->translation();
83  GeomData gd;
84  gd.z = c.z();
85  gd.s = d.x()*c.x() + d.y()*c.y();
86 
87  geometryMap[id] = gd;
88 
89  return gd;
90 }
91 
92 //----------------------------------------------------------------------------------------------------
93 
95  double threshold, DetSet<TotemRPUVPattern> &patterns)
96 {
97  // build collection of points in the global coordinate system
98  std::vector<Point> points;
99  for (auto &ds : input)
100  {
101  unsigned int detId = ds.detId();
102 
103  for (auto &h : ds)
104  {
105  const TotemRPRecHit *hit = &h;
106  const GeomData &gd = getGeomData(detId);
107 
108  double p = hit->getPosition() + gd.s;
109  double z = gd.z - z0;
110  double w = sigma0 / hit->getSigma();
111 
112  points.push_back(Point(detId, hit, p, z, w));
113  }
114  }
115 
116 #if CTPPS_DEBUG > 0
117  printf(">> FastLineRecognition::getPatterns(z0 = %E)\n", z0);
118  printf(">>>>>>>>>>>>>>>>>>>\n");
119 #endif
120 
121  // reset output
122  patterns.clear();
123 
124  Cluster c;
125  while (getOneLine(points, threshold, c))
126  {
127  // convert cluster to pattern and save it
129  pattern.setA(c.Saw/c.Sw);
130  pattern.setB(c.Sbw/c.Sw);
131  pattern.setW(c.weight);
132 
133 #if CTPPS_DEBUG > 0
134  printf("\tpoints of the selected cluster: %lu\n", c.contents.size());
135 #endif
136 
137  for (auto &pit : c.contents)
138  {
139 #if CTPPS_DEBUG > 0
140  printf("\t\t%.1f\n", pit->z);
141 #endif
142  pattern.addHit(pit->detId, *(pit->hit));
143  }
144 
145  patterns.push_back(pattern);
146 
147 #if CTPPS_DEBUG > 0
148  unsigned int u_points_b = 0;
149  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit)
150  if (dit->usable)
151  u_points_b++;
152  printf("\tusable points before: %u\n", u_points_b);
153 #endif
154 
155  // remove points belonging to the recognized line
156  for (vector<const Point *>::iterator hit = c.contents.begin(); hit != c.contents.end(); ++hit)
157  {
158  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit)
159  {
160  //printf("\t\t1: %.2f, %p vs. 2: %.2f, %p\n", (*hit)->z, (*hit)->hit, dit->z, dit->hit);
161  if ((*hit)->hit == dit->hit)
162  {
163  dit->usable = false;
164  //points.erase(dit);
165  break;
166  }
167  }
168  }
169 
170 #if CTPPS_DEBUG > 0
171  unsigned int u_points_a = 0;
172  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit)
173  if (dit->usable)
174  u_points_a++;
175  printf("\tusable points after: %u\n", u_points_a);
176 #endif
177  }
178 
179 #if CTPPS_DEBUG > 0
180  printf("patterns at end: %lu\n", patterns.size());
181  printf("<<<<<<<<<<<<<<<<<<<\n");
182 #endif
183 }
184 
185 //----------------------------------------------------------------------------------------------------
186 
187 bool FastLineRecognition::getOneLine(const vector<FastLineRecognition::Point> &points,
189 {
190 #if CTPPS_DEBUG > 0
191  printf("\tFastLineRecognition::getOneLine\n");
192 #endif
193 
194  if (points.size() < 2)
195  return false;
196 
197  vector<Cluster> clusters;
198 
199  // go through all the combinations of measured points
200  for (vector<Point>::const_iterator it1 = points.begin(); it1 != points.end(); ++it1)
201  {
202  if (!it1->usable)
203  continue;
204 
205  for (vector<Point>::const_iterator it2 = it1; it2 != points.end(); ++it2)
206  {
207  if (!it2->usable)
208  continue;
209 
210  const double &z1 = it1->z;
211  const double &z2 = it2->z;
212 
213  if (z1 == z2)
214  continue;
215 
216  const double &p1 = it1->h;
217  const double &p2 = it2->h;
218 
219  const double &w1 = it1->w;
220  const double &w2 = it2->w;
221 
222  // calculate intersection
223  double a = (p2 - p1) / (z2 - z1);
224  double b = p1 - z1 * a;
225  double w = w1 + w2;
226 
227 #if CTPPS_DEBUG > 0
228  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", z1, z2, p1, p2, a, b, w);
229 #endif
230 
231  // add it to the appropriate cluster
232  bool newCluster = true;
233  for (unsigned int k = 0; k < clusters.size(); k++)
234  {
235  Cluster &c = clusters[k];
236  if (c.S1 < 1. || c.Sw <= 0.)
237  continue;
238 
239 #if CTPPS_DEBUG > 0
240  if (k < 10)
241  printf("\t\t\t\ttest cluster %u at a=%+6.3f, b=%+6.3f : %+6.3f, %+6.3f : %i, %i\n", k, c.Saw/c.Sw, c.Sbw/c.Sw,
242  chw_a, chw_b,
243  (std::abs(a - c.Saw/c.Sw) < chw_a), (std::abs(b - c.Sbw/c.Sw) < chw_b));
244 #endif
245 
246  if ((std::abs(a - c.Saw/c.Sw) < chw_a) && (std::abs(b - c.Sbw/c.Sw) < chw_b))
247  {
248  newCluster = false;
249  clusters[k].add(& (*it1), & (*it2), a, b, w);
250 #if CTPPS_DEBUG > 0
251  printf("\t\t\t\t--> cluster %u\n", k);
252 #endif
253  break;
254  }
255  }
256 
257  // make new cluster
258  if (newCluster)
259  {
260 #if CTPPS_DEBUG > 0
261  printf("\t\t\t\t--> new cluster %lu\n", clusters.size());
262 #endif
263  clusters.push_back(Cluster());
264  clusters.back().add(& (*it1), & (*it2), a, b, w);
265  }
266  }
267  }
268 
269 #if CTPPS_DEBUG > 0
270  printf("\t\tclusters: %lu\n", clusters.size());
271 #endif
272 
273  // find the cluster with highest weight
274  unsigned int mk = 0;
275  double mw = -1.;
276  for (unsigned int k = 0; k < clusters.size(); k++)
277  {
278  double w = 0;
279  for (vector<const Point *>::iterator it = clusters[k].contents.begin(); it != clusters[k].contents.end(); ++it)
280  w += (*it)->w;
281  clusters[k].weight = w;
282 
283  if (w > mw)
284  {
285  mw = w;
286  mk = k;
287  }
288  }
289 
290 #if CTPPS_DEBUG > 0
291  printf("\t\tmw = %.1f, mk = %u\n", mw, mk);
292 #endif
293 
294  // rerturn result
295  if (mw >= threshold)
296  {
297  result = clusters[mk];
298 
299  return true;
300  } else
301  return false;
302 }
303 
Detector ID class for TOTEM Si strip detectors.
Definition: TotemRPDetId.h:30
void addHit(edm::det_id_type detId, const TotemRPRecHit &hit)
double chw_a
cluster half widths in a and b
void push_back(const T &t)
Definition: DetSet.h:68
double getSigma() const
Definition: TotemRPRecHit.h:28
common ppss p3p6s2 common epss epspn46 common const1 w2
Definition: inclppp.h:1
const double w
Definition: UKUtility.cc:23
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
const TotemRPRecHit * hit
pointer to original reco hit
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)
#define nullptr
std::pair< double, double > Point
Definition: CaloEllipse.h:18
static std::string const input
Definition: EdmProvDump.cc:45
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > DDTranslation
Definition: DDTranslation.h:7
bool getOneLine(const std::vector< Point > &points, double threshold, Cluster &result)
size_type size() const
Definition: DetSet.h:63
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:18
Reconstructed hit in TOTEM RP.
Definition: TotemRPRecHit.h:18
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void setW(double w_)
double p2[4]
Definition: TauolaWrapper.h:90
void setB(double b_)
std::vector< const Point * > contents
int k[5][pyjets_maxn]
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 p1[4]
Definition: TauolaWrapper.h:89
double a
Definition: hdecay.h:121
void clear()
Definition: DetSet.h:69
void setA(double a_)
double getPosition() const
Definition: TotemRPRecHit.h:25