CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalPulseCovariancesXMLTranslator.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>
9 #include <xercesc/util/XMLString.hpp>
10 #include <xercesc/sax/SAXException.hpp>
11 #include <xercesc/framework/LocalFileFormatTarget.hpp>
12 #include <TString.h>
13 
18 
19 
20 using namespace XERCES_CPP_NAMESPACE;
21 using namespace xuti;
22 using namespace std;
23 
25  EcalCondHeader& header,
27 
29 
30  XercesDOMParser* parser = new XercesDOMParser;
31  parser->setValidationScheme( XercesDOMParser::Val_Never );
32  parser->setDoNamespaces( false );
33  parser->setDoSchema( false );
34 
35  parser->parse(filename.c_str());
36 
37  DOMDocument* xmlDoc = parser->getDocument();
38  if (!xmlDoc) {
39  std::cout << "EcalPulseCovariancesXMLTranslator::Error parsing document" << std::endl;
40  return -1;
41  }
42 
43  DOMElement* elementRoot = xmlDoc->getDocumentElement();
44 
45  xuti::readHeader(elementRoot,header);
46 
47  DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
48 
49  int chan = 0;
50  while(cellnode) {
51  // std::cout << " Channel " << chan << std::endl;
53 
54  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
55  // std::cout << " readCell Id Channel " << chan << " tag " << mean12_tag << std::endl;
56 
57  std::vector<std::string> pulsecov_tag(static_cast<size_t>(std::pow(EcalPulseShape::TEMPLATESAMPLES,2)));
58  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k) {
61  pulsecov_tag[k] = Form("samplecov_%d_%d",i,j);
62  }
63 
64  DOMNode** covs_node = new DOMNode*[int(std::pow(EcalPulseShape::TEMPLATESAMPLES,2))];
65  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k)
66  covs_node[k] = getChildNode(cellnode,pulsecov_tag[k]);
67 
68  for(int i=0; i<EcalPulseShape::TEMPLATESAMPLES; ++i) for(int j=0; j<EcalPulseShape::TEMPLATESAMPLES; ++j) record[detid].covval[i][j] = covs[i][j];
69 
70  cellnode = cellnode->getNextSibling();
71 
72  while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
73  cellnode = cellnode->getNextSibling();
74  chan++;
75  }
76 
77  delete parser;
79  std::cout << " nb of channels found in xml file " << chan << std::endl;
80  return 0;
81  }
82 
84  const EcalCondHeader& header,
86  std::fstream fs(filename.c_str(),ios::out);
87  fs<< dumpXML(header,record);
88  return 0;
89 }
90 
91 
93 
95  DOMImplementation* impl =
96  DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
97 
98  DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
99  writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
100 
101  DOMDocumentType* doctype = impl->createDocumentType(fromNative("XML").c_str(), 0, 0 );
102  DOMDocument * doc =
103  impl->createDocument( 0, fromNative(PulseCovariances_tag).c_str(), doctype );
104 
105  doc->setEncoding(fromNative("UTF-8").c_str() );
106  doc->setStandalone(true);
107  doc->setVersion(fromNative("1.0").c_str() );
108 
109  DOMElement* root = doc->getDocumentElement();
110 
111  std::vector<std::string> pulsecov_tag(static_cast<size_t>(std::pow(EcalPulseShape::TEMPLATESAMPLES,2)));
112  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k) {
115  pulsecov_tag[k] = Form("samplecov_%d_%d",i,j);
116  }
117 
118  xuti::writeHeader(root,header);
119  if (!record.barrelItems().size()) return std::string();
120  for(int cellid = EBDetId::MIN_HASH;
122  ++cellid) {
123  uint32_t rawid = EBDetId::unhashIndex(cellid);
124 
125  if (record.find(rawid) == record.end()) continue;
126 
127  DOMElement* cellnode=writeCell(root,rawid);
128 
130  WriteNodeWithValue(cellnode,pulsecov_tag[i*EcalPulseShape::TEMPLATESAMPLES+j],record[rawid].covval[i][j]);
131 
132  }
133 
134  if (!record.endcapItems().size()) return std::string();
135  for(int cellid = 0;
137  ++cellid) {
138  if(!EEDetId::validHashIndex(cellid)) continue;
139 
140  uint32_t rawid = EEDetId::unhashIndex(cellid);
141 
142  if (record.find(rawid) == record.end()) continue;
143 
144  DOMElement* cellnode=writeCell(root,rawid);
145 
147  WriteNodeWithValue(cellnode,pulsecov_tag[i*EcalPulseShape::TEMPLATESAMPLES+j],record[rawid].covval[i][j]);
148 
149  }
150 
151  std::string dump= toNative(writer->writeToString(*root));
152  doc->release();
153  return dump;
154 }
int i
Definition: DBlmapReader.cc:9
static std::string dumpXML(const EcalCondHeader &header, const EcalPulseCovariances &record)
JetCorrectorParameters::Record record
Definition: classes.h:7
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalPulseCovariances &record)
void xercesTerminate()
Definition: Xerces.cc:22
const Items & barrelItems() const
static const int TEMPLATESAMPLES
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 const int MIN_HASH
Definition: EBDetId.h:156
const std::string Cell_tag("cell")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
int j
Definition: DBlmapReader.cc:9
tuple doc
Definition: asciidump.py:381
tuple out
Definition: dbtoconf.py:99
Definition: DetId.h:18
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 std::string PulseCovariances_tag("EcalPulseCovariances")
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
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
static int readXML(const std::string &filename, EcalCondHeader &header, EcalPulseCovariances &record)
const Items & endcapItems() const
const DetId readCellId(xercesc::DOMElement *node)
Assuming.
std::string toNative(const XMLCh *str)
Definition: XercesString.h:42
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
string root
initialization
Definition: dbtoconf.py:70