79#include "TRestMySQLToAnalysisProcess.h"
83#include <mysql/mysql.h>
121 SetName(this->ClassName());
122 SetTitle(
"Default config");
153 <<
"TRestMySQLToAnalysisProcess::InitProcess. No data base field entries have been specified!"
155 RESTWarning <<
"This process will do nothing!" <<
RESTendl;
167 RESTWarning <<
"TRestMySQLToAnalysisProcess. REST was compiled without mySQL support" <<
RESTendl;
168 RESTWarning <<
"This process will not be funcional" <<
RESTendl;
179 RESTDebug <<
"TRestMySQLToAnalysisProcess. Ev ID : " <<
fEvent->GetID() <<
RESTendl;
180 RESTDebug <<
"TRestMySQLToAnalysisProcess. Get timestamp : " <<
fEvent->GetTime() <<
RESTendl;
191 RESTWarning <<
"REST was not linked to SQL libraries. Run cmake using -DREST_SQL=ON" <<
RESTendl;
192 RESTWarning <<
"Clearing process metadata info." <<
RESTendl;
193 RESTWarning <<
"Please, remove this process from the data chain or enable support for MySQL."
220 RESTError <<
"TRestMySQLToAnalysisProcess. Database server name not found!" <<
RESTendl;
223 if (
fDBName ==
"") RESTError <<
"TRestMySQLToAnalysisProcess. Database not found!" <<
RESTendl;
227 RESTError <<
"TRestMySQLToAnalysisProcess. Database user name not found!" <<
RESTendl;
231 RESTError <<
"TRestMySQLToAnalysisProcess. Database user password not found!" <<
RESTendl;
235 RESTError <<
"TRestMySQLToAnalysisProcess. Database table name not found!" <<
RESTendl;
255 RESTMetadata <<
"SQL data extracted from:" <<
RESTendl;
260 RESTMetadata <<
"List of variables added to the analysis tree" <<
RESTendl;
261 RESTMetadata <<
" ------------------------------------------ " <<
RESTendl;
283 MYSQL* conn = mysql_init(
nullptr);
284 if (conn ==
nullptr) {
285 RESTError <<
"TRestMySQLToAnalysisProcess::InitProcess. mysql_init() failed" <<
RESTendl;
290 fDBName.c_str(), 0,
nullptr, 0)) {
291 RESTError <<
"TRestMySQLToAnalysisProcess::InitProcess. Connection to DB failed!" <<
RESTendl;
292 RESTError << mysql_error(conn) <<
RESTendl;
298 if (mysql_query(conn, sqlQuery.c_str())) {
299 RESTError <<
"Error making query to SQL database" <<
RESTendl;
300 RESTError << mysql_error(conn) <<
RESTendl;
301 RESTError <<
"Query string : " << sqlQuery <<
RESTendl;
305 MYSQL_RES* result = mysql_store_result(conn);
307 if (result ==
nullptr) {
308 RESTError <<
"Error getting result from SQL query" <<
RESTendl;
309 RESTError << mysql_error(conn) <<
RESTendl;
310 RESTError <<
"Query string : " << sqlQuery <<
RESTendl;
314 int num_fields = mysql_num_fields(result);
315 int num_rows = mysql_num_rows(result);
321 this->
SetError(
"No DB entries found for the run period!");
328 std::vector<std::vector<Double_t>> data;
330 while ((row = mysql_fetch_row(result))) {
331 std::vector<Double_t> dataRow;
332 for (
int i = 0; i < num_fields; i++) {
341 data.push_back(dataRow);
346 for (
int j = 0; j < data.size(); j++) {
348 for (
int n = 1; n < data.front().size(); n++) {
353 for (
int n = 1; n < data[j].size(); n++) {
359 RESTDebug <<
"Raw data size " << data.size() <<
RESTendl;
367 while (timeNow > data[dbEntry][0] && dbEntry + 1 < data.size()) dbEntry++;
369 std::vector<Double_t> dataBuff;
370 if (timeNow < data.front()[0]) {
371 for (
int n = 1; n < data.front().size(); n++) dataBuff.push_back(data.front()[n]);
372 }
else if (timeNow > data.back()[0]) {
373 for (
int n = 1; n < data.back().size(); n++) dataBuff.push_back(data.back()[n]);
375 for (
int n = 1; n < data[dbEntry].size(); n++) {
376 double y2 = data[dbEntry][n];
377 double y1 = data[dbEntry - 1][n];
380 double x2 = data[dbEntry][0];
381 double x1 = data[dbEntry - 1][0];
383 double m = (y2 - y1) / (x2 - x1);
384 double l = y1 - m * x1;
386 dataBuff.push_back(m * timeNow + l);
400 RESTWarning <<
"REST was not linked to SQL libraries. Run cmake using -DREST_SQL=ON" <<
RESTendl;
408 string sqlQuery =
"SELECT timestamp";
413 sqlQuery +=
" FROM " +
fDBTable +
" where timestamp between ";
418 sqlQuery += startStr +
" and " + endStr;
432 if (bin < 0)
return fDBdata.front()[index];
433 if ((
unsigned int)(bin + 1) >=
fDBdata.size())
return fDBdata.back()[index];
435 double y2 =
fDBdata[bin + 1][index];
436 double y1 =
fDBdata[bin][index];
441 return m * timestamp + l;
void BeginPrintProcess()
[name, cut range]
TRestRun * fRunInfo
< Pointer to TRestRun object where to find metadata.
void SetObservableValue(const std::string &name, const T &value)
Set observable value for AnalysisTree.
A base class for any REST event.
This process connects to a SQL database and adds new observables inside the analysis tree.
std::vector< TString > fAnaTreeVariables
The name of the varibles will be renamed to these values in the analysisTree.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
Double_t fSampling
Defines the internal sampling of the retrieved data for quick access.
std::string fDBServerName
The host indentification name or IP.
void InitFromConfigFile() override
Function reading input parameters from the RML TRestMySQLToAnalysisProcess section.
Double_t GetDBValueAtTimestamp(Int_t index, Double_t timestamp)
This method will retrieve the given data field component, specified by the argument index at the give...
std::string BuildQueryString()
Dedicated method to help building the SQL query string.
Double_t fStartTimestamp
Stores the start timestamp used to extract the SQL data.
TRestEvent * fEvent
A pointer to the event data.
std::string fDBTable
The database table.
std::vector< Double_t > fMinValues
The minimum value of the corresponding extracted field variable.
void InitProcess() override
Function to use in initialization of process members before starting to process the event.
std::string fDBName
The database name.
void FillDBArrays()
This method is the one accessing the SQL database and filling the internal arrays with data that will...
void PrintMetadata() override
It prints out basic information of the SQL database used to generate the analysis tree observables....
void Initialize() override
Function to initialize input/output event members and define the section name and library version.
TRestMySQLToAnalysisProcess()
Default constructor.
~TRestMySQLToAnalysisProcess()
Default destructor.
Double_t fEndTimestamp
Stores the end timestamp used to extract the SQL data.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
Bool_t fCheckSQL
A boolean to output a warning message just once inside the process.
std::vector< Double_t > fMaxValues
The maximum value of the corresponding extracted field variable.
Bool_t fDataBaseExists
It will be true in case the database found some entries for the timestamp range.
std::string fDBUserName
The database username.
std::vector< std::vector< Double_t > > fDBdata
A matrix containing extracted SQL data with fixed time bin.
std::string fDBUserPass
The database user password.
std::vector< TString > fSQLVariables
The name of the entries defined by user that will be retrieved at the SQL database.
@ REST_Extreme
show everything
@ REST_Debug
+show the defined debug messages
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
Double_t StringToDouble(std::string in)
Gets a double from a string.