ATLAS Offline Software
blobaccess.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <iostream>
8 #include "zlib.h"
9 #include "TMemFile.h"
10 #include "TTree.h"
11 #include "CoralBase/Blob.h"
12 #include "CxxUtils/base64.h"
13 
14 namespace CoralUtilities {
15 
16 
17 // Compression Functions
18 // -------------------------------------------------
19 
20 bool compressBlob(const char* in, coral::Blob& out) {
21  uLongf comprLen = compressBound(strlen(in));
23  blob.resize(comprLen);
24  unsigned char* ptr = static_cast<unsigned char*>(blob.startingAddress());
25  compress(ptr, &comprLen, reinterpret_cast<const unsigned char*>(in), strlen(in));
26  blob.resize(comprLen);
27  out = blob;
28  return true;
29 }
30 
31 bool writeBlobFromString(const std::string_view in, coral::Blob& out) {
32  return compressBlob(in.data(), out);
33 }
34 
36  return writeBlobFromString(in.dump(), out);
37 }
38 
40  TMemFile fout("buffer","recreate");
41  fout.cd();
42  tree->CloneTree(-1, "fast")->Write();
43  fout.Write();
44  uLongf comprLen = fout.GetSize();
45  unsigned char *buffer = new unsigned char[comprLen];
46  fout.CopyTo(buffer, comprLen);
48  blob.resize(comprLen);
49  char* ptr = reinterpret_cast<char*>(blob.startingAddress());
50  *ptr = *const_cast<char*>(CxxUtils::base64_encode(buffer, comprLen).c_str());
51  out = blob;
52  return true;
53 }
54 
55 
56 
57 
58 // Reading and Uncompressing Functions
59 // -------------------------------------------------
60 
61 bool uncompressBlob(const coral::Blob &blob, std::unique_ptr<unsigned char[]> & out, unsigned long& len){
62  uLongf uncompLen = 50000;
63  std::unique_ptr<unsigned char[]> uncompBuf(new unsigned char[uncompLen+1]);
64  uLongf actualLen{0};
65  while(true) {
66  actualLen = uncompLen;
67  int res(uncompress(uncompBuf.get(), &actualLen, reinterpret_cast<const unsigned char*>(blob.startingAddress()), static_cast<uLongf>(blob.size())));
68  if(res == Z_OK ) break;
69  if(res == Z_BUF_ERROR) { // double buffer if it was not big enough
70  uncompLen *= 2;
71  std::cout << "ATTENTION: Increasing buffer to " << uncompLen << std::endl;
72  uncompBuf.reset(new unsigned char[uncompLen+1]);
73  continue;
74  }
75  // something else is wrong
76  uncompBuf.reset();
77  return false;
78  }
79  uncompBuf.get()[actualLen]=0; // append 0 to terminate string
80  out = std::move(uncompBuf);
81  len = actualLen;
82  return true;
83 }
84 
85 bool readBlobAsString(const coral::Blob &blob, std::string& out){
86  std::unique_ptr<unsigned char[]> bf {};
87  uLongf len = 0;
88  if(!uncompressBlob(blob, bf, len)) return false;
89  const char* cstring = reinterpret_cast<const char*>(bf.get()); // need to cast to char*
90  out.assign(cstring, len); // copy over to C++ string
91  return true;
92 }
93 
95  std::string str = "";
96  if(!readBlobAsString(blob, str)) return false;
97  try {
99  }
100  catch(const std::exception& e) {
101  std::cout << "ATTENTION: Cannot unpack Blob object as JSON!" << std::endl;
102  return false;
103  }
104  return true;
105 }
106 
107 bool readBlobAsTTree(const coral::Blob &blob, std::unique_ptr<TTree>& out){
108  const char* cstring = reinterpret_cast<const char*>(blob.startingAddress());
109  std::string sb;
110  sb.assign(cstring, blob.size());
111  std::vector<unsigned char> bdata = CxxUtils::base64_decode(sb);
112  TMemFile f("buffer", reinterpret_cast<char*>(bdata.data()), bdata.size());
113  TTree* t = nullptr;
114  f.GetObject("tree",t);
115  if(!t) return false;
116  t->LoadBaskets();
117  t->SetDirectory(0);
118  f.Close();
119  out.reset(t);
120  return true;
121 }
122 
123 /*
124 // keep for now; msgpack maybe used later at some point
125 bool readBlobAsMsgpack(const coral::Blob &blob, std::string& out){
126  unsigned char* bf; uLongf len;
127  if(!uncompressBlob(blob, &bf, len)) return false;
128  const char* temp = reinterpret_cast<char*>(bf);
129  try {
130  auto upd = msgpack::unpack(temp, len);
131  msgpack::object obj = upd.get();
132  std::stringstream ss;
133  ss << obj;
134  out = ss.str();
135  }
136  catch(const std::exception& e) {
137  std::cout << "ATTENTION: Cannot unpack Blob object as Msgpack!" << std::endl;
138  return false;
139  }
140  return true;
141 }
142 */
143 
144 
145 
146 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
Amg::compress
void compress(const AmgSymMatrix(N) &covMatrix, std::vector< float > &vec)
Definition: EventPrimitivesHelpers.h:56
json
nlohmann::json json
Definition: HistogramDef.cxx:9
CoralUtilities::compressBlob
bool compressBlob(const char *, coral::Blob &)
Definition: blobaccess.cxx:20
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
tree
TChain * tree
Definition: tile_monitor.h:30
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
python.subdetectors.tile.Blob
Blob
Definition: tile.py:17
CoralUtilities::readBlobAsJson
bool readBlobAsJson(const coral::Blob &, nlohmann::json &)
Definition: blobaccess.cxx:94
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
CoralUtilities::writeBlobFromTTree
bool writeBlobFromTTree(TTree *, coral::Blob &)
Definition: blobaccess.cxx:39
CxxUtils::base64_decode
std::vector< unsigned char > base64_decode(const std::string &)
Definition: base64.cxx:97
CoralUtilities::readBlobAsString
bool readBlobAsString(const coral::Blob &, std::string &)
Definition: blobaccess.cxx:85
dqt_zlumi_alleff_HIST.fout
fout
Definition: dqt_zlumi_alleff_HIST.py:59
calibdata.exception
exception
Definition: calibdata.py:496
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
base64.h
CoralUtilities::uncompressBlob
bool uncompressBlob(const coral::Blob &, std::unique_ptr< unsigned char[]> &, unsigned long &)
Definition: blobaccess.cxx:61
hist_file_dump.f
f
Definition: hist_file_dump.py:135
CoralUtilities::readBlobAsTTree
bool readBlobAsTTree(const coral::Blob &, std::unique_ptr< TTree > &)
Definition: blobaccess.cxx:107
keylayer_zslicemap.sb
sb
Definition: keylayer_zslicemap.py:192
CxxUtils::base64_encode
std::string base64_encode(const unsigned char *, unsigned int)
Definition: base64.cxx:55
blobaccess.h
str
Definition: BTagTrackIpAccessor.cxx:11
CoralUtilities::writeBlobFromJson
bool writeBlobFromJson(const nlohmann::json &, coral::Blob &)
Definition: blobaccess.cxx:35
CoralUtilities
Definition: blobaccess.h:19
CoralUtilities::writeBlobFromString
bool writeBlobFromString(const std::string_view, coral::Blob &)
Definition: blobaccess.cxx:31
CaloCondBlobAlgs_fillNoiseFromASCII.blob
blob
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:96