]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTDataDeflaterSimple.cxx
calculating entropy for parameter histograms
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataDeflaterSimple.cxx
CommitLineData
80fb7693 1// $Id$
2
3//**************************************************************************
4//* This file is property of and copyright by the ALICE HLT Project *
5//* ALICE Experiment at CERN, All rights reserved. *
6//* *
7//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8//* for The ALICE HLT Project. *
9//* *
10//* Permission to use, copy, modify and distribute this software and its *
11//* documentation strictly for non-commercial purposes is hereby granted *
12//* without fee, provided that the above copyright notice appears in all *
13//* copies and that both the copyright notice and this permission notice *
14//* appear in the supporting documentation. The authors make no claims *
15//* about the suitability of this software for any purpose. It is *
16//* provided "as is" without express or implied warranty. *
17//**************************************************************************
18
19/// @file AliHLTDataDeflaterSimple.cxx
20/// @author Matthias Richter
21/// @date 2011-08-10
22/// @brief Simple deflater implementation storing frequent values below a
23/// maximum value with a reduced bit number and others with the full
24/// number of bits.
25
26#include "AliHLTDataDeflaterSimple.h"
a0d59d54 27#include "TFile.h"
28#include "TObjArray.h"
29#include "TH1I.h"
30#include "TH2F.h"
709053b2 31#include "TMath.h"
80fb7693 32#include <memory>
33#include <algorithm>
34#include <iostream>
35
36/** ROOT macro for the implementation of ROOT specific class methods */
37ClassImp(AliHLTDataDeflaterSimple)
38
39AliHLTDataDeflaterSimple::AliHLTDataDeflaterSimple()
40 : AliHLTDataDeflater()
41 , fParameterDefinitions()
a0d59d54 42 , fHistograms(NULL)
80fb7693 43{
44 // see header file for class documentation
45 // or
46 // refer to README to build package
47 // or
48 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
a0d59d54 49 if (fHistograms) fHistograms->SetOwner(kTRUE);
80fb7693 50}
51
52AliHLTDataDeflaterSimple::~AliHLTDataDeflaterSimple()
53{
54 // destructor
55 Clear();
a0d59d54 56
57 if (fHistograms) {
58 delete fHistograms;
59 }
60 fHistograms=NULL;
61
62}
63
64int AliHLTDataDeflaterSimple::AddParameterDefinition(const char* name, int bitLength, int reducedBitLength)
65{
66 /// add a parameter definition to the configuration, return reference id
67 fParameterDefinitions.push_back(AliHLTDataDeflaterParameter(name, bitLength, reducedBitLength));
68 if (fHistograms) {
69 if (!fHistograms->FindObject(name)) {
70 fHistograms->Add(new TH1I(name, name, 100, 0, 0x1<<bitLength));
71 }
72 }
73 return fParameterDefinitions.size()-1;
74}
75
76int AliHLTDataDeflaterSimple::AddHistogram(TH1* h)
77{
78 /// add a histogram for deflater statistic of the corresponding parameter
79 if (!fHistograms) fHistograms=new TObjArray;
80 if (!fHistograms) return -ENOMEM;
81 if (h!=NULL && fHistograms->FindObject(h->GetName())) {
82 HLTWarning("parameter with name %s already existing, skipping histogram", h->GetName());
83 return -EEXIST;
84 }
85 if (h) fHistograms->Add(h);
86 return 0;
80fb7693 87}
88
89bool AliHLTDataDeflaterSimple::OutputParameterBits( int memberId, AliHLTUInt64_t const & value )
90{
91 // write bit pattern of a member to the current byte and position
92 if (memberId>=(int)fParameterDefinitions.size()) return false;
93
94 AliHLTUInt32_t switchBit=fParameterDefinitions[memberId].SwitchBit(value); // 0 -> reduced, 1 -> full
95 AliHLTUInt64_t v=fParameterDefinitions[memberId].Value(value);
96 AliHLTUInt32_t length=fParameterDefinitions[memberId].ValueLength(value);
97 fParameterDefinitions[memberId].IncrementBitCount(value);
98
a0d59d54 99 if (fHistograms) {
100 TObject* o=fHistograms->FindObject(fParameterDefinitions[memberId].GetName());
101 if (o) {
102 TH1* h=dynamic_cast<TH1*>(o);
103 if (h) {
104 h->Fill(v);
105 }
106 }
107 }
108
80fb7693 109 if (!OutputBit(switchBit)) return false;
110 return OutputBits(v, length);
111}
112
f95bc7cd 113void AliHLTDataDeflaterSimple::Clear(Option_t * option)
80fb7693 114{
115 // internal cleanup
a0d59d54 116 TH2F* hParameterCompression=NULL;
117 TH2F* hParameterByteSaving=NULL;
118 if (fHistograms) {
119 int bins=fParameterDefinitions.size();
120 TObject* o=NULL;
121 o=fHistograms->FindObject("ParameterCompression");
122 if (o) {
123 hParameterCompression=dynamic_cast<TH2F*>(o);
124 } else {
125 hParameterCompression=new TH2F("ParameterCompression", "ParameterCompression", bins, 0, bins, 100, 0., 1.1);
126 if (hParameterCompression) fHistograms->Add(hParameterCompression);
127 }
128 /*
129 o=fHistograms->FindObject("ParameterByteSaving");
130 if (o) {
131 hParameterByteSaving=dynamic_cast<TH2F*>(o);
132 } else {
133 hParameterByteSaving=new TH2F("ParameterByteSaving", "ParameterByteSaving", bins, 0, bins, 10, 0., 1.1);
134 if (hParameterByteSaving) fHistograms->Add(hParameterByteSaving);
135 }
136 */
137 }
138 unsigned i=0;
f95bc7cd 139 for (vector<AliHLTDataDeflaterParameter>::iterator m=fParameterDefinitions.begin();
a0d59d54 140 m!=fParameterDefinitions.end(); m++, i++) {
141 int bitLength=m->GetBitLength();
142 int valueCount=m->GetValueCount();
143 if (bitLength==0 || valueCount==0) continue;
144 float ratio=(float)m->GetBitCount();
145 ratio/=bitLength*valueCount;
146 if (hParameterCompression)
147 hParameterCompression->Fill(i, ratio);
148 ratio=(1-ratio)*valueCount*bitLength/8;
149 if (hParameterByteSaving)
150 hParameterByteSaving->Fill(i, ratio);
151
f95bc7cd 152 m->ResetBitCount();
153 }
154 AliHLTDataDeflater::Clear(option);
80fb7693 155}
156
a0d59d54 157void AliHLTDataDeflaterSimple::SaveAs(const char *filename,Option_t */*option*/) const
158{
159 // safe histograms to file
160 std::auto_ptr<TFile> file(TFile::Open(filename, "RECREATE"));
161 if (!file.get() || file->IsZombie()) {
162 HLTError("can not open file %s", filename);;
163 return;
164 }
165 file->cd();
166 if (fHistograms) {
709053b2 167 for (int i=0; i<fHistograms->GetEntries(); i++) {
168 if (fHistograms->At(i)==NULL ||
169 !fHistograms->At(i)->InheritsFrom("TH1") ||
170 fHistograms->At(i)->InheritsFrom("TH2") ||
171 fHistograms->At(i)->InheritsFrom("TH3")
172 ) continue;
173 TH1* h=reinterpret_cast<TH1*>(fHistograms->At(i));
174 if (!h) continue;
175 float entropy=CalcEntropy(h);
176 if (entropy<0) continue;
177 TString title=h->GetTitle();
178 title+=Form(" entropy %.2f", entropy);
179 h->SetTitle(title);
180 }
a0d59d54 181 fHistograms->Write();
182 }
183
184 file->Close();
185}
186
709053b2 187float AliHLTDataDeflaterSimple::CalcEntropy(TH1* histo, const char* /*option*/, int mode)
188{
189
190 if (!histo) return -1000.;
191
192 float l2=TMath::Log(2.0);
193 float integral=histo->Integral(0,histo->GetNbinsX());
194 int centerbin=mode*histo->GetNbinsX()/2;
195 int nofBins=histo->GetNbinsX()-centerbin;
196 float entropy=0.0;
197 for (int offset=0; offset<nofBins; offset++) {
198 float abundance=histo->GetBinContent(offset);
199 if (abundance<1.0) continue;
200 entropy += (- (Double_t) abundance / (Double_t) integral ) * log( ( (Double_t) abundance / (Double_t) integral )) / (l2);
201 }
202
203 return entropy;
204}
205
80fb7693 206void AliHLTDataDeflaterSimple::Print(Option_t *option) const
207{
208 // print info
209 Print(cout, option);
210}
211
212void AliHLTDataDeflaterSimple::Print(ostream& out, Option_t *option) const
213{
214 // print to stream
215 out << "AliHLTDataDeflaterSimple:" << endl;
216 AliHLTUInt64_t bitCount=0;
217 AliHLTUInt64_t fullSize=0;
218 for (vector<AliHLTDataDeflaterParameter>::const_iterator m=fParameterDefinitions.begin();
219 m!=fParameterDefinitions.end(); m++) {
220 cout << " "; m->Print(option);
221 bitCount+=m->GetBitCount();
222 fullSize+=m->GetValueCount()*m->GetBitLength();
223 }
224 out << " total: " << bitCount << "/" << fullSize << " " << (fullSize>0?float(bitCount)/fullSize:0.0) << endl;
225}
226
227void AliHLTDataDeflaterSimple::AliHLTDataDeflaterParameter::Print(const char* /*option*/) const
228{
229 // print info
230 cout << fName << " (" << fFullBitLength << "," << fReducedBitLength << "): "
231 << fValueCount << " entries "
232 << fBitCount << "/" << fFullBitLength*fValueCount;
233 if (fFullBitLength && fValueCount) {
234 cout << " " << float(fBitCount)/(fValueCount*fFullBitLength);
235 }
236 cout << endl;
237}
238
239ostream& operator<<(ostream &out, const AliHLTDataDeflaterSimple& me)
240{
241 me.Print(out);
242 return out;
243}