Added boolean return values to Init, Make_Init_File and Read_Init function. Check...
[u/mrichter/AliRoot.git] / HLT / misc / AliTransBit.cxx
CommitLineData
1a3c8f6e 1///////////////////////////////////////////////////////////////////////////////
2// //
3// Time Projection Chamber ADC bit compresion lookup table //
4// //
5//
6// Origin: Marian Ivanov , GSI Darmstadt //
7// //
8
9//
10/*
11 Conversion equation:
12 For AliTransBit_v1
13 dy/dx= Int_t(1+x/fX0)
14
15 For AliTransBit_v2
16 y =(2**bit0) ln(1+x/fX0) / ln(1+(2**bit1-1)/fX0)
17
18 where x0 is calculated in function GetOptimumX0()
19
20*/
21
22// Example Session //
23/*
24
25Int_t b0=10; // original number of bits
26 Int_t b1=8; // compressed
27
28 AliTransBit_v1 trans;
29 Int_t x0=TMath::Nint(TMath::Exp(b0*TMath::Log(2)));
30 Int_t x1=TMath::Nint(TMath::Exp(b1*TMath::Log(2)));
31 trans.SetBits(b0,b1);
32 trans.FindOptimumX0();
33 trans.Update();
34 cout<<trans.Get0to1(x0-2)<<"\n";
35 cout<<trans.Get1to0(x1-2)<<"\n";
36
37 // to produce table of
38 for( Int_t i=0;i<x1;i++) cout<<i<<"\t"<<trans.Get1to0(i)<<"\n"; > table1to0.txt
39 for( Int_t i=0;i<x0;i++) cout<<i<<"\t"<<trans.Get0to1(i)<<"\n"; > table0to1.txt
40
41 for( Int_t i=0;i<x1-1;i++) cout<<i<<"\t"<<trans.Get1to0(i+1)-trans.Get1to0(i)<<"\n"; > tabled.txt
42
43
44
45*/
46//Begin_Html //
47/* //
48<img src="gif/AliTPCTransBit.gif">
49*/
50//End_Html
51// //
52// //
53///////////////////////////////////////////////////////////////////////////////
54
24dbb695 55#include "AliL3StandardIncludes.h"
1a3c8f6e 56
57#include "AliTransBit.h"
b328544b 58
1a3c8f6e 59ClassImp(AliTransBit)
60ClassImp(AliTransBit_v1)
61ClassImp(AliTransBit_v2)
62
63
64AliTransBit::AliTransBit()
65{
66 //
67 //
68 fTable0 = 0;
69 fTable1 = 0;
70 fBit0 = 10;
71 fBit1 = 8;
72 fX0 = 0;
73}
74
75AliTransBit::~AliTransBit()
76{
77 //
78 //default destructor
79 if (fTable0!=0) delete [] fTable0;
80 if (fTable1!=0) delete [] fTable1;
81}
82
83
84
85
86
87Double_t AliTransBit_v1::FindOptimumX0()
88{
89 //
90 //find x0 for which derivation at xder1 is equal 1
91 //
24dbb695 92 Int_t x0=(Int_t)rint(exp(fBit0*log(2.)));
93 Int_t x1=(Int_t)rint(exp(fBit1*log(2.)));
1a3c8f6e 94
95 fX0 = ((x1-2)*(x1-2)/2.)/(x0-x1-1); //starting fX0
96 Int_t digit=0;
97 Int_t j;
98 Double_t weight=1;
99 for(j=0;(j<50)&&(digit!=(x0-1));j++){
100 Int_t olddigit=digit;
101 digit=0;
102 for (Int_t i=0; i<x1-1;i++) digit+= Int_t(1+Double_t(i)/fX0);
103 fX0*= (1.+weight*Double_t(digit)/Double_t(x0-1))/(1.+weight);
104 if ( ((olddigit-(x0-1)) * (digit-(x0-1))) <0 )
105 weight*=0.25;
106 // cout<<j<<"\t"<<fX0<<"\t"<<digit<<"\n";
107 }
108 // cout<<"End of iteration "<<j<<"\n";
109 //cout<<"digit..."<<digit<<"\n";
110 return fX0;
111}
112
113
114void AliTransBit_v1::Update()
115{
116 //
117 //construct lookup tables for loosy compresion from
118 if (fX0<1) fX0 = FindOptimumX0();
24dbb695 119 Int_t x0=(Int_t)rint(exp(fBit0*log(2.)));
120 Int_t x1=(Int_t)rint(exp(fBit1*log(2.)));
1a3c8f6e 121
122 //fTable0 - conversion from bit0 coding to bit1 coding
123 if (fTable0!=0) delete fTable0;
124 fTable0= new Int_t[x0];
125 //fTable1 - conversion table from bit1 to bit0 coding
126 if (fTable1!=0) delete [] fTable1;
127 fTable1= new Int_t[x1];
128 //
129 Int_t digit=0;
130 for (Int_t i=0; i<x1-1;i++){
131 Int_t ddig=Int_t(1+Double_t(i)/fX0);
132 for (Int_t j=0;j<ddig;j++) if ((digit+j)<x0) fTable0[digit+j] = i;
133 fTable1[i]= digit+ddig/2;
134 digit+= ddig;
135 }
136 for (Int_t i=digit;i<x0-1;i++) fTable0[i]=x1-2;
137 fTable1[x1-1]=x0-1; //overflow
138 fTable0[x0-1]=x1-1; //overflow
139 return;
140}
141
142
143Double_t AliTransBit_v2::FindOptimumX0()
144{
145 //
146 //find x0 for which derivation at xder1 is equal 1
147 //
148 const Float_t xder1=1;
149 const Float_t dx=0.1;
150
24dbb695 151 Float_t x0=exp(fBit0*log(2.));
152 Float_t x1=exp(fBit1*log(2.));
1a3c8f6e 153 Float_t deriv = 0;
154 Float_t x;
155 for (x=x1;( (x>1)&&(deriv<1)) ;x-=dx)
156 {
b328544b 157 deriv = (x1-1)/( log(1.+x0/x) *x *(1+xder1/x));
1a3c8f6e 158 }
159 x+=dx/2.;
160 fX0 = x;
161 return fX0;
162}
163
164
165void AliTransBit_v2::Update()
166{
167 //
168 //construct lookup tables for loosy compresion from
169 if (fX0<1) fX0 = FindOptimumX0();
24dbb695 170 //Float_t x0=(Int_t)rint(exp(fBit0*log(2.)));
171 //Float_t x1=(Int_t)rint(exp(fBit1*log(2.)));
172 Int_t x0=(Int_t)rint(exp(fBit0*log(2.)));
173 Int_t x1=(Int_t)rint(exp(fBit1*log(2.)));
1a3c8f6e 174
175 //fTable0 - conversion from bit0 coding to bit1 coding
176 if (fTable0!=0) delete fTable0;
177 fTable0= new Int_t[x0];
178
179 //fTable1 - conversion table from bit1 to bit0 coding
180 if (fTable1!=0) delete [] fTable1;
181 fTable1= new Int_t[x1];
182
183 //
184 Int_t i;
185
186 for ( i=0; i<x0;i++)
b328544b 187 fTable0[i] =(Int_t)rint((x1-0.501)*log(1.+Float_t(i)/fX0)/
188 log(1.+(x0-1)/fX0));
1a3c8f6e 189
190 Int_t old0=-1;
191 Int_t old1=-1;
192 Int_t new1;
193 for ( i=0; i<x0;i++){
194 new1 = fTable0[i];
195 if (new1!=old1){
196 if (old1>=0) fTable1[old1] =(old0+i)/2;
197 old0 = i;
198 old1 = new1;
199 }
200 }
b328544b 201 fTable1[old1]=(Int_t)rint((Float_t)(old0+x0)/2);
1a3c8f6e 202
203 return;
204}