File indexing completed on 2024-04-06 11:58:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <memory>
0021
0022
0023 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0024 #include "FWCore/Framework/interface/EventSetup.h"
0025 #include "FWCore/Framework/interface/MakerMacros.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0027
0028 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
0029
0030 #include "DataFormats/DetId/interface/DetId.h"
0031 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
0032 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0033 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
0034 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
0035 #include "DataFormats/HcalDetId/interface/HcalElectronicsId.h"
0036 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
0037 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
0038
0039 #include "CalibFormats/HcalObjects/interface/HcalTPGRecord.h"
0040 #include "CalibFormats/HcalObjects/interface/HcalTPGCoder.h"
0041 #include "CalibFormats/CaloTPG/interface/CaloTPGRecord.h"
0042 #include "CalibFormats/CaloTPG/interface/CaloTPGTranscoder.h"
0043
0044 using namespace edm;
0045 using namespace std;
0046 #include <iostream>
0047 #include <fstream>
0048 #include "md5.h"
0049
0050
0051
0052
0053
0054 class HcalLuttoDB : public edm::one::EDAnalyzer<> {
0055 public:
0056 explicit HcalLuttoDB(const edm::ParameterSet&);
0057 ~HcalLuttoDB() override;
0058
0059 void beginJob() override;
0060 void analyze(const edm::Event&, const edm::EventSetup&) override;
0061 void endJob() override;
0062
0063 private:
0064 void writeoutlut1(HcalDetId id, HcalElectronicsId eid, const std::vector<unsigned short>& lut, std::ostream& os);
0065 std::vector<unsigned char> extractOutputLut(const CaloTPGTranscoder& coder, HcalTrigTowerDetId chan);
0066 void writeoutlut2(HcalTrigTowerDetId id,
0067 HcalElectronicsId eid,
0068 const std::vector<unsigned char>& lut,
0069 std::ostream& os);
0070 const std::string creationtag_;
0071 const std::string targetfirmware_;
0072 const bool filePerCrate_;
0073 const std::string fileformat_;
0074 std::string creationstamp_;
0075 std::ostream* openPerCrate(int crate);
0076 std::ostream* openPerLut1(HcalElectronicsId eid);
0077 std::ostream* openPerLut2(HcalElectronicsId eid);
0078 std::ostream* openChecksums();
0079 edm::ESGetToken<HcalDbService, HcalDbRecord> tokdb_;
0080 edm::ESGetToken<HcalTPGCoder, HcalTPGRecord> tokhcalCode_;
0081 edm::ESGetToken<CaloTPGTranscoder, CaloTPGRecord> tokcaloCode_;
0082 std::ostream* oc_;
0083 };
0084
0085
0086 static const int formatRevision_ = 1;
0087
0088
0089
0090 HcalLuttoDB::HcalLuttoDB(const edm::ParameterSet& iConfig)
0091 : creationtag_(iConfig.getParameter<std::string>("creationtag")),
0092 targetfirmware_(iConfig.getParameter<std::string>("targetfirmware")),
0093 filePerCrate_(iConfig.getUntrackedParameter<bool>("filePerCrate", true)),
0094 fileformat_(iConfig.getParameter<std::string>("filePrefix")) {
0095 tokdb_ = esConsumes<HcalDbService, HcalDbRecord>();
0096 tokhcalCode_ = esConsumes<HcalTPGCoder, HcalTPGRecord>();
0097 tokcaloCode_ = esConsumes<CaloTPGTranscoder, CaloTPGRecord>();
0098 }
0099
0100 HcalLuttoDB::~HcalLuttoDB() {}
0101
0102
0103
0104
0105
0106 std::ostream* HcalLuttoDB::openChecksums() {
0107 char fname[1024];
0108 snprintf(fname, 1024, "%s_checksums.xml", fileformat_.c_str());
0109 std::ostream* os = new std::ofstream(fname);
0110 (*os) << "<?xml version=\"1.0\"?>\n<CFGBrick>\n";
0111 return os;
0112 }
0113
0114 std::ostream* HcalLuttoDB::openPerCrate(int crate) {
0115 char fname[1024];
0116 snprintf(fname, 1024, "%s_%d.xml", fileformat_.c_str(), crate);
0117 std::ostream* os = new std::ofstream(fname);
0118 (*os) << "<?xml version=\"1.0\"?>\n<CFGBrickSet>\n";
0119 return os;
0120 }
0121
0122 std::ostream* HcalLuttoDB::openPerLut1(HcalElectronicsId eid) {
0123 char fname[1024];
0124 snprintf(fname,
0125 1024,
0126 "%s_%d_%d%c_%d_%d_1.xml",
0127 fileformat_.c_str(),
0128 eid.readoutVMECrateId(),
0129 eid.htrSlot(),
0130 ((eid.htrTopBottom()) ? ('t') : ('b')),
0131 eid.fiberIndex(),
0132 eid.fiberChanId());
0133 std::ostream* os = new std::ofstream(fname);
0134 (*os) << "<?xml version=\"1.0\"?>\n";
0135 return os;
0136 }
0137
0138 std::ostream* HcalLuttoDB::openPerLut2(HcalElectronicsId eid) {
0139 char fname[1024];
0140 snprintf(fname,
0141 1024,
0142 "%s_%d_%d%c_%d_%d_2.xml",
0143 fileformat_.c_str(),
0144 eid.readoutVMECrateId(),
0145 eid.htrSlot(),
0146 ((eid.htrTopBottom()) ? ('t') : ('b')),
0147 eid.slbSiteNumber(),
0148 eid.slbChannelIndex());
0149 std::ostream* os = new std::ofstream(fname);
0150 (*os) << "<?xml version=\"1.0\"?>\n";
0151 return os;
0152 }
0153
0154 void HcalLuttoDB::writeoutlut1(HcalDetId id,
0155 HcalElectronicsId eid,
0156 const std::vector<unsigned short>& lut,
0157 std::ostream& os) {
0158 os << "<CFGBrick> " << std::endl;
0159 os << " <Parameter name='IETA' type='int'>" << id.ieta() << "</Parameter>" << std::endl;
0160 os << " <Parameter name='IPHI' type='int'>" << id.iphi() << "</Parameter>" << std::endl;
0161 os << " <Parameter name='DEPTH' type='int'>" << id.depth() << "</Parameter>" << std::endl;
0162 os << " <Parameter name='CRATE' type='int'>" << eid.readoutVMECrateId() << "</Parameter>" << std::endl;
0163 os << " <Parameter name='SLOT' type='int'>" << eid.htrSlot() << "</Parameter>" << std::endl;
0164 os << " <Parameter name='TOPBOTTOM' type='int'>" << eid.htrTopBottom() << "</Parameter>" << std::endl;
0165 os << " <Parameter name='FIBER' type='int'>" << eid.fiberIndex() << "</Parameter>" << std::endl;
0166 os << " <Parameter name='FIBERCHAN' type='int'>" << eid.fiberChanId() << "</Parameter>" << std::endl;
0167 os << " <Parameter name='LUT_TYPE' type='int'>1</Parameter>" << std::endl;
0168 os << " <Parameter name='CREATIONTAG' type='string'>" << creationtag_ << "</Parameter>" << std::endl;
0169 os << " <Parameter name='CREATIONSTAMP' type='string'>" << creationstamp_ << "</Parameter>" << std::endl;
0170 os << " <Parameter name='FORMATREVISION' type='string'>" << formatRevision_ << "</Parameter>" << std::endl;
0171 os << " <Parameter name='TARGETFIRMWARE' type='string'>" << targetfirmware_ << "</Parameter>" << std::endl;
0172 int generalizedIndex = id.ietaAbs() + 1000 * id.depth() + 10000 * id.iphi() + ((id.ieta() < 0) ? (0) : (100)) +
0173 ((id.subdet() == HcalForward && id.ietaAbs() == 29) ? (4 * 10000) : (0));
0174
0175 os << " <Parameter name='GENERALIZEDINDEX' type='int'>" << generalizedIndex << "</Parameter>" << std::endl;
0176
0177 md5_state_t md5er;
0178 md5_byte_t digest[16];
0179 unsigned char tool[2];
0180 md5_init(&md5er);
0181 for (int i = 0; i < 128; i++) {
0182 tool[0] = lut[i] & 0xFF;
0183 tool[1] = (lut[i] >> 8) & 0xFF;
0184 md5_append(&md5er, tool, 2);
0185 }
0186 md5_finish(&md5er, digest);
0187 os << " <Parameter name='CHECKSUM' type='string'>";
0188 for (int i = 0; i < 16; i++)
0189 os << std::hex << (((int)(digest[i])) & 0xFF);
0190 os << "</Parameter>\n";
0191
0192 *oc_ << " <Data crate='" << eid.readoutVMECrateId() << "' slot='" << eid.htrSlot() << "' fpga='"
0193 << eid.htrTopBottom() << "' fiber='" << eid.fiberIndex() << "' fiberchan='" << eid.fiberChanId()
0194 << "' luttype='1' elements='1' encoding='hex'>";
0195 for (int i = 0; i < 16; i++)
0196 *oc_ << std::hex << (((int)(digest[i])) & 0xFF);
0197 *oc_ << "</Data>\n";
0198
0199 os << " <Data elements='128' encoding='hex'> " << std::endl;
0200 os << std::hex;
0201 for (int initr2 = 0; initr2 < 128; initr2++) {
0202 os << lut[initr2] << " ";
0203 }
0204 os << std::dec;
0205 os << std::endl;
0206 os << " </Data> " << std::endl;
0207 os << "</CFGBrick> " << std::endl;
0208 }
0209
0210 void HcalLuttoDB::writeoutlut2(HcalTrigTowerDetId id,
0211 HcalElectronicsId eid,
0212 const std::vector<unsigned char>& lut,
0213 std::ostream& os) {
0214 os << "<CFGBrick> " << std::endl;
0215 os << " <Parameter name='IETA' type='int'>" << id.ieta() << "</Parameter>" << std::endl;
0216 os << " <Parameter name='IPHI' type='int'>" << id.iphi() << "</Parameter>" << std::endl;
0217 os << " <Parameter name='CRATE' type='int'>" << eid.readoutVMECrateId() << "</Parameter>" << std::endl;
0218 os << " <Parameter name='SLOT' type='int'>" << eid.htrSlot() << "</Parameter>" << std::endl;
0219 os << " <Parameter name='TOPBOTTOM' type='int'>" << eid.htrTopBottom() << "</Parameter>" << std::endl;
0220 os << " <Parameter name='SLB' type='int'>" << eid.slbSiteNumber() << "</Parameter>" << std::endl;
0221 os << " <Parameter name='SLBCHAN' type='int'>" << eid.slbChannelIndex() << "</Parameter>" << std::endl;
0222 os << " <Parameter name='LUT_TYPE' type='int'>2</Parameter>" << std::endl;
0223 os << " <Parameter name='CREATIONTAG' type='string'>" << creationtag_ << "</Parameter>" << std::endl;
0224 os << " <Parameter name='CREATIONSTAMP' type='string'>" << creationstamp_ << "</Parameter>" << std::endl;
0225 os << " <Parameter name='FORMATREVISION' type='string'>" << formatRevision_ << "</Parameter>" << std::endl;
0226 os << " <Parameter name='TARGETFIRMWARE' type='string'>" << targetfirmware_ << "</Parameter>" << std::endl;
0227 int generalizedIndex = id.ietaAbs() + 10000 * id.iphi() + ((id.ieta() < 0) ? (0) : (100));
0228
0229 os << " <Parameter name='GENERALIZEDINDEX' type='int'>" << generalizedIndex << "</Parameter>" << std::endl;
0230
0231
0232 md5_state_t md5er;
0233 md5_byte_t digest[16];
0234 md5_init(&md5er);
0235 md5_append(&md5er, &(lut[0]), 1024);
0236 md5_finish(&md5er, digest);
0237 os << " <Parameter name='CHECKSUM' type='string'>";
0238 for (int i = 0; i < 16; i++)
0239 os << std::hex << (((int)(digest[i])) & 0xFF);
0240 os << "</Parameter>\n";
0241
0242 *oc_ << " <Data crate='" << eid.readoutVMECrateId() << "' slot='" << eid.htrSlot() << "' fpga='"
0243 << eid.htrTopBottom() << "' slb='" << eid.slbSiteNumber() << "' slbchan='" << eid.slbChannelIndex()
0244 << "' luttype='2' elements='1' encoding='hex'>";
0245 for (int i = 0; i < 16; i++)
0246 *oc_ << std::hex << (((int)(digest[i])) & 0xFF);
0247 *oc_ << "</Data>\n";
0248
0249 os << " <Data elements='1024' encoding='hex'> " << std::endl;
0250 os << std::hex;
0251 for (int initr2 = 0; initr2 < 1024; initr2++) {
0252 os << (int(lut[initr2]) & 0xFF) << " ";
0253 }
0254 os << std::dec;
0255 os << std::endl;
0256 os << " </Data> " << std::endl;
0257 os << "</CFGBrick> " << std::endl;
0258 }
0259
0260 std::vector<unsigned char> HcalLuttoDB::extractOutputLut(const CaloTPGTranscoder& coder, HcalTrigTowerDetId chan) {
0261 std::vector<unsigned char> lut;
0262 for (int i = 0; i < 1024; i++) {
0263 HcalTriggerPrimitiveSample s = coder.hcalCompress(chan, i, false);
0264 lut.push_back(s.compressedEt());
0265 }
0266 return lut;
0267 }
0268
0269
0270 void HcalLuttoDB::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0271
0272
0273
0274 edm::LogInfo("Hcal") << "Beginning dump of Hcal TPG LUTS (this may take a minute or two)";
0275
0276 const HcalElectronicsMap* Map_;
0277 const HcalDbService* pSetup = &iSetup.getData(tokdb_);
0278 Map_ = pSetup->getHcalMapping();
0279
0280 const HcalTPGCoder* inputCoder = &iSetup.getData(tokhcalCode_);
0281 const CaloTPGTranscoder* outTranscoder = &iSetup.getData(tokcaloCode_);
0282
0283 std::vector<HcalElectronicsId> allEID = Map_->allElectronicsId();
0284 std::vector<HcalElectronicsId>::iterator itreid;
0285
0286 std::ostream* pfile = nullptr;
0287 oc_ = openChecksums();
0288
0289 for (int crate = 0; crate < 20; crate++) {
0290 edm::LogInfo("Hcal") << "Beginning crate " << crate;
0291 for (itreid = allEID.begin(); itreid != allEID.end(); ++itreid) {
0292 if (itreid->readoutVMECrateId() != crate)
0293 continue;
0294 if (itreid->isTriggerChainId()) {
0295 HcalTrigTowerDetId tid = Map_->lookupTrigger(*itreid);
0296 if (tid.null())
0297 continue;
0298
0299 if (filePerCrate_ && pfile == nullptr)
0300 pfile = openPerCrate(crate);
0301 else if (pfile == nullptr)
0302 pfile = openPerLut2(*itreid);
0303
0304 std::vector<unsigned char> lut = extractOutputLut(*outTranscoder, tid);
0305 writeoutlut2(tid, *itreid, lut, *pfile);
0306 if (!filePerCrate_) {
0307 delete pfile;
0308 pfile = nullptr;
0309 }
0310 } else {
0311 HcalGenericDetId gid = Map_->lookup(*itreid);
0312 if (gid.null() || !(gid.genericSubdet() == HcalGenericDetId::HcalGenBarrel ||
0313 gid.genericSubdet() == HcalGenericDetId::HcalGenEndcap ||
0314 gid.genericSubdet() == HcalGenericDetId::HcalGenForward))
0315 continue;
0316
0317 if (filePerCrate_ && pfile == nullptr)
0318 pfile = openPerCrate(crate);
0319 else if (pfile == nullptr)
0320 pfile = openPerLut1(*itreid);
0321
0322 std::vector<unsigned short> lut = inputCoder->getLinearizationLUT(HcalDetId(gid));
0323 writeoutlut1(HcalDetId(gid), *itreid, lut, *pfile);
0324 if (!filePerCrate_) {
0325 delete pfile;
0326 pfile = nullptr;
0327 }
0328 }
0329 }
0330 if (pfile != nullptr) {
0331 if (filePerCrate_)
0332 *pfile << "</CFGBrickSet>\n";
0333 delete pfile;
0334 pfile = nullptr;
0335 }
0336 }
0337 *oc_ << "</CFGBrick>\n";
0338 delete oc_;
0339 }
0340
0341
0342 void HcalLuttoDB::beginJob() {
0343 char buffer[120];
0344 time_t now = time(nullptr);
0345 struct tm* tm = localtime(&now);
0346 strftime(buffer, 120, "%F %T", tm);
0347 creationstamp_ = buffer;
0348 }
0349
0350
0351 void HcalLuttoDB::endJob() {}
0352
0353
0354 DEFINE_FWK_MODULE(HcalLuttoDB);