]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONCalibParamNF.cxx
adding protections
[u/mrichter/AliRoot.git] / MUON / AliMUONCalibParamNF.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 #include "AliMUONCalibParamNF.h"
19
20 #include "AliLog.h"
21
22 #include "Riostream.h"
23 #include "TMath.h"
24 #include "TString.h"
25
26 #include <limits.h>
27
28 //-----------------------------------------------------------------------------
29 /// \class AliMUONCalibParamNF
30 ///
31 /// Handle the case of N floating point parameters per channel.
32 ///
33 /// \author Laurent Aphecetche
34 //-----------------------------------------------------------------------------
35
36 using std::cout;
37 using std::endl;
38 /// \cond CLASSIMP
39 ClassImp(AliMUONCalibParamNF)
40 /// \endcond
41
42 //_____________________________________________________________________________
43 AliMUONCalibParamNF::AliMUONCalibParamNF() 
44 : AliMUONVCalibParam(),
45   fDimension(0),
46   fSize(0),
47   fN(0),
48   fValues(0x0)
49 {
50 /// Default constructor.
51 }
52
53 //_____________________________________________________________________________
54 AliMUONCalibParamNF::AliMUONCalibParamNF(Int_t dimension, Int_t theSize, 
55                                          Int_t id0, Int_t id1,
56                                          Float_t fillWithValue) 
57 : AliMUONVCalibParam(id0,id1),
58   fDimension(dimension),
59   fSize(theSize),
60   fN(fSize*fDimension),
61   fValues(0x0)
62 {
63 /// Normal constructor, where theSize specifies the number of channels handled
64 /// by this object, and fillWithValue the default value assigned to each
65 /// channel.
66
67   if ( fN > 0 )
68   {
69     fValues = new Float_t[fN];
70     for ( Int_t i = 0; i < fN; ++i )
71     {
72       fValues[i] = fillWithValue;
73     }
74   }
75 }
76
77
78 //_____________________________________________________________________________
79 AliMUONCalibParamNF::AliMUONCalibParamNF(const AliMUONCalibParamNF& other) 
80 : AliMUONVCalibParam(),
81 fDimension(0),
82 fSize(0),
83 fN(0),
84 fValues(0x0)
85 {
86 /// Copy constructor.
87
88   other.CopyTo(*this);
89 }
90
91 //_____________________________________________________________________________
92 AliMUONCalibParamNF&
93 AliMUONCalibParamNF::operator=(const AliMUONCalibParamNF& other) 
94 {
95 /// Assignment operator
96
97   other.CopyTo(*this);
98   return *this;
99 }
100
101 //_____________________________________________________________________________
102 AliMUONCalibParamNF::~AliMUONCalibParamNF()
103 {
104 /// Destructor
105
106   delete[] fValues;
107 }
108
109 //_____________________________________________________________________________
110 void
111 AliMUONCalibParamNF::CopyTo(AliMUONCalibParamNF& destination) const
112 {
113 /// Copy *this to destination
114
115   TObject::Copy(destination);
116   
117   delete[] destination.fValues;
118   destination.fN = fN;
119   destination.fSize = fSize;
120   destination.fDimension = fDimension;
121   
122   if ( fN > 0 )
123   {
124     destination.fValues = new Float_t[fN];
125     for ( Int_t i = 0; i < fN; ++i )
126     {
127       destination.fValues[i] = fValues[i];
128     }
129   }
130 }
131
132 //_____________________________________________________________________________
133 Int_t
134 AliMUONCalibParamNF::Index(Int_t i, Int_t j) const
135 {
136 /// Compute the 1D index of the internal storage from the pair (i,j)
137 /// Returns -1 if the (i,j) pair is invalid
138
139   if ( i >= 0 && i < Size() && j >= 0 && j < Dimension() )
140   {
141     return IndexFast(i,j);
142   }
143   return -1;
144 }
145
146 //_____________________________________________________________________________
147 Int_t
148 AliMUONCalibParamNF::IndexFast(Int_t i, Int_t j) const
149 {
150   /// Compute the 1D index of the internal storage from the pair (i,j)
151   
152   return i + Size()*j;
153 }
154
155 //_____________________________________________________________________________
156 void
157 AliMUONCalibParamNF::Print(Option_t* opt) const
158 {
159 /// Output this object to stdout.
160 /// If opt=="full", then all channels are printed, 
161 /// if opt=="mean#", only the mean and sigma value are printed for j-th dimension
162 /// otherwise only the general characteristics are printed.
163
164   TString sopt(opt);
165   sopt.ToUpper();
166   cout << Form("AliMUONCalibParamNF Id=(%d,%d) Size=%d Dimension=%d",ID0(),
167                ID1(),Size(),Dimension()) << endl;
168   if ( sopt.Contains("FULL") )
169   {
170     for ( Int_t i = 0; i < Size(); ++i )
171     {
172       cout << Form("CH %3d",i);
173       for ( Int_t j = 0; j < Dimension(); ++j )
174       {
175         cout << Form(" %e",ValueAsFloat(i,j));
176       }
177       cout << endl;
178     }
179   }  
180   if ( sopt.Contains("MEAN") )
181   {
182     Int_t j(0);
183     sscanf(sopt.Data(),"MEAN%d",&j);
184     
185     Float_t mean(0);
186     Float_t v2(0);
187     
188     Int_t n = Size();
189     
190     for ( Int_t i = 0; i < Size(); ++i )
191     {
192       Float_t v = ValueAsFloat(i,j);
193       mean += v;
194       v2 += v*v;
195     }
196     mean /= n;
197     float sigma = 0;
198     if ( n > 1 ) sigma = TMath::Sqrt( (v2-n*mean*mean)/(n-1) );
199     cout << Form(" Mean(j=%d)=%e Sigma(j=%d)=%e",j,mean,j,sigma) << endl;
200   }
201   
202 }
203
204 //_____________________________________________________________________________
205 void
206 AliMUONCalibParamNF::SetValueAsFloat(Int_t i, Int_t j, Float_t value)
207 {
208 /// Set one value as a float, after checking that the indices are correct.
209
210   Int_t ix = Index(i,j);
211   
212   if ( ix < 0 )
213   {
214     AliError(Form("Invalid (i,j)=(%d,%d) max allowed is (%d,%d)",
215                   i,j,Size()-1,Dimension()-1));
216   }
217   else
218   {
219     fValues[ix]=value;
220   }
221 }
222
223 //_____________________________________________________________________________
224 void
225 AliMUONCalibParamNF::SetValueAsFloatFast(Int_t i, Int_t j, Float_t value)
226 {
227   /// Set one value as a float, w/o checking that the indices are correct.
228   
229   fValues[IndexFast(i,j)] = value;
230 }
231
232 //_____________________________________________________________________________
233 void
234 AliMUONCalibParamNF::SetValueAsInt(Int_t i, Int_t j, Int_t value)
235 {
236 /// Set one value as an int.
237
238   SetValueAsFloat(i,j,static_cast<Float_t>(value));
239 }
240
241 //_____________________________________________________________________________
242 void
243 AliMUONCalibParamNF::SetValueAsIntFast(Int_t i, Int_t j, Int_t value)
244 {
245   /// Set one value as an int.
246   
247   SetValueAsFloatFast(i,j,static_cast<Float_t>(value));
248 }
249
250 //_____________________________________________________________________________
251 Float_t
252 AliMUONCalibParamNF::ValueAsFloat(Int_t i, Int_t j) const
253 {
254 /// Return the value as a float (which it is), after checking indices.
255
256   Int_t ix = Index(i,j);
257   
258   if ( ix < 0 )
259   {
260     AliError(Form("Invalid (i,j)=(%d,%d) max allowed is (%d,%d)",
261                   i,j,Size()-1,Dimension()-1));
262     return 0.0;
263   }
264   else
265   {
266     return fValues[ix];
267   }
268 }
269
270 //_____________________________________________________________________________
271 Float_t
272 AliMUONCalibParamNF::ValueAsFloatFast(Int_t i, Int_t j) const
273 {
274   /// Return the value as a float (which it is), after checking indices.
275   
276   return fValues[IndexFast(i,j)];
277 }
278
279 //_____________________________________________________________________________
280 Int_t
281 AliMUONCalibParamNF::ValueAsInt(Int_t i, Int_t j) const
282 {
283 /// Return the value as an int, by rounding the internal float value.
284
285   Float_t v = ValueAsFloat(i,j);
286   
287   if ( v >= Float_t(INT_MAX) ) {
288     AliErrorStream() 
289       << "Cannot convert value " << v << " to Int_t." << endl;
290     return 0;
291   }  
292   
293   return TMath::Nint(v);
294 }
295
296 //_____________________________________________________________________________
297 Int_t
298 AliMUONCalibParamNF::ValueAsIntFast(Int_t i, Int_t j) const
299 {
300   /// Return the value as an int, by rounding the internal float value.
301   
302   Float_t v = ValueAsFloatFast(i,j);
303   return TMath::Nint(v);
304 }