CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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>
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 << "EcalPulseSymmCovariancesXMLTranslator::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>(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) k = j + (EcalPulseShape::TEMPLATESAMPLES-1)*i;
62  else k = i + (EcalPulseShape::TEMPLATESAMPLES-1)*j;
63  pulsecov_tag[k] = Form("samplecov_%d_%d",i,j);
64  }
65  }
66 
67  DOMNode** covs_node = new DOMNode*[int(EcalPulseShape::TEMPLATESAMPLES*(EcalPulseShape::TEMPLATESAMPLES+1)/2)];
68  for(int k=0; k<EcalPulseShape::TEMPLATESAMPLES*(EcalPulseShape::TEMPLATESAMPLES+1)/2; ++k) {
69  covs_node[k] = getChildNode(cellnode,pulsecov_tag[k]);
70  GetNodeData(covs_node[k], covs[k]);
71  }
72 
73  for(int k=0; k<EcalPulseShape::TEMPLATESAMPLES*(EcalPulseShape::TEMPLATESAMPLES+1)/2; ++k) record[detid].covval[k] = covs[k];
74 
75  cellnode = cellnode->getNextSibling();
76 
77  while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
78  cellnode = cellnode->getNextSibling();
79  chan++;
80  }
81 
82  delete parser;
84  std::cout << " nb of channels found in xml file " << chan << std::endl;
85  return 0;
86  }
87 
89  const EcalCondHeader& header,
91  std::fstream fs(filename.c_str(),ios::out);
92  fs<< dumpXML(header,record);
93  return 0;
94 }
95 
96 
98 
100  DOMImplementation* impl =
101  DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
102 
103  DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
104  writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
105 
106  DOMDocumentType* doctype = impl->createDocumentType(fromNative("XML").c_str(), 0, 0 );
107  DOMDocument * doc =
108  impl->createDocument( 0, fromNative(PulseSymmCovariances_tag).c_str(), doctype );
109 
110  doc->setEncoding(fromNative("UTF-8").c_str() );
111  doc->setStandalone(true);
112  doc->setVersion(fromNative("1.0").c_str() );
113 
114  DOMElement* root = doc->getDocumentElement();
115 
116  std::vector<std::string> pulsecov_tag(static_cast<size_t>(std::pow(EcalPulseShape::TEMPLATESAMPLES,2)));
117  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k) {
120  int linK=-1;
121  if(j >= i) linK = j + (EcalPulseShape::TEMPLATESAMPLES-1)*i;
122  else linK = i + (EcalPulseShape::TEMPLATESAMPLES-1)*j;
123  pulsecov_tag[linK] = Form("samplecov_%d_%d",i,j);
124  }
125 
126  xuti::writeHeader(root,header);
127  if (!record.barrelItems().size()) return std::string();
128  for(int cellid = EBDetId::MIN_HASH;
130  ++cellid) {
131  uint32_t rawid = EBDetId::unhashIndex(cellid);
132 
133  if (record.find(rawid) == record.end()) continue;
134 
135  DOMElement* cellnode=writeCell(root,rawid);
136 
137  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k) {
138  WriteNodeWithValue(cellnode,pulsecov_tag[k],record[rawid].covval[k]);
139  }
140 
141  }
142  if (!record.endcapItems().size()) return std::string();
143  for(int cellid = 0;
145  ++cellid) {
146  if(!EEDetId::validHashIndex(cellid)) continue;
147 
148  uint32_t rawid = EEDetId::unhashIndex(cellid);
149 
150  if (record.find(rawid) == record.end()) continue;
151 
152  DOMElement* cellnode=writeCell(root,rawid);
153 
154  for(int k=0; k<std::pow(EcalPulseShape::TEMPLATESAMPLES,2); ++k) {
155  WriteNodeWithValue(cellnode,pulsecov_tag[k],record[rawid].covval[k]);
156  }
157  }
158 
159  std::string dump= toNative(writer->writeToString(*root));
160  doc->release();
161  return dump;
162 }
int i
Definition: DBlmapReader.cc:9
JetCorrectorParameters::Record record
Definition: classes.h:7
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
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
int j
Definition: DBlmapReader.cc:9
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
const std::string PulseSymmCovariances_tag("EcalPulseSymmCovariances")
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
static int readXML(const std::string &filename, EcalCondHeader &header, EcalPulseSymmCovariances &record)
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.
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
static std::string dumpXML(const EcalCondHeader &header, const EcalPulseSymmCovariances &record)
string root
initialization
Definition: dbtoconf.py:70