GEMC  2.2
Geant4 Monte-Carlo Framework
mysql_parameters.cc
Go to the documentation of this file.
1 // Qt headers
2 #include <QtSql>
3 
4 // gemc headers
5 #include "parameter_factory.h"
6 #include "mysql_parameters.h"
7 #include "string_utilities.h"
8 #include "utils.h"
9 
11 {
12  string hd_msg = " > MYSQL Parameters: >> ";
13  double verbosity = opts.optMap["PARAMETER_VERBOSITY"].arg;
14 
15  map<string, double> GParameters; // parameters maps
16  // first check if there's at least one detector with MYSQL factory
18  return GParameters;
19 
20  // connection to the DB
21  openGdb(opts);
22  QSqlDatabase db = QSqlDatabase::database("gemc");
23 
24  // Looping over detectorConditionsMap for detector names
25  // To each detector is associated a material table and an opt properties table
26  for(map<string, detectorCondition>::iterator it=RC.detectorConditionsMap.begin(); it != RC.detectorConditionsMap.end(); it++)
27  {
28  if(it->second.get_factory() != factoryType)
29  continue;
30 
31  string dname = it->first ;
32  string tname = dname + "__parameters";
33  string variation = get_variation(it->second.get_variation());
34  if(is_main_variation(it->second.get_variation()))
35  tname += "_main";
36  int run = it->second.get_run_number();
37 
38  if(verbosity)
39  cout << hd_msg << " Importing Parameters for Detector: " << dname
40  << " with " << factoryType << " factory, variation " << variation << endl;
41 
42  string dbexecute = "select name, value, units, description from " + tname;
43  dbexecute += " where variation ='" + variation + "'";
44  dbexecute += " and rmin <= " + stringify(run) + " and rmax >= " + stringify(run) ;
45  dbexecute += " order by id desc limit 1 " ;
46 
47  // executing query - will exit if not successfull.
48  QSqlQuery q;
49  if(!q.exec(dbexecute.c_str()))
50  {
51  cout << hd_msg << " Errror! Failed to execute MYSQL query " << dbexecute << endl;
52  exit(0);
53  }
54  // Warning if nothing is found
55  if(q.size() == 0 && verbosity)
56  {
57  cout << " ** WARNING: detector \"" << dname << "\" not found with variation \"" << variation << "\" for run number " << run << endl << endl;
58  }
59 
60  while (q.next())
61  {
62 
63  gtable gt;
64  gt.add_data(dname + "/" + TrimSpaces(qv_tostring(q.value(0))));
65  gt.add_data(q.value(1));
66  gt.add_data(q.value(2));
67  gt.add_data(q.value(3));
68 
69  GParameters[gt.data[0]] = get_par_value(gt);
70 
71  if(verbosity > 1)
73  }
74  }
75 
76  // closing DB connection
77  db.close();
78  QSqlDatabase::removeDatabase("QMYSQL");
79 
80  return GParameters;
81 }
int check_if_factory_is_needed(map< string, detectorCondition > dcon, string factory)
double get_par_value(gtable gt)
QSqlDatabase openGdb(goptions gemcOpt)
Definition: utils.cc:220
vector< string > data
Definition: utils.h:76
Definition: utils.h:65
double verbosity
string get_variation(string var)
parse variation name from string
string qv_tostring(QVariant input)
void add_data(QVariant input)
Definition: utils.h:78
map< string, detectorCondition > detectorConditionsMap
map< string, double > loadParameters(goptions, runConditions)
string stringify(double x)
map< string, aopt > optMap
Options map.
Definition: options.h:71
bool is_main_variation(string var)
returns 1 if the string "main:" is found on the input
string TrimSpaces(string in)
Removes leading and trailing spaces.
void log_value(gtable gt, string factory)