ATLAS Offline Software
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
IOVDbNamespace::Json2Cool Class Reference

Produces cool objects from their JSON representation, storing them in a 'BasicFolder'. More...

#include <Json2Cool.h>

Collaboration diagram for IOVDbNamespace::Json2Cool:

Public Member Functions

 Json2Cool (std::istream &stream, BasicFolder &b, const std::string &specString, const IovStore::Iov_t *iov=nullptr)
 
 ~Json2Cool ()=default
 

Static Public Member Functions

static cool::Record createAttributeList (cool::RecordSpecification *pSpec, const nlohmann::json &j)
 
static cool::RecordSpecification * parsePayloadSpec (const std::string &stringSpecification)
 

Private Member Functions

void init (std::istream &stream, const std::string &specString, const IovStore::Iov_t *iov)
 

Private Attributes

cool::RecordSpecification * m_sharedSpec = nullptr
 
BasicFolderm_basicFolder
 

Detailed Description

Produces cool objects from their JSON representation, storing them in a 'BasicFolder'.

Definition at line 28 of file Json2Cool.h.

Constructor & Destructor Documentation

◆ Json2Cool()

IOVDbNamespace::Json2Cool::Json2Cool ( std::istream &  stream,
BasicFolder b,
const std::string &  specString,
const IovStore::Iov_t iov = nullptr 
)

Definition at line 55 of file Json2Cool.cxx.

55  :m_basicFolder(b){
56  init(stream, specString, iov);
57  }

◆ ~Json2Cool()

IOVDbNamespace::Json2Cool::~Json2Cool ( )
default

Member Function Documentation

◆ createAttributeList()

cool::Record IOVDbNamespace::Json2Cool::createAttributeList ( cool::RecordSpecification *  pSpec,
const nlohmann::json j 
)
static

Definition at line 150 of file Json2Cool.cxx.

150  {
151  cool::Record a(*pSpec);
152  unsigned int s=a.size();
153 
154  json::const_iterator it = j.begin();
155  for (unsigned int i(0);i!=s;++i){
156  auto & f=a[i];
157  if (it == j.end()){
158  continue;
159  }
160  const auto thisVal = it.value();
161  ++it;
162 
163  try{
164  // cool::Record does not provide non-const access to AttributeList.
165  // But this is safe because we are filling a local instance.
166  auto & att ATLAS_THREAD_SAFE = const_cast<coral::Attribute&>(a.attributeList()[i]);
167  if (thisVal.is_null()){
168  att.setNull();
169  continue;
170  }
171  cool::StorageType::TypeId typespec = f.storageType().id();
172  std::string strVal = to_string(thisVal);
173  if(strVal.size()>2&& strVal[0]=='"'&& strVal[strVal.size()-1]=='"')
174  strVal=strVal.substr(1,strVal.size()-2);
175 
176  if((strVal.compare("NULL")==0||strVal.compare("null")==0)&&
177  (typespec==StorageType::Bool || typespec==StorageType::Int16 || typespec==StorageType::UInt16
178  || typespec==StorageType::Int32 || typespec==StorageType::UInt32
179  || typespec==StorageType::Int64 || typespec==StorageType::UInt63
180  || typespec==StorageType::Float || typespec==StorageType::Double)){
181  att.setNull();
182  continue;
183  }
184  switch (typespec) {
185  case StorageType::Bool:
186  {
187  const bool newVal=(strVal == "true");
188  att.setValue<bool>(newVal);
189  break;
190  }
191  case StorageType::UChar:
192  {
193  const unsigned char newVal=std::stoul(strVal);
194  att.setValue<unsigned char>(newVal);
195  break;
196  }
197  case StorageType::Int16:
198  {
199  const short newVal=std::stol(strVal);
200  att.setValue<short>(newVal);
201  break;
202  }
203  case StorageType::UInt16:
204  {
205  const unsigned short newVal=std::stoul(strVal);
206  att.setValue<unsigned short>(newVal);
207  break;
208  }
209  case StorageType::Int32:
210  {
211  const int newVal=std::stoi(strVal);
212  att.setValue<int>(newVal);
213  break;
214  }
215  case StorageType::UInt32:
216  {
217  const unsigned int newVal=std::stoull(strVal);
218  att.setValue<unsigned int>(newVal);
219  break;
220  }
221  case StorageType::UInt63:
222  {
223  const unsigned long long newVal=std::stoull(strVal);
224  att.setValue<unsigned long long>(newVal);
225  break;
226  }
227  case StorageType::Int64:
228  {
229  const long long newVal=std::stoll(strVal);
230  att.setValue< long long>(newVal);
231  break;
232  }
233  case StorageType::Float:
234  {
235  const float newVal=std::stof(strVal);
236  att.setValue<float>(newVal);
237  break;
238  }
239  case StorageType::Double:
240  {
241  const double newVal=std::stod(strVal);
242  att.setValue<double>(newVal);
243  break;
244  }
245  case StorageType::String255:
246  case StorageType::String4k:
247  case StorageType::String64k:
248  case StorageType::String16M:
249  case StorageType::String128M:
250  {
251  att.setValue<std::string>(thisVal.get<std::string>());
252  break;
253  }
254  case StorageType::Blob128M:
255  case StorageType::Blob16M:
256  case StorageType::Blob64k:
257  {
258  auto blob = base64Decode(strVal);
259  att.setValue<coral::Blob>(blob);
260  break;
261  }
262  default:
263  {
264  std::string typeName{};
265  for (auto& [key,val] : typeCorrespondance) {
266  if(val==typespec) {
267  typeName = key;
268  break;
269  }
270  }
271  if(typeName.empty()) {
272  typeName = "Unexpected Type";
273  }
274  std::string errorMessage("UNTREATED TYPE! " + typeName);
275  std::cerr << errorMessage << std::endl;
276  throw std::runtime_error(errorMessage);
277  break;
278  }
279  }
280  }
281  catch (json::exception& e){
282  std::cerr << e.what() << std::endl;
283  throw std::runtime_error(e.what());
284  }
285  }
286  return a;
287  }

◆ init()

void IOVDbNamespace::Json2Cool::init ( std::istream &  stream,
const std::string &  specString,
const IovStore::Iov_t iov 
)
private

Definition at line 60 of file Json2Cool.cxx.

60  {
61  if (not s.good() or s.eof()){
62  const std::string msg("Json2Cool constructor; Input is invalid and could not be opened.");
63  throw std::runtime_error(msg);
64  } else {
65  json j;
66  try{
67  s>>j; //read into json
68  }catch (const std::exception& e) {
69  std::cout<<"ERROR AT LINE "<<__LINE__<<" of "<<__FILE__<<std::endl;
70  std::cout<<e.what()<<std::endl; //typically a parsing error
71  }
72  m_sharedSpec = parsePayloadSpec(specString);
73  const auto & payload=(j.contains("data")) ? j["data"] : j;
74  if(iov) {
76  }
77  else {
78  m_basicFolder.setIov(IovStore::Iov_t(0, cool::ValidityKeyMax));
79  }
81  for (json::const_iterator k=payload.begin();k!=payload.end();++k){ //k are {"0":}
82  const json& f=k.value(); //channel id
83  const std::string& ks=k.key();
84  const long long key=std::stoll(ks);
85  std::vector<coral::AttributeList> tempVector;//can optimise this by pre constructing it and using 'clear'
86  for (json::const_iterator i=f.begin();i!=f.end();++i){
87  const json& arrayElem=i.value();
88  auto r=createAttributeList(m_sharedSpec,arrayElem);
89  const auto & attList=r.attributeList();
90  tempVector.push_back(attList);
91  }
92  m_basicFolder.addChannelPayload(key, tempVector);
93  //add payload with channelId here
94  }
95  } else {
96  for (json::const_iterator i=payload.begin();i!=payload.end();++i){
97  const json& f=i.value();
98  const std::string& ks=i.key();
99  const long long key=std::stoll(ks);
101  const auto & attList=r.attributeList();
103  }
104  }
105  }
106  }

◆ parsePayloadSpec()

cool::RecordSpecification * IOVDbNamespace::Json2Cool::parsePayloadSpec ( const std::string &  stringSpecification)
static

Definition at line 111 of file Json2Cool.cxx.

111  {
112  if (stringSpecification.empty()) return nullptr;
113  auto *spec = new cool::RecordSpecification();
114 
115  std::string inputObj=stringSpecification;
116  nlohmann::json nl=nlohmann::json::parse(stringSpecification);
117  if(nl.is_array()){
118  for (unsigned int i = 0; i < nl.size(); i++)
119  {
120  if(nl[i].size()>0){
121  auto it = nl[i].items().begin();
122  std::string n((*it).key());
123  std::string t((*it).value());
124  spec->extend(n, typeCorrespondance.find(t)->second);
125  }
126  }
127  return spec;
128  }
129 
130  std::string input(stringSpecification);
131 
132  std::string regex=R"delim(([^\s,:]*):\s?([^\s,]*),?)delim";
134  boost::smatch what;
135 
136  bool match=boost::regex_search(input, what, expression);
137  while (match){
138  std::string n(what[1]);
139  std::string t(what[2]);
140  //todo: need to catch error if type not found, also
141  spec->extend(n, typeCorrespondance.find(t)->second);
142  input = what.suffix();
143  match=boost::regex_search(input, what, expression);
144 
145  }
146  return spec;
147  }

Member Data Documentation

◆ m_basicFolder

BasicFolder& IOVDbNamespace::Json2Cool::m_basicFolder
private

Definition at line 43 of file Json2Cool.h.

◆ m_sharedSpec

cool::RecordSpecification* IOVDbNamespace::Json2Cool::m_sharedSpec = nullptr
private

Definition at line 42 of file Json2Cool.h.


The documentation for this class was generated from the following files:
IOVDbNamespace::BasicFolder::isVectorPayload
bool isVectorPayload() const
Definition: BasicFolder.cxx:41
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
beamspotman.r
def r
Definition: beamspotman.py:676
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
dqt_zlumi_alleff_HIST.iov
iov
Definition: dqt_zlumi_alleff_HIST.py:119
json
nlohmann::json json
Definition: HistogramDef.cxx:9
CaloCondBlobAlgs_fillNoiseFromASCII.spec
spec
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:47
IOVDbNamespace::Json2Cool::m_basicFolder
BasicFolder & m_basicFolder
Definition: Json2Cool.h:43
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1082
skel.it
it
Definition: skel.GENtoEVGEN.py:396
python.subdetectors.tile.Blob
Blob
Definition: tile.py:17
IOVDbNamespace::base64Decode
coral::Blob base64Decode(const std::string &base64String)
Definition: Base64Codec.cxx:26
python.HION12.expression
string expression
Definition: HION12.py:55
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
IOVDbNamespace::IovStore::Iov_t
std::pair< cool::ValidityKey, cool::ValidityKey > Iov_t
Definition: IovStore.h:22
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
calibdata.exception
exception
Definition: calibdata.py:496
hist_file_dump.f
f
Definition: hist_file_dump.py:135
IOVDbNamespace::Json2Cool::parsePayloadSpec
static cool::RecordSpecification * parsePayloadSpec(const std::string &stringSpecification)
Definition: Json2Cool.cxx:111
python.ExitCodes.what
def what(code)
Definition: ExitCodes.py:73
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
IOVDbNamespace::Json2Cool::m_sharedSpec
cool::RecordSpecification * m_sharedSpec
Definition: Json2Cool.h:42
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
IOVDbNamespace::BasicFolder::addChannelPayload
void addChannelPayload(const cool::ChannelId &channelId, const std::string &name, const coral::AttributeList &payload)
Definition: BasicFolder.cxx:52
a
TList * a
Definition: liststreamerinfos.cxx:10
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
ReadCalibFromCool.typeName
typeName
Definition: ReadCalibFromCool.py:477
json
nlohmann::json json
Definition: Json2Cool.cxx:18
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
IOVDbNamespace::Json2Cool::createAttributeList
static cool::Record createAttributeList(cool::RecordSpecification *pSpec, const nlohmann::json &j)
Definition: Json2Cool.cxx:150
IOVDbNamespace::Json2Cool::init
void init(std::istream &stream, const std::string &specString, const IovStore::Iov_t *iov)
Definition: Json2Cool.cxx:60
Crest::TypeId
TypeId
Definition: CrestContainer.h:24
IOVDbNamespace::BasicFolder::setIov
void setIov(const std::pair< cool::ValidityKey, cool::ValidityKey > &iov)
Definition: BasicFolder.cxx:46
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
CaloCondBlobAlgs_fillNoiseFromASCII.blob
blob
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:96
match
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition: hcg.cxx:356
fitman.k
k
Definition: fitman.py:528
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37