]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONSparseHisto.cxx
TClonesArray->Clone() removed
[u/mrichter/AliRoot.git] / MUON / AliMUONSparseHisto.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 "AliMUONSparseHisto.h"
19
20 #include <Riostream.h>
21 #include <TH1.h>
22 #include <TMath.h>
23 #include <TString.h>
24
25 /// \class AliMUONSparseHisto
26 ///
27 /// Tiny histogram-like class to hold some distributions of tracker data.
28 /// Only intent of this class is to minimize memory consumption, in
29 /// order to fit a maximum number of channel histograms into memory.
30 /// The rest is not supported ;-)
31 ///
32 /// \author Laurent Aphecetche, Subatech
33
34 /// \cond CLASSIMP
35 ClassImp(AliMUONSparseHisto)
36 /// \endcond
37
38 //______________________________________________________________________________
39 AliMUONSparseHisto::AliMUONSparseHisto(Double_t xmin, Double_t xmax)
40 : TObject(),
41 fNbins(0),
42 fArray(0x0),
43 fXmin(xmin),
44 fXmax(xmax),
45 fFactor((1<<Nbits())/(xmax-xmin))
46 {
47   /// ctor
48   SetBit(kOverflow,0);
49   SetBit(kUnderflow,0);
50 }
51
52 //______________________________________________________________________________
53 AliMUONSparseHisto::AliMUONSparseHisto(const AliMUONSparseHisto& rhs)
54 : TObject(rhs),
55 fNbins(0),
56 fArray(0x0),
57 fXmin(0.0),
58 fXmax(0.0),
59 fFactor(0.0)
60 {
61   /// copy ctor
62   rhs.Copy(*this);
63 }
64
65 //______________________________________________________________________________
66 AliMUONSparseHisto&
67 AliMUONSparseHisto::operator=(const AliMUONSparseHisto& rhs)
68 {
69   /// assignment operator
70   if ( this != &rhs )
71   {
72     rhs.Copy(*this);
73   }
74   return *this;
75 }
76
77 //______________________________________________________________________________
78 AliMUONSparseHisto::~AliMUONSparseHisto()
79 {
80   /// dtor
81   delete[] fArray;
82 }
83
84 //______________________________________________________________________________
85 void 
86 AliMUONSparseHisto::Clear(Option_t*)
87 {
88   /// Reset the content
89   delete[] fArray;
90   fArray = 0x0;
91   fNbins = 0;
92 }  
93
94 //______________________________________________________________________________
95 void 
96 AliMUONSparseHisto::Copy(TObject& object) const
97 {
98   /// Copy this to *object
99   TObject::Copy(object);
100   AliMUONSparseHisto& h = static_cast<AliMUONSparseHisto&>(object);
101   delete[] h.fArray;
102   h.fArray = 0x0;
103   h.fNbins = GetNbins();
104   h.fXmin = Xmin();
105   h.fXmax = Xmax();
106   h.fFactor = Factor();
107   
108   if ( GetNbins() > 0 )
109   {
110     h.fArray = new UInt_t[GetNbins()];
111     for ( Int_t i = 0; i < GetNbins(); ++i ) 
112     {
113       h.fArray[i] = GetBin(i);
114     }
115   }
116 }
117
118 //______________________________________________________________________________
119 Double_t 
120 AliMUONSparseHisto::DecodeValue(Int_t value) const
121 {
122   /// From internal integer to "original" double
123   return value/Factor() + Xmin();
124 }
125
126 //______________________________________________________________________________
127 Int_t 
128 AliMUONSparseHisto::EncodeValue(Double_t value) const
129 {
130   /// From original double value to internal integer
131   return TMath::Nint(Factor()*(value-Xmin()));
132 }
133
134 //______________________________________________________________________________
135 Int_t
136 AliMUONSparseHisto::BinCenter(UInt_t x) const
137 {
138   /// Extract binCenter part from x
139   
140   return ( x & 0xFFF00000 ) >> 20;
141 }
142
143 //______________________________________________________________________________
144 Int_t
145 AliMUONSparseHisto::BinContent(UInt_t x) const
146 {
147   /// Extract binContent part from x
148   
149   return (x & 0xFFFFF);
150 }
151
152 //______________________________________________________________________________
153 UInt_t 
154 AliMUONSparseHisto::Encode(Int_t binCenter, Int_t binContent) const
155 {
156   /// Convert (binCenter,binContent) into a single value
157   
158   return ( ( binCenter & 0xFFF ) ) << 20 | ( ( binContent & 0xFFFFF ) );
159 }
160
161 //______________________________________________________________________________
162 void 
163 AliMUONSparseHisto::Expand()
164 {
165   /// Make fArray of size n
166   if (!fArray || !fNbins) 
167   {
168     delete[] fArray;
169     fArray = new UInt_t[1];
170     fNbins = 1;
171   }
172   else
173   {
174     UInt_t* tmp = new UInt_t[fNbins+1];
175     for ( Int_t i = 0; i < fNbins; ++i ) 
176     {
177       tmp[i] = fArray[i];
178     }
179     delete[] fArray;
180     fArray = tmp;
181     ++fNbins;
182   }
183 }
184
185 //______________________________________________________________________________
186 Int_t 
187 AliMUONSparseHisto::Fill(Double_t value)
188 {
189   /// Fill
190   
191   if ( value < Xmin() ) 
192   {
193     SetBit(kUnderflow,1);
194     return -1;
195   }
196   
197   if ( value > Xmax() ) 
198   {
199     SetBit(kOverflow,1);
200     return -1;
201   }
202   
203   Int_t ivalue = EncodeValue(value);
204   
205   Int_t i = Find(ivalue);
206   
207   if ( i < 0 ) 
208   {
209     Int_t n = fNbins;
210     Expand();
211     fArray[n] = Encode(ivalue,1);
212     i = n;
213   }
214   else
215   {
216     Int_t bc = GetBinContent(i);
217     if ( bc < 0xFFFFF ) 
218     {
219       fArray[i] = Encode(ivalue,bc+1);
220     }
221   }
222     
223   return i;
224 }
225
226 //______________________________________________________________________________
227 Int_t 
228 AliMUONSparseHisto::Find(Int_t binCenter) const
229 {
230   /// Return the index in fArray of value, or -1 if not found
231   
232   for ( Int_t i = 0; i < GetNbins(); ++i ) 
233   {
234     if ( binCenter == GetBinCenter(i) ) return i;
235   }
236   return -1;
237 }
238
239 //______________________________________________________________________________
240 UInt_t 
241 AliMUONSparseHisto::GetBin(Int_t bin) const
242 {
243   /// Get bin, which is a compacted form of two integers : (binCenter,binContent)
244   /// where binCenter itself might be an integer-fied double value.
245   return fArray[bin];
246 }
247
248 //______________________________________________________________________________
249 Double_t 
250 AliMUONSparseHisto::GetBinCenter(Int_t bin) const
251 {
252   /// Get bin center
253   if ( bin < 0 ) return -FLT_MAX;
254   if ( bin >= GetNbins() ) return FLT_MAX;
255   
256   UInt_t i = GetBin(bin);
257   
258   return DecodeValue(BinCenter(i));
259 }
260
261 //______________________________________________________________________________
262 Int_t 
263 AliMUONSparseHisto::GetBinContent(Int_t bin) const
264 {
265   /// Get bin content
266   
267   if ( bin < 0 || bin >= GetNbins() ) return 0xFFFFFFFF;
268
269   UInt_t i = GetBin(bin);
270
271   return BinContent(i);
272 }
273
274 //______________________________________________________________________________
275 void 
276 AliMUONSparseHisto::Print(Option_t* opt) const
277 {
278   /// Printout
279   Int_t id1 = ( GetUniqueID() & 0xFFFF0000 ) >> 16;
280   Int_t id2 = GetUniqueID() & 0xFFFF;
281   
282   cout << "ID=(" << id1 << "," << id2 << ") n bins = " << GetNbins();
283   if ( HasUnderflow() ) cout << " has underflow(s)";
284   if ( HasOverflow() ) cout << " has overflow(s)";
285   cout << endl;
286   
287   TString sopt(opt);
288   sopt.ToUpper();
289   
290   if ( sopt.Contains("FULL") )
291   {
292     for ( Int_t i = 0; i < GetNbins(); ++i ) 
293     {
294       cout << Form("Bin (%10u) %e = %6d",GetBin(i),GetBinCenter(i),GetBinContent(i)) << endl;
295     }
296   }
297 }