Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-29 02:25:58

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   ~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   //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
0057   //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
0058   //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0059   //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0060 
0061   // ----------member data ---------------------------
0062   edm::EDGetToken m_towerToken;
0063 
0064   std::string filename_;
0065   std::string outDir_;
0066 
0067   // constants
0068   unsigned nChan_;  // number of channels per quad
0069   unsigned nQuad_;
0070   unsigned nLink_;
0071   unsigned nHeaderFrames_;
0072   unsigned nPayloadFrames_;
0073   unsigned nClearFrames_;
0074   unsigned nFrame_;
0075   unsigned nEvents_;
0076 
0077   // data arranged by link and frame
0078   std::vector<std::vector<int> > data_;
0079 
0080   // data valid flags (just one per frame for now)
0081   std::vector<int> dataValid_;
0082 
0083   // map of towers onto links/frames
0084   std::map<int, int> map_;
0085 };
0086 
0087 //
0088 // constants, enums and typedefs
0089 //
0090 
0091 //
0092 // static data member definitions
0093 //
0094 
0095 //
0096 // constructors and destructor
0097 //
0098 L1TStage2InputPatternWriter::L1TStage2InputPatternWriter(const edm::ParameterSet& iConfig) {
0099   //now do what ever initialization is needed
0100 
0101   // register what you consume and keep token for later access:
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   // do anything here that needs to be done at desctruction time
0123   // (e.g. close files, deallocate resources etc.)
0124 }
0125 
0126 //
0127 // member functions
0128 //
0129 
0130 // ------------ method called for each event  ------------
0131 void L1TStage2InputPatternWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0132   using namespace edm;
0133 
0134   //count events
0135   nEvents_++;
0136 
0137   // get towers
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   // insert header frames
0148   for (unsigned iFrame = 0; iFrame < nHeaderFrames_; ++iFrame) {
0149     dataValid_.push_back(1);
0150 
0151     // loop over links
0152     for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0153       for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0154         int data = 0;
0155 
0156         // get tower ieta, iphi for link
0157         unsigned iLink = (iQuad * nChan_) + iChan;
0158 
0159         // add data to output
0160         data_.at(iLink).push_back(data);
0161       }
0162     }
0163 
0164     nFrame_++;
0165   }
0166 
0167   // loop over frames
0168   for (unsigned iFrame = 0; iFrame < nPayloadFrames_; ++iFrame) {
0169     dataValid_.push_back(1);
0170 
0171     // loop over links
0172     for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0173       for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0174         int data = 0;
0175 
0176         // get tower ieta, iphi for link
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         // get tower 1 data
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         // get tower 2
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         // add data to output
0196         data_.at(iLink).push_back(data);
0197       }
0198     }
0199 
0200     nFrame_++;
0201   }
0202 
0203   // loop over clear frames
0204   for (unsigned iFrame = 0; iFrame < nClearFrames_; ++iFrame) {
0205     dataValid_.push_back(0);
0206 
0207     // loop over links
0208     for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0209       for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0210         int data = 0;
0211 
0212         // get tower ieta, iphi for link
0213         unsigned iLink = (iQuad * nChan_) + iChan;
0214 
0215         // add data to output
0216         data_.at(iLink).push_back(data);
0217       }
0218     }
0219 
0220     nFrame_++;
0221   }
0222 }
0223 
0224 // ------------ method called once each job just before starting event loop  ------------
0225 void L1TStage2InputPatternWriter::beginJob() {}
0226 
0227 // ------------ method called once each job just after ending the event loop  ------------
0228 void L1TStage2InputPatternWriter::endJob() {
0229   //frames per event
0230   unsigned int framesPerEv = nHeaderFrames_ + nPayloadFrames_ + nClearFrames_;
0231 
0232   //events per file
0233   unsigned int evPerFile = floor(1024 / framesPerEv);
0234 
0235   //frames per file
0236   unsigned int framesPerFile = framesPerEv * evPerFile;
0237 
0238   //number of output files
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   //files
0247   std::vector<std::ofstream> outFiles(nOutFiles);
0248 
0249   //make output files and write to them
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     // quad/chan numbers
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     // link numbers
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     // then the data
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 // ------------ method called when starting to processes a run  ------------
0303 /*
0304 void 
0305 L1TStage2InputPatternWriter::beginRun(edm::Run const&, edm::EventSetup const&)
0306 {
0307 }
0308 */
0309 
0310 // ------------ method called when ending the processing of a run  ------------
0311 /*
0312 void 
0313 L1TStage2InputPatternWriter::endRun(edm::Run const&, edm::EventSetup const&)
0314 {
0315 }
0316 */
0317 
0318 // ------------ method called when starting to processes a luminosity block  ------------
0319 /*
0320 void 
0321 L1TStage2InputPatternWriter::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
0322 {
0323 }
0324 */
0325 
0326 // ------------ method called when ending the processing of a luminosity block  ------------
0327 /*
0328 void 
0329 L1TStage2InputPatternWriter::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
0330 {
0331 }
0332 */
0333 
0334 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0335 void L1TStage2InputPatternWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0336   //The following says we do not know what parameters are allowed so do no validation
0337   // Please change this to state exactly what you do use, even if it is no parameters
0338   edm::ParameterSetDescription desc;
0339   desc.setUnknown();
0340   descriptions.addDefault(desc);
0341 }
0342 
0343 //define this as a plug-in
0344 DEFINE_FWK_MODULE(L1TStage2InputPatternWriter);