CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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>
8 #include <xercesc/util/XMLString.hpp>
9 #include <xercesc/sax/SAXException.hpp>
10 #include <xercesc/framework/LocalFileFormatTarget.hpp>
11 
12 
17 
18 
19 using namespace XERCES_CPP_NAMESPACE;
20 using namespace xuti;
21 using namespace std;
22 
23 
24 
25 
26 
27 
29  EcalCondHeader& header,
31 
32 
33 
35 
36  XercesDOMParser* parser = new XercesDOMParser;
37  parser->setValidationScheme( XercesDOMParser::Val_Never );
38  parser->setDoNamespaces( false );
39  parser->setDoSchema( false );
40 
41  parser->parse(filename.c_str());
42 
43  DOMDocument* xmlDoc = parser->getDocument();
44  if (!xmlDoc) {
45  std::cout << "EcalGainRatiosXMLTranslator::Error parsing document" << std::endl;
46  return -1;
47  }
48 
49  DOMElement* elementRoot = xmlDoc->getDocumentElement();
50 
51  xuti::readHeader(elementRoot,header);
52 
53  DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
54 
55  while(cellnode)
56  {
57  float g12_6 = 0;
58  float g6_1 = 0;
59  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
60 
61  DOMNode* g12_6_node = getChildNode(cellnode,Gain12Over6_tag);
62  GetNodeData(g12_6_node,g12_6);
63 
64  DOMNode* g6_1_node = getChildNode(cellnode,Gain6Over1_tag);
65  GetNodeData(g6_1_node,g6_1);
66 
67  record[detid].setGain12Over6(g12_6);
68  record[detid].setGain6Over1(g6_1);
69 
70  cellnode = cellnode->getNextSibling();
71 
72  while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
73  cellnode = cellnode->getNextSibling();
74 
75  }
76 
77  delete parser;
79  return 0;
80 
81 
82 }
83 
84 
85 
86 
87 
89  const EcalCondHeader& header,
90  const EcalGainRatios& record){
91  std::fstream fs(filename.c_str(),ios::out);
92  fs<< dumpXML(header,record);
93  return 0;
94 }
95 
96 
98 
100 
101  DOMImplementation* impl =
102  DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
103 
104  DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
105  writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
106 
107  DOMDocumentType* doctype = impl->createDocumentType(fromNative("XML").c_str(), 0, 0 );
108  DOMDocument * doc =
109  impl->createDocument( 0, fromNative(GainRatios_tag).c_str(), doctype );
110 
111 
112  doc->setEncoding(fromNative("UTF-8").c_str() );
113  doc->setStandalone(true);
114  doc->setVersion(fromNative("1.0").c_str() );
115 
116 
117  DOMElement* root = doc->getDocumentElement();
118 
119 
120 
121  xuti::writeHeader(root,header);
122  if (!record.barrelItems().size()) return std::string();
123  for(int cellid = EBDetId::MIN_HASH;
125  ++cellid)
126  {
127 
128  uint32_t rawid = EBDetId::unhashIndex(cellid);
129 
130  if (record.find(rawid) == record.end()) continue;
131  if(!record[rawid].gain12Over6() && !record[rawid].gain6Over1()) continue;
132 
133  DOMElement* cellnode=writeCell(root,rawid);
134 
135  WriteNodeWithValue(cellnode,Gain12Over6_tag,record[rawid].gain12Over6());
136  WriteNodeWithValue(cellnode,Gain6Over1_tag,record[rawid].gain6Over1());
137  }
138 
139  if (!record.endcapItems().size()) return std::string();
140  for(int cellid = 0;
142  ++cellid)
143  {
144  if(!EEDetId::validHashIndex(cellid)) continue;
145 
146  uint32_t rawid = EEDetId::unhashIndex(cellid);
147 
148  if (record.find(rawid) == record.end()) continue;
149  if(!record[rawid].gain12Over6() && !record[rawid].gain6Over1()) continue;
150 
151  DOMElement* cellnode=writeCell(root,rawid);
152 
153  WriteNodeWithValue(cellnode,Gain12Over6_tag,record[rawid].gain12Over6());
154  WriteNodeWithValue(cellnode,Gain6Over1_tag,record[rawid].gain6Over1());
155 
156 
157  }
158 
159 
160  std::string dump= toNative(writer->writeToString(*root));
161  doc->release();
162  return dump;
163 }
static int readXML(const std::string &filename, EcalCondHeader &header, EcalGainRatios &record)
const std::string Gain6Over1_tag("Gain6Over1")
JetCorrectorParameters::Record record
Definition: classes.h:7
void xercesTerminate()
Definition: Xerces.cc:22
const Items & barrelItems() const
void xercesInitialize()
Definition: Xerces.cc:17
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
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
tuple out
Definition: dbtoconf.py:99
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
tuple filename
Definition: lut2db_cfg.py:20
const_iterator find(uint32_t rawId) const
XercesString fromNative(const char *str)
Definition: XercesString.h:31
tuple cout
Definition: gather_cfg.py:121
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.
std::string toNative(const XMLCh *str)
Definition: XercesString.h:42
const std::string Gain12Over6_tag("Gain12Over6")
string root
initialization
Definition: dbtoconf.py:70