CMS 3D CMS Logo

EcalGainRatiosXMLTranslator.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 #include <xercesc/dom/DOMNode.hpp>
5 #include <xercesc/dom/DOM.hpp>
6 #include <xercesc/parsers/XercesDOMParser.hpp>
9 #include <xercesc/util/XMLString.hpp>
10 #include <xercesc/sax/SAXException.hpp>
11 #include <xercesc/framework/LocalFileFormatTarget.hpp>
12 
17 
18 using namespace XERCES_CPP_NAMESPACE;
19 using namespace xuti;
20 using namespace std;
21 
24 
25  XercesDOMParser* parser = new XercesDOMParser;
26  parser->setValidationScheme(XercesDOMParser::Val_Never);
27  parser->setDoNamespaces(false);
28  parser->setDoSchema(false);
29 
30  parser->parse(filename.c_str());
31 
32  DOMDocument* xmlDoc = parser->getDocument();
33  if (!xmlDoc) {
34  std::cout << "EcalGainRatiosXMLTranslator::Error parsing document" << std::endl;
35  return -1;
36  }
37 
38  DOMElement* elementRoot = xmlDoc->getDocumentElement();
39 
40  xuti::readHeader(elementRoot, header);
41 
42  DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
43 
44  while (cellnode) {
45  float g12_6 = 0;
46  float g6_1 = 0;
47  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
48 
49  DOMNode* g12_6_node = getChildNode(cellnode, Gain12Over6_tag);
50  GetNodeData(g12_6_node, g12_6);
51 
52  DOMNode* g6_1_node = getChildNode(cellnode, Gain6Over1_tag);
53  GetNodeData(g6_1_node, g6_1);
54 
55  record[detid].setGain12Over6(g12_6);
56  record[detid].setGain6Over1(g6_1);
57 
58  cellnode = cellnode->getNextSibling();
59 
60  while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
61  cellnode = cellnode->getNextSibling();
62  }
63 
64  delete parser;
66  return 0;
67 }
68 
70  const EcalCondHeader& header,
71  const EcalGainRatios& record) {
73 
74  std::fstream fs(filename.c_str(), ios::out);
75  fs << dumpXML(header, record);
76 
78 
79  return 0;
80 }
81 
83  unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
84 
85  DOMLSSerializer* writer = impl->createLSSerializer();
86  if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
87  writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
88 
89  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
90  DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(GainRatios_tag.c_str()).ptr(), doctype);
91  DOMElement* root = doc->getDocumentElement();
92 
94  if (record.barrelItems().empty())
95  return std::string();
96  for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
97  uint32_t rawid = EBDetId::unhashIndex(cellid);
98 
99  if (record.find(rawid) == record.end())
100  continue;
101  if (!record[rawid].gain12Over6() && !record[rawid].gain6Over1())
102  continue;
103 
104  DOMElement* cellnode = writeCell(root, rawid);
105 
106  WriteNodeWithValue(cellnode, Gain12Over6_tag, record[rawid].gain12Over6());
107  WriteNodeWithValue(cellnode, Gain6Over1_tag, record[rawid].gain6Over1());
108  }
109 
110  if (record.endcapItems().empty())
111  return std::string();
112  for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
113  if (!EEDetId::validHashIndex(cellid))
114  continue;
115 
116  uint32_t rawid = EEDetId::unhashIndex(cellid);
117 
118  if (record.find(rawid) == record.end())
119  continue;
120  if (!record[rawid].gain12Over6() && !record[rawid].gain6Over1())
121  continue;
122 
123  DOMElement* cellnode = writeCell(root, rawid);
124 
125  WriteNodeWithValue(cellnode, Gain12Over6_tag, record[rawid].gain12Over6());
126  WriteNodeWithValue(cellnode, Gain6Over1_tag, record[rawid].gain6Over1());
127  }
128 
129  std::string dump = cms::xerces::toString(writer->writeToString(root));
130  doc->release();
131  doctype->release();
132  writer->release();
133 
134  return dump;
135 }
static int readXML(const std::string &filename, EcalCondHeader &header, EcalGainRatios &record)
const std::string Gain6Over1_tag("Gain6Over1")
def dumpXML(db, tag, since, filename='dump.xml')
void xercesTerminate()
Definition: Xerces.cc:23
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)
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalGainRatios &record)
static const int MIN_HASH
Definition: EBDetId.h:149
const std::string Cell_tag("cell")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
ZStr< XMLCh > uStr(char const *str)
Definition: DetId.h:17
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
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:110
static std::string dumpXML(const EcalCondHeader &header, const EcalGainRatios &record)
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const std::string GainRatios_tag("EcalGainRatios")
const DetId readCellId(xercesc::DOMElement *node)
Assuming.
const std::string Gain12Over6_tag("Gain12Over6")