File indexing completed on 2024-04-06 12:21:44
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 "DataFormats/L1TrackTrigger/interface/TTTypes.h"
0014 #include "L1Trigger/TrackTrigger/interface/Setup.h"
0015 #include "L1Trigger/TrackerTFP/interface/DataFormats.h"
0016 #include "L1Trigger/TrackerTFP/interface/LayerEncoding.h"
0017
0018 #include <string>
0019 #include <vector>
0020 #include <deque>
0021 #include <iterator>
0022 #include <cmath>
0023 #include <numeric>
0024 #include <algorithm>
0025
0026 using namespace std;
0027 using namespace edm;
0028 using namespace tt;
0029
0030 namespace trackerTFP {
0031
0032
0033
0034
0035
0036
0037 class ProducerKFin : public stream::EDProducer<> {
0038 public:
0039 explicit ProducerKFin(const ParameterSet&);
0040 ~ProducerKFin() override {}
0041
0042 private:
0043 void beginRun(const Run&, const EventSetup&) override;
0044 void produce(Event&, const EventSetup&) override;
0045 virtual void endJob() {}
0046
0047
0048 EDGetTokenT<vector<TTTrack<Ref_Phase2TrackerDigi_>>> edGetTokenTTTracks_;
0049
0050 EDGetTokenT<StreamsStub> edGetTokenStubs_;
0051
0052 EDPutTokenT<StreamsStub> edPutTokenAcceptedStubs_;
0053 EDPutTokenT<StreamsStub> edPutTokenLostStubs_;
0054
0055 EDPutTokenT<StreamsTrack> edPutTokenAcceptedTracks_;
0056 EDPutTokenT<StreamsTrack> edPutTokenLostTracks_;
0057
0058 ESGetToken<Setup, SetupRcd> esGetTokenSetup_;
0059
0060 ESGetToken<DataFormats, DataFormatsRcd> esGetTokenDataFormats_;
0061
0062 ESGetToken<LayerEncoding, LayerEncodingRcd> esGetTokenLayerEncoding_;
0063
0064 ParameterSet iConfig_;
0065
0066 const Setup* setup_ = nullptr;
0067
0068 const DataFormats* dataFormats_ = nullptr;
0069
0070 const LayerEncoding* layerEncoding_ = nullptr;
0071
0072 bool enableTruncation_;
0073 };
0074
0075 ProducerKFin::ProducerKFin(const ParameterSet& iConfig) : iConfig_(iConfig) {
0076 const string& labelTTTracks = iConfig.getParameter<string>("LabelZHTout");
0077 const string& labelStubs = iConfig.getParameter<string>("LabelZHT");
0078 const string& branchAcceptedStubs = iConfig.getParameter<string>("BranchAcceptedStubs");
0079 const string& branchAcceptedTracks = iConfig.getParameter<string>("BranchAcceptedTracks");
0080 const string& branchLostStubs = iConfig.getParameter<string>("BranchLostStubs");
0081 const string& branchLostTracks = iConfig.getParameter<string>("BranchLostTracks");
0082
0083 edGetTokenTTTracks_ =
0084 consumes<vector<TTTrack<Ref_Phase2TrackerDigi_>>>(InputTag(labelTTTracks, branchAcceptedTracks));
0085 edGetTokenStubs_ = consumes<StreamsStub>(InputTag(labelStubs, branchAcceptedStubs));
0086 edPutTokenAcceptedStubs_ = produces<StreamsStub>(branchAcceptedStubs);
0087 edPutTokenAcceptedTracks_ = produces<StreamsTrack>(branchAcceptedTracks);
0088 edPutTokenLostStubs_ = produces<StreamsStub>(branchLostStubs);
0089 edPutTokenLostTracks_ = produces<StreamsTrack>(branchLostTracks);
0090
0091 esGetTokenSetup_ = esConsumes<Setup, SetupRcd, Transition::BeginRun>();
0092 esGetTokenDataFormats_ = esConsumes<DataFormats, DataFormatsRcd, Transition::BeginRun>();
0093 esGetTokenLayerEncoding_ = esConsumes<LayerEncoding, LayerEncodingRcd, Transition::BeginRun>();
0094
0095 enableTruncation_ = iConfig.getParameter<bool>("EnableTruncation");
0096 }
0097
0098 void ProducerKFin::beginRun(const Run& iRun, const EventSetup& iSetup) {
0099
0100 setup_ = &iSetup.getData(esGetTokenSetup_);
0101 if (!setup_->configurationSupported())
0102 return;
0103
0104 if (iConfig_.getParameter<bool>("CheckHistory"))
0105 setup_->checkHistory(iRun.processHistory());
0106
0107 dataFormats_ = &iSetup.getData(esGetTokenDataFormats_);
0108
0109 layerEncoding_ = &iSetup.getData(esGetTokenLayerEncoding_);
0110 }
0111
0112 void ProducerKFin::produce(Event& iEvent, const EventSetup& iSetup) {
0113 const DataFormat& dfcot = dataFormats_->format(Variable::cot, Process::kfin);
0114 const DataFormat& dfzT = dataFormats_->format(Variable::zT, Process::kfin);
0115 const DataFormat& dfinv2R = dataFormats_->format(Variable::inv2R, Process::kfin);
0116 const DataFormat& dfdPhi = dataFormats_->format(Variable::dPhi, Process::kfin);
0117 const DataFormat& dfdZ = dataFormats_->format(Variable::dZ, Process::kfin);
0118
0119 StreamsStub streamAcceptedStubs(dataFormats_->numStreamsStubs(Process::kf));
0120 StreamsTrack streamAcceptedTracks(dataFormats_->numStreamsTracks(Process::kf));
0121 StreamsStub streamLostStubs(dataFormats_->numStreamsStubs(Process::kf));
0122 StreamsTrack streamLostTracks(dataFormats_->numStreamsTracks(Process::kf));
0123
0124 if (setup_->configurationSupported()) {
0125 Handle<StreamsStub> handleStubs;
0126 iEvent.getByToken<StreamsStub>(edGetTokenStubs_, handleStubs);
0127 const StreamsStub& streams = *handleStubs.product();
0128 Handle<vector<TTTrack<Ref_Phase2TrackerDigi_>>> handleTTTracks;
0129 iEvent.getByToken<vector<TTTrack<Ref_Phase2TrackerDigi_>>>(edGetTokenTTTracks_, handleTTTracks);
0130 const vector<TTTrack<Ref_Phase2TrackerDigi_>>& ttTracks = *handleTTTracks.product();
0131 for (int region = 0; region < setup_->numRegions(); region++) {
0132
0133 int nStubsZHR(0);
0134 for (int channel = 0; channel < dataFormats_->numChannel(Process::zht); channel++) {
0135 const int index = region * dataFormats_->numChannel(Process::zht) + channel;
0136 const StreamStub& stream = streams[index];
0137 nStubsZHR += accumulate(stream.begin(), stream.end(), 0, [](int sum, const FrameStub& frame) {
0138 return sum + (frame.first.isNonnull() ? 1 : 0);
0139 });
0140 }
0141 vector<StubZHT> stubsZHT;
0142 stubsZHT.reserve(nStubsZHR);
0143 for (int channel = 0; channel < dataFormats_->numChannel(Process::zht); channel++) {
0144 const int index = region * dataFormats_->numChannel(Process::zht) + channel;
0145 for (const FrameStub& frame : streams[index])
0146 if (frame.first.isNonnull())
0147 stubsZHT.emplace_back(frame, dataFormats_);
0148 }
0149 vector<deque<FrameStub>> dequesStubs(dataFormats_->numChannel(Process::kf) * setup_->numLayers());
0150 vector<deque<FrameTrack>> dequesTracks(dataFormats_->numChannel(Process::kf));
0151 int i(0);
0152 for (const TTTrack<Ref_Phase2TrackerDigi_>& ttTrack : ttTracks) {
0153 if ((int)ttTrack.phiSector() / setup_->numSectorsPhi() != region) {
0154 i++;
0155 continue;
0156 }
0157 const int sectorPhi = ttTrack.phiSector() % setup_->numSectorsPhi();
0158 deque<FrameTrack>& tracks = dequesTracks[sectorPhi];
0159 const int binEta = ttTrack.etaSector();
0160 const int binZT = dfzT.toUnsigned(dfzT.integer(ttTrack.z0()));
0161 const int binCot = dfcot.toUnsigned(dfcot.integer(ttTrack.tanL()));
0162 StubZHT* stubZHT = nullptr;
0163 vector<int> layerCounts(setup_->numLayers(), 0);
0164 for (const TTStubRef& ttStubRef : ttTrack.getStubRefs()) {
0165 const int layerId = setup_->layerId(ttStubRef);
0166 const int layerIdKF = layerEncoding_->layerIdKF(binEta, binZT, binCot, layerId);
0167 if (layerIdKF == -1)
0168 continue;
0169 if (layerCounts[layerIdKF] == setup_->zhtMaxStubsPerLayer())
0170 continue;
0171 layerCounts[layerIdKF]++;
0172 deque<FrameStub>& stubs = dequesStubs[sectorPhi * setup_->numLayers() + layerIdKF];
0173 auto identical = [ttStubRef, ttTrack](const StubZHT& stub) {
0174 return (int)ttTrack.trackSeedType() == stub.trackId() && ttStubRef == stub.ttStubRef();
0175 };
0176 stubZHT = &*find_if(stubsZHT.begin(), stubsZHT.end(), identical);
0177 const double inv2R = dfinv2R.floating(stubZHT->inv2R());
0178 const double cot = dfcot.floating(stubZHT->cot()) + setup_->sectorCot(binEta);
0179 const double dPhi = dfdPhi.digi(setup_->dPhi(ttStubRef, inv2R));
0180 const double dZ = dfdZ.digi(setup_->dZ(ttStubRef, cot));
0181 stubs.emplace_back(StubKFin(*stubZHT, dPhi, dZ, layerIdKF).frame());
0182 }
0183 const int size = *max_element(layerCounts.begin(), layerCounts.end());
0184 int layerIdKF(0);
0185 for (int layerCount : layerCounts) {
0186 deque<FrameStub>& stubs = dequesStubs[sectorPhi * setup_->numLayers() + layerIdKF++];
0187 const int nGaps = size - layerCount;
0188 stubs.insert(stubs.end(), nGaps, FrameStub());
0189 }
0190 const TTBV& maybePattern = layerEncoding_->maybePattern(binEta, binZT, binCot);
0191 const TrackKFin track(*stubZHT, TTTrackRef(handleTTTracks, i++), maybePattern);
0192 tracks.emplace_back(track.frame());
0193 const int nGaps = size - 1;
0194 tracks.insert(tracks.end(), nGaps, FrameTrack());
0195 }
0196
0197 for (int channel = 0; channel < dataFormats_->numChannel(Process::kf); channel++) {
0198 const int index = region * dataFormats_->numChannel(Process::kf) + channel;
0199 deque<FrameTrack>& tracks = dequesTracks[channel];
0200 auto limitTracks = next(tracks.begin(), min(setup_->numFrames(), (int)tracks.size()));
0201 if (!enableTruncation_)
0202 limitTracks = tracks.end();
0203 streamAcceptedTracks[index] = StreamTrack(tracks.begin(), limitTracks);
0204 streamLostTracks[index] = StreamTrack(limitTracks, tracks.end());
0205 for (int l = 0; l < setup_->numLayers(); l++) {
0206 deque<FrameStub>& stubs = dequesStubs[channel * setup_->numLayers() + l];
0207 auto limitStubs = next(stubs.begin(), min(setup_->numFrames(), (int)stubs.size()));
0208 if (!enableTruncation_)
0209 limitStubs = stubs.end();
0210 streamAcceptedStubs[index * setup_->numLayers() + l] = StreamStub(stubs.begin(), limitStubs);
0211 streamLostStubs[index * setup_->numLayers() + l] = StreamStub(limitStubs, stubs.end());
0212 }
0213 }
0214 }
0215 }
0216
0217 iEvent.emplace(edPutTokenAcceptedStubs_, std::move(streamAcceptedStubs));
0218 iEvent.emplace(edPutTokenAcceptedTracks_, std::move(streamAcceptedTracks));
0219 iEvent.emplace(edPutTokenLostStubs_, std::move(streamLostStubs));
0220 iEvent.emplace(edPutTokenLostTracks_, std::move(streamLostTracks));
0221 }
0222
0223 }
0224
0225 DEFINE_FWK_MODULE(trackerTFP::ProducerKFin);