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