CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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, mins and maxs
51  Saw += a*w;
52  Sbw += b*w;
53  Sw += w;
54  S1 += 1.;
55 
56  min_a = min(a, min_a);
57  min_b = min(b, min_b);
58  max_a = max(a, max_a);
59  max_b = max(b, max_b);
60 }
61 
62 //----------------------------------------------------------------------------------------------------
63 //----------------------------------------------------------------------------------------------------
64 
65 FastLineRecognition::FastLineRecognition(double cw_a, double cw_b) :
66  chw_a(cw_a/2.), chw_b(cw_b/2.), geometry(NULL)
67 {
68 }
69 
70 //----------------------------------------------------------------------------------------------------
71 
73 {
74 }
75 
76 //----------------------------------------------------------------------------------------------------
77 
79 {
80  // result already buffered?
81  map<unsigned int, GeomData>::iterator it = geometryMap.find(id);
82  if (it != geometryMap.end())
83  return it->second;
84 
85  // calculate it
86  CLHEP::Hep3Vector d = geometry->LocalToGlobalDirection(id, CLHEP::Hep3Vector(0., 1., 0.));
88  GeomData gd;
89  gd.z = c.z();
90  gd.s = d.x()*c.x() + d.y()*c.y();
91 
92  geometryMap[id] = gd;
93 
94  return gd;
95 }
96 
97 //----------------------------------------------------------------------------------------------------
98 
101 {
102  // build collection of points in the global coordinate system
103  std::vector<Point> points;
104  for (auto &ds : input)
105  {
106  unsigned int detId = ds.detId();
107 
108  for (auto &h : ds)
109  {
110  const TotemRPRecHit *hit = &h;
111  const GeomData &gd = getGeomData(detId);
112 
113  double p = hit->getPosition() + gd.s;
114  double z = gd.z - z0;
115  double w = sigma0 / hit->getSigma();
116 
117  points.push_back(Point(detId, hit, p, z, w));
118  }
119  }
120 
121 #if CTPPS_DEBUG > 0
122  printf(">> FastLineRecognition::getPatterns(z0 = %E)\n", z0);
123  printf(">>>>>>>>>>>>>>>>>>>\n");
124 #endif
125 
126  // reset output
127  patterns.clear();
128 
129  Cluster c;
130  while (getOneLine(points, threshold, c))
131  {
132  // convert cluster to pattern and save it
134  pattern.setA(c.Saw/c.Sw);
135  pattern.setB(c.Sbw/c.Sw);
136  pattern.setW(c.weight);
137 
138 #if CTPPS_DEBUG > 0
139  printf("\tpoints of the selected cluster: %lu\n", c.contents.size());
140 #endif
141 
142  for (auto &pit : c.contents)
143  {
144 #if CTPPS_DEBUG > 0
145  printf("\t\t%.1f\n", pit->z);
146 #endif
147  pattern.addHit(pit->detId, *(pit->hit));
148  }
149 
150  patterns.push_back(pattern);
151 
152 #if CTPPS_DEBUG > 0
153  unsigned int u_points_b = 0;
154  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit)
155  if (dit->usable)
156  u_points_b++;
157  printf("\tusable points before: %u\n", u_points_b);
158 #endif
159 
160  // remove points belonging to the recognized line
161  for (vector<const Point *>::iterator hit = c.contents.begin(); hit != c.contents.end(); ++hit)
162  {
163  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit)
164  {
165  //printf("\t\t1: %.2f, %p vs. 2: %.2f, %p\n", (*hit)->z, (*hit)->hit, dit->z, dit->hit);
166  if ((*hit)->hit == dit->hit)
167  {
168  dit->usable = false;
169  //points.erase(dit);
170  break;
171  }
172  }
173  }
174 
175 #if CTPPS_DEBUG > 0
176  unsigned int u_points_a = 0;
177  for (vector<Point>::iterator dit = points.begin(); dit != points.end(); ++dit)
178  if (dit->usable)
179  u_points_a++;
180  printf("\tusable points after: %u\n", u_points_a);
181 #endif
182  }
183 
184 #if CTPPS_DEBUG > 0
185  printf("patterns at end: %lu\n", patterns.size());
186  printf("<<<<<<<<<<<<<<<<<<<\n");
187 #endif
188 }
189 
190 //----------------------------------------------------------------------------------------------------
191 
192 bool FastLineRecognition::getOneLine(const vector<FastLineRecognition::Point> &points,
194 {
195 #if CTPPS_DEBUG > 0
196  printf("\tFastLineRecognition::getOneLine\n");
197 #endif
198 
199  if (points.size() < 2)
200  return false;
201 
202  vector<Cluster> clusters;
203 
204  // go through all the combinations of measured points
205  for (vector<Point>::const_iterator it1 = points.begin(); it1 != points.end(); ++it1)
206  {
207  if (!it1->usable)
208  continue;
209 
210  for (vector<Point>::const_iterator it2 = it1; it2 != points.end(); ++it2)
211  {
212  if (!it2->usable)
213  continue;
214 
215  const double &z1 = it1->z;
216  const double &z2 = it2->z;
217 
218  if (z1 == z2)
219  continue;
220 
221  const double &p1 = it1->h;
222  const double &p2 = it2->h;
223 
224  const double &w1 = it1->w;
225  const double &w2 = it2->w;
226 
227  // calculate intersection
228  double a = (p2 - p1) / (z2 - z1);
229  double b = p1 - z1 * a;
230  double w = w1 + w2;
231 
232 #if CTPPS_DEBUG > 0
233  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);
234 #endif
235 
236  // add it to the appropriate cluster
237  bool newCluster = true;
238  for (unsigned int k = 0; k < clusters.size(); k++)
239  {
240  Cluster &c = clusters[k];
241  if (c.S1 < 1. || c.Sw <= 0.)
242  continue;
243 
244 #if CTPPS_DEBUG > 0
245  if (k < 10)
246  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,
247  chw_a, chw_b,
248  (std::abs(a - c.Saw/c.Sw) < chw_a), (std::abs(b - c.Sbw/c.Sw) < chw_b));
249 #endif
250 
251  if ((std::abs(a - c.Saw/c.Sw) < chw_a) && (std::abs(b - c.Sbw/c.Sw) < chw_b))
252  {
253  newCluster = false;
254  clusters[k].add(& (*it1), & (*it2), a, b, w);
255 #if CTPPS_DEBUG > 0
256  printf("\t\t\t\t--> cluster %u\n", k);
257 #endif
258  break;
259  }
260  }
261 
262  // make new cluster
263  if (newCluster)
264  {
265 #if CTPPS_DEBUG > 0
266  printf("\t\t\t\t--> new cluster %lu\n", clusters.size());
267 #endif
268  clusters.push_back(Cluster());
269  clusters.back().add(& (*it1), & (*it2), a, b, w);
270  }
271  }
272  }
273 
274 #if CTPPS_DEBUG > 0
275  printf("\t\tclusters: %lu\n", clusters.size());
276 #endif
277 
278  // find the cluster with highest weight
279  unsigned int mk = 0;
280  double mw = -1.;
281  for (unsigned int k = 0; k < clusters.size(); k++)
282  {
283  double w = 0;
284  for (vector<const Point *>::iterator it = clusters[k].contents.begin(); it != clusters[k].contents.end(); ++it)
285  w += (*it)->w;
286  clusters[k].weight = w;
287 
288  if (w > mw)
289  {
290  mw = w;
291  mk = k;
292  }
293  }
294 
295 #if CTPPS_DEBUG > 0
296  printf("\t\tmw = %.1f, mk = %u\n", mw, mk);
297 #endif
298 
299  // rerturn result
300  if (mw >= threshold)
301  {
302  result = clusters[mk];
303 
304  return true;
305  } else
306  return false;
307 }
308 
void addHit(edm::det_id_type detId, const TotemRPRecHit &hit)
double chw_a
cluster half widths in a and b
PhiMemoryImage patterns[9]
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
#define NULL
Definition: scimark2.h:8
cluster of intersection points
DetGeomDesc * GetDetector(unsigned int) const
returns geometry of a detector performs necessary checks, returns NULL if fails input is raw ID ...
void getPatterns(const edm::DetSetVector< TotemRPRecHit > &input, double _z0, double threshold, edm::DetSet< TotemRPUVPattern > &patterns)
std::pair< double, double > Point
Definition: CaloEllipse.h:18
static std::string const input
Definition: EdmProvDump.cc:44
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > DDTranslation
Definition: DDTranslation.h:7
tuple result
Definition: mps_fire.py:83
bool getOneLine(const std::vector< Point > &points, double threshold, Cluster &result)
size_type size() const
Definition: DetSet.h:63
void add(const Point *p1, const Point *p2, double a, double b, double w)
tuple d
Definition: ztail.py:151
double z0
&quot;typical&quot; z
std::map< unsigned int, GeomData > geometryMap
map: raw detector id –&gt; GeomData
T sqrt(T t)
Definition: SSEVec.h:18
DDTranslation translation() const
Definition: DetGeomDesc.h:87
Reconstructed hit in TOTEM RP.
Definition: TotemRPRecHit.h:18
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void setW(double w_)
T min(T a, T b)
Definition: MathUtil.h:58
CLHEP::Hep3Vector LocalToGlobalDirection(unsigned int id, const CLHEP::Hep3Vector dir) const
double p2[4]
Definition: TauolaWrapper.h:90
void setB(double b_)
std::vector< const Point * > contents
A linear pattern in U or V projection. The intercept b is taken at the middle of a RP: (geometry-&gt;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)
ESHandle< TrackerGeometry > geometry
double p1[4]
Definition: TauolaWrapper.h:89
if(dp >Float(M_PI)) dp-
double a
Definition: hdecay.h:121
void clear()
Definition: DetSet.h:69
void setA(double a_)
double getPosition() const
Definition: TotemRPRecHit.h:25
const TotemRPGeometry * geometry
pointer to the geometry