ATLAS Offline Software
Loading...
Searching...
No Matches
TrigDBHelper.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2
3
4#include "./TrigDBHelper.h"
5
7
8#include "RelationalAccess/ISchema.h"
9#include <iostream>
10#include <format>
11
12std::unique_ptr< coral::IQuery >
13TrigConf::QueryDefinition::createQuery( coral::ISessionProxy * session )
14{
15
16 std::unique_ptr< coral::IQuery > query( session->nominalSchema().newQuery() );
17
18 // set tables
19 for( auto & table : m_tables ) {
20 query->addToTableList( table.first, table.second );
21 }
22
23 // ensure that all the requested variables are bound
24 for( auto & bindVar : m_bindList ) {
25 const std::string & fieldName = bindVar.specification().name();
26 if( m_bound.find( fieldName ) == m_bound.end() ) {
27 throw std::runtime_error( "Column " + fieldName + " has been bound, but not set to a value" );
28 }
29 }
30
31 // condition
32 query->setCondition( m_condition, m_bindList );
33
34 // output
35 query->defineOutput( m_attList );
36 for( const coral::Attribute & attr : m_attList ) {
37 query->addToOutputList(attr.specification().name());
38 }
39
40 return query;
41}
42
43void
44TrigConf::QueryDefinition::addToTableList(const std::string & table, const std::string & table_short)
45{
46 m_tables.emplace_back(table, table_short);
47}
48
49void
50TrigConf::QueryDefinition::extendCondition(const std::string & condext) {
51 if( m_condition.size()>0 && condext.size()>0 && m_condition.back() != ' ' && condext[0] != ' ') {
52 m_condition += " ";
53 }
54 m_condition += condext;
55}
56
57void
58TrigConf::blobToPtree( const coral::Blob & blob, boost::property_tree::ptree & pt ) {
59 boost::iostreams::stream<boost::iostreams::array_source> stream( static_cast<const char*> ( blob.startingAddress()),
60 blob.size());
61 boost::property_tree::read_json(stream, pt);
62}
63
64void
65TrigConf::stringToPtree( const std::string & json_string, boost::property_tree::ptree & pt ) {
66 boost::iostreams::array_source source(json_string.data(), json_string.size());
67 boost::iostreams::stream<boost::iostreams::array_source> stream(source);
68 boost::property_tree::read_json(stream, pt);
69}
70
71void
72TrigConf::writeRawFile(const coral::Blob & data, const std::string & outFileName)
73{
74 try {
75 std::ofstream outFile(outFileName, std::ofstream::binary);
76 if (!outFile) {
77 throw FileWritingException(std::format("Failed to open file {} for writing", outFileName));
78 }
79 outFile.write( static_cast<const char*> ( data.startingAddress()), data.size() );
80 if (!outFile) {
81 throw FileWritingException("Failed to write data to file " + outFileName);
82 }
83 outFile.close();
84 } catch (const std::exception& e) {
85 throw FileWritingException(std::format("Exception while writing file {} : {}", outFileName, e.what()));
86 }
87}
88
89void
90TrigConf::writeRawFile(const std::string & data, const std::string & outFileName)
91{
92 try {
93 std::ofstream outFile(outFileName, std::ofstream::binary);
94 if (!outFile) {
95 throw FileWritingException(std::format("Failed to open file {} for writing", outFileName));
96 }
97 outFile.write(data.data(), data.size());
98 if (!outFile) {
99 throw FileWritingException("Failed to write data to file " + outFileName);
100 }
101 outFile.close();
102 } catch (const std::exception& e) {
103 throw FileWritingException(std::format("Exception while writing file {} : {}", outFileName, e.what()));
104 }
105}
106
107
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
std::vector< std::pair< std::string, std::string > > m_tables
std::unique_ptr< coral::IQuery > createQuery(coral::ISessionProxy *session)
coral::AttributeList m_bindList
void extendCondition(const std::string &condext)
std::set< std::string > m_bound
void addToTableList(const std::string &table, const std::string &table_short="")
coral::AttributeList m_attList
void stringToPtree(const std::string &json_string, boost::property_tree::ptree &pt)
void writeRawFile(const coral::Blob &data, const std::string &outFileName)
write coral data blob to file
void blobToPtree(const coral::Blob &blob, boost::property_tree::ptree &pt)
Definition query.py:1