Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-10 01:53:52

0001 #include <memory>
0002 
0003 // user include files
0004 #include "FWCore/Framework/interface/Frameworkfwd.h"
0005 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0006 
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 
0013 #include "DataFormats/Common/interface/View.h"
0014 
0015 #include "L1Trigger/DemonstratorTools/interface/BoardDataWriter.h"
0016 #include "L1Trigger/DemonstratorTools/interface/utilities.h"
0017 #include "DataFormats/L1TParticleFlow/interface/PFJet.h"
0018 #include "DataFormats/L1TParticleFlow/interface/gt_datatypes.h"
0019 
0020 //
0021 // class declaration
0022 //
0023 
0024 class L1CTJetFileWriter : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0025 public:
0026   explicit L1CTJetFileWriter(const edm::ParameterSet&);
0027 
0028   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0029 
0030 private:
0031   // ----------constants, enums and typedefs ---------
0032   unsigned nJets_;
0033   size_t nFramesPerBX_;
0034   size_t ctl2BoardTMUX_;
0035   size_t gapLengthOutput_;
0036   size_t maxLinesPerFile_;
0037   std::map<l1t::demo::LinkId, std::pair<l1t::demo::ChannelSpec, std::vector<size_t>>> channelSpecsOutputToGT_;
0038 
0039   // ----------member functions ----------------------
0040   void analyze(const edm::Event&, const edm::EventSetup&) override;
0041   void endJob() override;
0042   std::vector<ap_uint<64>> encodeJets(const std::vector<l1t::PFJet> jets);
0043 
0044   edm::EDGetTokenT<edm::View<l1t::PFJet>> jetsToken_;
0045   l1t::demo::BoardDataWriter fileWriterOutputToGT_;
0046 };
0047 
0048 L1CTJetFileWriter::L1CTJetFileWriter(const edm::ParameterSet& iConfig)
0049     : nJets_(iConfig.getParameter<unsigned>("nJets")),
0050       nFramesPerBX_(iConfig.getParameter<unsigned>("nFramesPerBX")),
0051       ctl2BoardTMUX_(iConfig.getParameter<unsigned>("TMUX")),
0052       gapLengthOutput_(ctl2BoardTMUX_ * nFramesPerBX_ - 2 * nJets_),
0053       maxLinesPerFile_(iConfig.getParameter<unsigned>("maxLinesPerFile")),
0054       channelSpecsOutputToGT_{{{"jets", 0}, {{ctl2BoardTMUX_, gapLengthOutput_}, {0}}}},
0055       jetsToken_(consumes<edm::View<l1t::PFJet>>(iConfig.getParameter<edm::InputTag>("jets"))),
0056       fileWriterOutputToGT_(l1t::demo::parseFileFormat(iConfig.getParameter<std::string>("format")),
0057                             iConfig.getParameter<std::string>("outputFilename"),
0058                             nFramesPerBX_,
0059                             ctl2BoardTMUX_,
0060                             maxLinesPerFile_,
0061                             channelSpecsOutputToGT_) {}
0062 
0063 void L1CTJetFileWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0064   using namespace edm;
0065 
0066   // 1) Encode jet information onto vectors containing link data
0067   // TODO remove the sort here and sort the input collection where it's created
0068   const edm::View<l1t::PFJet>& jets = iEvent.get(jetsToken_);
0069   std::vector<l1t::PFJet> sortedJets;
0070   sortedJets.reserve(jets.size());
0071   std::copy(jets.begin(), jets.end(), std::back_inserter(sortedJets));
0072 
0073   std::stable_sort(
0074       sortedJets.begin(), sortedJets.end(), [](l1t::PFJet i, l1t::PFJet j) { return (i.hwPt() > j.hwPt()); });
0075   const auto outputJets(encodeJets(sortedJets));
0076 
0077   // 2) Pack jet information into 'event data' object, and pass that to file writer
0078   l1t::demo::EventData eventDataJets;
0079   eventDataJets.add({"jets", 0}, outputJets);
0080   fileWriterOutputToGT_.addEvent(eventDataJets);
0081 }
0082 
0083 // ------------ method called once each job just after ending the event loop  ------------
0084 void L1CTJetFileWriter::endJob() {
0085   // Writing pending events to file before exiting
0086   fileWriterOutputToGT_.flush();
0087 }
0088 
0089 std::vector<ap_uint<64>> L1CTJetFileWriter::encodeJets(const std::vector<l1t::PFJet> jets) {
0090   std::vector<ap_uint<64>> jet_words;
0091   for (unsigned i = 0; i < nJets_; i++) {
0092     l1t::PFJet j;
0093     if (i < jets.size()) {
0094       j = jets.at(i);
0095     } else {  // pad up to nJets_ with null jets
0096       l1t::PFJet j(0, 0, 0, 0, 0, 0);
0097     }
0098     jet_words.push_back(j.encodedJet()[0]);
0099     jet_words.push_back(j.encodedJet()[1]);
0100   }
0101   return jet_words;
0102 }
0103 
0104 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0105 void L1CTJetFileWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0106   //The following says we do not know what parameters are allowed so do no validation
0107   // Please change this to state exactly what you do use, even if it is no parameters
0108   edm::ParameterSetDescription desc;
0109   desc.add<edm::InputTag>("jets");
0110   desc.add<std::string>("outputFilename");
0111   desc.add<uint32_t>("nJets", 12);
0112   desc.add<uint32_t>("nFramesPerBX", 9);
0113   desc.add<uint32_t>("TMUX", 6);
0114   desc.add<uint32_t>("maxLinesPerFile", 1024);
0115   desc.add<std::string>("format", "EMP");
0116   descriptions.addDefault(desc);
0117 }
0118 
0119 //define this as a plug-in
0120 DEFINE_FWK_MODULE(L1CTJetFileWriter);