1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 #include "AliMUONSparseHisto.h"
21 #include <Riostream.h>
26 /// \class AliMUONSparseHisto
28 /// Tiny histogram-like class to hold some distributions of tracker data.
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 ;-)
33 /// \author Laurent Aphecetche, Subatech
38 ClassImp(AliMUONSparseHisto)
41 //______________________________________________________________________________
42 AliMUONSparseHisto::AliMUONSparseHisto(Double_t xmin, Double_t xmax)
48 fFactor((1<<Nbits())/(xmax-xmin))
55 //______________________________________________________________________________
56 AliMUONSparseHisto::AliMUONSparseHisto(const AliMUONSparseHisto& rhs)
68 //______________________________________________________________________________
70 AliMUONSparseHisto::operator=(const AliMUONSparseHisto& rhs)
72 /// assignment operator
80 //______________________________________________________________________________
81 AliMUONSparseHisto::~AliMUONSparseHisto()
87 //______________________________________________________________________________
89 AliMUONSparseHisto::Add(const AliMUONSparseHisto& h)
93 if ( fXmin != h.Xmin() || fXmax != h.Xmax() )
95 AliError("Cannot add sparse histograms with different limits !");
99 for ( Int_t i = 0; i < h.GetNbins(); ++i )
101 Fill(h.GetBinContent(i));
107 //______________________________________________________________________________
109 AliMUONSparseHisto::Clear(Option_t*)
111 /// Reset the content
117 //______________________________________________________________________________
119 AliMUONSparseHisto::Copy(TObject& object) const
121 /// Copy this to *object
122 TObject::Copy(object);
123 AliMUONSparseHisto& h = static_cast<AliMUONSparseHisto&>(object);
126 h.fNbins = GetNbins();
129 h.fFactor = Factor();
131 if ( GetNbins() > 0 )
133 h.fArray = new UInt_t[GetNbins()];
134 for ( Int_t i = 0; i < GetNbins(); ++i )
136 h.fArray[i] = GetBin(i);
141 //______________________________________________________________________________
143 AliMUONSparseHisto::DecodeValue(Int_t value) const
145 /// From internal integer to "original" double
146 return value/Factor() + Xmin();
149 //______________________________________________________________________________
151 AliMUONSparseHisto::EncodeValue(Double_t value) const
153 /// From original double value to internal integer
154 return TMath::Nint(Factor()*(value-Xmin()));
157 //______________________________________________________________________________
159 AliMUONSparseHisto::BinCenter(UInt_t x) const
161 /// Extract binCenter part from x
163 return ( x & 0xFFF00000 ) >> 20;
166 //______________________________________________________________________________
168 AliMUONSparseHisto::BinContent(UInt_t x) const
170 /// Extract binContent part from x
172 return (x & 0xFFFFF);
175 //______________________________________________________________________________
177 AliMUONSparseHisto::Encode(Int_t binCenter, Int_t binContent) const
179 /// Convert (binCenter,binContent) into a single value
181 return ( ( binCenter & 0xFFF ) ) << 20 | ( ( binContent & 0xFFFFF ) );
184 //______________________________________________________________________________
186 AliMUONSparseHisto::Expand()
188 /// Make fArray of size n
189 if (!fArray || !fNbins)
192 fArray = new UInt_t[1];
197 UInt_t* tmp = new UInt_t[fNbins+1];
198 for ( Int_t i = 0; i < fNbins; ++i )
208 //______________________________________________________________________________
210 AliMUONSparseHisto::Fill(Double_t value)
214 if ( value < Xmin() )
216 SetBit(kUnderflow,1);
220 if ( value > Xmax() )
226 Int_t ivalue = EncodeValue(value);
228 Int_t i = Find(ivalue);
234 fArray[n] = Encode(ivalue,1);
239 Int_t bc = GetBinContent(i);
242 fArray[i] = Encode(ivalue,bc+1);
249 //______________________________________________________________________________
251 AliMUONSparseHisto::Find(Int_t binCenter) const
253 /// Return the index in fArray of value, or -1 if not found
255 for ( Int_t i = 0; i < GetNbins(); ++i )
257 if ( binCenter == GetBinCenter(i) ) return i;
262 //______________________________________________________________________________
264 AliMUONSparseHisto::GetBin(Int_t bin) const
266 /// Get bin, which is a compacted form of two integers : (binCenter,binContent)
267 /// where binCenter itself might be an integer-fied double value.
271 //______________________________________________________________________________
273 AliMUONSparseHisto::GetBinCenter(Int_t bin) const
276 if ( bin < 0 ) return -FLT_MAX;
277 if ( bin >= GetNbins() ) return FLT_MAX;
279 UInt_t i = GetBin(bin);
281 return DecodeValue(BinCenter(i));
284 //______________________________________________________________________________
286 AliMUONSparseHisto::GetBinContent(Int_t bin) const
290 if ( bin < 0 || bin >= GetNbins() ) return 0xFFFFFFFF;
292 UInt_t i = GetBin(bin);
294 return BinContent(i);
297 //______________________________________________________________________________
299 AliMUONSparseHisto::Print(Option_t* opt) const
302 Int_t id1 = ( GetUniqueID() & 0xFFFF0000 ) >> 16;
303 Int_t id2 = GetUniqueID() & 0xFFFF;
305 cout << "ID=(" << id1 << "," << id2 << ") n bins = " << GetNbins();
306 if ( HasUnderflow() ) cout << " has underflow(s)";
307 if ( HasOverflow() ) cout << " has overflow(s)";
313 if ( sopt.Contains("FULL") )
315 for ( Int_t i = 0; i < GetNbins(); ++i )
317 cout << Form("Bin (%10u) %e = %6d",GetBin(i),GetBinCenter(i),GetBinContent(i)) << endl;