ATLAS Offline Software
MakeReferenceFile.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /* Standalone application to produce a reference file from the pixel TOT calibration DB */
6 
7 #include "CoolKernel/DatabaseId.h"
8 #include "CoolKernel/Exception.h"
9 #include "CoolKernel/IDatabaseSvc.h"
10 #include "CoolKernel/IDatabase.h"
11 #include "CoolApplication/Application.h"
12 #include "CoolKernel/IFolder.h"
13 #include "CoolKernel/FolderSpecification.h"
14 #include "CoolKernel/RecordSpecification.h"
15 #include "CoolKernel/Record.h"
16 #include "CoolKernel/FieldSpecification.h"
17 #include "CoolKernel/IObject.h"
18 #include "CoolKernel/IObjectIterator.h"
19 #include "CoolKernel/IRecordIterator.h"
20 #include "CoolKernel/StorageType.h"
21 #include "CoolKernel/ChannelSelection.h"
22 //
23 #include "RelationalAccess/ConnectionService.h"
24 // STL
25 #include <iostream>
26 #include <fstream>
27 #include <string>
28 #include <string_view>
29 #include <stdexcept>
30 #include <ctime>
31 #include <sstream>
32 #include <unordered_map>
33 
34 #ifdef ATLAS_GCC_CHECKERS
37 #endif
38 
39 using iovNamePair= std::pair<std::string, std::string >;
40 
42 {
43 public:
44  DbConnection(const std::string &sourceDb);
45  ~DbConnection();
46  bool isOpen() const;
47  cool::IDatabasePtr dbPtr() const;
48 
49 private:
50  coral::ConnectionService m_coralsvc{};
51  cool::Application m_coolapp;
52  const cool::IDatabaseSvc &m_dbSvc;
53  cool::IDatabasePtr m_sourceDbPtr{};
54  bool m_isOpen{};
55 };
56 
57 // make db connection upon construction
58 DbConnection::DbConnection(const std::string &sourceDb) : m_coolapp(&m_coralsvc),
59  m_dbSvc(m_coolapp.databaseService())
60 {
61  bool readOnly(true);
62  const std::string dbConnectionString{sourceDb};
63  try
64  {
65  m_sourceDbPtr = m_dbSvc.openDatabase(dbConnectionString, readOnly);
66  }
67  catch (std::exception &e)
68  {
69  std::cout << "Cool exception caught: " << e.what() << std::endl;
70  if (not readOnly)
71  {
72  try
73  {
74  std::cout << "creating " << dbConnectionString << std::endl;
75  m_sourceDbPtr = m_dbSvc.createDatabase(dbConnectionString);
76  }
77  catch (std::exception &e)
78  {
79  std::cout << "Cool exception caught: " << e.what() << std::endl;
80  }
81  }
82  }
83  m_isOpen = (m_sourceDbPtr != nullptr);
84 }
85 
86 // break db connection on destruction
88 {
89  if (isOpen())
90  {
91  m_sourceDbPtr->closeDatabase();
92  }
93 }
94 
95 // return bare db pointer
96 cool::IDatabasePtr
98 {
99  return m_sourceDbPtr;
100 }
101 
103 {
104  return m_isOpen;
105 }
106 
107 // specification to identify a given folder from full name and tag
109 {
110  const std::string name;
111  const std::string tag;
112  FolderSpec(const std::string &thename, const std::string &thetag) : name(thename), tag(thetag){};
114 };
115 
116 // Folder interface class
117 class Folder
118 {
119 public:
120  Folder(const DbConnection &theConnection, const FolderSpec &theFolder);
121  bool isValid() const;
122  std::string description() const;
123  const FolderSpec& folderSpec() const;
124  cool::RecordSpecification foreignKeySpec();
125  cool::RecordSpecification payloadSpec() const;
126  bool isSingleVersion() const;
127  std::string tag() const;
128  const cool::IFolderPtr &ptr() const;
129  cool::IObjectIteratorPtr objectIterator(bool) const;
130  std::vector<cool::ChannelId> channels() const;
131  std::vector<std::string> tags() const;
132 
133 private:
136  cool::IFolderPtr m_folderPtr;
137 };
138 
139 Folder::Folder(const DbConnection &theConnection, const FolderSpec &theFolder)
140  : m_connection(theConnection), m_folderSpec(theFolder), m_folderPtr{}
141 {
142  try
143  {
144  m_folderPtr = m_connection.dbPtr()->getFolder(theFolder.name);
145  }
146  catch (cool::Exception &e)
147  {
148  std::cout << " Could not get folder " << theFolder.name << std::endl;
149  }
150 }
151 
152 const FolderSpec&
154 {
155  return m_folderSpec;
156 }
157 
158 const cool::IFolderPtr &
159 Folder::ptr() const
160 {
161  return m_folderPtr;
162 }
163 
164 // assume the coracool folder is valid if the pointer to it is valid
165 bool Folder::isValid() const
166 {
167  bool result = (m_folderPtr != nullptr);
168  return result;
169 }
170 
172 {
173  return (cool::FolderVersioning::SINGLE_VERSION == m_folderPtr->versioningMode());
174 }
175 
176 std::string
178 {
179  return (m_folderPtr->description());
180 }
181 
182 cool::RecordSpecification
184 {
185  return m_folderPtr->payloadSpecification();
186 }
187 
188 std::string
189 Folder::tag() const
190 {
191  return m_folderSpec.tag;
192 }
193 
194 cool::IObjectIteratorPtr
195 Folder::objectIterator(bool uselastIOV = false) const
196 {
197  // iovs to browse
198  const auto hi = cool::ValidityKeyMax;
199  const auto lo = uselastIOV ? (hi-1) : cool::ValidityKeyMin;
201  return m_folderPtr->browseObjects(lo, hi, channelSelection, m_folderSpec.tag);
202 }
203 
204 std::vector<cool::ChannelId>
206 {
207  return m_folderPtr->listChannels();
208 }
209 
210 std::vector<std::string>
212 {
213  return m_folderPtr->listTags();
214 }
215 
216 std::string
217 timeRep(const cool::ValidityKey &t, bool isEnd = false, bool runLumi = true)
218 {
219  std::string result;
220  const std::string trail = isEnd ? (")") : ("]");
221  if (not runLumi)
222  { // ns of epoch
223  if (t == cool::ValidityKeyMin)
224  {
225  result = "ValidityKeyMin";
226  }
227  else if (t == cool::ValidityKeyMax)
228  {
229  result = "ValidityKeyMax";
230  }
231  else
232  {
233  long int seconds = static_cast<long int>(t / 1000000000);
234  std::string timeStr{std::asctime(std::gmtime(&seconds))};
235  result = timeStr + " UTC";
236  }
237  }
238  else
239  { // runLumi
240  auto run = t >> 32;
241  auto lumiblock = t & 0xFFFFFFFF;
242  result = "[" + std::to_string(run) + "," + std::to_string(lumiblock) + trail;
243  }
244  return result;
245 }
246 
247 std::string
248 iovToString(const cool::IObject &obj, bool runLumi = true)
249 {
250  std::string sinceStr = timeRep(obj.since(), false, runLumi);
251  std::string untilStr = timeRep(obj.until(), true, runLumi);
252  return sinceStr + " - " + untilStr;
253 }
254 
255 std::string
256 payloadToString(const cool::IObject &obj)
257 {
258  std::string result;
259  const cool::IRecord &record = obj.payload();
260  const auto &thisSpec = record.specification();
261  for (cool::UInt32 i = 0; i != thisSpec.size(); ++i)
262  {
263  const std::string delimiter = (i == 0) ? ("") : (",");
264  std::string typeName = thisSpec[i].storageType().name();
265  result += "[\'" + thisSpec[i].name() + " (" + typeName + ")\' : ";
266  std::ostringstream os;
267  record[i].printValue(os);
268  result += os.str();
269  }
270  result += "]";
271  return result;
272 }
273 
274 int main(int argc, char *argv[])
275 {
276 
277  std::string tagName{"PixelChargeCalibration-DATA-RUN2-UPD4-27"};
278  std::string folderName{"/PIXEL/ChargeCalibration"};
279  std::string outputFileName{"test"};
280  std::string dbName{"COOLOFL_PIXEL/CONDBR2"};
281  std::string useIOV{"latest"};
282  bool useLastIOV{true};
283 
284  for(int i=1; i<argc; i++){
285  std::string aux(argv[i]);
286  std::string variable(aux.substr(0,aux.find("=")));
287  std::string value(aux.substr(aux.find("=")+1));
288  if (variable.compare("tagName") == 0) {
289  tagName = value;
290  }
291  else if(variable.compare("folder") == 0) folderName = std::move(value);
292  else if(variable.compare("outputFile") == 0) outputFileName = std::move(value);
293  else if(variable.compare("dbName") == 0) dbName = std::move(value);
294  else if(variable.compare("IOV") == 0){
295  // By selecting an IOV is understood you dont want to use the last one
296  // hence use the "IOV=runNumber_LB" (it should exist in the DB)
297  // if you want to print all of them use "IOV=all"
298  useIOV = std::move(value);
299  useLastIOV = false;
300  }
301  else{
302  std::cout<< "ERROR - Option not found:"<< aux <<"\n";
303  return 1;
304  }
305 
306  }
307 
308  if (outputFileName.compare("test") == 0){
309  if(useLastIOV){
310  outputFileName = tagName+".log";
311  }
312  else if (useIOV.compare("all") == 0){
313  outputFileName = "XXXXXX_"+tagName+".log";
314  }
315  else{
316  outputFileName = useIOV+"_"+tagName+".log";
317  }
318  }
319 
320  printf("%-11s: %s\n", "Tag Name" ,tagName.c_str());
321  printf("%-11s: %s\n", "Folder Name",folderName.c_str()) ;
322  printf("%-11s: %s\n", "DB Name" ,dbName.c_str()) ;
323  printf("%-11s: %s\n", "Output File",outputFileName.c_str()) ;
324  printf("%-11s: %s\n", "Last IOV" ,useLastIOV ? "True" : "False");
325  printf("%-11s: %s\n", "IOV used" ,useIOV.c_str()) ;
326 
327  int returnCode = 0;
328  DbConnection connection(dbName);
329  FolderSpec fs(folderName, tagName);
330  Folder f(connection, fs);
331 
332  cool::IObjectIteratorPtr objectsIterator = f.objectIterator(useLastIOV); // True to use the last IOV
333  std::vector< iovNamePair > myIOVs;
334  while (objectsIterator->goToNext())
335  {
336  const cool::IObject &thisObject = objectsIterator->currentRef();
337  std::size_t posPar = iovToString(thisObject).find("]");
338  std::size_t posCom = iovToString(thisObject).find(",");
339  std::string name = iovToString(thisObject).substr(1,posPar-1).replace(posCom-1,1,"_");
340  std::string display = iovToString(thisObject) + " (" + std::to_string(thisObject.channelId()) + ")\n";
341  display += payloadToString(thisObject);
342  myIOVs.push_back(std::make_pair(name,display));
343  }
344 
345  if(!useLastIOV){
346  bool found = false;
347  // Saving all the IOVs in different files, just used for experts - Evolution monitoring
348  for(const auto & [IOVname,iov]:myIOVs){
349  if(IOVname.compare(useIOV)!=0 && useIOV.compare("all") != 0){
350  continue;
351  }
352  found = true;
353  std::cout<<IOVname<<"\n";
354  const std::string filename= IOVname +"_"+tagName+".log";
355  std::ofstream opFile(filename);
356  opFile <<iov<<"\n";
357  opFile.close();
358  }
359  if(!found){
360  std::cout<< "IOV not found - Choose between one of the following:\n";
361  for(const auto & [IOVname,iov]:myIOVs){
362  std::cout<<IOVname<<" ";
363  }
364  std::cout << "\n";
365  }
366 
367  }
368  else{
369  const std::string fileName = tagName + ".log";
370  std::ofstream opFile(fileName);
371  printf("%-11s: %s\n", "RunNumber" ,(myIOVs.back().first).c_str()) ;
372  opFile << myIOVs.back().second << "\n";
373  opFile.close();
374  }
375 
376  return returnCode;
377 }
Folder::Folder
Folder(const DbConnection &theConnection, const FolderSpec &theFolder)
Definition: MakeReferenceFile.cxx:139
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
Folder::m_connection
const DbConnection & m_connection
Definition: MakeReferenceFile.cxx:134
iovNamePair
std::pair< std::string, std::string > iovNamePair
Definition: MakeReferenceFile.cxx:39
Folder::tag
std::string tag() const
Definition: MakeReferenceFile.cxx:189
get_generator_info.result
result
Definition: get_generator_info.py:21
dqt_zlumi_alleff_HIST.iov
iov
Definition: dqt_zlumi_alleff_HIST.py:119
makeDTCalibBlob_pickPhase.theFolder
theFolder
Definition: makeDTCalibBlob_pickPhase.py:383
Folder::description
std::string description() const
Definition: MakeReferenceFile.cxx:177
FolderSpec
Definition: MakeReferenceFile.cxx:109
Folder::tags
std::vector< std::string > tags() const
Definition: MakeReferenceFile.cxx:211
python.MagFieldUtils.lumiblock
lumiblock
Definition: MagFieldUtils.py:188
DbConnection
Definition: MakeReferenceFile.cxx:42
DbConnection::m_dbSvc
const cool::IDatabaseSvc & m_dbSvc
Definition: MakeReferenceFile.cxx:52
Cut::all
@ all
Definition: SUSYToolsAlg.cxx:67
athena.value
value
Definition: athena.py:124
DbConnection::m_isOpen
bool m_isOpen
Definition: MakeReferenceFile.cxx:54
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DbConnection::~DbConnection
~DbConnection()
Definition: MakeReferenceFile.cxx:87
DbConnection::DbConnection
DbConnection(const std::string &sourceDb)
Definition: MakeReferenceFile.cxx:58
FolderSpec::tag
const std::string tag
Definition: MakeReferenceFile.cxx:111
FolderSpec::name
const std::string name
Definition: MakeReferenceFile.cxx:110
Folder::foreignKeySpec
cool::RecordSpecification foreignKeySpec()
Definition: openCoraCool.cxx:496
python.AthDsoLogger.delimiter
delimiter
Definition: AthDsoLogger.py:71
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
Folder::isValid
bool isValid() const
Definition: MakeReferenceFile.cxx:165
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
DbConnection::m_sourceDbPtr
cool::IDatabasePtr m_sourceDbPtr
Definition: MakeReferenceFile.cxx:53
PixelModuleFeMask_create_db.dbName
string dbName
Definition: PixelModuleFeMask_create_db.py:21
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
#define ATLAS_NO_CHECK_FILE_THREAD_SAFETY
Definition: checker_macros.h:209
payloadToString
std::string payloadToString(const cool::IObject &obj)
Definition: MakeReferenceFile.cxx:256
DbConnection::isOpen
bool isOpen() const
Definition: MakeReferenceFile.cxx:102
calibdata.exception
exception
Definition: calibdata.py:496
python.selection.variable
variable
Definition: selection.py:33
hist_file_dump.f
f
Definition: hist_file_dump.py:135
run
Definition: run.py:1
Folder::payloadSpec
cool::RecordSpecification payloadSpec() const
specification of the payload entries
Definition: MakeReferenceFile.cxx:183
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
Folder::objectIterator
cool::IObjectIteratorPtr objectIterator(bool) const
Definition: MakeReferenceFile.cxx:195
python.LArCalib_HVCorrConfig.seconds
seconds
Definition: LArCalib_HVCorrConfig.py:86
pyroot.display
display
Definition: pyroot.py:44
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
Folder
Definition: MakeReferenceFile.cxx:118
Folder::m_folderSpec
const FolderSpec m_folderSpec
Definition: MakeReferenceFile.cxx:135
COOLRates.channelSelection
channelSelection
Definition: COOLRates.py:1176
CaloCellTimeCorrFiller.folderName
string folderName
Definition: CaloCellTimeCorrFiller.py:20
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
Folder::m_folderPtr
cool::IFolderPtr m_folderPtr
Definition: MakeReferenceFile.cxx:136
Folder::ptr
const cool::IFolderPtr & ptr() const
Definition: MakeReferenceFile.cxx:159
FolderSpec::FolderSpec
FolderSpec(const std::string &thename, const std::string &thetag)
Definition: MakeReferenceFile.cxx:112
Folder::channels
std::vector< cool::ChannelId > channels() const
Definition: MakeReferenceFile.cxx:205
Folder::folderSpec
const FolderSpec & folderSpec() const
Definition: MakeReferenceFile.cxx:153
Folder::isSingleVersion
bool isSingleVersion() const
Definition: MakeReferenceFile.cxx:171
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
DbConnection::m_coralsvc
coral::ConnectionService m_coralsvc
Definition: MakeReferenceFile.cxx:50
main
int main(int argc, char *argv[])
Definition: MakeReferenceFile.cxx:274
DbConnection::m_coolapp
cool::Application m_coolapp
Definition: MakeReferenceFile.cxx:51
Herwig7_QED_EvtGen_ll.fs
dictionary fs
Definition: Herwig7_QED_EvtGen_ll.py:17
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
ReadCalibFromCool.typeName
typeName
Definition: ReadCalibFromCool.py:477
DbConnection::dbPtr
cool::IDatabasePtr dbPtr() const
Definition: MakeReferenceFile.cxx:97
checker_macros.h
Define macros for attributes used to control the static checker.
python.PyAthena.obj
obj
Definition: PyAthena.py:132
AthenaPoolExample_Copy.outputFileName
string outputFileName
Definition: AthenaPoolExample_Copy.py:40
timeRep
std::string timeRep(const cool::ValidityKey &t, bool isEnd=false, bool runLumi=true)
Definition: MakeReferenceFile.cxx:217
iovToString
std::string iovToString(const cool::IObject &obj, bool runLumi=true)
Definition: MakeReferenceFile.cxx:248