Class Reference for E1039 Core & Analysis Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PHParametersContainer.cc
Go to the documentation of this file.
2 #include "PHParameters.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  PHParameters *params = pariter->second;
51  params->FillFrom(iter->second);
52  }
53  }
54  return;
55 }
56 
57 void
59 {
60  if (parametermap.find(layer) != parametermap.end())
61  {
62  cout << PHWHERE << " layer " << layer << " already exists for "
63  << (parametermap.find(layer))->second->Name() << endl;
64  gSystem->Exit(1);
65  }
66  parametermap[layer] = params;
67 }
68 
69 const PHParameters *
71 {
72  map<int, PHParameters *>::const_iterator iter = parametermap.find(layer);
73 if (iter == parametermap.end())
74  {
75  cout << "could not find parameters for layer " << layer
76  << endl;
77  return NULL;
78  }
79  return iter->second;
80 }
81 
84 {
85  map<int, PHParameters *>::iterator iter = parametermap.find(layer);
86  if (iter == parametermap.end())
87  {
88  return NULL;
89  }
90  return iter->second;
91 }
92 
93 int
94 PHParametersContainer::WriteToFile(const string &extension, const string &dir)
95 {
96  ostringstream fullpath;
97  ostringstream fnamestream;
98  PdbBankID bankID(0); // lets start at zero
99  PHTimeStamp TStart(0);
100  PHTimeStamp TStop(0xffffffff);
101  fullpath << dir;
102  // add / if directory lacks ending /
103  if (*(dir.rbegin()) != '/')
104  {
105  fullpath << "/";
106  }
107  fnamestream << superdetectorname << "_geoparams" << "-" << bankID.getInternalValue()
108  << "-" << TStart.getTics() << "-" << TStop.getTics() << "-" << time(0)
109  << "." << extension;
110  string fname = fnamestream.str();
111  std::transform(fname.begin(), fname.end(), fname.begin(), ::tolower);
112  fullpath << fname;
113 
114  cout <<"PHParameters::WriteToFile - save to "<<fullpath.str()<<endl;
115 
118  TFile *f = TFile::Open(fullpath.str().c_str(), "recreate");
119  // force xml file writing to use extended precision shown experimentally
120  // to not modify input parameters (.15e)
121  string floatformat = TBufferXML::GetFloatFormat();
122  TBufferXML::SetFloatFormat("%.15e");
123  myparm->Write();
124  delete f;
125  // restore previous xml float format
126  TBufferXML::SetFloatFormat(floatformat.c_str());
127  cout << "sleeping 1 second to prevent duplicate inserttimes" << endl;
128  sleep(1);
129  return 0;
130 }
131 
132 int
134 {
135  PdbBankManager* bankManager = PdbBankManager::instance();
136  PdbApplication *application = bankManager->getApplication();
137  if (!application->startUpdate())
138  {
139  cout << PHWHERE << " Aborting, Database not writable" << endl;
140  application->abort();
141  exit(1);
142  }
143 
144  // Make a bank ID...
145  PdbBankID bankID(0); // lets start at zero
146  PHTimeStamp TStart(0);
147  PHTimeStamp TStop(0xffffffff);
148 
149  string tablename = superdetectorname + "_geoparams";
150  std::transform(tablename.begin(), tablename.end(), tablename.begin(),
151  ::tolower);
152  PdbCalBank *NewBank = bankManager->createBank("PdbParameterMapContainerBank", bankID,
153  "Geometry Parameters", TStart, TStop, tablename);
154  if (NewBank)
155  {
156  NewBank->setLength(1);
159  application->commit(NewBank);
160  delete NewBank;
161  }
162  else
163  {
164  cout << PHWHERE " Committing to DB failed" << endl;
165  return -1;
166  }
167  return 0;
168 }
169 
170 void
172 {
173  std::map<int, PHParameters *>::const_iterator iter;
174  for (iter = parametermap.begin(); iter != parametermap.end(); ++iter)
175  {
176  PdbParameterMap *myparm = new PdbParameterMap();
177  iter->second->CopyToPdbParameterMap(myparm);
178  myparmap->AddPdbParameterMap(iter->first,myparm);
179  }
180  return;
181 }
182 
183 void
185 {
186  cout << "Name: " << Name() << endl;
187  map<int, PHParameters *>::const_iterator iter;
188  for (iter = parametermap.begin(); iter != parametermap.end(); ++iter)
189  {
190  cout << "parameter detid: " << iter->first << endl;
191  iter->second->Print();
192  }
193  return;
194 }
195 
196 void
198 {
199  PdbParameterMapContainer *myparmap = findNode::getClass<PdbParameterMapContainer>(topNode, nodename);
200  if (! myparmap)
201  {
202  myparmap = new PdbParameterMapContainer();
204  new PHIODataNode<PdbParameterMapContainer>(myparmap, nodename);
205  topNode->addNode(newnode);
206  }
207  else
208  {
209  myparmap->Reset();
210  }
212  return;
213 }
214 
215 int
216 PHParametersContainer::ExistDetid(const int detid) const
217 {
218  if (parametermap.find(detid) != parametermap.end())
219  {
220  return 1;
221  }
222  return 0;
223 }
#define NULL
Definition: Pdb.h:9
PHBoolean addNode(PHNode *)
std::string Name() const
void CopyToPdbParameterMapContainer(PdbParameterMapContainer *myparm)
int WriteToFile(const std::string &extension, const std::string &dir)
PHParametersContainer(const std::string &name="NONE")
std::map< int, PHParameters * > parametermap
int ExistDetid(const int detid) const
void SaveToNodeTree(PHCompositeNode *topNode, const std::string &nodename)
PHParameters * GetParametersToModify(const int layer)
const PHParameters * GetParameters(const int layer) const
void AddPHParameters(const int layer, PHParameters *params)
void FillFrom(const PdbParameterMapContainer *saveparamcontainer)
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