CMS 3D CMS Logo

EcalLaserAPDPNRatiosXMLTranslator.cc
Go to the documentation of this file.
7 #include <xercesc/dom/DOMNode.hpp>
8 #include <xercesc/dom/DOM.hpp>
9 #include <xercesc/parsers/XercesDOMParser.hpp>
10 #include <xercesc/util/XMLString.hpp>
11 #include <xercesc/sax/SAXException.hpp>
12 #include <xercesc/framework/LocalFileFormatTarget.hpp>
13 #include <iostream>
14 #include <sstream>
15 #include <fstream>
16 
17 using namespace XERCES_CPP_NAMESPACE;
18 using namespace xuti;
19 using namespace std;
20 
21 
22 //
23 // TODO: write and read time map
24 //
25 //
26 
27 
28 
32 
33 
34 
36 
37  XercesDOMParser* parser = new XercesDOMParser;
38  parser->setValidationScheme( XercesDOMParser::Val_Never );
39  parser->setDoNamespaces( false );
40  parser->setDoSchema( false );
41 
42  parser->parse(filename.c_str());
43 
44  DOMDocument* xmlDoc = parser->getDocument();
45  if (!xmlDoc) {
46  std::cout << "EcalLaserAPDPNRatiosXMLTranslator::Error parsing document" << std::endl;
47  return -1;
48  }
49 
50  DOMElement* elementRoot = xmlDoc->getDocumentElement();
51 
52  xuti::readHeader(elementRoot,header);
53 
54  DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
55 
56  while(cellnode)
57  {
58  float p1 = 0;
59  float p2 = 0;
60  float p3 = 0;
61 // edm::TimeStamp t1=0;
62 // edm::TimeStamp t2=0;
63 // edm::TimeStamp t3=0;
64 
65 
66  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
67 
68  DOMNode* p1_node = getChildNode(cellnode,Laser_p1_tag);
69  GetNodeData(p1_node,p1);
70 
71  DOMNode* p2_node = getChildNode(cellnode,Laser_p2_tag);
72  GetNodeData(p2_node,p2);
73 
74  DOMNode* p3_node = getChildNode(cellnode,Laser_p3_tag);
75  GetNodeData(p3_node,p3);
76 
77 // DOMNode* t1_node = getChildNode(cellnode,Laser_t1_tag);
78 // GetNodeData(t1_node,t1);
79 
80 // DOMNode* t2_node = getChildNode(cellnode,Laser_t2_tag);
81 // GetNodeData(t3_node,t2);
82 
83 // DOMNode* p1_node = getChildNode(cellnode,Laser_t3_tag);
84 // GetNodeData(t3_node,t3);
85 
86 
88  pair.p1 =p1;
89  pair.p2 =p2;
90  pair.p3 =p3;
91 
92  record.setValue(detid,pair);
93 
94  cellnode = cellnode->getNextSibling();
95 
96  while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
97  cellnode = cellnode->getNextSibling();
98 
99 
100  }
101 
102  delete parser;
104  return 0;
105 }
106 
108  const EcalCondHeader& header,
110 
112 
113  std::fstream fs(filename.c_str(),ios::out);
114  fs<< dumpXML(header,record);
115 
117 
118  return 0;
119 }
120 
122  const EcalCondHeader& header,
124 
125  unique_ptr<DOMImplementation> impl( DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
126 
127  DOMLSSerializer* writer = impl->createLSSerializer();
128  if( writer->getDomConfig()->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true ))
129  writer->getDomConfig()->setParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true );
130 
131  DOMDocumentType* doctype = impl->createDocumentType( cms::xerces::uStr("XML").ptr(), nullptr, nullptr );
132  DOMDocument* doc = impl->createDocument( nullptr, cms::xerces::uStr(WeightGroups_tag.c_str()).ptr(), doctype );
133  DOMElement* root = doc->getDocumentElement();
134 
135  xuti::writeHeader(root,header);
136 
137  string Lasertag = "Laser", LMtag = "LM";
138  for(int cellid = 0; cellid < (int)record.getTimeMap().size(); cellid++) {
139 
140  DOMElement* cellnode = doc->createElement( cms::xerces::uStr(Lasertag.c_str()).ptr());
141  root->appendChild(cellnode);
142  stringstream value_s;
143  value_s << cellid;
144  cellnode->setAttribute(cms::xerces::uStr(LMtag.c_str()).ptr(),
145  cms::xerces::uStr(value_s.str().c_str()).ptr());
146 
147  long int t123[3];
148  t123[0]=(record.getTimeMap())[cellid].t1.value();
149  t123[1]=(record.getTimeMap())[cellid].t2.value();
150  t123[2]=(record.getTimeMap())[cellid].t3.value();
151  string Laser_t_tag[3] = {"t1","t2","t3"};
152  for(int i = 0; i < 3; i++) {
153  time_t t = t123[i] >> 32;
154  char buf[256];
155  struct tm lt;
156  localtime_r(&t, &lt);
157  strftime(buf, sizeof(buf), "%F %R:%S", &lt);
158  buf[sizeof(buf)-1] = 0;
159  DOMDocument* subdoc = cellnode->getOwnerDocument();
160  DOMElement* new_node = subdoc->createElement(cms::xerces::uStr(Laser_t_tag[i].c_str()).ptr());
161  cellnode->appendChild(new_node);
162  std::stringstream value_ss;
163  value_ss << t123[i];
164  string newstr = value_ss.str() + " [" + string(buf) +"]";
165  DOMText* tvalue =
166  subdoc->createTextNode(cms::xerces::uStr(newstr.c_str()).ptr());
167  new_node->appendChild(tvalue);
168  }
169  }
170 
171  for(int cellid = EBDetId::MIN_HASH;
173  ++cellid)
174  {
175 
176  uint32_t rawid = EBDetId::unhashIndex(cellid);
177 
178  DOMElement* cellnode= writeCell(root,rawid);
179 
180  float p1=(record.getLaserMap())[rawid].p1;
181  float p2=(record.getLaserMap())[rawid].p2;
182  float p3=(record.getLaserMap())[rawid].p3;
183 
184  WriteNodeWithValue(cellnode,Laser_p1_tag,p1);
185  WriteNodeWithValue(cellnode,Laser_p2_tag,p2);
186  WriteNodeWithValue(cellnode,Laser_p3_tag,p3);
187 
188  }
189 
190 
191 
192 
193  for(int cellid = 0;
195  ++cellid)
196  {
197 
198  if(!EEDetId::validHashIndex(cellid)) continue;
199 
200  uint32_t rawid = EEDetId::unhashIndex(cellid);
201 
202 
203  DOMElement* cellnode=writeCell(root,rawid);
204 
205  float p1=(record.getLaserMap())[rawid].p1;
206  float p2=(record.getLaserMap())[rawid].p2;
207  float p3=(record.getLaserMap())[rawid].p3;
208 
209  WriteNodeWithValue(cellnode,Laser_p1_tag,p1);
210  WriteNodeWithValue(cellnode,Laser_p2_tag,p2);
211  WriteNodeWithValue(cellnode,Laser_p3_tag,p3);
212  }
213 
214  std::string dump = cms::xerces::toString(writer->writeToString( root ));
215  doc->release();
216  doctype->release();
217  writer->release();
218 
219  return dump;
220 }
def dumpXML(db, tag, since, filename='dump.xml')
JetCorrectorParameters::Record record
Definition: classes.h:7
static int readXML(const std::string &filename, EcalCondHeader &header, EcalLaserAPDPNRatios &record)
void xercesTerminate()
Definition: Xerces.cc:23
static std::string dumpXML(const EcalCondHeader &header, const EcalLaserAPDPNRatios &record)
const std::string Laser_p3_tag("p3")
void xercesInitialize()
Definition: Xerces.cc:18
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:99
void WriteNodeWithValue(xercesc::DOMNode *parentNode, const std::string &tag, const T &value)
write a node with
static bool validHashIndex(int i)
Definition: EEDetId.h:239
std::string toString(XMLCh const *toTranscode)
const EcalLaserAPDPNRatiosMap & getLaserMap() const
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
auto const T2 &decltype(t1.eta()) t2
Definition: deltaR.h:16
static const int MIN_HASH
Definition: EBDetId.h:156
const std::string Cell_tag("cell")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
const std::string Laser_p1_tag("p1")
ZStr< XMLCh > uStr(char const *str)
double p2[4]
Definition: TauolaWrapper.h:90
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalLaserAPDPNRatios &record)
const std::string WeightGroups_tag("EcalWeightGroups")
Definition: DetId.h:18
const EcalLaserTimeStampMap & getTimeMap() const
void GetNodeData(xercesc::DOMNode *node, T &value)
get the node data
xercesc::DOMNode * getChildNode(xercesc::DOMNode *node, const std::string &nodename)
get the child of
void setValue(uint32_t rawId, const EcalLaserAPDPNpair &value)
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:114
double p1[4]
Definition: TauolaWrapper.h:89
const std::string Laser_p2_tag("p2")
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const DetId readCellId(xercesc::DOMElement *node)
Assuming.
double p3[4]
Definition: TauolaWrapper.h:91