EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4DetectorSubsystem.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4DetectorSubsystem.cc
2 
3 #include <phparameter/PHParameters.h>
4 #include <phparameter/PHParametersContainer.h>
5 
6 #include <pdbcalbase/PdbParameterMapContainer.h>
7 
8 #include <g4main/PHG4Subsystem.h> // for PHG4Subsystem
9 
10 #include <phool/PHCompositeNode.h>
11 #include <phool/PHDataNode.h>
12 #include <phool/PHNode.h> // for PHNode
13 #include <phool/PHNodeIterator.h>
14 #include <phool/getClass.h>
15 #include <phool/phool.h>
16 
17 #include <cstdlib> // for exit, NULL
18 #include <iostream>
19 #include <utility> // for pair
20 
21 using namespace std;
22 
23 PHG4DetectorSubsystem::PHG4DetectorSubsystem(const std::string &name, const int lyr)
24  : PHG4Subsystem(name)
25  , params(new PHParameters(Name()))
26  , layer(lyr)
27 {
28  // put the layer into the name so we get unique names
29  // for multiple layers
30  Name(name + "_" + to_string(lyr));
31 }
32 
34 {
35  savetopNode = topNode;
36  params->set_name(Name());
37  int iret = InitSubsystem(topNode);
38  return iret;
39 }
40 
42 {
43  PHNodeIterator iter(topNode);
44  PHCompositeNode *parNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "PAR"));
45  PHCompositeNode *runNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
46 
47  string g4geonodename = "G4GEO_";
48  string paramnodename = "G4GEOPARAM_";
49  string calibdetname;
50  int isSuperDetector = 0;
51  if (superdetector != "NONE")
52  {
53  g4geonodename += SuperDetector();
54  paramscontainer = findNode::getClass<PHParametersContainer>(parNode, g4geonodename);
55  if (!paramscontainer)
56  {
57  PHNodeIterator parIter(parNode);
58  PHCompositeNode *DetNode = dynamic_cast<PHCompositeNode *>(parIter.findFirst("PHCompositeNode", SuperDetector()));
59  if (!DetNode)
60  {
61  DetNode = new PHCompositeNode(SuperDetector());
62  parNode->addNode(DetNode);
63  }
65  DetNode->addNode(new PHDataNode<PHParametersContainer>(paramscontainer, g4geonodename));
66  }
68  paramnodename += superdetector;
69  calibdetname = superdetector;
70  isSuperDetector = 1;
71  }
72  else
73  {
74  g4geonodename += params->Name();
75  parNode->addNode(new PHDataNode<PHParameters>(params, g4geonodename));
76  paramnodename += params->Name();
77  calibdetname = params->Name();
78  }
79 
80  // ASSUMPTION: if we read from DB and/or file we don't want the stuff from
81  // the node tree
82  // We leave the defaults intact in case there is no entry for
83  // those in the object read from the DB or file
84  // Order: read first DB, then calib file if both are enabled
86  {
87  if (ReadDB())
88  {
89  ReadParamsFromDB(calibdetname, isSuperDetector);
90  }
92  {
93  ReadParamsFromFile(calibdetname, get_filetype(), isSuperDetector);
94  }
95  }
96  else
97  {
98  PdbParameterMapContainer *nodeparams = findNode::getClass<PdbParameterMapContainer>(topNode, paramnodename);
99  if (nodeparams)
100  {
101  params->FillFrom(nodeparams, layer);
102  }
103  }
104  // parameters set in the macro always override whatever is read from
105  // the node tree, DB or file
107  // save updated persistant copy on node tree
108  PHCompositeNode *RunDetNode = runNode;
109  if (superdetector != "NONE")
110  {
111  PHNodeIterator runIter(runNode);
112  RunDetNode = dynamic_cast<PHCompositeNode *>(runIter.findFirst("PHCompositeNode", SuperDetector()));
113  if (!RunDetNode)
114  {
115  RunDetNode = new PHCompositeNode(SuperDetector());
116  runNode->addNode(RunDetNode);
117  }
118  }
119  // put parameters on the node tree so the subsystem can read them from there (it does not have to)
120  params->SaveToNodeTree(RunDetNode, paramnodename, layer);
121 // define the materials for the detector
122 // at this point all flags are known so materials set in the macro can
123 // be implemented here
124  DefineMaterials();
125  int iret = InitRunSubsystem(topNode);
126  // update parameters on node tree in case the subsystem has changed them
127  // In the end the parameters on the node tree must reflect what was actually used
128  params->UpdateNodeTree(RunDetNode, paramnodename, layer);
129  if (Verbosity() > 0)
130  {
131  PdbParameterMapContainer *nodeparams = findNode::getClass<PdbParameterMapContainer>(topNode, paramnodename);
132  cout << Name() << endl;
133  nodeparams->print();
134  }
135  beginrunexecuted = 1;
136  return iret;
137 }
138 
140 {
142  return;
143 }
144 
145 void PHG4DetectorSubsystem::set_double_param(const std::string &name, const double dval)
146 {
147  if (default_double.find(name) == default_double.end())
148  {
149  cout << "double parameter " << name << " not implemented" << endl;
150  cout << "implemented double parameters are:" << endl;
151  for (map<const string, double>::const_iterator iter = default_double.begin(); iter != default_double.end(); ++iter)
152  {
153  cout << iter->first << endl;
154  }
155  return;
156  }
157  dparams[name] = dval;
158 }
159 
160 double
162 {
163  return params->get_double_param(name);
164 }
165 
166 void PHG4DetectorSubsystem::set_int_param(const std::string &name, const int ival)
167 {
168  if (default_int.find(name) == default_int.end())
169  {
170  cout << "integer parameter " << name << " not implemented" << endl;
171  cout << "implemented integer parameters are:" << endl;
172  for (map<const string, int>::const_iterator iter = default_int.begin(); iter != default_int.end(); ++iter)
173  {
174  cout << iter->first << endl;
175  }
176  return;
177  }
178  iparams[name] = ival;
179 }
180 
181 int PHG4DetectorSubsystem::get_int_param(const std::string &name) const
182 {
183  return params->get_int_param(name);
184 }
185 
186 void PHG4DetectorSubsystem::set_string_param(const std::string &name, const string &sval)
187 {
188  if (default_string.find(name) == default_string.end())
189  {
190  cout << "string parameter " << name << " not implemented" << endl;
191  cout << "implemented string parameters are:" << endl;
192  for (map<const string, string>::const_iterator iter = default_string.begin(); iter != default_string.end(); ++iter)
193  {
194  cout << iter->first << endl;
195  }
196  return;
197  }
198  cparams[name] = sval;
199 }
200 
201 string
203 {
204  return params->get_string_param(name);
205 }
206 
208 {
209  for (map<const string, double>::const_iterator iter = dparams.begin(); iter != dparams.end(); ++iter)
210  {
211  params->set_double_param(iter->first, iter->second);
212  }
213  for (map<const string, int>::const_iterator iter = iparams.begin(); iter != iparams.end(); ++iter)
214  {
215  params->set_int_param(iter->first, iter->second);
216  }
217  for (map<const string, string>::const_iterator iter = cparams.begin(); iter != cparams.end(); ++iter)
218  {
219  params->set_string_param(iter->first, iter->second);
220  }
221  return;
222 }
223 
224 void PHG4DetectorSubsystem::set_default_double_param(const std::string &name, const double dval)
225 {
226  if (default_double.find(name) == default_double.end())
227  {
228  default_double[name] = dval;
229  }
230  else
231  {
232  cout << "trying to overwrite default double " << name << " "
233  << default_double[name] << " with " << dval << endl;
234  exit(1);
235  }
236  return;
237 }
238 
239 void PHG4DetectorSubsystem::set_default_int_param(const std::string &name, const int ival)
240 {
241  if (default_int.find(name) == default_int.end())
242  {
243  default_int[name] = ival;
244  }
245  else
246  {
247  cout << "trying to overwrite default int " << name << " "
248  << default_int[name] << " with " << ival << endl;
249  exit(1);
250  }
251  return;
252 }
253 
254 void PHG4DetectorSubsystem::set_default_string_param(const std::string &name, const string &sval)
255 {
256  if (default_string.find(name) == default_string.end())
257  {
258  default_string[name] = sval;
259  }
260  else
261  {
262  cout << "trying to overwrite default string " << name << " "
263  << default_string[name] << " with " << sval << endl;
264  exit(1);
265  }
266  return;
267 }
268 
270 {
271  set_default_int_param("absorberactive", 0);
272  set_default_int_param("absorbertruth", 0);
273  set_default_int_param("active", 0);
274  set_default_int_param("blackhole", 0);
275  set_default_int_param("supportactive", 0);
276 
277  SetDefaultParameters(); // call method from specific subsystem
278  // now load those parameters to our params class
279  for (map<const string, double>::const_iterator iter = default_double.begin(); iter != default_double.end(); ++iter)
280  {
281  params->set_double_param(iter->first, iter->second);
282  }
283  for (map<const string, int>::const_iterator iter = default_int.begin(); iter != default_int.end(); ++iter)
284  {
285  params->set_int_param(iter->first, iter->second);
286  }
287  for (map<const string, string>::const_iterator iter = default_string.begin(); iter != default_string.end(); ++iter)
288  {
289  params->set_string_param(iter->first, iter->second);
290  }
291 }
292 
294 {
295  int iret = 0;
296  if (paramscontainer)
297  {
298  iret = paramscontainer->WriteToDB();
299  }
300  else
301  {
302  iret = params->WriteToDB();
303  }
304  if (iret)
305  {
306  cout << "problem committing to DB" << endl;
307  }
308  return iret;
309 }
310 
311 int PHG4DetectorSubsystem::ReadParamsFromDB(const string &name, const int issuper)
312 {
313  int iret = 0;
314  if (issuper)
315  {
316  iret = params->ReadFromDB(name, layer);
317  }
318  else
319  {
320  iret = params->ReadFromDB();
321  }
322  if (iret)
323  {
324  cout << "problem reading from DB" << endl;
325  }
326  return iret;
327 }
328 
330 {
331  string extension;
332  switch (ftyp)
333  {
334  case xml:
335  extension = "xml";
336  break;
337  case root:
338  extension = "root";
339  break;
340  default:
341  cout << PHWHERE << "filetype " << ftyp << " not implemented" << endl;
342  exit(1);
343  }
344  int iret = 0;
345  if (paramscontainer)
346  {
347  iret = paramscontainer->WriteToFile(extension, calibfiledir);
348  }
349  else
350  {
351  iret = params->WriteToFile(extension, calibfiledir);
352  }
353  if (iret)
354  {
355  cout << "problem saving to " << extension << " file " << endl;
356  }
357  return iret;
358 }
359 
360 int PHG4DetectorSubsystem::ReadParamsFromFile(const string &name, const PHG4DetectorSubsystem::FILE_TYPE ftyp, const int issuper)
361 {
362  string extension;
363  switch (ftyp)
364  {
365  case xml:
366  extension = "xml";
367  break;
368  case root:
369  extension = "root";
370  break;
371  default:
372  cout << PHWHERE << "filetype " << ftyp << " not implemented" << endl;
373  exit(1);
374  }
375  int iret = params->ReadFromFile(name, extension, layer, issuper, calibfiledir);
376  if (iret)
377  {
378  cout << "problem reading from " << extension << " file " << endl;
379  }
380  return iret;
381 }
382 
384 {
385  iparams["active"] = i;
386 }
387 
389 {
390  iparams["absorberactive"] = i;
391 }
392 
394 {
395  iparams["supportactive"] = i;
396 }
397 
399 {
400  iparams["blackhole"] = i;
401 }
402 
404 {
405  iparams["absorbertruth"] = i;
406 }