CMS 3D CMS Logo

EcalFloatCondObjectContainerXMLTranslator.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 
5 #include <xercesc/dom/DOMNode.hpp>
6 #include <xercesc/dom/DOM.hpp>
7 #include <xercesc/parsers/XercesDOMParser.hpp>
10 #include <xercesc/util/XMLString.hpp>
11 #include <xercesc/sax/SAXException.hpp>
12 #include <xercesc/framework/LocalFileFormatTarget.hpp>
13 
16 
17 using namespace XERCES_CPP_NAMESPACE;
18 using namespace xuti;
19 using namespace std;
20 
21 
22 int
26 
28 
29  XercesDOMParser* parser = new XercesDOMParser;
30  parser->setValidationScheme( XercesDOMParser::Val_Never );
31  parser->setDoNamespaces( false );
32  parser->setDoSchema( false );
33 
34  parser->parse(filename.c_str());
35 
36  DOMDocument* xmlDoc = parser->getDocument();
37 
38 
39  if (!xmlDoc) {
40  std::cout << "EcalFloatCondObjectContainerXMLTranslator::Error parsing document" << std::endl;
41  return -1;
42  }
43 
44  DOMElement* elementRoot = xmlDoc->getDocumentElement();
45 
46  xuti::readHeader(elementRoot, header);
47 
48  // get the first cell node
49  DOMNode * cellnode=getChildNode(elementRoot,Cell_tag);
50 
51  // loop on cell nodes
52  while (cellnode){
53 
54  float val=0;
55 
56  // read id
57  DetId detid= readCellId(dynamic_cast<DOMElement*>(cellnode));
58 
59  // read value
60  DOMNode * c_node = getChildNode(cellnode,Value_tag);
61  GetNodeData(c_node,val);
62 
63  // fill record
64  record[detid]=val;
65 
66  // get next cell
67  cellnode= cellnode->getNextSibling();
68 
69  while (cellnode&& cellnode->getNodeType( ) != DOMNode::ELEMENT_NODE)
70  cellnode= cellnode->getNextSibling();
71 
72 
73  }
74 
75 
76  delete parser;
78  return 0;
79 
80 }
81 
82 
83 
84 std::vector<float>
88  readXML(filename,header,record);
89 
90  return record.barrelItems();
91 
92 }
93 
94 
95 std::vector<float>
99  readXML(filename,header,record);
100 
101  return record.endcapItems();
102 
103 }
104 
105 
106 
109  const EcalCondHeader& header,
110  const std::vector<float>& eb,
111  const std::vector<float>& ee){
112 
113 
114  if (eb.size() != EBDetId::kSizeForDenseIndexing){
115  std::cerr<<"Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Barrel array size: "
116  <<eb.size() << " should be "<< EBDetId::kSizeForDenseIndexing<< std::endl;
117  return std::string("");
118  }
119 
120  if (ee.size() != EEDetId::kSizeForDenseIndexing){
121  std::cerr<<"Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Endcap array size: "
122  <<ee.size() << " should be "<< EEDetId::kSizeForDenseIndexing<< std::endl;
123  return std::string("");
124  }
125 
127 
128  for (int cellid = 0;
130  ++cellid){// loop on EB cells
131 
132  uint32_t rawid = EBDetId::unhashIndex(cellid);
133  record[rawid] = eb[cellid];
134  }
135 
136  for (int cellid = 0;
138  ++cellid){// loop on EE cells
139 
140 
141 
142  if (EEDetId::validHashIndex(cellid)){
143  uint32_t rawid = EEDetId::unhashIndex(cellid);
144 
145  record[rawid]=ee[cellid];
146  } // if
147  }
148 
149  return dumpXML(header,record);
150 
151 
152 }
153 
156  const EcalCondHeader& header,
158 
160 
161  unique_ptr<DOMImplementation> impl( DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
162 
163  DOMLSSerializer* writer = impl->createLSSerializer();
164  if( writer->getDomConfig()->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true ))
165  writer->getDomConfig()->setParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true );
166 
167  DOMDocumentType* doctype = impl->createDocumentType( cms::xerces::uStr("XML").ptr(), 0, 0 );
168  DOMDocument* doc = impl->createDocument( 0, cms::xerces::uStr(EcalFloatCondObjectContainer_tag.c_str()).ptr(), doctype );
169  DOMElement* root = doc->getDocumentElement();
170 
171  xuti::writeHeader(root, header);
172 
173  for (int cellid = EBDetId::MIN_HASH;
175  ++cellid){// loop on EB cells
176 
177  uint32_t rawid= EBDetId::unhashIndex(cellid);
179  record.find(rawid);
180  if (value_ptr==record.end()) continue; // cell absent from original record
181 
182  DOMElement* cellnode=writeCell(root,rawid);
183 
184  WriteNodeWithValue(cellnode,Value_tag,*value_ptr);
185 
186 
187  } // loop on EB cells
188 
189 
190 
191  for (int cellid = 0;
193  ++cellid){// loop on EE cells
194 
195  if (!EEDetId::validHashIndex(cellid)) continue;
196 
197  uint32_t rawid= EEDetId::unhashIndex(cellid);
199  record.find(rawid);
200  if (value_ptr==record.end()) continue; // cell absent from original record
201 
202 
203  DOMElement* cellnode= writeCell(root,rawid);
204  WriteNodeWithValue(cellnode,Value_tag,*value_ptr);
205 
206 
207  } // loop on EE cells
208 
209 
210  std::string dump = cms::xerces::toString( writer->writeToString( root ));
211  doc->release();
212  doctype->release();
213  writer->release();
214 
216 
217  return dump;
218 }
219 
220 
221 
222 int
224  const EcalCondHeader& header,
226 
228 
229  std::fstream fs(filename.c_str(),ios::out);
230  fs<< dumpXML(header,record);
231 
233 
234  return 0;
235 }
def readXML(fileName)
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalFloatCondObjectContainer &record)
static std::vector< float > barrelfromXML(const std::string &filename)
def dumpXML(db, tag, since, filename='dump.xml')
JetCorrectorParameters::Record record
Definition: classes.h:7
void xercesTerminate()
Definition: Xerces.cc:23
const std::string EcalFloatCondObjectContainer_tag("EcalFloatCondObjectContainer")
const Items & barrelItems() const
void xercesInitialize()
Definition: Xerces.cc:18
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:99
static int readXML(const std::string &filename, EcalCondHeader &header, EcalFloatCondObjectContainer &record)
void WriteNodeWithValue(xercesc::DOMNode *parentNode, const std::string &tag, const T &value)
write a node with
const std::string Value_tag("Value")
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 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)
static std::vector< float > endcapfromXML(const std::string &filename)
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
std::vector< Item >::const_iterator const_iterator
static std::string dumpXML(const EcalCondHeader &header, const std::vector< float > &eb, const std::vector< float > &ee)
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
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const Items & endcapItems() const
const DetId readCellId(xercesc::DOMElement *node)
Assuming.