Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:41:48

0001 // F.Ratnikov (UMd), Oct 28, 2005
0002 // Modified by S. Won 6 May 2008
0003 //
0004 #include <vector>
0005 #include <string>
0006 
0007 #include "CalibFormats/HcalObjects/interface/HcalText2DetIdConverter.h"
0008 
0009 #include "CondFormats/HcalObjects/interface/AllObjects.h"
0010 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbXml.h"
0011 
0012 namespace {
0013   void dumpProlog(std::ostream& fOutput) {
0014     fOutput << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
0015     fOutput << "<!DOCTYPE root []>" << std::endl;
0016     fOutput << "<ROOT>" << std::endl;
0017   }
0018 
0019   void dumpRun(std::ostream& fOutput, unsigned fRun) {
0020     fOutput << "<RUN>" << std::endl;
0021     fOutput << "   <RUN_TYPE>"
0022             << "HcalDbXml"
0023             << "</RUN_TYPE>" << std::endl;
0024     fOutput << "   <RUN_NUMBER>" << fRun << "</RUN_NUMBER>" << std::endl;
0025     fOutput << "</RUN>" << std::endl;
0026   }
0027 
0028   void dumpHeader(std::ostream& fOutput, unsigned fRun, const std::string& fTableName, const std::string& fTypeName) {
0029     fOutput << "  <HEADER>" << std::endl;
0030     fOutput << "    <TYPE>" << std::endl;
0031     fOutput << "      <EXTENSION_TABLE_NAME>" << fTableName << "</EXTENSION_TABLE_NAME>" << std::endl;
0032     fOutput << "      <NAME>" << fTypeName << "</NAME>" << std::endl;
0033     fOutput << "    </TYPE>" << std::endl;
0034     dumpRun(fOutput, fRun);
0035     fOutput << "  </HEADER>" << std::endl;
0036   }
0037 
0038   void dumpFooter(std::ostream& fOutput) { fOutput << "</ROOT>" << std::endl; }
0039 
0040   void dumpChannelId(std::ostream& fOutput, DetId fChannel) {
0041     HcalText2DetIdConverter converter(fChannel);
0042     fOutput << "<CHANNEL> " << std::endl;
0043     fOutput << "   <EXTENSION_TABLE_NAME>HCAL_CHANNELS</EXTENSION_TABLE_NAME> " << std::endl;
0044     fOutput << "   <ETA>" << abs(converter.getField(1)) << "</ETA>" << std::endl;
0045     fOutput << "   <PHI>" << converter.getField(2) << "</PHI> " << std::endl;
0046     fOutput << "   <DEPTH>" << converter.getField(3) << "</DEPTH> " << std::endl;
0047     fOutput << "   <Z>" << (converter.getField(1) > 0 ? "1" : "-1") << "</Z> " << std::endl;
0048     fOutput << "   <DETECTOR_NAME>" << converter.getFlavor() << "</DETECTOR_NAME> " << std::endl;
0049     fOutput << "   <HCAL_CHANNEL_ID>" << converter.getId().rawId() << "</HCAL_CHANNEL_ID> " << std::endl;
0050     fOutput << "</CHANNEL>" << std::endl;
0051     fOutput << std::endl;
0052   }
0053 
0054   void dumpData(std::ostream& fOutput, const float* fValues, const HcalPedestalWidth& fErrors) {
0055     fOutput << "<DATA> " << std::endl;
0056     fOutput << "   <CAPACITOR_0_VALUE>" << fValues[0] << "</CAPACITOR_0_VALUE> " << std::endl;
0057     fOutput << "   <CAPACITOR_1_VALUE>" << fValues[1] << "</CAPACITOR_1_VALUE> " << std::endl;
0058     fOutput << "   <CAPACITOR_2_VALUE>" << fValues[2] << "</CAPACITOR_2_VALUE> " << std::endl;
0059     fOutput << "   <CAPACITOR_3_VALUE>" << fValues[3] << "</CAPACITOR_3_VALUE> " << std::endl;
0060     fOutput << "   <SIGMA_0_0>" << fErrors.getSigma(0, 0) << "</SIGMA_0_0> " << std::endl;
0061     fOutput << "   <SIGMA_1_1>" << fErrors.getSigma(1, 1) << "</SIGMA_1_1> " << std::endl;
0062     fOutput << "   <SIGMA_2_2>" << fErrors.getSigma(2, 2) << "</SIGMA_2_2> " << std::endl;
0063     fOutput << "   <SIGMA_3_3>" << fErrors.getSigma(3, 3) << "</SIGMA_3_3> " << std::endl;
0064     fOutput << "   <SIGMA_0_1>" << fErrors.getSigma(1, 0) << "</SIGMA_0_1> " << std::endl;
0065     fOutput << "   <SIGMA_0_2>" << fErrors.getSigma(2, 0) << "</SIGMA_0_2> " << std::endl;
0066     fOutput << "   <SIGMA_0_3>" << fErrors.getSigma(3, 0) << "</SIGMA_0_3> " << std::endl;
0067     fOutput << "   <SIGMA_1_2>" << fErrors.getSigma(2, 1) << "</SIGMA_1_2> " << std::endl;
0068     fOutput << "   <SIGMA_1_3>" << fErrors.getSigma(3, 1) << "</SIGMA_1_3> " << std::endl;
0069     fOutput << "   <SIGMA_2_3>" << fErrors.getSigma(3, 2) << "</SIGMA_2_3> " << std::endl;
0070     fOutput << "</DATA> " << std::endl;
0071   }
0072 
0073   void dumpData(std::ostream& fOutput, const float* fValues, const float* fErrors) {
0074     fOutput << "<DATA> " << std::endl;
0075     fOutput << "   <CAPACITOR_0_VALUE>" << fValues[0] << "</CAPACITOR_0_VALUE> " << std::endl;
0076     fOutput << "   <CAPACITOR_1_VALUE>" << fValues[1] << "</CAPACITOR_1_VALUE> " << std::endl;
0077     fOutput << "   <CAPACITOR_2_VALUE>" << fValues[2] << "</CAPACITOR_2_VALUE> " << std::endl;
0078     fOutput << "   <CAPACITOR_3_VALUE>" << fValues[3] << "</CAPACITOR_3_VALUE> " << std::endl;
0079     fOutput << "   <CAPACITOR_0_ERROR>" << fErrors[0] << "</CAPACITOR_0_ERROR> " << std::endl;
0080     fOutput << "   <CAPACITOR_1_ERROR>" << fErrors[1] << "</CAPACITOR_1_ERROR> " << std::endl;
0081     fOutput << "   <CAPACITOR_2_ERROR>" << fErrors[2] << "</CAPACITOR_2_ERROR> " << std::endl;
0082     fOutput << "   <CAPACITOR_3_ERROR>" << fErrors[3] << "</CAPACITOR_3_ERROR> " << std::endl;
0083     fOutput << "</DATA> " << std::endl;
0084   }
0085 
0086   void dumpDataset(std::ostream& fOutput,
0087                    unsigned fVersion = 0,
0088                    const std::string& fFileName = "",
0089                    const std::string& fDescription = "") {
0090     fOutput << "<DATA_SET>" << std::endl;
0091     fOutput << "   <VERSION>" << fVersion << "</VERSION>" << std::endl;
0092     if (!fFileName.empty())
0093       fOutput << "      <DATA_FILE_NAME>" << fFileName << "</DATA_FILE_NAME>" << std::endl;
0094     if (!fDescription.empty())
0095       fOutput << "      <COMMENT_DESCRIPTION>" << fDescription << "</COMMENT_DESCRIPTION>" << std::endl;
0096   }
0097 
0098   void endDataset(std::ostream& fOutput) { fOutput << "</DATA_SET>" << std::endl; }
0099 
0100   void dumpMapping(std::ostream& fOutput,
0101                    unsigned fRun,
0102                    const std::string& fKind,
0103                    unsigned long fGMTIOVBegin,
0104                    unsigned long fGMTIOVEnd,
0105                    const std::string& fTag,
0106                    unsigned fVersion,
0107                    const std::vector<DetId>& fChannels) {
0108     const std::string IOV_ID = "IOV_ID";
0109     const std::string TAG_ID = "TAG_ID";
0110     fOutput << "<ELEMENTS>" << std::endl;
0111     // set channels affected
0112     unsigned int i = fChannels.size();
0113     unsigned int iend = 0;
0114     while (--i >= iend) {
0115       fOutput << "<DATA_SET id=\"" << i << "\">" << std::endl;
0116       dumpRun(fOutput, fRun);
0117       fOutput << "<KIND_OF_CONDITION><NAME>" << fKind << "</NAME></KIND_OF_CONDITION>" << std::endl;
0118       dumpChannelId(fOutput, fChannels[i]);
0119       fOutput << "<VERSION>" << fVersion << "</VERSION>" << std::endl;
0120       fOutput << "</DATA_SET>" << std::endl;
0121     }
0122     // set IOV
0123     fOutput << "<IOV id=\"" << IOV_ID << "\">";
0124     fOutput << "   <INTERVAL_OF_VALIDITY_BEGIN>" << fGMTIOVBegin << "</INTERVAL_OF_VALIDITY_BEGIN>" << std::endl;
0125     fOutput << "   <INTERVAL_OF_VALIDITY_END>" << fGMTIOVEnd << "</INTERVAL_OF_VALIDITY_END>" << std::endl;
0126     fOutput << "</IOV>" << std::endl;
0127     // set TAG
0128     fOutput << "<TAG id=\"" << TAG_ID << "\" mode=\"create\">" << std::endl;
0129     fOutput << "   <TAG_NAME>" << fTag << "</TAG_NAME>" << std::endl;
0130     fOutput << "   <DETECTOR_NAME>HCAL</DETECTOR_NAME>" << std::endl;
0131     fOutput << "   <COMMENT_DESCRIPTION>Automatically created by HcalDbXml</COMMENT_DESCRIPTION>" << std::endl;
0132     fOutput << "</TAG>" << std::endl;
0133 
0134     fOutput << "</ELEMENTS>" << std::endl;
0135 
0136     // mapping itself
0137     fOutput << "<MAPS>" << std::endl;
0138     fOutput << "<TAG idref=\"" << TAG_ID << "\">" << std::endl;
0139     fOutput << "<IOV idref=\"" << IOV_ID << "\">" << std::endl;
0140     i = fChannels.size();
0141     while (--i >= iend) {
0142       fOutput << "<DATA_SET idref=\"" << i << "\"/>" << std::endl;
0143     }
0144     fOutput << "</IOV>" << std::endl;
0145     fOutput << "</TAG>" << std::endl;
0146     fOutput << "</MAPS>" << std::endl;
0147   }
0148 }  // namespace
0149 
0150 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0151                            unsigned fRun,
0152                            unsigned long fGMTIOVBegin,
0153                            unsigned long fGMTIOVEnd,
0154                            const std::string& fTag,
0155                            unsigned fVersion,
0156                            const HcalPedestals& fObject) {
0157   float dummyError = 0.0001;
0158   std::cout << "HcalDbXml::dumpObject-> set default errors: 0.0001, 0.0001, 0.0001, 0.0001" << std::endl;
0159   HcalPedestalWidths widths(fObject.topo(), fObject.isADC());
0160   std::vector<DetId> channels = fObject.getAllChannels();
0161   for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0162     HcalPedestalWidth item(*channel);
0163     for (int iCapId = 1; iCapId <= 4; iCapId++) {
0164       item.setSigma(iCapId, iCapId, dummyError * dummyError);
0165     }
0166     widths.addValues(item);
0167   }
0168   return dumpObject(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, fObject, widths);
0169 }
0170 
0171 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0172                            unsigned fRun,
0173                            unsigned long fGMTIOVBegin,
0174                            unsigned long fGMTIOVEnd,
0175                            const std::string& fTag,
0176                            unsigned fVersion,
0177                            const HcalPedestals& fObject,
0178                            const HcalPedestalWidths& fError) {
0179   const std::string KIND = "HCAL_PEDESTALS_V2";
0180 
0181   dumpProlog(fOutput);
0182   dumpHeader(fOutput, fRun, KIND, KIND);
0183 
0184   std::vector<DetId> channels = fObject.getAllChannels();
0185   for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0186     DetId chId = *channel;
0187     const float* values = fObject.getValues(chId)->getValues();
0188     const HcalPedestalWidth* errors = fError.getValues(chId);
0189     if (!values) {
0190       std::cerr << "HcalDbXml::dumpObject-> Can not get data for channel " << HcalText2DetIdConverter(chId).toString()
0191                 << std::endl;
0192       continue;
0193     }
0194     if (!errors) {
0195       std::cerr << "HcalDbXml::dumpObject-> Can not get errors for channel " << HcalText2DetIdConverter(chId).toString()
0196                 << ". Use defaults" << std::endl;
0197       continue;
0198     }
0199     dumpDataset(fOutput, fVersion, "", "");
0200     dumpChannelId(fOutput, chId);
0201     dumpData(fOutput, values, *errors);
0202     endDataset(fOutput);
0203   }
0204   dumpMapping(fOutput, fRun, KIND, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, channels);
0205 
0206   dumpFooter(fOutput);
0207   return true;
0208 }
0209 
0210 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0211                            unsigned fRun,
0212                            unsigned long fGMTIOVBegin,
0213                            unsigned long fGMTIOVEnd,
0214                            const std::string& fTag,
0215                            unsigned fVersion,
0216                            const HcalGains& fObject) {
0217   float dummyErrors[4] = {0., 0., 0., 0.};
0218   std::cout << "HcalDbXml::dumpObject-> set default errors: 4 x 0.0" << std::endl;
0219 
0220   HcalGainWidths widths(fObject.topo());
0221   std::vector<DetId> channels = fObject.getAllChannels();
0222   for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0223     HcalGainWidth item(*channel, dummyErrors[0], dummyErrors[1], dummyErrors[2], dummyErrors[3]);
0224     widths.addValues(item);
0225   }
0226 
0227   return dumpObject(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, fObject, widths);
0228 }
0229 
0230 bool HcalDbXml::dumpObject(std::ostream& fOutput,
0231                            unsigned fRun,
0232                            unsigned long fGMTIOVBegin,
0233                            unsigned long fGMTIOVEnd,
0234                            const std::string& fTag,
0235                            unsigned fVersion,
0236                            const HcalGains& fObject,
0237                            const HcalGainWidths& fError) {
0238   const std::string KIND = "HCAL Gains";
0239   const std::string TABLE = "HCAL_GAIN_PEDSTL_CALIBRATIONS";
0240 
0241   dumpProlog(fOutput);
0242   dumpHeader(fOutput, fRun, TABLE, KIND);
0243 
0244   std::vector<DetId> channels = fObject.getAllChannels();
0245   for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0246     DetId chId = *channel;
0247     const float* values = fObject.getValues(chId)->getValues();
0248     const float* errors = fError.getValues(chId)->getValues();
0249     if (!values) {
0250       std::cerr << "HcalDbXml::dumpObject-> Can not get data for channel " << HcalText2DetIdConverter(chId).toString()
0251                 << std::endl;
0252       continue;
0253     }
0254     if (!errors) {
0255       std::cerr << "HcalDbXml::dumpObject-> Can not get errors for channel " << HcalText2DetIdConverter(chId).toString()
0256                 << ". Use defaults" << std::endl;
0257       continue;
0258     }
0259     dumpDataset(fOutput, fVersion, "", "");
0260     dumpChannelId(fOutput, chId);
0261     dumpData(fOutput, values, errors);
0262     endDataset(fOutput);
0263   }
0264   dumpMapping(fOutput, fRun, KIND, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, channels);
0265 
0266   dumpFooter(fOutput);
0267   return true;
0268 }