GEMC  2.2
Geant4 Monte-Carlo Framework
mysql_det_factory.cc
Go to the documentation of this file.
1 // Qt headers
2 #include <QtSql>
3 
4 // gemc headers
5 #include "mysql_det_factory.h"
6 #include "utils.h"
7 
8 map<string, detector> mysql_det_factory::loadDetectors()
9 {
10  string hd_msg = " > MYSQL Detector Factory: >> ";
11  double verbosity = gemcOpt.optMap["GEO_VERBOSITY"].arg;
12  string catch_v = gemcOpt.optMap["CATCH"].args;
13 
14  map<string, detector> dets;
15 
16  // first check if there's at least one detector with MYSQL factory
18  return dets;
19 
20  // connection to the DB
21  QSqlDatabase db = openGdb(gemcOpt);
22 
23  // connection is ok. Loading tables now
24  // building detectors that are tagged with MYSQL factory
25  for(map<string, detectorCondition>::iterator it=RC.detectorConditionsMap.begin(); it != RC.detectorConditionsMap.end(); it++)
26  {
27  if(it->second.get_factory() != factoryType)
28  continue;
29 
30  string dname = it->first;
31  string tname = dname + "__geometry";
32  string variation = get_variation(it->second.get_variation());
33  if(is_main_variation(it->second.get_variation()))
34  tname += "_main";
35  int run = it->second.get_run_number();
36 
37  if(verbosity)
38  cout << hd_msg << " Importing Detector: " << dname << " with <" << factoryType
39  << "> factory, variation \"" << variation << "\", run number: " << run << endl;
40 
41  // getting the last id entry that matches variation and run number range
42  int last_id = getLastId(db, tname, variation, run);
43 
44  string dbexecute = "select name, mother, description, pos, rot, col, type, ";
45  dbexecute += "dimensions, material, magfield, ncopy, pmany, exist, ";
46  dbexecute += "visible, style, sensitivity, hitType, identity from " + tname;
47  dbexecute += " where variation ='" + variation + "'";
48  dbexecute += " and rmin <= " + stringify(run) + " and rmax >= " + stringify(run) ;
49  dbexecute += " and id = " + stringify(last_id);
50 
51  // executing query - will exit if not successfull.
52  QSqlQuery q;
53  if(!q.exec(dbexecute.c_str()))
54  {
55  cout << hd_msg << " !!! Failed to execute MYSQL query " << dbexecute << ". This is a fatal error. Exiting." << endl;
56  qDebug() << q.lastError();
57  exit(0);
58  }
59  // Warning if nothing is found
60  if(q.size() == 0 && verbosity)
61  {
62  cout << " ** WARNING: detector \"" << dname << "\" not found with variation \"" << variation << "\" for run number " << run << endl << endl;
63  }
64 
65 
66  // else loading parameters from DB
67  while (q.next())
68  {
69  gtable gt;
70 
71  // there should be 18 values in the MYSQL table
72  for(int i=0; i<18; i++)
73  gt.add_data(q.value(i));
74 
75  // adding additional info: system, factory, variation, run number
76  gt.add_data(dname);
77  gt.add_data((string) "MYSQL");
78  gt.add_data(variation);
79  gt.add_data(stringify(run));
80 
81 
82  // big warning if detector already exist
83  // detector is NOT loaded if already existing
84  if(dets.find(gt.data[0]) != dets.end())
85  {
86  cout << endl << " *** WARNING! A detector >" << gt.data[0] << " exists already. Keeping original, not loading this instance. " << endl << endl;
87  }
88  else
89  {
90  dets[gt.data[0]] = get_detector(gt, gemcOpt, RC);
91  }
92 
93  if(verbosity > 3)
94  cout << get_detector(gt, gemcOpt, RC);
95  }
96  }
97 
98 
99  // closing DB connection
100  closeGdb(db);
101  cout << endl;
102 
103  return dets;
104 }
105 
106 
107 
108 
109 
110 
111 
112 
int check_if_factory_is_needed(map< string, detectorCondition > dcon, string factory)
void closeGdb(QSqlDatabase db)
Definition: utils.cc:254
QSqlDatabase openGdb(goptions gemcOpt)
Definition: utils.cc:220
vector< string > data
Definition: utils.h:76
Definition: utils.h:65
int getLastId(QSqlDatabase db, string t, string v, int run)
Definition: utils.cc:190
double verbosity
string get_variation(string var)
parse variation name from string
void add_data(QVariant input)
Definition: utils.h:78
map< string, detectorCondition > detectorConditionsMap
runConditions RC
string stringify(double x)
map< string, aopt > optMap
Options map.
Definition: options.h:71
map< string, detector > loadDetectors()
bool is_main_variation(string var)
returns 1 if the string "main:" is found on the input
detector get_detector(gtable gt, goptions go, runConditions RC)