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 #include "L1Trigger/TrackFindingTracklet/interface/ChannelAssignment.h"
0016 
0017 #include <string>
0018 #include <vector>
0019 #include <deque>
0020 #include <iterator>
0021 #include <cmath>
0022 #include <numeric>
0023 
0024 using namespace std;
0025 using namespace edm;
0026 using namespace trackerTFP;
0027 using namespace tt;
0028 
0029 namespace trklet {
0030 
0031   /*! \class  trklet::ProducerTBout
0032    *  \brief  Transforms TTTracks and Streams from Tracklet pattern reco. into StreamsTrack
0033    *          by adding to the digitised track stream a reference to the corresponding TTTrack.
0034    *          (Could not be done in previous L1TrackFPGAProducer, as single EDProducer can't
0035    *          produce output containing both an EDProduct and refs to that product).
0036    *          Writes Tracks & stubs rejected/kept after truncation to separate StreamsTrack & StreamsStub branches.
0037    *  \author Thomas Schuh
0038    *  \date   2021, Oct
0039    */
0040   class ProducerTBout : public stream::EDProducer<> {
0041   public:
0042     explicit ProducerTBout(const ParameterSet&);
0043     ~ProducerTBout() override {}
0044 
0045   private:
0046     void beginRun(const Run&, const EventSetup&) override;
0047     void produce(Event&, const EventSetup&) override;
0048     virtual void endJob() {}
0049 
0050     // ED input token of TTTracks
0051     EDGetTokenT<TTTracks> edGetTokenTTTracks_;
0052     // ED input token of Tracklet tracks
0053     EDGetTokenT<Streams> edGetTokenTracks_;
0054     // ED input token of Tracklet Stubs
0055     EDGetTokenT<StreamsStub> edGetTokenStubs_;
0056     // ED output token for stubs
0057     EDPutTokenT<StreamsStub> edPutTokenAcceptedStubs_;
0058     EDPutTokenT<StreamsStub> edPutTokenLostStubs_;
0059     // ED output token for tracks
0060     EDPutTokenT<StreamsTrack> edPutTokenAcceptedTracks_;
0061     EDPutTokenT<StreamsTrack> edPutTokenLostTracks_;
0062     // Setup token
0063     ESGetToken<Setup, SetupRcd> esGetTokenSetup_;
0064     // DataFormats token
0065     ESGetToken<DataFormats, DataFormatsRcd> esGetTokenDataFormats_;
0066     // ChannelAssignment token
0067     ESGetToken<ChannelAssignment, ChannelAssignmentRcd> esGetTokenChannelAssignment_;
0068     // configuration
0069     ParameterSet iConfig_;
0070     // helper class to store configurations
0071     const Setup* setup_ = nullptr;
0072     // helper class to extract structured data from tt::Frames
0073     const DataFormats* dataFormats_ = nullptr;
0074     // helper class to assign tracks to channel
0075     ChannelAssignment* channelAssignment_ = nullptr;
0076     //
0077     bool enableTruncation_;
0078   };
0079 
0080   ProducerTBout::ProducerTBout(const ParameterSet& iConfig) : iConfig_(iConfig) {
0081     const InputTag& inputTag = iConfig.getParameter<InputTag>("InputTag");
0082     const string& branchAcceptedStubs = iConfig.getParameter<string>("BranchAcceptedStubs");
0083     const string& branchAcceptedTracks = iConfig.getParameter<string>("BranchAcceptedTracks");
0084     const string& branchLostStubs = iConfig.getParameter<string>("BranchLostStubs");
0085     const string& branchLostTracks = iConfig.getParameter<string>("BranchLostTracks");
0086     // book in- and output ED products
0087     edGetTokenTTTracks_ = consumes<TTTracks>(inputTag);
0088     edGetTokenTracks_ = consumes<Streams>(inputTag);
0089     edGetTokenStubs_ = consumes<StreamsStub>(inputTag);
0090     edPutTokenAcceptedStubs_ = produces<StreamsStub>(branchAcceptedStubs);
0091     edPutTokenAcceptedTracks_ = produces<StreamsTrack>(branchAcceptedTracks);
0092     edPutTokenLostStubs_ = produces<StreamsStub>(branchLostStubs);
0093     edPutTokenLostTracks_ = produces<StreamsTrack>(branchLostTracks);
0094     // book ES products
0095     esGetTokenSetup_ = esConsumes<Setup, SetupRcd, Transition::BeginRun>();
0096     esGetTokenDataFormats_ = esConsumes<DataFormats, DataFormatsRcd, Transition::BeginRun>();
0097     esGetTokenChannelAssignment_ = esConsumes<ChannelAssignment, ChannelAssignmentRcd, Transition::BeginRun>();
0098     //
0099     enableTruncation_ = iConfig.getParameter<bool>("EnableTruncation");
0100   }
0101 
0102   void ProducerTBout::beginRun(const Run& iRun, const EventSetup& iSetup) {
0103     // helper class to store configurations
0104     setup_ = &iSetup.getData(esGetTokenSetup_);
0105     if (!setup_->configurationSupported())
0106       return;
0107     // check process history if desired
0108     if (iConfig_.getParameter<bool>("CheckHistory"))
0109       setup_->checkHistory(iRun.processHistory());
0110     // helper class to extract structured data from tt::Frames
0111     dataFormats_ = &iSetup.getData(esGetTokenDataFormats_);
0112     // helper class to assign tracks to channel
0113     channelAssignment_ = const_cast<ChannelAssignment*>(&iSetup.getData(esGetTokenChannelAssignment_));
0114   }
0115 
0116   void ProducerTBout::produce(Event& iEvent, const EventSetup& iSetup) {
0117     const int numStreamsTracks = setup_->numRegions() * channelAssignment_->numChannelsTrack();
0118     const int numStreamsStubs = setup_->numRegions() * channelAssignment_->numChannelsStub();
0119     // empty KFin products
0120     StreamsStub streamAcceptedStubs(numStreamsStubs);
0121     StreamsTrack streamAcceptedTracks(numStreamsTracks);
0122     StreamsStub streamLostStubs(numStreamsStubs);
0123     StreamsTrack streamLostTracks(numStreamsTracks);
0124     // read in hybrid track finding product and produce KFin product
0125     if (setup_->configurationSupported()) {
0126       // create and structure TTrackRefs in h/w channel
0127       vector<deque<TTTrackRef>> ttTrackRefs(numStreamsTracks);
0128       Handle<TTTracks> handleTTTracks;
0129       iEvent.getByToken<TTTracks>(edGetTokenTTTracks_, handleTTTracks);
0130       int channelId(-1);
0131       for (int i = 0; i < (int)handleTTTracks->size(); i++) {
0132         const TTTrackRef ttTrackRef(handleTTTracks, i);
0133         const int channelId = channelAssignment_->channelId(ttTrackRef);
0134         ttTrackRefs[channelId].push_back(ttTrackRef);
0135       }
0136       // get and trunacte tracks
0137       Handle<Streams> handleTracks;
0138       iEvent.getByToken<Streams>(edGetTokenTracks_, handleTracks);
0139       channelId = 0;
0140       for (const Stream& streamTrack : *handleTracks) {
0141         const int nTracks = accumulate(
0142             streamTrack.begin(), streamTrack.end(), 0, [](int sum, const Frame& f) { return sum + (f.any() ? 1 : 0); });
0143         StreamTrack& accepted = streamAcceptedTracks[channelId];
0144         StreamTrack& lost = streamLostTracks[channelId];
0145         auto limit = streamTrack.end();
0146         if (enableTruncation_ && (int)streamTrack.size() > setup_->numFrames())
0147           limit = next(streamTrack.begin(), setup_->numFrames());
0148         accepted.reserve(distance(streamTrack.begin(), limit));
0149         lost.reserve(distance(limit, streamTrack.end()));
0150         int nFrame(0);
0151         const deque<TTTrackRef>& ttTracks = ttTrackRefs[channelId++];
0152         if ((int)ttTracks.size() != nTracks) {
0153           cms::Exception exception("LogicError.");
0154           const int region = channelId / channelAssignment_->numChannelsTrack();
0155           const int channel = channelId % channelAssignment_->numChannelsTrack();
0156           exception << "Region " << region << " output channel " << channel << " has " << nTracks
0157                     << " tracks found but created " << ttTracks.size() << " TTTracks.";
0158           exception.addContext("trklet::ProducerTBout::produce");
0159           throw exception;
0160         }
0161         auto toFrameTrack = [&nFrame, &ttTracks](const Frame& frame) {
0162           if (frame.any())
0163             return FrameTrack(ttTracks[nFrame++], frame);
0164           return FrameTrack();
0165         };
0166         transform(streamTrack.begin(), limit, back_inserter(accepted), toFrameTrack);
0167         transform(limit, streamTrack.end(), back_inserter(lost), toFrameTrack);
0168       }
0169       // get and trunacte stubs
0170       Handle<StreamsStub> handleStubs;
0171       iEvent.getByToken<StreamsStub>(edGetTokenStubs_, handleStubs);
0172       const StreamsStub& streamsStub = *handleStubs;
0173       // reserve output ed products
0174       channelId = 0;
0175       for (const StreamStub& streamStub : streamsStub) {
0176         auto limit = streamStub.end();
0177         if (enableTruncation_ && (int)streamStub.size() > setup_->numFrames())
0178           limit = next(streamStub.begin(), setup_->numFrames());
0179         streamAcceptedStubs[channelId] = StreamStub(streamStub.begin(), limit);
0180         streamLostStubs[channelId++] = StreamStub(limit, streamStub.end());
0181       }
0182     }
0183     // store products
0184     iEvent.emplace(edPutTokenAcceptedStubs_, std::move(streamAcceptedStubs));
0185     iEvent.emplace(edPutTokenAcceptedTracks_, std::move(streamAcceptedTracks));
0186     iEvent.emplace(edPutTokenLostStubs_, std::move(streamLostStubs));
0187     iEvent.emplace(edPutTokenLostTracks_, std::move(streamLostTracks));
0188   }
0189 
0190 }  // namespace trklet
0191 
0192 DEFINE_FWK_MODULE(trklet::ProducerTBout);