ROOT logo
/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
*                                                                        *
* Author: The ALICE Off-line Project.                                    *
* Contributors are mentioned in the code where appropriate.              *
*                                                                        *
* Permission to use, copy, modify and distribute this software and its   *
* documentation strictly for non-commercial purposes is hereby granted   *
* without fee, provided that the above copyright notice appears in all   *
* copies and that both the copyright notice and this permission notice   *
* appear in the supporting documentation. The authors make no claims     *
* about the suitability of this software for any purpose. It is          *
* provided "as is" without express or implied warranty.                  *
**************************************************************************/

// $Id$

#include "AliMUONSparseHisto.h"

#include "AliLog.h"
#include <Riostream.h>
#include <TH1.h>
#include <TMath.h>
#include <TString.h>

/// \class AliMUONSparseHisto
///
/// Tiny histogram-like class to hold some distributions of tracker data.
/// Only intent of this class is to minimize memory consumption, in
/// order to fit a maximum number of channel histograms into memory.
/// The rest is not supported ;-)
///
/// \author Laurent Aphecetche, Subatech

using std::cout;
using std::endl;
/// \cond CLASSIMP
ClassImp(AliMUONSparseHisto)
/// \endcond

//______________________________________________________________________________
AliMUONSparseHisto::AliMUONSparseHisto(Double_t xmin, Double_t xmax)
: TObject(),
fNbins(0),
fArray(0x0),
fXmin(xmin),
fXmax(xmax),
fFactor((1<<Nbits())/(xmax-xmin))
{
  /// ctor
  SetBit(kOverflow,0);
  SetBit(kUnderflow,0);
}

//______________________________________________________________________________
AliMUONSparseHisto::AliMUONSparseHisto(const AliMUONSparseHisto& rhs)
: TObject(rhs),
fNbins(0),
fArray(0x0),
fXmin(0.0),
fXmax(0.0),
fFactor(0.0)
{
  /// copy ctor
  rhs.Copy(*this);
}

//______________________________________________________________________________
AliMUONSparseHisto&
AliMUONSparseHisto::operator=(const AliMUONSparseHisto& rhs)
{
  /// assignment operator
  if ( this != &rhs )
  {
    rhs.Copy(*this);
  }
  return *this;
}

//______________________________________________________________________________
AliMUONSparseHisto::~AliMUONSparseHisto()
{
  /// dtor
  delete[] fArray;
}

//______________________________________________________________________________
Bool_t 
AliMUONSparseHisto::Add(const AliMUONSparseHisto& h)
{
  /// Add h to this

  if ( fXmin != h.Xmin() || fXmax != h.Xmax() ) 
  {
    AliError("Cannot add sparse histograms with different limits !");
    return kFALSE;
  }
  
  for ( Int_t i = 0; i < h.GetNbins(); ++i ) 
  {
    Fill(h.GetBinContent(i));
  }  
  
  return kTRUE;
}

//______________________________________________________________________________
void 
AliMUONSparseHisto::Clear(Option_t*)
{
  /// Reset the content
  delete[] fArray;
  fArray = 0x0;
  fNbins = 0;
}  

//______________________________________________________________________________
void 
AliMUONSparseHisto::Copy(TObject& object) const
{
  /// Copy this to *object
  TObject::Copy(object);
  AliMUONSparseHisto& h = static_cast<AliMUONSparseHisto&>(object);
  delete[] h.fArray;
  h.fArray = 0x0;
  h.fNbins = GetNbins();
  h.fXmin = Xmin();
  h.fXmax = Xmax();
  h.fFactor = Factor();
  
  if ( GetNbins() > 0 )
  {
    h.fArray = new UInt_t[GetNbins()];
    for ( Int_t i = 0; i < GetNbins(); ++i ) 
    {
      h.fArray[i] = GetBin(i);
    }
  }
}

//______________________________________________________________________________
Double_t 
AliMUONSparseHisto::DecodeValue(Int_t value) const
{
  /// From internal integer to "original" double
  return value/Factor() + Xmin();
}

//______________________________________________________________________________
Int_t 
AliMUONSparseHisto::EncodeValue(Double_t value) const
{
  /// From original double value to internal integer
  return TMath::Nint(Factor()*(value-Xmin()));
}

//______________________________________________________________________________
Int_t
AliMUONSparseHisto::BinCenter(UInt_t x) const
{
  /// Extract binCenter part from x
  
  return ( x & 0xFFF00000 ) >> 20;
}

//______________________________________________________________________________
Int_t
AliMUONSparseHisto::BinContent(UInt_t x) const
{
  /// Extract binContent part from x
  
  return (x & 0xFFFFF);
}

//______________________________________________________________________________
UInt_t 
AliMUONSparseHisto::Encode(Int_t binCenter, Int_t binContent) const
{
  /// Convert (binCenter,binContent) into a single value
  
  return ( ( binCenter & 0xFFF ) ) << 20 | ( ( binContent & 0xFFFFF ) );
}

//______________________________________________________________________________
void 
AliMUONSparseHisto::Expand()
{
  /// Make fArray of size n
  if (!fArray || !fNbins) 
  {
    delete[] fArray;
    fArray = new UInt_t[1];
    fNbins = 1;
  }
  else
  {
    UInt_t* tmp = new UInt_t[fNbins+1];
    for ( Int_t i = 0; i < fNbins; ++i ) 
    {
      tmp[i] = fArray[i];
    }
    delete[] fArray;
    fArray = tmp;
    ++fNbins;
  }
}

//______________________________________________________________________________
Int_t 
AliMUONSparseHisto::Fill(Double_t value)
{
  /// Fill
  
  if ( value < Xmin() ) 
  {
    SetBit(kUnderflow,1);
    return -1;
  }
  
  if ( value > Xmax() ) 
  {
    SetBit(kOverflow,1);
    return -1;
  }
  
  Int_t ivalue = EncodeValue(value);
  
  Int_t i = Find(ivalue);
  
  if ( i < 0 ) 
  {
    Int_t n = fNbins;
    Expand();
    fArray[n] = Encode(ivalue,1);
    i = n;
  }
  else
  {
    Int_t bc = GetBinContent(i);
    if ( bc < 0xFFFFF ) 
    {
      fArray[i] = Encode(ivalue,bc+1);
    }
  }
    
  return i;
}

//______________________________________________________________________________
Int_t 
AliMUONSparseHisto::Find(Int_t binCenter) const
{
  /// Return the index in fArray of value, or -1 if not found
  
  for ( Int_t i = 0; i < GetNbins(); ++i ) 
  {
    if ( binCenter == GetBinCenter(i) ) return i;
  }
  return -1;
}

//______________________________________________________________________________
UInt_t 
AliMUONSparseHisto::GetBin(Int_t bin) const
{
  /// Get bin, which is a compacted form of two integers : (binCenter,binContent)
  /// where binCenter itself might be an integer-fied double value.
  return fArray[bin];
}

//______________________________________________________________________________
Double_t 
AliMUONSparseHisto::GetBinCenter(Int_t bin) const
{
  /// Get bin center
  if ( bin < 0 ) return -FLT_MAX;
  if ( bin >= GetNbins() ) return FLT_MAX;
  
  UInt_t i = GetBin(bin);
  
  return DecodeValue(BinCenter(i));
}

//______________________________________________________________________________
Int_t 
AliMUONSparseHisto::GetBinContent(Int_t bin) const
{
  /// Get bin content
  
  if ( bin < 0 || bin >= GetNbins() ) return 0xFFFFFFFF;

  UInt_t i = GetBin(bin);

  return BinContent(i);
}

//______________________________________________________________________________
void 
AliMUONSparseHisto::Print(Option_t* opt) const
{
  /// Printout
  Int_t id1 = ( GetUniqueID() & 0xFFFF0000 ) >> 16;
  Int_t id2 = GetUniqueID() & 0xFFFF;
  
  cout << "ID=(" << id1 << "," << id2 << ") n bins = " << GetNbins();
  if ( HasUnderflow() ) cout << " has underflow(s)";
  if ( HasOverflow() ) cout << " has overflow(s)";
  cout << endl;
  
  TString sopt(opt);
  sopt.ToUpper();
  
  if ( sopt.Contains("FULL") )
  {
    for ( Int_t i = 0; i < GetNbins(); ++i ) 
    {
      cout << Form("Bin (%10u) %e = %6d",GetBin(i),GetBinCenter(i),GetBinContent(i)) << endl;
    }
  }
}
 AliMUONSparseHisto.cxx:1
 AliMUONSparseHisto.cxx:2
 AliMUONSparseHisto.cxx:3
 AliMUONSparseHisto.cxx:4
 AliMUONSparseHisto.cxx:5
 AliMUONSparseHisto.cxx:6
 AliMUONSparseHisto.cxx:7
 AliMUONSparseHisto.cxx:8
 AliMUONSparseHisto.cxx:9
 AliMUONSparseHisto.cxx:10
 AliMUONSparseHisto.cxx:11
 AliMUONSparseHisto.cxx:12
 AliMUONSparseHisto.cxx:13
 AliMUONSparseHisto.cxx:14
 AliMUONSparseHisto.cxx:15
 AliMUONSparseHisto.cxx:16
 AliMUONSparseHisto.cxx:17
 AliMUONSparseHisto.cxx:18
 AliMUONSparseHisto.cxx:19
 AliMUONSparseHisto.cxx:20
 AliMUONSparseHisto.cxx:21
 AliMUONSparseHisto.cxx:22
 AliMUONSparseHisto.cxx:23
 AliMUONSparseHisto.cxx:24
 AliMUONSparseHisto.cxx:25
 AliMUONSparseHisto.cxx:26
 AliMUONSparseHisto.cxx:27
 AliMUONSparseHisto.cxx:28
 AliMUONSparseHisto.cxx:29
 AliMUONSparseHisto.cxx:30
 AliMUONSparseHisto.cxx:31
 AliMUONSparseHisto.cxx:32
 AliMUONSparseHisto.cxx:33
 AliMUONSparseHisto.cxx:34
 AliMUONSparseHisto.cxx:35
 AliMUONSparseHisto.cxx:36
 AliMUONSparseHisto.cxx:37
 AliMUONSparseHisto.cxx:38
 AliMUONSparseHisto.cxx:39
 AliMUONSparseHisto.cxx:40
 AliMUONSparseHisto.cxx:41
 AliMUONSparseHisto.cxx:42
 AliMUONSparseHisto.cxx:43
 AliMUONSparseHisto.cxx:44
 AliMUONSparseHisto.cxx:45
 AliMUONSparseHisto.cxx:46
 AliMUONSparseHisto.cxx:47
 AliMUONSparseHisto.cxx:48
 AliMUONSparseHisto.cxx:49
 AliMUONSparseHisto.cxx:50
 AliMUONSparseHisto.cxx:51
 AliMUONSparseHisto.cxx:52
 AliMUONSparseHisto.cxx:53
 AliMUONSparseHisto.cxx:54
 AliMUONSparseHisto.cxx:55
 AliMUONSparseHisto.cxx:56
 AliMUONSparseHisto.cxx:57
 AliMUONSparseHisto.cxx:58
 AliMUONSparseHisto.cxx:59
 AliMUONSparseHisto.cxx:60
 AliMUONSparseHisto.cxx:61
 AliMUONSparseHisto.cxx:62
 AliMUONSparseHisto.cxx:63
 AliMUONSparseHisto.cxx:64
 AliMUONSparseHisto.cxx:65
 AliMUONSparseHisto.cxx:66
 AliMUONSparseHisto.cxx:67
 AliMUONSparseHisto.cxx:68
 AliMUONSparseHisto.cxx:69
 AliMUONSparseHisto.cxx:70
 AliMUONSparseHisto.cxx:71
 AliMUONSparseHisto.cxx:72
 AliMUONSparseHisto.cxx:73
 AliMUONSparseHisto.cxx:74
 AliMUONSparseHisto.cxx:75
 AliMUONSparseHisto.cxx:76
 AliMUONSparseHisto.cxx:77
 AliMUONSparseHisto.cxx:78
 AliMUONSparseHisto.cxx:79
 AliMUONSparseHisto.cxx:80
 AliMUONSparseHisto.cxx:81
 AliMUONSparseHisto.cxx:82
 AliMUONSparseHisto.cxx:83
 AliMUONSparseHisto.cxx:84
 AliMUONSparseHisto.cxx:85
 AliMUONSparseHisto.cxx:86
 AliMUONSparseHisto.cxx:87
 AliMUONSparseHisto.cxx:88
 AliMUONSparseHisto.cxx:89
 AliMUONSparseHisto.cxx:90
 AliMUONSparseHisto.cxx:91
 AliMUONSparseHisto.cxx:92
 AliMUONSparseHisto.cxx:93
 AliMUONSparseHisto.cxx:94
 AliMUONSparseHisto.cxx:95
 AliMUONSparseHisto.cxx:96
 AliMUONSparseHisto.cxx:97
 AliMUONSparseHisto.cxx:98
 AliMUONSparseHisto.cxx:99
 AliMUONSparseHisto.cxx:100
 AliMUONSparseHisto.cxx:101
 AliMUONSparseHisto.cxx:102
 AliMUONSparseHisto.cxx:103
 AliMUONSparseHisto.cxx:104
 AliMUONSparseHisto.cxx:105
 AliMUONSparseHisto.cxx:106
 AliMUONSparseHisto.cxx:107
 AliMUONSparseHisto.cxx:108
 AliMUONSparseHisto.cxx:109
 AliMUONSparseHisto.cxx:110
 AliMUONSparseHisto.cxx:111
 AliMUONSparseHisto.cxx:112
 AliMUONSparseHisto.cxx:113
 AliMUONSparseHisto.cxx:114
 AliMUONSparseHisto.cxx:115
 AliMUONSparseHisto.cxx:116
 AliMUONSparseHisto.cxx:117
 AliMUONSparseHisto.cxx:118
 AliMUONSparseHisto.cxx:119
 AliMUONSparseHisto.cxx:120
 AliMUONSparseHisto.cxx:121
 AliMUONSparseHisto.cxx:122
 AliMUONSparseHisto.cxx:123
 AliMUONSparseHisto.cxx:124
 AliMUONSparseHisto.cxx:125
 AliMUONSparseHisto.cxx:126
 AliMUONSparseHisto.cxx:127
 AliMUONSparseHisto.cxx:128
 AliMUONSparseHisto.cxx:129
 AliMUONSparseHisto.cxx:130
 AliMUONSparseHisto.cxx:131
 AliMUONSparseHisto.cxx:132
 AliMUONSparseHisto.cxx:133
 AliMUONSparseHisto.cxx:134
 AliMUONSparseHisto.cxx:135
 AliMUONSparseHisto.cxx:136
 AliMUONSparseHisto.cxx:137
 AliMUONSparseHisto.cxx:138
 AliMUONSparseHisto.cxx:139
 AliMUONSparseHisto.cxx:140
 AliMUONSparseHisto.cxx:141
 AliMUONSparseHisto.cxx:142
 AliMUONSparseHisto.cxx:143
 AliMUONSparseHisto.cxx:144
 AliMUONSparseHisto.cxx:145
 AliMUONSparseHisto.cxx:146
 AliMUONSparseHisto.cxx:147
 AliMUONSparseHisto.cxx:148
 AliMUONSparseHisto.cxx:149
 AliMUONSparseHisto.cxx:150
 AliMUONSparseHisto.cxx:151
 AliMUONSparseHisto.cxx:152
 AliMUONSparseHisto.cxx:153
 AliMUONSparseHisto.cxx:154
 AliMUONSparseHisto.cxx:155
 AliMUONSparseHisto.cxx:156
 AliMUONSparseHisto.cxx:157
 AliMUONSparseHisto.cxx:158
 AliMUONSparseHisto.cxx:159
 AliMUONSparseHisto.cxx:160
 AliMUONSparseHisto.cxx:161
 AliMUONSparseHisto.cxx:162
 AliMUONSparseHisto.cxx:163
 AliMUONSparseHisto.cxx:164
 AliMUONSparseHisto.cxx:165
 AliMUONSparseHisto.cxx:166
 AliMUONSparseHisto.cxx:167
 AliMUONSparseHisto.cxx:168
 AliMUONSparseHisto.cxx:169
 AliMUONSparseHisto.cxx:170
 AliMUONSparseHisto.cxx:171
 AliMUONSparseHisto.cxx:172
 AliMUONSparseHisto.cxx:173
 AliMUONSparseHisto.cxx:174
 AliMUONSparseHisto.cxx:175
 AliMUONSparseHisto.cxx:176
 AliMUONSparseHisto.cxx:177
 AliMUONSparseHisto.cxx:178
 AliMUONSparseHisto.cxx:179
 AliMUONSparseHisto.cxx:180
 AliMUONSparseHisto.cxx:181
 AliMUONSparseHisto.cxx:182
 AliMUONSparseHisto.cxx:183
 AliMUONSparseHisto.cxx:184
 AliMUONSparseHisto.cxx:185
 AliMUONSparseHisto.cxx:186
 AliMUONSparseHisto.cxx:187
 AliMUONSparseHisto.cxx:188
 AliMUONSparseHisto.cxx:189
 AliMUONSparseHisto.cxx:190
 AliMUONSparseHisto.cxx:191
 AliMUONSparseHisto.cxx:192
 AliMUONSparseHisto.cxx:193
 AliMUONSparseHisto.cxx:194
 AliMUONSparseHisto.cxx:195
 AliMUONSparseHisto.cxx:196
 AliMUONSparseHisto.cxx:197
 AliMUONSparseHisto.cxx:198
 AliMUONSparseHisto.cxx:199
 AliMUONSparseHisto.cxx:200
 AliMUONSparseHisto.cxx:201
 AliMUONSparseHisto.cxx:202
 AliMUONSparseHisto.cxx:203
 AliMUONSparseHisto.cxx:204
 AliMUONSparseHisto.cxx:205
 AliMUONSparseHisto.cxx:206
 AliMUONSparseHisto.cxx:207
 AliMUONSparseHisto.cxx:208
 AliMUONSparseHisto.cxx:209
 AliMUONSparseHisto.cxx:210
 AliMUONSparseHisto.cxx:211
 AliMUONSparseHisto.cxx:212
 AliMUONSparseHisto.cxx:213
 AliMUONSparseHisto.cxx:214
 AliMUONSparseHisto.cxx:215
 AliMUONSparseHisto.cxx:216
 AliMUONSparseHisto.cxx:217
 AliMUONSparseHisto.cxx:218
 AliMUONSparseHisto.cxx:219
 AliMUONSparseHisto.cxx:220
 AliMUONSparseHisto.cxx:221
 AliMUONSparseHisto.cxx:222
 AliMUONSparseHisto.cxx:223
 AliMUONSparseHisto.cxx:224
 AliMUONSparseHisto.cxx:225
 AliMUONSparseHisto.cxx:226
 AliMUONSparseHisto.cxx:227
 AliMUONSparseHisto.cxx:228
 AliMUONSparseHisto.cxx:229
 AliMUONSparseHisto.cxx:230
 AliMUONSparseHisto.cxx:231
 AliMUONSparseHisto.cxx:232
 AliMUONSparseHisto.cxx:233
 AliMUONSparseHisto.cxx:234
 AliMUONSparseHisto.cxx:235
 AliMUONSparseHisto.cxx:236
 AliMUONSparseHisto.cxx:237
 AliMUONSparseHisto.cxx:238
 AliMUONSparseHisto.cxx:239
 AliMUONSparseHisto.cxx:240
 AliMUONSparseHisto.cxx:241
 AliMUONSparseHisto.cxx:242
 AliMUONSparseHisto.cxx:243
 AliMUONSparseHisto.cxx:244
 AliMUONSparseHisto.cxx:245
 AliMUONSparseHisto.cxx:246
 AliMUONSparseHisto.cxx:247
 AliMUONSparseHisto.cxx:248
 AliMUONSparseHisto.cxx:249
 AliMUONSparseHisto.cxx:250
 AliMUONSparseHisto.cxx:251
 AliMUONSparseHisto.cxx:252
 AliMUONSparseHisto.cxx:253
 AliMUONSparseHisto.cxx:254
 AliMUONSparseHisto.cxx:255
 AliMUONSparseHisto.cxx:256
 AliMUONSparseHisto.cxx:257
 AliMUONSparseHisto.cxx:258
 AliMUONSparseHisto.cxx:259
 AliMUONSparseHisto.cxx:260
 AliMUONSparseHisto.cxx:261
 AliMUONSparseHisto.cxx:262
 AliMUONSparseHisto.cxx:263
 AliMUONSparseHisto.cxx:264
 AliMUONSparseHisto.cxx:265
 AliMUONSparseHisto.cxx:266
 AliMUONSparseHisto.cxx:267
 AliMUONSparseHisto.cxx:268
 AliMUONSparseHisto.cxx:269
 AliMUONSparseHisto.cxx:270
 AliMUONSparseHisto.cxx:271
 AliMUONSparseHisto.cxx:272
 AliMUONSparseHisto.cxx:273
 AliMUONSparseHisto.cxx:274
 AliMUONSparseHisto.cxx:275
 AliMUONSparseHisto.cxx:276
 AliMUONSparseHisto.cxx:277
 AliMUONSparseHisto.cxx:278
 AliMUONSparseHisto.cxx:279
 AliMUONSparseHisto.cxx:280
 AliMUONSparseHisto.cxx:281
 AliMUONSparseHisto.cxx:282
 AliMUONSparseHisto.cxx:283
 AliMUONSparseHisto.cxx:284
 AliMUONSparseHisto.cxx:285
 AliMUONSparseHisto.cxx:286
 AliMUONSparseHisto.cxx:287
 AliMUONSparseHisto.cxx:288
 AliMUONSparseHisto.cxx:289
 AliMUONSparseHisto.cxx:290
 AliMUONSparseHisto.cxx:291
 AliMUONSparseHisto.cxx:292
 AliMUONSparseHisto.cxx:293
 AliMUONSparseHisto.cxx:294
 AliMUONSparseHisto.cxx:295
 AliMUONSparseHisto.cxx:296
 AliMUONSparseHisto.cxx:297
 AliMUONSparseHisto.cxx:298
 AliMUONSparseHisto.cxx:299
 AliMUONSparseHisto.cxx:300
 AliMUONSparseHisto.cxx:301
 AliMUONSparseHisto.cxx:302
 AliMUONSparseHisto.cxx:303
 AliMUONSparseHisto.cxx:304
 AliMUONSparseHisto.cxx:305
 AliMUONSparseHisto.cxx:306
 AliMUONSparseHisto.cxx:307
 AliMUONSparseHisto.cxx:308
 AliMUONSparseHisto.cxx:309
 AliMUONSparseHisto.cxx:310
 AliMUONSparseHisto.cxx:311
 AliMUONSparseHisto.cxx:312
 AliMUONSparseHisto.cxx:313
 AliMUONSparseHisto.cxx:314
 AliMUONSparseHisto.cxx:315
 AliMUONSparseHisto.cxx:316
 AliMUONSparseHisto.cxx:317
 AliMUONSparseHisto.cxx:318
 AliMUONSparseHisto.cxx:319
 AliMUONSparseHisto.cxx:320