SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
BasePlugin.h
1 // (c) Copyright 2022 CERN for the benefit of the LHCb Collaboration.
2 //
3 // This software is distributed under the terms of the GNU General Public
4 // Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE".
5 //
6 // In applying this licence, CERN does not waive the privileges and immunities
7 // granted to it by virtue of its status as an Intergovernmental Organization
8 // or submit itself to any jurisdiction.
9 
10 
11 #pragma once
12 
13 // Standard C
14 #include <dlfcn.h>
15 
16 // STL
17 #include <vector>
18 #include <string>
19 
20 // SQLamarr
21 #include "SQLamarr/db_functions.h"
22 #include "SQLamarr/BaseSqlInterface.h"
23 #include "SQLamarr/Transformer.h"
24 
25 namespace SQLamarr
26 {
59  class BasePlugin: public BaseSqlInterface, public Transformer
60  {
61  public:
63  BasePlugin (
64  SQLite3DB& db,
66  const std::string& library,
68  const std::string& function_name,
70  const std::string& select_query,
74  const std::string& output_table,
76  const std::vector<std::string> outputs,
80  const std::vector<std::string> reference_keys = {"ref_id"}
82  );
83 
84  BasePlugin (BasePlugin&) = delete;
85 
86  virtual ~BasePlugin() {dlclose(m_handle);}
87 
90  void execute () override;
91 
92  protected:
96  virtual void eval_parametrization (float* output, const float* input) = 0;
97 
98  private: // Properties
99  const std::string m_library;
100  const std::string m_function_name;
101  const std::string m_select_query;
102  const std::string m_output_table;
103  const std::vector<std::string> m_outputs;
104  const std::vector<std::string> m_refkeys;
105 
106  void *m_handle;
107 
108  private: // Methods
109  std::vector<std::string> get_column_names() const;
110  std::string compose_delete_query();
111  std::string compose_create_query();
112  std::string compose_insert_query();
113 
114  protected:
116  template <typename Func_t> Func_t load_func (const std::string& fname);
117  };
118 
119 
120  // Templated implementation
121  template <typename Func_t>
122  Func_t BasePlugin::load_func (const std::string& fname)
123  {
124  Func_t ret = Func_t(dlsym(m_handle, fname.c_str()));
125 
126  if (!ret)
127  {
128  std::cerr << "Failure while loading " << fname << std::endl;
129  throw std::runtime_error("Failed loading function");
130  }
131 
132  return ret;
133  }
134 }
Interface to dynamically linked parametrizations.
Definition: BasePlugin.h:60
Func_t load_func(const std::string &fname)
Load a generic-typed function from an external library.
Definition: BasePlugin.h:122
BasePlugin(BasePlugin &)=delete
virtual ~BasePlugin()
Definition: BasePlugin.h:86
void execute() override
Execute the external function and copies the output in a new table.
Definition: BasePlugin.cpp:124
BasePlugin(SQLite3DB &db, const std::string &library, const std::string &function_name, const std::string &select_query, const std::string &output_table, const std::vector< std::string > outputs, const std::vector< std::string > reference_keys={"ref_id"})
Constructor.
Definition: BasePlugin.cpp:29
virtual void eval_parametrization(float *output, const float *input)=0
Evaluate the external parametrization.
Abstract interface with helper functions to access an SQLite DB.
Interface to an executable class used for polymorphism.
Definition: Transformer.h:17
A database connection handler easying sharing the DB between C++ and Python.
Definition: db_functions.py:24