1 /**************************************************************************
2 * Copyright(c) 1998-2003, 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 **************************************************************************/
16 ////////////////////////////////////////////////
17 // Huffman classes for set:TPC //
18 ////////////////////////////////////////////////
20 #include <TObjArray.h>
21 #include "Riostream.h"
23 #include "AliTPCHuffman.h"
24 #include "AliTPCBuffer160.h"
28 AliTPCHNode::AliTPCHNode(){
34 //////////////////////////////////////////////////////////////////////////////
36 AliTPCHNode::AliTPCHNode(Int_t sym, Double_t freq){
37 // standard constructor
44 //////////////////////////////////////////////////////////////////////////////
46 AliTPCHNode::AliTPCHNode(const AliTPCHNode &source){
48 if(&source == this) return;
49 this->fSymbol = source.fSymbol;
50 this->fFrequency = source.fFrequency;
51 this->fLeft = source.fLeft;
52 this->fRight = source.fRight;
56 //////////////////////////////////////////////////////////////////////////////
58 AliTPCHNode& AliTPCHNode::operator=(const AliTPCHNode &source){
59 // Assignment operator
60 if(&source == this) return *this;
61 this->fSymbol = source.fSymbol;
62 this->fFrequency = source.fFrequency;
63 this->fLeft = source.fLeft;
64 this->fRight = source.fRight;
68 //////////////////////////////////////////////////////////////////////////////
70 Int_t AliTPCHNode::Compare(const TObject *obj)const{
71 // function called by Sort method of TObjArray
72 AliTPCHNode *node=(AliTPCHNode *)obj;
73 Double_t f=fFrequency;
74 Double_t fo=node->fFrequency;
76 else if (f>fo) return -1;
80 //////////////////////////////////////////////////////////////////////////////
81 //////////////////////////////////////////////////////////////////////////////
82 //////////////////////////////////////////////////////////////////////////////
84 ClassImp(AliTPCHTable)
86 AliTPCHTable::AliTPCHTable(){
96 //////////////////////////////////////////////////////////////////////////////
98 AliTPCHTable::AliTPCHTable(Int_t size){
101 fCodeLen = new UChar_t[fSize];
102 fCode = new Double_t[fSize];
103 fHNodes = new TObjArray;
107 fSym= new Short_t[fSize];
108 for (Short_t i=0;i<fSize;i++) {
114 //////////////////////////////////////////////////////////////////////////////
116 AliTPCHTable::AliTPCHTable(const AliTPCHTable &source){
118 if(&source == this) return;
119 this->fSize = source.fSize;
120 this->fCodeLen = source.fCodeLen;
121 this->fCode = source.fCode;
122 this->fSym = source.fSym;
123 this->fHNodes = source.fHNodes;
124 this->fNnodes = source.fNnodes;
125 this->fNum=source.fNum;
126 this->fVerbose=source.fVerbose;
130 //////////////////////////////////////////////////////////////////////////////
132 AliTPCHTable& AliTPCHTable::operator=(const AliTPCHTable &source) {
133 // Assignment operator
134 if(&source == this) return *this;
135 this->fSize = source.fSize;
136 this->fCodeLen = source.fCodeLen;
137 this->fCode = source.fCode;
138 this->fSym = source.fSym;
139 this->fHNodes = source.fHNodes;
140 this->fNnodes = source.fNnodes;
141 this->fNum=source.fNum;
142 this->fVerbose=source.fVerbose;
146 //////////////////////////////////////////////////////////////////////////////
148 AliTPCHTable::~AliTPCHTable(){
151 cout<<"HTable destructor !\n";
152 if (fCodeLen) delete[] fCodeLen;
153 if (fCode) delete [] fCode;
155 fHNodes->Delete(); //Clear out the collection and deletes the removed objects
160 //////////////////////////////////////////////////////////////////////////////
162 void AliTPCHTable::SetCodeLen(UChar_t len,Int_t val){
167 //////////////////////////////////////////////////////////////////////////////
169 void AliTPCHTable::SetCode(Double_t code,Int_t val){
174 //////////////////////////////////////////////////////////////////////////////
176 void AliTPCHTable::PrintTable()const{
177 cout<<"Table for Huffman coding\n";
178 cout<<" Symbol| Code | Length \n";
179 for (Int_t i=0;i<fSize;i++){
181 cout.width(6);cout<<fSym[i];
182 cout.width(3);cout<<"|";
183 cout.width(6);cout<<hex<<(ULong_t)fCode[i]<<dec;
184 cout.width(5);cout<<"|";
185 cout.width(6);cout<<(ULong_t)fCodeLen[i]<<endl;
190 //////////////////////////////////////////////////////////////////////////////
192 Bool_t AliTPCHTable::SpanTree(AliTPCHNode *start, ULong_t code, UChar_t len){
194 //In an Huffman tree any internal node has always two children
195 AliTPCHNode * visited;
197 Int_t idx=visited->GetSymbol();
198 if (!visited->GetLeft()) {
203 SpanTree(visited->GetLeft(), code << 1, len + 1);
204 SpanTree(visited->GetRight(), code << 1 | 0x0001, len + 1);
208 //////////////////////////////////////////////////////////////////////////////
210 void AliTPCHTable::ResetHNodes(){
211 // Reset number of HNodes and the HNodes array
212 if (fHNodes) fHNodes->Clear();
213 if (fNnodes) fNnodes=0;
216 //////////////////////////////////////////////////////////////////////////////
218 void AliTPCHTable::ClearTable(){
220 memset(fCodeLen,0,sizeof(UChar_t)*fSize);
221 memset(fCode,0,sizeof(Double_t)*fSize);
224 //////////////////////////////////////////////////////////////////////////////
226 Int_t AliTPCHTable::GetFrequencies(const char *fname){
227 AliTPCBuffer160 buff(fname,0);
228 ULong_t NumberOfWords=0;
230 while((Val=buff.GetNext())!=-1){
235 cout<<"Total number of words: "<<NumberOfWords<<endl;
236 //Print out the frequencies
238 for (Int_t i=0;i<fSize;i++){
239 if (fCode[i])cout<<"Symbol: "<<i<<" Freq: "<<fCode[i]<<endl;
246 Int_t AliTPCHTable::SetValFrequency(const Int_t Val,Double_t Value){
252 //////////////////////////////////////////////////////////////////////////////
254 Int_t AliTPCHTable::SetFrequency(const Int_t Val){
260 //////////////////////////////////////////////////////////////////////////////
262 Int_t AliTPCHTable::StoreFrequencies(const char *fname){
263 ofstream ftxt(fname);
264 for (Int_t i=0;i<fSize;i++){
265 ftxt<<(ULong_t)fCode[i]<<endl;
271 //////////////////////////////////////////////////////////////////////////////
273 void AliTPCHTable::CompleteTable(Int_t k){
291 for(Int_t i=0;i<max;i++){
292 if(fCode[i]==0.0)fCode[i]=val;
297 //////////////////////////////////////////////////////////////////////////////
299 Double_t AliTPCHTable::GetEntropy()const{
302 for (Int_t i=0;i<fSize;i++){
304 prob=fCode[i]/(Double_t)fNum;
305 entropy+=prob*(TMath::Log2(prob));
311 //////////////////////////////////////////////////////////////////////////////
313 Int_t AliTPCHTable::BuildHTable(){
315 if(GetWordsNumber()){
316 for (Int_t i=0; i< fSize; i++) {
319 //cout<< "Symbol:"<<i<<" Freq:"<<fCode[i]<<endl;
320 fHNodes->Add(new AliTPCHNode(i,fCode[i]));
323 Int_t nentries=fHNodes->GetEntriesFast();
324 //cout<<"Number of symbols: "<<nentries<<endl;
325 Int_t nindex=nentries-1;
327 fHNodes->Sort(nindex+1);
328 AliTPCHNode *aux = new AliTPCHNode(0,0);
329 AliTPCHNode *node= (AliTPCHNode*)fHNodes->UncheckedAt(nindex-1);
330 AliTPCHNode *node1= (AliTPCHNode*)fHNodes->UncheckedAt(nindex);
333 aux->SetRight(node1);
334 aux->SetFrequency(node->GetFrequency() + node1->GetFrequency());
335 fHNodes->RemoveAt(nindex-1);
336 fHNodes->AddAt(aux,nindex-1);
340 AliTPCHNode *start= (AliTPCHNode*)fHNodes->UncheckedAt(0);
344 cout<<"Table contains 0 elements\n";
349 //////////////////////////////////////////////////////////////////////////////