CMS 3D CMS Logo

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 
21 using namespace XERCES_CPP_NAMESPACE;
22 using namespace xuti;
23 using namespace std;
24 
28 
30 
31  XercesDOMParser* parser = new XercesDOMParser;
32  parser->setValidationScheme( XercesDOMParser::Val_Never );
33  parser->setDoNamespaces( false );
34  parser->setDoSchema( false );
35 
36  parser->parse(filename.c_str());
37 
38  DOMDocument* xmlDoc = parser->getDocument();
39  if (!xmlDoc) {
40  std::cout << "EcalPulseSymmCovariancesXMLTranslator::Error parsing document" << std::endl;
41  return -1;
42  }
43 
44  DOMElement* elementRoot = xmlDoc->getDocumentElement();
45 
46  xuti::readHeader(elementRoot,header);
47 
48  DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
49 
50  int chan = 0;
51  while(cellnode) {
52  // std::cout << " Channel " << chan << std::endl;
54 
55  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
56  // std::cout << " readCell Id Channel " << chan << " tag " << mean12_tag << std::endl;
57 
58  std::vector<std::string> pulsecov_tag(static_cast<size_t>(EcalPulseShape::TEMPLATESAMPLES*(EcalPulseShape::TEMPLATESAMPLES+1)/2));
59  for(int i=0; i<EcalPulseShape::TEMPLATESAMPLES; ++i) {
60  for(int j=0; j<EcalPulseShape::TEMPLATESAMPLES; ++j) {
61  int k=-1;
62  if(j >= i) k = j + (EcalPulseShape::TEMPLATESAMPLES-1)*i;
63  else k = i + (EcalPulseShape::TEMPLATESAMPLES-1)*j;
64  pulsecov_tag[k] = Form("samplecov_%d_%d",i,j);
65  }
66  }
67 
68  DOMNode** covs_node = new DOMNode*[int(EcalPulseShape::TEMPLATESAMPLES*(EcalPulseShape::TEMPLATESAMPLES+1)/2)];
69  for(int k=0; k<EcalPulseShape::TEMPLATESAMPLES*(EcalPulseShape::TEMPLATESAMPLES+1)/2; ++k) {
70  covs_node[k] = getChildNode(cellnode,pulsecov_tag[k]);
71  GetNodeData(covs_node[k], covs[k]);
72  }
73 
74  for(int k=0; k<EcalPulseShape::TEMPLATESAMPLES*(EcalPulseShape::TEMPLATESAMPLES+1)/2; ++k) record[detid].covval[k] = covs[k];
75 
76  cellnode = cellnode->getNextSibling();
77 
78  while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
79  cellnode = cellnode->getNextSibling();
80  chan++;
81  }
82 
83  delete parser;
85  std::cout << " nb of channels found in xml file " << chan << std::endl;
86  return 0;
87  }
88 
90  const EcalCondHeader& header,
93 
94  std::fstream fs(filename.c_str(),ios::out);
95  fs<< dumpXML(header,record);
96 
98 
99  return 0;
100 }
101 
102 
104 
105  unique_ptr<DOMImplementation> impl( DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
106 
107  DOMLSSerializer* writer = impl->createLSSerializer();
108  if( writer->getDomConfig()->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true ))
109  writer->getDomConfig()->setParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true );
110 
111  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), 0, 0 );
112  DOMDocument * doc =
113  impl->createDocument( 0, cms::xerces::uStr(PulseSymmCovariances_tag.c_str()).ptr(), doctype );
114 
115  DOMElement* root = doc->getDocumentElement();
116 
117  std::vector<std::string> pulsecov_tag(static_cast<size_t>(std::pow(EcalPulseShape::TEMPLATESAMPLES,2)));
118  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k) {
121  int linK=-1;
122  if(j >= i) linK = j + (EcalPulseShape::TEMPLATESAMPLES-1)*i;
123  else linK = i + (EcalPulseShape::TEMPLATESAMPLES-1)*j;
124  pulsecov_tag[linK] = Form("samplecov_%d_%d",i,j);
125  }
126 
127  xuti::writeHeader(root,header);
128  if (!record.barrelItems().size()) return std::string();
129  for(int cellid = EBDetId::MIN_HASH;
131  ++cellid) {
132  uint32_t rawid = EBDetId::unhashIndex(cellid);
133 
134  if (record.find(rawid) == record.end()) continue;
135 
136  DOMElement* cellnode=writeCell(root,rawid);
137 
138  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k) {
139  WriteNodeWithValue(cellnode,pulsecov_tag[k],record[rawid].covval[k]);
140  }
141 
142  }
143  if (!record.endcapItems().size()) return std::string();
144  for(int cellid = 0;
146  ++cellid) {
147  if(!EEDetId::validHashIndex(cellid)) continue;
148 
149  uint32_t rawid = EEDetId::unhashIndex(cellid);
150 
151  if (record.find(rawid) == record.end()) continue;
152 
153  DOMElement* cellnode=writeCell(root,rawid);
154 
155  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k) {
156  WriteNodeWithValue(cellnode,pulsecov_tag[k],record[rawid].covval[k]);
157  }
158  }
159 
160  std::string dump = cms::xerces::toString(writer->writeToString( root ));
161  doc->release();
162  doctype->release();
163  writer->release();
164 
165  return dump;
166 }
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
static const int TEMPLATESAMPLES
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)
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:156
const std::string Cell_tag("cell")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
ZStr< XMLCh > uStr(char const *str)
int k[5][pyjets_maxn]
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
const std::string PulseSymmCovariances_tag("EcalPulseSymmCovariances")
chan
lumi = TPaveText(lowX+0.38, lowY+0.061, lowX+0.45, lowY+0.161, "NDC") lumi.SetBorderSize( 0 ) lumi...
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
static int readXML(const std::string &filename, EcalCondHeader &header, EcalPulseSymmCovariances &record)
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:40
static std::string dumpXML(const EcalCondHeader &header, const EcalPulseSymmCovariances &record)