ATLAS Offline Software
Cool2Json.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "Cool2Json.h"
5 #include "CoolKernel/IFolderSpecification.h"
6 
7 #include "CoolKernel/IObject.h"
8 #include "CoolKernel/IObjectIterator.h"
9 #include "CoolKernel/IRecord.h"
10 #include "CoolKernel/IRecordIterator.h"
11 #include "CoralBase/AttributeList.h"
12 #include "CoralBase/AttributeListSpecification.h"
13 #include "CoralBase/Attribute.h"
14 #include "CoralBase/AttributeSpecification.h"
15 #include "CoralBase/Blob.h"
16 #include "TStopwatch.h"
17 
22 
30 #include "FolderTypes.h"
31 #include "IOVDbStringFunctions.h"
32 #include "Base64Codec.h"
33 #include <regex>
34 #include <nlohmann/json.hpp>
36 namespace {
37  std::string
38  spec2String(const cool::IFolderPtr & pFolder){
39  //open bracket, close bracket for json objects
40  const std::string ob="";
41  const std::string objName="\"folder_payloadspec\": \"";
42  std::string result=ob+objName;
43  const auto & rspec = pFolder->payloadSpecification();
44  std::string sep{""};
45  json chJson = json::array();
46  for (unsigned int i(0); i<rspec.size();++i){
47  const auto & f = rspec[i];
48  json obj = {};
49  obj[f.name()] = f.storageType().name();
50  chJson.push_back(obj);
51  }
52  result+=chJson.dump();
53  result+='\"';
54  return result;
55  }
56 }
57 
58 namespace IOVDbNamespace {
59  Cool2Json::Cool2Json(const cool::IFolderPtr & pFolder,
60  const cool::ValidityKey & since,
61  const cool::ValidityKey & until,
62  const cool::ChannelSelection & chansel,
63  const std::string& folderTag):
64  m_pFolder(pFolder),
65  m_start(since),
66  m_stop(until),
67  m_chansel(chansel),
68  m_tag(folderTag),
69  m_desc(pFolder->description()),
70  m_spec(spec2String(pFolder)),
71  m_nchans(0){
72  const auto & channums=pFolder->listChannels();
73  //find channels in the chansel which are chan nums
74  //theres no 'size' method in the ChannelSelection class
75  for (const auto &i:channums){
76  m_nchans+=m_chansel.inSelection(i);//bool casts to 0 or 1
77  }
78  }
79 
80  std::string
82  std::string saneXml=sanitiseXml(m_desc);
83  saneXml = std::regex_replace(saneXml, std::regex("\\\\\""), "\"");
84  std::string out = "\"node_description\" : \"";
85  out += saneXml;
86  out += '\"';
87  return out;
88  }
89 
90  const std::string&
92  return m_spec;
93  }
94 
95 
96  std::string
98  std::string result("\"data_array\" : [");
99  cool::IObjectIteratorPtr itr=m_pFolder->browseObjects(m_start,m_stop,m_chansel,m_tag);
101  std::string sep="";
102  while (itr->goToNext()){
103  const cool::IObject& ref=itr->currentRef();
104  result+=sep;
105  const long long cId=ref.channelId();
106  result+=s_openJson+quote(std::to_string(cId))+" : ";
107  switch (ftype){
109  result+=formatCvp(itr);
110  break;
112  result +=formatAttrList(itr);
113  break;
115  result +=formatAttrList(itr);
116  break;
118  result +=formatPoolRef(itr);
119  break;
121  result += formatAttrList(itr);
122  break;
124  result += " CoraCool";
125  break;
126  default:
127  result+=" a_data_value";
128  }
129  if (sep.empty()) sep=",";
130  result+=s_closeJson;
131  }
132  result+=']';
133  itr->close();
134  return result;
135  }
136 
137  unsigned int
139  return m_nchans;
140  }
141 
142  std::string
144  std::string result("");
145  //run-lumi and run-event can both be found in our database, but the meaning is run-lumi
146  if (m_desc.find("<timeStamp>run-lumi</timeStamp>") != std::string::npos) result = "run-lumi";
147  if (m_desc.find("<timeStamp>run-event</timeStamp>") != std::string::npos) result = "run-lumi";
148  if (m_desc.find("<timeStamp>time</timeStamp>") != std::string::npos) result = "time";
149  return result;
150  }
151 
152  const std::string&
153  Cool2Json::tag() const{
154  return m_tag;
155  }
156 
157  std::string
158  Cool2Json::formatCvp(const cool::IObjectIteratorPtr & itr){
159  std::string os;
160  const cool::IObject& ref=itr->currentRef();
161  cool::IRecordIterator& pitr=ref.payloadIterator();
162  auto pvec=pitr.fetchAllAsVector();
163  std::string sep="";
164  os+='[';//vector of vectors
165  for (const auto & vitr:*pvec){
166  os+=sep;
167  const coral::AttributeList& atrlist=(vitr)->attributeList();
169  if (sep.empty()) sep =IOVDbNamespace::s_delimiterJson;
170  }
171  os+=']';
172  return os;
173  }
174 
175  std::string
176  Cool2Json::formatAttrList(const cool::IObjectIteratorPtr & itr){
177  const cool::IObject& ref=itr->currentRef();
178  const coral::AttributeList& atrlist=ref.payload().attributeList();
179  std::string sep="";
180  return IOVDbNamespace::jsonAttributeList(atrlist);
181  }
182 
183  std::string
184  Cool2Json::formatPoolRef(const cool::IObjectIteratorPtr & itr){
185  const cool::IObject& ref=itr->currentRef();
186  const coral::AttributeList& atrlist=ref.payload().attributeList();
187  std::ostringstream os;
188  atrlist[0].toOutputStream(os);
189  auto res=os.str();
190  const std::string sep(" : ");
191  const auto separatorPosition = res.find(sep);
192  const std::string payloadOnly=res.substr(separatorPosition+3);
193  return quote(payloadOnly);
194  }
195 
196  std::string
197  Cool2Json::iov() const{
198  std::string iovString("\"iov\" : ");
199  return iovString+"["+std::to_string(m_start)+", "+std::to_string(m_stop)+"]";
200  }
201 
202  std::ostream &operator<<(std::ostream &o, const Cool2Json &c){
203  o << c.description() << std::endl;
204  return o;
205  }
206 
207 }
IOVDbNamespace::Cool2Json::m_nchans
unsigned int m_nchans
Definition: Cool2Json.h:87
IOVDbNamespace::Cool2Json
Definition: Cool2Json.h:26
get_generator_info.result
result
Definition: get_generator_info.py:21
CondAttrListCollection.h
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
CondAttrListCollAddress.h
This file contains the class definition for the CondAttrListCollAddress class.
json
nlohmann::json json
Definition: HistogramDef.cxx:9
IOVDbNamespace::Cool2Json::formatCvp
static std::string formatCvp(const cool::IObjectIteratorPtr &itr)
Definition: Cool2Json.cxx:158
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:70
IOVDbNamespace::PoolRefColl
@ PoolRefColl
Definition: FolderTypes.h:31
IOVDbNamespace::PoolRef
@ PoolRef
Definition: FolderTypes.h:30
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
IOVDbNamespace::operator<<
std::ostream & operator<<(std::ostream &o, const Cool2Json &c)
Definition: Cool2Json.cxx:202
AthenaAttributeList.h
IOVDbNamespace::Cool2Json::m_desc
const std::string m_desc
Definition: Cool2Json.h:85
IOVDbNamespace::Cool2Json::m_stop
const cool::ValidityKey m_stop
Definition: Cool2Json.h:82
IOVDbNamespace::Cool2Json::Cool2Json
Cool2Json(const cool::IFolderPtr &pFolder, const cool::ValidityKey &since, const cool::ValidityKey &until, const cool::ChannelSelection &m_chansel, const std::string &folderTag)
Definition: Cool2Json.cxx:59
CoraCoolObject.h
dq_defect_copy_defect_database.since
def since
Definition: dq_defect_copy_defect_database.py:54
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
dq_defect_copy_defect_database.until
def until
Definition: dq_defect_copy_defect_database.py:55
IOVDbStringFunctions.h
IOVDbJsonStringFunctions.h
IOVDbNamespace::Cool2Json::m_pFolder
const cool::IFolderPtr m_pFolder
Definition: Cool2Json.h:80
CondAttrListVec.h
A CondAttrListVec is an Athena DataObject holding a vector of CORAL AttributeLists,...
lumiFormat.i
int i
Definition: lumiFormat.py:85
Cool2Json.h
AthenaAttrListAddress.h
This file contains the class definition for theAthenaAttrListAddress class.
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:11
IOVDbNamespace::Cool2Json::m_start
const cool::ValidityKey m_start
Definition: Cool2Json.h:81
IOVDbNamespace::CoolVector
@ CoolVector
Definition: FolderTypes.h:33
IOVDbNamespace::CoraCool
@ CoraCool
Definition: FolderTypes.h:32
hist_file_dump.f
f
Definition: hist_file_dump.py:140
IOVDbNamespace::AttrListColl
@ AttrListColl
Definition: FolderTypes.h:29
m_start
std::chrono::time_point< std::chrono::high_resolution_clock > m_start
Definition: ColumnarPhysliteTest.cxx:64
IOVDbNamespace::Cool2Json::payload
std::string payload()
Payload (data for the given channel selection and iov)
Definition: Cool2Json.cxx:97
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
python.changerun.m_stop
m_stop
Definition: changerun.py:83
lumiFormat.array
array
Definition: lumiFormat.py:91
CoraCoolFolder.h
grepfile.sep
sep
Definition: grepfile.py:38
Base64Codec.h
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
IOVDbNamespace::Cool2Json::m_spec
const std::string m_spec
Definition: Cool2Json.h:86
IOVDbNamespace::jsonAttributeList
std::string jsonAttributeList(const coral::AttributeList &atrlist)
Produce a representation of a coral::AttributeList as a json string.
Definition: IOVDbJsonStringFunctions.cxx:40
IOVDbNamespace::Cool2Json::m_tag
const std::string m_tag
Definition: Cool2Json.h:84
IOVDbNamespace::Cool2Json::payloadSpec
const std::string & payloadSpec() const
Payload specification for this folder.
Definition: Cool2Json.cxx:91
CaloCondBlobAlgs_fillNoiseFromASCII.folderTag
folderTag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:51
ref
const boost::regex ref(r_ef)
IOVDbNamespace::Cool2Json::formatPoolRef
static std::string formatPoolRef(const cool::IObjectIteratorPtr &itr)
Definition: Cool2Json.cxx:184
IOVDbNamespace::Cool2Json::formatAttrList
static std::string formatAttrList(const cool::IObjectIteratorPtr &itr)
Definition: Cool2Json.cxx:176
IOVDbNamespace::AttrList
@ AttrList
Definition: FolderTypes.h:28
FolderTypes.h
CoraCoolDatabase.h
IOVDbNamespace::Cool2Json::tag
const std::string & tag() const
Simply the tag string passed to the constructor.
Definition: Cool2Json.cxx:153
CoraCoolObjectIter.h
pvec
std::array< fp_t, 2 > pvec
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:9
IOVDbNamespace::Cool2Json::iov
std::string iov() const
Formatted iov as '[<since>, <until>]'.
Definition: Cool2Json.cxx:197
IOVDbNamespace::Cool2Json::description
std::string description() const
Folder description string.
Definition: Cool2Json.cxx:81
IOVDbNamespace::Cool2Json::iovBase
std::string iovBase() const
'time' (ns of epoch) or 'run-lumi'
Definition: Cool2Json.cxx:143
IOVDbNamespace::determineFolderType
FolderType determineFolderType(const std::string &folderDescription, const std::string &spec, const std::vector< cool::ChannelId > &chans)
Determine folder type with optional check using clid service to check clid matches typename.
Definition: FolderTypes.cxx:17
IOVDbNamespace::Cool2Json::nchans
unsigned int nchans() const
Number of channels in the folder.
Definition: Cool2Json.cxx:138
IOVDbNamespace::quote
std::string quote(const std::string &sentence)
Enclose a string in ".
Definition: IOVDbStringFunctions.cxx:85
IOVDbNamespace::sanitiseXml
std::string sanitiseXml(const std::string &pseudoXmlString)
for use when converting cool folder description JSON
Definition: IOVDbStringFunctions.cxx:135
python.PyAthena.obj
obj
Definition: PyAthena.py:132
python.compressB64.c
def c
Definition: compressB64.py:93
CondAttrListVecAddress.h
This file contains the class definition for the CondAttrListVecAddress class.
json
nlohmann::json json
Definition: Cool2Json.cxx:35
IOVDbNamespace::Cool2Json::m_chansel
const cool::ChannelSelection m_chansel
Definition: Cool2Json.h:83
IOVDbNamespace::FolderType
FolderType
Definition: FolderTypes.h:26
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88
IOVDbNamespace
Definition: Base64Codec.cxx:16