KaliVeda
Toolkit for HIC analysis
KVIDLine.h
1 /***************************************************************************
2  KVIDLine.h - description
3  -------------------
4  begin : Nov 10 2004
5  copyright : (C) 2004 by J.D. Frankland
6  email : frankland@ganil.fr
7 
8 $Id: KVIDLine.h,v 1.16 2009/03/03 14:27:15 franklan Exp $
9 ***************************************************************************/
10 
11 #ifndef KVIDLine_H
12 #define KVIDLine_H
13 
14 #include "TVector2.h"
15 #include "TMath.h"
16 #include "TH2.h"
17 #include "KVIDentifier.h"
143 class KVIDLine : public KVIDentifier {
144 
157  class MyVector2 {
158  double x, y;
159  public:
160  MyVector2(double X, double Y) : x(X), y(Y) {}
162  {
163  *this = T;
164  }
166  {
167  if (this != &T) {
168  x = T.x;
169  y = T.y;
170  }
171  return *this;
172  }
173  friend MyVector2 operator-(const MyVector2& a, const MyVector2& b)
174  {
175  return {a.x - b.x, a.y - b.y};
176  }
177  friend double operator*(const MyVector2& a, const MyVector2& b)
178  {
179  return a.x * b.x + a.y * b.y;
180  }
181  friend MyVector2 operator*(const MyVector2& a, double s)
182  {
183  return {a.x * s, a.y * s};
184  }
185  friend MyVector2 operator*(double s, const MyVector2& a)
186  {
187  return a * s;
188  }
189  double Mod2() const
190  {
191  return (*this) * (*this);
192  }
193  double Mod() const
194  {
195  return sqrt(Mod2());
196  }
197  MyVector2 Proj(const MyVector2& v) const
198  {
199  double scalar = v * (*this);
200  scalar /= v.Mod2();
201  return v * scalar;
202  }
203  MyVector2 Norm(const MyVector2& v) const
204  {
205  return (*this) - Proj(v);
206  }
207  Double_t Phi() const
208  {
209  return TMath::Pi() + TMath::ATan2(-y, -x);
210  }
211  };
212 
213 public:
214 
215  KVIDLine();
216  KVIDLine(const TGraph& gr);
217  KVIDLine(const KVIDLine&);
218 
219  virtual ~ KVIDLine();
220 
221  virtual void WaitForPrimitive();
222  virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py);
223 
224  inline Double_t DistanceToLine(Double_t px, Double_t py, Int_t&);
225  inline Double_t DistanceToLine(Double_t px, Double_t py, Double_t xp1,
226  Double_t yp1, Double_t xp2, Double_t yp2,
227  Int_t&);
228  inline Bool_t WhereAmI(Double_t px, Double_t py, Option_t* opt);
230  Double_t xp1, Double_t yp1, Double_t xp2,
231  Double_t yp2);
232 
233  inline void GetStartPoint(Double_t& x, Double_t& y) const;
234  inline void GetEndPoint(Double_t& x, Double_t& y) const;
236  const Char_t* axis = "") const;
237 
238  static KVIDLine* MakeIDLine(TObject* obj, Double_t xdeb = -1., Double_t xfin = -1., Double_t np = 1., Bool_t save = kFALSE);
240 
241  ClassDef(KVIDLine, 2) //Base class for lines/cuts used for particle identification
242 };
243 
245 
247  Int_t& i_near)
248 {
267 
268  Double_t distance, dist2;
269  Int_t i;
270  Double_t d;
271  distance = dist2 = 9999.;
272  Int_t i_nearest_point = 0, inear1 = 0, inear2 = 0;
276 
277  for (i = 0; i < fNpoints - 1; i++) {
278  d = DistanceToLine(px, py, fX[i], fY[i], fX[i + 1], fY[i + 1],
279  i_nearest_point);
281  if (d >= 0.) {
282  if (d < distance) {
283  distance = d;
284  inear1 = i;
285  }
286  }
287  else {
289  if (-d < dist2) {
290  dist2 = -d;
291  inear2 = i + i_nearest_point;
292  }
293  }
294  }
295 
296  i_near = inear1;
297  if (distance < 9999.)
298  return distance;
299  i_near = inear2;
301  if (inear2 > 0 && inear2 < (fNpoints - 1))
302  return dist2;
304  return -dist2;
305 }
306 
307 
309 
311  Double_t xp1, Double_t yp1,
312  Double_t xp2, Double_t yp2,
313  Int_t& i_nearest_point)
314 {
337 
338  MyVector2 P1(xp1, yp1), P2(xp2, yp2), P(px, py);
339  MyVector2 P1P2 = P2 - P1;
340  MyVector2 P1P = P - P1;
341  MyVector2 P2P = P - P2;
342  MyVector2 MP = P1P.Norm((P1P2));
343  i_nearest_point = 0;
345  Double_t sum = (P1P - MP).Mod() + (P2P - MP).Mod();
347  if (TMath::Abs(sum - P1P2.Mod()) > 1.e-06) {
349  if (P1P.Mod() < P2P.Mod()) {
350  i_nearest_point = 0;
351  return -(P1P.Mod());
352  }
353  i_nearest_point = 1;
354  return -(P2P.Mod());
355  }
357  return MP.Mod();
358 }
359 
360 
362 
364  Double_t py, Double_t xp1,
365  Double_t yp1, Double_t xp2,
366  Double_t yp2)
367 {
391 
392  MyVector2 P1(xp1, yp1), P2(xp2, yp2), P(px, py);
393  MyVector2 P1P2 = P2 - P1;
394  MyVector2 P1P = P - P1;
395  MyVector2 MP = P1P.Norm((P1P2));
396  Double_t phi = MP.Phi() * TMath::RadToDeg();
401  Bool_t result = kFALSE;
402  if (!strcmp(opt, "left")) {
403  result = (phi > 90. && phi < 270.);
404  }
405  else if (!strcmp(opt, "right")) {
406  result = (phi < 90. || phi > 270.);
407  }
408  else if (!strcmp(opt, "above")) {
409  result = (phi > 0. && phi < 180.);
410  }
411  else if (!strcmp(opt, "below")) {
412  result = (phi > 180. && phi < 360.);
413  }
414  return result;
415 }
416 
418 
420 {
427 
428  Double_t* XX, *YY;
429  Double_t xx, yy;
430  Int_t sign = 1;
431 
432  if (!strcmp(opt, "left")) {
433  XX = fY;
434  xx = py;
435  YY = fX;
436  yy = px;
437  }
438  else if (!strcmp(opt, "right")) {
439  XX = fY;
440  xx = py;
441  YY = fX;
442  yy = px;
443  sign = -1;
444  }
445  else if (!strcmp(opt, "above")) {
446  XX = fX;
447  xx = px;
448  YY = fY;
449  yy = py;
450  sign = -1;
451  }
452  else if (!strcmp(opt, "below")) {
453  XX = fX;
454  xx = px;
455  YY = fY;
456  yy = py;
457  }
458  else return kFALSE;
459 
460  Int_t i_start = 0;
461  Int_t i_stop = fNpoints - 1;
462  Int_t prev_i_stop = 0;
463  Bool_t same_sign = TMath::Sign(1., XX[i_start] - xx) == TMath::Sign(1., XX[i_stop] - xx);
464  while ((i_start < i_stop - 1) || same_sign) {
465 
466  if (same_sign && (prev_i_stop == 0)) break;
467  else if (same_sign) {
468  i_start = i_stop;
469  i_stop = prev_i_stop;
470  }
471  else {
472  prev_i_stop = i_stop;
473  i_stop = (Int_t)((i_start + i_stop) / 2 + 0.5);
474  }
475 
476  same_sign = TMath::Sign(1., XX[i_start] - xx) == TMath::Sign(1., XX[i_stop] - xx);
477  }
478 
479  Double_t a = (YY[i_stop] - YY[i_start]) / (XX[i_stop] - XX[i_start]);
480  Double_t b = YY[i_start] - a * XX[i_start];
481 
482  Bool_t res = (sign * yy < sign * (a * xx + b));
483 
484  return res;
485 }
486 
488 
489 inline void KVIDLine::GetStartPoint(Double_t& x, Double_t& y) const
490 {
492 #if ROOT_VERSION_CODE >= ROOT_VERSION(4,0,3)
493  GetPoint(0, x, y);
494 #else
495  const_cast < KVIDLine* >(this)->GetPoint(0, x, y);
496 #endif
497 }
498 
500 
501 inline void KVIDLine::GetEndPoint(Double_t& x, Double_t& y) const
502 {
504 #if ROOT_VERSION_CODE >= ROOT_VERSION(4,0,3)
505  GetPoint((GetN() - 1), x, y);
506 #else
507  const_cast < KVIDLine* >(this)->GetPoint((GetN() - 1), x, y);
508 #endif
509 }
510 
512 
514  const Char_t* axis) const
515 {
520 
521  TString ax(axis);
522  ax.ToUpper();
523  Double_t x1, y1, x2, y2;
524  GetStartPoint(x1, y1);
525  GetEndPoint(x2, y2);
526  Double_t xmin = TMath::Min(x1, x2);
527  Double_t xmax = TMath::Max(x1, x2);
528  Double_t ymin = TMath::Min(y1, y2);
529  Double_t ymax = TMath::Max(y1, y2);
530 
531  Bool_t in_range_x = (x <= xmax && x >= xmin);
532  Bool_t in_range_y = (y <= ymax && y >= ymin);
533  if (ax == "X") {
534  return in_range_x;
535  }
536  else if (ax == "Y") {
537  return in_range_y;
538  }
539 
540  return (in_range_x && in_range_y);
541 }
542 
543 #endif
int Int_t
#define d(i)
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
const char Option_t
#define ClassDef(name, id)
#define X(type, name)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
float xmin
float ymin
float xmax
float ymax
A lightweight replacement for the TVector2 class.
Definition: KVIDLine.h:157
MyVector2(const MyVector2 &T)
Definition: KVIDLine.h:161
friend MyVector2 operator*(const MyVector2 &a, double s)
Definition: KVIDLine.h:181
friend MyVector2 operator*(double s, const MyVector2 &a)
Definition: KVIDLine.h:185
double Mod() const
Definition: KVIDLine.h:193
friend double operator*(const MyVector2 &a, const MyVector2 &b)
Definition: KVIDLine.h:177
MyVector2 Proj(const MyVector2 &v) const
Definition: KVIDLine.h:197
double Mod2() const
Definition: KVIDLine.h:189
Double_t Phi() const
Definition: KVIDLine.h:207
friend MyVector2 operator-(const MyVector2 &a, const MyVector2 &b)
Definition: KVIDLine.h:173
MyVector2(double X, double Y)
Definition: KVIDLine.h:160
MyVector2 Norm(const MyVector2 &v) const
Definition: KVIDLine.h:203
MyVector2 & operator=(const MyVector2 &T)
Definition: KVIDLine.h:165
Base class for lines/cuts used for particle identification in 2D data maps.
Definition: KVIDLine.h:143
static KVIDLine * MakeIDLine(TObject *obj, Double_t xdeb=-1., Double_t xfin=-1., Double_t np=1., Bool_t save=kFALSE)
Definition: KVIDLine.cpp:88
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
Definition: KVIDLine.cpp:226
Bool_t PosRelToLine(Option_t *opt, Double_t px, Double_t py, Double_t xp1, Double_t yp1, Double_t xp2, Double_t yp2)
virtual ~ KVIDLine()
Double_t DistanceToLine(Double_t px, Double_t py, Int_t &)
Definition: KVIDLine.h:246
Bool_t WhereAmI(Double_t px, Double_t py, Option_t *opt)
void GetStartPoint(Double_t &x, Double_t &y) const
Bool_t IsBetweenEndPoints(Double_t x, Double_t y, const Char_t *axis="") const
KVIDLine()
Default ctor.
Definition: KVIDLine.cpp:38
void GetEndPoint(Double_t &x, Double_t &y) const
virtual void WaitForPrimitive()
Definition: KVIDLine.cpp:194
Base class for graphical cuts used in particle identification.
Definition: KVIDentifier.h:28
Int_t fNpoints
Int_t GetN() const
Double_t * fY
Double_t * fX
virtual Int_t GetPoint(Int_t i, Double_t &x, Double_t &y) const
Expr< UnaryOp< Sqrt< T >, SMatrix< T, D, D2, R >, T >, T, D, D2, R > sqrt(const SMatrix< T, D, D2, R > &rhs)
Double_t y[n]
Double_t x[n]
double T(double x)
Double_t Min(Double_t a, Double_t b)
Double_t Sign(Double_t a, Double_t b)
Double_t ATan2(Double_t y, Double_t x)
constexpr Double_t Pi()
Double_t Abs(Double_t d)
Double_t Max(Double_t a, Double_t b)
constexpr Double_t RadToDeg()
v
TArc a