ATLAS Offline Software
cyl_geom.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // emacs this is -*-C++-*-
6 #ifndef _CYL_GEOM_HH_
7 #define _CYL_GEOM_HH_
8 
17 #include <set>
18 #include <list>
19 #include <vector>
20 #include <cmath>
21 
22 namespace JetGeom {
23 
25 class point_t : public std::pair<float , float>{
26 public:
27  point_t(float x, float y){first = x; second = y;}
28 };
29 class point_set_t : public std::set<point_t> {};
30 class point_vect_t : public std::vector<point_t> {};
31 
32 typedef std::list<point_t> point_list_t;
33 
34 
37 class line_t {
38 public:
39 
43  line_t(float cx, float cy, float cc, bool to_r) : m_cx(cx),m_cy(cy),m_cc(cc), m_oriented_r(to_r) {};
46 
47  float m_cx, m_cy, m_cc;
49 
50 
51  bool is_above(point_t &p);
52  bool is_below(point_t &p);
53 
54  bool is_left(point_t &p) {return m_oriented_r ? is_above(p) : is_below(p);};
55  bool is_right(point_t &p) {return m_oriented_r ? is_below(p) : is_above(p);};
56 
57  void phi_shift(float dphi) {m_cc = m_cc - m_cy*dphi;}
58 
59  point_t intercept_y(float y);
60  point_t intercept_x(float x);
61 
62 };
63 
64 
65 
66 
69 template< class inT, class ouT>
70 void findConvexHull(inT &inSet, ouT &outSet );
72 template<class ouT>
73 void findConvexHull(point_set_t &inSet, ouT &outSet );
74 // internally used by findConvexHull
75 template< class inT>
76 void _findConvexHull(point_set_t &inSet, inT &outSet );
77 
78 // internally used by findConvexHull
79 void testHullLine(point_list_t &hull, point_t p);
80 
81 
82 template<class T>
83 float polygon_area(T& line);
84 
85 template<class T>
86 float polygon_lenght(T& line);
87 
88 
89 
90 // *******************************************************
91 // Cylindrical geometry **********************************
92 // *******************************************************
93 
98 template <class T>
99 float getMeanPhi(T &set);
101 template<class T>
102 float max_deltaR(point_t p, T& set);
103 
104 
106 template<class T , class T2>
107 void recenter_set(T &inSet, T2 &outSet, float phicenter);
110 template<class T , class T2>
111 void recenter_set(T &inSet, T2 &outSet);
112 
113 
115 inline float in_mPI_pPI(float phi);
117 inline void fix2pi(point_t &p);
119 inline float deltaR(point_t &p1, point_t &p2);
120 inline float deltaR2(point_t &p1, point_t &p2);
121 
122 inline float deltaPhi(point_t &p1, point_t &p2);
123 inline float deltaPhi(float phi1, float phi2);
124 
125 
126 
127 // *******************************************************
128 // Utils function **********************************
129 // *******************************************************
130 void listToSet(point_list_t &inList, point_set_t &outSet);
131  template<class T>
132  void clear_delete(T &container);
133  template<class T>
134  void delete_content(T &container);
135 
136 
137 
138 
139 // ***************************************************************************
140 
141 // ***********************************************
142 // Inlined and template implementations ****************************
143 // ***********************************************
144 #ifndef G__DICTIONARY
145 
146 
147 template<class T>
148 void clear_delete(T &container){
149  typedef typename T::iterator it_t;
150  it_t it = container.begin();
151  it_t itE = container.end();
152  for(; it != itE; ++it) delete *it;
153  container.clear();
154 }
155 template<class T>
156 void delete_content(T &container){
157  typedef typename T::iterator it_t;
158  it_t it = container.begin();
159  it_t itE = container.end();
160  for(; it != itE; ++it) delete *it;
161 }
162 
163 template<class T>
164 float max_deltaR(point_t p, T& set){
165  typedef typename T::iterator it_t;
166  it_t it = set.begin();
167  it_t itE = set.end();
168  float r=0;
169  for(; it != itE; ++it){
170  float t = deltaR(p, *it);
171  if(t>r) r=t;
172  }
173  return r;
174 }
175 
176 template<class T>
177 float polygon_area(T& line){
178  typedef typename T::iterator it_t;
179  it_t it = line.begin();
180  it_t itE = line.end();
181  float a=0;
182  for(; it != itE; ++it){
183  it_t itp=it;
184  ++itp;
185  if(itp == itE) itp = line.begin();
186  a += ( (*it).first*(*itp).second - (*itp).first*(*it).second );
187  }
188  return a/2;
189 }
190 
191 template<class T>
193  typedef typename T::iterator it_t;
194  it_t it = line.begin();
195  it_t itE = line.end();
196  float a=0;
197  for(; it != itE; ++it){
198  it_t itp=it;
199  ++itp;
200  if(itp == itE) itp = line.begin();
201  a += deltaR(*it,*itp);
202  }
203  return a;
204 }
205 
206 inline float abs_dphi(float phi1, float phi2){
207  float d = fabs(phi1 - phi2);
208  return d < M_PI ? d: fabs(d-2*M_PI);
209 }
210 inline float deltaPhi(float phi1, float phi2){
211  return in_mPI_pPI(phi1-phi2);
212 }
213 inline float deltaPhi(point_t &p1, point_t &p2){
214  return deltaPhi(p1.second,p2.second);
215 }
216 inline float in_mPI_pPI(float phi){
217  while(phi < -M_PI) phi += 2*M_PI;
218  while(phi >= M_PI) phi -= 2*M_PI;
219  return phi;
220 }
221 inline void fix2pi(point_t &p){
222  while(p.second < -M_PI) p.second += 2*M_PI;
223  while(p.second >= M_PI) p.second -= 2*M_PI;
224 }
225 
226 inline point_t recenter(const point_t &p, const point_t &center){
227  point_t n(p.first, p.second - center.second);
228  fix2pi(n);
229  return n;
230 }
231 inline point_t recenter(const point_t &p, float phicenter){
232  point_t n(p.first, p.second - phicenter);
233  fix2pi(n);
234  return n;
235 }
236 inline float deltaR2(point_t &p1, point_t &p2){
237  return (p1.first-p2.first)*(p1.first-p2.first) +
238  abs_dphi(p1.second,p2.second)*abs_dphi(p1.second,p2.second) ;
239 }
240 inline float deltaR(point_t &p1, point_t &p2){
241  return sqrt( deltaR2(p1,p2) );
242 }
243 
244 template<class T , class T2>
245 void recenter_set(T &inSet, T2 &outSet, float phicenter){
246  typedef typename T::iterator it_t;
247  typedef typename T2::iterator it_t2;
248  it_t it = inSet.begin();
249  it_t itE = inSet.end();
250  it_t2 it2 = outSet.begin();
251  for(; it!= itE; ++it){
252  it2 = outSet.insert(it2,recenter(*it,phicenter));
253  }
254 }
255 template<class T , class T2>
256 void recenter_set(T &inSet, T2 &outSet){
257  float phicenter = getMeanPhi(inSet);
258  if (phicenter <-9) return;
259  recenter_set(inSet,outSet,phicenter);
260 }
261 
262 template <class T>
263 float getMeanPhi(T &set){
264  // return mean
265  typedef typename T::iterator it_t;
266  it_t it = set.begin();
267  it_t end = set.end();
268  point_t fp = (*it);
269  float max= 0,min=0, mean=0;
270  int n=0;
271  for(; it!= end; ++it){
272  float phi = recenter((*it),fp).second;
273  if( phi >0){
274  max = phi>max ? phi : max;
275  }else{
276  min = phi<min ? phi : min;
277  }
278  mean+=phi; n++;
279  }
280  if ( (max-min) <= (M_PI) )
281  return in_mPI_pPI(mean/n+fp.second);
282  else return -10;
283 }
284 
285 
286 
288  m_cy = p1.first - p2.first;
289  m_cx = p2.second - p1.second;
290  m_cc = -(m_cx*p1.first+m_cy*p1.second);
291  m_oriented_r = (p1.first < p2.first);
292 }
293 
294 inline bool line_t::is_above(point_t &p){
295  return ( (m_cy!=0) && ( p.second >= - (m_cx*p.first+m_cc)/m_cy) );
296 }
297 
298 inline bool line_t::is_below(point_t &p){
299  return ( (m_cy!=0) && ( p.second <= - (m_cx*p.first+m_cc)/m_cy) ) ;
300 }
301 
303  if(m_cx==0) return point_t(-99999,y);
304  return point_t( (-m_cc - m_cy*y)/m_cx , y);
305 }
306 
308  if(m_cy==0) return point_t(x,-99999);
309  return point_t( x, (-m_cc - m_cx*x)/m_cy );
310 }
311 
312 
313 
314 // Convex Hull *************************************
315 
316 template< class inT, class ouT>
317 void findConvexHull(inT &inSet, ouT &outSet ){
318  point_set_t rset;
319  typedef typename inT::iterator it_t;
320  it_t it = inSet.begin();
321  it_t itE = inSet.end();
322  for(; it!=itE; ++it) rset.insert(*it);
323  _findConvexHull(rset,outSet);
324 }
325 
326 template<class ouT>
327 void findConvexHull(point_set_t &inSet, ouT &outSet ){
328  _findConvexHull(inSet, outSet);
329 }
330 
331 template <class T>
332 void _findConvexHull(point_set_t &inSet, T &outSet ){
333 
334  if(inSet.size()<4){
335  outSet.insert(outSet.begin(), inSet.begin(), inSet.end() );
336  return;
337  }
338 
339  point_set_t::iterator s_it = inSet.begin();
340  point_t point00 = (*s_it);
341  point_t point01= point00;
342  while(point01.first == point00.first){
343  ++s_it;
344  point01 = *s_it;
345  }
346  --s_it; point01 = *s_it;
347 
348  s_it = inSet.end(); --s_it;
349  point_t point11 = (*s_it);
350  point_t point10= point11;
351  while(point10.first == point11.first){
352  --s_it;
353  point10 = *s_it;
354  }
355  ++s_it; point10 = *s_it;
356 
357  // lower line :
358  line_t lmin(point00, point10);
359  // lower line :
360  line_t lmax(point01, point11);
361 
362  // fill upper and lower list :
363  point_list_t lowPoints, upPoints;
364 
365  s_it = inSet.begin();
366  if(point00 == point01 ){ // make sure point is in both list
367  lowPoints.push_back(point00);
368  upPoints.push_back(point00);
369  ++s_it;
370  }
371  //std::cout << " lp size =" << lowPoints.size() << std::endl;
372  point_set_t::iterator s_itE = inSet.end();
373  --s_itE; // we'll deal ws_ith the end by hand
374  for(; s_it!= s_itE; ++s_it){
375  point_t p = *s_it;
376  //std::cout << " point "<< p.first << " , "<< p.second << std::endl;
377  if( lmin.is_below(p)) {
378  lowPoints.push_back(p);
379  continue;
380  }
381  if( lmax.is_above(p)) {
382  upPoints.push_front(p); // insert front soit is well sorted
383  }
384 
385  }
386  //std::cout << " lp size =" << lowPoints.size() << std::endl;
387  if(point11 == point10 ){ // make sure point is in both list
388  lowPoints.push_back(point11);
389  upPoints.push_front(point11);
390  ++s_it;
391  }else{
392  lowPoints.push_back(point10);
393  upPoints.push_front(point00);
394  }
395  //std::cout << " lp size =" << lowPoints.size() << std::endl;
396  //std::cout << lowPoints.size() << " " << upPoints.size() << std::endl;
397 
398  // find lower hull :
399  point_list_t lowHull;
400  point_list_t::iterator it = lowPoints.begin();
401  point_list_t::iterator itE = lowPoints.end();
402  lowHull.push_back(point00); // here point00 == (*it)
403  ++it;
404  lowHull.push_back(*it); // always put next point in stack
405  ++it;
406  unsigned int nn = lowPoints.size();
407  if(nn>2){
408  //std::cout << "building low hull : "<< lowPoints.size() <<std::endl;
409  while(it!=itE){
410  testHullLine(lowHull, *it);
411  ++it;
412  }
413  }
414  // find upper hull :
415  point_list_t upHull;
416  it = upPoints.begin();
417  itE = upPoints.end();
418  upHull.push_back(point11); // here point11 == (*it)
419  ++it;
420  upHull.push_back(*it); // always put next point in stack
421  ++it;
422  if(upPoints.size()>2){
423  //std::cout << "building up hull : "<< upPoints.size() <<std::endl;
424  while(it!=itE){
425  testHullLine(upHull, *it);
426  ++it;
427  }
428  }
429 // std::cout << " low size = "<< lowHull.size() << std::endl;
430 // std::cout << " up size = "<< upHull.size() << std::endl;
431  // join Hull !
432  outSet.insert(outSet.begin(), lowHull.begin(), lowHull.end() );
433  outSet.insert((outSet.end()), ++(upHull.begin()), --(upHull.end()) ); // avoid duplication
434 
435  // std::cout << " Out size = "<< outSet.size() << std::endl;
436 
437 }
438 
439 
440 #endif //DICT
441 
442 
443 } // namespace JetGeom
444 #endif
445 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
beamspotman.r
def r
Definition: beamspotman.py:676
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
JetGeom::listToSet
void listToSet(point_list_t &inList, point_set_t &outSet)
Definition: cyl_geom.cxx:27
JetGeom::fix2pi
void fix2pi(point_t &p)
convert
Definition: cyl_geom.h:221
TruthTest.itp
itp
Definition: TruthTest.py:46
checkFileSG.line
line
Definition: checkFileSG.py:75
JetGeom::recenter
point_t recenter(const point_t &p, const point_t &center)
Definition: cyl_geom.h:226
JetGeom::line_t::m_oriented_r
bool m_oriented_r
Definition: cyl_geom.h:48
mean
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Definition: dependence.cxx:254
max
#define max(a, b)
Definition: cfImp.cxx:41
JetGeom::clear_delete
void clear_delete(T &container)
Definition: cyl_geom.h:148
JetGeom::line_t::intercept_y
point_t intercept_y(float y)
Definition: cyl_geom.h:302
JetGeom::line_t::is_above
bool is_above(point_t &p)
Definition: cyl_geom.h:294
hist_file_dump.d
d
Definition: hist_file_dump.py:137
JetGeom::recenter_set
void recenter_set(T &inSet, T2 &outSet, float phicenter)
copy
Definition: cyl_geom.h:245
TRTCalib_cfilter.p1
p1
Definition: TRTCalib_cfilter.py:130
skel.it
it
Definition: skel.GENtoEVGEN.py:396
M_PI
#define M_PI
Definition: ActiveFraction.h:11
JetGeom::line_t::is_left
bool is_left(point_t &p)
Definition: cyl_geom.h:54
JetGeom::point_t::point_t
point_t(float x, float y)
Definition: cyl_geom.h:27
JetGeom::max_deltaR
float max_deltaR(point_t p, T &set)
Return max distance betweens point.
Definition: cyl_geom.h:164
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
JetGeom::abs_dphi
float abs_dphi(float phi1, float phi2)
Definition: cyl_geom.h:206
JetGeom::point_list_t
std::list< point_t > point_list_t
Definition: cyl_geom.h:32
x
#define x
JetGeom::deltaR
float deltaR(point_t &p1, point_t &p2)
distances between points
Definition: cyl_geom.h:240
TruthTest.itE
itE
Definition: TruthTest.py:25
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
JetGeom::_findConvexHull
void _findConvexHull(point_set_t &inSet, inT &outSet)
JetGeom::point_set_t
Definition: cyl_geom.h:29
PlotCalibFromCool.cx
cx
Definition: PlotCalibFromCool.py:666
JetGeom::line_t::phi_shift
void phi_shift(float dphi)
Definition: cyl_geom.h:57
TRTCalib_cfilter.p2
p2
Definition: TRTCalib_cfilter.py:131
JetGeom::getMeanPhi
float getMeanPhi(T &set)
return average phi (i.e.
Definition: cyl_geom.h:263
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
JetGeom::testHullLine
void testHullLine(point_list_t &hull, point_t p)
Definition: cyl_geom.cxx:9
trigmenu_modify_prescale_json.fp
fp
Definition: trigmenu_modify_prescale_json.py:53
beamspotman.n
n
Definition: beamspotman.py:731
JetGeom::point_vect_t
Definition: cyl_geom.h:30
JetGeom::line_t::m_cx
float m_cx
Definition: cyl_geom.h:47
JetGeom::line_t::line_t
line_t(float cx, float cy, float cc, bool to_r)
constructor by giving the coeff of equation of the line (
Definition: cyl_geom.h:43
JetGeom::point_t
Very basic point objects.
Definition: cyl_geom.h:25
JetGeom
A collection of routines for geometric tasks in 2D and on a cylinder.
Definition: cyl_geom.h:22
JetGeom::delete_content
void delete_content(T &container)
Definition: cyl_geom.h:156
JetGeom::findConvexHull
void findConvexHull(inT &inSet, ouT &outSet)
Find convex hull of a set of points in euclidian plan.
Definition: cyl_geom.h:317
JetGeom::line_t::m_cy
float m_cy
Definition: cyl_geom.h:47
JetGeom::deltaPhi
float deltaPhi(point_t &p1, point_t &p2)
Definition: cyl_geom.h:213
JetGeom::line_t
Definition: cyl_geom.h:37
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
min
#define min(a, b)
Definition: cfImp.cxx:40
JetGeom::polygon_area
float polygon_area(T &line)
Definition: cyl_geom.h:177
JetGeom::line_t::is_below
bool is_below(point_t &p)
Definition: cyl_geom.h:298
JetGeom::polygon_lenght
float polygon_lenght(T &line)
Definition: cyl_geom.h:192
JetGeom::line_t::intercept_x
point_t intercept_x(float x)
Definition: cyl_geom.h:307
a
TList * a
Definition: liststreamerinfos.cxx:10
y
#define y
PlotCalibFromCool.cy
cy
Definition: PlotCalibFromCool.py:667
JetGeom::line_t::m_cc
float m_cc
Definition: cyl_geom.h:47
DeMoScan.first
bool first
Definition: DeMoScan.py:536
JetGeom::line_t::is_right
bool is_right(point_t &p)
Definition: cyl_geom.h:55
JetGeom::in_mPI_pPI
float in_mPI_pPI(float phi)
convert
Definition: cyl_geom.h:216
JetGeom::deltaR2
float deltaR2(point_t &p1, point_t &p2)
Definition: cyl_geom.h:236
python.handimod.cc
int cc
Definition: handimod.py:523