]>
Commit | Line | Data |
---|---|---|
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 */ | |
37 | ClassImp(AliHLTDataDeflaterSimple) | |
38 | ||
39 | AliHLTDataDeflaterSimple::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 | ||
52 | AliHLTDataDeflaterSimple::~AliHLTDataDeflaterSimple() | |
53 | { | |
54 | // destructor | |
55 | Clear(); | |
a0d59d54 | 56 | |
57 | if (fHistograms) { | |
58 | delete fHistograms; | |
59 | } | |
60 | fHistograms=NULL; | |
61 | ||
62 | } | |
63 | ||
64 | int 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 | ||
76 | int 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 | ||
89 | bool 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 | 113 | void AliHLTDataDeflaterSimple::Clear(Option_t * option) |
80fb7693 | 114 | { |
115 | // internal cleanup | |
a0d59d54 | 116 | TH2F* hParameterCompression=NULL; |
a0d59d54 | 117 | if (fHistograms) { |
118 | int bins=fParameterDefinitions.size(); | |
119 | TObject* o=NULL; | |
120 | o=fHistograms->FindObject("ParameterCompression"); | |
121 | if (o) { | |
122 | hParameterCompression=dynamic_cast<TH2F*>(o); | |
123 | } else { | |
124 | hParameterCompression=new TH2F("ParameterCompression", "ParameterCompression", bins, 0, bins, 100, 0., 1.1); | |
125 | if (hParameterCompression) fHistograms->Add(hParameterCompression); | |
126 | } | |
a0d59d54 | 127 | } |
128 | unsigned i=0; | |
f95bc7cd | 129 | for (vector<AliHLTDataDeflaterParameter>::iterator m=fParameterDefinitions.begin(); |
a0d59d54 | 130 | m!=fParameterDefinitions.end(); m++, i++) { |
131 | int bitLength=m->GetBitLength(); | |
132 | int valueCount=m->GetValueCount(); | |
133 | if (bitLength==0 || valueCount==0) continue; | |
134 | float ratio=(float)m->GetBitCount(); | |
135 | ratio/=bitLength*valueCount; | |
136 | if (hParameterCompression) | |
137 | hParameterCompression->Fill(i, ratio); | |
a0d59d54 | 138 | |
f95bc7cd | 139 | m->ResetBitCount(); |
140 | } | |
141 | AliHLTDataDeflater::Clear(option); | |
80fb7693 | 142 | } |
143 | ||
a0d59d54 | 144 | void AliHLTDataDeflaterSimple::SaveAs(const char *filename,Option_t */*option*/) const |
145 | { | |
146 | // safe histograms to file | |
147 | std::auto_ptr<TFile> file(TFile::Open(filename, "RECREATE")); | |
148 | if (!file.get() || file->IsZombie()) { | |
149 | HLTError("can not open file %s", filename);; | |
150 | return; | |
151 | } | |
152 | file->cd(); | |
153 | if (fHistograms) { | |
709053b2 | 154 | for (int i=0; i<fHistograms->GetEntries(); i++) { |
155 | if (fHistograms->At(i)==NULL || | |
156 | !fHistograms->At(i)->InheritsFrom("TH1") || | |
157 | fHistograms->At(i)->InheritsFrom("TH2") || | |
158 | fHistograms->At(i)->InheritsFrom("TH3") | |
159 | ) continue; | |
160 | TH1* h=reinterpret_cast<TH1*>(fHistograms->At(i)); | |
161 | if (!h) continue; | |
162 | float entropy=CalcEntropy(h); | |
163 | if (entropy<0) continue; | |
164 | TString title=h->GetTitle(); | |
165 | title+=Form(" entropy %.2f", entropy); | |
166 | h->SetTitle(title); | |
167 | } | |
a0d59d54 | 168 | fHistograms->Write(); |
169 | } | |
170 | ||
171 | file->Close(); | |
172 | } | |
173 | ||
709053b2 | 174 | float AliHLTDataDeflaterSimple::CalcEntropy(TH1* histo, const char* /*option*/, int mode) |
175 | { | |
176 | ||
177 | if (!histo) return -1000.; | |
178 | ||
179 | float l2=TMath::Log(2.0); | |
180 | float integral=histo->Integral(0,histo->GetNbinsX()); | |
181 | int centerbin=mode*histo->GetNbinsX()/2; | |
182 | int nofBins=histo->GetNbinsX()-centerbin; | |
183 | float entropy=0.0; | |
184 | for (int offset=0; offset<nofBins; offset++) { | |
185 | float abundance=histo->GetBinContent(offset); | |
186 | if (abundance<1.0) continue; | |
187 | entropy += (- (Double_t) abundance / (Double_t) integral ) * log( ( (Double_t) abundance / (Double_t) integral )) / (l2); | |
188 | } | |
189 | ||
190 | return entropy; | |
191 | } | |
192 | ||
80fb7693 | 193 | void AliHLTDataDeflaterSimple::Print(Option_t *option) const |
194 | { | |
195 | // print info | |
196 | Print(cout, option); | |
197 | } | |
198 | ||
199 | void AliHLTDataDeflaterSimple::Print(ostream& out, Option_t *option) const | |
200 | { | |
201 | // print to stream | |
202 | out << "AliHLTDataDeflaterSimple:" << endl; | |
203 | AliHLTUInt64_t bitCount=0; | |
204 | AliHLTUInt64_t fullSize=0; | |
205 | for (vector<AliHLTDataDeflaterParameter>::const_iterator m=fParameterDefinitions.begin(); | |
206 | m!=fParameterDefinitions.end(); m++) { | |
207 | cout << " "; m->Print(option); | |
208 | bitCount+=m->GetBitCount(); | |
209 | fullSize+=m->GetValueCount()*m->GetBitLength(); | |
210 | } | |
211 | out << " total: " << bitCount << "/" << fullSize << " " << (fullSize>0?float(bitCount)/fullSize:0.0) << endl; | |
212 | } | |
213 | ||
214 | void AliHLTDataDeflaterSimple::AliHLTDataDeflaterParameter::Print(const char* /*option*/) const | |
215 | { | |
216 | // print info | |
217 | cout << fName << " (" << fFullBitLength << "," << fReducedBitLength << "): " | |
218 | << fValueCount << " entries " | |
219 | << fBitCount << "/" << fFullBitLength*fValueCount; | |
220 | if (fFullBitLength && fValueCount) { | |
221 | cout << " " << float(fBitCount)/(fValueCount*fFullBitLength); | |
222 | } | |
223 | cout << endl; | |
224 | } | |
225 | ||
226 | ostream& operator<<(ostream &out, const AliHLTDataDeflaterSimple& me) | |
227 | { | |
228 | me.Print(out); | |
229 | return out; | |
230 | } |