Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:57

0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002 #include "FWCore/Framework/interface/Run.h"
0003 #include "FWCore/Framework/interface/EventSetup.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/Utilities/interface/EDGetToken.h"
0007 #include "FWCore/Utilities/interface/EDPutToken.h"
0008 #include "FWCore/Utilities/interface/ESGetToken.h"
0009 #include "FWCore/Utilities/interface/InputTag.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "DataFormats/Common/interface/Handle.h"
0012 
0013 #include "L1Trigger/TrackTrigger/interface/Setup.h"
0014 #include "L1Trigger/TrackerTFP/interface/DataFormats.h"
0015 
0016 #include <string>
0017 #include <numeric>
0018 
0019 using namespace std;
0020 using namespace edm;
0021 using namespace trackerTFP;
0022 using namespace tt;
0023 
0024 namespace trklet {
0025 
0026   /*! \class  trklet::ProducerTT
0027    *  \brief  Converts KF output into TTTracks
0028    *  \author Thomas Schuh
0029    *  \date   2021, Aug
0030    */
0031   class ProducerTT : public stream::EDProducer<> {
0032   public:
0033     explicit ProducerTT(const ParameterSet&);
0034     ~ProducerTT() override {}
0035 
0036   private:
0037     void beginRun(const Run&, const EventSetup&) override;
0038     void produce(Event&, const EventSetup&) override;
0039     void endJob() {}
0040 
0041     // ED input token of kf stubs
0042     EDGetTokenT<StreamsStub> edGetTokenStubs_;
0043     // ED input token of kf tracks
0044     EDGetTokenT<StreamsTrack> edGetTokenTracks_;
0045     // ED output token for TTTracks
0046     EDPutTokenT<TTTracks> edPutToken_;
0047     // Setup token
0048     ESGetToken<Setup, SetupRcd> esGetTokenSetup_;
0049     // DataFormats token
0050     ESGetToken<DataFormats, DataFormatsRcd> esGetTokenDataFormats_;
0051     // configuration
0052     ParameterSet iConfig_;
0053     // helper class to store configurations
0054     const Setup* setup_ = nullptr;
0055     // helper class to extract structured data from tt::Frames
0056     const DataFormats* dataFormats_ = nullptr;
0057   };
0058 
0059   ProducerTT::ProducerTT(const ParameterSet& iConfig) : iConfig_(iConfig) {
0060     const string& label = iConfig.getParameter<string>("LabelKF");
0061     const string& branchStubs = iConfig.getParameter<string>("BranchAcceptedStubs");
0062     const string& branchTracks = iConfig.getParameter<string>("BranchAcceptedTracks");
0063     // book in- and output ED products
0064     edGetTokenStubs_ = consumes<StreamsStub>(InputTag(label, branchStubs));
0065     edGetTokenTracks_ = consumes<StreamsTrack>(InputTag(label, branchTracks));
0066     edPutToken_ = produces<TTTracks>(branchTracks);
0067     // book ES products
0068     esGetTokenSetup_ = esConsumes<Setup, SetupRcd, Transition::BeginRun>();
0069     esGetTokenDataFormats_ = esConsumes<DataFormats, DataFormatsRcd, Transition::BeginRun>();
0070   }
0071 
0072   void ProducerTT::beginRun(const Run& iRun, const EventSetup& iSetup) {
0073     // helper class to store configurations
0074     setup_ = &iSetup.getData(esGetTokenSetup_);
0075     if (!setup_->configurationSupported())
0076       return;
0077     // check process history if desired
0078     if (iConfig_.getParameter<bool>("CheckHistory"))
0079       setup_->checkHistory(iRun.processHistory());
0080     // helper class to extract structured data from tt::Frames
0081     dataFormats_ = &iSetup.getData(esGetTokenDataFormats_);
0082   }
0083 
0084   void ProducerTT::produce(Event& iEvent, const EventSetup& iSetup) {
0085     // empty KFout product
0086     TTTracks ttTracks;
0087     // read in KF Product and produce KFout product
0088     if (setup_->configurationSupported()) {
0089       Handle<StreamsStub> handleStubs;
0090       iEvent.getByToken<StreamsStub>(edGetTokenStubs_, handleStubs);
0091       const StreamsStub& streamsStubs = *handleStubs.product();
0092       Handle<StreamsTrack> handleTracks;
0093       iEvent.getByToken<StreamsTrack>(edGetTokenTracks_, handleTracks);
0094       const StreamsTrack& streamsTracks = *handleTracks.product();
0095       // count number of kf tracks
0096       int nTracks(0);
0097       for (const StreamTrack& stream : streamsTracks)
0098         nTracks += accumulate(stream.begin(), stream.end(), 0, [](int sum, const FrameTrack& frame) {
0099           return sum + (frame.first.isNonnull() ? 1 : 0);
0100         });
0101       ttTracks.reserve(nTracks);
0102       // convert kf track frames per channel and stub frames per channel and layer to TTTracks
0103       for (int channel = 0; channel < (int)streamsTracks.size(); channel++) {
0104         const int offset = channel * setup_->numLayers();
0105         int iTrk(0);
0106         for (const FrameTrack& frameTrack : streamsTracks[channel]) {
0107           if (frameTrack.first.isNull())
0108             continue;
0109           // convert stub frames to kf stubs
0110           vector<StubKF> stubs;
0111           stubs.reserve(setup_->numLayers());
0112           for (int layer = 0; layer < setup_->numLayers(); layer++) {
0113             const FrameStub& frameStub = streamsStubs[offset + layer][iTrk];
0114             if (frameStub.first.isNonnull())
0115               stubs.emplace_back(frameStub, dataFormats_, layer);
0116           }
0117           // convert track frame to kf track
0118           TrackKF track(frameTrack, dataFormats_);
0119           // convert kf track and kf stubs to TTTrack
0120           ttTracks.emplace_back(track.ttTrack(stubs));
0121           iTrk++;
0122         }
0123       }
0124     }
0125     // store products
0126     iEvent.emplace(edPutToken_, std::move(ttTracks));
0127   }
0128 
0129 }  // namespace trklet
0130 
0131 DEFINE_FWK_MODULE(trklet::ProducerTT);