21 #include "SQLamarr/preprocessor_symbols.h"
22 #include "SQLamarr/PVReconstruction.h"
23 #include "SQLamarr/GlobalPRNG.h"
24 #include "SQLamarr/SQLiteError.h"
31 std::string condition,
36 sqlite3_bind_text(stmt, 1, condition.c_str(), -1,
nullptr);
37 sqlite3_bind_text(stmt, 2, coord.c_str(), -1,
nullptr);
39 if (sqlite3_step(stmt) != SQLITE_ROW)
42 <<
"Failed query for " << condition
43 <<
" coordinate: " << coord
45 throw SQLiteError(
"Cannot load parametrization line");
49 static_cast<float>(sqlite3_column_double (stmt, 0)),
50 static_cast<float>(sqlite3_column_double (stmt, 1)),
51 static_cast<float>(sqlite3_column_double (stmt, 2)),
52 static_cast<float>(sqlite3_column_double (stmt, 3)),
53 static_cast<float>(sqlite3_column_double (stmt, 4)),
54 static_cast<float>(sqlite3_column_double (stmt, 5))
67 , m_parametrization (parametrization)
73 void PVReconstruction::_sql_rnd_ggg (
74 sqlite3_context *context,
81 const double mu = sqlite3_value_double(argv[0]);
82 const double f1 = sqlite3_value_double(argv[1]);
83 const double f2 = sqlite3_value_double(argv[2]);
84 const double sigma1 = sqlite3_value_double(argv[3]);
85 const double sigma2 = sqlite3_value_double(argv[4]);
86 const double sigma3 = sqlite3_value_double(argv[5]);
89 std::normal_distribution<double> one_g;
90 std::uniform_real_distribution<double> uniform;
91 const double r = uniform(*generator);
92 const double sigma = (
94 r < f1 + f2 ? sigma2 :
98 const double smear = one_g(*generator)*sigma + mu;
100 sqlite3_result_double(context, smear);
103 sqlite3_result_null(context);
111 PVReconstruction::SmearingParametrization
113 const std::string file_path,
114 const std::string table_name,
115 const std::string condition
120 sqlite3_open_v2(file_path.c_str(), &db, SQLITE_OPEN_READONLY,
nullptr);
123 throw SQLiteError(
"Cannot open PVReconstruction DB");
128 mu, f1, f2, sigma1, sigma2, sigma3
135 )", table_name.c_str());
137 sqlite3_stmt* load_stmt;
138 if (sqlite3_prepare_v2(db, query, -1, &load_stmt,
nullptr) != SQLITE_OK)
140 std::cerr << sqlite3_errmsg(db) << std::endl;
150 sqlite3_finalize(load_stmt);
166 INSERT INTO Vertices (
167 mcvertex_id, genevent_id,
170 sigma_x, sigma_y, sigma_z
173 mcv.mcvertex_id, mcv.genevent_id,
175 mcv.x + rnd_ggg(?, ?, ?, ?, ?, ?),
176 mcv.y + rnd_ggg(?, ?, ?, ?, ?, ?),
177 mcv.z + rnd_ggg(?, ?, ?, ?, ?, ?),
181 FROM MCVertices AS mcv
182 WHERE mcv.is_primary == TRUE
186 sqlite3_bind_int(reco_pv, slot_id++, LAMARR_VERTEX_PRIMARY);
188 for (
int iCoord = 0; iCoord < 3; ++iCoord)
190 sqlite3_bind_double(reco_pv, slot_id++, m_parametrization.
data[iCoord].mu);
191 sqlite3_bind_double(reco_pv, slot_id++, m_parametrization.
data[iCoord].f1);
192 sqlite3_bind_double(reco_pv, slot_id++, m_parametrization.
data[iCoord].f2);
193 sqlite3_bind_double(reco_pv, slot_id++, m_parametrization.
data[iCoord].sigma1);
194 sqlite3_bind_double(reco_pv, slot_id++, m_parametrization.
data[iCoord].sigma2);
195 sqlite3_bind_double(reco_pv, slot_id++, m_parametrization.
data[iCoord].sigma3);
198 for (
int iCoord = 0; iCoord < 3; ++iCoord)
200 float min_sigma = m_parametrization.
data[iCoord].sigma1;
201 if (m_parametrization.
data[iCoord].sigma2 < min_sigma)
202 min_sigma = m_parametrization.
data[iCoord].sigma2;
203 if (m_parametrization.
data[iCoord].sigma3 < min_sigma)
204 min_sigma = m_parametrization.
data[iCoord].sigma3;
206 sqlite3_bind_double(reco_pv, slot_id++, min_sigma);
Abstract interface with helper functions to access an SQLite DB.
sqlite3_stmt * get_statement(const std::string &name, const std::string &query)
Creates or retrieve from cache a statement.
bool exec_stmt(sqlite3_stmt *)
Execute a statement, possibly throwing an exception on failure.
void using_sql_function(const std::string &name, int argc, void(*xFunc)(sqlite3_context *, int, sqlite3_value **))
Register a static function in DB, enabling usage from SQL.
static SmearingParametrization load_parametrization(const std::string file_path, const std::string table_name, const std::string condition)
Instanciate a SmearingParametrization object from an SQLite file.
void execute() override
Execute the algorithm adding primary vertices to Vertices table.
PVReconstruction(SQLite3DB &db, const SmearingParametrization ¶metrization)
Constructor.
static PRNG * get_or_create(const sqlite3_context *db, uint64_t seed=no_seed)
Return a pointer to an initialized generator.
A database connection handler easying sharing the DB between C++ and Python.
PVReconstruction::SmearingParametrization_1D _get_param_line(sqlite3_stmt *stmt, std::string condition, std::string coord)
Set of parameters defining a 3-Gaussian resolution function in 1D.
Set of parmeters defining three 3-Gaussian functions for x, y and z.
std::array< SmearingParametrization_1D, 3 > data
Access to raw data in array format.
SmearingParametrization_1D & x()
Read/Write access to coordinate parametrization.
SmearingParametrization_1D & y()
Read/Write access to coordinate parametrization.
SmearingParametrization_1D & z()
Read/Write access to coordinate parametrization.