FastJet 3.5.0
Loading...
Searching...
No Matches
Dnn2piCylinder.hh
1//FJSTARTHEADER
2// $Id$
3//
4// Copyright (c) 2005-2025, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
5//
6//----------------------------------------------------------------------
7// This file is part of FastJet.
8//
9// FastJet is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 2 of the License, or
12// (at your option) any later version.
13//
14// The algorithms that underlie FastJet have required considerable
15// development. They are described in the original FastJet paper,
16// hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
17// FastJet as part of work towards a scientific publication, please
18// quote the version you use and include a citation to the manual and
19// optionally also to hep-ph/0512210.
20//
21// FastJet is distributed in the hope that it will be useful,
22// but WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24// GNU General Public License for more details.
25//
26// You should have received a copy of the GNU General Public License
27// along with FastJet. If not, see <http://www.gnu.org/licenses/>.
28//----------------------------------------------------------------------
29//FJENDHEADER
30
31#include "fastjet/config.h"
32
33#ifndef DROP_CGAL // in case we do not have the code for CGAL
34#ifndef __FASTJET_DNN2PICYLINDER_HH__
35#define __FASTJET_DNN2PICYLINDER_HH__
36
37#include "fastjet/internal/DynamicNearestNeighbours.hh"
38#include "fastjet/internal/DnnPlane.hh"
39#include "fastjet/internal/numconsts.hh"
40
41FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
42
43
44/// \if internal_doc
45/// @ingroup internal
46/// \class Dnn2piCylinder
47/// class derived from DynamicNearestNeighbours that provides an
48/// implementation for the surface of cylinder (using one
49/// DnnPlane object spanning 0--2pi).
50/// \endif
52 public:
53 /// empty initaliser
55
56 /// Initialiser from a set of points on an Eta-Phi plane, where
57 /// eta can have an arbitrary ranges and phi must be in range
58 /// 0 <= phi < 2pi;
59 ///
60 /// NB: this class is more efficient than the plain Dnn4piCylinder
61 /// class, but can give wrong answers when the nearest neighbour is
62 /// further away than 2pi (in this case a point's nearest neighbour
63 /// becomes itself, because it is considered to be a distance 2pi
64 /// away). For the kt-algorithm (e.g.) this is actually not a
65 /// problem (the distance need only be accurate when it is less than
66 /// R, assuming R<2pi [not necessarily always the case as of
67 /// 2010-11-19, when we've removed the requirement R<pi/2 in the
68 /// JetDefinition constructor]), so we can tell the routine to
69 /// ignore this problem -- alternatively the routine will crash if
70 /// it detects it occurring (only when finding the nearest neighbour
71 /// index, not its distance).
72 Dnn2piCylinder(const std::vector<EtaPhi> &,
73 const bool & ignore_nearest_is_mirror = false,
74 const bool & verbose = false );
75
76 /// Returns the index of the nearest neighbour of point labelled
77 /// by ii (assumes ii is valid)
78 int NearestNeighbourIndex(const int ii) const ;
79
80 /// Returns the distance to the nearest neighbour of point labelled
81 /// by index ii (assumes ii is valid)
82 double NearestNeighbourDistance(const int ii) const ;
83
84 /// Returns true iff the given index corresponds to a point that
85 /// exists in the DNN structure (meaning that it has been added, and
86 /// not removed in the meantime)
87 bool Valid(const int index) const;
88
89 void RemoveAndAddPoints(const std::vector<int> & indices_to_remove,
90 const std::vector<EtaPhi> & points_to_add,
91 std::vector<int> & indices_added,
92 std::vector<int> & indices_of_updated_neighbours);
93
95
96 private:
97
98 // our extras to help us navigate, find distance, etc.
99 const static int INEXISTENT_VERTEX=-3;
100
101 bool _verbose;
102
103 bool _ignore_nearest_is_mirror;
104
105 /// Picture of how things will work... Copy 0--pi part of the 0--2pi
106 /// cylinder into a region 2pi--2pi+ a bit of a Euclidean plane. Below we
107 /// show points labelled by + that have a mirror image in this
108 /// manner, while points labelled by * do not have a mirror image.
109 ///
110 /// | . |
111 /// | . |
112 /// | . |
113 /// | . |
114 /// | 2 . |
115 /// | * . |
116 /// | + . + |
117 /// | 0 . 1 |
118 /// | . |
119 /// 0 2pi 2pi + a bit
120 ///
121 /// Each "true" point has its true "cylinder" index (the index that
122 /// is known externally to this class) as well as euclidean plane
123 /// indices (main_index and mirror index in the MirrorVertexInfo
124 /// structure), which are private concepts of this class.
125 ///
126 /// In above picture our structures would hold the following info
127 /// (the picture shows the euclidean-plane numbering)
128 ///
129 /// _mirror_info[cylinder_index = 0] = (0, 1)
130 /// _mirror_info[cylinder_index = 1] = (2, INEXISTENT_VERTEX)
131 ///
132 /// We also need to be able to go from the euclidean plane indices
133 /// back to the "true" cylinder index, and for this purpose we use
134 /// the std::vector _cylinder_index_of_plane_vertex[...], which in the above example has
135 /// the following contents
136 ///
137 /// _cylinder_index_of_plane_vertex[0] = 0
138 /// _cylinder_index_of_plane_vertex[1] = 0
139 /// _cylinder_index_of_plane_vertex[2] = 1
140 ///
141
142 ///
143 struct MirrorVertexInfo {
144 /// index of the given point (appearing in the range 0--2pi) in the
145 /// 0--2pi euclidean plane structure (position will coincide with
146 /// that on the 0--2pi cylinder, but index labelling it will be
147 /// different)
148 int main_index;
149 /// index of the mirror point (appearing in the range 2pi--3pi) in the
150 /// 0--3pi euclidean plane structure
151 int mirror_index;
152 };
153
154 // for each "true" vertex we have reference to indices in the euclidean
155 // plane structure
156 std::vector<MirrorVertexInfo> _mirror_info;
157 // for each index in the euclidean 0--2pi plane structure we want to
158 // be able to get back to the "true" vertex index on the overall
159 // 0--2pi cylinder structure
160 std::vector<int> _cylinder_index_of_plane_vertex;
161
162 // NB: we define POINTERS here because the initialisation gave
163 // us problems (things crashed!), perhaps because in practice
164 // we were making a copy without being careful and defining
165 // a proper copy constructor.
166 DnnPlane * _DNN;
167
168 /// given a phi value in the 0--pi range return one
169 /// in the 2pi--3pi range; whereas if it is in the pi-2pi range then
170 /// remap it to be inthe range (-pi)--0.
171 inline EtaPhi _remap_phi(const EtaPhi & point) {
172 double phi = point.second;
173 if (phi < pi) { phi += twopi ;} else {phi -= twopi;}
174 return EtaPhi(point.first, phi);}
175
176
177 //----------------------------------------------------------------------
178 /// Actions here are similar to those in the
179 /// Dnn3piCylinder::_RegisterCylinderPoint case, however here we do
180 /// NOT create the mirror point -- instead we initialise the structure
181 /// as if there were no need for the mirror point.
182 ///
183 /// ADDITIONALLY push the cylinder_point onto the vector plane_points.
184 void _RegisterCylinderPoint (const EtaPhi & cylinder_point,
185 std::vector<EtaPhi> & plane_points);
186
187 /// For each plane point specified in the vector plane_indices,
188 /// establish whether there is a need to create a mirror point
189 /// according to the following criteria:
190 ///
191 /// . phi < pi
192 /// . mirror does not already exist
193 /// . phi < NearestNeighbourDistance
194 /// (if this is not true then there is no way that its mirror point
195 /// could have a nearer neighbour).
196 ///
197 /// If conditions all hold, then create the mirror point, insert it
198 /// into the _DNN structure, adjusting any nearest neighbours, and
199 /// return the list of plane points whose nearest neighbours have
200 /// changed (this will include the new neighbours that have just been
201 /// added)
202 void _CreateNecessaryMirrorPoints(
203 const std::vector<int> & plane_indices,
204 std::vector<int> & updated_plane_points);
205
206};
207
208
209// here follow some inline implementations of the simpler of the
210// functions defined above
211
212//----------------------------------------------------------------------
213/// Note: one of the difficulties of the 0--2pi mapping is that
214/// a point may have its mirror copy as its own nearest neighbour
215/// (if no other point is within a distance of 2pi). This does
216/// not matter for the kt_algorithm with
217/// reasonable values of radius, but might matter for a general use
218/// of this algorithm -- depending on whether or not the user has
219/// initialised the class with instructions to ignore this problem the
220/// program will detect and ignore it, or crash.
221inline int Dnn2piCylinder::NearestNeighbourIndex(const int current) const {
222 int main_index = _mirror_info[current].main_index;
223 int mirror_index = _mirror_info[current].mirror_index;
224 int plane_index;
225 if (mirror_index == INEXISTENT_VERTEX ) {
226 plane_index = _DNN->NearestNeighbourIndex(main_index);
227 } else {
228 plane_index = (
229 _DNN->NearestNeighbourDistance(main_index) <
230 _DNN->NearestNeighbourDistance(mirror_index)) ?
231 _DNN->NearestNeighbourIndex(main_index) :
232 _DNN->NearestNeighbourIndex(mirror_index) ;
233 }
234 int this_cylinder_index = _cylinder_index_of_plane_vertex[plane_index];
235 // either the user has acknowledged the fact that they may get the
236 // mirror copy as the closest point, or crash if it should occur
237 // that mirror copy is the closest point.
238 assert(_ignore_nearest_is_mirror || this_cylinder_index != current);
239 //if (this_cylinder_index == current) {
240 // cerr << "WARNING point "<<current<<
241 // " has its mirror copy as its own nearest neighbour"<<endl;
242 //}
243 return this_cylinder_index;
244}
245
246inline double Dnn2piCylinder::NearestNeighbourDistance(const int current) const {
247 int main_index = _mirror_info[current].main_index;
248 int mirror_index = _mirror_info[current].mirror_index;
249 if (mirror_index == INEXISTENT_VERTEX ) {
250 return _DNN->NearestNeighbourDistance(main_index);
251 } else {
252 return (
253 _DNN->NearestNeighbourDistance(main_index) <
254 _DNN->NearestNeighbourDistance(mirror_index)) ?
255 _DNN->NearestNeighbourDistance(main_index) :
256 _DNN->NearestNeighbourDistance(mirror_index) ;
257 }
258
259}
260
261inline bool Dnn2piCylinder::Valid(const int index) const {
262 return (_DNN->Valid(_mirror_info[index].main_index));
263}
264
265
266inline Dnn2piCylinder::~Dnn2piCylinder() {
267 delete _DNN;
268}
269
270
271FASTJET_END_NAMESPACE
272
273#endif // __FASTJET_DNN2PICYLINDER_HH__
274#endif //DROP_CGAL
Dnn2piCylinder(const std::vector< EtaPhi > &, const bool &ignore_nearest_is_mirror=false, const bool &verbose=false)
Initialiser from a set of points on an Eta-Phi plane, where eta can have an arbitrary ranges and phi ...
Dnn2piCylinder()
empty initaliser
double NearestNeighbourDistance(const int ii) const
Returns the distance to the nearest neighbour of point labelled by index ii (assumes ii is valid)
Definition DnnPlane.hh:253
bool Valid(const int index) const
Returns true iff the given index corresponds to a point that exists in the DNN structure (meaning tha...
Definition DnnPlane.hh:256
int NearestNeighbourIndex(const int ii) const
Returns the index of the nearest neighbour of point labelled by ii (assumes ii is valid)
Definition DnnPlane.hh:250
Shortcut for dealing with eta-phi coordinates.