File indexing completed on 2024-04-06 11:57:34
0001
0002
0003
0004 #include <vector>
0005 #include <string>
0006
0007 #include "CalibFormats/CastorObjects/interface/CastorText2DetIdConverter.h"
0008
0009 #include "CondFormats/CastorObjects/interface/AllObjects.h"
0010 #include "CalibCalorimetry/CastorCalib/interface/CastorDbXml.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 << "CastorDbXml"
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 CastorText2DetIdConverter 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 CastorPedestalWidth& 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
0112 int i = fChannels.size();
0113 while (--i >= 0) {
0114 fOutput << "<DATA_SET id=\"" << i << "\">" << std::endl;
0115 dumpRun(fOutput, fRun);
0116 fOutput << "<KIND_OF_CONDITION><NAME>" << fKind << "</NAME></KIND_OF_CONDITION>" << std::endl;
0117 dumpChannelId(fOutput, fChannels[i]);
0118 fOutput << "<VERSION>" << fVersion << "</VERSION>" << std::endl;
0119 fOutput << "</DATA_SET>" << std::endl;
0120 }
0121
0122 fOutput << "<IOV id=\"" << IOV_ID << "\">";
0123 fOutput << " <INTERVAL_OF_VALIDITY_BEGIN>" << fGMTIOVBegin << "</INTERVAL_OF_VALIDITY_BEGIN>" << std::endl;
0124 fOutput << " <INTERVAL_OF_VALIDITY_END>" << fGMTIOVEnd << "</INTERVAL_OF_VALIDITY_END>" << std::endl;
0125 fOutput << "</IOV>" << std::endl;
0126
0127 fOutput << "<TAG id=\"" << TAG_ID << "\" mode=\"create\">" << std::endl;
0128 fOutput << " <TAG_NAME>" << fTag << "</TAG_NAME>" << std::endl;
0129 fOutput << " <DETECTOR_NAME>HCAL</DETECTOR_NAME>" << std::endl;
0130 fOutput << " <COMMENT_DESCRIPTION>Automatically created by CastorDbXml</COMMENT_DESCRIPTION>" << std::endl;
0131 fOutput << "</TAG>" << std::endl;
0132
0133 fOutput << "</ELEMENTS>" << std::endl;
0134
0135
0136 fOutput << "<MAPS>" << std::endl;
0137 fOutput << "<TAG idref=\"" << TAG_ID << "\">" << std::endl;
0138 fOutput << "<IOV idref=\"" << IOV_ID << "\">" << std::endl;
0139 i = fChannels.size();
0140 while (--i >= 0) {
0141 fOutput << "<DATA_SET idref=\"" << i << "\"/>" << std::endl;
0142 }
0143 fOutput << "</IOV>" << std::endl;
0144 fOutput << "</TAG>" << std::endl;
0145 fOutput << "</MAPS>" << std::endl;
0146 }
0147 }
0148
0149 bool CastorDbXml::dumpObject(std::ostream& fOutput,
0150 unsigned fRun,
0151 unsigned long fGMTIOVBegin,
0152 unsigned long fGMTIOVEnd,
0153 const std::string& fTag,
0154 unsigned fVersion,
0155 const CastorPedestals& fObject) {
0156 float dummyError = 0.0001;
0157 std::cout << "CastorDbXml::dumpObject-> set default errors: 0.0001, 0.0001, 0.0001, 0.0001" << std::endl;
0158 CastorPedestalWidths widths(fObject.isADC());
0159 std::vector<DetId> channels = fObject.getAllChannels();
0160 for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0161 CastorPedestalWidth item(*channel);
0162 for (int iCapId = 1; iCapId <= 4; iCapId++) {
0163 item.setSigma(iCapId, iCapId, dummyError * dummyError);
0164 }
0165 widths.addValues(item);
0166 }
0167 return dumpObject(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, fObject, widths);
0168 }
0169
0170 bool CastorDbXml::dumpObject(std::ostream& fOutput,
0171 unsigned fRun,
0172 unsigned long fGMTIOVBegin,
0173 unsigned long fGMTIOVEnd,
0174 const std::string& fTag,
0175 unsigned fVersion,
0176 const CastorPedestals& fObject,
0177 const CastorPedestalWidths& fError) {
0178 const std::string KIND = "HCAL_PEDESTALS_V2";
0179
0180 dumpProlog(fOutput);
0181 dumpHeader(fOutput, fRun, KIND, KIND);
0182
0183 std::vector<DetId> channels = fObject.getAllChannels();
0184 for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0185 DetId chId = *channel;
0186 const float* values = fObject.getValues(chId)->getValues();
0187 const CastorPedestalWidth* errors = fError.getValues(chId);
0188 if (!values) {
0189 std::cerr << "CastorDbXml::dumpObject-> Can not get data for channel "
0190 << CastorText2DetIdConverter(chId).toString() << std::endl;
0191 continue;
0192 }
0193 if (!errors) {
0194 std::cerr << "CastorDbXml::dumpObject-> Can not get errors for channel "
0195 << CastorText2DetIdConverter(chId).toString() << ". Use defaults" << std::endl;
0196 continue;
0197 }
0198 dumpDataset(fOutput, fVersion, "", "");
0199 dumpChannelId(fOutput, chId);
0200 dumpData(fOutput, values, *errors);
0201 endDataset(fOutput);
0202 }
0203 dumpMapping(fOutput, fRun, KIND, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, channels);
0204
0205 dumpFooter(fOutput);
0206 return true;
0207 }
0208
0209 bool CastorDbXml::dumpObject(std::ostream& fOutput,
0210 unsigned fRun,
0211 unsigned long fGMTIOVBegin,
0212 unsigned long fGMTIOVEnd,
0213 const std::string& fTag,
0214 unsigned fVersion,
0215 const CastorGains& fObject) {
0216 float dummyErrors[4] = {0., 0., 0., 0.};
0217 std::cout << "CastorDbXml::dumpObject-> set default errors: 4 x 0.0" << std::endl;
0218
0219 CastorGainWidths widths;
0220 std::vector<DetId> channels = fObject.getAllChannels();
0221 for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0222 CastorGainWidth item(*channel, dummyErrors[0], dummyErrors[1], dummyErrors[2], dummyErrors[3]);
0223 widths.addValues(item);
0224 }
0225
0226 return dumpObject(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, fObject, widths);
0227 }
0228
0229 bool CastorDbXml::dumpObject(std::ostream& fOutput,
0230 unsigned fRun,
0231 unsigned long fGMTIOVBegin,
0232 unsigned long fGMTIOVEnd,
0233 const std::string& fTag,
0234 unsigned fVersion,
0235 const CastorGains& fObject,
0236 const CastorGainWidths& fError) {
0237 const std::string KIND = "HCAL Gains";
0238 const std::string TABLE = "HCAL_GAIN_PEDSTL_CALIBRATIONS";
0239
0240 dumpProlog(fOutput);
0241 dumpHeader(fOutput, fRun, TABLE, KIND);
0242
0243 std::vector<DetId> channels = fObject.getAllChannels();
0244 for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0245 DetId chId = *channel;
0246 const float* values = fObject.getValues(chId)->getValues();
0247 const float* errors = fError.getValues(chId)->getValues();
0248 if (!values) {
0249 std::cerr << "CastorDbXml::dumpObject-> Can not get data for channel "
0250 << CastorText2DetIdConverter(chId).toString() << std::endl;
0251 continue;
0252 }
0253 if (!errors) {
0254 std::cerr << "CastorDbXml::dumpObject-> Can not get errors for channel "
0255 << CastorText2DetIdConverter(chId).toString() << ". Use defaults" << std::endl;
0256 continue;
0257 }
0258 dumpDataset(fOutput, fVersion, "", "");
0259 dumpChannelId(fOutput, chId);
0260 dumpData(fOutput, values, errors);
0261 endDataset(fOutput);
0262 }
0263 dumpMapping(fOutput, fRun, KIND, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, channels);
0264
0265 dumpFooter(fOutput);
0266 return true;
0267 }