CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalLaserAPDPNRatiosXMLTranslator.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>
8 #include <xercesc/util/XMLString.hpp>
9 #include <xercesc/sax/SAXException.hpp>
10 #include <xercesc/framework/LocalFileFormatTarget.hpp>
11 
14 
17 
18 using namespace XERCES_CPP_NAMESPACE;
19 using namespace xuti;
20 using namespace std;
21 
22 
23 //
24 // TODO: write and read time map
25 //
26 //
27 
28 
29 
31  EcalCondHeader& header,
33 
34 
35 
37 
38  XercesDOMParser* parser = new XercesDOMParser;
39  parser->setValidationScheme( XercesDOMParser::Val_Never );
40  parser->setDoNamespaces( false );
41  parser->setDoSchema( false );
42 
43  parser->parse(filename.c_str());
44 
45  DOMDocument* xmlDoc = parser->getDocument();
46  if (!xmlDoc) {
47  std::cout << "EcalLaserAPDPNRatiosXMLTranslator::Error parsing document" << std::endl;
48  return -1;
49  }
50 
51  DOMElement* elementRoot = xmlDoc->getDocumentElement();
52 
53  xuti::readHeader(elementRoot,header);
54 
55  DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
56 
57  while(cellnode)
58  {
59  float p1 = 0;
60  float p2 = 0;
61  float p3 = 0;
62 // edm::TimeStamp t1=0;
63 // edm::TimeStamp t2=0;
64 // edm::TimeStamp t3=0;
65 
66 
67  DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
68 
69  DOMNode* p1_node = getChildNode(cellnode,Laser_p1_tag);
70  GetNodeData(p1_node,p1);
71 
72  DOMNode* p2_node = getChildNode(cellnode,Laser_p2_tag);
73  GetNodeData(p2_node,p2);
74 
75  DOMNode* p3_node = getChildNode(cellnode,Laser_p3_tag);
76  GetNodeData(p3_node,p3);
77 
78 // DOMNode* t1_node = getChildNode(cellnode,Laser_t1_tag);
79 // GetNodeData(t1_node,t1);
80 
81 // DOMNode* t2_node = getChildNode(cellnode,Laser_t2_tag);
82 // GetNodeData(t3_node,t2);
83 
84 // DOMNode* p1_node = getChildNode(cellnode,Laser_t3_tag);
85 // GetNodeData(t3_node,t3);
86 
87 
89  pair.p1 =p1;
90  pair.p2 =p2;
91  pair.p3 =p3;
92 
93  record.setValue(detid,pair);
94 
95  cellnode = cellnode->getNextSibling();
96 
97  while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
98  cellnode = cellnode->getNextSibling();
99 
100 
101  }
102 
103  delete parser;
105  return 0;
106 
107 
108 }
109 
110 
111 
112 
113 
115  const EcalCondHeader& header,
117  std::fstream fs(filename.c_str(),ios::out);
118  fs<< dumpXML(header,record);
119  return 0;
120  }
121 
122 
124  const EcalCondHeader& header,
126 
127 
129 
130  DOMImplementation* impl =
131  DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
132 
133  DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
134  writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
135 
136  DOMDocumentType* doctype = impl->createDocumentType( fromNative("XML").c_str(), 0, 0 );
137  DOMDocument * doc =
138  impl->createDocument( 0, fromNative(Laser_tag).c_str(), doctype );
139 
140 
141  doc->setEncoding(fromNative("UTF-8").c_str() );
142  doc->setStandalone(true);
143  doc->setVersion(fromNative("1.0").c_str() );
144 
145 
146  DOMElement* root = doc->getDocumentElement();
147 
148  xuti::writeHeader(root,header);
149 
150  string Lasertag = "Laser", LMtag = "LM";
151  for(int cellid = 0; cellid < (int)record.getTimeMap().size(); cellid++) {
152 
153  DOMElement* cellnode = doc->createElement( fromNative(Lasertag).c_str());
154  root->appendChild(cellnode);
155  stringstream value_s;
156  value_s << cellid;
157  cellnode->setAttribute(fromNative(LMtag).c_str(),
158  fromNative(value_s.str()).c_str());
159 
160  long int t123[3];
161  t123[0]=(record.getTimeMap())[cellid].t1.value();
162  t123[1]=(record.getTimeMap())[cellid].t2.value();
163  t123[2]=(record.getTimeMap())[cellid].t3.value();
164  string Laser_t_tag[3] = {"t1","t2","t3"};
165  for(int i = 0; i < 3; i++) {
166  time_t t = t123[i] >> 32;
167  char buf[256];
168  struct tm lt;
169  localtime_r(&t, &lt);
170  strftime(buf, sizeof(buf), "%F %R:%S", &lt);
171  buf[sizeof(buf)-1] = 0;
172  DOMDocument* subdoc = cellnode->getOwnerDocument();
173  DOMElement* new_node = subdoc->createElement(fromNative(Laser_t_tag[i]).c_str());
174  cellnode->appendChild(new_node);
175  std::stringstream value_ss;
176  value_ss << t123[i];
177  string newstr = value_ss.str() + " [" + string(buf) +"]";
178  DOMText* tvalue =
179  subdoc->createTextNode(fromNative(newstr).c_str());
180  new_node->appendChild(tvalue);
181  }
182  }
183 
184  for(int cellid = EBDetId::MIN_HASH;
186  ++cellid)
187  {
188 
189  uint32_t rawid = EBDetId::unhashIndex(cellid);
190 
191  DOMElement* cellnode= writeCell(root,rawid);
192 
193  float p1=(record.getLaserMap())[rawid].p1;
194  float p2=(record.getLaserMap())[rawid].p2;
195  float p3=(record.getLaserMap())[rawid].p3;
196 
197  WriteNodeWithValue(cellnode,Laser_p1_tag,p1);
198  WriteNodeWithValue(cellnode,Laser_p2_tag,p2);
199  WriteNodeWithValue(cellnode,Laser_p3_tag,p3);
200 
201  }
202 
203 
204 
205 
206  for(int cellid = 0;
208  ++cellid)
209  {
210 
211  if(!EEDetId::validHashIndex(cellid)) continue;
212 
213  uint32_t rawid = EEDetId::unhashIndex(cellid);
214 
215 
216  DOMElement* cellnode=writeCell(root,rawid);
217 
218  float p1=(record.getLaserMap())[rawid].p1;
219  float p2=(record.getLaserMap())[rawid].p2;
220  float p3=(record.getLaserMap())[rawid].p3;
221 
222  WriteNodeWithValue(cellnode,Laser_p1_tag,p1);
223  WriteNodeWithValue(cellnode,Laser_p2_tag,p2);
224  WriteNodeWithValue(cellnode,Laser_p3_tag,p3);
225  }
226 
227 
228  std::string dump= toNative(writer->writeToString(*root));
229  doc->release();
230  // cms::concurrency::xercesTerminate();
231 
232  return dump;
233 
234 }
int i
Definition: DBlmapReader.cc:9
JetCorrectorParameters::Record record
Definition: classes.h:7
static int readXML(const std::string &filename, EcalCondHeader &header, EcalLaserAPDPNRatios &record)
void xercesTerminate()
Definition: Xerces.cc:22
static std::string dumpXML(const EcalCondHeader &header, const EcalLaserAPDPNRatios &record)
const std::string Laser_p3_tag("p3")
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
const EcalLaserAPDPNRatiosMap & getLaserMap() const
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
auto const T2 &decltype(t1.eta()) t2
Definition: deltaR.h:16
static const int MIN_HASH
Definition: EBDetId.h:156
const std::string Cell_tag("cell")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
const std::string Laser_p1_tag("p1")
double p2[4]
Definition: TauolaWrapper.h:90
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalLaserAPDPNRatios &record)
const std::string Laser_tag("EcalLaserAPDPNRatios")
Definition: DetId.h:18
const EcalLaserTimeStampMap & getTimeMap() const
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
void setValue(uint32_t rawId, const EcalLaserAPDPNpair &value)
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:114
double p1[4]
Definition: TauolaWrapper.h:89
tuple filename
Definition: lut2db_cfg.py:20
const std::string Laser_p2_tag("p2")
XercesString fromNative(const char *str)
Definition: XercesString.h:31
tuple cout
Definition: gather_cfg.py:145
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const DetId readCellId(xercesc::DOMElement *node)
Assuming.
std::string toNative(const XMLCh *str)
Definition: XercesString.h:42
double p3[4]
Definition: TauolaWrapper.h:91