Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-23 10:17:44

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