Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-03-05 07:04:19

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/GTTInterface.h"
0046 #include "L1Trigger/DemonstratorTools/interface/codecs/tracks.h"
0047 #include "L1Trigger/DemonstratorTools/interface/codecs/vertices.h"
0048 #include "L1Trigger/DemonstratorTools/interface/codecs/tkjets.h"
0049 #include "L1Trigger/DemonstratorTools/interface/codecs/htsums.h"
0050 #include "L1Trigger/DemonstratorTools/interface/codecs/etsums.h"
0051 #include "L1Trigger/DemonstratorTools/interface/utilities.h"
0052 
0053 //
0054 // class declaration
0055 //
0056 
0057 class GTTFileWriter : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0058 public:
0059   explicit GTTFileWriter(const edm::ParameterSet&);
0060 
0061   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0062 
0063 private:
0064   // ----------constants, enums and typedefs ---------
0065 
0066   typedef TTTrack<Ref_Phase2TrackerDigi_> Track_t;
0067   typedef std::vector<Track_t> TrackCollection_t;
0068   typedef edm::RefVector<TrackCollection_t> TrackRefCollection_t;
0069 
0070   // ----------member functions ----------------------
0071   void analyze(const edm::Event&, const edm::EventSetup&) override;
0072   void endJob() override;
0073 
0074   // ----------member data ---------------------------
0075   const edm::EDGetTokenT<edm::View<Track_t>> tracksToken_;
0076   const edm::EDGetTokenT<edm::View<Track_t>> convertedTracksToken_;
0077   const edm::EDGetTokenT<TrackCollection_t> convertedTrackCollectionToken_;
0078   const edm::EDGetTokenT<TrackRefCollection_t> selectedTracksToken_;
0079   const edm::EDGetTokenT<TrackRefCollection_t> vertexAssociatedTracksToken_;
0080   const edm::EDGetTokenT<edm::View<l1t::VertexWord>> verticesToken_;
0081   const edm::EDGetTokenT<edm::View<l1t::TkJetWord>> jetsToken_;
0082   const edm::EDGetTokenT<edm::View<l1t::EtSum>> htMissToken_;
0083   const edm::EDGetTokenT<edm::View<l1t::EtSum>> etMissToken_;
0084 
0085   l1t::demo::BoardDataWriter fileWriterInputTracks_;
0086   l1t::demo::BoardDataWriter fileWriterConvertedTracks_;
0087   l1t::demo::BoardDataWriter fileWriterSelectedTracks_;
0088   l1t::demo::BoardDataWriter fileWriterVertexAssociatedTracks_;
0089   l1t::demo::BoardDataWriter fileWriterOutputToCorrelator_;
0090   l1t::demo::BoardDataWriter fileWriterOutputToGlobalTrigger_;
0091 };
0092 
0093 //
0094 // class implementation
0095 //
0096 
0097 GTTFileWriter::GTTFileWriter(const edm::ParameterSet& iConfig)
0098     : tracksToken_(consumes<edm::View<Track_t>>(iConfig.getUntrackedParameter<edm::InputTag>("tracks"))),
0099       convertedTracksToken_(
0100           consumes<edm::View<Track_t>>(iConfig.getUntrackedParameter<edm::InputTag>("convertedTracks"))),
0101       convertedTrackCollectionToken_(
0102           consumes<TrackCollection_t>(iConfig.getUntrackedParameter<edm::InputTag>("convertedTracks"))),
0103       selectedTracksToken_(
0104           consumes<TrackRefCollection_t>(iConfig.getUntrackedParameter<edm::InputTag>("selectedTracks"))),
0105       vertexAssociatedTracksToken_(
0106           consumes<TrackRefCollection_t>(iConfig.getUntrackedParameter<edm::InputTag>("vertexAssociatedTracks"))),
0107       verticesToken_(consumes<edm::View<l1t::VertexWord>>(iConfig.getUntrackedParameter<edm::InputTag>("vertices"))),
0108       jetsToken_(consumes<edm::View<l1t::TkJetWord>>(iConfig.getUntrackedParameter<edm::InputTag>("jets"))),
0109       htMissToken_(consumes<edm::View<l1t::EtSum>>(iConfig.getUntrackedParameter<edm::InputTag>("htmiss"))),
0110       etMissToken_(consumes<edm::View<l1t::EtSum>>(iConfig.getUntrackedParameter<edm::InputTag>("etmiss"))),
0111       fileWriterInputTracks_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0112                              iConfig.getUntrackedParameter<std::string>("inputFilename"),
0113                              iConfig.getUntrackedParameter<std::string>("fileExtension"),
0114                              l1t::demo::gtt::kFramesPerTMUXPeriod,
0115                              l1t::demo::gtt::kGTTBoardTMUX,
0116                              l1t::demo::gtt::kMaxLinesPerFile,
0117                              l1t::demo::gtt::kChannelIdsInput,
0118                              l1t::demo::gtt::kChannelSpecsInput),
0119       fileWriterConvertedTracks_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0120                                  iConfig.getUntrackedParameter<std::string>("inputConvertedFilename"),
0121                                  iConfig.getUntrackedParameter<std::string>("fileExtension"),
0122                                  l1t::demo::gtt::kFramesPerTMUXPeriod,
0123                                  l1t::demo::gtt::kGTTBoardTMUX,
0124                                  l1t::demo::gtt::kMaxLinesPerFile,
0125                                  l1t::demo::gtt::kChannelIdsInput,
0126                                  l1t::demo::gtt::kChannelSpecsInput),
0127       fileWriterSelectedTracks_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0128                                 iConfig.getUntrackedParameter<std::string>("selectedTracksFilename"),
0129                                 iConfig.getUntrackedParameter<std::string>("fileExtension"),
0130                                 l1t::demo::gtt::kFramesPerTMUXPeriod,
0131                                 l1t::demo::gtt::kGTTBoardTMUX,
0132                                 l1t::demo::gtt::kMaxLinesPerFile,
0133                                 l1t::demo::gtt::kChannelIdsInput,
0134                                 l1t::demo::gtt::kChannelSpecsInput),
0135       fileWriterVertexAssociatedTracks_(
0136           l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0137           iConfig.getUntrackedParameter<std::string>("vertexAssociatedTracksFilename"),
0138           iConfig.getUntrackedParameter<std::string>("fileExtension"),
0139           l1t::demo::gtt::kFramesPerTMUXPeriod,
0140           l1t::demo::gtt::kGTTBoardTMUX,
0141           l1t::demo::gtt::kMaxLinesPerFile,
0142           l1t::demo::gtt::kChannelIdsInput,
0143           l1t::demo::gtt::kChannelSpecsInput),
0144       fileWriterOutputToCorrelator_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0145                                     iConfig.getUntrackedParameter<std::string>("outputCorrelatorFilename"),
0146                                     iConfig.getUntrackedParameter<std::string>("fileExtension"),
0147                                     l1t::demo::gtt::kFramesPerTMUXPeriod,
0148                                     l1t::demo::gtt::kGTTBoardTMUX,
0149                                     l1t::demo::gtt::kMaxLinesPerFile,
0150                                     l1t::demo::gtt::kChannelIdsOutputToCorrelator,
0151                                     l1t::demo::gtt::kChannelSpecsOutputToCorrelator),
0152       fileWriterOutputToGlobalTrigger_(l1t::demo::parseFileFormat(iConfig.getUntrackedParameter<std::string>("format")),
0153                                        iConfig.getUntrackedParameter<std::string>("outputGlobalTriggerFilename"),
0154                                        iConfig.getUntrackedParameter<std::string>("fileExtension"),
0155                                        l1t::demo::gtt::kFramesPerTMUXPeriod,
0156                                        l1t::demo::gtt::kGTTBoardTMUX,
0157                                        l1t::demo::gtt::kMaxLinesPerFile,
0158                                        l1t::demo::gtt::kChannelIdsOutputToGlobalTrigger,
0159                                        l1t::demo::gtt::kChannelSpecsOutputToGlobalTrigger) {}
0160 
0161 void GTTFileWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0162   using namespace edm;
0163   using namespace l1t::demo::codecs;
0164 
0165   // 0) Gather the necessary collections
0166   const auto& tracksCollection = iEvent.get(tracksToken_);
0167   const auto& convertedTracksCollection = iEvent.get(convertedTracksToken_);
0168   const auto& verticesCollection = iEvent.get(verticesToken_);
0169   const auto& jetsCollection = iEvent.get(jetsToken_);
0170   const auto& htMissCollection = iEvent.get(htMissToken_);
0171   const auto& etMissCollection = iEvent.get(etMissToken_);
0172 
0173   edm::Handle<TrackCollection_t> convertedTracksHandle;
0174   edm::Handle<TrackRefCollection_t> selectedTracksHandle;
0175   edm::Handle<TrackRefCollection_t> vertexAssociatedTracksHandle;
0176   iEvent.getByToken(convertedTrackCollectionToken_, convertedTracksHandle);
0177   iEvent.getByToken(selectedTracksToken_, selectedTracksHandle);
0178   iEvent.getByToken(vertexAssociatedTracksToken_, vertexAssociatedTracksHandle);
0179 
0180   // 1) Encode 'object' information onto vectors containing link data
0181   const auto trackData(encodeTracks(tracksCollection));
0182   const auto convertedTrackData(encodeTracks(convertedTracksCollection));
0183   const auto selectedTrackData(encodeTracks(convertedTracksHandle, selectedTracksHandle));
0184   const auto vertexAssociatedTrackData(encodeTracks(convertedTracksHandle, vertexAssociatedTracksHandle));
0185   const auto vertexData(encodeVertices(verticesCollection));
0186   const auto jetsData(encodeTkJets(jetsCollection));
0187   const auto htMissData(encodeHtSums(htMissCollection));
0188   const auto etMissData(encodeEtSums(etMissCollection));
0189 
0190   // 2) Pack 'object' information into 'event data' object
0191   l1t::demo::EventData eventDataTracks;
0192   l1t::demo::EventData eventDataConvertedTracks;
0193   l1t::demo::EventData eventDataSelectedTracks;
0194   l1t::demo::EventData eventDataVertexAssociatedTracks;
0195   for (size_t i = 0; i < 18; i++) {
0196     eventDataTracks.add({"tracks", i}, trackData.at(i));
0197     eventDataConvertedTracks.add({"tracks", i}, convertedTrackData.at(i));
0198     eventDataSelectedTracks.add({"tracks", i}, selectedTrackData.at(i));
0199     eventDataVertexAssociatedTracks.add({"tracks", i}, vertexAssociatedTrackData.at(i));
0200   }
0201 
0202   l1t::demo::EventData eventDataVertices;
0203   eventDataVertices.add({"vertices", 0}, vertexData.at(0));
0204 
0205   // 2b) For the global trigger 'event data' combine different objects into one 'logical' link
0206   std::vector<ap_uint<64>> sumsData;
0207   sumsData.insert(sumsData.end(), jetsData.at(0).begin(), jetsData.at(0).end());
0208   sumsData.insert(sumsData.end(), 24, 0);
0209   sumsData.insert(sumsData.end(), htMissData.at(0).begin(), htMissData.at(0).end());
0210   sumsData.insert(sumsData.end(), 1, 0);
0211   sumsData.insert(sumsData.end(), etMissData.at(0).begin(), etMissData.at(0).end());
0212 
0213   std::vector<ap_uint<64>> tracksVerticesData;
0214   tracksVerticesData.insert(tracksVerticesData.end(), 36, 0);
0215   tracksVerticesData.insert(tracksVerticesData.end(), vertexData.at(0).begin(), vertexData.at(0).end());
0216   tracksVerticesData.insert(tracksVerticesData.end(), 2, 0);
0217 
0218   l1t::demo::EventData eventDataGlobalTrigger;
0219   eventDataGlobalTrigger.add({"sums", 0}, sumsData);
0220   eventDataGlobalTrigger.add({"taus", 1}, std::vector<ap_uint<64>>(18, 0));  // Placeholder until tau object is written
0221   eventDataGlobalTrigger.add({"mesons", 2},
0222                              std::vector<ap_uint<64>>(39, 0));  // Placeholder until light meson objects are written
0223   eventDataGlobalTrigger.add({"vertices", 3}, tracksVerticesData);
0224 
0225   // 3) Pass the 'event data' object to the file writer
0226 
0227   fileWriterInputTracks_.addEvent(eventDataTracks);
0228   fileWriterConvertedTracks_.addEvent(eventDataConvertedTracks);
0229   fileWriterSelectedTracks_.addEvent(eventDataSelectedTracks);
0230   fileWriterVertexAssociatedTracks_.addEvent(eventDataVertexAssociatedTracks);
0231   fileWriterOutputToCorrelator_.addEvent(eventDataVertices);
0232   fileWriterOutputToGlobalTrigger_.addEvent(eventDataGlobalTrigger);
0233 }
0234 
0235 // ------------ method called once each job just after ending the event loop  ------------
0236 void GTTFileWriter::endJob() {
0237   // Writing pending events to file before exiting
0238   fileWriterInputTracks_.flush();
0239   fileWriterConvertedTracks_.flush();
0240   fileWriterOutputToCorrelator_.flush();
0241   fileWriterOutputToGlobalTrigger_.flush();
0242 }
0243 
0244 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0245 void GTTFileWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0246   // GTTFileWriter
0247   edm::ParameterSetDescription desc;
0248   desc.addUntracked<edm::InputTag>("tracks", edm::InputTag("l1tTTTracksFromTrackletEmulation", "Level1TTTracks"));
0249   desc.addUntracked<edm::InputTag>("convertedTracks", edm::InputTag("l1tGTTInputProducer", "Level1TTTracksConverted"));
0250   desc.addUntracked<edm::InputTag>("selectedTracks",
0251                                    edm::InputTag("l1tTrackSelectionProducer", "Level1TTTracksSelectedEmulation"));
0252   desc.addUntracked<edm::InputTag>(
0253       "vertexAssociatedTracks",
0254       edm::InputTag("l1tTrackSelectionProducer", "Level1TTTracksSelectedAssociatedEmulation"));
0255   desc.addUntracked<edm::InputTag>("vertices", edm::InputTag("l1tVertexProducer", "L1VerticesEmulation"));
0256   desc.addUntracked<edm::InputTag>("jets", edm::InputTag("l1tTrackJetsEmulation", "L1TrackJets"));
0257   desc.addUntracked<edm::InputTag>("htmiss", edm::InputTag("l1tTrackerEmuHTMiss", "L1TrackerEmuHTMiss"));
0258   desc.addUntracked<edm::InputTag>("etmiss", edm::InputTag("l1tTrackerEmuEtMiss", "L1TrackerEmuEtMiss"));
0259   desc.addUntracked<std::string>("inputFilename", "L1GTTInputFile");
0260   desc.addUntracked<std::string>("inputConvertedFilename", "L1GTTInputConvertedFile");
0261   desc.addUntracked<std::string>("selectedTracksFilename", "L1GTTSelectedTracksFile");
0262   desc.addUntracked<std::string>("vertexAssociatedTracksFilename", "L1GTTVertexAssociatedTracksFile");
0263   desc.addUntracked<std::string>("outputCorrelatorFilename", "L1GTTOutputToCorrelatorFile");
0264   desc.addUntracked<std::string>("outputGlobalTriggerFilename", "L1GTTOutputToGlobalTriggerFile");
0265   desc.addUntracked<std::string>("format", "APx");
0266   desc.addUntracked<std::string>("fileExtension", "txt");
0267   descriptions.add("GTTFileWriter", desc);
0268 }
0269 
0270 //define this as a plug-in
0271 DEFINE_FWK_MODULE(GTTFileWriter);