CMS 3D CMS Logo

EcalWeightGroupXMLTranslator.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 
16 
17 using namespace XERCES_CPP_NAMESPACE;
18 using namespace xuti;
19 using namespace std;
20 
25 
26  XercesDOMParser* parser = new XercesDOMParser;
27  parser->setValidationScheme(XercesDOMParser::Val_Never);
28  parser->setDoNamespaces(false);
29  parser->setDoSchema(false);
30 
31  parser->parse(filename.c_str());
32 
33  DOMDocument* xmlDoc = parser->getDocument();
34  if (!xmlDoc) {
35  std::cout << "EcalWeightGroupXMLTranslator::Error parsing document" << std::endl;
36  return -1;
37  }
38 
39  DOMElement* elementRoot = xmlDoc->getDocumentElement();
40 
41  xuti::readHeader(elementRoot, header);
42  // get the first cell node
43  DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
44 
45  // loop on cell nodes
46  while (cellnode) {
47  unsigned int group = 0;
48 
49  // read id
50  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
51 
52  // read constant
53  DOMNode* c_node = getChildNode(cellnode, WeightGroup_tag);
54  GetNodeData(c_node, group);
55 
56  // fill record
57  record[detid] = EcalXtalGroupId(group);
58 
59  // get next cell
60  cellnode = cellnode->getNextSibling();
61 
62  while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
63  cellnode = cellnode->getNextSibling();
64  }
65 
66  delete parser;
68  return 0;
69 }
70 
72  const EcalCondHeader& header,
75 
76  std::fstream fs(filename.c_str(), ios::out);
77  fs << dumpXML(header, record);
78 
80 
81  return 0;
82 }
83 
85  unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
86 
87  DOMLSSerializer* writer = impl->createLSSerializer();
88  if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
89  writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
90 
91  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
92  DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(WeightGroups_tag.c_str()).ptr(), doctype);
93  DOMElement* root = doc->getDocumentElement();
94 
95  xuti::writeHeader(root, header);
96 
97  for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) { // loop on EB cells
98 
99  uint32_t rawid = EBDetId::unhashIndex(cellid);
100 
101  //if (!record[rawid]) continue; // cell absent from original record
102 
103  DOMElement* cellnode = writeCell(root, rawid);
104 
105  WriteNodeWithValue(cellnode, WeightGroup_tag, record[rawid].id());
106 
107  } // loop on EB cells
108 
109  for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) { // loop on EE cells
110 
111  if (!EEDetId::validHashIndex(cellid))
112  continue;
113 
114  uint32_t rawid = EEDetId::unhashIndex(cellid);
115  //xif (!record[rawid]) continue; // cell absent from original record
116 
117  DOMElement* cellnode = writeCell(root, rawid);
118  WriteNodeWithValue(cellnode, WeightGroup_tag, record[rawid].id());
119 
120  } // loop on EE cells
121 
122  std::string dump = cms::xerces::toString(writer->writeToString(root));
123  doc->release();
124  doctype->release();
125  writer->release();
126 
127  return dump;
128 }
def dumpXML(db, tag, since, filename='dump.xml')
JetCorrectorParameters::Record record
Definition: classes.h:7
void xercesTerminate()
Definition: Xerces.cc:23
void xercesInitialize()
Definition: Xerces.cc:18
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:65
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalWeightXtalGroups &record)
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
const std::string WeightGroup_tag("EcalWeightGroup")
std::string toString(XMLCh const *toTranscode)
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
static int readXML(const std::string &filename, EcalCondHeader &header, EcalWeightXtalGroups &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)
const std::string WeightGroups_tag("EcalWeightGroups")
static std::string dumpXML(const EcalCondHeader &header, const EcalWeightXtalGroups &record)
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
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const DetId readCellId(xercesc::DOMElement *node)
Assuming.