Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:22:29

0001 // -*- C++ -*-
0002 //
0003 // Package:    L1Trigger/L1CaloTrigger
0004 // Class:      L1TS2PFJetInputPatternWriter
0005 //
0006 /**\class L1TS2PFJetInputPatternWriter L1TS2PFJetInputPatternWriter.cc L1Trigger/L1TCalorimeter/plugins/L1TS2PFJetInputPatternWriter.cc
0007 
0008    Description: 
0009 
0010    Implementation:
0011 
0012 */
0013 //
0014 // Original Author:  Aaron Bundock
0015 //         Created:  Fri, 26 Jul 2018 14:20:25 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 #include "FWCore/Framework/interface/Event.h"
0026 #include "FWCore/Framework/interface/MakerMacros.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/ServiceRegistry/interface/Service.h"
0029 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0030 #include "L1Trigger/L1TCalorimeter/interface/CaloTools.h"
0031 #include "DataFormats/L1TParticleFlow/interface/PFCandidate.h"
0032 
0033 #include <fstream>
0034 #include <iostream>
0035 #include <iomanip>
0036 #include <cmath>
0037 
0038 //
0039 // class declaration
0040 //
0041 
0042 constexpr unsigned int bit_shift_phi = 16;
0043 constexpr unsigned int bit_shift_eta = 26;
0044 constexpr unsigned int mod_13_1 = 1;
0045 constexpr unsigned int mod_13_2 = 2;
0046 constexpr unsigned int n_latency_clocks = 13;
0047 constexpr unsigned int n_first_empty_frames = 14;
0048 constexpr unsigned int framesPerFile = 1015;
0049 constexpr float eta_first_region_boundary = 0.75;
0050 constexpr float eta_second_region_boundary = 1.5;
0051 constexpr float phi_region_boundary = 0.7;
0052 constexpr unsigned int hash_bitmask_16 = 0xffff;  // (2^16)-1
0053 constexpr unsigned int hash_bitmask_10 = 0x3ff;   // (2^10)-1
0054 constexpr float ptLSB = 0.25;
0055 constexpr float etaLSB = 0.0043633231;
0056 constexpr float phiLSB = 0.0043633231;
0057 
0058 class L1TS2PFJetInputPatternWriter : public edm::one::EDAnalyzer<> {
0059 public:
0060   explicit L1TS2PFJetInputPatternWriter(const edm::ParameterSet&);
0061   ~L1TS2PFJetInputPatternWriter() override;
0062 
0063   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0064 
0065 private:
0066   void beginJob() override;
0067   void analyze(const edm::Event&, const edm::EventSetup&) override;
0068   void endJob() override;
0069 
0070   // ----------member data ---------------------------
0071   edm::EDGetTokenT<std::vector<l1t::PFCandidate>> pfToken_;
0072   std::string filename_;
0073   std::string outDir_;
0074 
0075   // constants
0076   unsigned nChan_;  // number of channels per quad
0077   unsigned nQuad_;
0078   unsigned nLink_;
0079   unsigned nHeaderFrames_;
0080   unsigned nPayloadFrames_;
0081   unsigned nClearFrames_;
0082   unsigned nFrame_;
0083   unsigned nFrameFile_;
0084   unsigned nEvents_;
0085 
0086   // data arranged by link and frame
0087   std::vector<std::vector<uint64_t>> data_;
0088 
0089   // data valid flags (just one per frame for now)
0090   std::vector<int> dataValid_;
0091 
0092   // map of towers onto links/frames
0093   std::map<int, int> map_;
0094 };
0095 
0096 //
0097 // constants, enums and typedefs
0098 //
0099 
0100 //
0101 // static data member definitions
0102 //
0103 
0104 //
0105 // constructors and destructor
0106 //
0107 L1TS2PFJetInputPatternWriter::L1TS2PFJetInputPatternWriter(const edm::ParameterSet& iConfig)
0108     : pfToken_(consumes<std::vector<l1t::PFCandidate>>(iConfig.getParameter<edm::InputTag>("pfTag"))),
0109       filename_(iConfig.getUntrackedParameter<std::string>("filename")),
0110       outDir_(iConfig.getUntrackedParameter<std::string>("outDir")),
0111       nChan_(iConfig.getUntrackedParameter<unsigned>("nChanPerQuad")),
0112       nQuad_(iConfig.getUntrackedParameter<unsigned>("nQuads")),
0113       nHeaderFrames_(iConfig.getUntrackedParameter<unsigned>("nHeaderFrames")),
0114       nPayloadFrames_(iConfig.getUntrackedParameter<unsigned>("nPayloadFrames")),
0115       nClearFrames_(iConfig.getUntrackedParameter<unsigned>("nClearFrames")) {
0116   //now do what ever initialization is needed
0117 
0118   // register what you consume and keep token for later access:
0119 
0120   nFrame_ = 0;
0121   nFrameFile_ = 0;
0122   nEvents_ = 0;
0123 
0124   nLink_ = nChan_ * nQuad_;
0125   data_.resize(nLink_);
0126   LogDebug("L1TDebug") << "Preparing for " << nLink_ << " links" << std::endl;
0127 }
0128 
0129 L1TS2PFJetInputPatternWriter::~L1TS2PFJetInputPatternWriter() {
0130   // do anything here that needs to be done at desctruction time
0131   // (e.g. close files, deallocate resources etc.)
0132 }
0133 
0134 //
0135 // member functions
0136 //
0137 
0138 // ------------ method called for each event  ------------
0139 void L1TS2PFJetInputPatternWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0140   using namespace edm;
0141 
0142   //count events
0143   nEvents_++;
0144 
0145   edm::Handle<std::vector<l1t::PFCandidate>> pfHandle;
0146   iEvent.getByToken(pfToken_, pfHandle);
0147   std::vector<l1t::PFCandidate> const& pfCands = *pfHandle;
0148 
0149   std::vector<l1t::PFCandidate> pfPartsA;
0150   std::vector<l1t::PFCandidate> pfPartsB;
0151 
0152   for (auto const& pfc : pfCands) {
0153     // select first two "small" regions for current fw
0154     if (pfc.eta() >= 0 && pfc.eta() < eta_first_region_boundary && pfc.phi() >= 0 && pfc.phi() < phi_region_boundary)
0155       pfPartsA.push_back(pfc);
0156     if (pfc.eta() >= eta_first_region_boundary && pfc.eta() < eta_second_region_boundary && pfc.phi() >= 0 &&
0157         pfc.phi() < phi_region_boundary)
0158       pfPartsB.push_back(pfc);
0159   }
0160 
0161   if (pfPartsA.empty() && pfPartsB.empty())
0162     return;
0163 
0164   if (nFrame_ == 0 || nFrameFile_ == 0) {
0165     //first empty frames
0166     while (nFrameFile_ < n_first_empty_frames) {
0167       dataValid_.push_back(1);
0168       for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0169         for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0170           uint iLink = (iQuad * nChan_) + iChan;
0171           data_.at(iLink).push_back(0);
0172         }
0173       }
0174       nFrame_++;
0175       nFrameFile_++;
0176     }
0177   }
0178 
0179   // loop over frames
0180   for (unsigned iFrame = 0; iFrame < nPayloadFrames_; ++iFrame) {
0181     dataValid_.push_back(1);
0182     // loop over links
0183     for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0184       for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0185         // get tower ieta, iphi for link
0186         uint iLink = (iQuad * nChan_) + iChan;
0187 
0188         uint64_t data = 0;
0189 
0190         if ((nFrameFile_ % n_latency_clocks) == mod_13_1) {
0191           if (iLink < 24 && pfPartsA.size() > iLink) {
0192             data |= ((uint64_t)floor(pfPartsA.at(iLink).pt() / ptLSB) & hash_bitmask_16);
0193             data |= ((uint64_t)floor(pfPartsA.at(iLink).phi() / phiLSB) & hash_bitmask_10) << bit_shift_phi;
0194             data |= ((uint64_t)floor(pfPartsA.at(iLink).eta() / etaLSB) & hash_bitmask_10) << bit_shift_eta;
0195           }
0196         }
0197         if ((nFrameFile_ % n_latency_clocks) == mod_13_2) {
0198           if (iLink < 24 && pfPartsB.size() > iLink) {
0199             data |= ((uint64_t)floor(pfPartsB.at(iLink).pt() / ptLSB) & hash_bitmask_16);
0200             data |= ((uint64_t)floor(pfPartsB.at(iLink).phi() / phiLSB) & hash_bitmask_10) << bit_shift_phi;
0201             data |= ((uint64_t)floor((pfPartsB.at(iLink).eta() - eta_first_region_boundary) / etaLSB) & hash_bitmask_10)
0202                     << bit_shift_eta;
0203           }
0204         }
0205         // add data to output
0206         data_.at(iLink).push_back(data);
0207       }
0208     }
0209     nFrame_++;
0210     nFrameFile_++;
0211     if (nFrame_ % framesPerFile == 0)
0212       nFrameFile_ = 0;
0213   }
0214 }
0215 
0216 // ------------ method called once each job just before starting event loop  ------------
0217 void L1TS2PFJetInputPatternWriter::beginJob() {}
0218 
0219 // ------------ method called once each job just after ending the event loop  ------------
0220 void L1TS2PFJetInputPatternWriter::endJob() {
0221   //frames per event
0222   unsigned int framesPerEv = nHeaderFrames_ + nPayloadFrames_ + nClearFrames_;
0223 
0224   //events per file
0225   unsigned int evPerFile = floor(framesPerFile / framesPerEv);
0226 
0227   //number of output files
0228   unsigned int nOutFiles = ceil((float)nEvents_ / (float)evPerFile);
0229 
0230   LogDebug("L1TDebug") << "Read " << nFrame_ << " frames" << std::endl;
0231   LogDebug("L1TDebug") << "Read " << nEvents_ << " events" << std::endl;
0232   LogDebug("L1TDebug") << "Writing " << nOutFiles << " files" << std::endl;
0233   LogDebug("L1TDebug") << "Output directory: ./" << outDir_ << "/" << std::endl;
0234 
0235   //files
0236   std::vector<std::ofstream> outFiles(nOutFiles);
0237 
0238   //make output files and write to them
0239   for (uint itFile = 0; itFile < nOutFiles; ++itFile) {
0240     std::stringstream outFilename;
0241     outFilename << outDir_ << "/" << filename_ << "_" << itFile << ".txt";
0242     outFiles[itFile] = std::ofstream(outFilename.str());
0243     LogDebug("L1TDebug") << "Writing to file: ./" << outFilename.str() << std::endl;
0244 
0245     outFiles[itFile] << "Board SRNTY_TEST" << std::endl;
0246 
0247     // quad/chan numbers
0248     outFiles[itFile] << " Quad/Chan :      ";
0249     for (unsigned i = 0; i < nQuad_; ++i) {
0250       for (unsigned j = 0; j < nChan_; ++j) {
0251         outFiles[itFile] << "  q" << setfill('0') << setw(2) << i << "c" << j << "            ";
0252       }
0253     }
0254     outFiles[itFile] << std::endl;
0255 
0256     // link numbers
0257     outFiles[itFile] << "      Link :     ";
0258     for (unsigned i = 0; i < nQuad_; ++i) {
0259       for (unsigned j = 0; j < nChan_; ++j) {
0260         outFiles[itFile] << "    " << setfill('0') << setw(2) << (i * nChan_) + j << "             ";
0261       }
0262     }
0263 
0264     outFiles[itFile] << std::endl;
0265 
0266     // then the data
0267     unsigned iFileFrame = 0;
0268     for (unsigned iFrame = itFile * framesPerFile; iFrame < (itFile * framesPerFile + framesPerFile); ++iFrame) {
0269       if (iFrame <= nFrame_ && iFrame < (framesPerEv * nEvents_)) {
0270         outFiles[itFile] << "Frame " << std::dec << std::setw(4) << std::setfill('0') << iFileFrame << " : ";
0271         for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
0272           for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
0273             unsigned iLink = (iQuad * nChan_) + iChan;
0274             if (iLink < data_.size() && iFrame < data_.at(iLink).size()) {
0275               outFiles[itFile] << std::hex << ::std::setw(1) << dataValid_.at(iFrame) << "v" << std::hex
0276                                << std::setw(16) << std::setfill('0') << data_.at(iLink).at(iFrame) << " ";
0277             } else {
0278               outFiles[itFile] << std::hex << ::std::setw(1) << 0 << "v" << std::hex << std::setw(16)
0279                                << std::setfill('0') << 0 << " ";
0280             }
0281           }
0282         }
0283       }
0284       outFiles[itFile] << std::endl;
0285       iFileFrame++;
0286     }
0287     outFiles[itFile].close();
0288   }
0289 }
0290 
0291 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0292 void L1TS2PFJetInputPatternWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0293   edm::ParameterSetDescription desc;
0294   desc.addUntracked<unsigned int>("nPayloadFrames", 40);
0295   desc.addUntracked<unsigned int>("nHeaderFrames", 1);
0296   desc.add<edm::InputTag>("inputCollectionTag", edm::InputTag("l1pfCandidates", "Puppi", "IN"));
0297   desc.addUntracked<std::string>("filename", "pattern.txt");
0298   desc.addUntracked<unsigned int>("nChanPerQuad", 4);
0299   desc.addUntracked<unsigned int>("nClearFrames", 13);
0300   desc.addUntracked<unsigned int>("nQuads", 18);
0301   descriptions.add("l1tS2PFJetInputPatternWriter", desc);
0302 }
0303 
0304 //define this as a plug-in
0305 DEFINE_FWK_MODULE(L1TS2PFJetInputPatternWriter);