10#define UINT32 uint32_t
11#define UINT16 uint16_t
17#define code_ped4 TileRawChannel2Bytes5::code_ped4
18#define code_ped5 TileRawChannel2Bytes5::code_ped5
19#define code_amp5 TileRawChannel2Bytes5::code_amp5
20#define code_amp6 TileRawChannel2Bytes5::code_amp6
21#define code_raws TileRawChannel2Bytes5::code_raws
22#define code_rawf TileRawChannel2Bytes5::code_rawf
23#define code_full TileRawChannel2Bytes5::code_full
24#define code_dump TileRawChannel2Bytes5::code_dump
25#define code_null TileRawChannel2Bytes5::code_null
32const int OF_E_FRACBITS[4] = { 4, 5, 5, -5 };
36const unsigned short ampcorr_lookup[64] = {
37 0x505A, 0x4F62, 0x4E71, 0x4D87, 0x4CA4, 0x4BC8, 0x4AF4, 0x4A26,
38 0x4960, 0x48A1, 0x47EA, 0x473A, 0x4692, 0x45F1, 0x4558, 0x44C7,
39 0x443D, 0x43BC, 0x4342, 0x42D0, 0x4267, 0x4205, 0x41AC, 0x415B,
40 0x4112, 0x40D2, 0x409A, 0x406B, 0x4044, 0x4026, 0x4011, 0x4004,
41 0x4000, 0x4005, 0x4013, 0x402A, 0x404A, 0x4073, 0x40A6, 0x40E2,
42 0x4127, 0x4175, 0x41CD, 0x422F, 0x429A, 0x430E, 0x438D, 0x4415,
43 0x44A7, 0x4543, 0x45E9, 0x4699, 0x4753, 0x4817, 0x48E6, 0x49BF,
44 0x4AA2, 0x4B8F, 0x4C87, 0x4D8A, 0x4E97, 0x4FAF, 0x50D2, 0x51FF
49int32_t _ext(int32_t d, uint32_t
a, uint32_t b) {
50 a =
a & 0x1F; b = b & 0x1F;
51 return ((int32_t)((d <<
a) & 0xFFFFFFFF)) >> b;
53uint32_t _extu(uint32_t d, uint32_t
a, uint32_t b) {
54 a =
a & 0x1F; b = b & 0x1F;
55 return ((d <<
a) & 0xFFFFFFFF) >> b;
57int32_t _extr(int32_t d, uint32_t ab) {
58 return _ext(d, ab >> 5, ab);
62uint32_t _hi(uint32_t d) {
return (d >> 16) & 0xFFFF; }
63uint32_t _lo(uint32_t d) {
return d & 0xFFFF; }
65uint32_t _pack2(uint32_t
a, uint32_t b) {
return (
a & 0xFFFF) << 16 | (b & 0xFFFF); }
67uint32_t _packh2(uint32_t
a, uint32_t b) {
return _pack2(
a >> 16, b >> 16); }
68uint32_t _sub2(uint32_t
a, uint32_t b) {
return _pack2(_hi(
a) - _hi(b), _lo(
a) - _lo(b)); }
69uint32_t _set(uint32_t d,
int a,
int b) {
return d | _extu(0xFFFFFFFF,
a, b); }
70uint32_t _clr(uint32_t d,
int a,
int b) {
return d & ~_extu(0xFFFFFFFF,
a, b); }
71uint32_t _setr(
int a,
int b) {
return _set(0,
a, b); }
74unsigned char getbyte(
const unsigned char* ptr) {
76 uintptr_t u =
reinterpret_cast<uintptr_t
> (ptr);
77 u = u + 3 - 2*(u % 4);
78 ptr =
reinterpret_cast<const unsigned char*
> (u);
83uint8_t _mem1(
const unsigned char* ptr) {
88uint8_t _mem1(
const char* ptr) {
return _mem1((
const unsigned char*) ptr); }
93uint16_t _mem2(
const unsigned char* ptr) {
97 return (b0 << 8) | b1;
99uint16_t _mem2(
const char* ptr) {
return _mem2((
const unsigned char*) ptr); }
102uint32_t _mem4(
const unsigned char* ptr) {
103 unsigned char b0, b1, b2, b3;
108 return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
112uint32_t _mem4(
const char* ptr) {
return _mem4((
const unsigned char*) ptr); }
115uint32_t _packhl2(uint32_t
a, uint32_t b) {
return (
a & 0xFFFF0000) | (b & 0x0000FFFF); }
119uint32_t _norm(uint32_t d) {
120 uint32_t u = d & 0x80000000;
123 while ((u == (d & 0x80000000)) && (n < 31)) {
132int32_t _sadd(int32_t
a, int32_t b) {
134 if (
a > 0 && b > 0 && u < 0) u = 0x7FFFFFFF;
else
135 if (
a < 0 && b < 0 && u > 0) u = 0x80000000;
139int32_t _sshl(int32_t
a, uint32_t b) {
143 if (
a > 0) u = 0x7FFFFFFF;
else u = 0x80000000;
148int32_t _mpy(uint32_t
a, uint32_t b) {
149 int16_t a16 = (
a & 0xFFFF), b16 = (b & 0xFFFF);
150 int32_t a32 = a16, b32 = b16;
153int32_t _mpyh(uint32_t
a, uint32_t b) {
return _mpy(
a >> 16, b >> 16); }
154int32_t _mpyhl(uint32_t
a, uint32_t b) {
return _mpy(
a >> 16, b); }
156int32_t _dotp2(uint32_t
a, uint32_t b) {
157 return _mpy(
a, b) + _mpyh(
a, b);
159uint32_t _ssub(int32_t
a, int32_t b) {
160 int64_t a64 =
a, b64 = b;
161 int64_t u = 0x100000000LL;
162 if (a64 - b64 > u - 1)
return (u - 1) & 0xFFFFFFFF;
163 if (a64 - b64 < -u)
return -u & 0xFFFFFFFF;
164 return (a64 - b64) & 0xFFFFFFFF;
167uint32_t _deal(uint32_t u) {
168 uint32_t u1, u0, k, k1, k0;
170 k = 1; k0 = 1; k1 = 1 << 16;
172 for (
int i = 0; i < 16; ++i, (k1 <<= 1), (k <<= 1)) {
197#define sbits(u, n) (32 - 1 - (n) < _norm(u))
198#define mvbits(u, n, i) _extu(u, 32 - (n), 32 - (n + i))
199#define mvbits_ex(u, n, i, i0) mvbits(mvbits(u, n + i0, -(i0)), n, i)
200#define nonsat_ene(ene) ((0 < ene) && (ene < 0x7FFF))
203#define _round(u, k) (_ext(u + _sshl(0x1, k - 1), 0, k))
205#define ssatbits(u, n) _ext(_sshl(u, 32 - (n)), 0, 32 - (n))
208#define saturate(u, n) _ext(_sshl(u + _abs(u), 30 - (n)), 0, 31 - (n))
214inline int _roundf(
float f) {
215 if (f > 0)
return (
int)(
f + 0.5);
216 else return (
int)(
f - 0.5);
227 int FRACBITS_MINUS_11 = _sadd(OF_E_FRACBITS[
unit], 11);
228 UINT16 calib_coeff = _extu(calib, 0, 16);
229 UINT16 calib_scale = _extu(calib, 16, 16) - FRACBITS_MINUS_11;
230 UINT16 uncalib_coeff = (int)((1.0/calib_coeff)*(1 << 14)*(1 << 14) + 0.5);
231 UINT16 uncalib_scale = 17 - calib_scale;
233 UINT32 uncalib = _pack2(uncalib_coeff, uncalib_scale);
239 return _packhl2(_extr(hg, shift), hg);
243INT16 get_ampcorr_coeff(
int time)
248 double ampcorr = ((0.0000006467*
time + 0.0002713687)*
time + 0.0000365732)*time + 1;
249 return _roundf(ampcorr*(0x1 << 14));
253void print_ampcorr_lookup() {
257 printf(
"// ampcorr lookup table, step = 0.5 ns\n");
258 printf(
"unsigned short ampcorr_lookup[64] = {");
259 for (time = -32;
time < 32;
time++) {
260 UINT16 ampcorr_coeff = get_ampcorr_coeff(time);
261 if (k % 8 == 0) printf(
"\n ");
263 printf(
" 0x%04X,", ampcorr_coeff);
270#define unpack_diff(diff, nbits, d2, d3, d5, d6, d7) \
272 UINT32 u = _deal(_deal((diff >> nbits))); \
273 d5 = _ext(diff, 32 - nbits, 32 - nbits); \
274 d7 = _ext(u, 8 - nbits, 32 - nbits); \
275 d6 = _ext(u, 16 - nbits, 32 - nbits); \
276 d3 = _ext(u, 24 - nbits, 32 - nbits); \
277 d2 = _ext(u, 32 - nbits, 32 - nbits); \
280#define fill_samp(s1, s2, s3, s4, s5, s6, s7, s) \
291int get_amp_reco(
const UINT32* ofw,
int unit,
int chan,
int gain,
int ene0)
298 INT32 amp_reco = _extr(_mpyhl(uncalib, ene0), uncalib);
302int get_s4(
const UINT32* ofw,
int unit,
int chan,
int gain,
int ene,
303 int s1,
int s2,
int s3,
int s5,
int s6,
int s7)
308 UINT32 s1x, s32, s5x, s76;
309 UINT32 a1x, a32, a54, a76;
310 int amp, amp_reco, a4,
s4;
314 s32 = _pack2(s3, s2);
316 s76 = _pack2(s7, s6);
326 amp_reco = get_amp_reco(ofw,
unit, chan, gain, ene);
328 amp = _mpyh (a1x, s1x)
333 a4s4 = amp_reco - amp;
334 a4 = _ext(a54, 16, 16);
335 s4 = _roundf(a4s4/a4);
340inline UINT32 pop_buf(
const unsigned char** pptr_buff,
int size)
344 *pptr_buff += (size) >> 3;
348int calc_ped_s4(
const UINT32* ofw,
int unit,
int chan,
int gain,
int ene,
349 int s1,
int s2,
int s3,
int s5,
int s6,
int s7)
351 return get_s4(ofw,
unit, chan, gain, ene, s1, s2, s3, s5, s6, s7);
354void calc_amp(
const UINT32* ofw,
int unit,
int chan,
int gain,
int ene,
int time,
355 int s1,
int d2,
int d3,
int d5,
int d6,
int d7,
int s[])
357 int s2,
s3,
s4, s5, s6, s7;
358 INT16 amp2, ampcorr, a_scale;
366 a_scale = _extu(ofc[0], 16, 16);
367 amp_reco = get_amp_reco(ofw,
unit, chan, gain, ene);
368 amp2 = (
INT16) (amp_reco >> a_scale);
370 ampcorr = ampcorr_lookup[
time + 32];
371 amp2 = _mpy(amp2, ampcorr) >> 14;
372 amptime = _sshl(
_round(_mpy(amp2, time), 3), 16);
373 wamptime = _packhl2((~amptime) + 1, amp2);
376 UINT16 g_scale = _extu(ofc[13], 16, 16);
377 UINT16 h_scale = _extu(ofc[13], 0, 16) - 1;
378 UINT16 shift = h_scale - g_scale;
389 int q2 = _extr(_dotp2(repack_hg(hg2, shift), wamptime), g_scale);
390 int q3 = _extr(_dotp2(repack_hg(hg3, shift), wamptime), g_scale);
391 int q5 = _extr(_dotp2(repack_hg(hg5, shift), wamptime), g_scale);
392 int q6 = _extr(_dotp2(repack_hg(hg6, shift), wamptime), g_scale);
393 int q7 = _extr(_dotp2(repack_hg(hg7, shift), wamptime), g_scale);
401 s4 = get_s4(ofw,
unit, chan, gain, ene, s1, s2, s3, s5, s6, s7);
403 fill_samp(s1, s2, s3, s4, s5, s6, s7, s);
406void calc_raw(
const UINT32* ofw,
int unit,
int chan,
int gain,
int ene,
407 int s1,
int s2,
int s3,
int s5,
int s6,
int s7,
int s[])
409 int s4 = get_s4(ofw,
unit, chan, gain, ene, s1, s2, s3, s5, s6, s7);
411 fill_samp(s1, s2, s3, s4, s5, s6, s7, s);
414int unpack_null(
const UINT32* ,
int ,
const UINT32** pptr_reco,
415 int* gain,
int* ene,
int* time,
int s[])
418 UINT32 code = get_code(reco);
426 for (i = 0;
i < 7;
i++)
427 s[i] = (reco >> 1) & 0x3FF;
432int unpack_ped(
const UINT32* ofw,
int unit,
int chan,
433 const UINT32** pptr_reco,
const unsigned char** pptr_buff,
434 int* gain,
int* ene,
int* time,
int s[])
438 int s1,
s2,
s3,
s4, s5, s6, s7,
d2, d3, d5, d6, d7;
441 UINT32 code = get_code(reco);
453 data = pop_buf(pptr_buff, 8);
460 data = pop_buf(pptr_buff, 16);
472 s4 = calc_ped_s4(ofw,
unit, chan, *gain, *ene, s1, s2, s3, s5, s6, s7);
473 fill_samp(s1, s2, s3, s4, s5, s6, s7, s);
477int unpack_amp(
const UINT32* ofw,
int unit,
int chan,
478 const UINT32** pptr_reco,
const unsigned char** pptr_buff,
479 int* gain,
int* ene,
int* time,
int s[])
483 int s1 = 0,
d2, d3, d5, d6, d7;
486 UINT32 code = get_code(reco);
491 *ene =
mvbits_ex(reco, 15, 0, 0) - ((*
gain == 0) ? 512 : 2048);
492 *
time = _ext(reco, 32 - 15 - 6, 32 - 6);
497 diff = _extu(pop_buf(pptr_buff, 24), 1, 0) |
diff;
503 diff = pop_buf(pptr_buff, 32);
504 s1 = _extu(
diff, 32 - 2, 32 - 2 - 5) |
s1;
511 calc_amp(ofw,
unit, chan, *gain, *ene, *time, s1, d2, d3, d5, d6, d7, s);
515int unpack_raw(
const UINT32* ofw,
int unit,
int chan,
516 const UINT32** pptr_reco,
const unsigned char** pptr_buff,
517 int* gain,
int* ene,
int* time,
int s[])
519 int s1,
s2,
s3, s5, s6, s7;
522 UINT32 code = get_code(reco);
528 *ene =
mvbits_ex(reco, 15, 0, 0) - ((*
gain == 0) ? 512 : 2048);
533 diff1 = pop_buf(pptr_buff, 32);
534 diff2 = pop_buf(pptr_buff, 8);
539 s6 =
mvbits_ex(diff1, 9, 0, 10 + 10 + 3);
544 diff1 = pop_buf(pptr_buff, 32);
545 diff2 = pop_buf(pptr_buff, 16);
555 calc_raw(ofw,
unit, chan, *gain, *ene, s1, s2, s3, s5, s6, s7, s);
559int unpack_full(
const UINT32* ,
int ,
560 const UINT32** pptr_reco,
const unsigned char** pptr_buff,
561 int* gain,
int* ene,
int* time,
int s[])
565 UINT32 code = get_code(reco);
570 diff1 = pop_buf(pptr_buff, 32);
571 diff2 = pop_buf(pptr_buff, 16);
575 *ene = (overflow ? 0x7FFF : 0) - ((*gain == 0) ? 512 : 2048);
589int unpack_dump(
const UINT32* ,
int ,
590 const UINT32** pptr_reco,
const unsigned char** pptr_buff,
591 int* gain,
int* ene,
int* time,
int s[])
595 UINT32 code = get_code(reco);
599 diff1 = pop_buf(pptr_buff, 32);
600 diff2 = pop_buf(pptr_buff, 24);
603 *ene =
mvbits_ex(reco, 15, 0, 0) - ((*
gain == 0) ? 512 : 2048);
626 UINT32 calib, calib_coeff;
628 int amp, ene, ene0, ene_shift;
632 int FRACBITS_MINUS_11 = _sadd(OF_E_FRACBITS[
unit], 11);
634 int s1 = s[0], s2 = s[1], s3 = s[2], s4 = s[3], s5 = s[4], s6 = s[5], s7 = s[6];
637 UINT32 s1x = _pack2(s1, gain << 15);
638 UINT32 s32 = _pack2(s3, s2);
639 UINT32 s54 = _pack2(s5, s4);
640 UINT32 s76 = _pack2(s7, s6);
642 ene_shift = 512 + _mpy(gain, 2048 - 512);
653 amp = _mpyh (a1x, s1x)
661 calib_coeff = _ext(calib, 0, 16);
662 calib_scale = _ssub(_extu(calib, 16, 16) , FRACBITS_MINUS_11);
664 ene0 =
_round(ene0*calib_coeff, calib_scale);
665 ene = ene0 + ene_shift;
673 int unit = _extu(ptr_frag[2], 0, 32 - 2);
674 int size_L2 = _extu(ptr_frag[2], 2, 32 - 3);
675 int i, chan, code, size;
676 int gain(0),
bad, ene(0), time(0), u[7];
677 const UINT32* ptr_reco = ptr_frag + 3;
678 const unsigned char* ptr_buff =
reinterpret_cast<const unsigned char*
> (ptr_frag + 3 + 48 + size_L2);
679 const UINT32* ptr = ptr_reco;
683 const unsigned char* ptr_ped4;
684 const unsigned char* ptr_ped5;
685 const unsigned char* ptr_amp5;
686 const unsigned char* ptr_amp6;
687 const unsigned char* ptr_raws;
688 const unsigned char* ptr_rawf;
689 const unsigned char* ptr_full;
690 const unsigned char* ptr_dump;
692 int cnt_ped4, cnt_ped5, cnt_amp5, cnt_amp6,
693 cnt_raws, cnt_rawf, cnt_full, cnt_null, cnt_dump;
694 cnt_ped4 = cnt_ped5 = cnt_amp5 = cnt_amp6 =
695 cnt_raws = cnt_rawf = cnt_full = cnt_null = cnt_dump = 0;
697 bad_bits[0] = pop_buf(&ptr_buff, 16);
698 bad_bits[1] = pop_buf(&ptr_buff, 16);
699 bad_bits[2] = pop_buf(&ptr_buff, 16);
701 for (chan = 0; chan < 48; ++chan) {
702 for (i = 0; i < 7; ++i) u[i] = 0;
716 ptr_ped5 = ptr_ped4 + cnt_ped4*1;
717 ptr_amp5 = ptr_ped5 + cnt_ped5*2;
718 ptr_amp6 = ptr_amp5 + cnt_amp5*3;
719 ptr_raws = ptr_amp6 + cnt_amp6*4;
720 ptr_rawf = ptr_raws + cnt_raws*5;
721 ptr_full = ptr_rawf + cnt_rawf*6;
722 ptr_dump = ptr_full + cnt_full*6;
724 for (chan = 0; chan < 48; ++chan) {
725 if (chan % 16 == 0) bad16 = ~bad_bits[chan/16];
726 bad = (bad16 & 0x1); bad16 >>= 1;
730 if (code ==
code_ped4) size = unpack_ped (ofw,
unit, chan, &ptr, &ptr_ped4, &gain, &ene, &time, u);
else
731 if (code ==
code_ped5) size = unpack_ped (ofw,
unit, chan, &ptr, &ptr_ped5, &gain, &ene, &time, u);
else
732 if (code ==
code_amp5) size = unpack_amp (ofw,
unit, chan, &ptr, &ptr_amp5, &gain, &ene, &time, u);
else
733 if (code ==
code_amp6) size = unpack_amp (ofw,
unit, chan, &ptr, &ptr_amp6, &gain, &ene, &time, u);
else
734 if (code ==
code_raws) size = unpack_raw (ofw,
unit, chan, &ptr, &ptr_raws, &gain, &ene, &time, u);
else
735 if (code ==
code_rawf) size = unpack_raw (ofw,
unit, chan, &ptr, &ptr_rawf, &gain, &ene, &time, u);
else
736 if (code ==
code_full) size = unpack_full(ofw, chan, &ptr, &ptr_full, &gain, &ene, &time, u);
else
737 if (code ==
code_dump) size = unpack_dump(ofw, chan, &ptr, &ptr_dump, &gain, &ene, &time, u);
else
738 if (code ==
code_null) size = unpack_null(ofw, chan, &ptr, &gain, &ene, &time, u);
739 if (size == 0) printf(
"\nTileRawChannel2Bytes5::unpack ERROR size = 0\n");
742 ChanData[chan].
code = code;
743 ChanData[chan].
gain = gain;
750 ChanData[chan].
time = time/2.0;
752 for (i = 0; i < 7; ++i) ChanData[chan].s[i] = u[i];
760 int gain, ene, u[7], s[7];
762 for (chan = 0; chan < 48; ++chan) {
766 gain =
Data[chan].gain;
767 ene =
Data[chan].ene_bin;
770 for (i = 0; i < 7; ++i) u[i] =
Data[chan].s[i];
778 s[1-1] = _extu(s1x, 0, 16);
779 s[2-1] = _extu(s32, 16, 16);
780 s[3-1] = _extu(s32, 0, 16);
781 s[4-1] = _extu(s54, 16, 16);
782 s[5-1] = _extu(s54, 0, 16);
783 s[6-1] = _extu(s76, 16, 16);
784 s[7-1] = _extu(s76, 0, 16);
787 for (i = 0; i < 7; ++i) {
788 if (s[i] != u[i]) chOK =
false;
790 if (!chOK) { printf(
"\n %d: Raw Data ERROR", chan); }
792 ene += (gain == 0 ? 512 : 2048);
794 if (of_energy[chan] != ene) {
796 printf(
"\n %d : amp error ene: %d e_t_amp: %d\n", chan, ene, of_energy[chan]);
801 printf(
"\n %2d ERROR", chan);
804 printf(
"\nunpack_data");
805 if (OK) printf(
" OK");
else printf(
" ERROR");
814 int bad, gain, amp, ene, time;
815 const UINT32* reco = (frag + 3);
818 for (chan = 0; chan < 48; ++chan) {
822 printf(
"\nTileRawChannel2Bytes5::unpack_reco_bin ERROR\n");
828 if (of_energy[chan] != ene) {
const PlainObject unit() const
This is a plugin that makes Eigen look like CLHEP & defines some convenience methods.
char data[hepevt_bytes_allocation_ATLAS]
void diff(const Jet &rJet1, const Jet &rJet2, std::map< std::string, double > varDiff)
Difference between jets - Non-Class function required by trigger.
#define unpack_diff(diff, nbits, d2, d3, d5, d6, d7)
#define fill_samp(s1, s2, s3, s4, s5, s6, s7, s)
#define mvbits_ex(u, n, i, i0)
const double AMPLITUDE_FACTOR5_HG[4]
const double AMPLITUDE_FACTOR5_LG[4]
const int AMPLITUDE_OFFSET5[2]
const int NOFWORDS_WEIGHTS_7S
const int NOFWORDS_WEIGHTS_7S_1GAIN
static int get_code(uint32_t reco)
bool check_reco(const uint32_t *frag, int of_energy[]) const
bool unpack_reco_bin(unsigned int w, int &fmt, int &gain, int &, int &time) const
int amplitude(const uint32_t *ofw, int unit, int chan, int gain, int s[]) const
void unpack(const uint32_t *ofw, const uint32_t *ptr_frag, TileChanData *ChanData) const
bool check_raw(const uint32_t *feb, int of_energy[], TileChanData *ChanData) const
time(flags, cells_name, *args, **kw)