ATLAS Offline Software
Compressor.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /*
6  * Compressor.cpp
7  * Compressor
8  *
9  * Created by Ilija Vukotic on 05/07/07.
10  * <ivukotic@cern.ch>
11  *
12  */
13 
15 using namespace std;
16 
17 // ---------- STANDARD
18 void Compressor::reduceToUS(const std::vector<float> &vf, std::vector<unsigned short> &vc ){
19  constexpr unsigned int max_short_flt = 0x7f7f7fff;
20  union {unsigned int u;float f;} m;
21  vc.reserve(vf.size());
22  for (const auto& value : vf){
23  m.f = value;
24  if ( (m.u & 0x7fffffff) > max_short_flt) vc.push_back (m.u>>16);
25  else vc.push_back((m.u+0x8000)>>16);
26  }
27 }
28 
29 void Compressor::expandFromUStoFloat(const std::vector<unsigned short> &vc, std::vector<float> &vf){
30  union {unsigned int u;float f;} m;
31  vf.reserve(vc.size());
32  for (const auto& value : vc){
33  unsigned int ui(value << 16);
34  m.u = ui;
35  vf.push_back(m.f);
36  }
37 }
38 
39 // ------------- WITH BITS AND SIGN
40 void Compressor::reduce(const std::vector<float> &vf, std::vector<unsigned int> &vi){
41 
42  vi.reserve(vf.size()*m_bits/16+17);//this is roughly. can be made precisse. depends on sign stripping too.
43  // composing format word
44  unsigned int format=0;
45  unsigned int vfs=vf.size();
46  format |= vfs; // writes down 25 bits of vector size
47  format=format<<6;
48  format |= m_bits & 63;// six bits
49  format =format<<1;
50  format |= m_sign;
51  vi.push_back(format);
52 
53  // initializing variables
54  int bshift=32-m_bits;
55  int rounding=0x1<<(bshift-1);
56  unsigned int vmax= 0x7f7<<20;
57  vmax |= 0x000fffff xor (rounding);
58  unsigned int rems=0xffffffff;
59  if(m_sign)
60  rems>>=(bshift+1);
61  else
62  rems>>=bshift;
63 
64  union {unsigned int u;float f;} m;
65 
66 
67  if (m_bitStrip){
68  int F=32;
69  int L;
70  if (m_sign) L=m_bits-1; else L=m_bits;
71  unsigned int CUR=0; int IN=0;
72  for (const auto& value : vf){
73  m.f = value;
74  if ( (m.u & 0x7fffffff) > vmax) IN=m.u>>bshift;
75  else IN=(m.u+rounding)>>bshift;
76 
77  IN&=rems;
78 
79  if (F>L){
80  F-=L;
81  CUR|=(IN<<F);
82  }
83  else if(F==L){
84  F-=L;
85  CUR|=(IN<<F);
86  vi.push_back(CUR);
87  CUR=0;F=32;
88  }
89  else{
90  int D=L-F;
91  CUR|=(IN>>D);
92  vi.push_back(CUR);
93  CUR=0; F=32-D;
94  // Avoid int overflow in shift.
95  IN &= (1U<<D)-1;
96  CUR|=(IN<<F);
97  }
98  }
99  vi.push_back(CUR);
100 
101  }
102  else
103  for (const auto& value : vf){
104  m.f = value;
105  vi.push_back(m.u);
106  }
107 
108  return;
109 }
110 
111 
112 void Compressor::expandToFloat(const std::vector<unsigned int> & vi, std::vector<float> & vf){
113  vf.clear();
114  if (vi.empty())
115  return;
116 
117  std::vector<unsigned int>::const_iterator i=vi.begin();
118 
119  int format=(*i); ++i;
120  m_sign= 1 & format;
121 // if (m_sign) cout<<"Sign was neglected"<<endl;
122  format=format>>1;
123  m_bits= 63 & format;
124 // cout<<"Uncompressing "<<m_bits<<" bits!"<<endl;
125  unsigned int vecs=(format>>6)&0xfffffff;
126 // cout<<"vec size: "<<vecs<<endl;
127  int bshift=32-m_bits;
128 
129  vf.reserve(vi.size()*m_bits/16+17); //this is roughly. can be made precisse. depends on sign stripping too.
130 
131  int L;
132  if (m_sign) L=m_bits-1; else L=m_bits;
133  int CP=0; int FP=0; int REM=0;
134  unsigned int V=0xffffffff>>(32-L); unsigned int R=0;
135  unsigned int ui(*i);
136 
137  union {unsigned int u;float f;} m;
138 
139  while (vecs){
140  FP = CP + L; // Future point = Current point + lenght
141  if (FP<=32){ // all of it is inside this integer
142  R = ( ui >> (32-FP) ) & V;
143  R <<= bshift;
144  m.u=R;
145  vf.push_back(m.f);
146  if (FP < 32)
147  CP=FP;
148  else {
149  CP = 0;
150  ++i;
151  ui = (*i); // take next integer
152  }
153  }
154  else{ // part of the float is in the next integer
155  REM = FP - 32; // Remainder = Future point - 32
156  R = ( ui & (0xffffffff >> CP) ) << REM; // find first part
157  ++i;
158  ui = (*i); // take next integer
159  R |= ui >> (32-REM) ;
160  R <<= bshift;
161  if (m_sign) R &= 0x7fffffff;
162  m.u=R;
163  vf.push_back(m.f);
164  CP = REM; // move Current point
165  }
166  --vecs;
167  }
168  return;
169 }
vtune_athena.format
format
Definition: vtune_athena.py:14
Compressor::reduceToUS
void reduceToUS(const std::vector< float > &vf, std::vector< unsigned short > &vi)
Definition: Compressor.cxx:18
athena.value
value
Definition: athena.py:124
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:25
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
Compressor.h
Compressor::expandToFloat
void expandToFloat(const std::vector< unsigned int > &vc, std::vector< float > &vf)
Definition: Compressor.cxx:112
lumiFormat.i
int i
Definition: lumiFormat.py:85
Compressor::reduce
void reduce(const std::vector< float > &vf, std::vector< unsigned int > &vi)
Definition: Compressor.cxx:40
skel.rounding
int rounding
Check if the tune/PDF part is needed, and if so whether it's present.
Definition: skel.ABtoEVGEN.py:291
CaloCellPos2Ntuple.x7fffffff
x7fffffff
Definition: CaloCellPos2Ntuple.py:24
hist_file_dump.f
f
Definition: hist_file_dump.py:140
AnalysisUtils::Delta::R
double R(const INavigable4Momentum *p1, const double v_eta, const double v_phi)
Definition: AnalysisMisc.h:49
Compressor::expandFromUStoFloat
void expandFromUStoFloat(const std::vector< unsigned short > &vi, std::vector< float > &vf)
Definition: Compressor.cxx:29
F
#define F(x, y, z)
Definition: MD5.cxx:112
PlotCalibFromCool.vmax
vmax
Definition: PlotCalibFromCool.py:697
python.SystemOfUnits.m
float m
Definition: SystemOfUnits.py:106
python.SystemOfUnits.L
float L
Definition: SystemOfUnits.py:92