File indexing completed on 2023-10-25 09:54:51
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/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0025
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030
0031 #include "DataFormats/L1TCalorimeter/interface/CaloTower.h"
0032
0033 #include "L1Trigger/L1TCalorimeter/interface/CaloTools.h"
0034
0035 #include <fstream>
0036 #include <iostream>
0037 #include <iomanip>
0038 #include <cmath>
0039
0040
0041
0042
0043
0044 class L1TStage2InputPatternWriter : public edm::one::EDAnalyzer<> {
0045 public:
0046 explicit L1TStage2InputPatternWriter(const edm::ParameterSet&);
0047 ~L1TStage2InputPatternWriter() override;
0048
0049 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0050
0051 private:
0052 void beginJob() override;
0053 void analyze(const edm::Event&, const edm::EventSetup&) override;
0054 void endJob() override;
0055
0056
0057
0058
0059
0060
0061
0062 edm::EDGetToken m_towerToken;
0063
0064 std::string filename_;
0065 std::string outDir_;
0066
0067
0068 unsigned nChan_;
0069 unsigned nQuad_;
0070 unsigned nLink_;
0071 unsigned nHeaderFrames_;
0072 unsigned nPayloadFrames_;
0073 unsigned nClearFrames_;
0074 unsigned nFrame_;
0075 unsigned nEvents_;
0076
0077
0078 std::vector<std::vector<int> > data_;
0079
0080
0081 std::vector<int> dataValid_;
0082
0083
0084 std::map<int, int> map_;
0085 };
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 L1TStage2InputPatternWriter::L1TStage2InputPatternWriter(const edm::ParameterSet& iConfig) {
0099
0100
0101
0102 m_towerToken = consumes<l1t::CaloTowerBxCollection>(iConfig.getParameter<edm::InputTag>("towerToken"));
0103
0104 filename_ = iConfig.getUntrackedParameter<std::string>("filename");
0105 outDir_ = iConfig.getUntrackedParameter<std::string>("outDir");
0106
0107 nChan_ = 4;
0108 nQuad_ = 18;
0109
0110 nHeaderFrames_ = iConfig.getUntrackedParameter<unsigned>("mpHeaderFrames");
0111 nPayloadFrames_ = iConfig.getUntrackedParameter<unsigned>("mpPayloadFrames");
0112 nClearFrames_ = iConfig.getUntrackedParameter<unsigned>("mpClearFrames");
0113 nFrame_ = 0;
0114 nEvents_ = 0;
0115
0116 nLink_ = nChan_ * nQuad_;
0117 data_.resize(nLink_);
0118 LogDebug("L1TDebug") << "Preparing for " << nLink_ << " links" << std::endl;
0119 }
0120
0121 L1TStage2InputPatternWriter::~L1TStage2InputPatternWriter() {
0122
0123
0124 }
0125
0126
0127
0128
0129
0130
0131 void L1TStage2InputPatternWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0132 using namespace edm;
0133
0134
0135 nEvents_++;
0136
0137
0138 Handle<BXVector<l1t::CaloTower> > towHandle;
0139 iEvent.getByToken(m_towerToken, towHandle);
0140
0141 std::vector<l1t::CaloTower> towers;
0142
0143 for (std::vector<l1t::CaloTower>::const_iterator tower = towHandle->begin(0); tower != towHandle->end(0); ++tower) {
0144 towers.push_back(*tower);
0145 }
0146
0147
0148 for (unsigned iFrame = 0; iFrame < nHeaderFrames_; ++iFrame) {
0149 dataValid_.push_back(1);
0150
0151
0152 for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0153 for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0154 int data = 0;
0155
0156
0157 unsigned iLink = (iQuad * nChan_) + iChan;
0158
0159
0160 data_.at(iLink).push_back(data);
0161 }
0162 }
0163
0164 nFrame_++;
0165 }
0166
0167
0168 for (unsigned iFrame = 0; iFrame < nPayloadFrames_; ++iFrame) {
0169 dataValid_.push_back(1);
0170
0171
0172 for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0173 for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0174 int data = 0;
0175
0176
0177 int iLink = (iQuad * nChan_) + iChan;
0178 int ietaSgn = (iLink % 2 == 0 ? +1 : -1);
0179 int ieta = ietaSgn * (iFrame + 1);
0180 int iphi = 1 + (iLink % 2 == 0 ? iLink : iLink - 1);
0181
0182
0183 l1t::CaloTower tower = l1t::CaloTools::getTower(towers, l1t::CaloTools::caloEta(ieta), iphi);
0184 data |= tower.hwPt() & 0x1ff;
0185 data |= (tower.hwEtRatio() & 0x7) << 9;
0186 data |= (tower.hwQual() & 0xf) << 12;
0187
0188
0189 iphi = iphi + 1;
0190 tower = l1t::CaloTools::getTower(towers, l1t::CaloTools::caloEta(ieta), iphi);
0191 data |= (tower.hwPt() & 0x1ff) << 16;
0192 data |= (tower.hwEtRatio() & 0x7) << 25;
0193 data |= (tower.hwQual() & 0xf) << 28;
0194
0195
0196 data_.at(iLink).push_back(data);
0197 }
0198 }
0199
0200 nFrame_++;
0201 }
0202
0203
0204 for (unsigned iFrame = 0; iFrame < nClearFrames_; ++iFrame) {
0205 dataValid_.push_back(0);
0206
0207
0208 for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0209 for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0210 int data = 0;
0211
0212
0213 unsigned iLink = (iQuad * nChan_) + iChan;
0214
0215
0216 data_.at(iLink).push_back(data);
0217 }
0218 }
0219
0220 nFrame_++;
0221 }
0222 }
0223
0224
0225 void L1TStage2InputPatternWriter::beginJob() {}
0226
0227
0228 void L1TStage2InputPatternWriter::endJob() {
0229
0230 unsigned int framesPerEv = nHeaderFrames_ + nPayloadFrames_ + nClearFrames_;
0231
0232
0233 unsigned int evPerFile = floor(1024 / framesPerEv);
0234
0235
0236 unsigned int framesPerFile = framesPerEv * evPerFile;
0237
0238
0239 unsigned int nOutFiles = ceil(nEvents_ / evPerFile);
0240
0241 LogDebug("L1TDebug") << "Read " << nFrame_ << " frames" << std::endl;
0242 LogDebug("L1TDebug") << "Read " << nEvents_ << " events" << std::endl;
0243 LogDebug("L1TDebug") << "Writing " << nOutFiles << " files" << std::endl;
0244 LogDebug("L1TDebug") << "Output directory: ./" << outDir_ << "/" << std::endl;
0245
0246
0247 std::vector<std::ofstream> outFiles(nOutFiles);
0248
0249
0250 for (uint itFile = 0; itFile < nOutFiles; ++itFile) {
0251 std::stringstream outFilename;
0252 outFilename << outDir_ << "/" << filename_ << "_" << itFile << ".txt";
0253 outFiles[itFile] = std::ofstream(outFilename.str());
0254 LogDebug("L1TDebug") << "Writing to file: ./" << outFilename.str() << std::endl;
0255 std::cout << "Writing to file: ./" << outFilename.str() << std::endl;
0256
0257 outFiles[itFile] << "Board MP7_TEST" << std::endl;
0258
0259
0260 outFiles[itFile] << " Quad/Chan : ";
0261 for (unsigned i = 0; i < nQuad_; ++i) {
0262 for (unsigned j = 0; j < nChan_; ++j) {
0263 outFiles[itFile] << " q" << setfill('0') << setw(2) << i << "c" << j << " ";
0264 }
0265 }
0266 outFiles[itFile] << std::endl;
0267
0268
0269 outFiles[itFile] << " Link : ";
0270 for (unsigned i = 0; i < nQuad_; ++i) {
0271 for (unsigned j = 0; j < nChan_; ++j) {
0272 outFiles[itFile] << " " << setfill('0') << setw(2) << (i * nChan_) + j << " ";
0273 }
0274 }
0275
0276 outFiles[itFile] << std::endl;
0277
0278
0279 unsigned iFileFrame = 0;
0280 for (unsigned iFrame = itFile * framesPerFile; iFrame < (itFile * framesPerFile + framesPerFile); ++iFrame) {
0281 if (iFrame <= nFrame_) {
0282 outFiles[itFile] << "Frame " << std::dec << std::setw(4) << std::setfill('0') << iFileFrame << " : ";
0283 for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0284 for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0285 unsigned iLink = (iQuad * nChan_) + iChan;
0286 if (iLink < data_.size() && iFrame < data_.at(iLink).size()) {
0287 outFiles[itFile] << std::hex << ::std::setw(1) << dataValid_.at(iFrame) << "v" << std::hex << std::setw(8)
0288 << std::setfill('0') << data_.at(iLink).at(iFrame) << " ";
0289 } else {
0290 std::cerr << "Out of range : " << iLink << ", " << iFrame << std::endl;
0291 }
0292 }
0293 }
0294 }
0295 outFiles[itFile] << std::endl;
0296 iFileFrame++;
0297 }
0298 outFiles[itFile].close();
0299 }
0300 }
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 void L1TStage2InputPatternWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0336
0337
0338 edm::ParameterSetDescription desc;
0339 desc.setUnknown();
0340 descriptions.addDefault(desc);
0341 }
0342
0343
0344 DEFINE_FWK_MODULE(L1TStage2InputPatternWriter);