ATLAS Offline Software
compressB64.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
4 
5 """ Encodes a bit string composed mostly by zeroes"""
6 
7 _B64_alphabet = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8  "abcdefghijklmnopqrstuvwxyz"
9  "0123456789+/")
10 
11 
12 def _toB64(n):
13  s = ""
14  while n > 63:
15  s = _B64_alphabet[n % 64] + s
16  n = n//64
17  s = _B64_alphabet[n % 64] + s
18  return s
19 
20 
21 def _fromB64(s):
22  n = 0
23  for i in range(0, len(s)):
24  n *= 64
25  n += _B64_alphabet.index(s[i])
26  return n
27 
28 
29 def compressB64(s):
30  """Encodes a string of zeroes and ones using a RLE method.
31 
32  s is the string to encode.
33 
34  Consecutive zeroes are encoded by its repetition count in Base64.
35  Ones are copied to output substituted by exclamation marks.
36 
37  Example:
38  input: 0001100001001101010000
39  output: D!!E!C!!B!B!E
40 
41  Effective compression is only achieved when input is composed mostly
42  by zeroes.
43 
44  The encoded string is returned.
45  """
46 
47  count = 0
48  res = ""
49  for k in range(0, len(s)):
50  c = s[k]
51  if c == '0':
52  count += 1
53  else:
54  if count > 0:
55  res += _toB64(count)
56  res += "!"
57  count = 0
58 
59  if count > 0:
60  res += _toB64(count)
61 
62  return res
63 
64 
66  """Decodes a string coded by compressB().
67 
68  s is the string to decode.
69 
70  The decoded string is returned.
71  """
72 
73  res = ""
74  count = ""
75  for k in range(0, len(s)):
76  c = s[k]
77  if c == "!" or c == " ":
78  if len(count) > 0:
79  res += "0" * _fromB64(count)
80  count = ""
81  res += c.replace("!", "1").replace(" ", "0")
82  else:
83  count += c
84  if len(count) > 0:
85  res += "0" * _fromB64(count)
86 
87  return res
88 
89 
90 if __name__ == "__main__":
91 
92  s = "000010000000000010000001000000000001000000000010000001"
93  c = compressB64(s)
95  x = int(s, 2)
96  sx = "{:0X}".format(x)
97 
98  print("")
99  print("Original: >>"+s+"<<")
100  print("Compressed: >>"+c+"<<")
101  print("Hex repr: >>"+sx+"<<")
102  print("Decompressed: >>"+d+"<<")
103 
104  import random
105  for i in range(0, 100):
106  # generate random bit string
107  s = ""
108  for j in range(0, 1000):
109  if random.random() > 0.02:
110  s += '0'
111  else:
112  s += '1'
113 
114  # test compression and decompression
115  c = compressB64(s)
116  d = decompressB64(c)
117  x = int(s, 2)
118  sx = "{:0X}".format(x)
119 
120  if i == 0:
121  print("")
122  print("Original: >>"+s+"<<")
123  print("Compressed: >>"+c+"<<")
124  print("Hex repr: >>"+sx+"<<")
125  print("Decompressed: >>"+d+"<<")
126 
127  if s != d:
128  print("")
129  print("Decompression error")
130  print("Original: >>"+s+"<<")
131  print("Compressed: >>"+c+"<<")
132  print("Hex repr: >>"+sx+"<<")
133  print("Decompressed: >>"+d+"<<")
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
python.compressB64._toB64
def _toB64(n)
Definition: compressB64.py:12
vtune_athena.format
format
Definition: vtune_athena.py:14
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.compressB64.decompressB64
def decompressB64(s)
Definition: compressB64.py:65
python.compressB64.compressB64
def compressB64(s)
Definition: compressB64.py:29
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
python.compressB64._fromB64
def _fromB64(s)
Definition: compressB64.py:21