Class Reference for E1039 Core & Analysis Software
PHG4ParametersContainer.cc
Go to the documentation of this file.
2 #include "PHG4Parameters.h"
3 
4 #include <pdbcalbase/PdbBankManager.h>
5 #include <pdbcalbase/PdbApplication.h>
6 #include <pdbcalbase/PdbBankList.h>
7 #include <pdbcalbase/PdbCalBank.h>
8 #include <pdbcalbase/PdbParameterMap.h>
9 #include <pdbcalbase/PdbParameterMapContainer.h>
10 
11 #include <phool/phool.h>
12 #include <phool/getClass.h>
13 
14 
15 #include <TBufferXML.h>
16 #include <TFile.h>
17 #include <TSystem.h>
18 
19 #include <algorithm>
20 #include <iostream>
21 #include <sstream>
22 
23 using namespace std;
24 
26  superdetectorname(name)
27 {}
28 
30 {
31  while(parametermap.begin() != parametermap.end())
32  {
33  delete parametermap.begin()->second;
34  parametermap.erase(parametermap.begin());
35  }
36 
37 }
38 
39 void
41 {
42 // this fill only existing detids - no new ones are created (if the PdbParameterMapContainer contains
43 // entries from another detector)
44  PdbParameterMapContainer::parConstRange begin_end = saveparamcontainer->get_ParameterMaps();
45  for (PdbParameterMapContainer::parIter iter = begin_end.first; iter != begin_end.second; ++iter)
46  {
47  Iterator pariter = parametermap.find(iter->first);
48  if (pariter != parametermap.end())
49  {
50  PHG4Parameters *params = pariter->second;
51  params->FillFrom(iter->second);
52  }
53  }
54  return;
55 }
56 
57 void
58 PHG4ParametersContainer::CreateAndFillFrom(const PdbParameterMapContainer *saveparamcontainer, const string &name)
59 {
60  PdbParameterMapContainer::parConstRange begin_end = saveparamcontainer->get_ParameterMaps();
61  for (PdbParameterMapContainer::parIter iter = begin_end.first; iter != begin_end.second; ++iter)
62  {
63  Iterator pariter = parametermap.find(iter->first);
64  if (pariter != parametermap.end())
65  {
66  PHG4Parameters *params = pariter->second;
67  params->FillFrom(iter->second);
68  }
69  else
70  {
71  PHG4Parameters *params = new PHG4Parameters(name);
72  params->FillFrom(iter->second);
73  AddPHG4Parameters(iter->first,params);
74  }
75  }
76  return;
77 }
78 
79 void
81 {
82  if (parametermap.find(layer) != parametermap.end())
83  {
84  cout << PHWHERE << " layer " << layer << " already exists for "
85  << (parametermap.find(layer))->second->Name() << endl;
86  gSystem->Exit(1);
87  }
88  parametermap[layer] = params;
89 }
90 
91 const PHG4Parameters *
93 {
94  map<int, PHG4Parameters *>::const_iterator iter = parametermap.find(layer);
95 if (iter == parametermap.end())
96  {
97  cout << "could not find parameters for layer " << layer
98  << endl;
99  return NULL;
100  }
101  return iter->second;
102 }
103 
106 {
107  map<int, PHG4Parameters *>::iterator iter = parametermap.find(layer);
108  if (iter == parametermap.end())
109  {
110  return NULL;
111  }
112  return iter->second;
113 }
114 
115 int
116 PHG4ParametersContainer::WriteToFile(const string &extension, const string &dir)
117 {
118  ostringstream fullpath;
119  ostringstream fnamestream;
120  PdbBankID bankID(0); // lets start at zero
121  PHTimeStamp TStart(0);
122  PHTimeStamp TStop(0xffffffff);
123  fullpath << dir;
124  // add / if directory lacks ending /
125  if (*(dir.rbegin()) != '/')
126  {
127  fullpath << "/";
128  }
129  fnamestream << superdetectorname << "_geoparams" << "-" << bankID.getInternalValue()
130  << "-" << TStart.getTics() << "-" << TStop.getTics() << "-" << time(0)
131  << "." << extension;
132  string fname = fnamestream.str();
133  std::transform(fname.begin(), fname.end(), fname.begin(), ::tolower);
134  fullpath << fname;
135 
136  cout <<"PHG4Parameters::WriteToFile - save to "<<fullpath.str()<<endl;
137 
140  TFile *f = TFile::Open(fullpath.str().c_str(), "recreate");
141  // force xml file writing to use extended precision shown experimentally
142  // to not modify input parameters (.15e)
143  string floatformat = TBufferXML::GetFloatFormat();
144  TBufferXML::SetFloatFormat("%.15e");
145  myparm->Write();
146  delete f;
147  // restore previous xml float format
148  TBufferXML::SetFloatFormat(floatformat.c_str());
149  cout << "sleeping 1 second to prevent duplicate inserttimes" << endl;
150  sleep(1);
151  return 0;
152 }
153 
154 int
156 {
157  PdbBankManager* bankManager = PdbBankManager::instance();
158  PdbApplication *application = bankManager->getApplication();
159  if (!application->startUpdate())
160  {
161  cout << PHWHERE << " Aborting, Database not writable" << endl;
162  application->abort();
163  exit(1);
164  }
165 
166  // Make a bank ID...
167  PdbBankID bankID(0); // lets start at zero
168  PHTimeStamp TStart(0);
169  PHTimeStamp TStop(0xffffffff);
170 
171  string tablename = superdetectorname + "_geoparams";
172  std::transform(tablename.begin(), tablename.end(), tablename.begin(),
173  ::tolower);
174  PdbCalBank *NewBank = bankManager->createBank("PdbParameterMapContainerBank", bankID,
175  "Geometry Parameters", TStart, TStop, tablename);
176  if (NewBank)
177  {
178  NewBank->setLength(1);
181  application->commit(NewBank);
182  delete NewBank;
183  }
184  else
185  {
186  cout << PHWHERE " Committing to DB failed" << endl;
187  return -1;
188  }
189  return 0;
190 }
191 
192 void
194 {
195  std::map<int, PHG4Parameters *>::const_iterator iter;
196  for (iter = parametermap.begin(); iter != parametermap.end(); ++iter)
197  {
198  PdbParameterMap *myparm = new PdbParameterMap();
199  iter->second->CopyToPdbParameterMap(myparm);
200  myparmap->AddPdbParameterMap(iter->first,myparm);
201  }
202  return;
203 }
204 
205 void
207 {
208  cout << "Name: " << Name() << endl;
209  map<int, PHG4Parameters *>::const_iterator iter;
210  for (iter = parametermap.begin(); iter != parametermap.end(); ++iter)
211  {
212  cout << "parameter detid: " << iter->first << endl;
213  iter->second->Print();
214  }
215  return;
216 }
217 
218 void
220 {
221  PdbParameterMapContainer *myparmap = findNode::getClass<PdbParameterMapContainer>(topNode, nodename);
222  if (! myparmap)
223  {
224  myparmap = new PdbParameterMapContainer();
226  new PHIODataNode<PdbParameterMapContainer>(myparmap, nodename);
227  topNode->addNode(newnode);
228  }
229  else
230  {
231  myparmap->Reset();
232  }
234  return;
235 }
236 
237 int
239 {
240  if (parametermap.find(detid) != parametermap.end())
241  {
242  return 1;
243  }
244  return 0;
245 }
#define NULL
Definition: Pdb.h:9
PHBoolean addNode(PHNode *)
PHG4ParametersContainer(const std::string &name="NONE")
int WriteToFile(const std::string &extension, const std::string &dir)
std::map< int, PHG4Parameters * > parametermap
void CreateAndFillFrom(const PdbParameterMapContainer *saveparamcontainer, const std::string &name)
void AddPHG4Parameters(const int layer, PHG4Parameters *params)
int ExistDetid(const int detid) const
void CopyToPdbParameterMapContainer(PdbParameterMapContainer *myparm)
const PHG4Parameters * GetParameters(const int layer) const
void FillFrom(const PdbParameterMapContainer *saveparamcontainer)
void SaveToNodeTree(PHCompositeNode *topNode, const std::string &nodename)
PHG4Parameters * GetParametersToModify(const int layer)
void FillFrom(const PdbParameterMap *saveparams)
time_t getTics() const
Definition: PHTimeStamp.cc:97
virtual PdbStatus abort()=0
virtual PdbStatus commit()=0
virtual PdbStatus startUpdate()=0
int getInternalValue() const
Definition: PdbBankID.h:26
static PdbBankManager * instance()
virtual PdbApplication * getApplication()=0
virtual PdbCalBank * createBank(const std::string &, PdbBankID, const std::string &, PHTimeStamp &, PHTimeStamp &, const std::string &)=0
virtual void setLength(size_t val)=0
virtual PdbCalChan & getEntry(size_t)=0
parConstRange get_ParameterMaps() const
void AddPdbParameterMap(const int layer, PdbParameterMap *params)
parMap::const_iterator parIter
std::pair< parIter, parIter > parConstRange
#define PHWHERE
Definition: phool.h:23