File indexing completed on 2024-04-06 12:03:09
0001
0002
0003
0004
0005
0006 #include <vector>
0007 #include <string>
0008 #include <fstream>
0009 #include <sstream>
0010
0011 #include "CalibFormats/HcalObjects/interface/HcalText2DetIdConverter.h"
0012
0013 #include "CondFormats/HcalObjects/interface/AllObjects.h"
0014
0015
0016 #include <xercesc/util/XMLString.hpp>
0017 #include <xercesc/dom/DOMElement.hpp>
0018 #include <xercesc/dom/DOMText.hpp>
0019 #include <xercesc/dom/DOMImplementation.hpp>
0020 #include <xercesc/dom/DOMImplementationRegistry.hpp>
0021 #include <xercesc/dom/DOMConfiguration.hpp>
0022 #include <xercesc/dom/DOMDocument.hpp>
0023 #include <xercesc/dom/DOMLSOutput.hpp>
0024 #include <xercesc/dom/DOMLSSerializer.hpp>
0025
0026 #include "Utilities/Xerces/interface/Xerces.h"
0027 #include "CondTools/Hcal/interface/StreamOutFormatTarget.h"
0028
0029 #include "CondTools/Hcal/interface/HcalDbXml.h"
0030
0031 using namespace std;
0032 using namespace xercesc;
0033
0034 namespace {
0035 template <class T>
0036 XMLCh* transcode(const T& fInput) {
0037 ostringstream ost;
0038 ost << fInput;
0039 return XMLString::transcode(ost.str().c_str());
0040 }
0041
0042 const char* IOV_ID = "IOV_ID";
0043 const char* TAG_ID = "TAG_ID";
0044
0045 std::string kind(const HcalPedestals& fObject) { return "HCAL_PEDESTALS_V2"; }
0046 std::string kind(const HcalGains& fObject) { return "HCAL Gains"; }
0047 std::string kind(const HcalRawGains& fObject) { return "HCAL Raw Gains"; }
0048
0049 std::string extensionTableName(const std::string& fKind) {
0050 if (fKind == "HCAL_PEDESTALS_V2")
0051 return "HCAL_PEDESTALS_V2";
0052 if (fKind == "HCAL Gains")
0053 return "HCAL_GAIN_PEDSTL_CALIBRATIONS";
0054 if (fKind == "HCAL Raw Gains")
0055 return "HCAL_RAW_GAINS";
0056 return "UNKNOWN";
0057 }
0058 }
0059
0060 class XMLDocument {
0061 public:
0062 XMLDocument();
0063 template <class T>
0064 DOMElement* newElement(DOMElement* fParent, const T& fName);
0065 template <class T>
0066 DOMElement* newValue(DOMElement* fParent, const std::string& fName, const T& fValue);
0067 template <class T>
0068 void addAttribute(DOMElement* fElement, const std::string& fName, const T& fValue);
0069 const DOMDocument* document();
0070 void streamOut(std::ostream& fOut);
0071
0072 DOMElement* root();
0073 DOMElement* makeHeader(DOMElement* fRoot, const std::string& fExtensionName, unsigned long fRun);
0074 DOMElement* makeDataset(DOMElement* fRoot, const std::string& fVersion);
0075 DOMElement* makeElement(DOMElement* fRoot);
0076 DOMElement* makeMaps(DOMElement* fRoot);
0077
0078 DOMElement* makeType(DOMElement* fHeader, const std::string& fExtensionName);
0079 DOMElement* makeRun(DOMElement* fHeader, unsigned long fRun);
0080
0081 DOMElement* makeChId(DOMElement* fDataset, DetId fId);
0082
0083 DOMElement* makeElementDataset(DOMElement* fElement,
0084 int fXMLId,
0085 DetId fDetId,
0086 const std::string& fVersion,
0087 const std::string& fKind,
0088 unsigned long fRun);
0089 DOMElement* makeElementIOV(DOMElement* fElement, unsigned long long fIovBegin, unsigned long long fIovEnd = 0);
0090 DOMElement* makeElementTag(DOMElement* fElement,
0091 const std::string& fTagName,
0092 const std::string& fDetectorName,
0093 const std::string& fComment = "Automatically created by HcalDbXml");
0094
0095 DOMElement* makeMapTag(DOMElement* fMap);
0096 DOMElement* makeMapIOV(DOMElement* fTag);
0097 DOMElement* makeMapDataset(DOMElement* fIov, int fXMLId);
0098
0099 DOMElement* makeData(DOMElement* fDataset, const HcalPedestal& fPed, const HcalPedestalWidth& fWidth);
0100 DOMElement* makeData(DOMElement* fDataset);
0101
0102
0103 void addData(DOMElement* fData, const HcalPedestal& fItem);
0104 void addData(DOMElement* fData, const HcalPedestalWidth& fItem);
0105 void addData(DOMElement* fData, const HcalGain& fItem);
0106 void addData(DOMElement* fData, const HcalRawGain& fItem);
0107 void addData(DOMElement* fData, const HcalGainWidth& fItem);
0108
0109 private:
0110 DOMImplementation* mDom;
0111 DOMDocument* mDoc;
0112 };
0113
0114 XMLDocument::XMLDocument() : mDoc(nullptr) {
0115 cms::concurrency::xercesInitialize();
0116 mDom = DOMImplementationRegistry::getDOMImplementation(transcode("Core"));
0117 mDoc = mDom->createDocument(nullptr,
0118 transcode("ROOT"),
0119 nullptr);
0120 }
0121
0122 template <class T>
0123 DOMElement* XMLDocument::newElement(DOMElement* fParent, const T& fName) {
0124 DOMElement* element = mDoc->createElement(transcode(fName));
0125 fParent->appendChild(element);
0126 return element;
0127 }
0128
0129 template <class T>
0130 DOMElement* XMLDocument::newValue(DOMElement* fParent, const std::string& fName, const T& fValue) {
0131 DOMElement* element = newElement(fParent, fName);
0132 DOMText* text = mDoc->createTextNode(transcode(fValue));
0133 element->appendChild(text);
0134 return element;
0135 }
0136
0137 template <class T>
0138 void XMLDocument::addAttribute(DOMElement* fElement, const std::string& fName, const T& fValue) {
0139 fElement->setAttribute(transcode(fName), transcode(fValue));
0140 }
0141
0142 DOMElement* XMLDocument::root() { return mDoc->getDocumentElement(); }
0143
0144 DOMElement* XMLDocument::makeHeader(DOMElement* fRoot, const std::string& fExtensionName, unsigned long fRun) {
0145 DOMElement* header = newElement(fRoot, "HEADER");
0146 makeType(header, fExtensionName);
0147 makeRun(header, fRun);
0148 return header;
0149 }
0150
0151 DOMElement* XMLDocument::makeType(DOMElement* fHeader, const std::string& fKind) {
0152 DOMElement* type = newElement(fHeader, "TYPE");
0153 newValue(type, "EXTENSION_TABLE_NAME", extensionTableName(fKind));
0154 newValue(type, "NAME", fKind);
0155 return type;
0156 }
0157
0158 DOMElement* XMLDocument::makeRun(DOMElement* fHeader, unsigned long fRun) {
0159 DOMElement* run = newElement(fHeader, "RUN");
0160 newValue(run, "RUN_TYPE", "HcalDbXml");
0161 newValue(run, "RUN_NUMBER", fRun);
0162 return run;
0163 }
0164
0165 DOMElement* XMLDocument::makeDataset(DOMElement* fRoot, const std::string& fVersion) {
0166 DOMElement* dataset = newElement(fRoot, "DATA_SET");
0167 newValue(dataset, "VERSION", fVersion);
0168 return dataset;
0169 }
0170
0171 DOMElement* XMLDocument::makeChId(DOMElement* fDataset, DetId fId) {
0172 DOMElement* channel = newElement(fDataset, "CHANNEL");
0173 newValue(channel, "EXTENSION_TABLE_NAME", "HCAL_CHANNELS");
0174 HcalText2DetIdConverter parser(fId);
0175 newValue(channel, "DETECTOR_NAME", parser.getFlavor());
0176 int eta = parser.getField(1);
0177 newValue(channel, "ETA", abs(eta));
0178 newValue(channel, "Z", eta > 0 ? 1 : -1);
0179 newValue(channel, "PHI", parser.getField2());
0180 newValue(channel, "DEPTH", parser.getField3());
0181 newValue(channel, "HCAL_CHANNEL_ID", fId.rawId());
0182 return channel;
0183 }
0184
0185 DOMElement* XMLDocument::makeElementDataset(DOMElement* fElement,
0186 int fXMLId,
0187 DetId fDetId,
0188 const std::string& fVersion,
0189 const std::string& fKind,
0190 unsigned long fRun) {
0191 DOMElement* dataset = newElement(fElement, "DATA_SET");
0192 addAttribute(dataset, "id", fXMLId);
0193 newValue(newElement(dataset, "KIND_OF_CONDITION"), "NAME", fKind);
0194 newValue(dataset, "VERSION", fVersion);
0195 makeRun(dataset, fRun);
0196 makeChId(dataset, fDetId);
0197 return dataset;
0198 }
0199
0200 DOMElement* XMLDocument::makeElementIOV(DOMElement* fElement,
0201 unsigned long long fIovBegin,
0202 unsigned long long fIovEnd) {
0203 DOMElement* iov = newElement(fElement, "IOV");
0204 addAttribute(iov, "id", IOV_ID);
0205 newValue(iov, "INTERVAL_OF_VALIDITY_BEGIN", fIovBegin);
0206 if (fIovEnd) {
0207 newValue(iov, "INTERVAL_OF_VALIDITY_END", fIovEnd);
0208 }
0209 return iov;
0210 }
0211
0212 DOMElement* XMLDocument::makeElementTag(DOMElement* fElement,
0213 const std::string& fTagName,
0214 const std::string& fDetectorName,
0215 const std::string& fComment) {
0216 DOMElement* tag = newElement(fElement, "TAG");
0217 addAttribute(tag, "id", TAG_ID);
0218 addAttribute(tag, "mode", "create");
0219 newValue(tag, "TAG_NAME", fTagName);
0220 newValue(tag, "DETECTOR_NAME", fDetectorName);
0221 newValue(tag, "COMMENT_DESCRIPTION", fComment);
0222 return tag;
0223 }
0224
0225 DOMElement* XMLDocument::makeElement(DOMElement* fRoot) {
0226 DOMElement* element = newElement(fRoot, "ELEMENTS");
0227 return element;
0228 }
0229
0230 DOMElement* XMLDocument::makeMaps(DOMElement* fRoot) {
0231 DOMElement* map = newElement(fRoot, "MAPS");
0232 return map;
0233 }
0234
0235 DOMElement* XMLDocument::makeMapTag(DOMElement* fMap) {
0236 DOMElement* tag = newElement(fMap, "TAG");
0237 addAttribute(tag, "idref", TAG_ID);
0238 return tag;
0239 }
0240
0241 DOMElement* XMLDocument::makeMapIOV(DOMElement* fTag) {
0242 DOMElement* iov = newElement(fTag, "IOV");
0243 addAttribute(iov, "idref", IOV_ID);
0244 return iov;
0245 }
0246
0247 DOMElement* XMLDocument::makeMapDataset(DOMElement* fIov, int fXMLId) {
0248 DOMElement* element = newElement(fIov, "DATA_SET");
0249 addAttribute(element, "idref", fXMLId);
0250 return element;
0251 }
0252
0253 DOMElement* XMLDocument::makeData(DOMElement* fDataset) { return newElement(fDataset, "DATA"); }
0254
0255 void XMLDocument::addData(DOMElement* fData, const HcalPedestal& fItem) {
0256 newValue(fData, "CAPACITOR_0_VALUE", fItem.getValue(0));
0257 newValue(fData, "CAPACITOR_1_VALUE", fItem.getValue(1));
0258 newValue(fData, "CAPACITOR_2_VALUE", fItem.getValue(2));
0259 newValue(fData, "CAPACITOR_3_VALUE", fItem.getValue(3));
0260 }
0261
0262 void XMLDocument::addData(DOMElement* fData, const HcalPedestalWidth& fItem) {
0263
0264 newValue(fData, "SIGMA_0_0", fItem.getSigma(0, 0));
0265 newValue(fData, "SIGMA_1_1", fItem.getSigma(1, 1));
0266 newValue(fData, "SIGMA_2_2", fItem.getSigma(2, 2));
0267 newValue(fData, "SIGMA_3_3", fItem.getSigma(3, 3));
0268 newValue(fData, "SIGMA_0_1", fItem.getSigma(0, 1));
0269 newValue(fData, "SIGMA_0_2", fItem.getSigma(0, 2));
0270 newValue(fData, "SIGMA_0_3", fItem.getSigma(0, 3));
0271 newValue(fData, "SIGMA_1_2", fItem.getSigma(1, 2));
0272 newValue(fData, "SIGMA_1_3", fItem.getSigma(1, 3));
0273 newValue(fData, "SIGMA_2_3", fItem.getSigma(2, 3));
0274 }
0275
0276 void XMLDocument::addData(DOMElement* fData, const HcalGain& fItem) {
0277 newValue(fData, "CAPACITOR_0_VALUE", fItem.getValue(0));
0278 newValue(fData, "CAPACITOR_1_VALUE", fItem.getValue(1));
0279 newValue(fData, "CAPACITOR_2_VALUE", fItem.getValue(2));
0280 newValue(fData, "CAPACITOR_3_VALUE", fItem.getValue(3));
0281 }
0282
0283 void XMLDocument::addData(DOMElement* fData, const HcalRawGain& fItem) {
0284 newValue(fData, "VALUE", fItem.getValue());
0285 newValue(fData, "ERROR", fItem.getError());
0286 newValue(fData, "VOLTAGE", fItem.getVoltage());
0287 newValue(fData, "STATUS", fItem.strStatus());
0288 }
0289
0290 void XMLDocument::addData(DOMElement* fData, const HcalGainWidth& fItem) {
0291 newValue(fData, "CAPACITOR_0_ERROR", fItem.getValue(0));
0292 newValue(fData, "CAPACITOR_1_ERROR", fItem.getValue(1));
0293 newValue(fData, "CAPACITOR_2_ERROR", fItem.getValue(2));
0294 newValue(fData, "CAPACITOR_3_ERROR", fItem.getValue(3));
0295 }
0296
0297 DOMElement* XMLDocument::makeData(DOMElement* fDataset, const HcalPedestal& fPed, const HcalPedestalWidth& fWidth) {
0298 DOMElement* data = newElement(fDataset, "DATA");
0299
0300 newValue(data, "CAPACITOR_0_VALUE", fPed.getValue(0));
0301 newValue(data, "CAPACITOR_1_VALUE", fPed.getValue(1));
0302 newValue(data, "CAPACITOR_2_VALUE", fPed.getValue(2));
0303 newValue(data, "CAPACITOR_3_VALUE", fPed.getValue(3));
0304
0305 newValue(data, "SIGMA_0_0", fWidth.getSigma(0, 0));
0306 newValue(data, "SIGMA_1_1", fWidth.getSigma(1, 1));
0307 newValue(data, "SIGMA_2_2", fWidth.getSigma(2, 2));
0308 newValue(data, "SIGMA_3_3", fWidth.getSigma(3, 3));
0309 newValue(data, "SIGMA_0_1", fWidth.getSigma(0, 1));
0310 newValue(data, "SIGMA_0_2", fWidth.getSigma(0, 2));
0311 newValue(data, "SIGMA_0_3", fWidth.getSigma(0, 3));
0312 newValue(data, "SIGMA_1_2", fWidth.getSigma(1, 2));
0313 newValue(data, "SIGMA_1_3", fWidth.getSigma(1, 3));
0314 newValue(data, "SIGMA_2_3", fWidth.getSigma(2, 3));
0315 return data;
0316 }
0317
0318 const DOMDocument* XMLDocument::document() { return mDoc; }
0319
0320 void XMLDocument::streamOut(std::ostream& fOut) {
0321 StreamOutFormatTarget formTarget(fOut);
0322 DOMLSSerializer* domWriter = mDom->createLSSerializer();
0323 DOMConfiguration* dc = domWriter->getDomConfig();
0324 dc->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0325
0326 DOMLSOutput* outputDesc = ((DOMImplementationLS*)mDom)->createLSOutput();
0327 outputDesc->setByteStream(&formTarget);
0328
0329 domWriter->write(root(), outputDesc);
0330 mDoc->release();
0331 }
0332
0333 template <class T1, class T2>
0334 bool dumpObject_(std::ostream& fOutput,
0335 unsigned fRun,
0336 unsigned long fGMTIOVBegin,
0337 unsigned long fGMTIOVEnd,
0338 const std::string& fTag,
0339 const T1* fObject1,
0340 const T2* fObject2 = 0) {
0341 if (!fObject1)
0342 return false;
0343 const std::string KIND = kind(*fObject1);
0344
0345 XMLDocument doc;
0346 DOMElement* root = doc.root();
0347 doc.makeHeader(root, KIND, fRun);
0348
0349 DOMElement* elements = doc.makeElement(root);
0350 doc.makeElementIOV(elements, fGMTIOVBegin, fGMTIOVEnd);
0351 doc.makeElementTag(elements, fTag, "HCAL");
0352
0353 DOMElement* iovmap = doc.makeMapIOV(doc.makeMapTag(doc.makeMaps(root)));
0354
0355 std::vector<DetId> detids = fObject1->getAllChannels();
0356 for (unsigned iCh = 0; iCh < detids.size(); iCh++) {
0357 DetId id = detids[iCh];
0358 ostringstream version;
0359 version << fTag << '_' << fGMTIOVBegin;
0360 DOMElement* dataset = doc.makeDataset(root, version.str());
0361 doc.makeChId(dataset, id);
0362 DOMElement* data = doc.makeData(dataset);
0363 doc.addData(data, *(fObject1->getValues(id)));
0364 try {
0365 if (fObject2)
0366 doc.addData(data, *(fObject2->getValues(id)));
0367 } catch (...) {
0368 std::cout << "dumpObject_-> ERROR: width is not available for cell # " << id.rawId() << std::endl;
0369 }
0370 doc.makeElementDataset(elements, iCh, id, version.str(), KIND, fRun);
0371 doc.makeMapDataset(iovmap, iCh);
0372 }
0373 doc.streamOut(fOutput);
0374 return true;
0375 }
0376
0377 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0378 unsigned fRun,
0379 unsigned long fGMTIOVBegin,
0380 unsigned long fGMTIOVEnd,
0381 const std::string& fTag,
0382 const HcalPedestals& fObject,
0383 const HcalPedestalWidths& fError) {
0384 return dumpObject_(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, &fObject, &fError);
0385 }
0386
0387 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0388 unsigned fRun,
0389 unsigned long fGMTIOVBegin,
0390 unsigned long fGMTIOVEnd,
0391 const std::string& fTag,
0392 const HcalPedestals& fObject) {
0393 float dummyError = 0.0001;
0394 std::cout << "HcalDbXml::dumpObject-> set default errors: 0.0001, 0.0001, 0.0001, 0.0001" << std::endl;
0395 HcalPedestalWidths widths(fObject.topo(), fObject.isADC());
0396 std::vector<DetId> channels = fObject.getAllChannels();
0397 for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); channel++) {
0398 HcalPedestalWidth item(*channel);
0399 for (int iCapId = 0; iCapId < 4; iCapId++) {
0400 item.setSigma(iCapId, iCapId, dummyError * dummyError);
0401 }
0402 widths.addValues(item);
0403 }
0404 return dumpObject(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fObject, widths);
0405 }
0406
0407 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0408 unsigned fRun,
0409 unsigned long fGMTIOVBegin,
0410 unsigned long fGMTIOVEnd,
0411 const std::string& fTag,
0412 const HcalGains& fObject,
0413 const HcalGainWidths& fError) {
0414 return dumpObject_(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, &fObject, &fError);
0415 }
0416
0417 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0418 unsigned fRun,
0419 unsigned long fGMTIOVBegin,
0420 unsigned long fGMTIOVEnd,
0421 const std::string& fTag,
0422 const HcalGains& fObject) {
0423 HcalGainWidths widths(fObject.topo());
0424 std::vector<DetId> channels = fObject.getAllChannels();
0425 for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); channel++) {
0426 HcalGainWidth item(*channel, 0, 0, 0, 0);
0427 widths.addValues(item);
0428 }
0429 return dumpObject(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fObject, widths);
0430 }
0431
0432 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0433 unsigned fRun,
0434 unsigned long fGMTIOVBegin,
0435 unsigned long fGMTIOVEnd,
0436 const std::string& fTag,
0437 const HcalRawGains& fObject) {
0438 return dumpObject_(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, &fObject, (const HcalGainWidths*)nullptr);
0439 }