CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EcalPulseSymmCovariancesXMLTranslator.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 #include <vector>
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 #include <TString.h>
14 
19 
20 using namespace XERCES_CPP_NAMESPACE;
21 using namespace xuti;
22 using namespace std;
23 
25  EcalCondHeader& header,
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  if (!xmlDoc) {
38  std::cout << "EcalPulseSymmCovariancesXMLTranslator::Error parsing document" << std::endl;
39  return -1;
40  }
41 
42  DOMElement* elementRoot = xmlDoc->getDocumentElement();
43 
44  xuti::readHeader(elementRoot, header);
45 
46  DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
47 
48  int chan = 0;
49  while (cellnode) {
50  // std::cout << " Channel " << chan << std::endl;
52 
53  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
54  // std::cout << " readCell Id Channel " << chan << " tag " << mean12_tag << std::endl;
55 
56  std::vector<std::string> pulsecov_tag(
57  static_cast<size_t>(EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2));
58  for (int i = 0; i < EcalPulseShape::TEMPLATESAMPLES; ++i) {
59  for (int j = 0; j < EcalPulseShape::TEMPLATESAMPLES; ++j) {
60  int k = -1;
61  if (j >= i)
62  k = j + (EcalPulseShape::TEMPLATESAMPLES - 1) * i;
63  else
64  k = i + (EcalPulseShape::TEMPLATESAMPLES - 1) * j;
65  pulsecov_tag[k] = Form("samplecov_%d_%d", i, j);
66  }
67  }
68 
69  DOMNode** covs_node =
70  new DOMNode*[int(EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2)];
71  for (int k = 0; k < EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2; ++k) {
72  covs_node[k] = getChildNode(cellnode, pulsecov_tag[k]);
73  GetNodeData(covs_node[k], covs[k]);
74  }
75 
76  for (int k = 0; k < EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2; ++k)
77  record[detid].covval[k] = covs[k];
78 
79  cellnode = cellnode->getNextSibling();
80 
81  while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
82  cellnode = cellnode->getNextSibling();
83  chan++;
84  }
85 
86  delete parser;
88  std::cout << " nb of channels found in xml file " << chan << std::endl;
89  return 0;
90 }
91 
93  const EcalCondHeader& header,
96 
97  std::fstream fs(filename.c_str(), ios::out);
98  fs << dumpXML(header, record);
99 
101 
102  return 0;
103 }
104 
107  unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
108 
109  DOMLSSerializer* writer = impl->createLSSerializer();
110  if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
111  writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
112 
113  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
114  DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(PulseSymmCovariances_tag.c_str()).ptr(), doctype);
115 
116  DOMElement* root = doc->getDocumentElement();
117 
118  std::vector<std::string> pulsecov_tag(static_cast<size_t>(std::pow(EcalPulseShape::TEMPLATESAMPLES, 2)));
119  for (int k = 0; k < std::pow(EcalPulseShape::TEMPLATESAMPLES, 2); ++k) {
122  int linK = -1;
123  if (j >= i)
124  linK = j + (EcalPulseShape::TEMPLATESAMPLES - 1) * i;
125  else
126  linK = i + (EcalPulseShape::TEMPLATESAMPLES - 1) * j;
127  pulsecov_tag[linK] = Form("samplecov_%d_%d", i, j);
128  }
129 
130  xuti::writeHeader(root, header);
131  if (record.barrelItems().empty())
132  return std::string();
133  for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
134  uint32_t rawid = EBDetId::unhashIndex(cellid);
135 
136  if (record.find(rawid) == record.end())
137  continue;
138 
139  DOMElement* cellnode = writeCell(root, rawid);
140 
141  for (int k = 0; k < std::pow(EcalPulseShape::TEMPLATESAMPLES, 2); ++k) {
142  WriteNodeWithValue(cellnode, pulsecov_tag[k], record[rawid].covval[k]);
143  }
144  }
145  if (record.endcapItems().empty())
146  return std::string();
147  for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
148  if (!EEDetId::validHashIndex(cellid))
149  continue;
150 
151  uint32_t rawid = EEDetId::unhashIndex(cellid);
152 
153  if (record.find(rawid) == record.end())
154  continue;
155 
156  DOMElement* cellnode = writeCell(root, rawid);
157 
158  for (int k = 0; k < std::pow(EcalPulseShape::TEMPLATESAMPLES, 2); ++k) {
159  WriteNodeWithValue(cellnode, pulsecov_tag[k], record[rawid].covval[k]);
160  }
161  }
162 
163  std::string dump = cms::xerces::toString(writer->writeToString(root));
164  doc->release();
165  doctype->release();
166  writer->release();
167 
168  return dump;
169 }
void xercesTerminate()
Definition: Xerces.cc:23
tuple chan
lumi = TPaveText(lowX+0.38, lowY+0.061, lowX+0.45, lowY+0.161, &quot;NDC&quot;) lumi.SetBorderSize( 0 ) lumi...
const Items & barrelItems() const
static const int TEMPLATESAMPLES
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)
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalPulseSymmCovariances &record)
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
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
const std::string PulseSymmCovariances_tag("EcalPulseSymmCovariances")
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:110
tuple filename
Definition: lut2db_cfg.py:20
const_iterator find(uint32_t rawId) const
static int readXML(const std::string &filename, EcalCondHeader &header, EcalPulseSymmCovariances &record)
tuple cout
Definition: gather_cfg.py:144
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.
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
tuple dump
OutputFilePath = cms.string(&#39;/tmp/zhokin/&#39;), OutputFileExt = cms.string(&#39;&#39;),.
static std::string dumpXML(const EcalCondHeader &header, const EcalPulseSymmCovariances &record)