Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHParametersContainer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHParametersContainer.cc
2 #include "PHParameters.h"
3 
4 #include <pdbcalbase/PdbApplication.h>
5 #include <pdbcalbase/PdbBankID.h>
6 #include <pdbcalbase/PdbBankManager.h>
7 #include <pdbcalbase/PdbCalBank.h>
8 #include <pdbcalbase/PdbParameterMap.h>
9 #include <pdbcalbase/PdbParameterMapContainer.h>
10 
11 #include <phool/PHCompositeNode.h>
12 #include <phool/PHIODataNode.h>
13 #include <phool/PHTimeStamp.h>
14 #include <phool/getClass.h>
15 #include <phool/phool.h>
16 
17 #include <TBufferXML.h>
18 #include <TFile.h>
19 #include <TSystem.h>
20 
21 // stacktrace gives a shadow warning
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wshadow"
24 #include <boost/stacktrace.hpp>
25 #pragma GCC diagnostic pop
26 
27 #include <unistd.h>
28 #include <algorithm>
29 #include <cctype>
30 #include <cstdlib>
31 #include <ctime>
32 #include <iostream>
33 #include <sstream>
34 
36  : superdetectorname(name)
37 {
38 }
39 
41 {
42  while (parametermap.begin() != parametermap.end())
43  {
44  delete parametermap.begin()->second;
45  parametermap.erase(parametermap.begin());
46  }
47 }
48 
50 {
51  // this fill only existing detids - no new ones are created (if the PdbParameterMapContainer contains
52  // entries from another detector)
53  PdbParameterMapContainer::parConstRange begin_end = saveparamcontainer->get_ParameterMaps();
54  for (PdbParameterMapContainer::parIter iter = begin_end.first; iter != begin_end.second; ++iter)
55  {
56  Iterator pariter = parametermap.find(iter->first);
57  if (pariter != parametermap.end())
58  {
59  PHParameters *params = pariter->second;
60  params->FillFrom(iter->second);
61  }
62  }
63  return;
64 }
65 
67 {
68  PdbParameterMapContainer::parConstRange begin_end = saveparamcontainer->get_ParameterMaps();
69  for (PdbParameterMapContainer::parIter iter = begin_end.first; iter != begin_end.second; ++iter)
70  {
71  Iterator pariter = parametermap.find(iter->first);
72  if (pariter != parametermap.end())
73  {
74  PHParameters *params = pariter->second;
75  params->FillFrom(iter->second);
76  }
77  else
78  {
79  PHParameters *params = new PHParameters(name);
80  params->FillFrom(iter->second);
81  AddPHParameters(iter->first, params);
82  }
83  }
84  return;
85 }
86 
88 {
89  if (parametermap.find(detid) != parametermap.end())
90  {
91  std::cout << PHWHERE << " detector id " << detid << " already exists for "
92  << (parametermap.find(detid))->second->Name() << std::endl;
93  gSystem->Exit(1);
94  }
95  parametermap[detid] = params;
96 }
97 
98 const PHParameters *
100 {
101  std::map<int, PHParameters *>::const_iterator iter = parametermap.find(detid);
102  if (iter == parametermap.end())
103  {
104  std::cout << "could not find parameters for detector id " << detid
105  << std::endl;
106  std::cout << "Here is the stacktrace: " << std::endl;
107  std::cout << boost::stacktrace::stacktrace();
108  std::cout << std::endl
109  << "DO NOT PANIC - this is not a segfault" << std::endl;
110  std::cout << "Check the stacktrace for the guilty party (typically #2)" << std::endl;
111  gSystem->Exit(1);
112  exit(1);
113  }
114  return iter->second;
115 }
116 
117 PHParameters *
119 {
120  std::map<int, PHParameters *>::iterator iter = parametermap.find(detid);
121  if (iter == parametermap.end())
122  {
123  return nullptr;
124  }
125  return iter->second;
126 }
127 
129 {
130  std::ostringstream fullpath;
131  std::ostringstream fnamestream;
132  PdbBankID bankID(0); // lets start at zero
133  PHTimeStamp TStart(0);
134  PHTimeStamp TStop(0xffffffff);
135  fullpath << dir;
136  // add / if directory lacks ending /
137  if (*(dir.rbegin()) != '/')
138  {
139  fullpath << "/";
140  }
141  fnamestream << superdetectorname << "_geoparams"
142  << "-" << bankID.getInternalValue()
143  << "-" << TStart.getTics() << "-" << TStop.getTics() << "-" << time(nullptr)
144  << "." << extension;
145  std::string fname = fnamestream.str();
146  std::transform(fname.begin(), fname.end(), fname.begin(), ::tolower);
147  fullpath << fname;
148 
149  std::cout << "PHParameters::WriteToFile - save to " << fullpath.str() << std::endl;
150 
153  TFile *f = TFile::Open(fullpath.str().c_str(), "recreate");
154  // force xml file writing to use extended precision shown experimentally
155  // to not modify input parameters (.15e)
156  std::string floatformat = TBufferXML::GetFloatFormat();
157  TBufferXML::SetFloatFormat("%.15e");
158  myparm->Write();
159  delete f;
160  // restore previous xml float format
161  TBufferXML::SetFloatFormat(floatformat.c_str());
162  std::cout << "sleeping 1 second to prevent duplicate inserttimes" << std::endl;
163  sleep(1);
164  return 0;
165 }
166 
168 {
169  PdbBankManager *bankManager = PdbBankManager::instance();
170  PdbApplication *application = bankManager->getApplication();
171  if (!application->startUpdate())
172  {
173  std::cout << PHWHERE << " Aborting, Database not writable" << std::endl;
174  application->abort();
175  exit(1);
176  }
177 
178  // Make a bank ID...
179  PdbBankID bankID(0); // lets start at zero
180  PHTimeStamp TStart(0);
181  PHTimeStamp TStop(0xffffffff);
182 
183  std::string tablename = superdetectorname + "_geoparams";
184  std::transform(tablename.begin(), tablename.end(), tablename.begin(),
185  ::tolower);
186  PdbCalBank *NewBank = bankManager->createBank("PdbParameterMapContainerBank", bankID,
187  "Geometry Parameters", TStart, TStop, tablename);
188  if (NewBank)
189  {
190  NewBank->setLength(1);
191  PdbParameterMapContainer *myparm = (PdbParameterMapContainer *) &NewBank->getEntry(0);
193  application->commit(NewBank);
194  delete NewBank;
195  }
196  else
197  {
198  std::cout << PHWHERE " Committing to DB failed" << std::endl;
199  return -1;
200  }
201  return 0;
202 }
203 
205 {
206  std::map<int, PHParameters *>::const_iterator iter;
207  for (iter = parametermap.begin(); iter != parametermap.end(); ++iter)
208  {
209  PdbParameterMap *myparm = new PdbParameterMap();
210  iter->second->CopyToPdbParameterMap(myparm);
211  myparmap->AddPdbParameterMap(iter->first, myparm);
212  }
213  return;
214 }
215 
217 {
218  std::map<int, PHParameters *>::const_iterator iter;
219  for (iter = parametermap.begin(); iter != parametermap.end(); ++iter)
220  {
221  PdbParameterMap *nodeparams = myparmap->GetParametersToModify(iter->first);
222  iter->second->CopyToPdbParameterMap(nodeparams);
223  }
224  return;
225 }
226 
227 void PHParametersContainer::Print(Option_t * /*option*/) const
228 {
229  std::cout << "Name: " << Name() << std::endl;
230  std::map<int, PHParameters *>::const_iterator iter;
231  for (iter = parametermap.begin(); iter != parametermap.end(); ++iter)
232  {
233  std::cout << "parameter detid: " << iter->first << std::endl;
234  iter->second->Print();
235  }
236  return;
237 }
238 
240 {
241  PdbParameterMapContainer *myparmap = findNode::getClass<PdbParameterMapContainer>(topNode, nodename);
242  if (!myparmap)
243  {
244  myparmap = new PdbParameterMapContainer();
246  new PHIODataNode<PdbParameterMapContainer>(myparmap, nodename);
247  topNode->addNode(newnode);
248  }
249  else
250  {
251  myparmap->Reset();
252  }
254  return;
255 }
256 
258 {
259  PdbParameterMapContainer *myparmap = findNode::getClass<PdbParameterMapContainer>(topNode, nodename);
260  if (!myparmap)
261  {
262  std::cout << PHWHERE << " could not find PdbParameterMapContainer " << nodename
263  << " which must exist" << std::endl;
264  gSystem->Exit(1);
265  }
267  return;
268 }
269 
270 int PHParametersContainer::ExistDetid(const int detid) const
271 {
272  if (parametermap.find(detid) != parametermap.end())
273  {
274  return 1;
275  }
276  return 0;
277 }