SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
AbsDataLoader.cpp
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 // STL
12 #include <iostream>
13 
14 // Local
15 #include "SQLamarr/AbsDataLoader.h"
16 #include "SQLamarr/db_functions.h"
17 #include "SQLamarr/preprocessor_symbols.h"
18 
19 namespace SQLamarr
20 {
21  //==========================================================================
22  // insert_event
23  //==========================================================================
25  const std::string& datasource,
26  uint64_t run_number,
27  uint64_t evt_number
28  )
29  {
30  sqlite3_stmt* stmt = get_statement ("insert_event",
31  "INSERT INTO DataSources(datasource, run_number, evt_number) "
32  "VALUES (?, ?, ?); "
33  );
34 
35  sqlite3_bind_text (stmt, 1, datasource.c_str(), datasource.length()+1, SQLITE_TRANSIENT);
36  sqlite3_bind_int64(stmt, 2, run_number);
37  sqlite3_bind_int64(stmt, 3, evt_number);
38 
39  exec_stmt(stmt);
40 
41  return last_insert_row();
42  }
43 
44 
45  //==========================================================================
46  // insert_collision
47  //==========================================================================
49  int datasource_id,
50  int collision,
51  float t,
52  float x,
53  float y,
54  float z
55  )
56  {
57  sqlite3_stmt* stmt = get_statement ("insert_collision",
58  "INSERT INTO GenEvents(datasource_id, collision, x, y, z, t) "
59  "VALUES (?, ?, ?, ?, ?, ?) "
60  );
61 
62  sqlite3_bind_int(stmt, 1, datasource_id);
63  sqlite3_bind_int(stmt, 2, collision);
64  sqlite3_bind_double(stmt, 3, x);
65  sqlite3_bind_double(stmt, 4, y);
66  sqlite3_bind_double(stmt, 5, z);
67  sqlite3_bind_double(stmt, 6, t);
68 
69  exec_stmt(stmt);
70 
71  return last_insert_row();
72  }
73 
74 
75  //==========================================================================
76  // insert_vertex
77  //==========================================================================
79  int genevent_id,
80  int hepmc_id,
81  int status,
82  float t,
83  float x,
84  float y,
85  float z,
86  bool is_primary
87  )
88  {
89  sqlite3_stmt* stmt = get_statement ("insert_vertex",
90  "INSERT INTO GenVertices"
91  " (genevent_id, hepmc_id, status, x, y, z, t, is_primary) "
92  "VALUES (?, ?, ?, ?, ?, ?, ?, ?) "
93  );
94 
95  sqlite3_bind_int(stmt, 1, genevent_id);
96  sqlite3_bind_int(stmt, 2, hepmc_id);
97  sqlite3_bind_int(stmt, 3, status);
98  sqlite3_bind_double(stmt, 4, x);
99  sqlite3_bind_double(stmt, 5, y);
100  sqlite3_bind_double(stmt, 6, z);
101  sqlite3_bind_double(stmt, 7, t);
102  sqlite3_bind_int(stmt, 8, int(is_primary));
103 
104  exec_stmt(stmt);
105 
106  return last_insert_row();
107  }
108 
109  //==========================================================================
110  // insert_particle
111  //==========================================================================
113  int genevent_id,
114  int hepmc_id,
115  int production_vertex,
116  int end_vertex,
117  int pid,
118  int status,
119  float pe,
120  float px,
121  float py,
122  float pz,
123  float m
124  )
125  {
126  sqlite3_stmt* stmt = get_statement ("insert_particle", R"(
127  INSERT INTO GenParticles(
128  genevent_id, hepmc_id,
129  production_vertex, end_vertex,
130  pid, status,
131  pe, px, py, pz, m
132  )
133  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); "
134  )");
135 
136  int iVar = 1;
137  sqlite3_bind_int(stmt, iVar++, genevent_id);
138  sqlite3_bind_int(stmt, iVar++, hepmc_id);
139 
140  if (production_vertex != LAMARR_BAD_INDEX)
141  sqlite3_bind_int(stmt, iVar++, production_vertex);
142  else
143  {
144  sqlite3_bind_null(stmt, iVar++);
145  }
146 
147  if (end_vertex != LAMARR_BAD_INDEX)
148  sqlite3_bind_int(stmt, iVar++, end_vertex);
149  else
150  sqlite3_bind_null(stmt, iVar++);
151 
152  sqlite3_bind_int(stmt, iVar++, pid);
153  sqlite3_bind_int(stmt, iVar++, status);
154  sqlite3_bind_double(stmt, iVar++, pe);
155  sqlite3_bind_double(stmt, iVar++, px);
156  sqlite3_bind_double(stmt, iVar++, py);
157  sqlite3_bind_double(stmt, iVar++, pz);
158  sqlite3_bind_double(stmt, iVar++, m);
159 
160  exec_stmt(stmt);
161 
162  return last_insert_row();
163  }
164 }
165 
166 
int insert_collision(int datasource_id, int collision, float t, float x, float y, float z)
Insert a collision in the GenEvent table.
int insert_particle(int genevent_id, int hepmc_id, int production_vertex, int end_vertex, int pid, int status, float pe, float px, float py, float pz, float m)
Insert a particle in the GenParticles table.
int insert_vertex(int genevent_id, int hepmc_id, int status, float t, float x, float y, float z, bool is_primary)
Insert a vertex in the GenVertices
int insert_event(const std::string &data_source, uint64_t run_number, uint64_t evt_number)
Insert data source reference in the DataSources table.
sqlite3_stmt * get_statement(const std::string &name, const std::string &query)
Creates or retrieve from cache a statement.
int last_insert_row()
Return the index of the last rows inserted in any table.
bool exec_stmt(sqlite3_stmt *)
Execute a statement, possibly throwing an exception on failure.