GEMC  2.3
Geant4 Monte-Carlo Framework
run_conditions.cc
Go to the documentation of this file.
1 // gemc headers
2 #include "run_conditions.h"
3 #include "string_utilities.h"
4 #include "utils.h"
5 
6 // CLHEP units
7 #include "CLHEP/Units/PhysicalConstants.h"
8 using namespace CLHEP;
9 
10 // geant4
11 #include "Randomize.hh"
12 
13 
15 {
16  string hd_msg = gemcOpt.optMap["LOG_MSG"].args + " gcard: >> " ;
17  int run_number = gemcOpt.optMap["RUNNO"].arg;
18 
19 
20  // adding detector defined with the DF directive Detector/Factory
21  vector<string> dfopt = get_info(gemcOpt.optMap["DF"].args);
22  if(dfopt[0] != "no" && dfopt.size() > 1)
23  {
24  detectorConditionsMap[dfopt[0]] = detectorCondition(dfopt[1]);
25  }
26 
27  string file = gemcOpt.optMap["gcard"].args;
28  if(file=="no") return;
29 
30  QFile gcard(file.c_str());
31  // reading gcard and checking the consistency
32  if (!domDocument.setContent(&gcard))
33  {
34  gcard.close();
35  cout << hd_msg << " gcard format is wrong - check XML syntax. Exiting." << endl;
36  exit(0);
37  }
38  gcard.close();
39 
40  // start reading gcard file
41  QDomElement docElem = domDocument.documentElement();
42 
43  QDomNode n = docElem.firstChild();
44  // looping over detector entries
45  while(!n.isNull())
46  {
47  QDomElement e = n.toElement(); // converts the node to an element.
48  if(!e.isNull()) // the node really is an element.
49  if(qv_tostring(e.tagName()) == "detector") // selecting "detector" nodes
50  {
51  detectorCondition dCon;
52  dCon.set_system(assignAttribute(e, "name", "na"));
53  dCon.set_factory(assignAttribute(e, "factory", "na"));
54  dCon.set_variation(assignAttribute(e, "variation", "main")); // default variation is "main"
55  dCon.set_run_number(assignAttribute(e, "run_number", run_number));
56 
57  // Initializing Position and rotation
58  dCon.set_position("0*cm", "0*cm", "0*cm");
59  dCon.set_rotation("0*deg", "0*deg", "0*deg");
60 
61 
62  QDomNode nn = e.firstChild();
63  while(!nn.isNull())
64  {
65  QDomElement ee = nn.toElement();
66  if(!ee.isNull())
67  {
68  if(qv_tostring(ee.tagName()) == "position")
69  {
70  dCon.set_position(qv_tostring(ee.attributeNode("x").value()),
71  qv_tostring(ee.attributeNode("y").value()),
72  qv_tostring(ee.attributeNode("z").value()) );
73  }
74  if(qv_tostring(ee.tagName()) == "rotation")
75  {
76  dCon.set_rotation(qv_tostring(ee.attributeNode("x").value()),
77  qv_tostring(ee.attributeNode("y").value()),
78  qv_tostring(ee.attributeNode("z").value()) );
79  }
80  if(qv_tostring(ee.tagName()) == "existence")
81  {
82  dCon.set_existance(qv_tostring(ee.attributeNode("exist").value()));
83  }
84  }
85  nn = nn.nextSibling();
86  }
87 
88  // inserting this detector in the map
89  detectorConditionsMap[assignAttribute(e, "name", "na")] = dCon;
90 
91  }
92 
93  n = n.nextSibling();
94  }
95 
96  cout << endl;
97 }
98 
100 
101 
102 void detectorCondition::set_position(string X, string Y, string Z)
103 {
104  pos.setX(get_number(X,1));
105  pos.setY(get_number(Y,1));
106  pos.setZ(get_number(Z,1));
107 }
108 
109 void detectorCondition::set_rotation(string X, string Y, string Z)
110 {
111  rot = G4RotationMatrix(G4ThreeVector(1, 0, 0),
112  G4ThreeVector(0, 1, 0),
113  G4ThreeVector(0, 0, 1));
114 
115  rot.rotateX(get_number(X,1));
116  rot.rotateY(get_number(Y,1));
117  rot.rotateZ(get_number(Z,1));
118 
119  vrot.setX(get_number(X,1));
120  vrot.setY(get_number(Y,1));
121  vrot.setZ(get_number(Z,1));
122 
123 }
124 
126 {
127  if(exist == "no" || exist == "NO" || exist == "No")
128  is_present = 0;
129 }
130 
131 
133 {
134  map<string, string> detmap;
135 
136  // filling name, rotation, position modifications from gcard
137  for(map<string, detectorCondition>::iterator it = detectorConditionsMap.begin(); it != detectorConditionsMap.end(); it++)
138  {
139 
140  detmap[it->first] = " is loaded with factory " + it->second.get_factory()
141  + ", variation " + it->second.get_variation()
142  + " and run number " + stringify(it->second.get_run_number());
143 
144  if(it->second.get_position().mag2() != 0)
145  {
146  string key = "local shift for " + it->first;
147  detmap[key] = "(" + stringify(it->second.get_position().x()/mm) + ", "
148  + stringify(it->second.get_position().y()/mm) + ", "
149  + stringify(it->second.get_position().z()/mm) + ")mm";
150  }
151 
152  if(it->second.get_vrotation().mag2() != 0)
153  {
154  string key = "local rotation for " + it->first;
155  detmap[key] = "(" + stringify(it->second.get_vrotation().x()/degree) + ", "
156  + stringify(it->second.get_vrotation().y()/degree) + ", "
157  + stringify(it->second.get_vrotation().z()/degree) + ")deg";
158  }
159  }
160 
161  return detmap;
162 }
163 
164 int check_if_factory_is_needed(map<string, detectorCondition> dcon, string factory)
165 {
166  int isneeded = 0;
167 
168  for(map<string, detectorCondition>::iterator it=dcon.begin(); it != dcon.end(); it++)
169  {
170  if(it->second.get_factory() == factory)
171  isneeded++;
172  }
173  return isneeded;
174 }
175 
177 {
178  if(detectorConditionsMap.find(detector) != detectorConditionsMap.end())
179  return detectorConditionsMap[detector].get_run_number();
180 
181  else return 0;
182 }
183 
184 
186 {
187  if(detectorConditionsMap.find(detector) != detectorConditionsMap.end())
188  return detectorConditionsMap[detector].get_variation();
189 
190  else return "na";
191 }
192 
194 {
195  if(detectorConditionsMap.find(detector) != detectorConditionsMap.end())
196  return detectorConditionsMap[detector].get_system();
197 
198  else return "na";
199 
200 }
201 
202 // get_systems return a map<string, string>
203 map<string, string> runConditions::get_systems()
204 {
205  map<string, string> allSystems;
206 
207  for(map<string, detectorCondition>::iterator it=detectorConditionsMap.begin(); it != detectorConditionsMap.end(); it++)
208  allSystems[it->first] = it->second.get_factory();
209 
210  return allSystems;
211 }
212 
213 
214 
216 {
217  string fname = opts.optMap["RUN_WEIGHTS"].args;
218  int nevts = opts.optMap["N"].arg;
219  startEvent = opts.optMap["EVN"].arg;
220  defaultRunNumber = opts.optMap["RUNNO"].arg;
221 
222  isNewRun = FALSE;
223 
224  if(nevts==0) return;
225 
226  // by default there is only 1 weight, the run number is defaultRunNumber for all events
227  if(fname == "no")
228  {
229  w[defaultRunNumber] = 1;
230  n[defaultRunNumber] = nevts;
231  return;
232  }
233 
234  ifstream in(fname.c_str());
235 
236  if(!in)
237  {
238  cerr << " !!! Can't open input file " << fname.c_str() << ". Exiting. " << endl;
239  exit(1);
240  }
241 
242  // filling weight map
243  while (!in.eof())
244  {
245  int rr;
246  double ww;
247  in >> rr >> ww;
248  w[rr] = ww;
249  n[rr] = 0;
250  }
251 
252  // now randomizing the run / event map based on number of events
253  for(int i=0; i<nevts; i++)
254  {
255  double random = G4UniformRand();
256  double ww = 0;
257  for(map<int, double>::iterator it = w.begin(); it != w.end(); it++)
258  {
259  ww += it->second;
260  if(random <= ww)
261  {
262  n[it->first]++;
263  break;
264  }
265  }
266  }
267 
268  cout << " > Run weights table loaded: " << endl;
269  for(map<int, double>::iterator it = w.begin(); it != w.end(); it++)
270  cout << " - run: " << it->first << "\t weight: " << w[it->first] << "\t n. events: " << n[it->first] << endl;
271 
272 }
273 
274 
276 {
277  int dn = evn - startEvent ;
278 
279  double nn = 0;
280 
281  for(map<int, int>::iterator it = n.begin(); it != n.end(); it++)
282  {
283  nn += it->second;
284  if(dn < nn)
285  {
286  if(it->first != runNo)
287  {
288  isNewRun = TRUE;
289  runNo = it->first;
290  }
291  else
292  {
293  isNewRun = FALSE;
294  }
295  return it->first;
296  }
297  }
298 
299  // default comes from the option map
300  runNo = defaultRunNumber;
301 
302  return runNo;
303 }
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
void set_system(string s)
int check_if_factory_is_needed(map< string, detectorCondition > dcon, string factory)
map< string, string > getDetectorConditionsMap()
void set_rotation(string X, string Y, string Z)
vector< string > get_info(string input, string chars)
get information from strings such as "5*GeV, 2*deg, 10*deg", parses out strings in second argument ...
void set_factory(string f)
int get_run_number(string detector)
string get_variation(string var)
parse variation name from string
string qv_tostring(QVariant input)
string get_variation(string detector)
double get_number(string v, int warn_no_unit)
Returns number with dimension from string, i.e. 100*cm.
string assignAttribute(QDomElement e, string attribute, string defaultValue)
Definition: utils.cc:294
string stringify(double x)
map< string, aopt > optMap
Options map.
Definition: options.h:75
int getRunNumber(int n)
void set_existance(string exist)
void set_variation(string v)
string get_system(string detector)
void set_position(string X, string Y, string Z)
void set_run_number(int r)
map< string, string > get_systems()