ATLAS Offline Software
JetVoronoiDiagramHelpers.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
10 
11 
12 namespace JetVoronoiDiagramHelpers{
13  // operators *
14  Point operator*(double a, const Point &b){
15  coord x = (a * b.x);
16  coord y = (a * b.y);
17  return {x,y};
18  }
19 
20  Point operator*(const Point &b,double a){
21  return a * b;
22  }
23 
24  double operator*(const Point &a, const Point &b){
25  coord x = (a.x * b.x);
26  coord y = (a.y * b.y);
27  return x+y;
28  }
29 
30 
31  // operators +
32  Point operator+(const Point &a, const Point &b){
33  coord x = (a.x + b.x);
34  coord y = (a.y + b.y);
35  return {x,y};
36  }
37 
38  Point operator+(double a, const Point &b){
39  coord x = (a + b.x);
40  coord y = (a + b.y);
41  return {x,y};
42  }
43 
44  Point operator+(const Point &b, double a){
45  return a+b;
46  }
47 
48 
49  // operators -
50  Point operator-(const Point &a, const Point &b){
51  coord x = (a.x - b.x);
52  coord y = (a.y - b.y);
53  return {x,y};
54  }
55 
57  return -1*b;
58  }
59 
60 
61  // logic operators
62  bool operator== (const Point &a, const Point &b){
63  bool isSame = true;
64  isSame &= (a.x == b.x);
65  isSame &= (a.y == b.y);
66  return isSame;
67  }
68 
69  bool operator!= (const Point &a, const Point &b){
70  return ! (a==b);
71  }
72 
73 
74  // space functions
75  Point Center(const Point &a, const Point &b) {
76  return 0.5* (a+b);
77  }
78 
79  Point Norm(const Point &a) {
80  return {a.y, -a.x};
81  }
82 
83 
84 
85  Diagram::Diagram(const std::string & name)
86  : asg::AsgTool(name)
87  , m_x_min(-10.)
88  , m_x_max( 10.)
89  , m_y_min(-4. )
90  , m_y_max( 4. )
91  , m_scaleIntFloat( 1 )
92  , m_area_borders( 0 )
93  , m_N_points( 0 )
94  {
95  }
96 
97 
99  clearDiagram();
100  // recalculate borders
101  double w_x = (m_x_max - m_x_min) ;
102  double w_y = (m_y_max - m_y_min) ;
103  if (w_x<=0 || w_y<=0){
104  ATH_MSG_WARNING( "Wrong range setting x: " << m_x_min << " .. " << m_x_max << " y " << m_y_min << " .. " << m_y_max );
105  return StatusCode::FAILURE;
106  }
107  m_scaleIntFloat = ( (w_x >= w_y ) ? 1e7/w_x : 1e7/w_y );
112  m_area_borders = w_x*w_y;
113  ATH_MSG_INFO( "Initialized with range x: " << m_x_min << " .. " << m_x_max << " y " << m_y_min << " .. " << m_y_max );
114  return StatusCode::SUCCESS;
115  }
116 
117 
118  float Diagram::getCellArea(const coord x, const coord y) const{
119  Point p ( x , y );
120  size_t i_p = findPointIndex(p);
121  if ( i_p >= m_N_points ) {
122  ATH_MSG_WARNING("Can not find point : " << p.x << " " << p.y);
123  return 0;
124  }
125  return m_area_cells[i_p];
126  }
127 
128 
129  size_t Diagram::findPointIndex(const Point &a) const{
130  size_t index=0;
131  for (const Point& b: m_voro_vtx){
132  if (a==b) return index;
133  index++;
134  }
135  index++;
136  return index;
137  }
138 
139 
141  m_voro_vtx .clear();
142  m_area_cells .clear();
143  // m_voro_cells.clear();
144  m_N_points = 0;
145  }
146 
147 
149  m_N_points = m_voro_vtx.size();
150  if (m_N_points==0) {
151  ATH_MSG_WARNING( "Trying to create diagram withou any input");
152  return StatusCode::FAILURE;
153  }
154  Polygon mypts;
155  for (const Point& p : m_voro_vtx) mypts.Add(p.x*m_scaleIntFloat,p.y*m_scaleIntFloat);
156  VoronoiBoost vd;
157  // boost make voronoi
158  construct_voronoi(mypts.begin(), mypts.end(), &vd);
159  int cell_i = 0;
160  for (VoronoiCellBoost cell : vd.cells() ){ // for all cells in diagram
161  Polygon cell_area_tmp; // temprary polygon of cell
162  const VoronoiEdgeBoost * edge = cell.incident_edge(); // first edge
163  int csi = cell.source_index(); // index of cell
164  do { // for all edges which the cell has
165  const VoronoiVtxBoost * vtx0 = edge->vertex0();
166  const VoronoiVtxBoost * vtx1 = edge->vertex1();
167  int csi_ngbr = edge->twin()->cell()->source_index() ; // index of neighbour cell
168  if (edge->is_primary()){ // is nonintersecting edge (or something like that)
169  // first vertex in infinity -- interpolate
170  if ( vtx0==nullptr ) cell_area_tmp.push_back( interpolateInfinityVertex( csi, csi_ngbr ) );
171  // adding only second vertex if exists
172  if ( vtx1!=nullptr ) cell_area_tmp.Add(vtx1->x()/m_scaleIntFloat, vtx1->y()/m_scaleIntFloat); // correct for scale
173  // second vertex in infinity -- interpolate (notice the change of index order)
174  else cell_area_tmp.push_back( interpolateInfinityVertex( csi_ngbr, csi ) );
175  }
176  edge=edge->next();
177  } while (edge != cell.incident_edge() );
178  // intersection and are calculation
179  Polygon cell_area;
180  double areasize = intersectionAndArea(cell_area_tmp,m_borders,cell_area);
181  // m_voro_cells.push_back(cell_area); // save cell polygon
182  m_area_cells.push_back(areasize);
183  cell_i++;
184  }
185  return checkStatus(vd,cell_i);
186  }
187 
188 
189  Point Diagram::interpolateInfinityVertex(const int i_a, const int i_b){
190  double sign = 300; // to limit infinity
191  Point A = m_voro_vtx[i_a];
192  Point B = m_voro_vtx[i_b];
193  return Center(A,B) + Norm(B-A) * sign ;
194  }
195 
196 
197  bool Diagram::checkSameNumber(double in, double out, const std::string & description){
198  if ( fabs(in-out)/in > 1e4) {
199  ATH_MSG_WARNING("Difference in " << description.c_str() << ": " << in << " (should be " << out << ")");
200  return false;
201  };
202  return true;
203  }
204 
205 
206  StatusCode Diagram::checkStatus(const VoronoiBoost &vd, size_t n_cells_processed){
207  bool isAllOK = true;
208  double sum = 0; for (double voroarea : m_area_cells) sum+=voroarea;
209  isAllOK &= checkSameNumber( vd.cells().size() , m_voro_vtx.size(), "number of boost cells" );
210  isAllOK &= checkSameNumber( n_cells_processed , m_voro_vtx.size(), "number of processed cells" );
211  isAllOK &= checkSameNumber( m_area_cells.size(), m_voro_vtx.size(), "number of calculated areas" );
212  isAllOK &= checkSameNumber( sum , m_area_borders , "area sum" );
213  if(!isAllOK){
214  m_N_points = 0;
215  return StatusCode::FAILURE;
216  }
217  return StatusCode::SUCCESS;
218  }
219 
220 
221  double Diagram::intersectionAndArea(Polygon const & geo1, Polygon const & geo2, Polygon &out){
222  VoronoiPolygonBoost pol1, pol2;
223  std::deque<VoronoiPolygonBoost> pol3; // list of poly's because intersection could be more than one polygon (not in our case)
224  geo1.FillVoroPolygon(pol1);
225  geo2.FillVoroPolygon(pol2);
226  // boost make intersection
227  intersection(pol1,pol2,pol3);
228  if ( !pol3.empty() ){ // it has something our shape is on first place
229  for ( const VoronoiPointBoost vp: pol3.at(0).outer() ) out.Add(vp.x(),vp.y());
230  }
231  else return 0;
232  // boost calculate area
233  return area(pol3.at(0));
234  }
235 
236 }
JetVoronoiDiagramHelpers::operator+
Point operator+(const Point &a, const Point &b)
Definition: JetVoronoiDiagramHelpers.cxx:32
VoronoiEdgeBoost
boost::polygon::voronoi_diagram< double >::edge_type VoronoiEdgeBoost
Definition: JetVoronoiDiagramHelpers.h:31
VoronoiPolygonBoost
boost::geometry::model::polygon< VoronoiPointBoost > VoronoiPolygonBoost
Definition: JetVoronoiDiagramHelpers.h:40
JetVoronoiDiagramHelpers::Diagram::m_x_min
double m_x_min
Definition: JetVoronoiDiagramHelpers.h:140
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
JetVoronoiDiagramHelpers::Diagram::checkSameNumber
bool checkSameNumber(double in, double out, const std::string &description)
Definition: JetVoronoiDiagramHelpers.cxx:197
JetVoronoiDiagramHelpers::Polygon::FillVoroPolygon
void FillVoroPolygon(VoronoiPolygonBoost &out) const
Definition: JetVoronoiDiagramHelpers.h:98
JetVoronoiDiagramHelpers::Diagram::Diagram
Diagram(const std::string &name)
Definition: JetVoronoiDiagramHelpers.cxx:85
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
JetVoronoiDiagramHelpers::Diagram::intersectionAndArea
double intersectionAndArea(JetVoronoiDiagramHelpers::Polygon const &geo1, JetVoronoiDiagramHelpers::Polygon const &geo2, JetVoronoiDiagramHelpers::Polygon &out)
Definition: JetVoronoiDiagramHelpers.cxx:221
index
Definition: index.py:1
JetVoronoiDiagramHelpers::operator==
bool operator==(const Point &a, const Point &b)
Definition: JetVoronoiDiagramHelpers.cxx:62
asg
Definition: DataHandleTestTool.h:28
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
JetVoronoiDiagramHelpers::Polygon
Definition: JetVoronoiDiagramHelpers.h:94
VoronoiBoost
boost::polygon::voronoi_diagram< double > VoronoiBoost
Jakub Cuth May 2015.
Definition: JetVoronoiDiagramHelpers.h:28
JetVoronoiDiagramHelpers.h
JetVoronoiDiagramHelpers::operator!=
bool operator!=(const Point &a, const Point &b)
Definition: JetVoronoiDiagramHelpers.cxx:69
x
#define x
intersection
std::vector< std::string > intersection(std::vector< std::string > &v1, std::vector< std::string > &v2)
Definition: compareFlatTrees.cxx:25
JetVoronoiDiagramHelpers::Point
Definition: JetVoronoiDiagramHelpers.h:50
JetVoronoiDiagramHelpers::Diagram::m_area_borders
double m_area_borders
Definition: JetVoronoiDiagramHelpers.h:148
JetVoronoiDiagramHelpers::Diagram::initialize
StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: JetVoronoiDiagramHelpers.cxx:98
VoronoiVtxBoost
boost::polygon::voronoi_diagram< double >::vertex_type VoronoiVtxBoost
Definition: JetVoronoiDiagramHelpers.h:29
A
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
PixelAthClusterMonAlgCfg.e4
e4
Definition: PixelAthClusterMonAlgCfg.py:332
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
JetVoronoiDiagramHelpers::Diagram::checkStatus
StatusCode checkStatus(const VoronoiBoost &vd, size_t n_cells_processed)
Definition: JetVoronoiDiagramHelpers.cxx:206
JetVoronoiDiagramHelpers::Norm
Point Norm(const Point &a)
Definition: JetVoronoiDiagramHelpers.cxx:79
sign
int sign(int a)
Definition: TRT_StrawNeighbourSvc.h:107
JetVoronoiDiagramHelpers::Diagram::m_voro_vtx
Polygon m_voro_vtx
Definition: JetVoronoiDiagramHelpers.h:146
VoronoiCellBoost
boost::polygon::voronoi_diagram< double >::cell_type VoronoiCellBoost
Definition: JetVoronoiDiagramHelpers.h:30
JetVoronoiDiagramHelpers::Diagram::m_y_max
double m_y_max
Definition: JetVoronoiDiagramHelpers.h:143
JetVoronoiDiagramHelpers::Diagram::createVoronoiDiagram
StatusCode createVoronoiDiagram()
Definition: JetVoronoiDiagramHelpers.cxx:148
JetVoronoiDiagramHelpers::Diagram::m_x_max
double m_x_max
Definition: JetVoronoiDiagramHelpers.h:141
JetVoronoiDiagramHelpers::Diagram::m_borders
Polygon m_borders
Definition: JetVoronoiDiagramHelpers.h:145
VoronoiPointBoost
boost::geometry::model::d2::point_xy< double > VoronoiPointBoost
Definition: JetVoronoiDiagramHelpers.h:39
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
JetVoronoiDiagramHelpers::operator*
Point operator*(double a, const Point &b)
Definition: JetVoronoiDiagramHelpers.cxx:14
JetVoronoiDiagramHelpers::Diagram::m_area_cells
std::vector< double > m_area_cells
Definition: JetVoronoiDiagramHelpers.h:147
JetVoronoiDiagramHelpers::Diagram::m_N_points
size_t m_N_points
Definition: JetVoronoiDiagramHelpers.h:150
JetVoronoiDiagramHelpers::Diagram::m_scaleIntFloat
double m_scaleIntFloat
Definition: JetVoronoiDiagramHelpers.h:144
JetVoronoiDiagramHelpers::coord
double coord
Definition: JetVoronoiDiagramHelpers.h:45
JetVoronoiDiagramHelpers::Center
Point Center(const Point &a, const Point &b)
Definition: JetVoronoiDiagramHelpers.cxx:75
JetVoronoiDiagramHelpers::Polygon::Add
void Add(coord x, coord y)
Definition: JetVoronoiDiagramHelpers.h:95
DeMoScan.index
string index
Definition: DeMoScan.py:364
a
TList * a
Definition: liststreamerinfos.cxx:10
JetVoronoiDiagramHelpers::Diagram::m_y_min
double m_y_min
Definition: JetVoronoiDiagramHelpers.h:142
y
#define y
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
JetVoronoiDiagramHelpers::Diagram::findPointIndex
size_t findPointIndex(const Point &a) const
Definition: JetVoronoiDiagramHelpers.cxx:129
JetVoronoiDiagramHelpers::Diagram::interpolateInfinityVertex
Point interpolateInfinityVertex(const int i_a, const int i_b)
Definition: JetVoronoiDiagramHelpers.cxx:189
JetVoronoiDiagramHelpers::Diagram::getCellArea
float getCellArea(const coord x, const coord y) const
Definition: JetVoronoiDiagramHelpers.cxx:118
area
double area(double R)
Definition: ConvertStaveServices.cxx:42
JetVoronoiDiagramHelpers::operator-
Point operator-(const Point &a, const Point &b)
Definition: JetVoronoiDiagramHelpers.cxx:50
JetVoronoiDiagramHelpers::Diagram::clearDiagram
void clearDiagram()
Definition: JetVoronoiDiagramHelpers.cxx:140
JetVoronoiDiagramHelpers
Jakub Cuth May 2015.
Definition: JetVoronoiDiagramHelpers.h:42
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88