GEMC  2.2
Geant4 Monte-Carlo Framework
mysql_materials.cc
Go to the documentation of this file.
1 // Qt headers
2 #include <QtSql>
3 
4 // gemc headers
5 #include "material_factory.h"
6 #include "mysql_materials.h"
7 #include "string_utilities.h"
8 #include "utils.h"
9 
10 // G4 headers
11 #include "G4Element.hh"
12 #include "G4NistManager.hh"
13 #include "G4OpBoundaryProcess.hh"
14 
15 
16 map<string, G4Material*> mysql_materials::initMaterials(runConditions rc, goptions opts)
17 {
18 
19  string hd_msg = opts.optMap["LOG_MSG"].args + " MYSQL Materials Factory: >> ";
20  double verbosity = opts.optMap["MATERIAL_VERBOSITY"].arg;
21 
22  map<string, material> mymats; // material map
23 
24  // first check if there's at least one detector with MYSQL factory
26  return materialsFromMap(mymats);
27 
28  // connection to the DB
29  QSqlDatabase db = openGdb(opts);
30 
31  // Looping over detectorConditionsMap for detector names
32  // To each detector is associated a material and (optional) opt properties
33  for(map<string, detectorCondition>::iterator it=rc.detectorConditionsMap.begin(); it != rc.detectorConditionsMap.end(); it++)
34  {
35  // building materials belonging to detectors that are tagged with MYSQL factory
36  if(it->second.get_factory() != "MYSQL")
37  continue;
38 
39  if(verbosity)
40  cout << hd_msg << " Initializing " << it->second.get_factory() << " for detector " << it->first << endl;
41 
42  // only add "main" if it's the main variation
43  string dname = it->first ;
44  string tname = dname + "__materials";
45  string variation = get_variation(it->second.get_variation());
46  if(is_main_variation(it->second.get_variation()))
47  tname += "_main";
48 
49  string dbexecute = "select name, description, density, ncomponents, components from " + tname;
50  dbexecute += " where variation ='" + variation + "'";
51 
52  // executing query - will exit if not successfull.
53  QSqlQuery q;
54  if(!q.exec(dbexecute.c_str()))
55  {
56  cout << hd_msg << " Failed to execute MYSQL query " << dbexecute << ". This is a fatal error. Exiting." << endl;
57  qDebug() << q.lastError();
58  exit(0);
59  }
60  // Warning if nothing is found
61  if(q.size() == 0 && verbosity)
62  {
63  cout << " ** WARNING: material for system \"" << dname << "\" not found with variation \"" << variation << endl << endl;
64  }
65 
66  while (q.next())
67  {
68  material thisMat(TrimSpaces(qv_tostring( q.value(0)))); // name
69  thisMat.desc = qv_tostring(q.value(1)); // description
70  thisMat.density = q.value(2).toDouble(); // density
71  thisMat.ncomponents = q.value(3).toInt(); // number of components
72  thisMat.componentsFromString(qv_tostring(q.value(4))); // component + quantity list
73  thisMat.opticalsFromString(qv_tostring( q.value(5)), "photonEnergy");
74  thisMat.opticalsFromString(qv_tostring( q.value(6)), "indexOfRefraction");
75  thisMat.opticalsFromString(qv_tostring( q.value(7)), "absorptionLength");
76  thisMat.opticalsFromString(qv_tostring( q.value(8)), "reflectivity");
77  thisMat.opticalsFromString(qv_tostring( q.value(9)), "efficiency");
78 
79  mymats[thisMat.name] = thisMat;
80 
81  }
82  }
83 
84  // closing DB connection
85  closeGdb(db);
86  cout << endl;
87 
88  map<string, G4Material*> returnMap = materialsFromMap(mymats);
89  if(verbosity>0) printMaterials(returnMap);
90 
91  return returnMap;
92 }
93 
94 
95 
96 
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
double verbosity
map< string, G4Material * > materialsFromMap(map< string, material >)
string get_variation(string var)
parse variation name from string
string qv_tostring(QVariant input)
map< string, detectorCondition > detectorConditionsMap
void printMaterials(map< string, G4Material * > matMap)
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
map< string, G4Material * > initMaterials(runConditions, goptions)
string TrimSpaces(string in)
Removes leading and trailing spaces.