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