CMS 3D CMS Logo

EcalPedestalsXMLTranslator.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 #include <xercesc/dom/DOMNode.hpp>
5 #include <xercesc/dom/DOM.hpp>
6 #include <xercesc/parsers/XercesDOMParser.hpp>
9 #include <xercesc/util/XMLString.hpp>
10 #include <xercesc/sax/SAXException.hpp>
11 #include <xercesc/framework/LocalFileFormatTarget.hpp>
12 
13 
18 
19 
20 using namespace XERCES_CPP_NAMESPACE;
21 using namespace xuti;
22 using namespace std;
23 
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 << "EcalPedestalsXMLTranslator::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;
52  float mean12 = 0;
53  float mean6 = 0;
54  float mean1 = 0;
55  float rms12 = 0;
56  float rms6 = 0;
57  float rms1 = 0;
58  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
59  // std::cout << " readCell Id Channel " << chan << " tag " << mean12_tag << std::endl;
60 
61  DOMNode* mean12_node = getChildNode(cellnode,mean12_tag);
62  GetNodeData(mean12_node,mean12);
63  // std::cout << " tag " << mean12_tag << " mean12 " << mean12 << std::endl;
64 
65  DOMNode* mean6_node = getChildNode(cellnode,mean6_tag);
66  GetNodeData(mean6_node,mean6);
67  // std::cout << " tag " << mean6_tag << " mean6 " << mean6 << std::endl;
68 
69  DOMNode* mean1_node = getChildNode(cellnode,mean1_tag);
70  GetNodeData(mean1_node,mean1);
71  // std::cout << " tag " << mean1_tag << " mean1 " << mean1 << std::endl;
72 
73  DOMNode* rms12_node = getChildNode(cellnode,rms12_tag);
74  GetNodeData(rms12_node,rms12);
75  // std::cout << " tag 12 " << rms12_tag << " rms 12 " << rms12 << std::endl;
76 
77  DOMNode* rms6_node = getChildNode(cellnode,rms6_tag);
78  GetNodeData(rms6_node,rms6);
79  // std::cout << " tag 6 " << rms6_tag << " rms 6 " << rms6 << std::endl;
80 
81  DOMNode* rms1_node = getChildNode(cellnode,rms1_tag);
82  // std::cout << " tag 1 " << rms1_tag << std::endl;
83 
84  GetNodeData(rms1_node,rms1);
85  // std::cout << " Channel " << chan << " mean12 " << mean12 << " rms 1 " << rms1 << std::endl;
86  record[detid].mean_x12 = mean12;
87  record[detid].mean_x6 = mean6;
88  record[detid].mean_x1 = mean1;
89  record[detid].rms_x12 = rms12;
90  record[detid].rms_x6 = rms6;
91  record[detid].rms_x1 = rms1;
92 
93  cellnode = cellnode->getNextSibling();
94 
95  while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
96  cellnode = cellnode->getNextSibling();
97  chan++;
98  }
99 
100  delete parser;
102  std::cout << " nb of channels found in xml file " << chan << std::endl;
103  return 0;
104  }
105 
107  const EcalCondHeader& header,
108  const EcalPedestals& record){
110 
111  std::fstream fs(filename.c_str(),ios::out);
112  fs<< dumpXML(header,record);
113 
115 
116  return 0;
117 }
118 
119 
121 
122  unique_ptr<DOMImplementation> impl( DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
123 
124  DOMLSSerializer* writer = impl->createLSSerializer();
125  if( writer->getDomConfig()->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true ))
126  writer->getDomConfig()->setParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true );
127 
128  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr );
129  DOMDocument * doc =
130  impl->createDocument( nullptr, cms::xerces::uStr(Pedestals_tag.c_str()).ptr(), doctype );
131 
132  DOMElement* root = doc->getDocumentElement();
133 
134  xuti::writeHeader(root,header);
135  if (record.barrelItems().empty()) return std::string();
136  for(int cellid = EBDetId::MIN_HASH;
138  ++cellid) {
139  uint32_t rawid = EBDetId::unhashIndex(cellid);
140 
141  if (record.find(rawid) == record.end()) continue;
142  if(!record[rawid].mean_x12 && !record[rawid].rms_x12) continue;
143 
144  DOMElement* cellnode=writeCell(root,rawid);
145 
146  WriteNodeWithValue(cellnode,mean12_tag,record[rawid].mean_x12);
147  WriteNodeWithValue(cellnode,mean6_tag,record[rawid].mean_x6);
148  WriteNodeWithValue(cellnode,mean1_tag,record[rawid].mean_x1);
149  WriteNodeWithValue(cellnode,rms12_tag,record[rawid].rms_x12);
150  WriteNodeWithValue(cellnode,rms6_tag,record[rawid].rms_x6);
151  WriteNodeWithValue(cellnode,rms1_tag,record[rawid].rms_x1);
152  }
153 
154  if (record.endcapItems().empty()) return std::string();
155  for(int cellid = 0;
157  ++cellid) {
158  if(!EEDetId::validHashIndex(cellid)) continue;
159 
160  uint32_t rawid = EEDetId::unhashIndex(cellid);
161 
162  if (record.find(rawid) == record.end()) continue;
163  if(!record[rawid].mean_x12 && !record[rawid].rms_x12) continue;
164 
165  DOMElement* cellnode=writeCell(root,rawid);
166 
167  WriteNodeWithValue(cellnode,mean12_tag,record[rawid].mean_x12);
168  WriteNodeWithValue(cellnode,mean6_tag,record[rawid].mean_x6);
169  WriteNodeWithValue(cellnode,mean1_tag,record[rawid].mean_x1);
170  WriteNodeWithValue(cellnode,rms12_tag,record[rawid].rms_x12);
171  WriteNodeWithValue(cellnode,rms6_tag,record[rawid].rms_x6);
172  WriteNodeWithValue(cellnode,rms1_tag,record[rawid].rms_x1);
173  }
174 
175  std::string dump = cms::xerces::toString(writer->writeToString( root ));
176  doc->release();
177  doctype->release();
178  writer->release();
179 
180  return dump;
181 }
const std::string mean12_tag("mean_x12")
static int readXML(const std::string &filename, EcalCondHeader &header, EcalPedestals &record)
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
void xercesInitialize()
Definition: Xerces.cc:18
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:65
const std::string mean1_tag("mean_x1")
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 std::string dumpXML(const EcalCondHeader &header, const EcalPedestals &record)
std::string toString(XMLCh const *toTranscode)
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")
const std::string Pedestals_tag("EcalPedestals")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
ZStr< XMLCh > uStr(char const *str)
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 mean6_tag("mean_x6")
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:110
const_iterator find(uint32_t rawId) const
const std::string rms12_tag("rms_x12")
const std::string rms6_tag("rms_x6")
const_iterator end() const
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const Items & endcapItems() const
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalPedestals &record)
const std::string rms1_tag("rms_x1")
const DetId readCellId(xercesc::DOMElement *node)
Assuming.