CMS 3D CMS Logo

DOMHelperFunctions.cc
Go to the documentation of this file.
1 
12 #include <xercesc/dom/DOM.hpp>
13 #include <xercesc/dom/DOMNode.hpp>
14 #include <xercesc/parsers/XercesDOMParser.hpp>
17 #include <xercesc/framework/LocalFileFormatTarget.hpp>
18 #include <sstream>
19 
20 using namespace std;
21 using namespace xuti;
22 using namespace xercesc;
23 
24 const DetId xuti::readCellId(xercesc::DOMElement* node) {
25  int ieta = 0;
26  int iphi = 0;
27  int ix = 0;
28  int iy = 0;
29  int ixSC = 0;
30  int iySC = 0;
31  int zside = 0;
32 
33  stringstream ieta_str;
34  stringstream iphi_str;
35  stringstream ix_str;
36  stringstream iy_str;
37  stringstream ixSC_str;
38  stringstream iySC_str;
39  stringstream zside_str;
40 
41  ieta_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr()));
42  iphi_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr()));
43  ix_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(ix_tag.c_str()).ptr()));
44  iy_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iy_tag.c_str()).ptr()));
45  ixSC_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(ixSC_tag.c_str()).ptr()));
46  iySC_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iySC_tag.c_str()).ptr()));
47  zside_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr()));
48 
49  ieta_str >> ieta;
50  iphi_str >> iphi;
51  ix_str >> ix;
52  iy_str >> iy;
53  ixSC_str >> ixSC;
54  iySC_str >> iySC;
55  zside_str >> zside;
56 
57  if (ieta && iphi) {
58  return EBDetId(ieta, iphi);
59  }
60  if (ix && iy && zside) {
61  return EEDetId(ix, iy, zside);
62  }
63  if (ixSC && iySC && zside) {
64  return EcalScDetId(ixSC, iySC, zside);
65  }
66 
67  cerr << "XMLCell: error reading cell, missing field ?" << std::endl;
68  return 0;
69 }
70 
71 DOMElement* xuti::writeCell(xercesc::DOMNode* node, const DetId& detid) {
72  DOMElement* cell_node = node->getOwnerDocument()->createElement(cms::xerces::uStr(Cell_tag.c_str()).ptr());
73 
74  node->appendChild(cell_node);
75 
76  if (detid.subdetId() == EcalBarrel) {
77  stringstream value_s;
78  value_s << EBDetId(detid).ieta();
79 
80  cell_node->setAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
81  value_s.str("");
82  value_s << EBDetId(detid).iphi();
83 
84  cell_node->setAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
85 
86  } else if (detid.subdetId() == EcalEndcap) {
87  // is it a EcalScDetId ?
88  unsigned int ScIdCheck = detid.rawId() & 0x00008000;
89  if (ScIdCheck == 0) {
90  stringstream value_s;
91  value_s << EEDetId(detid).ix();
92 
93  cell_node->setAttribute(cms::xerces::uStr(ix_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
94  value_s.str("");
95  value_s << EEDetId(detid).iy();
96 
97  cell_node->setAttribute(cms::xerces::uStr(iy_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
98  value_s.str("");
99  value_s << EEDetId(detid).zside();
100 
101  cell_node->setAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr(),
102  cms::xerces::uStr(value_s.str().c_str()).ptr());
103  } else {
104  stringstream value_s;
105  value_s << EcalScDetId(detid).ix();
106 
107  cell_node->setAttribute(cms::xerces::uStr(ixSC_tag.c_str()).ptr(),
108  cms::xerces::uStr(value_s.str().c_str()).ptr());
109  value_s.str("");
110  value_s << EcalScDetId(detid).iy();
111 
112  cell_node->setAttribute(cms::xerces::uStr(iySC_tag.c_str()).ptr(),
113  cms::xerces::uStr(value_s.str().c_str()).ptr());
114  value_s.str("");
115  value_s << EcalScDetId(detid).zside();
116 
117  cell_node->setAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr(),
118  cms::xerces::uStr(value_s.str().c_str()).ptr());
119  }
120 
121  } else if (detid.subdetId() == EcalTriggerTower) {
122  stringstream value_s;
123  value_s << EcalTrigTowerDetId(detid).ieta();
124 
125  cell_node->setAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
126  value_s.str("");
127  value_s << EcalTrigTowerDetId(detid).iphi();
128 
129  cell_node->setAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
130  }
131  return cell_node;
132 }
133 
134 // return 0 if not found
135 DOMNode* xuti::getChildNode(DOMNode* node, const std::string& nodename) {
136  if (!node)
137  return nullptr;
138 
139  for (DOMNode* childNode = node->getFirstChild(); childNode; childNode = childNode->getNextSibling()) {
140  if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
141  const string foundName = cms::xerces::toString(childNode->getNodeName());
142 
143  if (foundName == nodename)
144  return childNode;
145  } // if element
146  } // for child
147 
148  return nullptr;
149 }
150 
151 void xuti::writeHeader(xercesc::DOMNode* parentNode, const EcalCondHeader& header) {
152  DOMElement* headernode = parentNode->getOwnerDocument()->createElement(cms::xerces::uStr(Header_tag.c_str()).ptr());
153  parentNode->appendChild(headernode);
154 
155  // write the actual header
156  WriteNodeWithValue(headernode, Header_methodtag, header.method_);
157  WriteNodeWithValue(headernode, Header_versiontag, header.version_);
158  WriteNodeWithValue(headernode, Header_datasourcetag, header.datasource_);
159  WriteNodeWithValue(headernode, Header_sincetag, header.since_);
160  WriteNodeWithValue(headernode, Header_tagtag, header.tag_);
161  WriteNodeWithValue(headernode, Header_datetag, header.date_);
162 }
163 
164 void xuti::readHeader(xercesc::DOMNode* parentNode, EcalCondHeader& header) {
165  DOMNode* hnode = getChildNode(parentNode, Header_tag);
166 
167  DOMNode* node = getChildNode(hnode, Header_methodtag);
168  GetNodeStringData(node, header.method_);
169 
170  node = getChildNode(hnode, Header_versiontag);
171  GetNodeStringData(node, header.version_);
172 
173  node = getChildNode(hnode, Header_datasourcetag);
174  GetNodeStringData(node, header.datasource_);
175 
176  node = getChildNode(hnode, Header_sincetag);
177  GetNodeData(node, header.since_);
178 
179  node = getChildNode(hnode, Header_tagtag);
180  GetNodeStringData(node, header.tag_);
181 
182  node = getChildNode(hnode, Header_datetag);
183  GetNodeStringData(node, header.date_);
184 }
185 
188 
189  XercesDOMParser* parser = new XercesDOMParser;
190  parser->setValidationScheme(XercesDOMParser::Val_Never);
191  parser->setDoNamespaces(false);
192  parser->setDoSchema(false);
193 
194  parser->parse(filename.c_str());
195 
196  DOMDocument* xmlDoc = parser->getDocument();
197 
198  if (!xmlDoc) {
199  std::cout << "Error parsing document" << std::endl;
200  return -1;
201  }
202 
203  DOMElement* elementRoot = xmlDoc->getDocumentElement();
204 
205  xuti::readHeader(elementRoot, header);
206 
207  delete parser;
209 
210  return 0;
211 }
212 
213 void xuti::GetNodeStringData(xercesc::DOMNode* node, std::string& value) {
214  value = cms::xerces::toString(node->getTextContent());
215 }
xuti::getChildNode
xercesc::DOMNode * getChildNode(xercesc::DOMNode *node, const std::string &nodename)
get the child of
XercesStrUtils.h
xuti::Header_versiontag
const std::string Header_versiontag("version")
EBDetId::ieta
int ieta() const
get the crystal ieta
Definition: EBDetId.h:49
EcalTrigTowerDetId::iphi
int iphi() const
get the tower iphi
Definition: EcalTrigTowerDetId.h:52
xuti::iySC_tag
const std::string iySC_tag("iySC")
xuti::writeHeader
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
Definition: DOMHelperFunctions.cc:151
xuti::writeCell
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
Definition: DOMHelperFunctions.cc:71
xuti::GetNodeStringData
void GetNodeStringData(xercesc::DOMNode *node, std::string &value)
get the node data as string. Needs to be used to avoid splitting
Definition: DOMHelperFunctions.cc:213
xuti::iPhi_tag
const std::string iPhi_tag("iPhi")
ecaldqm::zside
int zside(DetId const &)
Definition: EcalDQMCommonUtils.cc:189
EBDetId
Definition: EBDetId.h:17
EcalScDetId::zside
int zside() const
Definition: EcalScDetId.h:64
gather_cfg.cout
cout
Definition: gather_cfg.py:144
EBDetId.h
EEDetId.h
xuti::Header_tag
const std::string Header_tag("EcalCondHeader")
writedatasetfile.parser
parser
Definition: writedatasetfile.py:7
EcalTrigTowerDetId
Definition: EcalTrigTowerDetId.h:14
EEDetId::ix
int ix() const
Definition: EEDetId.h:77
EcalBarrel
Definition: EcalSubdetector.h:10
LEDCalibrationChannels.iphi
iphi
Definition: LEDCalibrationChannels.py:64
DetId
Definition: DetId.h:17
cms::xerces::toString
std::string toString(XMLCh const *toTranscode)
Definition: XercesStrUtils.h:35
xuti::Header_sincetag
const std::string Header_sincetag("since")
xuti::WriteNodeWithValue
void WriteNodeWithValue(xercesc::DOMNode *parentNode, const std::string &tag, const T &value)
write a node with
Definition: DOMHelperFunctions.h:46
xuti::zside_tag
const std::string zside_tag("zside")
xuti::Header_datasourcetag
const std::string Header_datasourcetag("datasource")
EcalScDetId
Definition: EcalScDetId.h:24
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
EEDetId::zside
int zside() const
Definition: EEDetId.h:71
DetId::subdetId
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector's numbering enum)
Definition: DetId.h:48
EEDetId
Definition: EEDetId.h:14
EcalTrigTowerDetId.h
EcalEndcap
Definition: EcalSubdetector.h:10
LEDCalibrationChannels.ieta
ieta
Definition: LEDCalibrationChannels.py:63
xuti::iEta_tag
const std::string iEta_tag("iEta")
DOMHelperFunctions.h
EcalCondHeader
Definition: EcalCondHeader.h:16
Xerces.h
cms::concurrency::xercesTerminate
void xercesTerminate()
Definition: Xerces.cc:23
EcalScDetId::iy
int iy() const
Definition: EcalScDetId.h:76
xuti::ixSC_tag
const std::string ixSC_tag("ixSC")
value
Definition: value.py:1
xuti
Definition: DOMHelperFunctions.h:21
xuti::ix_tag
const std::string ix_tag("ix")
cms::xerces::uStr
ZStr< XMLCh > uStr(char const *str)
Definition: XercesStrUtils.h:31
EcalTrigTowerDetId::ieta
int ieta() const
get the tower ieta
Definition: EcalTrigTowerDetId.h:44
EEDetId::iy
int iy() const
Definition: EEDetId.h:83
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
xuti::Header_methodtag
const std::string Header_methodtag("method")
xuti::Header_tagtag
const std::string Header_tagtag("tag")
std
Definition: JetResolutionObject.h:76
DetId::rawId
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
EcalTriggerTower
Definition: EcalSubdetector.h:10
RecoTauValidation_cfi.header
header
Definition: RecoTauValidation_cfi.py:291
EBDetId::iphi
int iphi() const
get the crystal iphi
Definition: EBDetId.h:51
xuti::readCellId
const DetId readCellId(xercesc::DOMElement *node)
Assuming.
Definition: DOMHelperFunctions.cc:24
XMLTags.h
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
xuti::iy_tag
const std::string iy_tag("iy")
cms::concurrency::xercesInitialize
void xercesInitialize()
Definition: Xerces.cc:18
xuti::readHeader
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
Definition: DOMHelperFunctions.cc:164
xuti::Cell_tag
const std::string Cell_tag("cell")
EcalScDetId::ix
int ix() const
Definition: EcalScDetId.h:70
xuti::Header_datetag
const std::string Header_datetag("date")
xuti::GetNodeData
void GetNodeData(xercesc::DOMNode *node, T &value)
get the node data
Definition: DOMHelperFunctions.h:38