Class Reference for E1039 Core & Analysis Software
PHG4DetectorSubsystem.cc
Go to the documentation of this file.
2 
3 #include <phparameter/PHParameters.h>
4 #include <phparameter/PHParametersContainer.h>
5 
6 #include <pdbcalbase/PdbParameterMap.h>
7 #include <pdbcalbase/PdbParameterMapContainer.h>
8 
9 #include <phool/getClass.h>
10 #include <phool/phool.h>
11 #include <phool/PHCompositeNode.h>
12 #include <phool/PHDataNode.h>
13 #include <phool/PHIODataNode.h>
14 #include <phool/PHNodeIterator.h>
15 
16 #include <iostream>
17 #include <sstream>
18 #include <algorithm>
19 
20 using namespace std;
21 
22 PHG4DetectorSubsystem::PHG4DetectorSubsystem(const std::string &name, const int lyr):
23  PHG4Subsystem(name),
24  //params(new PHParameters(Name())),
25  paramscontainer(NULL),
26  savetopNode(NULL),
27  overlapcheck(false),
28  layer(lyr),
29  usedb(0),
30  beginrunexecuted(0),
31  filetype(PHG4DetectorSubsystem::none),
32  superdetector("NONE"),
33  calibfiledir("./")
34 {
35  // put the layer into the name so we get unique names
36  // for multiple layers
37  ostringstream nam;
38  nam << name << "_" << lyr;
39  Name(nam.str().c_str());
40 
41  params = new PHParameters(Name());
42 }
43 
44 int
46 {
47  savetopNode = topNode;
48  params->set_name(Name());
49  int iret = InitSubsystem(topNode);
50  return iret;
51 }
52 
53 int
55 {
56  PHNodeIterator iter( topNode );
57  PHCompositeNode *parNode = dynamic_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "PAR" ));
58  PHCompositeNode *runNode = dynamic_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "RUN" ));
59 
60  string g4geonodename = "G4GEO_";
61  string paramnodename = "G4GEOPARAM_";
62  string calibdetname;
63  int isSuperDetector = 0;
64  if (superdetector != "NONE")
65  {
66  g4geonodename += SuperDetector();
67  paramscontainer = findNode::getClass<PHParametersContainer>(parNode,g4geonodename);
68  if (! paramscontainer)
69  {
70  PHNodeIterator parIter(parNode);
71  PHCompositeNode *DetNode = dynamic_cast<PHCompositeNode*>(parIter.findFirst("PHCompositeNode",SuperDetector()));
72  if (! DetNode)
73  {
74  DetNode = new PHCompositeNode(SuperDetector());
75  parNode->addNode(DetNode);
76  }
77  paramscontainer = new PHParametersContainer(superdetector);
78  DetNode->addNode(new PHDataNode<PHParametersContainer>(paramscontainer,g4geonodename));
79  }
80  paramscontainer->AddPHParameters(layer,params);
81  paramnodename += superdetector;
82  calibdetname = superdetector;
83  isSuperDetector = 1;
84  }
85  else
86  {
87  g4geonodename += params->Name();
88  parNode->addNode(new PHDataNode<PHParameters>(params,g4geonodename));
89  paramnodename += params->Name();
90  calibdetname = params->Name();
91  }
92 
93 
94 
95  // ASSUMPTION: if we read from DB and/or file we don't want the stuff from
96  // the node tree
97  // We leave the defaults intact in case there is no entry for
98  // those in the object read from the DB or file
99  // Order: read first DB, then calib file if both are enabled
101  {
102  if (ReadDB())
103  {
104  ReadParamsFromDB(calibdetname,isSuperDetector);
105  }
107  {
108  std::cout << PHWHERE << "FIXME!" << std::endl;
109  //ReadParamsFromFile(calibdetname, get_filetype(),isSuperDetector );
110  }
111  }
112  else
113  {
114  PdbParameterMapContainer *nodeparams = findNode::getClass<PdbParameterMapContainer>(topNode,paramnodename);
115  if (nodeparams)
116  {
117  params->FillFrom(nodeparams, layer);
118  }
119  }
120  // parameters set in the macro always override whatever is read from
121  // the node tree, DB or file
123  // save updated persistant copy on node tree
124  PHCompositeNode *RunDetNode = runNode;
125  if (superdetector != "NONE")
126  {
127  PHNodeIterator runIter(runNode);
128  RunDetNode = dynamic_cast<PHCompositeNode*>(runIter.findFirst("PHCompositeNode",SuperDetector()));
129  if (! RunDetNode)
130  {
131  RunDetNode = new PHCompositeNode(SuperDetector());
132  runNode->addNode(RunDetNode);
133  }
134  }
135  params->SaveToNodeTree(RunDetNode,paramnodename,layer);
136  int iret = InitRunSubsystem(topNode);
137  if (Verbosity() > 0)
138  {
139  PdbParameterMapContainer *nodeparams = findNode::getClass<PdbParameterMapContainer>(topNode,paramnodename);
140  cout << Name() << endl;
141  nodeparams->print();
142  }
143  beginrunexecuted = 1;
144  return iret;
145 }
146 
147 void
148 PHG4DetectorSubsystem::SuperDetector(const std::string &name)
149 {
150  superdetector = name;
151  return;
152 }
153 
154 void
155 PHG4DetectorSubsystem::set_double_param(const std::string &name, const double dval)
156 {
157  if (default_double.find(name) == default_double.end())
158  {
159  cout << "double parameter " << name << " not implemented" << endl;
160  cout << "implemented double parameters are:" << endl;
161  for (map<const string, double>::const_iterator iter = default_double.begin(); iter != default_double.end(); ++iter)
162  {
163  cout << iter->first << endl;
164  }
165  return;
166  }
167  dparams[name] = dval;
168 }
169 
170 std::vector<double>
171 PHG4DetectorSubsystem::get_vdouble_param(const std::string &name) const
172 {
173  return params->get_vdouble_param(name);
174 }
175 
176 void
177 PHG4DetectorSubsystem::set_vdouble_param(const std::string &name, const std::vector<double> vdval)
178 {
179  if (default_vdouble.find(name) == default_vdouble.end())
180  {
181  cout << "vdouble parameter " << name << " not implemented" << endl;
182  cout << "implemented vdouble parameters are:" << endl;
183  for (map<const string, std::vector<double> >::const_iterator iter = default_vdouble.begin(); iter != default_vdouble.end(); ++iter)
184  {
185  cout << iter->first << endl;
186  }
187  return;
188  }
189  vdparams[name] = vdval;
190 }
191 
192 void
193 PHG4DetectorSubsystem::set_vdouble_param(const std::string &name, const double dvals[], const unsigned int n)
194 {
195  std::vector<double> vdval;
196  vdval.assign(dvals, dvals+n);
197  set_vdouble_param(name, vdval);
198 }
199 
200 double
201 PHG4DetectorSubsystem::get_double_param(const std::string &name) const
202 {
203  return params->get_double_param(name);
204 }
205 
206 void
207 PHG4DetectorSubsystem::set_int_param(const std::string &name, const int ival)
208 {
209  if (default_int.find(name) == default_int.end())
210  {
211  cout << "integer parameter " << name << " not implemented" << endl;
212  cout << "implemented integer parameters are:" << endl;
213  for (map<const string, int>::const_iterator iter = default_int.begin(); iter != default_int.end(); ++iter)
214  {
215  cout << iter->first << endl;
216  }
217  return;
218  }
219  iparams[name] = ival;
220 }
221 
222 int
223 PHG4DetectorSubsystem::get_int_param(const std::string &name) const
224 {
225  return params->get_int_param(name);
226 }
227 
228 void
229 PHG4DetectorSubsystem::set_string_param(const std::string &name, const string &sval)
230 {
231  if (default_string.find(name) == default_string.end())
232  {
233  cout << "string parameter " << name << " not implemented" << endl;
234  cout << "implemented string parameters are:" << endl;
235  for (map<const string, string>::const_iterator iter = default_string.begin(); iter != default_string.end(); ++iter)
236  {
237  cout << iter->first << endl;
238  }
239  return;
240  }
241  cparams[name] = sval;
242 }
243 
244 string
245 PHG4DetectorSubsystem::get_string_param(const std::string &name) const
246 {
247  return params->get_string_param(name);
248 }
249 
250 void
252 {
253  for (map<const string,double>::const_iterator iter = dparams.begin(); iter != dparams.end(); ++iter)
254  {
255  params->set_double_param(iter->first,iter->second);
256  }
257  for (auto iter = vdparams.begin(); iter != vdparams.end(); ++iter)
258  {
259  params->set_vdouble_param(iter->first,iter->second);
260  }
261  for (map<const string,int>::const_iterator iter = iparams.begin(); iter != iparams.end(); ++iter)
262  {
263  params->set_int_param(iter->first,iter->second);
264  }
265  for (map<const string,string>::const_iterator iter = cparams.begin(); iter != cparams.end(); ++iter)
266  {
267  params->set_string_param(iter->first,iter->second);
268  }
269  return;
270 }
271 
272 void
273 PHG4DetectorSubsystem::set_default_double_param( const std::string &name, const double dval)
274 {
275  if (default_double.find(name) == default_double.end())
276  {
277  default_double[name] = dval;
278  }
279  else
280  {
281  cout << "trying to overwrite default double " << name << " "
282  << default_double[name] << " with " << dval << endl;
283  exit(1);
284  }
285  return;
286 }
287 
288 void
289 PHG4DetectorSubsystem::set_default_vdouble_param( const std::string &name, const std::vector<double> &vdval)
290 {
291  if (default_vdouble.find(name) == default_vdouble.end())
292  {
293  default_vdouble[name] = vdval;
294  }
295  else
296  {
297  cout << "trying to overwrite default double " << name << " "
298  //<< default_vdouble[name] <<
299  " with "
300  << "{ "
301  //<< std::for_each(vdval.cbegin(), vdval.cend(), [](const double &d){std::cout << d << " ";})
302  << "}"
303  << endl;
304  exit(1);
305  }
306  return;
307 }
308 
309 void
310 PHG4DetectorSubsystem::set_default_int_param( const std::string &name, const int ival)
311 {
312  if (default_int.find(name) == default_int.end())
313  {
314  default_int[name] = ival;
315  }
316  else
317  {
318  cout << "trying to overwrite default int " << name << " "
319  << default_int[name] << " with " << ival << endl;
320  exit(1);
321  }
322  return;
323 }
324 
325 void
326 PHG4DetectorSubsystem::set_default_string_param( const std::string &name, const string &sval)
327 {
328  if (default_string.find(name) == default_string.end())
329  {
330  default_string[name] = sval;
331  }
332  else
333  {
334  cout << "trying to overwrite default string " << name << " "
335  << default_string[name] << " with " << sval << endl;
336  exit(1);
337  }
338  return;
339 }
340 
341 void
343 {
344  set_default_int_param("absorberactive", 0);
345  set_default_int_param("absorbertruth", 0);
346  set_default_int_param("active", 0);
347  set_default_int_param("blackhole", 0);
348 
349  SetDefaultParameters(); // call method from specific subsystem
350  // now load those parameters to our params class
351  for (map<const string,double>::const_iterator iter = default_double.begin(); iter != default_double.end(); ++iter)
352  {
353  params->set_double_param(iter->first,iter->second);
354  }
355  for (auto iter = default_vdouble.begin(); iter != default_vdouble.end(); ++iter)
356  {
357  params->set_vdouble_param(iter->first,iter->second);
358  }
359  for (map<const string,int>::const_iterator iter = default_int.begin(); iter != default_int.end(); ++iter)
360  {
361  params->set_int_param(iter->first,iter->second);
362  }
363  for (map<const string,string>::const_iterator iter = default_string.begin(); iter != default_string.end(); ++iter)
364  {
365  params->set_string_param(iter->first,iter->second);
366  }
367 }
368 
369 int
371 {
372  int iret = 0;
373  if (paramscontainer)
374  {
375  iret = paramscontainer->WriteToDB();
376  }
377  else
378  {
379  iret = params->WriteToDB();
380  }
381  if (iret)
382  {
383  cout << "problem committing to DB" << endl;
384  }
385  return iret;
386 }
387 
388 int
389 PHG4DetectorSubsystem::ReadParamsFromDB(const string &name, const int issuper)
390 {
391  int iret = 0;
392  if (issuper)
393  {
394  iret = params->ReadFromDB(name,layer);
395  }
396  else
397  {
398  iret = params->ReadFromDB();
399  }
400  if (iret)
401  {
402  cout << "problem reading from DB" << endl;
403  }
404  return iret;
405 }
406 
407 //int
408 //PHG4DetectorSubsystem::SaveParamsToFile(const PHG4DetectorSubsystem::FILE_TYPE ftyp)
409 //{
410 // string extension;
411 // switch(ftyp)
412 // {
413 // case xml:
414 // extension = "xml";
415 // break;
416 // case root:
417 // extension = "root";
418 // break;
419 // default:
420 // cout << PHWHERE << "filetype " << ftyp << " not implemented" << endl;
421 // exit(1);
422 // }
423 // int iret = 0;
424 // if (paramscontainer)
425 // {
426 // iret = paramscontainer->WriteToFile(extension,calibfiledir);
427 // }
428 // else
429 // {
430 // iret = params->WriteToFile(extension,calibfiledir);
431 // }
432 // if (iret)
433 // {
434 // cout << "problem saving to " << extension << " file " << endl;
435 // }
436 // return iret;
437 //}
438 //
439 //int
440 //PHG4DetectorSubsystem::ReadParamsFromFile(const string &name, const PHG4DetectorSubsystem::FILE_TYPE ftyp, const int issuper)
441 //{
442 // string extension;
443 // switch(ftyp)
444 // {
445 // case xml:
446 // extension = "xml";
447 // break;
448 // case root:
449 // extension = "root";
450 // break;
451 // default:
452 // cout << PHWHERE << "filetype " << ftyp << " not implemented" << endl;
453 // exit(1);
454 // }
455 // int iret = params->ReadFromFile(name, extension, layer, issuper, calibfiledir);
456 // if (iret)
457 // {
458 // cout << "problem reading from " << extension << " file " << endl;
459 // }
460 // return iret;
461 //}
462 
463 void
465 {
466  iparams["active"] = i;
467 }
468 
469 void
471 {
472  iparams["absorberactive"] = i;
473 }
474 
475 void
477 {
478  iparams["blackhole"] = i;
479 }
480 
481 void
483 {
484  iparams["absorbertruth"] = i;
485 }
486 
#define NULL
Definition: Pdb.h:9
virtual const std::string Name() const
Returns the name of this module.
Definition: Fun4AllBase.h:23
virtual int Verbosity() const
Gets the verbosity of this module.
Definition: Fun4AllBase.h:64
PHBoolean addNode(PHNode *)
void SetActive(const int i=1)
void set_default_vdouble_param(const std::string &name, const std::vector< double > &dval)
void set_vdouble_param(const std::string &name, const std::vector< double > vdval)
const std::string SuperDetector() const
int InitRun(PHCompositeNode *) final
void set_double_param(const std::string &name, const double dval)
int ReadParamsFromDB(const std::string &name, const int issuper)
void set_default_double_param(const std::string &name, const double dval)
int Init(PHCompositeNode *) final
void BlackHole(const int i=1)
FILE_TYPE get_filetype() const
virtual int InitSubsystem(PHCompositeNode *)
std::vector< double > get_vdouble_param(const std::string &name) const
void set_string_param(const std::string &name, const std::string &sval)
void set_default_string_param(const std::string &name, const std::string &sval)
void set_int_param(const std::string &name, const int ival)
double get_double_param(const std::string &name) const
void SetAbsorberTruth(const int i=1)
int get_int_param(const std::string &name) const
virtual int InitRunSubsystem(PHCompositeNode *)
std::string get_string_param(const std::string &name) const
virtual void SetDefaultParameters()=0
void SetAbsorberActive(const int i=1)
PHG4DetectorSubsystem(const std::string &name="GenericSubsystem", const int lyr=0)
void set_default_int_param(const std::string &name, const int ival)
PHParameters * params
Definition: PHG4Subsystem.h:56
PHNode * findFirst(const std::string &, const std::string &)
void AddPHParameters(const int layer, PHParameters *params)
void set_string_param(const std::string &name, const std::string &str)
double get_double_param(const std::string &name) const
void set_name(const std::string &name)
Definition: PHParameters.h:62
int get_int_param(const std::string &name) const
Definition: PHParameters.cc:60
void FillFrom(const PdbParameterMap *saveparams)
void set_int_param(const std::string &name, const int ival)
Definition: PHParameters.cc:54
void set_double_param(const std::string &name, const double dval)
Definition: PHParameters.cc:95
void SaveToNodeTree(PHCompositeNode *topNode, const std::string &nodename)
std::string get_string_param(const std::string &name) const
std::string Name() const
Definition: PHParameters.h:63
void set_vdouble_param(const std::string &name, const vdouble vdval)
vdouble get_vdouble_param(const std::string &name) const
#define PHWHERE
Definition: phool.h:23