]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONSegment.cxx
Coverity inspired fix
[u/mrichter/AliRoot.git] / MUON / AliMUONSegment.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 // $Id$
17
18 /// \class AliMUONSegment
19 ///
20 /// A basic line segment, to be used in contour making algorithms. 
21 ///
22 /// In particular, this class defines what a left or right edge is.
23 ///
24 /// Also, please note that, due to the way Root collections are sorted (relying
25 /// on TObject::Compare method), the way the AliMUONSegment::Compare method
26 /// is implemented below is really important when it comes to understand
27 /// contour making algorithm. Keep that in mind. 
28 ///
29 /// \author Laurent Aphecetche, Subatech
30 ///
31
32 #include "AliMUONSegment.h"
33
34 #include "TMath.h"
35 #include "Riostream.h"
36 #include "AliMpConstants.h"
37
38 /// \cond CLASSIMP
39 ClassImp(AliMUONSegment)
40 /// \endcond
41
42 const Double_t AliMUONSegment::fgkPrecision(AliMpConstants::LengthTolerance());
43   
44 //_____________________________________________________________________________
45 AliMUONSegment::AliMUONSegment() : 
46 TObject(),
47 fStartX(), fStartY(), fEndX(), fEndY(), fSmallerY(), fIsHorizontal(), fIsVertical(),
48 fIsLeftEdge(), fIsRightEdge(), fIsAPoint(kTRUE)
49 {
50   /// Ctor
51   Set(fStartX,fStartY,fEndX,fEndY);
52 }
53
54 //_____________________________________________________________________________
55 AliMUONSegment::AliMUONSegment(Double_t xstart, Double_t ystart, Double_t xend, Double_t yend)
56 : TObject(),
57 fStartX(xstart), fStartY(ystart), fEndX(xend), fEndY(yend), fSmallerY(), fIsHorizontal(), fIsVertical(),
58 fIsLeftEdge(), fIsRightEdge(), fIsAPoint(kTRUE)
59 {
60   /// Ctor
61   Set(xstart,ystart,xend,yend);
62 }
63
64 //_____________________________________________________________________________
65 Bool_t
66 AliMUONSegment::AreEqual(double a, double b)
67 {
68   /// Whether the two floats are equal within the given precision
69   return (TMath::Abs(b-a) < fgkPrecision);
70 }
71
72 //_____________________________________________________________________________
73 Int_t   
74 AliMUONSegment::Compare(const TObject* obj) const
75 {
76   /// Compare method, which sort segments in ascending x order
77   /// if same x, insure that left edges are before right edges
78   /// within same x, order by increasing bottommost y
79   /// Mind your steps ! This method is critical to the contour merging algorithm !
80   
81   const AliMUONSegment* rhs = static_cast<const AliMUONSegment*>(obj);
82   
83   if ( AreEqual(StartX(),rhs->StartX()) )
84   {
85     if ( IsLeftEdge() && rhs->IsRightEdge() ) return -1;
86     if ( IsRightEdge() && rhs->IsLeftEdge() ) return 1;
87     if ( SmallerY() < rhs->SmallerY() ) return -1;
88     if ( SmallerY() > rhs->SmallerY() ) return 1;
89     return 0;
90   }
91   else if ( StartX() < rhs->StartX() )
92   {
93     return -1;
94   }
95   else //if ( StartX() > rhs->StartX() ) 
96   {
97     return 1;
98   }
99 }
100
101 //_____________________________________________________________________________
102 double AliMUONSegment::Top() const 
103 {
104   /// Max Y of the segment
105   return TMath::Max(fStartY,fEndY); 
106 }
107
108 //_____________________________________________________________________________
109 double AliMUONSegment::Distance() const 
110 {
111   /// Length of the segment
112   return TMath::Sqrt((fStartX-fEndX)*(fStartX-fEndX) +
113                      (fStartY-fEndY)*(fStartY-fEndY)); 
114 }
115
116 //_____________________________________________________________________________
117 void AliMUONSegment::Print(Option_t*) const
118 {
119   /// Printout
120   cout << AsString() << endl;
121 }
122
123 //_____________________________________________________________________________
124 const char* AliMUONSegment::AsString() const 
125 {
126   /// Return a string representation of this object
127   return Form("[ (%10.5f,%10.5f) -> (%10.5f,%10.5f) %s ] (d=%e)",fStartX,fStartY,fEndX,fEndY,
128               IsLeftEdge() ? "L" : ( IsRightEdge() ? "R" : ( IsHorizontal() ? "H" : "" )),
129               Distance() ); 
130 }  
131
132 //_____________________________________________________________________________
133 void 
134 AliMUONSegment::Set(Double_t xstart, Double_t ystart, Double_t xend, Double_t yend)
135 {
136   /// Set start and end point, and (re)compute internal values
137   fStartX = xstart;
138   fEndX = xend;
139   fStartY = ystart;
140   fEndY = yend;
141   fSmallerY = TMath::Min(fStartY,fEndY); 
142   fIsHorizontal = AreEqual(fStartY,fEndY); 
143   fIsVertical = AreEqual(fStartX,fEndX); 
144   fIsLeftEdge = fIsVertical && ( fStartY > fEndY );
145   fIsRightEdge = fIsVertical && ( fStartY < fEndY );
146   fIsAPoint = ( Distance() < fgkPrecision );
147 }
148