GEMC  1.8
Geant4 Monte-Carlo Framework
mysql_parameters.cc
Go to the documentation of this file.
1 // %%%%%%%%%%
2 // Qt headers
3 // %%%%%%%%%%
4 #include <QtSql>
5 
6 // %%%%%%%%%%%%%
7 // gemc headers
8 // %%%%%%%%%%%%%
9 #include "parameter_factory.h"
10 #include "mysql_parameters.h"
11 #include "detector.h" // just for TrimSpaces
12 #include "string_utilities.h"
13 
14 
15 
17 {
18  // DB settings
19  string database = opts.args["DATABASE"].args;
20  string dbhost = opts.args["DBHOST"].args;
21  string dbUser = opts.args["DBUSER"].args;
22  string dbPswd = opts.args["DBPSWD"].args;
23  string hd_msg = opts.args["LOG_MSG"].args + " MYSQL Parameters: >> ";
24  double verbos = opts.args["PARAMETER_VERBOSITY"].arg;
25 
26 
27  // connection to the DB
28  QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
29  db.setHostName(dbhost.c_str());
30  db.setDatabaseName(database.c_str());
31  db.setUserName( dbUser.c_str() );
32  db.setPassword( dbPswd.c_str() );
33  bool ok = db.open();
34 
35  if(!ok)
36  {
37  cout << hd_msg << " Cannot connect to database " << database << ". Exiting." << endl;
38  exit(-1);
39  }
40 
41 
42  map<string, double> GParameters; // parameters maps
43  string dbtable = opts.args["GT"].args;
44 
45  if(runConditions.gTab_Vec.size() == 0 && dbtable != "no")
46  runConditions.gTab_Vec.push_back(dbtable);
47 
48 
49 
50  // at this point RunConditions should have something inside
51  for(unsigned int sql_t=0; sql_t< runConditions.gTab_Vec.size(); sql_t++)
52  {
53  string parameter_table = runConditions.gTab_Vec[sql_t] + "__parameters";
54  string dbexecute = "select name, value, units, description from " + parameter_table;
55 
56  // will exit if table cannot be accessed.
57  QSqlQuery q;
58  if(!q.exec(dbexecute.c_str()))
59  {
60  cout << hd_msg << " WARNING! Failed to access DB for table: " << parameter_table << ". Maybe the table doesn't exist? " << endl;
61  }
62 
63  while (q.next())
64  {
65  string pname = runConditions.gTab_Vec[sql_t] + "/" + TrimSpaces(gemc_tostring(q.value(0).toString()));
66  double pvalue = q.value(1).toDouble();
67  string punits = TrimSpaces(gemc_tostring(q.value(2).toString()));
68  string pdescr = gemc_tostring(q.value(3).toString());
69 
70  if( punits == "m") pvalue *= m;
71  else if( punits == "inches") pvalue *= 2.54*cm;
72  else if( punits == "cm") pvalue *= cm;
73  else if( punits == "mm") pvalue *= mm;
74  else if( punits == "um") pvalue *= 1E-6*m;
75  else if( punits == "fm") pvalue *= 1E-15*m;
76  else if( punits == "deg") pvalue *= deg;
77  else if( punits == "arcmin") pvalue = pvalue/60.0*deg;
78  else if( punits == "rad") pvalue *= rad;
79  else if( punits == "mrad") pvalue *= mrad;
80  else if( punits == "eV") pvalue *= eV;
81  else if( punits == "MeV") pvalue *= MeV;
82  else if( punits == "KeV") pvalue *= 0.001*MeV;
83  else if( punits == "GeV") pvalue *= GeV;
84  else if( punits == "T") pvalue *= tesla;
85  else if( punits == "Tesla") pvalue *= tesla;
86  else if( punits == "ns") pvalue *= ns;
87  else if( punits == "na") pvalue *= 1;
88  else cout << punits << ": unit not recognized!!" << endl;
89 
90  GParameters[pname] = pvalue;
91 
92  if(verbos > 1)
93  {
94  cout << hd_msg << " Parameter: " << pname << " with value " << pvalue << " ";
95  if(punits != "na") cout << punits ;
96  cout << " : " << pdescr << endl;
97  }
98 
99  }
100 
101  }
102 
103 
104  // closing DB connection
105  db.close();
106  // need to create empty db before removing the connection
107  db = QSqlDatabase();
108  db.removeDatabase("qt_sql_default_connection");
109  cout << endl;
110 
111 
112 
113  return GParameters;
114 }
vector< string > gTab_Vec
Vector of SQL tables names.
Definition: usage.h:43
string gemc_tostring(QString input)
map< string, opts > args
Options map.
Definition: usage.h:68
map< string, double > initParameters(run_conditions, gemc_opts)
string TrimSpaces(string in)
Removes leading and trailing spaces.