CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CondTools/Hcal/src/HcalDbXml.cc

Go to the documentation of this file.
00001 
00002 //
00003 // F.Ratnikov (UMd), Oct 28, 2005
00004 // $Id: HcalDbXml.cc,v 1.9 2008/11/10 10:13:28 rofierzy Exp $
00005 //
00006 #include <vector>
00007 #include <string>
00008 #include <fstream>
00009 #include <sstream>
00010 
00011 
00012 #include "CalibFormats/HcalObjects/interface/HcalText2DetIdConverter.h"
00013 
00014 #include "CondFormats/HcalObjects/interface/AllObjects.h"
00015 
00016 // Xerces-C
00017 #include <xercesc/util/XMLString.hpp>
00018 #include <xercesc/dom/DOMElement.hpp>
00019 #include <xercesc/dom/DOMText.hpp>
00020 #include <xercesc/dom/DOMImplementation.hpp>
00021 #include <xercesc/dom/DOMImplementationRegistry.hpp>
00022 #include <xercesc/dom/DOMDocument.hpp>
00023 #include <xercesc/dom/DOMWriter.hpp>
00024 
00025 #include "CondTools/Hcal/interface/StreamOutFormatTarget.h"
00026 
00027 #include "CondTools/Hcal/interface/HcalDbXml.h"
00028 
00029 using namespace std;
00030 using namespace xercesc;
00031 
00032 namespace {
00033   template <class T> XMLCh* transcode (const T& fInput) {
00034     ostringstream ost;
00035     ost << fInput;
00036     return XMLString::transcode (ost.str().c_str());
00037   } 
00038 
00039   const char* IOV_ID = "IOV_ID";
00040   const char* TAG_ID = "TAG_ID";
00041 
00042   std::string kind (const HcalPedestals& fObject) { return "HCAL_PEDESTALS_V2";}
00043   std::string kind (const HcalGains& fObject) { return "HCAL Gains";}
00044   std::string kind (const HcalRawGains& fObject) { return "HCAL Raw Gains";}
00045 
00046   std::string extensionTableName (const std::string& fKind) {
00047     if (fKind == "HCAL_PEDESTALS_V2") return "HCAL_PEDESTALS_V2";
00048     if (fKind == "HCAL Gains") return "HCAL_GAIN_PEDSTL_CALIBRATIONS";
00049     if (fKind == "HCAL Raw Gains") return "HCAL_RAW_GAINS";
00050     return "UNKNOWN";
00051   }
00052 }
00053 
00054 class XMLDocument {
00055 public:
00056   XMLDocument ();
00057   template <class T> DOMElement* newElement (DOMElement* fParent, const T& fName);
00058   template <class T> DOMElement* newValue (DOMElement* fParent, const std::string& fName, const T& fValue);
00059   template <class T> void addAttribute (DOMElement* fElement, const std::string& fName, const T& fValue);
00060   const DOMDocument* document ();
00061   void streamOut (std::ostream& fOut);
00062 
00063   DOMElement* root ();
00064   DOMElement* makeHeader (DOMElement* fRoot, const std::string& fExtensionName, unsigned long fRun);
00065   DOMElement* makeDataset (DOMElement* fRoot, const std::string& fVersion);
00066   DOMElement* makeElement (DOMElement* fRoot);
00067   DOMElement* makeMaps (DOMElement* fRoot);
00068 
00069   DOMElement* makeType (DOMElement* fHeader, const std::string& fExtensionName);
00070   DOMElement* makeRun (DOMElement* fHeader, unsigned long fRun);
00071 
00072   DOMElement* makeChId (DOMElement* fDataset, DetId fId);
00073 
00074   DOMElement* makeElementDataset (DOMElement* fElement, int fXMLId, DetId fDetId, const std::string& fVersion, const std::string& fKind, unsigned long fRun);
00075   DOMElement* makeElementIOV (DOMElement* fElement, unsigned long long fIovBegin, unsigned long long fIovEnd = 0);
00076   DOMElement* makeElementTag (DOMElement* fElement, const std::string& fTagName, const std::string& fDetectorName, const std::string& fComment = "Automatically created by HcalDbXml");
00077 
00078   DOMElement* makeMapTag (DOMElement* fMap);
00079   DOMElement* makeMapIOV (DOMElement* fTag);
00080   DOMElement* makeMapDataset (DOMElement* fIov, int fXMLId);
00081 
00082   DOMElement* makeData (DOMElement* fDataset, const HcalPedestal& fPed, const HcalPedestalWidth& fWidth);
00083   DOMElement* makeData (DOMElement* fDataset);
00084 
00085   // specializations
00086   void addData (DOMElement* fData, const HcalPedestal& fItem);
00087   void addData (DOMElement* fData, const HcalPedestalWidth& fItem);
00088   void addData (DOMElement* fData, const HcalGain& fItem);
00089   void addData (DOMElement* fData, const HcalRawGain& fItem);
00090   void addData (DOMElement* fData, const HcalGainWidth& fItem);
00091 
00092 private:
00093   DOMImplementation* mDom;
00094   DOMDocument* mDoc;
00095 };
00096 
00097   XMLDocument::XMLDocument () 
00098     : mDoc (0)
00099   {
00100     XMLPlatformUtils::Initialize();
00101     mDom =  DOMImplementationRegistry::getDOMImplementation (transcode ("Core"));
00102     mDoc = mDom->createDocument(
00103                                 0,                    // root element namespace URI.
00104                                 transcode ("ROOT"),         // root element name
00105                                 0);                   // document type object (DTD).
00106   }
00107 
00108   template <class T> DOMElement* XMLDocument::newElement (DOMElement* fParent, const T& fName) {
00109     DOMElement* element = mDoc->createElement (transcode (fName));
00110     fParent->appendChild (element);
00111     return element;
00112   }
00113 
00114   template <class T> DOMElement* XMLDocument::newValue (DOMElement* fParent, const std::string& fName, const T& fValue) {
00115     DOMElement* element = newElement (fParent, fName);
00116     DOMText* text = mDoc->createTextNode (transcode (fValue));
00117     element->appendChild (text);
00118     return element;
00119   }
00120   
00121   template <class T> void XMLDocument::addAttribute (DOMElement* fElement, const std::string& fName, const T& fValue) {
00122     fElement->setAttribute (transcode (fName), transcode (fValue));
00123   }
00124   
00125   DOMElement* XMLDocument::root () { return mDoc->getDocumentElement();}
00126 
00127   DOMElement* XMLDocument::makeHeader (DOMElement* fRoot, const std::string& fExtensionName, unsigned long fRun) {
00128     DOMElement* header = newElement (fRoot, "HEADER");
00129     makeType (header, fExtensionName);
00130     makeRun (header, fRun);
00131     return header;
00132   }
00133 
00134   DOMElement* XMLDocument::makeType (DOMElement* fHeader, const std::string& fKind) {
00135       DOMElement* type = newElement (fHeader, "TYPE");
00136       newValue (type, "EXTENSION_TABLE_NAME", extensionTableName (fKind));
00137       newValue (type, "NAME", fKind);
00138       return type;
00139   }
00140 
00141   DOMElement* XMLDocument::makeRun (DOMElement* fHeader, unsigned long fRun) {
00142       DOMElement* run =newElement (fHeader, "RUN");
00143       newValue (run, "RUN_TYPE", "HcalDbXml");
00144       newValue (run, "RUN_NUMBER", fRun);
00145       return run;
00146   }
00147 
00148   DOMElement* XMLDocument::makeDataset (DOMElement* fRoot, const std::string& fVersion) {
00149       DOMElement* dataset =newElement (fRoot, "DATA_SET");
00150       newValue (dataset, "VERSION", fVersion);
00151       return dataset;
00152   }
00153 
00154   DOMElement* XMLDocument::makeChId (DOMElement* fDataset, DetId fId) {
00155     DOMElement* channel = newElement (fDataset, "CHANNEL");
00156     newValue (channel, "EXTENSION_TABLE_NAME", "HCAL_CHANNELS");
00157     HcalText2DetIdConverter parser (fId);
00158     newValue (channel, "DETECTOR_NAME", parser.getFlavor ());
00159     int eta = parser.getField (1);
00160     newValue (channel, "ETA", abs(eta));
00161     newValue (channel, "Z", eta > 0 ? 1 : -1);
00162     newValue (channel, "PHI", parser.getField2 ());
00163     newValue (channel, "DEPTH", parser.getField3 ());
00164     newValue (channel, "HCAL_CHANNEL_ID", fId.rawId());
00165     return channel;
00166   }
00167 
00168   DOMElement* XMLDocument::makeElementDataset (DOMElement* fElement, int fXMLId, DetId fDetId, const std::string& fVersion, const std::string& fKind, unsigned long fRun) {
00169     DOMElement* dataset = newElement (fElement, "DATA_SET");
00170     addAttribute (dataset, "id", fXMLId);
00171     newValue (newElement (dataset, "KIND_OF_CONDITION"), "NAME", fKind);
00172     newValue (dataset, "VERSION", fVersion);
00173     makeRun (dataset, fRun);
00174     makeChId (dataset, fDetId);
00175     return dataset;
00176   }
00177 
00178   DOMElement* XMLDocument::makeElementIOV (DOMElement* fElement, unsigned long long fIovBegin, unsigned long long fIovEnd) {
00179     DOMElement* iov = newElement (fElement, "IOV");
00180     addAttribute (iov, "id", IOV_ID);
00181     newValue (iov, "INTERVAL_OF_VALIDITY_BEGIN", fIovBegin);
00182     if (fIovEnd) {
00183       newValue (iov, "INTERVAL_OF_VALIDITY_END", fIovEnd);
00184     }
00185     return iov;
00186   }
00187 
00188   DOMElement* XMLDocument::makeElementTag (DOMElement* fElement, const std::string& fTagName, const std::string& fDetectorName, const std::string& fComment) {
00189     DOMElement* tag = newElement (fElement, "TAG");
00190     addAttribute (tag, "id", TAG_ID);
00191     addAttribute (tag, "mode", "create");
00192     newValue (tag, "TAG_NAME", fTagName);
00193     newValue (tag, "DETECTOR_NAME", fDetectorName);
00194     newValue (tag, "COMMENT_DESCRIPTION", fComment);
00195     return tag;
00196   }
00197 
00198   DOMElement* XMLDocument::makeElement (DOMElement* fRoot) {
00199     DOMElement* element = newElement (fRoot, "ELEMENTS");
00200     return element;
00201   }
00202   
00203   DOMElement* XMLDocument::makeMaps (DOMElement* fRoot) {
00204     DOMElement* map = newElement (fRoot, "MAPS");
00205     return map;
00206   }
00207 
00208   DOMElement* XMLDocument::makeMapTag (DOMElement* fMap) {
00209     DOMElement* tag = newElement (fMap, "TAG");
00210     addAttribute (tag, "idref", TAG_ID);
00211     return tag;
00212   }
00213 
00214   DOMElement* XMLDocument::makeMapIOV (DOMElement* fTag) {
00215     DOMElement* iov = newElement (fTag, "IOV");
00216     addAttribute (iov, "idref", IOV_ID);
00217     return iov;
00218   }
00219 
00220   DOMElement* XMLDocument::makeMapDataset (DOMElement* fIov, int fXMLId) {
00221     DOMElement* element = newElement (fIov, "DATA_SET");
00222     addAttribute (element, "idref", fXMLId);
00223     return element;
00224   }
00225   
00226   DOMElement* XMLDocument::makeData (DOMElement* fDataset) {
00227     return  newElement (fDataset, "DATA");
00228   }
00229 
00230   void XMLDocument::addData (DOMElement* fData, const HcalPedestal& fItem) {
00231     newValue (fData, "CAPACITOR_0_VALUE", fItem.getValue (0));
00232     newValue (fData, "CAPACITOR_1_VALUE", fItem.getValue (1));
00233     newValue (fData, "CAPACITOR_2_VALUE", fItem.getValue (2));
00234     newValue (fData, "CAPACITOR_3_VALUE", fItem.getValue (3));
00235   }
00236 
00237   void XMLDocument::addData (DOMElement* fData, const HcalPedestalWidth& fItem) {
00238     // widths
00239     newValue (fData, "SIGMA_0_0", fItem.getSigma (0, 0));
00240     newValue (fData, "SIGMA_1_1", fItem.getSigma (1, 1));
00241     newValue (fData, "SIGMA_2_2", fItem.getSigma (2, 2));
00242     newValue (fData, "SIGMA_3_3", fItem.getSigma (3, 3));
00243     newValue (fData, "SIGMA_0_1", fItem.getSigma (0, 1));
00244     newValue (fData, "SIGMA_0_2", fItem.getSigma (0, 2));
00245     newValue (fData, "SIGMA_0_3", fItem.getSigma (0, 3));
00246     newValue (fData, "SIGMA_1_2", fItem.getSigma (1, 2));
00247     newValue (fData, "SIGMA_1_3", fItem.getSigma (1, 3));
00248     newValue (fData, "SIGMA_2_3", fItem.getSigma (2, 3));
00249   }
00250 
00251   void XMLDocument::addData (DOMElement* fData, const HcalGain& fItem) {
00252     newValue (fData, "CAPACITOR_0_VALUE", fItem.getValue (0));
00253     newValue (fData, "CAPACITOR_1_VALUE", fItem.getValue (1));
00254     newValue (fData, "CAPACITOR_2_VALUE", fItem.getValue (2));
00255     newValue (fData, "CAPACITOR_3_VALUE", fItem.getValue (3));
00256   }
00257 
00258   void XMLDocument::addData (DOMElement* fData, const HcalRawGain& fItem) {
00259     newValue (fData, "VALUE", fItem.getValue ());
00260     newValue (fData, "ERROR", fItem.getError ());
00261     newValue (fData, "VOLTAGE", fItem.getVoltage ());
00262     newValue (fData, "STATUS", fItem.strStatus ());
00263   }
00264 
00265  void XMLDocument::addData (DOMElement* fData, const HcalGainWidth& fItem) {
00266     newValue (fData, "CAPACITOR_0_ERROR", fItem.getValue (0));
00267     newValue (fData, "CAPACITOR_1_ERROR", fItem.getValue (1));
00268     newValue (fData, "CAPACITOR_2_ERROR", fItem.getValue (2));
00269     newValue (fData, "CAPACITOR_3_ERROR", fItem.getValue (3));
00270   }
00271 
00272 
00273   DOMElement* XMLDocument::makeData (DOMElement* fDataset, const HcalPedestal& fPed, const HcalPedestalWidth& fWidth) {
00274     DOMElement* data = newElement (fDataset, "DATA");
00275     // pedestals
00276     newValue (data, "CAPACITOR_0_VALUE", fPed.getValue (0));
00277     newValue (data, "CAPACITOR_1_VALUE", fPed.getValue (1));
00278     newValue (data, "CAPACITOR_2_VALUE", fPed.getValue (2));
00279     newValue (data, "CAPACITOR_3_VALUE", fPed.getValue (3));
00280     // widths
00281     newValue (data, "SIGMA_0_0", fWidth.getSigma (0, 0));
00282     newValue (data, "SIGMA_1_1", fWidth.getSigma (1, 1));
00283     newValue (data, "SIGMA_2_2", fWidth.getSigma (2, 2));
00284     newValue (data, "SIGMA_3_3", fWidth.getSigma (3, 3));
00285     newValue (data, "SIGMA_0_1", fWidth.getSigma (0, 1));
00286     newValue (data, "SIGMA_0_2", fWidth.getSigma (0, 2));
00287     newValue (data, "SIGMA_0_3", fWidth.getSigma (0, 3));
00288     newValue (data, "SIGMA_1_2", fWidth.getSigma (1, 2));
00289     newValue (data, "SIGMA_1_3", fWidth.getSigma (1, 3));
00290     newValue (data, "SIGMA_2_3", fWidth.getSigma (2, 3));
00291     return data;
00292   }
00293 
00294 
00295   const DOMDocument* XMLDocument::document () {return mDoc;}
00296 
00297   void XMLDocument::streamOut (std::ostream& fOut) {
00298     StreamOutFormatTarget formTaget (fOut);
00299     DOMWriter* domWriter = mDom->createDOMWriter();
00300     domWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00301     domWriter->writeNode (&formTaget, *(root()));
00302     mDoc->release ();
00303   }
00304 
00305 
00306 template <class T1, class T2>
00307 bool dumpObject_ (std::ostream& fOutput, 
00308                   unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, 
00309                   const T1* fObject1, const T2* fObject2 = 0) {
00310   if (!fObject1) return false;
00311   const std::string KIND = kind (*fObject1);
00312   
00313   XMLDocument doc;
00314   DOMElement* root = doc.root ();
00315   doc.makeHeader (root, KIND, fRun);
00316 
00317   DOMElement* elements = doc.makeElement (root);
00318   doc.makeElementIOV (elements, fGMTIOVBegin, fGMTIOVEnd);
00319   doc.makeElementTag (elements, fTag, "HCAL");
00320 
00321   DOMElement* iovmap = doc.makeMapIOV (doc.makeMapTag (doc.makeMaps (root)));
00322   
00323   std::vector<DetId> detids = fObject1->getAllChannels ();
00324   for (unsigned iCh = 0; iCh < detids.size(); iCh++) {
00325     DetId id = detids [iCh];
00326     ostringstream version;
00327     version << fTag << '_' << fGMTIOVBegin; // CONVENTION: version == tag + iov for initial setting
00328     DOMElement* dataset = doc.makeDataset (root, version.str());  
00329     doc.makeChId (dataset, id);
00330     DOMElement* data = doc.makeData (dataset);
00331     doc.addData (data, *(fObject1->getValues (id)));
00332     try {
00333       if (fObject2) doc.addData (data, *(fObject2->getValues (id)));
00334     }
00335     catch (...) {
00336       std::cout << "dumpObject_-> ERROR: width is not available for cell # " << id.rawId() << std::endl;
00337     }
00338     doc.makeElementDataset (elements, iCh, id, version.str(), KIND, fRun);
00339     doc.makeMapDataset (iovmap, iCh);
00340   }
00341   doc.streamOut (fOutput);
00342   return true;
00343 }
00344 
00345 
00346 bool HcalDbXml::dumpObject (std::ostream& fOutput, 
00347                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, 
00348                             const HcalPedestals& fObject, const HcalPedestalWidths& fError) {
00349   return dumpObject_ (fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, &fObject, &fError);
00350 }
00351 
00352 bool HcalDbXml::dumpObject (std::ostream& fOutput, 
00353                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, 
00354                             const HcalPedestals& fObject) {
00355   float dummyError = 0.0001;
00356   std::cout << "HcalDbXml::dumpObject-> set default errors: 0.0001, 0.0001, 0.0001, 0.0001" << std::endl;
00357   HcalPedestalWidths widths(fObject.isADC() );
00358   std::vector<DetId> channels = fObject.getAllChannels ();
00359   for (std::vector<DetId>::iterator channel = channels.begin ();
00360        channel !=  channels.end ();
00361        channel++) {
00362 
00363     HcalPedestalWidth item(*channel);
00364     for (int iCapId = 0; iCapId < 4; iCapId++) {
00365       item.setSigma (iCapId, iCapId, dummyError*dummyError);
00366     }
00367     widths.addValues(item);
00368   }
00369   return dumpObject (fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fObject, widths);
00370 }
00371 
00372 bool HcalDbXml::dumpObject (std::ostream& fOutput, 
00373                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, 
00374                             const HcalGains& fObject, const HcalGainWidths& fError) {
00375   return dumpObject_ (fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, &fObject, &fError);
00376 }
00377 
00378 bool HcalDbXml::dumpObject (std::ostream& fOutput, 
00379                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, 
00380                             const HcalGains& fObject) {
00381   HcalGainWidths widths;
00382   std::vector<DetId> channels = fObject.getAllChannels ();
00383   for (std::vector<DetId>::iterator channel = channels.begin (); channel !=  channels.end (); channel++) 
00384     {
00385       HcalGainWidth item(*channel,0,0,0,0);
00386       widths.addValues(item); // no error
00387     }
00388   return dumpObject (fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fObject, widths);
00389 }
00390 
00391 bool HcalDbXml::dumpObject (std::ostream& fOutput, 
00392                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, 
00393                             const HcalRawGains& fObject) {
00394   return dumpObject_ (fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, &fObject, (const HcalGainWidths*)0);
00395 }