]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONCalibParamNI.cxx
Correct function Compare() for "pixels" from MLEM cluster finder.
[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     AliDebug(1,Form("this=%p default ctor",this));
54 }
55
56 //_____________________________________________________________________________
57 AliMUONCalibParamNI::AliMUONCalibParamNI(Int_t dimension, Int_t theSize,
58                                          Int_t id0, Int_t id1,
59                                          Int_t fillWithValue, Int_t packingFactor) 
60 : AliMUONVCalibParam(id0,id1),
61   fDimension(dimension),
62   fSize(theSize),
63   fN(fSize*fDimension),
64   fPackingFactor(packingFactor),
65   fValues(0x0)
66 {
67 /// Normal constructor, where theSize specifies the number of channels handled
68 /// by this object, and fillWithValue the default value assigned to each
69 /// channel.
70
71     AliDebug(1,Form("this=%p dimension=%d theSize=%d fillWithValue=%d packingFactor=%d",
72                     this,dimension,theSize,fillWithValue,packingFactor));
73
74   if ( fN > 0 )
75   {
76     fValues = new Int_t[fN];
77     for ( Int_t i = 0; i < fN; ++i )
78     {
79       fValues[i] = fillWithValue;
80     }
81   }
82 }
83
84
85 //_____________________________________________________________________________
86 AliMUONCalibParamNI::AliMUONCalibParamNI(const AliMUONCalibParamNI& other) 
87 : AliMUONVCalibParam(),
88 fDimension(0),
89 fSize(0),
90 fN(0),
91 fPackingFactor(0),
92 fValues(0x0)
93 {
94 /// Copy constructor.
95
96   AliDebug(1,Form("this=%p copy ctor",this));
97   other.CopyTo(*this);
98 }
99
100 //_____________________________________________________________________________
101 AliMUONCalibParamNI&
102 AliMUONCalibParamNI::operator=(const AliMUONCalibParamNI& other) 
103 {
104 /// Assignment operator
105
106   other.CopyTo(*this);
107   return *this;
108 }
109
110 //_____________________________________________________________________________
111 AliMUONCalibParamNI::~AliMUONCalibParamNI()
112 {
113 /// Destructor
114
115   AliDebug(1,Form("this=%p",this));
116   delete[] fValues;
117 }
118
119 //_____________________________________________________________________________
120 void
121 AliMUONCalibParamNI::CopyTo(AliMUONCalibParamNI& destination) const
122 {
123 /// Copy *this to destination
124
125   const TObject& o = static_cast<const TObject&>(*this);
126   o.Copy(destination);
127   
128   delete[] destination.fValues;
129   destination.fN = fN;
130   destination.fSize = fSize;
131   destination.fDimension = fDimension;
132   destination.fPackingFactor = fPackingFactor;
133   
134   if ( fN > 0 )
135   {
136     destination.fValues = new Int_t[fN];
137     for ( Int_t i = 0; i < fN; ++i )
138     {
139       destination.fValues[i] = fValues[i];
140     }
141   }
142 }
143
144 //_____________________________________________________________________________
145 Int_t
146 AliMUONCalibParamNI::Index(Int_t i, Int_t j) const
147 {
148 /// Compute the 1D index of the internal storage from the pair (i,j)
149 /// Returns -1 if the (i,j) pair is invalid
150
151   if ( i >= 0 && i < Size() && j >= 0 && j < Dimension() )
152   {
153     return i + Size()*j;
154   }
155   return -1;
156 }
157
158 //_____________________________________________________________________________
159 void
160 AliMUONCalibParamNI::Print(Option_t* opt) const
161 {
162 /// Output this object to stdout.
163 /// If opt=="full" then all channels are printed, 
164 /// if opt=="mean#", only the mean and sigma value are printed for j-th dimension
165 /// otherwise only the general characteristics are printed.
166
167   TString sopt(opt);
168   sopt.ToUpper();
169   cout << "AliMUONCalibParamNI - Size=" << Size()
170     << " Dimension=" << Dimension()
171     << " Id=(" << ID0() << "," << ID1() << ")";
172   
173   if ( IsPacked() ) 
174   {
175     cout << " Packing Factor=" << fPackingFactor;
176   }
177   cout << endl;
178   
179   if ( sopt.Contains("FULL") )
180   {
181     for ( Int_t i = 0; i < Size(); ++i )
182     {
183       cout << Form("CH %3d",i);
184       for ( Int_t j = 0; j < Dimension(); ++j )
185       {
186         Int_t v = ValueAsInt(i,j);
187         if ( IsPacked() )
188         {
189           Int_t m,c;
190           UnpackValue(v,m,c);
191           cout << Form(" (%d,%d) (0x%x,0x%x)",m,c,m,c);
192         }
193         else
194         {
195           cout << Form(" %d (0x%x)",v,v);
196         }
197       }
198       cout << endl;
199     }
200   }  
201   if ( sopt.Contains("MEAN") )
202   {
203     Int_t j(0);
204     sscanf(sopt.Data(),"MEAN%d",&j);
205     
206     Float_t mean(0);
207     Float_t v2(0);
208     
209     Int_t n = Size();
210     
211     for ( Int_t i = 0; i < Size(); ++i )
212     {
213       Float_t v = ValueAsFloat(i,j);
214       mean += v;
215       v2 += v*v;
216     }
217     mean /= n;
218     float sigma = 0;
219     if ( n > 1 ) sigma = TMath::Sqrt( (v2-n*mean*mean)/(n-1) );
220     cout << Form(" Mean(j=%d)=%e Sigma(j=%d)=%e",j,mean,j,sigma) << endl;
221   }
222   
223 }
224
225 //_____________________________________________________________________________
226 void
227 AliMUONCalibParamNI::SetValueAsInt(Int_t i, Int_t j, Int_t value)
228 {
229 /// Set one value as a float, after checking that the indices are correct.
230
231   Int_t ix = Index(i,j);
232   
233   if ( ix < 0 )
234   {
235     AliError(Form("Invalid (i,j)=(%d,%d) max allowed is (%d,%d)",
236                   i,j,Size()-1,Dimension()-1));
237   }
238   else
239   {
240     fValues[ix]=value;
241   }
242 }
243
244 //_____________________________________________________________________________
245 void
246 AliMUONCalibParamNI::SetValueAsFloat(Int_t i, Int_t j, Float_t value)
247 {
248   /// Set one value as an int.
249
250   AliWarning("Float will be rounded to be stored...");
251   SetValueAsInt(i,j,TMath::Nint(value));
252 }
253
254 //_____________________________________________________________________________
255 Int_t
256 AliMUONCalibParamNI::ValueAsInt(Int_t i, Int_t j) const
257 {
258 /// Return the value as an int (which it is), after checking indices.
259
260   Int_t ix = Index(i,j);
261   
262   if ( ix < 0 )
263   {
264     AliError(Form("Invalid (i,j)=(%d,%d) max allowed is (%d,%d)",
265                   i,j,Size()-1,Dimension()-1));
266     return 0;
267   }
268   else
269   {
270     return fValues[ix];
271   }
272 }
273
274 //_____________________________________________________________________________
275 Float_t
276 AliMUONCalibParamNI::ValueAsFloat(Int_t i, Int_t j) const
277 {
278   /// Return the value as a float
279   return static_cast<Float_t>(ValueAsInt(i,j));
280 }
281
282 //_____________________________________________________________________________
283 Bool_t 
284 AliMUONCalibParamNI::UnpackValue(Int_t value, Int_t& a, Int_t& b) const
285 {
286   /// Unpack single value into a couple (a,b), using packingFactor
287   /// Returns false if IsPacked()==false
288   
289   if ( !IsPacked() ) return kFALSE;
290   a = value / fPackingFactor;
291   b = value % fPackingFactor;
292   return kTRUE;
293 }
294
295 //_____________________________________________________________________________
296 Bool_t 
297 AliMUONCalibParamNI::PackValues(Int_t a, Int_t b, Int_t& packedValue) const
298 {
299   /// Pack a couple (a,b) into a single value, using packingFactor
300   /// Returns false if IsPacked()==false
301
302   if ( !IsPacked() ) return kFALSE;
303   packedValue = a*fPackingFactor + b;
304   return kTRUE;
305 }
306
307 //_____________________________________________________________________________
308 Bool_t 
309 AliMUONCalibParamNI::IsPacked() const
310 {
311   /// Whether the values we store are packed or not.
312   /// If false, Pack and Unpack methods will always return false and do nothing
313   return (fPackingFactor != 0);
314 }
315