Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-11 04:36:44

0001 //
0002 // F.Ratnikov (UMd), Oct 28, 2005
0003 //
0004 #include <vector>
0005 #include <string>
0006 #include <cstdio>
0007 
0008 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
0009 #include "DataFormats/HcalDetId/interface/CastorElectronicsId.h"
0010 #include "CalibFormats/CastorObjects/interface/CastorText2DetIdConverter.h"
0011 
0012 #include "CondFormats/CastorObjects/interface/AllObjects.h"
0013 #include "CalibCalorimetry/CastorCalib/interface/CastorDbASCIIIO.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 
0016 namespace CastorDbASCIIIO {
0017   class DetIdLess {
0018   public:
0019     bool operator()(DetId fFirst, DetId fSecond) const {
0020       HcalGenericDetId first(fFirst);
0021       HcalGenericDetId second(fSecond);
0022       if (first.genericSubdet() != second.genericSubdet())
0023         return first.genericSubdet() < second.genericSubdet();
0024       if (first.isHcalDetId()) {
0025         HcalDetId f1(first);
0026         HcalDetId s1(second);
0027         return f1.zside() != s1.zside()       ? f1.zside() < s1.zside()
0028                : f1.iphi() != s1.iphi()       ? f1.iphi() < s1.iphi()
0029                : f1.ietaAbs() != s1.ietaAbs() ? f1.ietaAbs() < s1.ietaAbs()
0030                                               : f1.depth() < s1.depth();
0031       } else {
0032         return first.rawId() < second.rawId();
0033       }
0034     }
0035   };
0036   class CastorElectronicsIdLess {
0037   public:
0038     bool operator()(CastorElectronicsId first, CastorElectronicsId second) const {
0039       return first.readoutVMECrateId() != second.readoutVMECrateId()
0040                  ? first.readoutVMECrateId() < second.readoutVMECrateId()
0041              : first.htrSlot() != second.htrSlot()           ? first.htrSlot() < second.htrSlot()
0042              : first.htrTopBottom() != second.htrTopBottom() ? first.htrTopBottom() < second.htrTopBottom()
0043              : first.fiberIndex() != second.fiberIndex()     ? first.fiberIndex() < second.fiberIndex()
0044                                                              : first.fiberChanId() < second.fiberChanId();
0045     }
0046   };
0047 
0048   std::vector<std::string> splitString(const std::string& fLine) {
0049     std::vector<std::string> result;
0050     int start = 0;
0051     bool empty = true;
0052     for (unsigned i = 0; i <= fLine.size(); i++) {
0053       if (fLine[i] == ' ' || i == fLine.size()) {
0054         if (!empty) {
0055           std::string item(fLine, start, i - start);
0056           result.push_back(item);
0057           empty = true;
0058         }
0059         start = i + 1;
0060       } else {
0061         if (empty)
0062           empty = false;
0063       }
0064     }
0065     return result;
0066   }
0067 
0068   DetId getId(const std::vector<std::string>& items) {
0069     CastorText2DetIdConverter converter(items[3], items[0], items[1], items[2]);
0070     return converter.getId();
0071   }
0072 
0073   void dumpId(std::ostream& fOutput, DetId id) {
0074     CastorText2DetIdConverter converter(id);
0075     char buffer[1024];
0076     sprintf(buffer,
0077             "  %15s %15s %15s %15s",
0078             converter.getField1().c_str(),
0079             converter.getField2().c_str(),
0080             converter.getField3().c_str(),
0081             converter.getFlavor().c_str());
0082     fOutput << buffer;
0083   }
0084 
0085   template <class S, class T>
0086   bool getCastorObject(std::istream& fInput, T& fObject) {
0087     char buffer[1024];
0088     while (fInput.getline(buffer, 1024)) {
0089       if (buffer[0] == '#')
0090         continue;  //ignore comment
0091       std::vector<std::string> items = splitString(std::string(buffer));
0092       if (items.empty())
0093         continue;  // blank line
0094       if (items.size() < 8) {
0095         edm::LogWarning("Format Error") << "Bad line: " << buffer
0096                                         << "\n line must contain 8 items: eta, phi, depth, subdet, 4x values"
0097                                         << std::endl;
0098         continue;
0099       }
0100       DetId id = getId(items);
0101 
0102       //    if (fObject->exists(id) )
0103       //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
0104       //    else
0105       //      {
0106       S fCondObject(id, atof(items[4].c_str()), atof(items[5].c_str()), atof(items[6].c_str()), atof(items[7].c_str()));
0107       fObject.addValues(fCondObject);
0108       //      }
0109     }
0110 
0111     return true;
0112   }
0113 
0114   template <class T>
0115   bool dumpCastorObject(std::ostream& fOutput, const T& fObject) {
0116     char buffer[1024];
0117     sprintf(buffer,
0118             "# %15s %15s %15s %15s %8s %8s %8s %8s %10s\n",
0119             "eta",
0120             "phi",
0121             "dep",
0122             "det",
0123             "cap0",
0124             "cap1",
0125             "cap2",
0126             "cap3",
0127             "DetId");
0128     fOutput << buffer;
0129     std::vector<DetId> channels = fObject.getAllChannels();
0130     //std::sort (channels.begin(), channels.end(), DetIdLess ());
0131     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0132       const float* values = fObject.getValues(*channel)->getValues();
0133       if (values) {
0134         dumpId(fOutput, *channel);
0135         sprintf(
0136             buffer, " %8.5f %8.5f %8.5f %8.5f %10X\n", values[0], values[1], values[2], values[3], channel->rawId());
0137         fOutput << buffer;
0138       }
0139     }
0140     return true;
0141   }
0142 
0143   template <class S, class T>
0144   bool getCastorSingleFloatObject(std::istream& fInput, T& fObject) {
0145     char buffer[1024];
0146     while (fInput.getline(buffer, 1024)) {
0147       if (buffer[0] == '#')
0148         continue;  //ignore comment
0149       std::vector<std::string> items = splitString(std::string(buffer));
0150       if (items.empty())
0151         continue;  // blank line
0152       if (items.size() < 5) {
0153         edm::LogWarning("Format Error") << "Bad line: " << buffer
0154                                         << "\n line must contain 5 items: eta, phi, depth, subdet, value" << std::endl;
0155         continue;
0156       }
0157       DetId id = getId(items);
0158 
0159       //    if (fObject->exists(id) )
0160       //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
0161       //    else
0162       //      {
0163       S fCondObject(id, atof(items[4].c_str()));
0164       fObject.addValues(fCondObject);
0165       //      }
0166     }
0167     return true;
0168   }
0169 
0170   template <class T>
0171   bool dumpCastorSingleFloatObject(std::ostream& fOutput, const T& fObject) {
0172     char buffer[1024];
0173     sprintf(buffer, "# %15s %15s %15s %15s %8s %10s\n", "eta", "phi", "dep", "det", "value", "DetId");
0174     fOutput << buffer;
0175     std::vector<DetId> channels = fObject.getAllChannels();
0176     std::sort(channels.begin(), channels.end(), DetIdLess());
0177     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0178       const float value = fObject.getValues(*channel)->getValue();
0179       dumpId(fOutput, *channel);
0180       sprintf(buffer, " %8.5f %10X\n", value, channel->rawId());
0181       fOutput << buffer;
0182     }
0183     return true;
0184   }
0185 
0186   template <class S, class T>
0187   bool getCastorSingleIntObject(std::istream& fInput, T& fObject, S* fCondObject) {
0188     char buffer[1024];
0189     while (fInput.getline(buffer, 1024)) {
0190       if (buffer[0] == '#')
0191         continue;  //ignore comment
0192       std::vector<std::string> items = splitString(std::string(buffer));
0193       if (items.empty())
0194         continue;  // blank line
0195       if (items.size() < 5) {
0196         edm::LogWarning("Format Error") << "Bad line: " << buffer
0197                                         << "\n line must contain 5 items: eta, phi, depth, subdet, value" << std::endl;
0198         continue;
0199       }
0200       DetId id = getId(items);
0201 
0202       //    if (fObject->exists(id) )
0203       //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
0204       //    else
0205       //      {
0206       fCondObject = new S(id, atoi(items[4].c_str()));
0207       fObject.addValues(*fCondObject);
0208       delete fCondObject;
0209       //      }
0210     }
0211     return true;
0212   }
0213 
0214   template <class T>
0215   bool dumpCastorSingleIntObject(std::ostream& fOutput, const T& fObject) {
0216     char buffer[1024];
0217     sprintf(buffer, "# %15s %15s %15s %15s %8s %10s\n", "eta", "phi", "dep", "det", "value", "DetId");
0218     fOutput << buffer;
0219     std::vector<DetId> channels = fObject.getAllChannels();
0220     std::sort(channels.begin(), channels.end(), DetIdLess());
0221     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0222       const int value = fObject.getValues(*channel)->getValue();
0223       dumpId(fOutput, *channel);
0224       sprintf(buffer, " %15d %10X\n", value, channel->rawId());
0225       fOutput << buffer;
0226     }
0227     return true;
0228   }
0229 
0230   bool getObject(std::istream& fInput, CastorGains& fObject) { return getCastorObject<CastorGain>(fInput, fObject); }
0231   bool dumpObject(std::ostream& fOutput, const CastorGains& fObject) { return dumpCastorObject(fOutput, fObject); }
0232   bool getObject(std::istream& fInput, CastorGainWidths& fObject) {
0233     return getCastorObject<CastorGainWidth>(fInput, fObject);
0234   }
0235   bool dumpObject(std::ostream& fOutput, const CastorGainWidths& fObject) { return dumpCastorObject(fOutput, fObject); }
0236 
0237   bool getObject(std::istream& fInput, CastorSaturationCorrs& fObject) {
0238     return getCastorSingleFloatObject<CastorSaturationCorr>(fInput, fObject);
0239   }
0240   bool dumpObject(std::ostream& fOutput, const CastorSaturationCorrs& fObject) {
0241     return dumpCastorSingleFloatObject(fOutput, fObject);
0242   }
0243 
0244   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
0245   bool getObject(std::istream& fInput, CastorPedestals& fObject) {
0246     char buffer[1024];
0247 
0248     while (fInput.getline(buffer, 1024)) {
0249       std::vector<std::string> items = splitString(std::string(buffer));
0250       if (items.empty())
0251         continue;  // blank line
0252       else {
0253         if (items[0] == "#U") {
0254           if (items[1] == (std::string) "ADC")
0255             fObject.setUnitADC(true);
0256           else if (items[1] == (std::string) "fC")
0257             fObject.setUnitADC(false);
0258           else {
0259             edm::LogWarning("Pedestal Unit Error") << "Unrecognized unit for pedestals. Assuming fC." << std::endl;
0260             fObject.setUnitADC(false);
0261           }
0262           break;
0263         } else {
0264           edm::LogWarning("Pedestal Unit Missing")
0265               << "The unit for the pedestals is missing in the txt file." << std::endl;
0266           return false;
0267         }
0268       }
0269     }
0270     while (fInput.getline(buffer, 1024)) {
0271       if (buffer[0] == '#')
0272         continue;
0273       std::vector<std::string> items = splitString(std::string(buffer));
0274       if (items.empty())
0275         continue;  // blank line
0276       if (items.size() < 8) {
0277         edm::LogWarning("Format Error")
0278             << "Bad line: " << buffer << "\n line must contain 8 items: eta, phi, depth, subdet, 4x values"
0279             << " or 12 items: eta, phi, depth, subdet, 4x values for mean, 4x values for width" << std::endl;
0280         continue;
0281       }
0282       DetId id = getId(items);
0283 
0284       //    if (fObject.exists(id) )
0285       //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
0286       //    else
0287       //      {
0288 
0289       if (items.size() < 12)  // old format without widths
0290       {
0291         CastorPedestal fCondObject(id,
0292                                    atof(items[4].c_str()),
0293                                    atof(items[5].c_str()),
0294                                    atof(items[6].c_str()),
0295                                    atof(items[7].c_str()),
0296                                    0.,
0297                                    0.,
0298                                    0.,
0299                                    0.);
0300         fObject.addValues(fCondObject);
0301       } else  // new format with widths
0302       {
0303         CastorPedestal fCondObject(id,
0304                                    atof(items[4].c_str()),
0305                                    atof(items[5].c_str()),
0306                                    atof(items[6].c_str()),
0307                                    atof(items[7].c_str()),
0308                                    atof(items[8].c_str()),
0309                                    atof(items[9].c_str()),
0310                                    atof(items[10].c_str()),
0311                                    atof(items[11].c_str()));
0312         fObject.addValues(fCondObject);
0313       }
0314 
0315       //      }
0316     }
0317     return true;
0318   }
0319 
0320   bool dumpObject(std::ostream& fOutput, const CastorPedestals& fObject) {
0321     char buffer[1024];
0322     if (fObject.isADC())
0323       sprintf(buffer, "#U ADC  << this is the unit \n");
0324     else
0325       sprintf(buffer, "#U fC  << this is the unit \n");
0326     fOutput << buffer;
0327 
0328     sprintf(buffer,
0329             "# %15s %15s %15s %15s %8s %8s %8s %8s %8s %8s %8s %8s %10s\n",
0330             "eta",
0331             "phi",
0332             "dep",
0333             "det",
0334             "cap0",
0335             "cap1",
0336             "cap2",
0337             "cap3",
0338             "widthcap0",
0339             "widthcap1",
0340             "widthcap2",
0341             "widthcap3",
0342             "DetId");
0343     fOutput << buffer;
0344 
0345     std::vector<DetId> channels = fObject.getAllChannels();
0346     std::sort(channels.begin(), channels.end(), DetIdLess());
0347     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0348       const float* values = fObject.getValues(*channel)->getValues();
0349       if (values) {
0350         dumpId(fOutput, *channel);
0351         sprintf(buffer,
0352                 " %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %10X\n",
0353                 values[0],
0354                 values[1],
0355                 values[2],
0356                 values[3],
0357                 values[4],
0358                 values[5],
0359                 values[6],
0360                 values[7],
0361                 channel->rawId());
0362         fOutput << buffer;
0363       }
0364     }
0365     return true;
0366   }
0367 
0368   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
0369   bool getObject(std::istream& fInput, CastorChannelQuality& fObject) {
0370     char buffer[1024];
0371     while (fInput.getline(buffer, 1024)) {
0372       if (buffer[0] == '#')
0373         continue;  //ignore comment
0374       std::vector<std::string> items = splitString(std::string(buffer));
0375       if (items.empty())
0376         continue;  // blank line
0377       if (items.size() < 5) {
0378         edm::LogWarning("Format Error") << "Bad line: " << buffer
0379                                         << "\n line must contain 5 items: eta, phi, depth, subdet, GOOD/BAD/HOT/DEAD"
0380                                         << std::endl;
0381         continue;
0382       }
0383       DetId id = getId(items);
0384 
0385       if (fObject.exists(id)) {
0386         edm::LogWarning("Redefining Channel")
0387             << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
0388         continue;
0389       }
0390       //    else
0391       //      {
0392       uint32_t mystatus;
0393       std::unique_ptr<CastorChannelStatus> fCondObject;
0394       if (items[4].substr(0, 2) == "0x") {
0395         sscanf(items[4].c_str(), "%X", &mystatus);
0396         fCondObject = std::make_unique<CastorChannelStatus>(id, mystatus);
0397       } else if (isalpha(items[4].c_str()[0])) {
0398         fCondObject = std::make_unique<CastorChannelStatus>(id, items[4]);
0399       } else {
0400         sscanf(items[4].c_str(), "%u", &mystatus);
0401         fCondObject = std::make_unique<CastorChannelStatus>(id, mystatus);
0402       }
0403       fObject.addValues(*fCondObject);
0404       //      }
0405     }
0406     return true;
0407   }
0408 
0409   bool dumpObject(std::ostream& fOutput, const CastorChannelQuality& fObject) {
0410     char buffer[1024];
0411     sprintf(buffer, "# %15s %15s %15s %15s %15s %10s\n", "eta", "phi", "dep", "det", "value", "DetId");
0412     fOutput << buffer;
0413     std::vector<DetId> channels = fObject.getAllChannels();
0414     std::sort(channels.begin(), channels.end(), DetIdLess());
0415     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0416       const int value = fObject.getValues(*channel)->getValue();
0417       dumpId(fOutput, *channel);
0418       sprintf(buffer, " %15X %10X\n", value, channel->rawId());
0419       fOutput << buffer;
0420     }
0421     return true;
0422   }
0423 
0424   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
0425   bool getObject(std::istream& fInput, CastorPedestalWidths& fObject) {
0426     char buffer[1024];
0427     int linecounter = 0;
0428 
0429     while (fInput.getline(buffer, 1024)) {
0430       linecounter++;
0431       std::vector<std::string> items = splitString(std::string(buffer));
0432       if (items.empty())
0433         continue;  // blank line
0434       else {
0435         if (items[0] == (std::string) "#U") {
0436           if (items[1] == (std::string) "ADC")
0437             fObject.setUnitADC(true);
0438           else if (items[1] == (std::string) "fC")
0439             fObject.setUnitADC(false);
0440           else {
0441             edm::LogWarning("Pedestal Width Unit Error")
0442                 << "Unrecognized unit for pedestal widths. Assuming fC." << std::endl;
0443             fObject.setUnitADC(false);
0444           }
0445           break;
0446         } else {
0447           edm::LogWarning("Pedestal Width Unit Missing")
0448               << "The unit for the pedestal widths is missing in the txt file." << std::endl;
0449           return false;
0450         }
0451       }
0452     }
0453 
0454     while (fInput.getline(buffer, 1024)) {
0455       linecounter++;
0456       if (buffer[0] == '#')
0457         continue;  //ignore comment
0458       std::vector<std::string> items = splitString(std::string(buffer));
0459       if (items.empty())
0460         continue;  // blank line
0461       if (items.size() < 14) {
0462         edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line number: " << linecounter
0463                                         << "\n line must contain 14 items: eta, phi, depth, subdet, 10x correlations"
0464                                         << " or 20 items: eta, phi, depth, subdet, 16x correlations" << std::endl;
0465         continue;
0466       }
0467       DetId id = getId(items);
0468 
0469       //    if (fObject.exists(id) )
0470       //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
0471       //    else
0472       //      {
0473 
0474       if (items.size() < 20)  //old format
0475       {
0476         CastorPedestalWidth values(id);
0477         values.setSigma(0, 0, atof(items[4].c_str()));
0478         values.setSigma(1, 0, atof(items[5].c_str()));
0479         values.setSigma(1, 1, atof(items[6].c_str()));
0480         values.setSigma(2, 0, atof(items[7].c_str()));
0481         values.setSigma(2, 1, atof(items[8].c_str()));
0482         values.setSigma(2, 2, atof(items[9].c_str()));
0483         values.setSigma(3, 0, atof(items[10].c_str()));
0484         values.setSigma(3, 1, atof(items[11].c_str()));
0485         values.setSigma(3, 2, atof(items[12].c_str()));
0486         values.setSigma(3, 3, atof(items[13].c_str()));
0487         values.setSigma(0, 1, 0.);
0488         values.setSigma(0, 2, 0.);
0489         values.setSigma(0, 3, 0.);
0490         values.setSigma(1, 2, 0.);
0491         values.setSigma(1, 3, 0.);
0492         values.setSigma(2, 3, 0.);
0493         fObject.addValues(values);
0494       } else  // new format
0495       {
0496         CastorPedestalWidth values(id);
0497         values.setSigma(0, 0, atof(items[4].c_str()));
0498         values.setSigma(0, 1, atof(items[5].c_str()));
0499         values.setSigma(0, 2, atof(items[6].c_str()));
0500         values.setSigma(0, 3, atof(items[7].c_str()));
0501         values.setSigma(1, 0, atof(items[8].c_str()));
0502         values.setSigma(1, 1, atof(items[9].c_str()));
0503         values.setSigma(1, 2, atof(items[10].c_str()));
0504         values.setSigma(1, 3, atof(items[11].c_str()));
0505         values.setSigma(2, 0, atof(items[12].c_str()));
0506         values.setSigma(2, 1, atof(items[13].c_str()));
0507         values.setSigma(2, 2, atof(items[14].c_str()));
0508         values.setSigma(2, 3, atof(items[15].c_str()));
0509         values.setSigma(3, 0, atof(items[16].c_str()));
0510         values.setSigma(3, 1, atof(items[17].c_str()));
0511         values.setSigma(3, 2, atof(items[18].c_str()));
0512         values.setSigma(3, 3, atof(items[19].c_str()));
0513         fObject.addValues(values);
0514       }
0515 
0516       //      }
0517     }
0518     return true;
0519   }
0520 
0521   bool dumpObject(std::ostream& fOutput, const CastorPedestalWidths& fObject) {
0522     char buffer[1024];
0523     if (fObject.isADC())
0524       sprintf(buffer, "#U ADC  << this is the unit \n");
0525     else
0526       sprintf(buffer, "#U fC  << this is the unit \n");
0527     fOutput << buffer;
0528 
0529     sprintf(buffer,
0530             "# %15s %15s %15s %15s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %10s\n",
0531             "eta",
0532             "phi",
0533             "dep",
0534             "det",
0535             "cov_0_0",
0536             "cov_0_1",
0537             "cov_0_2",
0538             "cov_0_3",
0539             "cov_1_0",
0540             "cov_1_1",
0541             "cov_1_2",
0542             "cov_1_3",
0543             "cov_2_0",
0544             "cov_2_1",
0545             "cov_2_2",
0546             "cov_2_3",
0547             "cov_3_0",
0548             "cov_3_1",
0549             "cov_3_2",
0550             "cov_3_3",
0551             "DetId");
0552     fOutput << buffer;
0553     std::vector<DetId> channels = fObject.getAllChannels();
0554     std::sort(channels.begin(), channels.end(), DetIdLess());
0555     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0556       const CastorPedestalWidth* item = fObject.getValues(*channel);
0557       if (item) {
0558         dumpId(fOutput, *channel);
0559         sprintf(
0560             buffer,
0561             " %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %10X\n",
0562             item->getSigma(0, 0),
0563             item->getSigma(0, 1),
0564             item->getSigma(0, 2),
0565             item->getSigma(0, 3),
0566             item->getSigma(1, 0),
0567             item->getSigma(1, 1),
0568             item->getSigma(1, 2),
0569             item->getSigma(1, 3),
0570             item->getSigma(2, 0),
0571             item->getSigma(2, 1),
0572             item->getSigma(2, 2),
0573             item->getSigma(2, 3),
0574             item->getSigma(3, 0),
0575             item->getSigma(3, 1),
0576             item->getSigma(3, 2),
0577             item->getSigma(3, 3),
0578             channel->rawId());
0579         fOutput << buffer;
0580       }
0581     }
0582     return true;
0583   }
0584 
0585   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
0586   bool getObject(std::istream& fInput, CastorQIEData& fObject) {
0587     char buffer[1024];
0588     while (fInput.getline(buffer, 1024)) {
0589       if (buffer[0] == '#')
0590         continue;  //ignore comment
0591       std::vector<std::string> items = splitString(std::string(buffer));
0592       if (items.empty())
0593         continue;
0594       if (items[0] == "SHAPE") {  // basic shape
0595         if (items.size() < 33) {
0596           edm::LogWarning("Format Error")
0597               << "Bad line: " << buffer << "\n line must contain 33 items: SHAPE  32 x low QIE edges for first 32 bins"
0598               << std::endl;
0599           continue;
0600         }
0601         //float lowEdges [32];
0602         //int i = 32;
0603         //while (--i >= 0) lowEdges [i] = atof (items [i+1].c_str ());
0604         //      fObject.setShape (lowEdges);
0605       } else {  // QIE parameters
0606         if (items.size() < 36) {
0607           edm::LogWarning("Format Error") << "Bad line: " << buffer
0608                                           << "\n line must contain 36 items: eta, phi, depth, subdet, 4 capId x 4 "
0609                                              "Ranges x offsets, 4 capId x 4 Ranges x slopes"
0610                                           << std::endl;
0611           continue;
0612         }
0613         DetId id = getId(items);
0614         fObject.sort();
0615         //      try {
0616         //      fObject.getCoder (id);
0617         //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
0618         //      }
0619         //      catch (cms::Exception& e) {
0620         CastorQIECoder coder(id.rawId());
0621         int index = 4;
0622         for (unsigned capid = 0; capid < 4; capid++) {
0623           for (unsigned range = 0; range < 4; range++) {
0624             coder.setOffset(capid, range, atof(items[index++].c_str()));
0625           }
0626         }
0627         for (unsigned capid = 0; capid < 4; capid++) {
0628           for (unsigned range = 0; range < 4; range++) {
0629             coder.setSlope(capid, range, atof(items[index++].c_str()));
0630           }
0631         }
0632         fObject.addCoder(coder);
0633         //      }
0634       }
0635     }
0636     fObject.sort();
0637     return true;
0638   }
0639 
0640   bool dumpObject(std::ostream& fOutput, const CastorQIEData& fObject) {
0641     char buffer[1024];
0642     fOutput << "# QIE basic shape: SHAPE 32 x low edge values for first 32 channels" << std::endl;
0643     sprintf(buffer, "SHAPE ");
0644     fOutput << buffer;
0645     for (unsigned bin = 0; bin < 32; bin++) {
0646       sprintf(buffer, " %8.5f", fObject.getShape().lowEdge(bin));
0647       fOutput << buffer;
0648     }
0649     fOutput << std::endl;
0650 
0651     fOutput << "# QIE data" << std::endl;
0652     sprintf(buffer,
0653             "# %15s %15s %15s %15s %36s %36s %36s %36s %36s %36s %36s %36s\n",
0654             "eta",
0655             "phi",
0656             "dep",
0657             "det",
0658             "4 x offsets cap0",
0659             "4 x offsets cap1",
0660             "4 x offsets cap2",
0661             "4 x offsets cap3",
0662             "4 x slopes cap0",
0663             "4 x slopes cap1",
0664             "4 x slopes cap2",
0665             "4 x slopes cap3");
0666     fOutput << buffer;
0667     std::vector<DetId> channels = fObject.getAllChannels();
0668     std::sort(channels.begin(), channels.end(), DetIdLess());
0669     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0670       const CastorQIECoder* coder = fObject.getCoder(*channel);
0671       dumpId(fOutput, *channel);
0672       for (unsigned capid = 0; capid < 4; capid++) {
0673         for (unsigned range = 0; range < 4; range++) {
0674           sprintf(buffer, " %8.5f", coder->offset(capid, range));
0675           fOutput << buffer;
0676         }
0677       }
0678       for (unsigned capid = 0; capid < 4; capid++) {
0679         for (unsigned range = 0; range < 4; range++) {
0680           sprintf(buffer, " %8.5f", coder->slope(capid, range));
0681           fOutput << buffer;
0682         }
0683       }
0684       fOutput << std::endl;
0685     }
0686     return true;
0687   }
0688 
0689   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
0690   bool getObject(std::istream& fInput, CastorCalibrationQIEData& fObject) {
0691     char buffer[1024];
0692     while (fInput.getline(buffer, 1024)) {
0693       if (buffer[0] == '#')
0694         continue;  //ignore comment
0695       std::vector<std::string> items = splitString(std::string(buffer));
0696       if (items.size() < 36) {
0697         edm::LogWarning("Format Error") << "Bad line: " << buffer
0698                                         << "\n line must contain 36 items: eta, phi, depth, subdet, 32 bin values"
0699                                         << std::endl;
0700         continue;
0701       }
0702       DetId id = getId(items);
0703       fObject.sort();
0704       //    try {
0705       //    fObject.getCoder (id);
0706       //    edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
0707       //    }
0708       //    catch (cms::Exception& e) {
0709       CastorCalibrationQIECoder coder(id.rawId());
0710       int index = 4;
0711       float values[32];
0712       for (unsigned bin = 0; bin < 32; bin++) {
0713         values[bin] = atof(items[index++].c_str());
0714       }
0715       coder.setMinCharges(values);
0716       fObject.addCoder(coder);
0717       //    }
0718     }
0719     fObject.sort();
0720     return true;
0721   }
0722 
0723   bool dumpObject(std::ostream& fOutput, const CastorCalibrationQIEData& fObject) {
0724     char buffer[1024];
0725     fOutput << "# QIE data in calibration mode" << std::endl;
0726     sprintf(buffer, "# %15s %15s %15s %15s %288s\n", "eta", "phi", "dep", "det", "32 x charges");
0727     fOutput << buffer;
0728     std::vector<DetId> channels = fObject.getAllChannels();
0729     std::sort(channels.begin(), channels.end(), DetIdLess());
0730     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0731       const CastorCalibrationQIECoder* coder = fObject.getCoder(*channel);
0732       if (coder) {
0733         dumpId(fOutput, *channel);
0734         const float* lowEdge = coder->minCharges();
0735         for (unsigned bin = 0; bin < 32; bin++) {
0736           sprintf(buffer, " %8.5f", lowEdge[bin]);
0737           fOutput << buffer;
0738         }
0739         fOutput << std::endl;
0740       }
0741     }
0742     return true;
0743   }
0744 
0745   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
0746   bool getObject(std::istream& fInput, CastorElectronicsMap& fObject) {
0747     char buffer[1024];
0748     while (fInput.getline(buffer, 1024)) {
0749       if (buffer[0] == '#')
0750         continue;  //ignore comment
0751       std::vector<std::string> items = splitString(std::string(buffer));
0752       if (items.size() < 12) {
0753         if (items.empty())
0754           continue;  // no warning here
0755         if (items.size() < 9) {
0756           edm::LogError("MapFormat") << "CastorElectronicsMap-> line too short: " << buffer;
0757           continue;
0758         }
0759         if (items[8] == "NA" || items[8] == "NT") {
0760           while (items.size() < 12)
0761             items.push_back("");  // don't worry here
0762         } else if (items[8] == "HT") {
0763           if (items.size() == 11)
0764             items.push_back("");
0765           else {
0766             edm::LogError("MapFormat") << "CastorElectronicsMap-> Bad line: " << buffer
0767                                        << "\n HT line must contain at least 11 items: i  cr sl tb dcc spigot fiber "
0768                                           "fiberchan subdet=HT ieta iphi";
0769             continue;
0770           }
0771         } else {
0772           edm::LogError("MapFormat")
0773               << "CastorElectronicsMap-> Bad line: " << buffer
0774               << "\n line must contain 12 items: i  cr sl tb dcc spigot fiber fiberchan subdet ieta iphi depth";
0775           continue;
0776         }
0777       }
0778       //    std::cout << "CastorElectronicsMap-> processing line: " << buffer << std::endl;
0779       int crate = atoi(items[1].c_str());
0780       int slot = atoi(items[2].c_str());
0781       int top = 1;
0782       if (items[3] == "b")
0783         top = 0;
0784       int dcc = atoi(items[4].c_str());
0785       int spigot = atoi(items[5].c_str());
0786       CastorElectronicsId elId;
0787       if (items[8] == "HT" || items[8] == "NT") {
0788         int slb = atoi(items[6].c_str());
0789         int slbCh = atoi(items[7].c_str());
0790         elId = CastorElectronicsId(slbCh, slb, spigot, dcc, crate, slot, top);
0791       } else {
0792         int fiber = atoi(items[6].c_str());
0793         int fiberCh = atoi(items[7].c_str());
0794 
0795         elId = CastorElectronicsId(fiberCh, fiber, spigot, dcc);
0796         elId.setHTR(crate, slot, top);
0797       }
0798 
0799       // first, handle undefined cases
0800       if (items[8] == "NA") {  // undefined channel
0801         fObject.mapEId2chId(elId, DetId(HcalDetId::Undefined));
0802       } else if (items[8] == "NT") {  // undefined trigger channel
0803         fObject.mapEId2tId(elId, DetId(HcalTrigTowerDetId::Undefined));
0804       } else {
0805         CastorText2DetIdConverter converter(items[8], items[9], items[10], items[11]);
0806         if (converter.isHcalCastorDetId()) {
0807           fObject.mapEId2chId(elId, converter.getId());
0808         } else {
0809           edm::LogWarning("Format Error") << "CastorElectronicsMap-> Unknown subdetector: " << items[8] << '/'
0810                                           << items[9] << '/' << items[10] << '/' << items[11] << std::endl;
0811         }
0812       }
0813     }
0814     fObject.sort();
0815     return true;
0816   }
0817 
0818   bool dumpObject(std::ostream& fOutput, const CastorElectronicsMap& fObject) {
0819     std::vector<CastorElectronicsId> eids = fObject.allElectronicsId();
0820     char buf[1024];
0821     // changes by Jared, 6.03.09/(included 25.03.09)
0822     //  sprintf (buf, "#%10s %6s %6s %6s %6s %6s %6s %6s %15s %15s %15s %15s",
0823     sprintf(buf,
0824             "# %7s %3s %3s %3s %4s %7s %10s %14s %7s %5s %5s %6s",
0825             "i",
0826             "cr",
0827             "sl",
0828             "tb",
0829             "dcc",
0830             "spigot",
0831             "fiber/slb",
0832             "fibcha/slbcha",
0833             "subdet",
0834             "ieta",
0835             "iphi",
0836             "depth");
0837     fOutput << buf << std::endl;
0838 
0839     for (unsigned i = 0; i < eids.size(); i++) {
0840       CastorElectronicsId eid = eids[i];
0841       if (eid.isTriggerChainId()) {
0842         DetId trigger = fObject.lookupTrigger(eid);
0843         if (trigger.rawId()) {
0844           CastorText2DetIdConverter converter(trigger);
0845           // changes by Jared, 6.03.09/(included 25.03.09)
0846           //    sprintf (buf, " %10X %6d %6d %6c %6d %6d %6d %6d %15s %15s %15s %15s",
0847           sprintf(
0848               buf,
0849               " %7X %3d %3d %3c %4d %7d %10d %14d %7s %5s %5s %6s",
0850               //         i,
0851               converter.getId().rawId(),
0852               // changes by Jared, 6.03.09/(included 25.03.09)
0853               //         eid.readoutVMECrateId(), eid.htrSlot(), eid.htrTopBottom()>0?'t':'b', eid.dccid(), eid.spigot(), eid.fiberIndex(), eid.fiberChanId(),
0854               eid.readoutVMECrateId(),
0855               eid.htrSlot(),
0856               eid.htrTopBottom() > 0 ? 't' : 'b',
0857               eid.dccid(),
0858               eid.spigot(),
0859               eid.slbSiteNumber(),
0860               eid.slbChannelIndex(),
0861               converter.getFlavor().c_str(),
0862               converter.getField1().c_str(),
0863               converter.getField2().c_str(),
0864               converter.getField3().c_str());
0865           fOutput << buf << std::endl;
0866         }
0867       } else {
0868         DetId channel = fObject.lookup(eid);
0869         if (channel.rawId()) {
0870           CastorText2DetIdConverter converter(channel);
0871           // changes by Jared, 6.03.09/(included 25.03.09)
0872           //    sprintf (buf, " %10X %6d %6d %6c %6d %6d %6d %6d %15s %15s %15s %15s",
0873           sprintf(buf,
0874                   " %7X %3d %3d %3c %4d %7d %10d %14d %7s %5s %5s %6s",
0875                   //         i,
0876                   converter.getId().rawId(),
0877                   eid.readoutVMECrateId(),
0878                   eid.htrSlot(),
0879                   eid.htrTopBottom() > 0 ? 't' : 'b',
0880                   eid.dccid(),
0881                   eid.spigot(),
0882                   eid.fiberIndex(),
0883                   eid.fiberChanId(),
0884                   converter.getFlavor().c_str(),
0885                   converter.getField1().c_str(),
0886                   converter.getField2().c_str(),
0887                   converter.getField3().c_str());
0888           fOutput << buf << std::endl;
0889         }
0890       }
0891     }
0892     return true;
0893   }
0894 
0895   bool getObject(std::istream& fInput, CastorRecoParams& fObject) {
0896     char buffer[1024];
0897     while (fInput.getline(buffer, 1024)) {
0898       if (buffer[0] == '#')
0899         continue;  //ignore comment
0900       std::vector<std::string> items = splitString(std::string(buffer));
0901       if (items.empty())
0902         continue;  // blank line
0903       if (items.size() < 6) {
0904         edm::LogWarning("Format Error")
0905             << "Bad line: " << buffer
0906             << "\n line must contain 6 items: eta, phi, depth, subdet, firstSample, samplesToAdd" << std::endl;
0907         continue;
0908       }
0909       DetId id = getId(items);
0910 
0911       CastorRecoParam fCondObject(id, atoi(items[4].c_str()), atoi(items[5].c_str()));
0912       fObject.addValues(fCondObject);
0913     }
0914     return true;
0915   }
0916 
0917   bool dumpObject(std::ostream& fOutput, const CastorRecoParams& fObject) {
0918     char buffer[1024];
0919     sprintf(buffer,
0920             "# %15s %15s %15s %15s %18s %15s %10s\n",
0921             "eta",
0922             "phi",
0923             "dep",
0924             "det",
0925             "firstSample",
0926             "samplesToAdd",
0927             "DetId");
0928     fOutput << buffer;
0929     std::vector<DetId> channels = fObject.getAllChannels();
0930     std::sort(channels.begin(), channels.end(), DetIdLess());
0931     for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
0932       dumpId(fOutput, *channel);
0933       sprintf(buffer,
0934               " %15d %15d %16X\n",
0935               fObject.getValues(*channel)->firstSample(),
0936               fObject.getValues(*channel)->samplesToAdd(),
0937               channel->rawId());
0938       fOutput << buffer;
0939     }
0940     return true;
0941   }
0942 
0943 }  // namespace CastorDbASCIIIO