ATLAS Offline Software
Loading...
Searching...
No Matches
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"
32#include "Base64Codec.h"
33#include <regex>
34#include <nlohmann/json.hpp>
35using json = nlohmann::json;
36namespace {
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
58namespace 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=",";
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&
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
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}
const boost::regex ref(r_ef)
This file contains the class definition for the AthenaAttrListAddress class.
This file contains the class definition for the CondAttrListCollAddress class.
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
This file contains the class definition for the CondAttrListVecAddress class.
A CondAttrListVec is an Athena DataObject holding a vector of CORAL AttributeLists,...
std::array< fp_t, 2 > pvec
nlohmann::json json
std::pair< std::vector< unsigned int >, bool > res
unsigned int nchans() const
Number of channels in the folder.
const std::string & tag() const
Simply the tag string passed to the constructor.
std::string iov() const
Formatted iov as '[<since>, <until>]'.
const cool::ChannelSelection m_chansel
Definition Cool2Json.h:83
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
const std::string m_spec
Definition Cool2Json.h:86
static std::string formatPoolRef(const cool::IObjectIteratorPtr &itr)
std::string description() const
Folder description string.
Definition Cool2Json.cxx:81
const std::string & payloadSpec() const
Payload specification for this folder.
Definition Cool2Json.cxx:91
static std::string formatAttrList(const cool::IObjectIteratorPtr &itr)
static std::string formatCvp(const cool::IObjectIteratorPtr &itr)
const cool::ValidityKey m_stop
Definition Cool2Json.h:82
const cool::IFolderPtr m_pFolder
Definition Cool2Json.h:80
std::string iovBase() const
'time' (ns of epoch) or 'run-lumi'
std::string payload()
Payload (data for the given channel selection and iov)
Definition Cool2Json.cxx:97
const cool::ValidityKey m_start
Definition Cool2Json.h:81
const std::string m_tag
Definition Cool2Json.h:84
const std::string m_desc
Definition Cool2Json.h:85
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.
std::string quote(const std::string &sentence)
Enclose a string in ".
std::string jsonAttributeList(const coral::AttributeList &atrlist)
Produce a representation of a coral::AttributeList as a json string.
std::ostream & operator<<(std::ostream &o, const Cool2Json &c)
static const std::string s_delimiterJson
json standard delimiter ', '
std::string sanitiseXml(const std::string &pseudoXmlString)
for use when converting cool folder description JSON
static const std::string s_closeJson
json close tag, '}'
static const std::string s_openJson
json open tag, '{'