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 
25 
27 
28  XercesDOMParser* parser = new XercesDOMParser;
29  parser->setValidationScheme( XercesDOMParser::Val_Never );
30  parser->setDoNamespaces( false );
31  parser->setDoSchema( false );
32 
33  parser->parse(filename.c_str());
34 
35  DOMDocument* xmlDoc = parser->getDocument();
36  if (!xmlDoc) {
37  std::cout << "EcalGainRatiosXMLTranslator::Error parsing document" << std::endl;
38  return -1;
39  }
40 
41  DOMElement* elementRoot = xmlDoc->getDocumentElement();
42 
43  xuti::readHeader(elementRoot,header);
44 
45  DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
46 
47  while(cellnode)
48  {
49  float g12_6 = 0;
50  float g6_1 = 0;
51  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
52 
53  DOMNode* g12_6_node = getChildNode(cellnode,Gain12Over6_tag);
54  GetNodeData(g12_6_node,g12_6);
55 
56  DOMNode* g6_1_node = getChildNode(cellnode,Gain6Over1_tag);
57  GetNodeData(g6_1_node,g6_1);
58 
59  record[detid].setGain12Over6(g12_6);
60  record[detid].setGain6Over1(g6_1);
61 
62  cellnode = cellnode->getNextSibling();
63 
64  while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
65  cellnode = cellnode->getNextSibling();
66 
67  }
68 
69  delete parser;
71  return 0;
72 }
73 
75  const EcalCondHeader& header,
76  const EcalGainRatios& record){
78 
79  std::fstream fs(filename.c_str(),ios::out);
80  fs<< dumpXML(header,record);
81 
83 
84  return 0;
85 }
86 
87 
89 
90  unique_ptr<DOMImplementation> impl( DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
91 
92  DOMLSSerializer* writer = impl->createLSSerializer();
93  if( writer->getDomConfig()->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true ))
94  writer->getDomConfig()->setParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true );
95 
96  DOMDocumentType* doctype = impl->createDocumentType( cms::xerces::uStr("XML").ptr(), nullptr, nullptr );
97  DOMDocument* doc = impl->createDocument( nullptr, cms::xerces::uStr(GainRatios_tag.c_str()).ptr(), doctype );
98  DOMElement* root = doc->getDocumentElement();
99 
100  xuti::writeHeader(root,header);
101  if (record.barrelItems().empty()) return std::string();
102  for(int cellid = EBDetId::MIN_HASH;
104  ++cellid)
105  {
106 
107  uint32_t rawid = EBDetId::unhashIndex(cellid);
108 
109  if (record.find(rawid) == record.end()) continue;
110  if(!record[rawid].gain12Over6() && !record[rawid].gain6Over1()) continue;
111 
112  DOMElement* cellnode=writeCell(root,rawid);
113 
114  WriteNodeWithValue(cellnode,Gain12Over6_tag,record[rawid].gain12Over6());
115  WriteNodeWithValue(cellnode,Gain6Over1_tag,record[rawid].gain6Over1());
116  }
117 
118  if (record.endcapItems().empty()) return std::string();
119  for(int cellid = 0;
121  ++cellid)
122  {
123  if(!EEDetId::validHashIndex(cellid)) continue;
124 
125  uint32_t rawid = EEDetId::unhashIndex(cellid);
126 
127  if (record.find(rawid) == record.end()) continue;
128  if(!record[rawid].gain12Over6() && !record[rawid].gain6Over1()) continue;
129 
130  DOMElement* cellnode=writeCell(root,rawid);
131 
132  WriteNodeWithValue(cellnode,Gain12Over6_tag,record[rawid].gain12Over6());
133  WriteNodeWithValue(cellnode,Gain6Over1_tag,record[rawid].gain6Over1());
134 
135 
136  }
137 
138 
139  std::string dump = cms::xerces::toString( writer->writeToString( root ));
140  doc->release();
141  doctype->release();
142  writer->release();
143 
144  return dump;
145 }
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')
JetCorrectorParameters::Record record
Definition: classes.h:7
void xercesTerminate()
Definition: Xerces.cc:23
const Items & barrelItems() const
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)
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:156
const std::string Cell_tag("cell")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
ZStr< XMLCh > uStr(char const *str)
Definition: DetId.h:18
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:114
const_iterator find(uint32_t rawId) const
const_iterator end() const
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 Items & endcapItems() const
const DetId readCellId(xercesc::DOMElement *node)
Assuming.
const std::string Gain12Over6_tag("Gain12Over6")