ATLAS Offline Software
TrigDBLoader.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "./TrigDBHelper.h"
7 
8 #include "CoralBase/Exception.h"
9 #include "CoralBase/Blob.h"
10 
11 #include "RelationalAccess/ConnectionService.h"
12 #include "RelationalAccess/IConnectionServiceConfiguration.h"
13 #include "RelationalAccess/ISessionProxy.h"
14 #include "RelationalAccess/ISchema.h"
15 
16 #include "boost/property_tree/ptree.hpp"
17 #include <fstream>
18 
20 
21 TrigConf::TrigDBLoader::TrigDBLoader(const std::string & loaderName, const std::string & connection) :
22  TrigConfMessaging(loaderName),
23  m_connection(connection)
24 {}
25 
26 // Destructor defined here because QueryDefinition is an incomplete type in the header
28 
29 namespace {
30  bool startswith(const std::string& str, const std::string& sub) {
31  if(str.size()<sub.size())
32  return false;
33  return (str.compare(0,sub.size(),sub) == 0);
34  }
35 }
36 
37 size_t
38 TrigConf::TrigDBLoader::schemaVersion(coral::ISessionProxy* session) const {
39 
40  static const std::string versionTagPrefix("Trigger-Run3-Schema-v");
41 
42  // if database has no schema version, then we return 0
43  if(! session->nominalSchema().existsTable("TRIGGER_SCHEMA") ) {
44  throw std::runtime_error( "Trigger schema has no schema version table" );
45  }
46 
48  // tables
49  qdef.addToTableList ( "TRIGGER_SCHEMA" );
50  // attributes
51  qdef.extendOutput<std::string>( "TS_TAG" );
52 
53  auto query = qdef.createQuery( session );
54  auto & cursor = query->execute();
55  if ( ! cursor.next() ) {
56  throw std::runtime_error( "Trigger schema has schema version table but it is empty" );
57  }
58 
59  const coral::AttributeList& row = cursor.currentRow();
60  std::string versionTag = row["TS_TAG"].data<std::string>();
61  if( ! startswith(versionTag, versionTagPrefix)) {
62  throw std::runtime_error( "Tag format error: Trigger schema version tag " + versionTag + "does not start with " + versionTagPrefix);
63  }
64 
65  std::string vstr = versionTag.substr(versionTagPrefix.size()); // the part of the string containing the version
66  size_t schemaVersion{0};
67  try {
68  schemaVersion = std::stoi(vstr);
69  }
70  catch (const std::invalid_argument& ia) {
71  TRG_MSG_ERROR("Invalid argument when interpreting the version part " << vstr << " of schema tag " << versionTag << ". " << ia.what());
72  throw;
73  }
74 
75  TRG_MSG_INFO("TriggerDB schema version: " << schemaVersion);
76  return schemaVersion;
77 }
78 
79 bool
81 {
82  if( outFileName.empty() ) {
83  return true;
84  }
85  std::ofstream outFile;
86  outFile.open( outFileName, std::ofstream::binary );
87  outFile.write( static_cast<const char*> ( data.startingAddress()), data.size() );
88  outFile.close();
89  TRG_MSG_INFO("Wrote file " << outFileName);
90  return true;
91 }
92 
93 std::unique_ptr<coral::ISessionProxy>
95 
96  coral::ConnectionService connSvc;
97  coral::IConnectionServiceConfiguration& csc = connSvc.configuration();
98  csc.setConnectionRetrialPeriod( m_retrialPeriod );
99  csc.setConnectionRetrialTimeOut( m_retrialTimeout );
100  csc.setConnectionTimeOut( m_connectionTimeout );
101 
102  /* TODO
103  if(csc.replicaSortingAlgorithm() == nullptr) { // likely to be standalone, create our own
104  TRG_MSG_INFO("Create own ReplicaSortingAlgorithm");
105  m_replicaSorter = new TrigConf::ReplicaSorter();
106  csc.setReplicaSortingAlgorithm(*m_replicaSorter);
107  }
108  */
109 
110  TRG_MSG_INFO("Connecting to " << m_connection);
111 
112  auto proxy = std::unique_ptr<coral::ISessionProxy>( connSvc.connect(m_connection, coral::AccessMode::ReadOnly) );
113 
114  TRG_MSG_INFO("Opened session " << m_connection << " with retrialPeriod/retrialTimeout/connectionTimeout: "
115  << m_retrialPeriod << "/" << m_retrialTimeout << "/" << m_connectionTimeout);
116 
117  return proxy;
118 }
119 
120 
123  const std::map<size_t, TrigConf::QueryDefinition> & queries) const
124 {
125  // find the largest version key in the map of defined queries that is <= the schemaVersion
126  size_t maxDefVersion = 0;
127  for(auto & entry : queries) {
128  size_t vkey = entry.first;
129  if(vkey>maxDefVersion and vkey<=schemaVersion) {
130  maxDefVersion = vkey;
131  }
132  }
133  // if nothing found, throw an error
134  if( maxDefVersion==0 ) {
135  TRG_MSG_ERROR("No query for schema version " << schemaVersion << " defined" );
136  throw TrigConf::NoQueryException( "No query available for schema version" + std::to_string(schemaVersion) );
137  }
138  return queries.at(maxDefVersion);
139 }
TRG_MSG_ERROR
#define TRG_MSG_ERROR(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:29
query_example.row
row
Definition: query_example.py:24
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:392
ptree
boost::property_tree::ptree ptree
Definition: TrigDBLoader.cxx:19
TrigConf::TrigDBLoader::schemaVersion
size_t schemaVersion(coral::ISessionProxy *session) const
access to TriggerDB schema version
Definition: TrigDBLoader.cxx:38
python.subdetectors.tile.Blob
Blob
Definition: tile.py:17
TrigDBLoader.h
Loader class for Trigger configuration from the Trigger DB.
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
TrigConf::QueryDefinition
Definition: TrigDBHelper.h:28
TrigConf::QueryDefinition::extendOutput
void extendOutput(const std::string &fieldName)
Definition: TrigDBHelper.h:65
query
Definition: query.py:1
DumpGeoConfig.outFileName
string outFileName
Definition: DumpGeoConfig.py:238
TrigConf::QueryDefinition::createQuery
std::unique_ptr< coral::IQuery > createQuery(coral::ISessionProxy *session)
Definition: TrigDBHelper.cxx:10
TrigConf::NoQueryException
Definition: Trigger/TrigConfiguration/TrigConfIO/TrigConfIO/Exceptions.h:26
TRG_MSG_INFO
#define TRG_MSG_INFO(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:27
DQPostProcessTest.outFile
outFile
Comment Out Those You do not wish to run.
Definition: DQPostProcessTest.py:37
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
ptree
boost::property_tree::ptree ptree
Definition: JsonFileLoader.cxx:16
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
TrigConf::TrigConfMessaging
Class to provide easy access to TrigConf::MsgStream for TrigConf classes.
Definition: TrigConfMessaging.h:28
query_example.cursor
cursor
Definition: query_example.py:21
TrigConf::TrigDBLoader::writeRawFile
bool writeRawFile(const coral::Blob &data, const std::string &outFileName) const
write data blob into file This can be used to write the DB content to file without going through a pt...
Definition: TrigDBLoader.cxx:80
TrigConf::TrigDBLoader::~TrigDBLoader
virtual ~TrigDBLoader() override
Destructor - cannot be defined here because QueryDefinition is an incomplete type.
TrigDBHelper.h
str
Definition: BTagTrackIpAccessor.cxx:11
TrigConf::TrigDBLoader::createDBSession
std::unique_ptr< coral::ISessionProxy > createDBSession() const
create (if needed) DB session and return the session proxy
Definition: TrigDBLoader.cxx:94
TrigConf::QueryDefinition::addToTableList
void addToTableList(const std::string &table, const std::string &table_short="")
Definition: TrigDBHelper.cxx:42
TrigConf::TrigDBLoader::getQueryDefinition
QueryDefinition getQueryDefinition(size_t schemaVersion, const std::map< size_t, QueryDefinition > &queries) const
return query for given schemaVersion from possible queries
Definition: TrigDBLoader.cxx:122
TrigConf::TrigDBLoader::TrigDBLoader
TrigDBLoader(const std::string &loaderName, const std::string &connection)
Constructor.
Definition: TrigDBLoader.cxx:21