]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTDataDeflaterSimple.h
calculating entropy for parameter histograms
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataDeflaterSimple.h
1 //-*- Mode: C++ -*-
2 // $Id$
3 #ifndef ALIHLTDATADEFLATERSIMPLE_H
4 #define ALIHLTDATADEFLATERSIMPLE_H
5 //* This file is property of and copyright by the ALICE HLT Project        * 
6 //* ALICE Experiment at CERN, All rights reserved.                         *
7 //* See cxx source for full Copyright notice                               *
8
9 /// @file   AliHLTDataDeflaterSimple.h
10 /// @author Matthias Richter
11 /// @date   2011-08-10
12 /// @brief  Data deflater class storing only necessary bits
13 /// @note   Code original from AliHLTTPCCompModelDeflater
14
15 #include "AliHLTDataDeflater.h"
16 #include <vector>
17 #include <string>
18
19 class TObjArray;
20 class TH1;
21
22 /**
23  * @class AliHLTDataDeflaterSimple
24  * Simple deflater implementation storing frequent values below a
25  * maximum value with a reduced bit number and others with the full
26  * number of bits. The reduced value is indicated by a preceeding '0'
27  * and the full bit by '1'. The algorithm can be applied to data with an
28  * occurrence distribution peaking close to zero and having less frequent
29  * occurrence at higher values.
30  *
31  * @ingroup alihlt_base
32  */
33 class AliHLTDataDeflaterSimple : public AliHLTDataDeflater
34 {
35 public:
36   /// standard constructor
37   AliHLTDataDeflaterSimple();
38   /// destructor
39   ~AliHLTDataDeflaterSimple();
40
41   /// @class AliHLTDataDeflaterParameter definition of parameters
42   class AliHLTDataDeflaterParameter {
43   public:
44     AliHLTDataDeflaterParameter()
45       : fName(), fFullBitLength(0), fReducedBitLength(0)
46       , fMax(0), fMaxReduced(0)
47       , fMask(0), fMaskReduced(0)
48       , fValueCount(0), fBitCount(0) {}
49
50     AliHLTDataDeflaterParameter(const char* name, int length, int reduced)
51       : fName(name), fFullBitLength(length), fReducedBitLength(reduced)
52       , fMax((((AliHLTUInt64_t)0x1)<<length)-1), fMaxReduced((((AliHLTUInt64_t)0x1)<<reduced)-1)
53       , fMask(fMax), fMaskReduced(fMaxReduced) 
54       , fValueCount(0), fBitCount(0) {}
55
56     AliHLTDataDeflaterParameter(const AliHLTDataDeflaterParameter& src)
57       : fName(src.fName), fFullBitLength(src.fFullBitLength), fReducedBitLength(src.fReducedBitLength)
58       , fMax(src.fMax), fMaxReduced(src.fMaxReduced)
59       , fMask(src.fMask), fMaskReduced(src.fMaskReduced) 
60       , fValueCount(0), fBitCount(0) {}
61
62     AliHLTDataDeflaterParameter& operator=(const AliHLTDataDeflaterParameter& src) {
63       fName=src.fName; fFullBitLength=src.fFullBitLength; fReducedBitLength=src.fReducedBitLength;
64       fMax=src.fMax; fMaxReduced=src.fMaxReduced;
65       fMask=src.fMask; fMaskReduced=src.fMaskReduced;
66       fValueCount=src.fValueCount; fBitCount=src.fBitCount;
67       return *this;
68     }
69
70     ~AliHLTDataDeflaterParameter() {}
71
72     const char* GetName() const {return fName.c_str();}
73     AliHLTUInt64_t Value(const AliHLTUInt64_t& value) const {
74       return value>fMax?fMax:value;
75     }
76     AliHLTUInt32_t ValueLength(const AliHLTUInt64_t& value) const{
77       return value>fMaxReduced?fFullBitLength:fReducedBitLength;
78     }
79     AliHLTUInt32_t SwitchBit(const AliHLTUInt64_t& value) const {
80       return value>fMaxReduced;
81     }
82     const int& GetBitLength() const {return fFullBitLength;}
83     const int& GetReducedBitLength() const {return fReducedBitLength;}
84     const AliHLTUInt64_t& GetMax() const {return fMax;}
85     const AliHLTUInt64_t& GetMaxReduced() const {return fMaxReduced;}
86     const AliHLTUInt64_t& GetMask() const {return fMask;}
87     const AliHLTUInt64_t& GetReducedMask() const {return fMaskReduced;}
88
89     const AliHLTUInt32_t& GetBitCount() const {return fBitCount;}
90     const AliHLTUInt32_t& GetValueCount() const {return fValueCount;}
91     void IncrementBitCount(const AliHLTUInt64_t& value) {
92       fBitCount+=(value>fMaxReduced?fFullBitLength:fReducedBitLength)+1;
93       fValueCount++;
94     }
95     void ResetBitCount() {fValueCount=0; fBitCount=0;}
96     void Print(const char* option="") const;
97
98   private:
99     std::string fName; //!
100     int fFullBitLength; //!
101     int fReducedBitLength; //!
102     AliHLTUInt64_t fMax; //!
103     AliHLTUInt64_t fMaxReduced; //!
104     AliHLTUInt64_t fMask; //!
105     AliHLTUInt64_t fMaskReduced; //!
106     AliHLTUInt32_t fValueCount; //!
107     AliHLTUInt32_t fBitCount; //!
108   };
109
110   /// add a parameter definition to the configuration, return reference id
111   int AddParameterDefinition(const char* name, int bitLength, int reducedBitLength);
112
113   /// add a histogram for deflater statistic of the corresponding parameter
114   int AddHistogram(TH1* h);
115
116   /// inherited from AliHLTDataDeflater: write bit pattern according to configuration
117   virtual bool OutputParameterBits( int parameterId, AliHLTUInt64_t const & value );
118
119   /// clear the object and reset pointer references
120   virtual void Clear(Option_t * /*option*/ ="");
121
122   /// print info
123   virtual void Print(Option_t *option="") const;
124
125   /// print info
126   virtual void Print(ostream& out, Option_t *option="") const;
127
128   /// safe statistics histograms to file
129   virtual void SaveAs(const char *filename="",Option_t *option="") const;
130
131   /// DataDeflaterSimple has deflater version 1
132   virtual int GetDeflaterVersion() const {return 1;}
133
134   static float CalcEntropy(TH1* histo, const char* option="", int mode=0);
135
136  protected:
137  private:
138   /// copy constructor prohibited
139   AliHLTDataDeflaterSimple(const AliHLTDataDeflaterSimple&);
140   /// assignment operator prohibited
141   AliHLTDataDeflaterSimple& operator=(const AliHLTDataDeflaterSimple&);
142
143   vector<AliHLTDataDeflaterParameter> fParameterDefinitions; //!
144
145   TObjArray* fHistograms; //! list of histograms for parameters
146
147   ClassDef(AliHLTDataDeflaterSimple, 0)
148 };
149
150 ostream& operator<<(ostream &out, const AliHLTDataDeflaterSimple& me);
151
152 #endif