Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-25 03:22:21

0001 // -*- C++ -*-
0002 //
0003 // Package:    L1Trigger/DemonstratorTools
0004 // Class:      GTTFileWriter
0005 //
0006 /**\class GTTFileWriter GTTFileWriter.cc L1Trigger/DemonstratorTools/plugins/GTTFileWriter.cc
0007 
0008  Description: Example EDAnalyzer class, illustrating how BoardDataWriter can be used to
0009    write I/O buffer files for hardware/firmware tests
0010 
0011  Implementation:
0012      [Notes on implementation]
0013 */
0014 //
0015 // Original Author:  Thomas Williams <thomas.williams@stfc.ac.uk>
0016 //         Created:  Mon, 15 Feb 2021 00:39:44 GMT
0017 //
0018 //
0019 
0020 // system include files
0021 #include <memory>
0022 
0023 #include "ap_int.h"
0024 
0025 // user include files
0026 #include "FWCore/Framework/interface/Frameworkfwd.h"
0027 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0028 
0029 #include "FWCore/Framework/interface/Event.h"
0030 #include "FWCore/Framework/interface/MakerMacros.h"
0031 
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "FWCore/Utilities/interface/InputTag.h"
0034 
0035 #include "DataFormats/Common/interface/Handle.h"
0036 #include "DataFormats/Common/interface/Ref.h"
0037 #include "DataFormats/Common/interface/View.h"
0038 #include "DataFormats/L1TrackTrigger/interface/TTTrack_TrackWord.h"
0039 #include "DataFormats/L1TrackTrigger/interface/TTTrack.h"
0040 #include "DataFormats/L1TrackTrigger/interface/TTTypes.h"
0041 #include "DataFormats/L1Trigger/interface/EtSum.h"
0042 #include "DataFormats/L1Trigger/interface/VertexWord.h"
0043 
0044 #include "L1Trigger/DemonstratorTools/interface/BoardDataWriter.h"
0045 #include "L1Trigger/DemonstratorTools/interface/codecs/tracks.h"
0046 #include "L1Trigger/DemonstratorTools/interface/codecs/vertices.h"
0047 #include "L1Trigger/DemonstratorTools/interface/codecs/tkjets.h"
0048 #include "L1Trigger/DemonstratorTools/interface/codecs/htsums.h"
0049 #include "L1Trigger/DemonstratorTools/interface/codecs/etsums.h"
0050 #include "L1Trigger/DemonstratorTools/interface/utilities.h"
0051 
0052 //
0053 // class declaration
0054 //
0055 
0056 class GTTFileWriter : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0057 public:
0058   explicit GTTFileWriter(const edm::ParameterSet&);
0059 
0060   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0061 
0062 private:
0063   // ----------constants, enums and typedefs ---------
0064   // NOTE: At least some of the info from these constants will eventually come from config files
0065   static constexpr size_t kFramesPerTMUXPeriod = 9;
0066   static constexpr size_t kGapLengthInput = 6;
0067   static constexpr size_t kGapLengthOutputToCorrelator = 44;
0068   static constexpr size_t kGapLengthOutputToGlobalTriggerSums = 3;
0069   static constexpr size_t kGapLengthOutputToGlobalTriggerTaus = 36;
0070   static constexpr size_t kGapLengthOutputToGlobalTriggerMesons = 15;
0071   static constexpr size_t kGapLengthOutputToGlobalTriggerVertices = 6;
0072   static constexpr size_t kTrackTMUX = 18;
0073   static constexpr size_t kGTTBoardTMUX = 6;
0074   static constexpr size_t kMaxLinesPerFile = 1024;
0075 
0076   const std::map<l1t::demo::LinkId, std::vector<size_t>> kChannelIdsInput = {
0077       /* logical channel within time slice -> vector of channel indices (one entry per time slice) */
0078       {{"tracks", 0}, {0, 18, 36}},
0079       {{"tracks", 1}, {1, 19, 37}},
0080       {{"tracks", 2}, {2, 20, 38}},
0081       {{"tracks", 3}, {3, 21, 39}},
0082       {{"tracks", 4}, {4, 22, 40}},
0083       {{"tracks", 5}, {5, 23, 41}},
0084       {{"tracks", 6}, {6, 24, 42}},
0085       {{"tracks", 7}, {7, 25, 43}},
0086       {{"tracks", 8}, {8, 26, 44}},
0087       {{"tracks", 9}, {9, 27, 45}},
0088       {{"tracks", 10}, {10, 28, 46}},
0089       {{"tracks", 11}, {11, 29, 47}},
0090       {{"tracks", 12}, {12, 30, 48}},
0091       {{"tracks", 13}, {13, 31, 49}},
0092       {{"tracks", 14}, {14, 32, 50}},
0093       {{"tracks", 15}, {15, 33, 51}},
0094       {{"tracks", 16}, {16, 34, 52}},
0095       {{"tracks", 17}, {17, 35, 53}}};
0096 
0097   const std::map<std::string, l1t::demo::ChannelSpec> kChannelSpecsInput = {
0098       /* interface name -> {link TMUX, inter-packet gap} */
0099       {"tracks", {kTrackTMUX, kGapLengthInput}}};
0100 
0101   const std::map<l1t::demo::LinkId, std::pair<l1t::demo::ChannelSpec, std::vector<size_t>>>
0102       kChannelSpecsOutputToCorrelator = {
0103           /* logical channel within time slice -> {{link TMUX, inter-packet gap}, vector of channel indices} */
0104           {{"vertices", 0}, {{kGTTBoardTMUX, kGapLengthOutputToCorrelator}, {0}}}};
0105 
0106   const std::map<l1t::demo::LinkId, std::vector<size_t>> kChannelIdsOutputToGlobalTrigger = {
0107       /* logical channel within time slice -> vector of channel indices (one entry per time slice) */
0108       {{"sums", 0}, {0}},
0109       {{"taus", 1}, {1}},
0110       {{"mesons", 2}, {2}},
0111       {{"vertices", 3}, {3}}};
0112 
0113   const std::map<std::string, l1t::demo::ChannelSpec> kChannelSpecsOutputToGlobalTrigger = {
0114       /* interface name -> {link TMUX, inter-packet gap} */
0115       {"sums", {kGTTBoardTMUX, kGapLengthOutputToGlobalTriggerSums}},
0116       {"taus", {kGTTBoardTMUX, kGapLengthOutputToGlobalTriggerTaus}},
0117       {"mesons", {kGTTBoardTMUX, kGapLengthOutputToGlobalTriggerMesons}},
0118       {"vertices", {kGTTBoardTMUX, kGapLengthOutputToGlobalTriggerVertices}}};
0119 
0120   typedef TTTrack<Ref_Phase2TrackerDigi_> Track_t;
0121   typedef std::vector<Track_t> TrackCollection_t;
0122   typedef edm::RefVector<TrackCollection_t> TrackRefCollection_t;
0123 
0124   // ----------member functions ----------------------
0125   void analyze(const edm::Event&, const edm::EventSetup&) override;
0126   void endJob() override;
0127 
0128   // ----------member data ---------------------------
0129   const edm::EDGetTokenT<edm::View<Track_t>> tracksToken_;
0130   const edm::EDGetTokenT<edm::View<Track_t>> convertedTracksToken_;
0131   const edm::EDGetTokenT<TrackCollection_t> convertedTrackCollectionToken_;
0132   const edm::EDGetTokenT<TrackRefCollection_t> selectedTracksToken_;
0133   const edm::EDGetTokenT<TrackRefCollection_t> vertexAssociatedTracksToken_;
0134   const edm::EDGetTokenT<edm::View<l1t::VertexWord>> verticesToken_;
0135   const edm::EDGetTokenT<edm::View<l1t::TkJetWord>> jetsToken_;
0136   const edm::EDGetTokenT<edm::View<l1t::EtSum>> htMissToken_;
0137   const edm::EDGetTokenT<edm::View<l1t::EtSum>> etMissToken_;
0138 
0139   l1t::demo::BoardDataWriter fileWriterInputTracks_;
0140   l1t::demo::BoardDataWriter fileWriterConvertedTracks_;
0141   l1t::demo::BoardDataWriter fileWriterSelectedTracks_;
0142   l1t::demo::BoardDataWriter fileWriterVertexAssociatedTracks_;
0143   l1t::demo::BoardDataWriter fileWriterOutputToCorrelator_;
0144   l1t::demo::BoardDataWriter fileWriterOutputToGlobalTrigger_;
0145 };
0146 
0147 //
0148 // class implementation
0149 //
0150 
0151 GTTFileWriter::GTTFileWriter(const edm::ParameterSet& iConfig)
0152     : tracksToken_(consumes<edm::View<Track_t>>(iConfig.getUntrackedParameter<edm::InputTag>("tracks"))),
0153       convertedTracksToken_(
0154           consumes<edm::View<Track_t>>(iConfig.getUntrackedParameter<edm::InputTag>("convertedTracks"))),
0155       convertedTrackCollectionToken_(
0156           consumes<TrackCollection_t>(iConfig.getUntrackedParameter<edm::InputTag>("convertedTracks"))),
0157       selectedTracksToken_(
0158           consumes<TrackRefCollection_t>(iConfig.getUntrackedParameter<edm::InputTag>("selectedTracks"))),
0159       vertexAssociatedTracksToken_(
0160           consumes<TrackRefCollection_t>(iConfig.getUntrackedParameter<edm::InputTag>("vertexAssociatedTracks"))),
0161       verticesToken_(consumes<edm::View<l1t::VertexWord>>(iConfig.getUntrackedParameter<edm::InputTag>("vertices"))),
0162       jetsToken_(consumes<edm::View<l1t::TkJetWord>>(iConfig.getUntrackedParameter<edm::InputTag>("jets"))),
0163       htMissToken_(consumes<edm::View<l1t::EtSum>>(iConfig.getUntrackedParameter<edm::InputTag>("htmiss"))),
0164       etMissToken_(consumes<edm::View<l1t::EtSum>>(iConfig.getUntrackedParameter<edm::InputTag>("etmiss"))),
0165       fileWriterInputTracks_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0166                              iConfig.getUntrackedParameter<std::string>("inputFilename"),
0167                              kFramesPerTMUXPeriod,
0168                              kGTTBoardTMUX,
0169                              kMaxLinesPerFile,
0170                              kChannelIdsInput,
0171                              kChannelSpecsInput),
0172       fileWriterConvertedTracks_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0173                                  iConfig.getUntrackedParameter<std::string>("inputConvertedFilename"),
0174                                  kFramesPerTMUXPeriod,
0175                                  kGTTBoardTMUX,
0176                                  kMaxLinesPerFile,
0177                                  kChannelIdsInput,
0178                                  kChannelSpecsInput),
0179       fileWriterSelectedTracks_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0180                                 iConfig.getUntrackedParameter<std::string>("selectedTracksFilename"),
0181                                 kFramesPerTMUXPeriod,
0182                                 kGTTBoardTMUX,
0183                                 kMaxLinesPerFile,
0184                                 kChannelIdsInput,
0185                                 kChannelSpecsInput),
0186       fileWriterVertexAssociatedTracks_(
0187           l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0188           iConfig.getUntrackedParameter<std::string>("vertexAssociatedTracksFilename"),
0189           kFramesPerTMUXPeriod,
0190           kGTTBoardTMUX,
0191           kMaxLinesPerFile,
0192           kChannelIdsInput,
0193           kChannelSpecsInput),
0194       fileWriterOutputToCorrelator_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0195                                     iConfig.getUntrackedParameter<std::string>("outputCorrelatorFilename"),
0196                                     kFramesPerTMUXPeriod,
0197                                     kGTTBoardTMUX,
0198                                     kMaxLinesPerFile,
0199                                     kChannelSpecsOutputToCorrelator),
0200       fileWriterOutputToGlobalTrigger_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0201                                        iConfig.getUntrackedParameter<std::string>("outputGlobalTriggerFilename"),
0202                                        kFramesPerTMUXPeriod,
0203                                        kGTTBoardTMUX,
0204                                        kMaxLinesPerFile,
0205                                        kChannelIdsOutputToGlobalTrigger,
0206                                        kChannelSpecsOutputToGlobalTrigger) {}
0207 
0208 void GTTFileWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0209   using namespace edm;
0210   using namespace l1t::demo::codecs;
0211 
0212   // 0) Gather the necessary collections
0213   const auto& tracksCollection = iEvent.get(tracksToken_);
0214   const auto& convertedTracksCollection = iEvent.get(convertedTracksToken_);
0215   const auto& verticesCollection = iEvent.get(verticesToken_);
0216   const auto& jetsCollection = iEvent.get(jetsToken_);
0217   const auto& htMissCollection = iEvent.get(htMissToken_);
0218   const auto& etMissCollection = iEvent.get(etMissToken_);
0219 
0220   edm::Handle<TrackCollection_t> convertedTracksHandle;
0221   edm::Handle<TrackRefCollection_t> selectedTracksHandle;
0222   edm::Handle<TrackRefCollection_t> vertexAssociatedTracksHandle;
0223   iEvent.getByToken(convertedTrackCollectionToken_, convertedTracksHandle);
0224   iEvent.getByToken(selectedTracksToken_, selectedTracksHandle);
0225   iEvent.getByToken(vertexAssociatedTracksToken_, vertexAssociatedTracksHandle);
0226 
0227   // 1) Encode 'object' information onto vectors containing link data
0228   const auto trackData(encodeTracks(tracksCollection));
0229   const auto convertedTrackData(encodeTracks(convertedTracksCollection));
0230   const auto selectedTrackData(encodeTracks(convertedTracksHandle, selectedTracksHandle));
0231   const auto vertexAssociatedTrackData(encodeTracks(convertedTracksHandle, vertexAssociatedTracksHandle));
0232   const auto vertexData(encodeVertices(verticesCollection));
0233   const auto jetsData(encodeTkJets(jetsCollection));
0234   const auto htMissData(encodeHtSums(htMissCollection));
0235   const auto etMissData(encodeEtSums(etMissCollection));
0236 
0237   // 2) Pack 'object' information into 'event data' object
0238   l1t::demo::EventData eventDataTracks;
0239   l1t::demo::EventData eventDataConvertedTracks;
0240   l1t::demo::EventData eventDataSelectedTracks;
0241   l1t::demo::EventData eventDataVertexAssociatedTracks;
0242   for (size_t i = 0; i < 18; i++) {
0243     eventDataTracks.add({"tracks", i}, trackData.at(i));
0244     eventDataConvertedTracks.add({"tracks", i}, convertedTrackData.at(i));
0245     eventDataSelectedTracks.add({"tracks", i}, selectedTrackData.at(i));
0246     eventDataVertexAssociatedTracks.add({"tracks", i}, vertexAssociatedTrackData.at(i));
0247   }
0248 
0249   l1t::demo::EventData eventDataVertices;
0250   eventDataVertices.add({"vertices", 0}, vertexData.at(0));
0251 
0252   // 2b) For the global trigger 'event data' combine different objects into one 'logical' link
0253   std::vector<ap_uint<64>> sumsData;
0254   sumsData.insert(sumsData.end(), jetsData.at(0).begin(), jetsData.at(0).end());
0255   sumsData.insert(sumsData.end(), 24, 0);
0256   sumsData.insert(sumsData.end(), htMissData.at(0).begin(), htMissData.at(0).end());
0257   sumsData.insert(sumsData.end(), 1, 0);
0258   sumsData.insert(sumsData.end(), etMissData.at(0).begin(), etMissData.at(0).end());
0259 
0260   std::vector<ap_uint<64>> tracksVerticesData;
0261   tracksVerticesData.insert(tracksVerticesData.end(), 36, 0);
0262   tracksVerticesData.insert(tracksVerticesData.end(), vertexData.at(0).begin(), vertexData.at(0).end());
0263   tracksVerticesData.insert(tracksVerticesData.end(), 2, 0);
0264 
0265   l1t::demo::EventData eventDataGlobalTrigger;
0266   eventDataGlobalTrigger.add({"sums", 0}, sumsData);
0267   eventDataGlobalTrigger.add({"taus", 1}, std::vector<ap_uint<64>>(18, 0));  // Placeholder until tau object is written
0268   eventDataGlobalTrigger.add({"mesons", 2},
0269                              std::vector<ap_uint<64>>(39, 0));  // Placeholder until light meson objects are written
0270   eventDataGlobalTrigger.add({"vertices", 3}, tracksVerticesData);
0271 
0272   // 3) Pass the 'event data' object to the file writer
0273 
0274   fileWriterInputTracks_.addEvent(eventDataTracks);
0275   fileWriterConvertedTracks_.addEvent(eventDataConvertedTracks);
0276   fileWriterSelectedTracks_.addEvent(eventDataSelectedTracks);
0277   fileWriterVertexAssociatedTracks_.addEvent(eventDataVertexAssociatedTracks);
0278   fileWriterOutputToCorrelator_.addEvent(eventDataVertices);
0279   fileWriterOutputToGlobalTrigger_.addEvent(eventDataGlobalTrigger);
0280 }
0281 
0282 // ------------ method called once each job just after ending the event loop  ------------
0283 void GTTFileWriter::endJob() {
0284   // Writing pending events to file before exiting
0285   fileWriterInputTracks_.flush();
0286   fileWriterConvertedTracks_.flush();
0287   fileWriterOutputToCorrelator_.flush();
0288   fileWriterOutputToGlobalTrigger_.flush();
0289 }
0290 
0291 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0292 void GTTFileWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0293   // GTTFileWriter
0294   edm::ParameterSetDescription desc;
0295   desc.addUntracked<edm::InputTag>("tracks", edm::InputTag("l1tTTTracksFromTrackletEmulation", "Level1TTTracks"));
0296   desc.addUntracked<edm::InputTag>("convertedTracks", edm::InputTag("l1tGTTInputProducer", "Level1TTTracksConverted"));
0297   desc.addUntracked<edm::InputTag>("selectedTracks",
0298                                    edm::InputTag("l1tTrackSelectionProducer", "Level1TTTracksSelectedEmulation"));
0299   desc.addUntracked<edm::InputTag>(
0300       "vertexAssociatedTracks",
0301       edm::InputTag("l1tTrackSelectionProducer", "Level1TTTracksSelectedAssociatedEmulation"));
0302   desc.addUntracked<edm::InputTag>("vertices", edm::InputTag("l1tVertexProducer", "l1verticesEmulation"));
0303   desc.addUntracked<edm::InputTag>("jets", edm::InputTag("l1tTrackJetsEmulation", "L1TrackJets"));
0304   desc.addUntracked<edm::InputTag>("htmiss", edm::InputTag("l1tTrackerEmuHTMiss", "L1TrackerEmuHTMiss"));
0305   desc.addUntracked<edm::InputTag>("etmiss", edm::InputTag("l1tTrackerEmuEtMiss", "L1TrackerEmuEtMiss"));
0306   desc.addUntracked<std::string>("inputFilename", "L1GTTInputFile");
0307   desc.addUntracked<std::string>("inputConvertedFilename", "L1GTTInputConvertedFile");
0308   desc.addUntracked<std::string>("selectedTracksFilename", "L1GTTSelectedTracksFile");
0309   desc.addUntracked<std::string>("vertexAssociatedTracksFilename", "L1GTTVertexAssociatedTracksFile");
0310   desc.addUntracked<std::string>("outputCorrelatorFilename", "L1GTTOutputToCorrelatorFile");
0311   desc.addUntracked<std::string>("outputGlobalTriggerFilename", "L1GTTOutputToGlobalTriggerFile");
0312   desc.addUntracked<std::string>("format", "APx");
0313   descriptions.add("GTTFileWriter", desc);
0314 }
0315 
0316 //define this as a plug-in
0317 DEFINE_FWK_MODULE(GTTFileWriter);