File indexing completed on 2024-09-07 04:36:13
0001 #include "EventFilter/L1ScoutingRawToDigi/plugins/ScCALORawToDigi.h"
0002
0003 ScCaloRawToDigi::ScCaloRawToDigi(const edm::ParameterSet& iConfig) {
0004 using namespace edm;
0005 using namespace l1ScoutingRun3;
0006 srcInputTag_ = iConfig.getParameter<InputTag>("srcInputTag");
0007 enableAllSums_ = iConfig.getParameter<bool>("enableAllSums");
0008 debug_ = iConfig.getUntrackedParameter<bool>("debug", false);
0009 dataSourceConfig_ = iConfig.getParameter<edm::ParameterSet>("dataSource");
0010 rawToken_ = consumes<SDSRawDataCollection>(srcInputTag_);
0011
0012 orbitBufferJets_ = std::vector<std::vector<Jet>>(3565);
0013 orbitBufferEGammas_ = std::vector<std::vector<EGamma>>(3565);
0014 orbitBufferTaus_ = std::vector<std::vector<Tau>>(3565);
0015 orbitBufferEtSums_ = std::vector<std::vector<BxSums>>(3565);
0016
0017 nJetsOrbit_ = 0;
0018 nEGammasOrbit_ = 0;
0019 nTausOrbit_ = 0;
0020 nEtSumsOrbit_ = 0;
0021
0022 produces<JetOrbitCollection>("Jet").setBranchAlias("JetOrbitCollection");
0023 produces<TauOrbitCollection>("Tau").setBranchAlias("TauOrbitCollection");
0024 produces<EGammaOrbitCollection>("EGamma").setBranchAlias("EGammaOrbitCollection");
0025 produces<BxSumsOrbitCollection>("EtSum").setBranchAlias("BxSumsOrbitCollection");
0026 }
0027
0028 ScCaloRawToDigi::~ScCaloRawToDigi() {}
0029
0030 void ScCaloRawToDigi::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0031 using namespace edm;
0032 using namespace l1ScoutingRun3;
0033
0034 Handle<SDSRawDataCollection> ScoutingRawDataCollection;
0035 iEvent.getByToken(rawToken_, ScoutingRawDataCollection);
0036
0037 std::unique_ptr<JetOrbitCollection> unpackedJets(new JetOrbitCollection);
0038 std::unique_ptr<TauOrbitCollection> unpackedTaus(new TauOrbitCollection);
0039 std::unique_ptr<EGammaOrbitCollection> unpackedEGammas(new EGammaOrbitCollection);
0040 std::unique_ptr<BxSumsOrbitCollection> unpackedEtSums(new BxSumsOrbitCollection);
0041
0042
0043 nJetsOrbit_ = 0;
0044 nEGammasOrbit_ = 0;
0045 nTausOrbit_ = 0;
0046 nEtSumsOrbit_ = 0;
0047
0048 std::string dataSourceMode = dataSourceConfig_.getParameter<std::string>("dataSourceMode");
0049 if (dataSourceMode == "DMA") {
0050
0051 int sourceId = dataSourceConfig_.getParameter<int>("dmaSourceId");
0052 if (sourceId != SDSNumbering::CaloSDSID)
0053 edm::LogWarning("ScCaloRawToDIgi::produce") << "Provided an unexpected source ID: " << sourceId << "/"
0054 << SDSNumbering::CaloSDSID << " [provided/expected]";
0055 unpackOrbitFromDMA(ScoutingRawDataCollection, sourceId);
0056 } else if (dataSourceMode == "TCP") {
0057
0058 jetSourceIdList_ = dataSourceConfig_.getParameter<std::vector<int>>("jetSourceIdList");
0059 unpackTcpData(ScoutingRawDataCollection, jetSourceIdList_, CaloObjectType::Jet);
0060
0061
0062 eGammaSourceIdList_ = dataSourceConfig_.getParameter<std::vector<int>>("eGammaSourceIdList");
0063 unpackTcpData(ScoutingRawDataCollection, eGammaSourceIdList_, CaloObjectType::EGamma);
0064
0065
0066 tauSourceIdList_ = dataSourceConfig_.getParameter<std::vector<int>>("tauSourceIdList");
0067 unpackTcpData(ScoutingRawDataCollection, tauSourceIdList_, CaloObjectType::Tau);
0068
0069
0070 etSumSourceIdList_ = dataSourceConfig_.getParameter<std::vector<int>>("etSumSourceIdList");
0071 unpackTcpData(ScoutingRawDataCollection, etSumSourceIdList_, CaloObjectType::EtSum);
0072 } else {
0073 throw cms::Exception("ScCaloRawToDIgi::produce") << "Unknown data source mode. Use DMA or TCP(default).";
0074 }
0075
0076
0077 unpackedJets->fillAndClear(orbitBufferJets_, nJetsOrbit_);
0078 unpackedEGammas->fillAndClear(orbitBufferEGammas_, nEGammasOrbit_);
0079 unpackedTaus->fillAndClear(orbitBufferTaus_, nTausOrbit_);
0080 unpackedEtSums->fillAndClear(orbitBufferEtSums_, nEtSumsOrbit_);
0081
0082
0083 iEvent.put(std::move(unpackedJets), "Jet");
0084 iEvent.put(std::move(unpackedTaus), "Tau");
0085 iEvent.put(std::move(unpackedEGammas), "EGamma");
0086 iEvent.put(std::move(unpackedEtSums), "EtSum");
0087 }
0088
0089 void ScCaloRawToDigi::unpackOrbitFromDMA(edm::Handle<SDSRawDataCollection>& ScoutingRawDataCollection, int sourceId) {
0090 using namespace l1ScoutingRun3;
0091
0092 const FEDRawData& sourceRawData = ScoutingRawDataCollection->FEDData(sourceId);
0093 if ((sourceRawData.size() == 0) && debug_) {
0094 std::cout << "No raw data for CALO DMA source ID=" << sourceId << std::endl;
0095 }
0096
0097
0098 size_t len = sourceRawData.size();
0099 const unsigned char* buf = sourceRawData.data();
0100
0101 size_t pos = 0;
0102
0103 while (pos < len) {
0104 assert(pos + sizeof(demux::dmaBlock) <= len);
0105
0106 demux::dmaBlock* bl = (demux::dmaBlock*)(buf + pos);
0107 pos += sizeof(demux::dmaBlock);
0108
0109 assert(pos <= len);
0110 uint32_t orbit = bl->orbit & 0x7FFFFFFF;
0111 uint32_t bx = bl->bx;
0112
0113 if (debug_) {
0114 std::cout << "CALO Orbit " << orbit << ", BX -> " << bx << std::endl;
0115 }
0116
0117
0118 if (debug_)
0119 std::cout << "--- Jets link 1 ---\n";
0120 unpackJets(bl->jet1, bx, 6);
0121
0122
0123 if (debug_)
0124 std::cout << "--- Jets link 2 ---\n";
0125 unpackJets(bl->jet2, bx, 6);
0126
0127
0128 if (debug_)
0129 std::cout << "--- E/g link 1 ---\n";
0130 unpackEGammas(bl->egamma1, bx, 6);
0131
0132
0133 if (debug_)
0134 std::cout << "--- E/g link 2 ---\n";
0135 unpackEGammas(bl->egamma2, bx, 6);
0136
0137
0138 if (debug_)
0139 std::cout << "--- Taus link 1 ---\n";
0140 unpackTaus(bl->tau1, bx, 6);
0141
0142
0143 if (debug_)
0144 std::cout << "--- Taus link 2 ---\n";
0145 unpackTaus(bl->tau2, bx, 6);
0146
0147
0148 if (debug_)
0149 std::cout << "--- Sums ---\n";
0150 unpackEtSums(bl->sum, bx);
0151
0152 }
0153 }
0154
0155 void ScCaloRawToDigi::unpackTcpData(edm::Handle<SDSRawDataCollection>& ScoutingRawDataCollection,
0156 std::vector<int> sourceList,
0157 CaloObjectType dataType) {
0158 using namespace l1ScoutingRun3;
0159 for (const int& sourceId : sourceList) {
0160 if ((sourceId < SDSNumbering::CaloTCPMinSDSID) || (sourceId > SDSNumbering::CaloTCPMaxSDSID)) {
0161 edm::LogWarning("ScCaloRawToDIgi::unpackTCPData")
0162 << "Source ID outside the expected range " << sourceId << "/[" << SDSNumbering::CaloTCPMinSDSID << "-"
0163 << SDSNumbering::CaloTCPMaxSDSID << "]"
0164 << " [provided/expected range]";
0165 }
0166 const FEDRawData& sourceRawData = ScoutingRawDataCollection->FEDData(sourceId);
0167 size_t orbitSize = sourceRawData.size();
0168
0169 if ((sourceRawData.size() == 0) && debug_) {
0170
0171 std::cout << "No raw data for calo TCP source ID=" << sourceId << std::endl;
0172 }
0173
0174 unpackOrbitFromTCP(sourceRawData.data(), orbitSize, dataType);
0175 }
0176 }
0177
0178 void ScCaloRawToDigi::unpackOrbitFromTCP(const unsigned char* buf, size_t len, CaloObjectType dataType) {
0179 using namespace l1ScoutingRun3;
0180 size_t pos = 0;
0181
0182 while (pos < len) {
0183
0184 assert(pos + 4 <= len);
0185
0186
0187 if (dataType == CaloObjectType::EtSum) {
0188 demux::caloSumTcpBlock* bl = (demux::caloSumTcpBlock*)(buf + pos);
0189 pos += sizeof(demux::caloSumTcpBlock);
0190 assert(pos <= len);
0191 if (debug_)
0192 std::cout << "Sums BX -> " << bl->bx << std::endl;
0193 unpackEtSums(bl->sum, bl->bx);
0194 } else {
0195
0196 demux::caloObjTcpBlock* bl = (demux::caloObjTcpBlock*)(buf + pos);
0197 int nObj = (bl->header) & 0xff;
0198 pos += 12 + nObj * 4;
0199
0200 switch (dataType) {
0201 case CaloObjectType::Jet:
0202 if (debug_)
0203 std::cout << "Jets BX -> " << bl->bx << std::endl;
0204 unpackJets(bl->obj, bl->bx, nObj);
0205 break;
0206
0207 case CaloObjectType::EGamma:
0208 if (debug_)
0209 std::cout << "E/Gammas BX -> " << bl->bx << std::endl;
0210 unpackEGammas(bl->obj, bl->bx, nObj);
0211 break;
0212
0213 case CaloObjectType::Tau:
0214 if (debug_)
0215 std::cout << "Taus BX -> " << bl->bx << std::endl;
0216 unpackTaus(bl->obj, bl->bx, nObj);
0217 break;
0218
0219 default:
0220 throw cms::Exception("ScCaloRawToDigi::unpackOrbitFromTCP") << "Unknown data type.";
0221 break;
0222 }
0223
0224 }
0225
0226 }
0227 }
0228
0229 void ScCaloRawToDigi::unpackJets(uint32_t* dataBlock, int bx, int nObjets) {
0230 using namespace l1ScoutingRun3;
0231
0232 int32_t ET(0), Eta(0), Phi(0), Qual(0);
0233 for (int i = 0; i < nObjets; i++) {
0234 ET = ((dataBlock[i] >> demux::shiftsJet::ET) & demux::masksJet::ET);
0235
0236 if (ET != 0) {
0237 Eta = ((dataBlock[i] >> demux::shiftsJet::eta) & demux::masksJet::eta);
0238 Phi = ((dataBlock[i] >> demux::shiftsJet::phi) & demux::masksJet::phi);
0239 Qual = ((dataBlock[i] >> demux::shiftsJet::qual) & demux::masksJet::qual);
0240
0241 if (Eta > 127)
0242 Eta = Eta - 256;
0243
0244 Jet jet(ET, Eta, Phi, Qual);
0245 orbitBufferJets_[bx].push_back(jet);
0246 nJetsOrbit_++;
0247
0248 if (debug_) {
0249 std::cout << "Jet " << i << std::endl;
0250 std::cout << " Raw: 0x" << std::hex << dataBlock[i] << std::dec << std::endl;
0251 printJet(jet);
0252 }
0253 }
0254 }
0255 }
0256
0257 void ScCaloRawToDigi::unpackEGammas(uint32_t* dataBlock, int bx, int nObjets) {
0258 using namespace l1ScoutingRun3;
0259
0260 int32_t ET(0), Eta(0), Phi(0), Iso(0);
0261 for (int i = 0; i < nObjets; i++) {
0262 ET = ((dataBlock[i] >> demux::shiftsEGamma::ET) & demux::masksEGamma::ET);
0263 if (ET != 0) {
0264 Eta = ((dataBlock[i] >> demux::shiftsEGamma::eta) & demux::masksEGamma::eta);
0265 Phi = ((dataBlock[i] >> demux::shiftsEGamma::phi) & demux::masksEGamma::phi);
0266 Iso = ((dataBlock[i] >> demux::shiftsEGamma::iso) & demux::masksEGamma::iso);
0267
0268 if (Eta > 127)
0269 Eta = Eta - 256;
0270
0271 EGamma eGamma(ET, Eta, Phi, Iso);
0272 orbitBufferEGammas_[bx].push_back(eGamma);
0273 nEGammasOrbit_++;
0274
0275 if (debug_) {
0276 std::cout << "E/g " << i << std::endl;
0277 std::cout << " Raw: 0x" << std::hex << dataBlock[i] << std::dec << std::endl;
0278 printEGamma(eGamma);
0279 }
0280 }
0281 }
0282 }
0283
0284 void ScCaloRawToDigi::unpackTaus(uint32_t* dataBlock, int bx, int nObjets) {
0285 using namespace l1ScoutingRun3;
0286
0287 int32_t ET(0), Eta(0), Phi(0), Iso(0);
0288 for (int i = 0; i < nObjets; i++) {
0289 ET = ((dataBlock[i] >> demux::shiftsTau::ET) & demux::masksTau::ET);
0290 if (ET != 0) {
0291 Eta = ((dataBlock[i] >> demux::shiftsTau::eta) & demux::masksTau::eta);
0292 Phi = ((dataBlock[i] >> demux::shiftsTau::phi) & demux::masksTau::phi);
0293 Iso = ((dataBlock[i] >> demux::shiftsTau::iso) & demux::masksTau::iso);
0294
0295 if (Eta > 127)
0296 Eta = Eta - 256;
0297
0298 Tau tau(ET, Eta, Phi, Iso);
0299 orbitBufferTaus_[bx].push_back(tau);
0300 nTausOrbit_++;
0301
0302 if (debug_) {
0303 std::cout << "Tau " << i << std::endl;
0304 std::cout << " Raw: 0x" << std::hex << dataBlock[i] << std::dec << std::endl;
0305 printTau(tau);
0306 }
0307 }
0308 }
0309 }
0310
0311 void ScCaloRawToDigi::unpackEtSums(uint32_t* dataBlock, int bx) {
0312 using namespace l1ScoutingRun3;
0313
0314 BxSums bxSums;
0315
0316 int32_t ETEt(0), ETEttem(0), ETMinBiasHFP0(0);
0317 int32_t HTEt(0), HTtowerCount(0), HTMinBiasHFM0(0);
0318 int32_t ETmissEt(0), ETmissPhi(0), ETmissASYMET(0), ETmissMinBiasHFP1(0);
0319 int32_t HTmissEt(0), HTmissPhi(0), HTmissASYMHT(0), HTmissMinBiasHFM1(0);
0320 int32_t ETHFmissEt(0), ETHFmissPhi(0), ETHFmissASYMETHF(0), ETHFmissCENT(0);
0321 int32_t HTHFmissEt(0), HTHFmissPhi(0), HTHFmissASYMHTHF(0), HTHFmissCENT(0);
0322
0323
0324 ETEt = ((dataBlock[0] >> demux::shiftsESums::ETEt) & demux::masksESums::ETEt);
0325 ETEttem = ((dataBlock[0] >> demux::shiftsESums::ETEttem) & demux::masksESums::ETEttem);
0326
0327 bxSums.setHwTotalEt(ETEt);
0328 bxSums.setHwTotalEtEm(ETEttem);
0329
0330
0331 HTEt = ((dataBlock[1] >> demux::shiftsESums::HTEt) & demux::masksESums::HTEt);
0332
0333 bxSums.setHwTotalHt(HTEt);
0334
0335
0336 ETmissEt = ((dataBlock[2] >> demux::shiftsESums::ETmissEt) & demux::masksESums::ETmissEt);
0337 ETmissPhi = ((dataBlock[2] >> demux::shiftsESums::ETmissPhi) & demux::masksESums::ETmissPhi);
0338
0339 if (ETmissEt > 0) {
0340 bxSums.setHwMissEt(ETmissEt);
0341 bxSums.setHwMissEtPhi(ETmissPhi);
0342 }
0343
0344
0345 HTmissEt = ((dataBlock[3] >> demux::shiftsESums::HTmissEt) & demux::masksESums::HTmissEt);
0346 HTmissPhi = ((dataBlock[3] >> demux::shiftsESums::HTmissPhi) & demux::masksESums::HTmissPhi);
0347
0348 if (HTmissEt > 0) {
0349 bxSums.setHwMissHt(HTmissEt);
0350 bxSums.setHwMissHtPhi(HTmissPhi);
0351 }
0352
0353
0354 ETHFmissEt = ((dataBlock[4] >> demux::shiftsESums::ETHFmissEt) & demux::masksESums::ETHFmissEt);
0355 ETHFmissPhi = ((dataBlock[4] >> demux::shiftsESums::ETHFmissPhi) & demux::masksESums::ETHFmissPhi);
0356
0357 if (ETHFmissEt > 0) {
0358 bxSums.setHwMissEtHF(ETHFmissEt);
0359 bxSums.setHwMissEtHFPhi(ETHFmissPhi);
0360 }
0361
0362
0363 HTHFmissEt = ((dataBlock[5] >> demux::shiftsESums::ETHFmissEt) & demux::masksESums::ETHFmissEt);
0364 HTHFmissPhi = ((dataBlock[5] >> demux::shiftsESums::ETHFmissPhi) & demux::masksESums::ETHFmissPhi);
0365
0366 if (HTHFmissEt > 0) {
0367 bxSums.setHwMissHtHF(HTHFmissEt);
0368 bxSums.setHwMissHtHFPhi(HTHFmissPhi);
0369 }
0370
0371
0372 if (enableAllSums_) {
0373
0374 ETMinBiasHFP0 = ((dataBlock[0] >> demux::shiftsESums::ETMinBiasHF) & demux::masksESums::ETMinBiasHF);
0375 bxSums.setMinBiasHFP0(ETMinBiasHFP0);
0376
0377
0378 HTtowerCount = ((dataBlock[1] >> demux::shiftsESums::HTtowerCount) & demux::masksESums::HTtowerCount);
0379 HTMinBiasHFM0 = ((dataBlock[1] >> demux::shiftsESums::HTMinBiasHF) & demux::masksESums::HTMinBiasHF);
0380
0381 bxSums.setTowerCount(HTtowerCount);
0382 bxSums.setMinBiasHFM0(HTMinBiasHFM0);
0383
0384
0385 ETmissASYMET = ((dataBlock[2] >> demux::shiftsESums::ETmissASYMET) & demux::masksESums::ETmissASYMET);
0386 ETmissMinBiasHFP1 = ((dataBlock[2] >> demux::shiftsESums::ETmissMinBiasHF) & demux::masksESums::ETmissMinBiasHF);
0387 bxSums.setHwAsymEt(ETmissASYMET);
0388 bxSums.setMinBiasHFP1(ETmissMinBiasHFP1);
0389
0390
0391 HTmissASYMHT = ((dataBlock[3] >> demux::shiftsESums::HTmissASYMHT) & demux::masksESums::HTmissASYMHT);
0392 HTmissMinBiasHFM1 = ((dataBlock[3] >> demux::shiftsESums::HTmissMinBiasHF) & demux::masksESums::HTmissMinBiasHF);
0393
0394 bxSums.setHwAsymHt(HTmissASYMHT);
0395 bxSums.setMinBiasHFM1(HTmissMinBiasHFM1);
0396
0397
0398 ETHFmissASYMETHF = ((dataBlock[4] >> demux::shiftsESums::ETHFmissASYMETHF) & demux::masksESums::ETHFmissASYMETHF);
0399 ETHFmissCENT = ((dataBlock[4] >> demux::shiftsESums::ETHFmissCENT) & demux::masksESums::ETHFmissCENT);
0400
0401 bxSums.setHwAsymEtHF(ETHFmissASYMETHF);
0402
0403
0404 HTHFmissASYMHTHF = ((dataBlock[5] >> demux::shiftsESums::ETHFmissASYMETHF) & demux::masksESums::ETHFmissASYMETHF);
0405 HTHFmissCENT = ((dataBlock[5] >> demux::shiftsESums::ETHFmissCENT) & demux::masksESums::ETHFmissCENT);
0406
0407 bxSums.setHwAsymHtHF(HTHFmissASYMHTHF);
0408 bxSums.setCentrality((HTHFmissCENT << 4) + ETHFmissCENT);
0409 }
0410
0411 orbitBufferEtSums_[bx].push_back(bxSums);
0412 nEtSumsOrbit_ += 1;
0413
0414 if (debug_) {
0415 std::cout << "Raw frames:\n";
0416 for (int frame = 0; frame < 6; frame++) {
0417 std::cout << " frame " << frame << ": 0x" << std::hex << dataBlock[frame] << std::dec << std::endl;
0418 }
0419 printBxSums(bxSums);
0420 }
0421 }
0422
0423 void ScCaloRawToDigi::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0424 edm::ParameterSetDescription desc;
0425 desc.add<edm::InputTag>("srcInputTag", edm::InputTag("rawDataCollector"));
0426 {
0427 edm::ParameterSetDescription dataSource;
0428 dataSource.add<std::string>("dataSourceMode", std::string("TCP"));
0429 dataSource.add<std::vector<int>>("jetSourceIdList", std::vector<int>({22}));
0430 dataSource.add<std::vector<int>>("eGammaSourceIdList", std::vector<int>({23}));
0431 dataSource.add<std::vector<int>>("tauSourceIdList", std::vector<int>({25}));
0432 dataSource.add<std::vector<int>>("etSumSourceIdList", std::vector<int>({24}));
0433 dataSource.add<int>("dmaSourceId", 2);
0434 desc.add("dataSource", dataSource);
0435 }
0436 desc.add<bool>("enableAllSums", true);
0437 desc.addUntracked<bool>("debug", false);
0438 descriptions.add("ScCaloRawToDigi", desc);
0439 }
0440
0441 DEFINE_FWK_MODULE(ScCaloRawToDigi);