Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "L1Trigger/TrackFindingTracklet/interface/DR.h"
0002 
0003 #include <vector>
0004 #include <numeric>
0005 #include <algorithm>
0006 
0007 using namespace std;
0008 using namespace edm;
0009 using namespace tt;
0010 using namespace trackerTFP;
0011 
0012 namespace trklet {
0013 
0014   DR::DR(const ParameterSet& iConfig,
0015          const Setup* setup,
0016          const DataFormats* dataFormats,
0017          const ChannelAssignment* channelAssignment,
0018          int region)
0019       : enableTruncation_(iConfig.getParameter<bool>("EnableTruncation")),
0020         setup_(setup),
0021         dataFormats_(dataFormats),
0022         channelAssignment_(channelAssignment),
0023         region_(region),
0024         input_(channelAssignment_->numNodesDR()) {}
0025 
0026   // read in and organize input tracks and stubs
0027   void DR::consume(const StreamsTrack& streamsTrack, const StreamsStub& streamsStub) {
0028     const int offsetTrack = region_ * channelAssignment_->numNodesDR();
0029     auto nonNullTrack = [](int sum, const FrameTrack& frame) { return sum + (frame.first.isNonnull() ? 1 : 0); };
0030     auto nonNullStub = [](int sum, const FrameStub& frame) { return sum + (frame.first.isNonnull() ? 1 : 0); };
0031     // count tracks and stubs and reserve corresponding vectors
0032     int sizeTracks(0);
0033     int sizeStubs(0);
0034     for (int channel = 0; channel < channelAssignment_->numNodesDR(); channel++) {
0035       const int streamTrackId = offsetTrack + channel;
0036       const int offsetStub = streamTrackId * setup_->numLayers();
0037       const StreamTrack& streamTrack = streamsTrack[streamTrackId];
0038       input_[channel].reserve(streamTrack.size());
0039       sizeTracks += accumulate(streamTrack.begin(), streamTrack.end(), 0, nonNullTrack);
0040       for (int layer = 0; layer < setup_->numLayers(); layer++) {
0041         const StreamStub& streamStub = streamsStub[offsetStub + layer];
0042         sizeStubs += accumulate(streamStub.begin(), streamStub.end(), 0, nonNullStub);
0043       }
0044     }
0045     tracks_.reserve(sizeTracks);
0046     stubs_.reserve(sizeStubs);
0047     // transform input data into handy structs
0048     for (int channel = 0; channel < channelAssignment_->numNodesDR(); channel++) {
0049       vector<Track*>& input = input_[channel];
0050       const int streamTrackId = offsetTrack + channel;
0051       const int offsetStub = streamTrackId * setup_->numLayers();
0052       const StreamTrack& streamTrack = streamsTrack[streamTrackId];
0053       for (int frame = 0; frame < (int)streamTrack.size(); frame++) {
0054         const FrameTrack& frameTrack = streamTrack[frame];
0055         if (frameTrack.first.isNull()) {
0056           input.push_back(nullptr);
0057           continue;
0058         }
0059         vector<Stub*> stubs;
0060         stubs.reserve(setup_->numLayers());
0061         for (int layer = 0; layer < setup_->numLayers(); layer++) {
0062           const FrameStub& frameStub = streamsStub[offsetStub + layer][frame];
0063           if (frameStub.first.isNull())
0064             continue;
0065           TTBV ttBV = frameStub.second;
0066           const TTBV z(ttBV, dataFormats_->format(Variable::z, Process::kfin).width(), 0, true);
0067           ttBV >>= dataFormats_->format(Variable::z, Process::kfin).width();
0068           const TTBV phi(ttBV, dataFormats_->format(Variable::phi, Process::kfin).width(), 0, true);
0069           ttBV >>= dataFormats_->format(Variable::phi, Process::kfin).width();
0070           const TTBV r(ttBV, dataFormats_->format(Variable::r, Process::kfin).width(), 0, true);
0071           ttBV >>= dataFormats_->format(Variable::r, Process::kfin).width();
0072           const TTBV stubId(ttBV, channelAssignment_->widthSeedStubId(), 0);
0073           ttBV >>= channelAssignment_->widthSeedStubId();
0074           const TTBV layerId(ttBV, channelAssignment_->widthLayerId(), 0);
0075           ttBV >>= channelAssignment_->widthLayerId();
0076           const TTBV tilt(ttBV, channelAssignment_->widthPSTilt(), 0);
0077           const FrameStub frame(frameStub.first,
0078                                 Frame("1" + tilt.str() + layerId.str() + r.str() + phi.str() + z.str()));
0079           stubs_.emplace_back(frame, stubId.val(), layer);
0080           stubs.push_back(&stubs_.back());
0081         }
0082         tracks_.emplace_back(frameTrack, stubs);
0083         input.push_back(&tracks_.back());
0084       }
0085       // remove all gaps between end and last track
0086       for (auto it = input.end(); it != input.begin();)
0087         it = (*--it) ? input.begin() : input.erase(it);
0088     }
0089   }
0090 
0091   // fill output products
0092   void DR::produce(StreamsStub& accpetedStubs,
0093                    StreamsTrack& acceptedTracks,
0094                    StreamsStub& lostStubs,
0095                    StreamsTrack& lostTracks) {
0096     const int offsetTrack = region_ * channelAssignment_->numNodesDR();
0097     for (int node = 0; node < channelAssignment_->numNodesDR(); node++) {
0098       const int channelTrack = offsetTrack + node;
0099       const int offsetStub = channelTrack * setup_->numLayers();
0100       // remove duplicated tracks, no merge of stubs, one stub per layer expected
0101       vector<Track*> cms(channelAssignment_->numComparisonModules(), nullptr);
0102       vector<Track*>& tracks = input_[node];
0103       for (Track*& track : tracks) {
0104         if (!track)
0105           // gaps propagate trough chain and appear in output stream
0106           continue;
0107         for (Track*& trackCM : cms) {
0108           if (!trackCM) {
0109             // tracks used in CMs propagate trough chain and appear in output stream unaltered
0110             trackCM = track;
0111             break;
0112           }
0113           if (equalEnough(track, trackCM)) {
0114             // tracks compared in CMs propagate trough chain and appear in output stream as gap if identified as duplicate or unaltered elsewise
0115             track = nullptr;
0116             break;
0117           }
0118         }
0119       }
0120       // remove all gaps between end and last track
0121       for (auto it = tracks.end(); it != tracks.begin();)
0122         it = (*--it) ? tracks.begin() : tracks.erase(it);
0123       // store output
0124       StreamTrack& streamTrack = acceptedTracks[channelTrack];
0125       streamTrack.reserve(tracks.size());
0126       for (int layer = 0; layer < setup_->numLayers(); layer++)
0127         accpetedStubs[offsetStub + layer].reserve(tracks.size());
0128       for (Track* track : tracks) {
0129         if (!track) {
0130           streamTrack.emplace_back(FrameTrack());
0131           for (int layer = 0; layer < setup_->numLayers(); layer++)
0132             accpetedStubs[offsetStub + layer].emplace_back(FrameStub());
0133           continue;
0134         }
0135         streamTrack.push_back(track->frame_);
0136         TTBV hitPattern(0, setup_->numLayers());
0137         for (Stub* stub : track->stubs_) {
0138           hitPattern.set(stub->channel_);
0139           accpetedStubs[offsetStub + stub->channel_].push_back(stub->frame_);
0140         }
0141         for (int layer : hitPattern.ids(false))
0142           accpetedStubs[offsetStub + layer].emplace_back(FrameStub());
0143       }
0144     }
0145   }
0146 
0147   // compares two tracks, returns true if those are considered duplicates
0148   bool DR::equalEnough(Track* t0, Track* t1) const {
0149     int same(0);
0150     for (int layer = 0; layer < setup_->numLayers(); layer++) {
0151       auto onLayer = [layer](Stub* stub) { return stub->channel_ == layer; };
0152       const auto s0 = find_if(t0->stubs_.begin(), t0->stubs_.end(), onLayer);
0153       const auto s1 = find_if(t1->stubs_.begin(), t1->stubs_.end(), onLayer);
0154       if (s0 != t0->stubs_.end() && s1 != t1->stubs_.end() && **s0 == **s1)
0155         same++;
0156     }
0157     return same >= channelAssignment_->minIdenticalStubs();
0158   }
0159 
0160 }  // namespace trklet