]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/misc/AliL3FFloat.cxx
Added virtual function Init for setting the slice, patch and n_eta_seg information...
[u/mrichter/AliRoot.git] / HLT / misc / AliL3FFloat.cxx
1 //$Id$
2
3 // Author: Constantin Loizides <mailto:loizides@fi.uib.no>
4 //*-- Copyright & copy CL
5
6 #include <stream.h>
7 #include <math.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "AliL3Logging.h"
12 #include "AliL3Logger.h"
13 #include "AliL3FFloat.h"
14
15 /** \class AliL3FFloat
16 //<pre>
17 //----------------------------------------------------
18 // AliL3FFloat
19 //
20 // Fixed Floating Point class for debugging purposes.
21 //
22 // The class behaves like a normal Double_t class but
23 // calculates everything in respect to fDigits (eg. 
24 // fDigits=100 -> 2 digits behind the comma). Further-
25 // more it keeps the exact value in floating precision
26 // and gathers some statistical information about 
27 // its usage.
28 //</pre>
29  */
30
31 ClassImp(AliL3FFloat)
32
33 Int_t AliL3FFloat::fDigits = DEFDIG;
34 Int_t AliL3FFloat::fMax    = DEFMAX;
35 Int_t AliL3FFloat::fMin    = DEFMIN;
36 Char_t AliL3FFloat::fQuery[10] = "%.2f";
37
38 #ifdef CALCSTATS
39 Int_t AliL3FFloat::fN          = 0;
40 Int_t AliL3FFloat::fNRounded   = 0;
41 Int_t AliL3FFloat::fNOpAdds    = 0;
42 Int_t AliL3FFloat::fNOpMults   = 0;
43 Int_t AliL3FFloat::fNOpDivs    = 0;
44 Int_t AliL3FFloat::fNOpSubs    = 0;
45 Int_t AliL3FFloat::fNOverFlow  = 0;
46 Int_t AliL3FFloat::fNUnderFlow = 0;
47 Double_t AliL3FFloat::fNDiff   = 0;
48 #endif
49
50
51 AliL3FFloat::~AliL3FFloat()
52 {
53 #ifdef CALCSTATS
54   fNDiff+=fabs(fVal-fExactVal);
55 #endif
56 }
57
58 ostream& operator<<(ostream &os, const AliL3FFloat &f) 
59 {
60   //  os << (Double_t)f << endl; 
61   os << (Double_t)f << "(" << f.fExactVal << ")"; 
62   return os;
63 }
64
65 AliL3FFloat operator + (const AliL3FFloat &f1,const AliL3FFloat &f2)
66 {
67   AliL3FFloat r(f1); 
68   r+=f2; 
69   return r;
70 }
71
72 AliL3FFloat operator + (const AliL3FFloat &f1,const Double_t f2)
73 {
74   AliL3FFloat r(f1); 
75   r+=f2; 
76   return r;
77 }
78
79 AliL3FFloat operator + (const Double_t f1,const AliL3FFloat &f2)
80 {
81   AliL3FFloat r(f1); 
82   r+=f2; 
83   return r;
84 }
85
86 AliL3FFloat operator + (const AliL3FFloat &f)
87 {
88   AliL3FFloat r(f); 
89   return r;
90 }
91
92
93 AliL3FFloat operator - (const AliL3FFloat &f1,const AliL3FFloat &f2)
94 {
95   AliL3FFloat r(f1); 
96   r-=f2; 
97   return r;
98 }
99
100 AliL3FFloat operator - (const AliL3FFloat &f1,const Double_t f2)
101 {
102   AliL3FFloat r(f1); 
103   r-=f2; 
104   return r;
105 }
106
107 AliL3FFloat operator - (const Double_t f1,const AliL3FFloat &f2)
108 {
109   AliL3FFloat r(f1); 
110   r-=f2; 
111   return r;
112 }
113
114 AliL3FFloat operator - (const AliL3FFloat &f)
115 {
116   AliL3FFloat r((-(Double_t)f)); 
117   return r;
118 }
119
120 AliL3FFloat operator * (const AliL3FFloat &f1,const AliL3FFloat &f2)
121 {
122   AliL3FFloat r(f1); 
123   r*=f2; 
124   return r;
125 }
126
127 AliL3FFloat operator * (const AliL3FFloat &f1,const Double_t f2)
128 {
129   AliL3FFloat r(f1); 
130   r*=f2; 
131   return r;
132 }
133
134 AliL3FFloat operator * (const Double_t f1,const AliL3FFloat &f2)
135 {
136   AliL3FFloat r(f1); 
137   r*=f2; 
138   return r;
139 }
140
141 AliL3FFloat operator / (const AliL3FFloat &f1,const AliL3FFloat &f2)
142 {
143   AliL3FFloat r(f1); 
144   r/=f2; 
145   return r;
146 }
147
148 AliL3FFloat operator / (const AliL3FFloat &f1,const Double_t f2)
149 {
150   AliL3FFloat r(f1); 
151   r/=f2; 
152   return r;
153 }
154
155 AliL3FFloat operator / (const Double_t f1,const AliL3FFloat &f2)
156 {
157   AliL3FFloat r(f1); 
158   r/=f2; 
159   return r;
160 }
161
162 AliL3FFloat& AliL3FFloat::operator += (const AliL3FFloat &f)
163 {
164   Double_t ev=fExactVal+f.GetExactVal();
165   Set(fVal+(Double_t)f); 
166   fExactVal=ev;
167 #ifdef CALCSTATS
168   fNOpAdds++; 
169 #endif
170   return *this;
171 }
172
173 AliL3FFloat& AliL3FFloat::operator += (const Double_t f)     
174 {
175   Double_t ev=fExactVal+f;
176   Set(fVal+Round(f));   
177   fExactVal=ev;
178 #ifdef CALCSTATS
179   fNOpAdds++; 
180 #endif
181   return *this;
182 }
183
184 AliL3FFloat& AliL3FFloat::operator -= (const AliL3FFloat &f) 
185 {
186   Double_t ev=fExactVal-f.GetExactVal();
187   Set(fVal-(Double_t)f);
188   fExactVal=ev;
189 #ifdef CALCSTATS
190   fNOpSubs++; 
191 #endif
192   return *this;
193 }
194
195 AliL3FFloat& AliL3FFloat::operator -= (const Double_t f)
196 {
197   Double_t ev=fExactVal-f;
198   Set(fVal-Round(f)); 
199   fExactVal=ev;
200 #ifdef CALCSTATS
201   fNOpSubs++; 
202 #endif
203   return *this;
204 }
205
206 AliL3FFloat& AliL3FFloat::operator *= (const AliL3FFloat &f) 
207 {
208   Double_t ev=fExactVal*f.GetExactVal();
209   Set(fVal*(Double_t)f);
210   fExactVal=ev;
211 #ifdef CALCSTATS
212   fNOpMults++;
213 #endif
214   return *this;
215 }
216
217 AliL3FFloat& AliL3FFloat::operator *= (const Double_t f)     
218 {
219   Double_t ev=fExactVal*f;
220   Set(fVal*Round(f));   
221   fExactVal=ev;
222 #ifdef CALCSTATS
223   fNOpMults++;
224 #endif
225   return *this;
226 }
227
228 AliL3FFloat& AliL3FFloat::operator /= (const AliL3FFloat &f) 
229 {
230   Double_t ev=fExactVal/f.GetExactVal();
231   Set(fVal/(Double_t)f);
232   fExactVal=ev;
233 #ifdef CALCSTATS
234   fNOpDivs++; 
235 #endif
236   return *this;
237 }
238
239 AliL3FFloat& AliL3FFloat::operator /= (const Double_t f)     
240 {
241   Double_t ev=fExactVal/f;
242   Set(fVal/Round(f));   
243   fExactVal=ev;
244 #ifdef CALCSTATS
245   fNOpDivs++; 
246 #endif
247   return *this;
248 }
249
250 inline void AliL3FFloat::Set(Double_t val)
251 {
252   fVal=Round(val);
253   fExactVal=val;
254   CheckBounds();
255 #ifdef CALCSTATS
256   fN++;
257 #endif
258 }
259
260 inline void AliL3FFloat::Set(AliL3FFloat &f)
261 {
262   fVal=(Double_t)f;
263   fExactVal=f.GetExactVal();
264   CheckBounds();
265 #ifdef CALCSTATS
266   fN++;
267 #endif
268 }
269
270 #ifdef FASTWITHROUNDINDERROS
271 inline Double_t AliL3FFloat::Round(Double_t val)
272 {
273   Int_t dummy=Int_t(fDigits*val);
274   Double_t ret=(Double_t)(dummy)/fDigits;
275 #ifdef CALCSTATS
276   if(ret!=val) fNRounded++;
277 #endif
278   return ret;
279 }
280 #else
281 inline Double_t AliL3FFloat::Round(Double_t val)
282 {
283   static Char_t strnum[100];
284   sprintf(strnum,fQuery,val);
285   Double_t ret=atof(strnum);
286 #ifdef CALCSTATS
287   if(ret!=val) fNRounded++;
288 #endif
289   return ret;
290 }
291 #endif
292
293 inline Bool_t AliL3FFloat::CheckUpperBound()
294 {
295   if(fVal>fMax){
296     fVal=fMax;
297 #ifdef CALCSTATS
298     fNOverFlow++;
299 #endif
300     return kFALSE;
301   }
302
303   return kTRUE;
304 }
305
306 inline Bool_t AliL3FFloat::CheckLowerBound()
307 {
308   if(fVal<fMin){
309     fVal=fMin;
310 #ifdef CALCSTATS
311     fNUnderFlow++;
312 #endif
313     return kFALSE;
314   }
315
316   return kTRUE;
317 }
318
319 void AliL3FFloat::SetParams(Int_t dig,Int_t min,Int_t max)
320 {
321   fDigits=dig;
322   Int_t prec=0;
323   if(fDigits>0) prec=(Int_t)log10(fDigits);
324   sprintf(fQuery,"%%.%df",prec);
325   fMin=min;
326   fMax=max;
327 }
328
329 void AliL3FFloat::PrintStat(){
330 #ifdef CALCSTATS
331   cout << "fN:            " << fN << endl;
332   cout << "fNRounded:     " << fNRounded << endl;
333   cout << "fNOpAdds:      " << fNOpAdds << endl;
334   cout << "fNOpSubs:      " << fNOpSubs << endl;
335   cout << "fNOpMults:     " << fNOpMults << endl;
336   cout << "fNOpDivs:      " << fNOpDivs << endl;
337   cout << "fNOpOverFlow:  " << fNOverFlow << endl;
338   cout << "fNOpUnderFlow: " << fNUnderFlow << endl;
339   if(fN) cout << "fNDiff:        " << fNDiff/fN << endl;
340 #else
341   cerr << "Not compiled with #define CALCSTATS!" << endl;
342 #endif
343
344 }