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