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