Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  using namespace Crest;
23 
24  CrestFunctions::CrestFunctions(const std::string & crest_path){
25  setURLBase(crest_path);
26 
27  const std::string prefix1 = "http://";
28  const std::string prefix2 = "https://";
29  if (crest_path.starts_with(prefix1) || crest_path.starts_with(prefix2)){
30  m_crestCl = std::unique_ptr<Crest::CrestApi>(new Crest::CrestApi(getURLBase()));
31  }
32  else{
33  m_crestCl = std::unique_ptr<Crest::CrestApiFs>(new Crest::CrestApiFs(true,getURLBase()));
34  }
35  }
36 
37  const std::string &
39  return m_CREST_PATH;
40  }
41 
42  void
43  CrestFunctions::setURLBase(const std::string & crest_path){
44  m_CREST_PATH = crest_path;
45  }
46 
47 
48  std::string
49  CrestFunctions::extractHashFromJson(const std::string & jsonReply){
50  std::string hash{};
51  try{
52  std::string_view signature="payloadHash\":\"";
53  auto signaturePosition=jsonReply.rfind(signature);
54  if (signaturePosition == std::string::npos) throw std::runtime_error("signature "+std::string(signature)+" not found");
55  auto startOfHash=signaturePosition + signature.size();
56  auto endOfHash=jsonReply.find('\"',startOfHash);
57  auto len=endOfHash-startOfHash;
58  if (startOfHash > jsonReply.size()) throw std::runtime_error("Hash start is beyond end of string");
59  hash=jsonReply.substr(startOfHash, len);
60  } catch (std::exception & e){
61  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the hash in "<<jsonReply<<std::endl;
62  }
63  return hash;
64  }
65 
66 
67  std::string
68  CrestFunctions::getLastHashForTag(const std::string & tag){
69  char tu[] = "";
70  strfry(tu);
71  std::string reply = "";
72 
73  try{
74  IovSetDto dto = m_crestCl->selectIovs(tag, 0, -1, 0, 10000, 0, "id.since:ASC");
75 
76  nlohmann::json iov_data = dto.toJson();
77  nlohmann::json iov_list = getResources(iov_data);
78  reply = iov_list.dump();
79  } catch (std::exception & e){
80  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the IOVs"<<std::endl;
81  return "";
82  }
83 
84  return extractHashFromJson(reply);
85  }
86 
87  std::string
89  std::string reply = "";
90 
91  try{
92  reply = m_crestCl->getPayload(hash);
93  } catch (std::exception & e){
94  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the payload"<<std::endl;
95  return "";
96  }
97 
98  return reply;
99  }
100 
101 
104  nlohmann::json js2 = json::array();
105  nlohmann::json result = js.value("resources", js2);
106  return result;
107  }
108 
109  std::vector<uint64_t>
110  CrestFunctions::getIovGroups(const std::string & tag){
111  std::vector<uint64_t> v;
112  try{
113  IovSetDto dto = m_crestCl->selectGroups(tag, 0, 10000, 0, "id.since:ASC");
114  const std::vector<IovDto> & res = dto.getResources();
115  for (const IovDto & item_iov: res){
116  v.emplace_back(item_iov.getSince());
117  }
118  } catch (std::exception & e){
119  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the IOVs"<<std::endl;
120  return {};
121  }
122  return v;
123  }
124 
125 
126  std::pair<uint64_t,uint64_t>
127  CrestFunctions::getSinceUntilPair(const std::vector<uint64_t>& v, const uint64_t since, const uint64_t until){
128  uint64_t new_since = 0;
129  uint64_t new_until = 0;
130  std::pair<uint64_t,uint64_t> answer = std::make_pair(0,0);
131 
132  if (until < since){
133  std::cerr << "Wrong since/until." << std::endl;
134  return answer;
135  }
136 
137  int N = v.size();
138  for (int i = 0; i < N; i++) {
139  if(v[i] <= since && since < v[i+1]){
140  new_since = v[i];
141  break;
142  }
143  }
144 
145  for (int i = 0; i < N; i++) {
146  if(v[i] < until && until <= v[i+1]){
147  new_until = v[i+1];
148  break;
149  }
150  }
151 
152  answer = std::make_pair(new_since,new_until);
153  return answer;
154  }
155 
156 
157  int CrestFunctions::getTagSize(const std::string& tagname){
158  int res = 0;
159  try{
160  res = m_crestCl->getSize(tagname);
161  } catch (std::exception & e){
162  std::cerr<<__FILE__<<":"<<__LINE__<< ": " << e.what() << " Cannot get the tag size for " << tagname << std::endl;
163  }
164  return res;
165  }
166 
167  std::pair<uint64_t,uint64_t>
168  CrestFunctions::getIovInterval(const std::string& tag, const uint64_t since, const uint64_t until){
169  std::vector<uint64_t> v = getIovGroups(tag);
170  v.push_back(std::numeric_limits<uint64_t>::max()); // added "infinity" as the last item
171  return getSinceUntilPair(std::move(v), since, until);
172  }
173 
174 
175  std::vector<IovHashPair>
177  std::vector<IovHashPair> iovHashPairs;
178  int iovNumber = getTagSize(tag);
179  try{
180  IovSetDto dto;
181  if (iovNumber <=1000) {
182  dto = m_crestCl->selectIovs(tag, 0, -1, 0, 10000, 0, "id.since:ASC");
183  } else {
184  const auto &[s_time, u_time] = getIovInterval(tag, since, until);
185  if (s_time == 0 && u_time == 0){ // data out of range
186  return iovHashPairs;
187  } else {
188  dto = m_crestCl->selectIovs(tag, s_time, u_time, 0, 10000, 0, "id.since:ASC");
189  }
190  }
191  std::vector<IovDto> res = dto.getResources();
192  std::map<uint64_t, std::string> hashmap;
193  for (const IovDto & item: res) {
194  hashmap[item.getSince()] = item.getPayloadHash();
195  }
196  for (auto& t : hashmap){
197  iovHashPairs.emplace_back(std::to_string(t.first),t.second);
198  }
199  } catch (std::exception & e){
200  std::cerr<<__FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the IOVs"<<std::endl;
201  return {};
202  }
203  return iovHashPairs;
204  }
205 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
IOVDbNamespace::CrestFunctions::getIovsForTag
std::vector< IovHashPair > getIovsForTag(const std::string &tag, uint64_t since, uint64_t until)
Definition: CrestFunctions.cxx:176
get_generator_info.result
result
Definition: get_generator_info.py:21
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:168
json
nlohmann::json json
Definition: HistogramDef.cxx:9
IOVDbNamespace::CrestFunctions::getSinceUntilPair
std::pair< uint64_t, uint64_t > getSinceUntilPair(const std::vector< uint64_t > &v, const uint64_t since, const uint64_t until)
Definition: CrestFunctions.cxx:127
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
IOVDbNamespace::CrestFunctions::setURLBase
void setURLBase(const std::string &crest_path)
Definition: CrestFunctions.cxx:43
IOVDbNamespace::CrestFunctions::CrestFunctions
CrestFunctions(const std::string &crest_path)
Definition: CrestFunctions.cxx:24
JetTiledMap::N
@ N
Definition: TiledEtaPhiMap.h:44
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
IOVDbNamespace::CrestFunctions::extractHashFromJson
std::string extractHashFromJson(const std::string &jsonReply)
Definition: CrestFunctions.cxx:49
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
calibdata.exception
exception
Definition: calibdata.py:496
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:11
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
lumiFormat.array
array
Definition: lumiFormat.py:91
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
item
Definition: ItemListSvc.h:43
python.PyAthena.v
v
Definition: PyAthena.py:154
IOVDbNamespace::CrestFunctions::getResources
nlohmann::json getResources(nlohmann::json &js)
Definition: CrestFunctions.cxx:103
IOVDbNamespace::CrestFunctions::getURLBase
const std::string & getURLBase()
Definition: CrestFunctions.cxx:38
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
IOVDbNamespace::CrestFunctions::getPayloadForHash
std::string getPayloadForHash(const std::string &hash)
Definition: CrestFunctions.cxx:88
IOVDbNamespace::CrestFunctions::getIovGroups
std::vector< uint64_t > getIovGroups(const std::string &tag)
Definition: CrestFunctions.cxx:110
tagname
Definition: tagname.h:29
IOVDbNamespace::CrestFunctions::getTagSize
int getTagSize(const std::string &tagname)
Definition: CrestFunctions.cxx:157
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
IOVDbNamespace::CrestFunctions::getLastHashForTag
std::string getLastHashForTag(const std::string &tag)
Definition: CrestFunctions.cxx:68
CrestFunctions.h
Header for CrestFunctions utilities.
IOVDbNamespace
Definition: Base64Codec.cxx:16