ATLAS Offline Software
CrestFunctions.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 // @file CrestFunctions.cxx
5 // Implementation for CrestFunctions utilities
6 // @author Shaun Roe
7 // @date 1 July 2019
8 
9 #include "CrestFunctions.h"
10 #include "CrestApi/CrestApi.h"
11 #include "CrestApi/CrestApiFs.h"
12 #include <iostream>
13 #include <exception>
14 #include <regex>
15 #include "IOVDbStringFunctions.h"
16 #include <string>
17 #include <algorithm>
18 #include <map>
19 
20 namespace IOVDbNamespace{
21 
22  CrestFunctions::CrestFunctions(const std::string & crest_path){
23  setURLBase(crest_path);
24 
25  const std::string prefix1 = "http://";
26  const std::string prefix2 = "https://";
27  if (crest_path.starts_with(prefix1) || crest_path.starts_with(prefix2)){
28  m_crestCl = std::unique_ptr<Crest::CrestClient>(new Crest::CrestClient(getURLBase()));
29  }
30  else{
31  m_crestCl = std::unique_ptr<Crest::CrestFsClient>(new Crest::CrestFsClient(true,getURLBase()));
32  }
33  }
34 
35  const std::string &
37  return m_CREST_PATH;
38  }
39 
40  void
41  CrestFunctions::setURLBase(const std::string & crest_path){
42  m_CREST_PATH = crest_path;
43  }
44 
45 
46  std::string
47  CrestFunctions::extractHashFromJson(const std::string & jsonReply){
48  std::string hash{};
49  try{
50  std::string_view signature="payloadHash\":\"";
51  auto signaturePosition=jsonReply.rfind(signature);
52  if (signaturePosition == std::string::npos) throw std::runtime_error("signature "+std::string(signature)+" not found");
53  auto startOfHash=signaturePosition + signature.size();
54  auto endOfHash=jsonReply.find('\"',startOfHash);
55  auto len=endOfHash-startOfHash;
56  if (startOfHash > jsonReply.size()) throw std::runtime_error("Hash start is beyond end of string");
57  hash=jsonReply.substr(startOfHash, len);
58  } catch (std::exception & e){
59  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the hash in "<<jsonReply<<std::endl;
60  }
61  return hash;
62  }
63 
64 
65  std::string
66  CrestFunctions::getLastHashForTag(const std::string & tag){
67  char tu[] = "";
68  strfry(tu);
69  std::string reply = "";
70 
71  try{
72  IovSetDto dto = m_crestCl->selectIovs(tag, 0, -1, 0, 10000, 0, "id.since:ASC");
73 
74  nlohmann::json iov_data = dto.to_json();
75  nlohmann::json iov_list = getResources(iov_data);
76  reply = iov_list.dump();
77  } catch (std::exception & e){
78  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the IOVs"<<std::endl;
79  return "";
80  }
81 
82  return extractHashFromJson(reply);
83  }
84 
85  std::string
87  std::string reply = "";
88 
89  try{
90  reply = m_crestCl->getPayload(hash);
91  } catch (std::exception & e){
92  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the payload"<<std::endl;
93  return "";
94  }
95 
96  return reply;
97  }
98 
99 
100  std::string
101  CrestFunctions::extractDescriptionFromJson(const std::string & jsonReply){
102  std::string description{};
103  try{
104  const std::string_view signature="node_description\\\":\\\"";
105  const auto signaturePosition = jsonReply.find(signature);
106  if (signaturePosition == std::string::npos) throw std::runtime_error("signature "+std::string(signature)+" not found");
107  const auto startOfDescription= signaturePosition + signature.size();
108  const std::string_view endSignature = "\\\",\\\"payload_spec";
109  const auto endOfDescription=jsonReply.find(endSignature);
110  if (endOfDescription == std::string::npos) throw std::runtime_error("end signature "+std::string(endSignature)+" not found");
111  const auto len=endOfDescription-startOfDescription;
112  description=jsonReply.substr(startOfDescription, len);
113  } catch (std::exception & e){
114  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the description in "<<jsonReply<<std::endl;
115  }
116 
118  }
119 
120 
121  std::string
123 
124  std::string jsonReply = "";
125 
126  TagMetaDto dto = m_crestCl->findTagMeta(tag);
127  jsonReply = dto.tagInfo.getFolderDescription();
128  return jsonReply;
129  }
130 
131 
132  std::map<std::string, std::string>
134  std::map<std::string, std::string> tagmap;
135  try{
136  GlobalTagMapSetDto dto = m_crestCl->findGlobalTagMap(globaltag,"Trace");
137  nlohmann::json globaltag_map_data = dto.to_json();
138  nlohmann::json j = getResources(globaltag_map_data);
139  int n = j.size();
140  for (int i = 0; i < n; i++ ){
141  nlohmann::json j_item = j[i];
142  if (j_item.contains("label") && j_item.contains("tagName") ){
143  tagmap[j_item["label"]] = j_item["tagName"];
144  }
145  }
146  } catch (std::exception & e){
147  std::cerr<<__FILE__<<":"<<__LINE__<< ": " << e.what() << " Cannot get a global tag map for " << globaltag << std::endl;
148  }
149 
150  return tagmap;
151  }
152 
153 
155  try{
156  TagMetaDto dto = m_crestCl->findTagMeta(tag);
157  nlohmann::json meta_info = dto.to_json();
158 
159  if (meta_info.contains("tagInfo")){
160  std::string metainf = meta_info["tagInfo"];
162  return js;
163  }
164 
165  } catch (std::exception & e){
166  std::cerr<<__FILE__<<":"<<__LINE__<< ": " << e.what() << " Cannot get a tag meta info " << tag << std::endl;
167  }
168  return nullptr;
169  }
170 
172  CrestFunctions::getTagProperties(const std::string & tag){
173  try{
174  TagDto dto = m_crestCl->findTag(tag);
175  return dto.to_json();
176  } catch (std::exception & e){
177  std::cerr<<__FILE__<<":"<<__LINE__<< ": " << e.what() << " Cannot get a tag Properties of " << tag << std::endl;
178  }
179  return nullptr;
180  }
181 
182  std::string
183  CrestFunctions::getTagInfoElement(nlohmann::json tag_info, const std::string & key){
184  if (tag_info.contains(key)){
185  if (key == "channel_list"){
186  return tag_info[key].dump();
187  } else if (key== "node_description"){
188  std::string v;
189  tag_info[key].get_to(v);
190  return v;
191  } else{
192  return nlohmann::to_string(tag_info[key]);
193  }
194  }
195  return "";
196  }
197 
198  std::pair<std::vector<cool::ChannelId> , std::vector<std::string>>
199  CrestFunctions::extractChannelListFromString(const std::string & chanString){
200  std::vector<cool::ChannelId> list;
201  std::vector<std::string> names;
202  nlohmann::json js = nlohmann::json::parse(chanString);
203  int n = js.size();
204 
205  for (int i = 0; i <= n; i++) {
206  nlohmann::json j_object = js[i];
207  for (auto& [key, val] : j_object.items()){
208  list.push_back(std::stoll(key));
209  names.push_back(val);
210  }
211  }
212 
213  // if all the names are empty, these are unnamed channels, and can just return an empty vector for the names
214  auto isEmpty=[](const std::string & s){return s.empty();};
215  if ( std::all_of(names.begin(), names.end(), isEmpty)) names.clear();
216  return std::make_pair(std::move(list), std::move(names));
217  }
218 
221  nlohmann::json js2 = json::array();
222  nlohmann::json result = js.value("resources", js2);
223  return result;
224  }
225 
226  std::vector<uint64_t>
227  CrestFunctions::getIovGroups(const std::string & tag){
228  std::vector<uint64_t> v;
229  try{
230  IovSetDto dto = m_crestCl->selectGroups(tag, 0, 10000, 0, "id.since:ASC");
231  const std::vector<IovDto> & res = dto.resources;
232  for (const IovDto & item_iov: res){
233  v.emplace_back(item_iov.since);
234  }
235  } catch (std::exception & e){
236  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the IOVs"<<std::endl;
237  return {};
238  }
239  return v;
240  }
241 
242 
243  std::pair<uint64_t,uint64_t>
244  CrestFunctions::getSinceUntilPair(std::vector<uint64_t> v, const uint64_t since, const uint64_t until){
245  uint64_t new_since = 0;
246  uint64_t new_until = 0;
247  std::pair<uint64_t,uint64_t> answer = std::make_pair(0,0);
248 
249  if (until < since){
250  std::cerr << "Wrong since/until." << std::endl;
251  return answer;
252  }
253 
254  int N = v.size();
255  for (int i = 0; i < N; i++) {
256  if(v[i] <= since && since < v[i+1]){
257  new_since = v[i];
258  break;
259  }
260  }
261 
262  for (int i = 0; i < N; i++) {
263  if(v[i] < until && until <= v[i+1]){
264  new_until = v[i+1];
265  break;
266  }
267  }
268 
269  answer = std::make_pair(new_since,new_until);
270  return answer;
271  }
272 
273 
274  int CrestFunctions::getTagSize(const std::string& tagname){
275  int res = 0;
276  try{
277  res = m_crestCl->getSize(tagname);
278  } catch (std::exception & e){
279  std::cerr<<__FILE__<<":"<<__LINE__<< ": " << e.what() << " Cannot get the tag size for " << tagname << std::endl;
280  }
281  return res;
282  }
283 
284  std::pair<uint64_t,uint64_t>
285  CrestFunctions::getIovInterval(const std::string& tag, const uint64_t since, const uint64_t until){
286  std::vector<uint64_t> v = getIovGroups(tag);
287  v.push_back(std::numeric_limits<uint64_t>::max()); // added "infinity" as the last item
288  return getSinceUntilPair(std::move(v), since, until);
289  }
290 
291 
292  std::vector<IovHashPair>
294  std::vector<IovHashPair> iovHashPairs;
295  int iovNumber = getTagSize(tag);
296  try{
297  IovSetDto dto;
298  if (iovNumber <=1000) {
299  dto = m_crestCl->selectIovs(tag, 0, -1, 0, 10000, 0, "id.since:ASC");
300  } else {
301  const auto &[s_time, u_time] = getIovInterval(tag, since, until);
302  if (s_time == 0 && u_time == 0){ // data out of range
303  return iovHashPairs;
304  } else {
305  dto = m_crestCl->selectIovs(tag, s_time, u_time, 0, 10000, 0, "id.since:ASC");
306  }
307  }
308  std::vector<IovDto> res = dto.resources;
309  std::map<uint64_t, std::string> hashmap;
310  for (const IovDto & item: res) {
311  hashmap[item.since] = item.payloadHash;
312  }
313  for (auto& t : hashmap){
314  iovHashPairs.emplace_back(std::to_string(t.first),t.second);
315  }
316  } catch (std::exception & e){
317  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the IOVs"<<std::endl;
318  return {};
319  }
320  return iovHashPairs;
321  }
322 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
TagMetaDto::tagInfo
TagInfoDto tagInfo
Definition: CrestModel.h:243
IOVDbNamespace::CrestFunctions::m_CREST_PATH
std::string m_CREST_PATH
Definition: CrestFunctions.h:88
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
IOVDbNamespace::CrestFunctions::getIovsForTag
std::vector< IovHashPair > getIovsForTag(const std::string &tag, uint64_t since, uint64_t until)
Definition: CrestFunctions.cxx:293
get_generator_info.result
result
Definition: get_generator_info.py:21
IovSetDto::resources
std::vector< IovDto > resources
Definition: CrestModel.h:289
max
#define max(a, b)
Definition: cfImp.cxx:41
IOVDbNamespace::CrestFunctions::getIovInterval
std::pair< uint64_t, uint64_t > getIovInterval(const std::string &tag, const uint64_t since, const uint64_t until)
Definition: CrestFunctions.cxx:285
IOVDbNamespace::CrestFunctions::m_crestCl
std::unique_ptr< Crest::CrestApiBase > m_crestCl
Definition: CrestFunctions.h:86
json
nlohmann::json json
Definition: HistogramDef.cxx:9
TagDto::to_json
json to_json() const
Definition: CrestModel.cxx:172
IovDto
Definition: CrestModel.h:268
IOVDbNamespace::CrestFunctions::setURLBase
void setURLBase(const std::string &crest_path)
Definition: CrestFunctions.cxx:41
IOVDbNamespace::CrestFunctions::extractChannelListFromString
std::pair< std::vector< cool::ChannelId >, std::vector< std::string > > extractChannelListFromString(const std::string &chanString)
Definition: CrestFunctions.cxx:199
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
CrestApiFs.h
Header file for CREST C++ Client Library.
IOVDbNamespace::CrestFunctions::getTagProperties
nlohmann::json getTagProperties(const std::string &tag)
Definition: CrestFunctions.cxx:172
IOVDbNamespace::CrestFunctions::CrestFunctions
CrestFunctions(const std::string &crest_path)
Definition: CrestFunctions.cxx:22
JetTiledMap::N
@ N
Definition: TiledEtaPhiMap.h:44
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
TagMetaDto::to_json
json to_json() const
Definition: CrestModel.cxx:389
CrestApi.h
Header file for CREST C++ Client Library.
TagMetaDto
Definition: CrestModel.h:238
IOVDbNamespace::CrestFunctions::extractHashFromJson
std::string extractHashFromJson(const std::string &jsonReply)
Definition: CrestFunctions.cxx:47
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
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
python.subdetectors.mmg.names
names
Definition: mmg.py:8
TagDto
The TagDto class It contains all fields of the TagDto class from the CREST API.
Definition: CrestModel.h:98
calibdata.exception
exception
Definition: calibdata.py:496
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
GlobalTagMapSetDto
Definition: CrestModel.h:160
GlobalTagMapSetDto::to_json
json to_json() const
Definition: CrestModel.cxx:253
CheckTagAssociation.globaltag
globaltag
Definition: CheckTagAssociation.py:18
lumiFormat.array
array
Definition: lumiFormat.py:91
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
IOVDbNamespace::unescapeBackslash
std::string unescapeBackslash(const std::string &original)
Definition: IOVDbStringFunctions.cxx:102
item
Definition: ItemListSvc.h:43
Crest::CrestFsClient
Definition: CrestApiFs.h:32
IOVDbNamespace::CrestFunctions::getTagInfoElement
std::string getTagInfoElement(nlohmann::json tag_info, const std::string &key)
Definition: CrestFunctions.cxx:183
IOVDbNamespace::CrestFunctions::getTagInfo
nlohmann::json getTagInfo(const std::string &tag)
Definition: CrestFunctions.cxx:154
python.PyAthena.v
v
Definition: PyAthena.py:154
IOVDbNamespace::CrestFunctions::getSinceUntilPair
std::pair< uint64_t, uint64_t > getSinceUntilPair(std::vector< uint64_t > v, const uint64_t since, const uint64_t until)
Definition: CrestFunctions.cxx:244
IOVDbNamespace::CrestFunctions::getResources
nlohmann::json getResources(nlohmann::json &js)
Definition: CrestFunctions.cxx:220
IOVDbNamespace::CrestFunctions::getURLBase
const std::string & getURLBase()
Definition: CrestFunctions.cxx:36
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
IOVDbNamespace::CrestFunctions::getPayloadForHash
std::string getPayloadForHash(const std::string &hash)
Definition: CrestFunctions.cxx:86
IOVDbNamespace::CrestFunctions::getIovGroups
std::vector< uint64_t > getIovGroups(const std::string &tag)
Definition: CrestFunctions.cxx:227
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
IovSetDto
Definition: CrestModel.h:287
IOVDbNamespace::unescapeQuotes
std::string unescapeQuotes(const std::string &original)
Definition: IOVDbStringFunctions.cxx:95
Crest::CrestClient
Definition: CrestApi.h:33
IOVDbNamespace::CrestFunctions::folderDescriptionForTag
std::string folderDescriptionForTag(const std::string &tag)
Definition: CrestFunctions.cxx:122
tagname
Definition: tagname.h:29
IOVDbNamespace::CrestFunctions::getTagSize
int getTagSize(const std::string &tagname)
Definition: CrestFunctions.cxx:274
TagInfoDto::getFolderDescription
std::string getFolderDescription()
Definition: CrestModel.h:228
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
IOVDbNamespace::CrestFunctions::getLastHashForTag
std::string getLastHashForTag(const std::string &tag)
Definition: CrestFunctions.cxx:66
CrestFunctions.h
Header for CrestFunctions utilities.
IOVDbNamespace::CrestFunctions::getGlobalTagMap
std::map< std::string, std::string > getGlobalTagMap(const std::string &globaltag)
Definition: CrestFunctions.cxx:133
IovSetDto::to_json
json to_json() const
Definition: CrestModel.cxx:497
IOVDbNamespace::CrestFunctions::extractDescriptionFromJson
std::string extractDescriptionFromJson(const std::string &jsonReply)
Definition: CrestFunctions.cxx:101
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88
IOVDbNamespace
Definition: Base64Codec.cxx:16
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37