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 
23 int
27  std::cout << "EcalFloatCondObjectContainerXMLTranslatorr::readXML filename " << filename << std::endl;
28  /* old method for DBv1
29  cms::concurrency::xercesInitialize();
30 
31  XercesDOMParser* parser = new XercesDOMParser;
32  parser->setValidationScheme( XercesDOMParser::Val_Never );
33  parser->setDoNamespaces( false );
34  parser->setDoSchema( false );
35 
36  parser->parse(filename.c_str());
37 
38  DOMDocument* xmlDoc = parser->getDocument();
39 
40 
41  if (!xmlDoc) {
42  std::cout << "EcalFloatCondObjectContainerXMLTranslator::Error parsing document" << std::endl;
43  return -1;
44  }
45 
46  DOMElement* elementRoot = xmlDoc->getDocumentElement();
47 
48  xuti::readHeader(elementRoot, header);
49 
50  // get the first cell node
51  DOMNode * cellnode=getChildNode(elementRoot,Cell_tag);
52 
53  // loop on cell nodes
54  while (cellnode){
55 
56  float val=0;
57 
58  // read id
59  DetId detid= readCellId(dynamic_cast<DOMElement*>(cellnode));
60 
61  // read value
62  DOMNode * c_node = getChildNode(cellnode,Value_tag);
63  GetNodeData(c_node,val);
64 
65  // fill record
66  record[detid]=val;
67 
68  // get next cell
69  cellnode= cellnode->getNextSibling();
70 
71  while (cellnode&& cellnode->getNodeType( ) != DOMNode::ELEMENT_NODE)
72  cellnode= cellnode->getNextSibling();
73 
74 
75  }
76 
77 
78  delete parser;
79  cms::concurrency::xercesTerminate();
80  */
81  // new method for DBv2
82  std::string dummyLine, bid;
83  std::ifstream fxml;
84  fxml.open(filename);
85  if(!fxml.is_open()) {
86  std::cout << "ERROR : cannot open file " << filename << std::endl;
87  exit (1);
88  }
89  // header
90  for( int i=0; i< 6; i++) {
91  getline(fxml, dummyLine); // skip first lines
92  // std::cout << dummyLine << std::endl;
93  }
94  fxml >> bid;
95  // std::cout << bid << std::endl;
96  std::string stt = bid.substr(7,5);
97  std::istringstream iEB(stt);
98  int nEB;
99  iEB >> nEB;
100  if(nEB != kEBChannels) {
101  std::cout << " strange number of EB channels " << nEB << std::endl;
102  exit(-1);
103  }
104  fxml >> bid; // <item_version>0</item_version>
105  for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
106  EBDetId myEBDetId = EBDetId::unhashIndex(iChannel);
107  fxml >> bid;
108  std::size_t found = bid.find("</");
109  stt = bid.substr(6, found - 6);
110  float val = std::stof(stt);
111  record[myEBDetId] = val;
112  }
113  for( int i=0; i< 5; i++) {
114  getline(fxml, dummyLine); // skip first lines
115  // std::cout << dummyLine << std::endl;
116  }
117  fxml >> bid;
118  // cout << bid << endl;
119  stt = bid.substr(7,5);
120  std::istringstream iEE(stt);
121  int nEE;
122  iEE >> nEE;
123  if(nEE != kEEChannels) {
124  std::cout << " strange number of EE channels " << nEE << std::endl;
125  exit(-1);
126  }
127  fxml >> bid; // <item_version>0</item_version>
128  // now endcaps
129  for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
130  EEDetId myEEDetId = EEDetId::unhashIndex(iChannel);
131  fxml >> bid;
132  std::size_t found = bid.find("</");
133  stt = bid.substr(6, found - 6);
134  float val = std::stof(stt);
135  record[myEEDetId] = val;
136  }
137 
138  return 0;
139 
140 }
141 
142 
143 
144 std::vector<float>
148  readXML(filename,header,record);
149 
150  return record.barrelItems();
151 
152 }
153 
154 
155 std::vector<float>
159  readXML(filename,header,record);
160 
161  return record.endcapItems();
162 
163 }
164 
165 
166 
169  const EcalCondHeader& header,
170  const std::vector<float>& eb,
171  const std::vector<float>& ee){
172 
173 
174  if (eb.size() != EBDetId::kSizeForDenseIndexing){
175  std::cerr<<"Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Barrel array size: "
176  <<eb.size() << " should be "<< EBDetId::kSizeForDenseIndexing<< std::endl;
177  return std::string("");
178  }
179 
180  if (ee.size() != EEDetId::kSizeForDenseIndexing){
181  std::cerr<<"Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Endcap array size: "
182  <<ee.size() << " should be "<< EEDetId::kSizeForDenseIndexing<< std::endl;
183  return std::string("");
184  }
185 
187 
188  for (int cellid = 0;
190  ++cellid){// loop on EB cells
191 
192  uint32_t rawid = EBDetId::unhashIndex(cellid);
193  record[rawid] = eb[cellid];
194  }
195 
196  for (int cellid = 0;
198  ++cellid){// loop on EE cells
199 
200 
201 
202  if (EEDetId::validHashIndex(cellid)){
203  uint32_t rawid = EEDetId::unhashIndex(cellid);
204 
205  record[rawid]=ee[cellid];
206  } // if
207  }
208 
209  return dumpXML(header,record);
210 
211 
212 }
213 
216  const EcalCondHeader& header,
218 
220 
221  unique_ptr<DOMImplementation> impl( DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
222 
223  DOMLSSerializer* writer = impl->createLSSerializer();
224  if( writer->getDomConfig()->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true ))
225  writer->getDomConfig()->setParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true );
226 
227  DOMDocumentType* doctype = impl->createDocumentType( cms::xerces::uStr("XML").ptr(), nullptr, nullptr );
228  DOMDocument* doc = impl->createDocument( nullptr, cms::xerces::uStr(EcalFloatCondObjectContainer_tag.c_str()).ptr(), doctype );
229  DOMElement* root = doc->getDocumentElement();
230 
231  xuti::writeHeader(root, header);
232 
233  for (int cellid = EBDetId::MIN_HASH;
235  ++cellid){// loop on EB cells
236 
237  uint32_t rawid= EBDetId::unhashIndex(cellid);
239  record.find(rawid);
240  if (value_ptr==record.end()) continue; // cell absent from original record
241 
242  DOMElement* cellnode=writeCell(root,rawid);
243 
244  WriteNodeWithValue(cellnode,Value_tag,*value_ptr);
245 
246 
247  } // loop on EB cells
248 
249 
250 
251  for (int cellid = 0;
253  ++cellid){// loop on EE cells
254 
255  if (!EEDetId::validHashIndex(cellid)) continue;
256 
257  uint32_t rawid= EEDetId::unhashIndex(cellid);
259  record.find(rawid);
260  if (value_ptr==record.end()) continue; // cell absent from original record
261 
262 
263  DOMElement* cellnode= writeCell(root,rawid);
264  WriteNodeWithValue(cellnode,Value_tag,*value_ptr);
265 
266 
267  } // loop on EE cells
268 
269 
270  std::string dump = cms::xerces::toString( writer->writeToString( root ));
271  doc->release();
272  doctype->release();
273  writer->release();
274 
276 
277  return dump;
278 }
279 
280 
281 
282 int
284  const EcalCondHeader& header,
286 
288 
289  std::fstream fs(filename.c_str(),ios::out);
290  fs<< dumpXML(header,record);
291 
293 
294  return 0;
295 }
def readXML(fileName)
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')
JetCorrectorParameters::Record record
Definition: classes.h:7
void xercesTerminate()
Definition: Xerces.cc:23
const std::string EcalFloatCondObjectContainer_tag("EcalFloatCondObjectContainer")
const Items & barrelItems() const
void xercesInitialize()
Definition: Xerces.cc:18
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:99
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:156
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
ZStr< XMLCh > uStr(char const *str)
static std::vector< float > endcapfromXML(const std::string &filename)
Definition: dump.py:1
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:114
const_iterator find(uint32_t rawId) const
const_iterator end() const
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const Items & endcapItems() const