ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Event
EventCommonTPCnv
src
ErrorMatrixCompressor.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
/*
6
* ErrorMatrixCompressor.cxx
7
*
8
* Created by Dmitry Emeliyanov on 12/11/08.
9
* <Dmitry.Emeliyanov@cern.ch>
10
*
11
*/
12
#include "
EventCommonTPCnv/ErrorMatrixCompressor.h
"
13
14
#include <cmath>
15
#include <cstring>
16
#include <iostream>
17
#include <format>
18
19
20
void
DecoderFloat_IEEE754::print
()
21
{
22
std::cout << std::format(
"{:032b}\n"
,
m_data
);
23
}
24
25
short
int
DecoderFloat_IEEE754::getExponent
()
26
{
27
std::uint32_t mask = 0x7f800000u;
28
return
((
m_data
& mask) >> 23);
29
}
30
31
unsigned
int
DecoderFloat_IEEE754::getMantissa
()
32
{
33
std::uint32_t mask = 0x007fffffu;
34
return
(
m_data
& mask);
35
}
36
37
38
void
DecoderFloat_IEEE754::setExponent
(
short
int
ex)
39
{
40
std::uint32_t buf=ex;
41
buf=buf<<23;
42
std::uint32_t mask=0x7F800000u;
43
m_data
=
m_data
| (buf & mask);
44
}
45
46
void
DecoderFloat_IEEE754::setSign
(
int
s)
47
{
48
if
(s>0)
49
{
50
m_data
=
m_data
& 0x7FFFFFFFu;
51
}
52
else
53
{
54
m_data
=
m_data
| 0x80000000u;
55
}
56
}
57
58
59
ErrorMatrixCompressor::ErrorMatrixCompressor
(
int
b)
60
{
61
short
int
biasArray[6] = {116,120,118,116,116,102};
62
63
64
double
T[5]={0.2,0.5,100.0,100.0,20000.0};
65
66
m_bitStrip
=b;
67
for
(
int
i=0;i<6;i++)
68
{
69
m_biases
[i]=biasArray[i];
70
}
71
for
(
int
i=0;i<5;i++)
72
{
73
m_scales
[i]=T[i];
74
}
75
m_limits
[0]=15;
76
m_limits
[1]=31;
77
m_srcMasks
[0]= 0x80000000;
78
m_srcMasks
[1]= 0xC0000000;
79
m_srcMasks
[2]= 0xE0000000;
80
m_srcMasks
[3]= 0xF0000000;
81
m_srcMasks
[4]= 0xF8000000;
82
m_srcMasks
[5]= 0xFC000000;
83
m_srcMasks
[6]= 0xFE000000;
84
m_srcMasks
[7]= 0xFF000000;
85
m_srcMasks
[8]= 0xFF800000;
86
m_srcMasks
[9]= 0xFFC00000;
87
m_srcMasks
[10]=0xFFE00000;
88
m_srcMasks
[11]=0xFFF00000;
89
m_srcMasks
[12]=0xFFF80000;
90
m_srcMasks
[13]=0xFFFC0000;
91
m_srcMasks
[14]=0xFFFE0000;
92
m_srcMasks
[15]=0xFFFF0000;
93
m_tripVec
.clear();
94
m_tripVec
.push_back(
Triplet
(0,1,3));
95
m_tripVec
.push_back(
Triplet
(2,4,6));
96
m_tripVec
.push_back(
Triplet
(5,7,8));
97
m_tripVec
.push_back(
Triplet
(9,10,11));
98
m_tripVec
.push_back(
Triplet
(14,12,13));
99
}
100
101
void
ErrorMatrixCompressor::setBiases
(
const
int
b[6])
102
{
103
for
(
int
i=0;i<6;i++)
104
{
105
m_biases
[i]=b[i];
106
}
107
}
108
109
void
ErrorMatrixCompressor::setUpperLimits
(
const
int
l[2])
110
{
111
for
(
int
i=0;i<2;i++)
112
{
113
m_limits
[i]=l[i];
114
}
115
}
116
117
118
bool
ErrorMatrixCompressor::CholeskyDecomposition
(
double
a
[5][5],
double
L[5][5])
119
{
120
121
int
i{},j{},k{};
122
double
sum{};
123
double
p[5]{};
124
125
for
(i=0;i<5;i++)
126
{
127
for
(j=i;j<5;j++)
128
{
129
sum=
a
[i][j];
130
for
(k=i-1;k>=0;k--)
131
sum-=
a
[i][k]*
a
[j][k];
132
if
(i==j)
133
{
134
if
(sum<=0.0)
135
{
136
return
false
;
137
}
138
p[i]=std::sqrt(sum);
139
L[i][i]=p[i];
140
}
141
else
142
{
143
a
[j][i]=sum/p[i];
144
L[j][i]=
a
[j][i];
145
}
146
}
147
}
148
return
true
;
149
}
150
151
bool
ErrorMatrixCompressor::compress
(
const
std::vector<double>& src, std::vector<unsigned int>& dest)
152
{
153
int
i{},j{};
154
double
C0[5][5]{},
C
[5][5]{};
155
156
157
dest.clear();
158
159
int
idx=0;
160
for
(i=0;i<5;i++)
161
for
(j=0;j<=i;j++)
162
{
163
C
[i][j]=src[idx];
164
C
[j][i]=C0[i][j]=C0[j][i]=
C
[i][j];
165
idx++;
166
}
167
double
L[5][5]{};
168
169
for
(i=0;i<5;i++)
170
for
(j=0;j<=i;j++)
171
{
172
C
[i][j]*=(
m_scales
[i]*
m_scales
[j]);
173
C
[j][i]=
C
[i][j];
174
}
175
if
(!
CholeskyDecomposition
(
C
,L))
return
false
;
176
//
177
float
S[5][5]{};
178
for
(i=0;i<5;i++)
179
for
(j=0;j<=i;j++) S[i][j]=L[i][j];
180
181
std::vector<FloatRep> vecFR;
182
vecFR.clear();
183
184
for
(i=0;i<5;i++)
185
{
186
for
(j=0;j<=i;j++)
187
{
188
m_decoder
.setF(S[i][j]);
189
char
sign
=(S[i][j]<0)?1:0;
190
std::uint32_t mant=
m_decoder
.getMantissa();
191
unsigned
short
int
ex=
m_decoder
.getExponent();
192
vecFR.push_back(
FloatRep
(
sign
,ex,mant));
193
}
194
}
195
std::vector<unsigned short> vShorts;
196
vShorts.clear();
197
if
(!
compressFR
(vecFR,vShorts))
return
false
;
198
199
//re-packing as ints
200
201
int
nBits=0;
202
std::uint32_t buffer = 0x00000000u;
203
204
for
(std::vector<unsigned short>::iterator it = vShorts.begin(); it != vShorts.end();++it) {
205
// printf("short = 0x%X\n",(*it));
206
if
(nBits==2) {
207
dest.push_back(buffer);
208
nBits=0;
209
buffer = 0x00000000;
210
}
211
if
(nBits==0) {
212
buffer = (*it);
213
buffer = buffer << 16;
214
nBits++;
215
}
216
else
{
217
buffer = buffer | (*it);
218
nBits++;
219
}
220
}
221
dest.push_back(buffer);
222
return
true
;
223
}
224
225
bool
ErrorMatrixCompressor::restore
(
const
std::vector<unsigned int>& src, std::vector<double>& dest)
226
{
227
int
i{},j{};
228
double
C
[5][5]{};
229
230
dest.clear();
231
232
std::vector<FloatRep> vecFR;
233
vecFR.clear();
234
235
std::vector<unsigned short> vShorts;
236
237
vShorts.clear();
238
239
for
(
unsigned
int
ii : src) {
240
unsigned
short
s1,s2;
241
242
s1 = (
unsigned
short)((0xFFFF0000 & ii) >> 16);
243
s2 = (
unsigned
short)(0x0000FFFF & ii);
244
vShorts.push_back(s1);
245
//if(!((s2==0) && ((it+1)==src.end()))) //do not store last zero
246
vShorts.push_back(s2);
247
}
248
249
if
(!
restoreFR
(vShorts,vecFR))
return
false
;
250
251
std::vector<FloatRep>::iterator fIt(vecFR.begin());
252
float
S[5][5]{};
253
for
(i=0;i<5;i++)
254
for
(j=0;j<=i;j++)
255
{
256
if
(fIt==vecFR.end())
break
;
257
S[i][j]=(*fIt).restore();
258
++fIt;
259
}
260
261
double
L[5][5]{};
262
for
(i=0;i<5;i++)
263
for
(j=0;j<=i;j++) L[i][j]=S[i][j];
264
for
(i=0;i<5;i++)
265
for
(j=i;j<5;j++)
266
{
267
C
[i][j]=0.0;
268
for
(
int
k=0;k<5;k++)
269
C
[i][j]+=L[i][k]*L[j][k];
270
C
[j][i]=
C
[i][j];
271
}
272
for
(i=0;i<5;i++)
273
for
(j=0;j<=i;j++)
274
{
275
C
[i][j]/=(
m_scales
[i]*
m_scales
[j]);
276
C
[j][i]=
C
[i][j];
277
}
278
for
(i=0;i<5;i++)
for
(j=0;j<=i;j++) dest.push_back(
C
[i][j]);
279
return
true
;
280
}
281
282
283
bool
ErrorMatrixCompressor::compressFR
(
const
std::vector<FloatRep>& src, std::vector<unsigned short>& dest)
284
{
285
unsigned
short
buf=0x0000;
286
dest.clear();
287
int
nMantLength=23-
m_bitStrip
;
288
if
(nMantLength<8)
289
{
290
std::cout<<
"Requested mantissa reduction is too large: 23->"
<<nMantLength<<std::endl;
291
return
false
;
292
}
293
std::vector<FloatRep>::const_iterator fIt;
294
295
// 1. Check limits
296
fIt=src.begin();
297
int
i,j;
298
for
(i=0;i<5;i++)
299
for
(j=0;j<=i;j++)
300
{
301
unsigned
short
int
ex=(*fIt).exponent();
302
std::uint32_t mant=(*fIt).mantissa();
303
int
bias,limit;
304
if
(i==j)
305
{
306
bias=
m_biases
[i];
307
limit=
m_limits
[0];
308
}
309
else
310
{
311
bias=
m_biases
[5];
312
limit=
m_limits
[1];
313
}
314
if
((ex!=0)&&(mant!=0))
315
{
316
if
((ex<bias)||(ex-bias>limit))
317
{
318
return
false
;
319
}
320
}
321
++fIt;
322
}
323
// 2. Pack exponents: diag e + non-diag s1 + non-diag e1 + non-diag s2 + non-diag e2
324
325
i=0;
326
for
(std::vector<Triplet>::iterator trIt=
m_tripVec
.begin();trIt!=
m_tripVec
.end();++trIt)
327
{
328
int
i1,i2[2];
329
i1=(*trIt).m_d;
330
i2[0]=(*trIt).m_nd1;i2[1]=(*trIt).m_nd2;
331
//printf("%d %d %d\n",i1,i2[0],i2[1]);
332
buf=0x0000;
333
unsigned
short
e;
334
335
e=src[i1].exponent()-
m_biases
[i];
336
buf = buf | ((e<<12) & 0xF000);
337
if
((src[i2[0]].exponent()==0)&&(src[i2[0]].mantissa()==0))
338
{
339
e=src[i2[0]].exponent();
340
buf = buf | 0x0800;
//-0
341
}
342
else
343
{
344
if
(src[i2[0]].
sign
())
345
buf = buf | 0x0800;
346
e=src[i2[0]].exponent()-
m_biases
[5];
347
}
348
buf = buf | ((e<<6) & 0x07C0);
349
if
((src[i2[1]].exponent()==0)&&(src[i2[1]].mantissa()==0))
350
{
351
buf = buf | 0x0020;
//-0
352
e=src[i2[1]].exponent();
353
}
354
else
355
{
356
e=src[i2[1]].exponent()-
m_biases
[5];
357
if
(src[i2[1]].
sign
())
358
buf = buf | 0x0020;
359
}
360
buf = buf | (e & 0x001F);
361
// printf("Exponent 0x%X\n",buf);
362
dest.push_back(buf);
363
i++;
364
}
365
// 3. Pack reduced mantissas
366
fIt=src.begin();
367
unsigned
int
nPacked=0;
368
int
nFreeBits=0,nBitsToStore=0,nBufferLength=0;
369
std::uint32_t srcBuffer=0x00000000u;
370
// printf("L=%d\n",nMantLength);
371
while
(nPacked<=src.size()+1)
372
{
373
if
(nFreeBits==0)
374
{
375
if
(nBufferLength!=0)
376
{
377
dest.push_back(buf);
378
// printf("Storing 0x%X\n",buf);
379
}
380
buf=0x0000;
381
nFreeBits=16;
382
nBufferLength++;
383
}
384
if
(nBitsToStore==0)
385
{
386
if
(nPacked!=0)
387
++fIt;
388
if
(fIt==src.end())
break
;
389
nPacked++;
390
srcBuffer=((*fIt).mantissa()<<9);
391
// printf("Packing 0x%X = %ud\n",(*fIt).mantissa(),(*fIt).mantissa());
392
nBitsToStore=nMantLength;
393
}
394
int
Np=(nBitsToStore>nFreeBits)?nFreeBits:nBitsToStore;
395
std::uint32_t mask=
m_srcMasks
[Np-1];
396
std::uint32_t slice = srcBuffer & mask;
397
slice = (slice >> (32-nFreeBits)) & 0x0000FFFF;
398
std::uint32_t tmp = slice;
399
// printf("Np=%d Tmp=0x%X\n",Np,tmp);
400
buf = buf | tmp;
401
srcBuffer = srcBuffer << Np;
402
// printf("dest=0x%X src=0x%X\n",buf,srcBuffer);
403
nFreeBits-=Np;
404
nBitsToStore-=Np;
405
}
406
dest.push_back(buf);
407
return
true
;
408
}
409
410
bool
ErrorMatrixCompressor::restoreFR
(
const
std::vector<unsigned short>& src, std::vector<FloatRep>& dest)
411
{
412
int
i,nRestored,nFreeBits,nBitsToStore;
413
unsigned
short
buf=0x0000;
414
std::uint32_t destBuffer=0x00000000;
415
std::vector<unsigned short>::const_iterator uIt(src.begin());
416
dest.clear();
417
418
for
(i=0;i<15;i++)
419
{
420
dest.push_back(
FloatRep
(0,0,0));
421
}
422
423
i=0;
424
for
(;uIt!=src.end();++uIt)
425
{
426
int
i0=
m_tripVec
[i].m_d;
427
int
i1=
m_tripVec
[i].m_nd1;
428
int
i2=
m_tripVec
[i].m_nd2;
429
buf=(*uIt);
430
char
s=((buf & 0x0800)==0)?0:1;
431
dest[i1].sign(s);
432
s=((buf & 0x0020)==0)?0:1;
433
dest[i2].sign(s);
434
unsigned
short
e = ((buf & 0xF000) >> 12);
435
dest[i0].exponent(e+
m_biases
[i]);
436
e = ((buf & 0x07C0) >> 6);
437
dest[i1].exponent(e+
m_biases
[5]);
438
e = (buf & 0x001F);
439
dest[i2].exponent(e+
m_biases
[5]);
440
i++;
441
if
(i==5)
break
;
442
}
443
if
(i<5)
return
false
;
444
nRestored=0;
445
nBitsToStore=0;
446
nFreeBits=0;
447
int
nMantLength=23-
m_bitStrip
;
448
while
(nRestored<=15)
449
{
450
if
(nFreeBits==0)
451
{
452
if
(nRestored!=0)
453
{
454
//printf("Dest 0x%X ",destBuffer);
455
destBuffer = destBuffer <<
m_bitStrip
;
456
//printf("<< 0x%X = %lu\n",destBuffer,destBuffer);
457
dest[nRestored-1].mantissa(destBuffer);
458
}
459
++nRestored;
460
destBuffer=0x00000000;
461
nFreeBits=nMantLength;
462
}
463
if
(nBitsToStore==0)
464
{
465
if
(uIt==src.end())
break
;
466
++uIt;
467
if
(uIt==src.end())
468
{
469
//printf("Breaking ... nR=%d\n",nRestored);
470
break
;
471
}
472
nBitsToStore=16;buf=(*uIt);
//printf("Source 0x%X\n",buf);
473
}
474
int
Np=(nFreeBits>nBitsToStore) ? nBitsToStore : nFreeBits;
475
std::uint32_t tmp = buf;
476
tmp = tmp >> (16-Np);
477
nBitsToStore-=Np;
478
destBuffer = destBuffer | tmp;
479
nFreeBits-=Np;
480
}
481
return
true
;
482
}
483
ErrorMatrixCompressor.h
a
static Double_t a
Definition
LArPhysWaveHECTool.cxx:38
sign
int sign(int a)
Definition
TRT_StrawNeighbourSvc.h:108
DecoderFloat_IEEE754::getExponent
short int getExponent()
Definition
ErrorMatrixCompressor.cxx:25
DecoderFloat_IEEE754::m_data
std::uint32_t m_data
Definition
ErrorMatrixCompressor.h:52
DecoderFloat_IEEE754::getMantissa
unsigned int getMantissa()
Definition
ErrorMatrixCompressor.cxx:31
DecoderFloat_IEEE754::setExponent
void setExponent(short int)
Definition
ErrorMatrixCompressor.cxx:38
DecoderFloat_IEEE754::print
void print()
Definition
ErrorMatrixCompressor.cxx:20
DecoderFloat_IEEE754::setSign
void setSign(int)
Definition
ErrorMatrixCompressor.cxx:46
ErrorMatrixCompressor::Triplet
Definition
ErrorMatrixCompressor.h:114
ErrorMatrixCompressor::m_srcMasks
unsigned int m_srcMasks[16]
Definition
ErrorMatrixCompressor.h:149
ErrorMatrixCompressor::compressFR
bool compressFR(const std::vector< FloatRep > &, std::vector< unsigned short > &)
Definition
ErrorMatrixCompressor.cxx:283
ErrorMatrixCompressor::m_biases
short int m_biases[6]
Definition
ErrorMatrixCompressor.h:141
ErrorMatrixCompressor::setUpperLimits
void setUpperLimits(const int l[2])
Definition
ErrorMatrixCompressor.cxx:109
ErrorMatrixCompressor::m_limits
short int m_limits[2]
Definition
ErrorMatrixCompressor.h:142
ErrorMatrixCompressor::restore
bool restore(const std::vector< unsigned int > &, std::vector< double > &)
Definition
ErrorMatrixCompressor.cxx:225
ErrorMatrixCompressor::m_tripVec
std::vector< Triplet > m_tripVec
Definition
ErrorMatrixCompressor.h:150
ErrorMatrixCompressor::m_scales
double m_scales[5]
Definition
ErrorMatrixCompressor.h:143
ErrorMatrixCompressor::compress
bool compress(const std::vector< double > &, std::vector< unsigned int > &)
Definition
ErrorMatrixCompressor.cxx:151
ErrorMatrixCompressor::m_bitStrip
int m_bitStrip
Definition
ErrorMatrixCompressor.h:145
ErrorMatrixCompressor::ErrorMatrixCompressor
ErrorMatrixCompressor(int)
Definition
ErrorMatrixCompressor.cxx:59
ErrorMatrixCompressor::m_decoder
DecoderFloat_IEEE754 m_decoder
Definition
ErrorMatrixCompressor.h:148
ErrorMatrixCompressor::CholeskyDecomposition
bool CholeskyDecomposition(double a[5][5], double L[5][5])
Definition
ErrorMatrixCompressor.cxx:118
ErrorMatrixCompressor::restoreFR
bool restoreFR(const std::vector< unsigned short > &, std::vector< FloatRep > &)
Definition
ErrorMatrixCompressor.cxx:410
ErrorMatrixCompressor::setBiases
void setBiases(const int b[6])
Definition
ErrorMatrixCompressor.cxx:101
FloatRep
Definition
ErrorMatrixCompressor.h:56
C
struct color C
Generated on
for ATLAS Offline Software by
1.17.0