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 
34 namespace {
35  std::string
36  spec2String(const cool::IFolderPtr & pFolder){
37  //open bracket, close bracket for json objects
38  const std::string ob="";
39  const std::string cb="";
40  const std::string objName="\"folder_payloadspec\": \"";
41  std::string result=ob+objName;
42  const auto & rspec = pFolder->payloadSpecification();
43  std::string sep{""};
44  for (unsigned int i(0); i<rspec.size();++i){
45  if (i==1) sep=", ";
46  const auto & f = rspec[i];
47  result+=sep;
48  result+=f.name();
49  result+=": ";
50  result+=f.storageType().name();
51  }
52  result+='\"';
53  result+=cb;
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  std::string out = "\"node_description\" : \"";
84  out += saneXml;
85  out += '\"';
86  return out;
87  }
88 
89  const std::string&
91  return m_spec;
92  }
93 
94 
95  std::string
97  std::string result("\"data_array\" : [");
98  cool::IObjectIteratorPtr itr=m_pFolder->browseObjects(m_start,m_stop,m_chansel,m_tag);
100  std::string sep="";
101  while (itr->goToNext()){
102  const cool::IObject& ref=itr->currentRef();
103  result+=sep;
104  const long long cId=ref.channelId();
105  result+=s_openJson+quote(std::to_string(cId))+" : ";
106  switch (ftype){
108  result+=formatCvp(itr);
109  break;
111  result +=formatAttrList(itr);
112  break;
114  result +=formatAttrList(itr);
115  break;
117  result +=formatPoolRef(itr);
118  break;
120  result += formatAttrList(itr);
121  break;
123  result += " CoraCool";
124  break;
125  default:
126  result+=" a_data_value";
127  }
128  if (sep.empty()) sep=",";
129  result+=s_closeJson;
130  }
131  result+=']';
132  itr->close();
133  return result;
134  }
135 
136  unsigned int
138  return m_nchans;
139  }
140 
141  std::string
143  std::string result("");
144  //run-lumi and run-event can both be found in our database, but the meaning is run-lumi
145  if (m_desc.find("<timeStamp>run-lumi</timeStamp>") != std::string::npos) result = "run-lumi";
146  if (m_desc.find("<timeStamp>run-event</timeStamp>") != std::string::npos) result = "run-lumi";
147  if (m_desc.find("<timeStamp>time</timeStamp>") != std::string::npos) result = "time";
148  return result;
149  }
150 
151  const std::string&
152  Cool2Json::tag() const{
153  return m_tag;
154  }
155 
156  std::string
157  Cool2Json::formatCvp(const cool::IObjectIteratorPtr & itr){
158  std::string os;
159  const cool::IObject& ref=itr->currentRef();
160  cool::IRecordIterator& pitr=ref.payloadIterator();
161  auto pvec=pitr.fetchAllAsVector();
162  std::string sep="";
163  os+='[';//vector of vectors
164  for (const auto & vitr:*pvec){
165  os+=sep;
166  const coral::AttributeList& atrlist=(vitr)->attributeList();
168  if (sep.empty()) sep =IOVDbNamespace::s_delimiterJson;
169  }
170  os+=']';
171  return os;
172  }
173 
174  std::string
175  Cool2Json::formatAttrList(const cool::IObjectIteratorPtr & itr){
176  const cool::IObject& ref=itr->currentRef();
177  const coral::AttributeList& atrlist=ref.payload().attributeList();
178  std::string sep="";
179  return IOVDbNamespace::jsonAttributeList(atrlist);
180  }
181 
182  std::string
183  Cool2Json::formatPoolRef(const cool::IObjectIteratorPtr & itr){
184  const cool::IObject& ref=itr->currentRef();
185  const coral::AttributeList& atrlist=ref.payload().attributeList();
186  std::ostringstream os;
187  atrlist[0].toOutputStream(os);
188  auto res=os.str();
189  const std::string sep(" : ");
190  const auto separatorPosition = res.find(sep);
191  const std::string payloadOnly=res.substr(separatorPosition+3);
192  return quote(payloadOnly);
193  }
194 
195  std::string
196  Cool2Json::iov() const{
197  std::string iovString("\"iov\" : ");
198  return iovString+"["+std::to_string(m_start)+", "+std::to_string(m_stop)+"]";
199  }
200 
201  std::ostream &operator<<(std::ostream &o, const Cool2Json &c){
202  o << c.description() << std::endl;
203  return o;
204  }
205 
206 }
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.
IOVDbNamespace::Cool2Json::formatCvp
static std::string formatCvp(const cool::IObjectIteratorPtr &itr)
Definition: Cool2Json.cxx:157
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
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:201
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
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:14
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:135
IOVDbNamespace::AttrListColl
@ AttrListColl
Definition: FolderTypes.h:29
IOVDbNamespace::Cool2Json::payload
std::string payload()
Payload (data for the given channel selection and iov)
Definition: Cool2Json.cxx:96
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
python.changerun.m_stop
m_stop
Definition: changerun.py:85
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
python.changerun.m_start
m_start
Definition: changerun.py:84
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:90
CaloCondBlobAlgs_fillNoiseFromASCII.folderTag
folderTag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:52
ref
const boost::regex ref(r_ef)
IOVDbNamespace::Cool2Json::formatPoolRef
static std::string formatPoolRef(const cool::IObjectIteratorPtr &itr)
Definition: Cool2Json.cxx:183
IOVDbNamespace::Cool2Json::formatAttrList
static std::string formatAttrList(const cool::IObjectIteratorPtr &itr)
Definition: Cool2Json.cxx:175
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:152
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:196
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:142
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:137
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.compressB64.c
def c
Definition: compressB64.py:93
CondAttrListVecAddress.h
This file contains the class definition for the CondAttrListVecAddress class.
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