CMS 3D CMS Logo

EcalFloatCondObjectContainerXMLTranslator.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 
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 
16 
17 using namespace XERCES_CPP_NAMESPACE;
18 using namespace xuti;
19 using namespace std;
20 
21 const int kEBChannels = 61200, kEEChannels = 14648;
22 
26  std::cout << "EcalFloatCondObjectContainerXMLTranslatorr::readXML filename " << filename << std::endl;
27  /* old method for DBv1
28  cms::concurrency::xercesInitialize();
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 
39 
40  if (!xmlDoc) {
41  std::cout << "EcalFloatCondObjectContainerXMLTranslator::Error parsing document" << std::endl;
42  return -1;
43  }
44 
45  DOMElement* elementRoot = xmlDoc->getDocumentElement();
46 
47  xuti::readHeader(elementRoot, header);
48 
49  // get the first cell node
50  DOMNode * cellnode=getChildNode(elementRoot,Cell_tag);
51 
52  // loop on cell nodes
53  while (cellnode){
54 
55  float val=0;
56 
57  // read id
58  DetId detid= readCellId(dynamic_cast<DOMElement*>(cellnode));
59 
60  // read value
61  DOMNode * c_node = getChildNode(cellnode,Value_tag);
62  GetNodeData(c_node,val);
63 
64  // fill record
65  record[detid]=val;
66 
67  // get next cell
68  cellnode= cellnode->getNextSibling();
69 
70  while (cellnode&& cellnode->getNodeType( ) != DOMNode::ELEMENT_NODE)
71  cellnode= cellnode->getNextSibling();
72 
73 
74  }
75 
76 
77  delete parser;
78  cms::concurrency::xercesTerminate();
79  */
80  // new method for DBv2
81  std::string dummyLine, bid;
82  std::ifstream fxml;
83  fxml.open(filename);
84  if (!fxml.is_open()) {
85  std::cout << "ERROR : cannot open file " << filename << std::endl;
86  exit(1);
87  }
88  // header
89  for (int i = 0; i < 6; i++) {
90  getline(fxml, dummyLine); // skip first lines
91  // std::cout << dummyLine << std::endl;
92  }
93  fxml >> bid;
94  // std::cout << bid << std::endl;
95  std::string stt = bid.substr(7, 5);
96  std::istringstream iEB(stt);
97  int nEB;
98  iEB >> nEB;
99  if (nEB != kEBChannels) {
100  std::cout << " strange number of EB channels " << nEB << std::endl;
101  exit(-1);
102  }
103  fxml >> bid; // <item_version>0</item_version>
104  for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
105  EBDetId myEBDetId = EBDetId::unhashIndex(iChannel);
106  fxml >> bid;
107  std::size_t found = bid.find("</");
108  stt = bid.substr(6, found - 6);
109  float val = std::stof(stt);
110  record[myEBDetId] = val;
111  }
112  for (int i = 0; i < 5; i++) {
113  getline(fxml, dummyLine); // skip first lines
114  // std::cout << dummyLine << std::endl;
115  }
116  fxml >> bid;
117  // cout << bid << endl;
118  stt = bid.substr(7, 5);
119  std::istringstream iEE(stt);
120  int nEE;
121  iEE >> nEE;
122  if (nEE != kEEChannels) {
123  std::cout << " strange number of EE channels " << nEE << std::endl;
124  exit(-1);
125  }
126  fxml >> bid; // <item_version>0</item_version>
127  // now endcaps
128  for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
129  EEDetId myEEDetId = EEDetId::unhashIndex(iChannel);
130  fxml >> bid;
131  std::size_t found = bid.find("</");
132  stt = bid.substr(6, found - 6);
133  float val = std::stof(stt);
134  record[myEEDetId] = val;
135  }
136 
137  return 0;
138 }
139 
143  readXML(filename, header, record);
144 
145  return record.barrelItems();
146 }
147 
151  readXML(filename, header, record);
152 
153  return record.endcapItems();
154 }
155 
157  const std::vector<float>& eb,
158  const std::vector<float>& ee) {
159  if (eb.size() != EBDetId::kSizeForDenseIndexing) {
160  std::cerr << "Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Barrel array size: " << eb.size()
161  << " should be " << EBDetId::kSizeForDenseIndexing << std::endl;
162  return std::string("");
163  }
164 
165  if (ee.size() != EEDetId::kSizeForDenseIndexing) {
166  std::cerr << "Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Endcap array size: " << ee.size()
167  << " should be " << EEDetId::kSizeForDenseIndexing << std::endl;
168  return std::string("");
169  }
170 
172 
173  for (int cellid = 0; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) { // loop on EB cells
174 
175  uint32_t rawid = EBDetId::unhashIndex(cellid);
176  record[rawid] = eb[cellid];
177  }
178 
179  for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) { // loop on EE cells
180 
181  if (EEDetId::validHashIndex(cellid)) {
182  uint32_t rawid = EEDetId::unhashIndex(cellid);
183 
184  record[rawid] = ee[cellid];
185  } // if
186  }
187 
188  return dumpXML(header, record);
189 }
190 
194 
195  unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
196 
197  DOMLSSerializer* writer = impl->createLSSerializer();
198  if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
199  writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
200 
201  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
202  DOMDocument* doc =
203  impl->createDocument(nullptr, cms::xerces::uStr(EcalFloatCondObjectContainer_tag.c_str()).ptr(), doctype);
204  DOMElement* root = doc->getDocumentElement();
205 
207 
208  for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) { // loop on EB cells
209 
210  uint32_t rawid = EBDetId::unhashIndex(cellid);
212  if (value_ptr == record.end())
213  continue; // cell absent from original record
214 
215  DOMElement* cellnode = writeCell(root, rawid);
216 
217  WriteNodeWithValue(cellnode, Value_tag, *value_ptr);
218 
219  } // loop on EB cells
220 
221  for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) { // loop on EE cells
222 
223  if (!EEDetId::validHashIndex(cellid))
224  continue;
225 
226  uint32_t rawid = EEDetId::unhashIndex(cellid);
228  if (value_ptr == record.end())
229  continue; // cell absent from original record
230 
231  DOMElement* cellnode = writeCell(root, rawid);
232  WriteNodeWithValue(cellnode, Value_tag, *value_ptr);
233 
234  } // loop on EE cells
235 
236  std::string dump = cms::xerces::toString(writer->writeToString(root));
237  doc->release();
238  doctype->release();
239  writer->release();
240 
242 
243  return dump;
244 }
245 
247  const EcalCondHeader& header,
250 
251  std::fstream fs(filename.c_str(), ios::out);
252  fs << dumpXML(header, record);
253 
255 
256  return 0;
257 }
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalFloatCondObjectContainer &record)
static std::vector< float > barrelfromXML(const std::string &filename)
def dumpXML(db, tag, since, filename='dump.xml')
void xercesTerminate()
Definition: Xerces.cc:23
const std::string EcalFloatCondObjectContainer_tag("EcalFloatCondObjectContainer")
void xercesInitialize()
Definition: Xerces.cc:18
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:65
static int readXML(const std::string &filename, EcalCondHeader &header, EcalFloatCondObjectContainer &record)
void WriteNodeWithValue(xercesc::DOMNode *parentNode, const std::string &tag, const T &value)
write a node with
const std::string Value_tag("Value")
static bool validHashIndex(int i)
Definition: EEDetId.h:239
std::string toString(XMLCh const *toTranscode)
static const int MIN_HASH
Definition: EBDetId.h:149
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
ZStr< XMLCh > uStr(char const *str)
static std::vector< float > endcapfromXML(const std::string &filename)
std::vector< Item >::const_iterator const_iterator
static std::string dumpXML(const EcalCondHeader &header, const std::vector< float > &eb, const std::vector< float > &ee)
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:110
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
def exit(msg="")