CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 // TODO: write and read time map
23 //
24 //
25 
27  EcalCondHeader& header,
30 
31  XercesDOMParser* parser = new XercesDOMParser;
32  parser->setValidationScheme(XercesDOMParser::Val_Never);
33  parser->setDoNamespaces(false);
34  parser->setDoSchema(false);
35 
36  parser->parse(filename.c_str());
37 
38  DOMDocument* xmlDoc = parser->getDocument();
39  if (!xmlDoc) {
40  std::cout << "EcalLaserAPDPNRatiosXMLTranslator::Error parsing document" << std::endl;
41  return -1;
42  }
43 
44  DOMElement* elementRoot = xmlDoc->getDocumentElement();
45 
46  xuti::readHeader(elementRoot, header);
47 
48  DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
49 
50  while (cellnode) {
51  float p1 = 0;
52  float p2 = 0;
53  float p3 = 0;
54  // edm::TimeStamp t1=0;
55  // edm::TimeStamp t2=0;
56  // edm::TimeStamp t3=0;
57 
58  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
59 
60  DOMNode* p1_node = getChildNode(cellnode, Laser_p1_tag);
61  GetNodeData(p1_node, p1);
62 
63  DOMNode* p2_node = getChildNode(cellnode, Laser_p2_tag);
64  GetNodeData(p2_node, p2);
65 
66  DOMNode* p3_node = getChildNode(cellnode, Laser_p3_tag);
67  GetNodeData(p3_node, p3);
68 
69  // DOMNode* t1_node = getChildNode(cellnode,Laser_t1_tag);
70  // GetNodeData(t1_node,t1);
71 
72  // DOMNode* t2_node = getChildNode(cellnode,Laser_t2_tag);
73  // GetNodeData(t3_node,t2);
74 
75  // DOMNode* p1_node = getChildNode(cellnode,Laser_t3_tag);
76  // GetNodeData(t3_node,t3);
77 
79  pair.p1 = p1;
80  pair.p2 = p2;
81  pair.p3 = p3;
82 
83  record.setValue(detid, pair);
84 
85  cellnode = cellnode->getNextSibling();
86 
87  while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
88  cellnode = cellnode->getNextSibling();
89  }
90 
91  delete parser;
93  return 0;
94 }
95 
97  const EcalCondHeader& header,
100 
101  std::fstream fs(filename.c_str(), ios::out);
102  fs << dumpXML(header, record);
103 
105 
106  return 0;
107 }
108 
110  const EcalLaserAPDPNRatios& record) {
111  unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
112 
113  DOMLSSerializer* writer = impl->createLSSerializer();
114  if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
115  writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
116 
117  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
118  DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(WeightGroups_tag.c_str()).ptr(), doctype);
119  DOMElement* root = doc->getDocumentElement();
120 
121  xuti::writeHeader(root, header);
122 
123  string Lasertag = "Laser", LMtag = "LM";
124  for (int cellid = 0; cellid < (int)record.getTimeMap().size(); cellid++) {
125  DOMElement* cellnode = doc->createElement(cms::xerces::uStr(Lasertag.c_str()).ptr());
126  root->appendChild(cellnode);
127  stringstream value_s;
128  value_s << cellid;
129  cellnode->setAttribute(cms::xerces::uStr(LMtag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
130 
131  long int t123[3];
132  t123[0] = (record.getTimeMap())[cellid].t1.value();
133  t123[1] = (record.getTimeMap())[cellid].t2.value();
134  t123[2] = (record.getTimeMap())[cellid].t3.value();
135  string Laser_t_tag[3] = {"t1", "t2", "t3"};
136  for (int i = 0; i < 3; i++) {
137  time_t t = t123[i] >> 32;
138  char buf[256];
139  struct tm lt;
140  localtime_r(&t, &lt);
141  strftime(buf, sizeof(buf), "%F %R:%S", &lt);
142  buf[sizeof(buf) - 1] = 0;
143  DOMDocument* subdoc = cellnode->getOwnerDocument();
144  DOMElement* new_node = subdoc->createElement(cms::xerces::uStr(Laser_t_tag[i].c_str()).ptr());
145  cellnode->appendChild(new_node);
146  std::stringstream value_ss;
147  value_ss << t123[i];
148  string newstr = value_ss.str() + " [" + string(buf) + "]";
149  DOMText* tvalue = subdoc->createTextNode(cms::xerces::uStr(newstr.c_str()).ptr());
150  new_node->appendChild(tvalue);
151  }
152  }
153 
154  for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
155  uint32_t rawid = EBDetId::unhashIndex(cellid);
156 
157  DOMElement* cellnode = writeCell(root, rawid);
158 
159  float p1 = (record.getLaserMap())[rawid].p1;
160  float p2 = (record.getLaserMap())[rawid].p2;
161  float p3 = (record.getLaserMap())[rawid].p3;
162 
163  WriteNodeWithValue(cellnode, Laser_p1_tag, p1);
164  WriteNodeWithValue(cellnode, Laser_p2_tag, p2);
165  WriteNodeWithValue(cellnode, Laser_p3_tag, p3);
166  }
167 
168  for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
169  if (!EEDetId::validHashIndex(cellid))
170  continue;
171 
172  uint32_t rawid = EEDetId::unhashIndex(cellid);
173 
174  DOMElement* cellnode = writeCell(root, rawid);
175 
176  float p1 = (record.getLaserMap())[rawid].p1;
177  float p2 = (record.getLaserMap())[rawid].p2;
178  float p3 = (record.getLaserMap())[rawid].p3;
179 
180  WriteNodeWithValue(cellnode, Laser_p1_tag, p1);
181  WriteNodeWithValue(cellnode, Laser_p2_tag, p2);
182  WriteNodeWithValue(cellnode, Laser_p3_tag, p3);
183  }
184 
185  std::string dump = cms::xerces::toString(writer->writeToString(root));
186  doc->release();
187  doctype->release();
188  writer->release();
189 
190  return dump;
191 }
const TString p2
Definition: fwPaths.cc:13
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:65
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
static const int MIN_HASH
Definition: EBDetId.h:149
const std::string Cell_tag("cell")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
const TString p1
Definition: fwPaths.cc:12
const std::string Laser_p1_tag("p1")
ZStr< XMLCh > uStr(char const *str)
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalLaserAPDPNRatios &record)
const std::string WeightGroups_tag("EcalWeightGroups")
Definition: DetId.h:17
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:110
tuple filename
Definition: lut2db_cfg.py:20
const std::string Laser_p2_tag("p2")
tuple cout
Definition: gather_cfg.py:144
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const DetId readCellId(xercesc::DOMElement *node)
Assuming.
tuple dump
OutputFilePath = cms.string(&#39;/tmp/zhokin/&#39;), OutputFileExt = cms.string(&#39;&#39;),.