ATLAS Offline Software
CrestContainer.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2019-2024 CERN for the benefit of the ATLAS collaboration
3  */
4 
13 #ifndef CREST_COND_CONTAINER_H
14 #define CREST_COND_CONTAINER_H
15 #include <vector>
16 #include <string>
17 #include <iostream>
18 #include <sstream>
19 #include <utility>
20 #include <map>
21 #include "nlohmann/json.hpp"
22 
23 namespace Crest {
24  enum class TypeId {
29  };
30  const static std::map<TypeId, std::string> s_typeToString = {
31  { TypeId::Bool, "Bool" },
32  { TypeId::UChar, "UChar" },
33  { TypeId::Int16, "Int16" },
34  { TypeId::UInt16, "UInt16" },
35  { TypeId::Int32, "Int32" },
36  { TypeId::UInt32, "UInt32" },
37  { TypeId::UInt63, "UInt63" },
38  { TypeId::Int64, "Int64" },
39  { TypeId::Float, "Float" },
40  { TypeId::Double, "Double" },
41  { TypeId::String255, "String255" },
42  { TypeId::String4k, "String4k" },
43  { TypeId::String64k, "String64k" },
44  { TypeId::String16M, "String16M" },
45  { TypeId::String128M,"String128M"},
46  { TypeId::Blob64k, "Blob64k" },
47  { TypeId::Blob16M, "Blob16M" },
48  { TypeId::Blob128M, "Blob128M" }
49  };
50 
51  enum class ModeId{
53  };
55  private:
56  std::vector<std::pair<std::string,TypeId>> m_payload_spec;
63  bool m_isVectorPayload=false;
64 
65  void validatePayloadSize(const nlohmann::json &data) const;
66  nlohmann::json createRowArray(const nlohmann::json& data_row) const;
67  const nlohmann::json& getRow();
68 
69  public:
80  void addColumn(const std::string& name, TypeId type);
81  void addColumn(const std::string& name, const char* type);
82  void addColumn(const std::string& name, uint32_t type);
83 
87  bool isVectorPayload();
89 
90  Crest::TypeId stringToTypeId(const std::string& type) const;
91  Crest::TypeId intToTypeId(int value) const;
92 
98  void putRow2Vector();
99 
106  void addNullRecord(const char* name);
107 
116  void addRecord(const char* name,int number, ...);
123  void addData(const char* channel_id);
132  void addExternalData(const char* channel_id, const nlohmann::json& data);
139  void addIov(const uint64_t since);
140 
141  const std::vector<std::pair<std::string,TypeId>>& getMPayloadSpec();
142 
143  template<typename T> T getRecord(const std::string& name)
144  {
145  for (const auto& column : m_payload_spec) {
146  if (column.first == name) {
147  try {
148  return m_row.at(name).get<T>();
149  } catch (nlohmann::json::exception& e) {
150  throw std::runtime_error("JSON exception for key: " + name);
151  }
152  }
153  }
154  throw std::runtime_error("Column name not found or type mismatch.");
155  }
156 
157  const nlohmann::json& getPayload();
158  const nlohmann::json& getIovData();
159 
160  const nlohmann::json& getPayloadChannel(const char* channel_id);
161 
162 
163  void setIovData(const nlohmann::json& j);
164  void setPayload(const nlohmann::json& j);
165  // return the payload spec as a copy
167  void setPayloadSpec(const nlohmann::json& j);
168 
172  static std::string base64_encode(const uint8_t* bytes_to_encode, unsigned int in_len);
176  static std::vector<unsigned char> base64_decode(const std::string& encodedData);
182  int getColumnIndex(const std::string& name);
186  void flush();
187  void clear();
188  void selectIov(const uint64_t since);
189  std::vector<std::string> channelIds();
193  std::string getJsonPayload();
197  std::string getJsonIovData();
201  void dump_json_to_file(const nlohmann::json& j, const std::string& filename);
205  nlohmann::json read_json_from_file(const std::string& filename, const std::string& spec_filename);
209  void from_json(const uint64_t since, const nlohmann::json& j);
210 
211  void parseOldFormat(std::string& colName, TypeId& typespec,const nlohmann::json & j);
212  };
213 }
214 #endif
Crest::CrestContainer::stringToTypeId
Crest::TypeId stringToTypeId(const std::string &type) const
Definition: CrestContainer.cxx:32
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Crest::CrestContainer::addData
void addData(const char *channel_id)
It associate the payload row to a channel_id.
Definition: CrestContainer.cxx:160
Crest::CrestContainer::setPayloadSpec
void setPayloadSpec(const nlohmann::json &j)
Definition: CrestContainer.cxx:365
Crest::CrestContainer::addColumn
void addColumn(const std::string &name, TypeId type)
It adds a column to the payload specification.
Definition: CrestContainer.cxx:79
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:557
Crest::CrestContainer::parseOldFormat
void parseOldFormat(std::string &colName, TypeId &typespec, const nlohmann::json &j)
Definition: CrestContainer.cxx:449
Crest::TypeId::UInt16
@ UInt16
json
nlohmann::json json
Definition: HistogramDef.cxx:9
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
Crest::TypeId::Blob128M
@ Blob128M
Crest::CrestContainer::m_payload
nlohmann::json m_payload
Definition: CrestContainer.h:57
Crest::CrestContainer::m_full_data
nlohmann::json m_full_data
Definition: CrestContainer.h:61
Crest::CrestContainer::getIovData
const nlohmann::json & getIovData()
Definition: CrestContainer.cxx:318
Crest::CrestContainer::setVectorPayload
void setVectorPayload(bool isVectorPayload)
Definition: CrestContainer.cxx:27
Crest::CrestContainer::getRow
const nlohmann::json & getRow()
Definition: CrestContainer.cxx:296
Crest::TypeId::UInt63
@ UInt63
DeMoUpdate.column
dictionary column
Definition: DeMoUpdate.py:1110
Crest::CrestContainer::getRecord
T getRecord(const std::string &name)
Definition: CrestContainer.h:143
Crest::CrestContainer::getPayload
const nlohmann::json & getPayload()
Definition: CrestContainer.cxx:304
Crest::CrestContainer::isVectorPayload
bool isVectorPayload()
It sets the mode of the container.
Definition: CrestContainer.cxx:23
athena.value
value
Definition: athena.py:124
Crest::ModeId::DCS_FULL
@ DCS_FULL
Crest::CrestContainer::CrestContainer
CrestContainer()
Definition: CrestContainer.cxx:17
Crest::CrestContainer::getJsonPayload
std::string getJsonPayload()
It returns the json representation of the container.
Definition: CrestContainer.cxx:338
Crest::CrestContainer::flush
void flush()
It reinitializes the containers.
Definition: CrestContainer.cxx:386
Crest::CrestContainer::dump_json_to_file
void dump_json_to_file(const nlohmann::json &j, const std::string &filename)
It creates a file with the json representation of the container.
Definition: CrestContainer.cxx:403
Crest::CrestContainer::base64_decode
static std::vector< unsigned char > base64_decode(const std::string &encodedData)
It decodes the input string from base64.
Definition: CrestContainer.cxx:65
Crest
Definition: CrestApi.h:30
Crest::CrestContainer::setIovData
void setIovData(const nlohmann::json &j)
Definition: CrestContainer.cxx:328
Crest::TypeId::String16M
@ String16M
Crest::CrestContainer::getColumnIndex
int getColumnIndex(const std::string &name)
It returns the index of the column with the given name.
Definition: CrestContainer.cxx:266
Crest::CrestContainer::m_vector_data
nlohmann::json m_vector_data
Definition: CrestContainer.h:60
Crest::CrestContainer::m_iov_data
nlohmann::json m_iov_data
Definition: CrestContainer.h:59
dq_defect_copy_defect_database.since
def since
Definition: dq_defect_copy_defect_database.py:54
Crest::CrestContainer::channelIds
std::vector< std::string > channelIds()
Definition: CrestContainer.cxx:227
Crest::TypeId::String255
@ String255
Crest::CrestContainer::getMPayloadSpec
const std::vector< std::pair< std::string, TypeId > > & getMPayloadSpec()
Definition: CrestContainer.cxx:234
Crest::CrestContainer::addNullRecord
void addNullRecord(const char *name)
It adds a null value to the payload.
Definition: CrestContainer.cxx:94
Crest::TypeId::Blob64k
@ Blob64k
Crest::TypeId::Float
@ Float
Crest::ModeId
ModeId
Definition: CrestContainer.h:51
Crest::CrestContainer::~CrestContainer
~CrestContainer()
Definition: CrestContainer.cxx:18
Crest::TypeId::Bool
@ Bool
Crest::CrestContainer::clear
void clear()
Definition: CrestContainer.cxx:394
Crest::TypeId::Blob16M
@ Blob16M
Crest::CrestContainer::m_modeId
ModeId m_modeId
Definition: CrestContainer.h:62
Crest::TypeId::UChar
@ UChar
Crest::TypeId::UInt32
@ UInt32
calibdata.exception
exception
Definition: calibdata.py:496
createCoolChannelIdFile.channel_id
channel_id
Definition: createCoolChannelIdFile.py:52
Crest::CrestContainer::read_json_from_file
nlohmann::json read_json_from_file(const std::string &filename, const std::string &spec_filename)
It reads a json file and returns the json object.
Definition: CrestContainer.cxx:418
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
Crest::CrestContainer::base64_encode
static std::string base64_encode(const uint8_t *bytes_to_encode, unsigned int in_len)
It encodes the input string to base64.
Definition: CrestContainer.cxx:50
Crest::TypeId::Int32
@ Int32
Crest::CrestContainer::intToTypeId
Crest::TypeId intToTypeId(int value) const
Definition: CrestContainer.cxx:42
Crest::CrestContainer::getPayloadSpec
nlohmann::json getPayloadSpec()
Definition: CrestContainer.cxx:348
Crest::CrestContainer::addRecord
void addRecord(const char *name, int number,...)
It adds a record to the payload.
Definition: CrestContainer.cxx:106
Crest::CrestContainer::from_json
void from_json(const uint64_t since, const nlohmann::json &j)
It reads a json object to fill the containers.
Definition: CrestContainer.cxx:547
Crest::TypeId::String128M
@ String128M
lumiFormat.array
array
Definition: lumiFormat.py:91
python.selection.number
number
Definition: selection.py:20
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
Crest::CrestContainer::validatePayloadSize
void validatePayloadSize(const nlohmann::json &data) const
Definition: CrestContainer.cxx:189
Crest::CrestContainer
Definition: CrestContainer.h:54
Crest::CrestContainer::addExternalData
void addExternalData(const char *channel_id, const nlohmann::json &data)
It associate the payload row to a channel_id.
Definition: CrestContainer.cxx:165
Crest::ModeId::Standard
@ Standard
Crest::TypeId::Int64
@ Int64
Crest::CrestContainer::selectIov
void selectIov(const uint64_t since)
Definition: CrestContainer.cxx:222
Crest::TypeId::String64k
@ String64k
Crest::CrestContainer::setPayload
void setPayload(const nlohmann::json &j)
Definition: CrestContainer.cxx:333
Crest::TypeId::TypeIdCount
@ TypeIdCount
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Crest::CrestContainer::getJsonIovData
std::string getJsonIovData()
It returns the json representation of the container.
Definition: CrestContainer.cxx:343
Crest::CrestContainer::m_payload_spec
std::vector< std::pair< std::string, TypeId > > m_payload_spec
Definition: CrestContainer.h:56
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
TypeId
Definition: IdDictMgr.cxx:29
Crest::CrestContainer::createRowArray
nlohmann::json createRowArray(const nlohmann::json &data_row) const
Definition: CrestContainer.cxx:199
Crest::ModeId::DCS
@ DCS
Crest::TypeId::String4k
@ String4k
Crest::TypeId::Int16
@ Int16
Crest::CrestContainer::addIov
void addIov(const uint64_t since)
It adds an IOV to the json object m_iov_data.
Definition: CrestContainer.cxx:214
Crest::TypeId
TypeId
Definition: CrestContainer.h:24
Crest::CrestContainer::getPayloadChannel
const nlohmann::json & getPayloadChannel(const char *channel_id)
Definition: CrestContainer.cxx:239
Crest::CrestContainer::m_isVectorPayload
bool m_isVectorPayload
Definition: CrestContainer.h:63
Crest::CrestContainer::m_row
nlohmann::json m_row
Definition: CrestContainer.h:58
Crest::TypeId::Double
@ Double
Crest::CrestContainer::putRow2Vector
void putRow2Vector()
It adds row data to vector.
Definition: CrestContainer.cxx:282