ATLAS Offline Software
DatabaseConfig.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <iostream>
6 #include <nlohmann/json.hpp>
7 
8 #include "CoolApplication/DatabaseSvcFactory.h"
9 #include "CoolKernel/IDatabase.h"
10 #include "CoolKernel/IDatabaseSvc.h"
11 #include "CoolKernel/IObject.h"
12 #include "CoolKernel/IFolder.h"
13 
15 #include <array>
16 #include <utility>
17 
18 namespace dqi {
19 
21 DatabaseConfig(std::string connectionString, long runNumber)
22  : m_connectionString(std::move(connectionString))
23  , m_runNumber((runNumber << 32) + 1)
24  , m_dbConnected(false)
25  , m_folderConnected(false)
26 {
27 }
28 
29 void
31 Disconnect()
32 {
33  if(m_dbConnected) {
34  m_database->closeDatabase();
35  m_dbConnected = false;
36  m_folderConnected = false;
37  }
38 }
39 
40 void
42 Connect()
43 {
44  try {
45  cool::IDatabaseSvc& dbSvc = cool::DatabaseSvcFactory::databaseService();
46  m_database = dbSvc.openDatabase(m_connectionString, true);
47 
48  } catch (std::exception& e) {
49  std::cerr << "Unable to open database -> " << e.what() << '\n';
50  return;
51  }
52  m_dbConnected = true;
53  try {
54  m_folder= m_database->getFolder("/GLOBAL/DQM/REFERENCES");
55 
56  } catch (std::exception& e) {
57  std::cerr << "Unable loading folder for this tag/run-> " << e.what() << '\n';
58  return;
59  }
60  m_folderConnected = true;
61 }
62 
65 GetPayload(const std::string& tag)
66 {
68  Connect();
69  }
70  if(m_jsonData.find(tag) == m_jsonData.end()) {
71  return LoadPayload(tag);
72  }
73  return m_jsonData.at(tag);
74 }
75 
78 LoadPayload(const std::string& tag)
79 {
80  nlohmann::json jsonData;
82  try {
83  cool::IObjectPtr object = m_folder->findObject(GetRunNumber(), 1, tag);
84 
85  std::string data = object->payloadValue("jsonData");
86  jsonData = nlohmann::json::parse(data);
87  m_jsonData[tag] = jsonData;
88 
89  } catch (std::exception& e) {
90  std::cerr << "Error: Failed loading payload for this tag/run-> " << e.what() << '\n';
91  }
92  }
93  return jsonData;
94 }
95 
96 bool
98 IsConnected() const
99 {
101 }
102 
103 long
105 GetRunNumber() const
106 {
107  return m_runNumber;
108 }
109 
110 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
dqi::DatabaseConfig::m_folderConnected
bool m_folderConnected
Definition: DatabaseConfig.h:29
dqi::DatabaseConfig::m_database
cool::IDatabasePtr m_database
Definition: DatabaseConfig.h:33
json
nlohmann::json json
Definition: HistogramDef.cxx:9
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
dqi::DatabaseConfig::GetRunNumber
long GetRunNumber() const
Definition: DatabaseConfig.cxx:105
dqi::DatabaseConfig::DatabaseConfig
DatabaseConfig(std::string connectionString, long runNumber)
Definition: DatabaseConfig.cxx:21
dqi::DatabaseConfig::IsConnected
bool IsConnected() const
Definition: DatabaseConfig.cxx:98
dqi::DatabaseConfig::m_connectionString
const std::string m_connectionString
Definition: DatabaseConfig.h:25
calibdata.exception
exception
Definition: calibdata.py:496
CaloNoise_fillDB.dbSvc
dbSvc
Definition: CaloNoise_fillDB.py:108
dqi::DatabaseConfig::GetPayload
nlohmann::json GetPayload(const std::string &tag)
Definition: DatabaseConfig.cxx:65
dqi::DatabaseConfig::m_dbConnected
bool m_dbConnected
Definition: DatabaseConfig.h:28
dqi::DatabaseConfig::Connect
void Connect()
Definition: DatabaseConfig.cxx:42
dqi::DatabaseConfig::m_folder
cool::IFolderPtr m_folder
Definition: DatabaseConfig.h:32
dqi::DatabaseConfig::LoadPayload
nlohmann::json LoadPayload(const std::string &tag)
Definition: DatabaseConfig.cxx:78
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
dqi::DatabaseConfig::m_jsonData
std::unordered_map< std::string, nlohmann::json > m_jsonData
Definition: DatabaseConfig.h:31
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
dqi
Definition: CompositeAlgorithm.h:16
dqi::DatabaseConfig::m_runNumber
const long m_runNumber
Definition: DatabaseConfig.h:26
dqi::DatabaseConfig::Disconnect
void Disconnect()
Definition: DatabaseConfig.cxx:31
DatabaseConfig.h