2 ///////////////////////////////////////////////////////////////////////////////
4 // Description: header file for two-dimensional geometry tools //
5 // This file is part of the SISCone project. //
6 // For more details, see http://projects.hepforge.org/siscone //
8 // Copyright (c) 2006 Gavin Salam and Gregory Soyez //
10 // This program is free software; you can redistribute it and/or modify //
11 // it under the terms of the GNU General Public License as published by //
12 // the Free Software Foundation; either version 2 of the License, or //
13 // (at your option) any later version. //
15 // This program is distributed in the hope that it will be useful, //
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
18 // GNU General Public License for more details. //
20 // You should have received a copy of the GNU General Public License //
21 // along with this program; if not, write to the Free Software //
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA //
24 // $Revision:: 268 $//
25 // $Date:: 2009-03-12 21:24:16 +0100 (Thu, 12 Mar 2009) $//
26 ///////////////////////////////////////////////////////////////////////////////
36 #define M_PI 3.141592653589793238462643383279502884197
41 /// return a result that corresponds to phi, but in the
42 /// range (-pi..pi]; the result is only correct if -3pi < phi <= 3pi
43 inline double phi_in_range(double phi) {
44 if (phi <= -M_PI) phi += twopi;
45 else if (phi > M_PI) phi -= twopi;
49 /// return the difference between the two phi values,
50 /// placed in the correct range (-pi..pi], , assuming that phi1,phi2
51 /// are already in the correct range.
52 inline double dphi(double phi1, double phi2) {
53 return phi_in_range(phi1-phi2);
57 /// return the absolute difference between the two phi values,
58 /// placed in the correct range, assuming that phi1,phi2 are already
59 /// in the correct range.
60 inline double abs_dphi(double phi1, double phi2) {
61 double delta = fabs(phi1-phi2);
62 return delta > M_PI ? twopi-delta : delta;
65 /// return the square of the argument
66 inline double pow2(double x) {return x*x;}
71 * \brief class for holding a two-vector
76 Ctwovect() : x(0.0), y(0.0) {}
78 /// ctor with initialisation
79 /// \param _x first coordinate
80 /// \param _y second coordinate
81 Ctwovect(double _x, double _y) : x(_x), y(_y) {}
83 /// vector coordinates
86 /// norm (modulud square) of the vector
87 inline double mod2() const {return pow2(x)+pow2(y);}
89 /// modulus of the vector
90 inline double modulus() const {return sqrt(mod2());}
94 /// dot product of two 2-vectors
95 /// \param a first 2-vect
96 /// \param b second 2-vect
97 /// \return a.b is returned
98 inline double dot_product(const Ctwovect & a, const Ctwovect & b) {
99 return a.x*b.x + a.y*b.y;
103 /// cross product of two 2-vectors
104 /// \param a first 2-vect
105 /// \param b second 2-vect
106 /// \return a x b is returned
107 inline double cross_product(const Ctwovect & a, const Ctwovect & b) {
108 return a.x*b.y - a.y*b.x;
113 * \class Ceta_phi_range
114 * \brief class for holding a covering range in eta-phi
116 * This class deals with ranges in the eta-phi plane. It
117 * implements methods to test if two ranges overlap and
118 * to take the union of two overlapping intervals.
120 class Ceta_phi_range{
125 /// ctor with initialisation
126 /// we initialise with a centre (in eta,phi) and a radius
127 /// \param c_eta eta coordinate of the centre
128 /// \param c_phi phi coordinate of the centre
130 Ceta_phi_range(double c_eta, double c_phi, double R);
132 /// assignment of range
133 /// \param r range to assign to current one
134 Ceta_phi_range& operator = (const Ceta_phi_range &r);
136 /// add a particle to the range
137 /// \param eta eta coordinate of the particle
138 /// \param phi phi coordinate of the particle
139 /// \return 0 on success, 1 on error
140 int add_particle(const double eta, const double phi);
142 /// eta range as a binary coding of covered cells
143 unsigned int eta_range;
145 /// phi range as a binary coding of covered cells
146 unsigned int phi_range;
148 // extremal value for eta
149 static double eta_min; ///< minimal value for eta
150 static double eta_max; ///< maximal value for eta
153 /// return the cell index corrsponding to an eta value
154 inline unsigned int get_eta_cell(double eta){
155 return (unsigned int) (1 << ((int) (32*((eta-eta_min)/(eta_max-eta_min)))));
158 /// return the cell index corrsponding to a phi value
159 inline unsigned int get_phi_cell(double phi){
160 return (unsigned int) (1 << ((int) (32*phi/twopi+16)%32));
165 /// \param r1 first range
166 /// \param r2 second range
167 /// \return true if overlap, false otherwise.
168 bool is_range_overlap(const Ceta_phi_range &r1, const Ceta_phi_range &r2);
171 /// Note: we assume that the two intervals overlap
172 /// \param r1 first range
173 /// \param r2 second range
174 /// \return union of the two ranges
175 const Ceta_phi_range range_union(const Ceta_phi_range &r1, const Ceta_phi_range &r2);