Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:20:19

0001 // -*- C++ -*-
0002 //
0003 // Package:    L1Trigger/L1TCalorimeter
0004 // Class:      L1TStage2InputPatternWriter
0005 //
0006 /**\class L1TStage2InputPatternWriter L1TStage2InputPatternWriter.cc L1Trigger/L1TCalorimeter/plugins/L1TStage2InputPatternWriter.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  James Brooke
0015 //         Created:  Tue, 11 Mar 2014 14:55:45 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
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 // class declaration
0042 //
0043 
0044 class L1TStage2InputPatternWriter : public edm::one::EDAnalyzer<> {
0045 public:
0046   explicit L1TStage2InputPatternWriter(const edm::ParameterSet&);
0047 
0048   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0049 
0050 private:
0051   void analyze(const edm::Event&, const edm::EventSetup&) override;
0052   void endJob() override;
0053 
0054   // ----------member data ---------------------------
0055   edm::EDGetToken m_towerToken;
0056 
0057   std::string filename_;
0058   std::string outDir_;
0059 
0060   // constants
0061   unsigned nChan_;  // number of channels per quad
0062   unsigned nQuad_;
0063   unsigned nLink_;
0064   unsigned nHeaderFrames_;
0065   unsigned nPayloadFrames_;
0066   unsigned nClearFrames_;
0067   unsigned nFrame_;
0068   unsigned nEvents_;
0069 
0070   // data arranged by link and frame
0071   std::vector<std::vector<int> > data_;
0072 
0073   // data valid flags (just one per frame for now)
0074   std::vector<int> dataValid_;
0075 
0076   // map of towers onto links/frames
0077   std::map<int, int> map_;
0078 };
0079 
0080 //
0081 // constants, enums and typedefs
0082 //
0083 
0084 //
0085 // static data member definitions
0086 //
0087 
0088 //
0089 // constructors and destructor
0090 //
0091 L1TStage2InputPatternWriter::L1TStage2InputPatternWriter(const edm::ParameterSet& iConfig) {
0092   //now do what ever initialization is needed
0093 
0094   // register what you consume and keep token for later access:
0095   m_towerToken = consumes<l1t::CaloTowerBxCollection>(iConfig.getParameter<edm::InputTag>("towerToken"));
0096 
0097   filename_ = iConfig.getUntrackedParameter<std::string>("filename");
0098   outDir_ = iConfig.getUntrackedParameter<std::string>("outDir");
0099 
0100   nChan_ = 4;
0101   nQuad_ = 18;
0102 
0103   nHeaderFrames_ = iConfig.getUntrackedParameter<unsigned>("mpHeaderFrames");
0104   nPayloadFrames_ = iConfig.getUntrackedParameter<unsigned>("mpPayloadFrames");
0105   nClearFrames_ = iConfig.getUntrackedParameter<unsigned>("mpClearFrames");
0106   nFrame_ = 0;
0107   nEvents_ = 0;
0108 
0109   nLink_ = nChan_ * nQuad_;
0110   data_.resize(nLink_);
0111   LogDebug("L1TDebug") << "Preparing for " << nLink_ << " links" << std::endl;
0112 }
0113 
0114 //
0115 // member functions
0116 //
0117 
0118 // ------------ method called for each event  ------------
0119 void L1TStage2InputPatternWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0120   using namespace edm;
0121 
0122   //count events
0123   nEvents_++;
0124 
0125   // get towers
0126   Handle<BXVector<l1t::CaloTower> > towHandle;
0127   iEvent.getByToken(m_towerToken, towHandle);
0128 
0129   std::vector<l1t::CaloTower> towers;
0130 
0131   for (std::vector<l1t::CaloTower>::const_iterator tower = towHandle->begin(0); tower != towHandle->end(0); ++tower) {
0132     towers.push_back(*tower);
0133   }
0134 
0135   // insert header frames
0136   for (unsigned iFrame = 0; iFrame < nHeaderFrames_; ++iFrame) {
0137     dataValid_.push_back(1);
0138 
0139     // loop over links
0140     for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0141       for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0142         int data = 0;
0143 
0144         // get tower ieta, iphi for link
0145         unsigned iLink = (iQuad * nChan_) + iChan;
0146 
0147         // add data to output
0148         data_.at(iLink).push_back(data);
0149       }
0150     }
0151 
0152     nFrame_++;
0153   }
0154 
0155   // loop over frames
0156   for (unsigned iFrame = 0; iFrame < nPayloadFrames_; ++iFrame) {
0157     dataValid_.push_back(1);
0158 
0159     // loop over links
0160     for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0161       for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0162         int data = 0;
0163 
0164         // get tower ieta, iphi for link
0165         int iLink = (iQuad * nChan_) + iChan;
0166         int ietaSgn = (iLink % 2 == 0 ? +1 : -1);
0167         int ieta = ietaSgn * (iFrame + 1);
0168         int iphi = 1 + (iLink % 2 == 0 ? iLink : iLink - 1);
0169 
0170         // get tower 1 data
0171         l1t::CaloTower tower = l1t::CaloTools::getTower(towers, l1t::CaloTools::caloEta(ieta), iphi);
0172         data |= tower.hwPt() & 0x1ff;
0173         data |= (tower.hwEtRatio() & 0x7) << 9;
0174         data |= (tower.hwQual() & 0xf) << 12;
0175 
0176         // get tower 2
0177         iphi = iphi + 1;
0178         tower = l1t::CaloTools::getTower(towers, l1t::CaloTools::caloEta(ieta), iphi);
0179         data |= (tower.hwPt() & 0x1ff) << 16;
0180         data |= (tower.hwEtRatio() & 0x7) << 25;
0181         data |= (tower.hwQual() & 0xf) << 28;
0182 
0183         // add data to output
0184         data_.at(iLink).push_back(data);
0185       }
0186     }
0187 
0188     nFrame_++;
0189   }
0190 
0191   // loop over clear frames
0192   for (unsigned iFrame = 0; iFrame < nClearFrames_; ++iFrame) {
0193     dataValid_.push_back(0);
0194 
0195     // loop over links
0196     for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0197       for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0198         int data = 0;
0199 
0200         // get tower ieta, iphi for link
0201         unsigned iLink = (iQuad * nChan_) + iChan;
0202 
0203         // add data to output
0204         data_.at(iLink).push_back(data);
0205       }
0206     }
0207 
0208     nFrame_++;
0209   }
0210 }
0211 
0212 // ------------ method called once each job just after ending the event loop  ------------
0213 void L1TStage2InputPatternWriter::endJob() {
0214   //frames per event
0215   unsigned int framesPerEv = nHeaderFrames_ + nPayloadFrames_ + nClearFrames_;
0216 
0217   //events per file
0218   unsigned int evPerFile = floor(1024 / framesPerEv);
0219 
0220   //frames per file
0221   unsigned int framesPerFile = framesPerEv * evPerFile;
0222 
0223   //number of output files
0224   unsigned int nOutFiles = ceil(nEvents_ / evPerFile);
0225 
0226   LogDebug("L1TDebug") << "Read " << nFrame_ << " frames" << std::endl;
0227   LogDebug("L1TDebug") << "Read " << nEvents_ << " events" << std::endl;
0228   LogDebug("L1TDebug") << "Writing " << nOutFiles << " files" << std::endl;
0229   LogDebug("L1TDebug") << "Output directory: ./" << outDir_ << "/" << std::endl;
0230 
0231   //files
0232   std::vector<std::ofstream> outFiles(nOutFiles);
0233 
0234   //make output files and write to them
0235   for (uint itFile = 0; itFile < nOutFiles; ++itFile) {
0236     std::stringstream outFilename;
0237     outFilename << outDir_ << "/" << filename_ << "_" << itFile << ".txt";
0238     outFiles[itFile] = std::ofstream(outFilename.str());
0239     LogDebug("L1TDebug") << "Writing to file: ./" << outFilename.str() << std::endl;
0240     std::cout << "Writing to file: ./" << outFilename.str() << std::endl;
0241 
0242     outFiles[itFile] << "Board MP7_TEST" << std::endl;
0243 
0244     // quad/chan numbers
0245     outFiles[itFile] << " Quad/Chan :  ";
0246     for (unsigned i = 0; i < nQuad_; ++i) {
0247       for (unsigned j = 0; j < nChan_; ++j) {
0248         outFiles[itFile] << "  q" << setfill('0') << setw(2) << i << "c" << j << "    ";
0249       }
0250     }
0251     outFiles[itFile] << std::endl;
0252 
0253     // link numbers
0254     outFiles[itFile] << "      Link : ";
0255     for (unsigned i = 0; i < nQuad_; ++i) {
0256       for (unsigned j = 0; j < nChan_; ++j) {
0257         outFiles[itFile] << "    " << setfill('0') << setw(2) << (i * nChan_) + j << "     ";
0258       }
0259     }
0260 
0261     outFiles[itFile] << std::endl;
0262 
0263     // then the data
0264     unsigned iFileFrame = 0;
0265     for (unsigned iFrame = itFile * framesPerFile; iFrame < (itFile * framesPerFile + framesPerFile); ++iFrame) {
0266       if (iFrame <= nFrame_) {
0267         outFiles[itFile] << "Frame " << std::dec << std::setw(4) << std::setfill('0') << iFileFrame << " : ";
0268         for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0269           for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0270             unsigned iLink = (iQuad * nChan_) + iChan;
0271             if (iLink < data_.size() && iFrame < data_.at(iLink).size()) {
0272               outFiles[itFile] << std::hex << ::std::setw(1) << dataValid_.at(iFrame) << "v" << std::hex << std::setw(8)
0273                                << std::setfill('0') << data_.at(iLink).at(iFrame) << " ";
0274             } else {
0275               std::cerr << "Out of range : " << iLink << ", " << iFrame << std::endl;
0276             }
0277           }
0278         }
0279       }
0280       outFiles[itFile] << std::endl;
0281       iFileFrame++;
0282     }
0283     outFiles[itFile].close();
0284   }
0285 }
0286 
0287 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0288 void L1TStage2InputPatternWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0289   //The following says we do not know what parameters are allowed so do no validation
0290   // Please change this to state exactly what you do use, even if it is no parameters
0291   edm::ParameterSetDescription desc;
0292   desc.setUnknown();
0293   descriptions.addDefault(desc);
0294 }
0295 
0296 //define this as a plug-in
0297 DEFINE_FWK_MODULE(L1TStage2InputPatternWriter);