Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-01-20 02:57:50

0001 #include <memory>
0002 #include <numeric>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0007 
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/Utilities/interface/InputTag.h"
0013 
0014 #include "DataFormats/Common/interface/View.h"
0015 
0016 #include "L1Trigger/DemonstratorTools/interface/BoardDataWriter.h"
0017 #include "L1Trigger/DemonstratorTools/interface/utilities.h"
0018 #include "DataFormats/L1TParticleFlow/interface/PFJet.h"
0019 #include "DataFormats/L1TParticleFlow/interface/gt_datatypes.h"
0020 #include "DataFormats/L1Trigger/interface/EtSum.h"
0021 
0022 //
0023 // class declaration
0024 //
0025 
0026 class L1CTJetFileWriter : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0027 public:
0028   explicit L1CTJetFileWriter(const edm::ParameterSet&);
0029 
0030   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0031 
0032 private:
0033   // ----------constants, enums and typedefs ---------
0034   std::vector<edm::ParameterSet> collections_;
0035 
0036   size_t nFramesPerBX_;
0037   size_t ctl2BoardTMUX_;
0038   size_t gapLengthOutput_;
0039   size_t maxLinesPerFile_;
0040   std::map<l1t::demo::LinkId, std::pair<l1t::demo::ChannelSpec, std::vector<size_t>>> channelSpecsOutputToGT_;
0041 
0042   // ----------member functions ----------------------
0043   void analyze(const edm::Event&, const edm::EventSetup&) override;
0044   void endJob() override;
0045   std::vector<ap_uint<64>> encodeJets(const std::vector<l1t::PFJet> jets, unsigned nJets);
0046   std::vector<ap_uint<64>> encodeSums(const std::vector<l1t::EtSum> sums, unsigned nSums);
0047 
0048   l1t::demo::BoardDataWriter fileWriterOutputToGT_;
0049   std::vector<std::pair<edm::EDGetTokenT<edm::View<l1t::PFJet>>, edm::EDGetTokenT<edm::View<l1t::EtSum>>>> tokens_;
0050   std::vector<std::pair<bool, bool>> tokensToWrite_;
0051   std::vector<unsigned> nJets_;
0052   std::vector<unsigned> nSums_;
0053 };
0054 
0055 L1CTJetFileWriter::L1CTJetFileWriter(const edm::ParameterSet& iConfig)
0056     : collections_(iConfig.getParameter<std::vector<edm::ParameterSet>>("collections")),
0057       nFramesPerBX_(iConfig.getParameter<unsigned>("nFramesPerBX")),
0058       ctl2BoardTMUX_(iConfig.getParameter<unsigned>("TMUX")),
0059       gapLengthOutput_(iConfig.getParameter<unsigned>("gapLengthOutput")),
0060       maxLinesPerFile_(iConfig.getParameter<unsigned>("maxLinesPerFile")),
0061       channelSpecsOutputToGT_{{{"jets", 0}, {{ctl2BoardTMUX_, gapLengthOutput_}, {0}}}},
0062       fileWriterOutputToGT_(l1t::demo::parseFileFormat(iConfig.getParameter<std::string>("format")),
0063                             iConfig.getParameter<std::string>("outputFilename"),
0064                             iConfig.getParameter<std::string>("outputFileExtension"),
0065                             nFramesPerBX_,
0066                             ctl2BoardTMUX_,
0067                             maxLinesPerFile_,
0068                             channelSpecsOutputToGT_) {
0069   for (const auto& pset : collections_) {
0070     edm::EDGetTokenT<edm::View<l1t::PFJet>> jetToken;
0071     edm::EDGetTokenT<edm::View<l1t::EtSum>> mhtToken;
0072     unsigned nJets = pset.getParameter<unsigned>("nJets");
0073     unsigned nSums = pset.getParameter<unsigned>("nSums");
0074     nJets_.push_back(nJets);
0075     nSums_.push_back(nSums);
0076     bool writeJetToken(false), writeMhtToken(false);
0077     if (nJets > 0) {
0078       jetToken = consumes<edm::View<l1t::PFJet>>(pset.getParameter<edm::InputTag>("jets"));
0079       writeJetToken = true;
0080     }
0081     if (nSums > 0) {
0082       mhtToken = consumes<edm::View<l1t::EtSum>>(pset.getParameter<edm::InputTag>("mht"));
0083       writeMhtToken = true;
0084     }
0085     tokens_.emplace_back(jetToken, mhtToken);
0086     tokensToWrite_.emplace_back(writeJetToken, writeMhtToken);
0087   }
0088 }
0089 
0090 void L1CTJetFileWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0091   using namespace edm;
0092 
0093   // 1) Pack collections in the order they're specified. jets then sums within collection
0094   std::vector<ap_uint<64>> link_words;
0095   for (unsigned iCollection = 0; iCollection < collections_.size(); iCollection++) {
0096     if (tokensToWrite_.at(iCollection).first) {
0097       const auto& jetToken = tokens_.at(iCollection).first;
0098       // 2) Encode jet information onto vectors containing link data
0099       const edm::View<l1t::PFJet>& jets = iEvent.get(jetToken);
0100       std::vector<l1t::PFJet> sortedJets;
0101       sortedJets.reserve(jets.size());
0102       std::copy(jets.begin(), jets.end(), std::back_inserter(sortedJets));
0103 
0104       std::stable_sort(
0105           sortedJets.begin(), sortedJets.end(), [](l1t::PFJet i, l1t::PFJet j) { return (i.hwPt() > j.hwPt()); });
0106       const auto outputJets(encodeJets(sortedJets, nJets_.at(iCollection)));
0107       link_words.insert(link_words.end(), outputJets.begin(), outputJets.end());
0108     }
0109 
0110     if (tokensToWrite_.at(iCollection).second) {
0111       // 3) Encode sums onto vectors containing link data
0112       const auto& mhtToken = tokens_.at(iCollection).second;
0113       const edm::View<l1t::EtSum>& mht = iEvent.get(mhtToken);
0114       std::vector<l1t::EtSum> orderedSums;
0115       std::copy(mht.begin(), mht.end(), std::back_inserter(orderedSums));
0116       const auto outputSums(encodeSums(orderedSums, nSums_.at(iCollection)));
0117       link_words.insert(link_words.end(), outputSums.begin(), outputSums.end());
0118     }
0119   }
0120   // 4) Pack jet information into 'event data' object, and pass that to file writer
0121   l1t::demo::EventData eventDataJets;
0122   eventDataJets.add({"jets", 0}, link_words);
0123   fileWriterOutputToGT_.addEvent(eventDataJets);
0124 }
0125 
0126 // ------------ method called once each job just after ending the event loop  ------------
0127 void L1CTJetFileWriter::endJob() {
0128   // Writing pending events to file before exiting
0129   fileWriterOutputToGT_.flush();
0130 }
0131 
0132 std::vector<ap_uint<64>> L1CTJetFileWriter::encodeJets(const std::vector<l1t::PFJet> jets, const unsigned nJets) {
0133   // Encode up to nJets jets, padded with 0s
0134   std::vector<ap_uint<64>> jet_words(2 * nJets, 0);  // allocate 2 words per jet
0135   for (unsigned i = 0; i < std::min(nJets, (uint)jets.size()); i++) {
0136     const l1t::PFJet& j = jets.at(i);
0137     jet_words[2 * i] = j.encodedJet()[0];
0138     jet_words[2 * i + 1] = j.encodedJet()[1];
0139   }
0140   return jet_words;
0141 }
0142 
0143 std::vector<ap_uint<64>> L1CTJetFileWriter::encodeSums(const std::vector<l1t::EtSum> sums, unsigned nSums) {
0144   // Need two l1t::EtSum for each GT Sum
0145   std::vector<ap_uint<64>> sum_words;
0146   for (unsigned i = 0; i < nSums; i++) {
0147     if (2 * i < sums.size()) {
0148       l1gt::Sum gtSum;
0149       gtSum.valid = 1;  // if the sums are sent at all, they are valid
0150       gtSum.vector_pt.V = sums.at(2 * i + 1).hwPt();
0151       gtSum.vector_phi.V = sums.at(2 * i + 1).hwPhi();
0152       gtSum.scalar_pt.V = sums.at(2 * i).hwPt();
0153       sum_words.push_back(gtSum.pack_ap());
0154     } else {
0155       sum_words.push_back(0);
0156     }
0157   }
0158   return sum_words;
0159 }
0160 
0161 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0162 void L1CTJetFileWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0163   edm::ParameterSetDescription desc;
0164   {
0165     edm::ParameterSetDescription vpsd1;
0166     vpsd1.addOptional<edm::InputTag>("jets");
0167     vpsd1.addOptional<edm::InputTag>("mht");
0168     vpsd1.add<uint>("nJets", 0);
0169     vpsd1.add<uint>("nSums", 0);
0170     desc.addVPSet("collections", vpsd1);
0171   }
0172   desc.add<std::string>("outputFilename");
0173   desc.add<std::string>("outputFileExtension", "txt");
0174   desc.add<uint32_t>("nJets", 12);
0175   desc.add<uint32_t>("nFramesPerBX", 9);
0176   desc.add<uint32_t>("gapLengthOutput", 4);
0177   desc.add<uint32_t>("TMUX", 6);
0178   desc.add<uint32_t>("maxLinesPerFile", 1024);
0179   desc.add<std::string>("format", "EMPv2");
0180   descriptions.addDefault(desc);
0181 }
0182 
0183 //define this as a plug-in
0184 DEFINE_FWK_MODULE(L1CTJetFileWriter);