Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-03 00:12:21

0001 #include "L1Trigger/TrackFindingTracklet/interface/ChannelAssignment.h"
0002 #include "L1Trigger/TrackFindingTracklet/interface/Settings.h"
0003 
0004 #include <vector>
0005 #include <array>
0006 #include <set>
0007 #include <algorithm>
0008 #include <numeric>
0009 #include <iterator>
0010 
0011 using namespace std;
0012 using namespace edm;
0013 using namespace tt;
0014 
0015 namespace trklet {
0016 
0017   ChannelAssignment::ChannelAssignment(const Config& iConfig, const Setup* setup)
0018       : setup_(setup),
0019         tmMuxOrder_(iConfig.tmMuxOrder_),
0020         tmNumLayers_(iConfig.tmNumLayers_),
0021         tmWidthStubId_(iConfig.tmWidthStubId_),
0022         tmWidthCot_(iConfig.tmWidthCot_),
0023         numComparisonModules_(iConfig.numComparisonModules_),
0024         minIdenticalStubs_(iConfig.minIdenticalStubs_),
0025         seedTypeNames_(iConfig.seedTypeNames_),
0026         numSeedTypes_(iConfig.seedTypeNames_.size()),
0027         numChannelsTrack_(numSeedTypes_),
0028         numChannelsStub_(iConfig.numChannelsStub_),
0029         seedTypesSeedLayers_(iConfig.seedTypesSeedLayers_),
0030         seedTypesProjectionLayers_(iConfig.seedTypesProjectionLayers_),
0031         maxNumProjectionLayers_(iConfig.maxNumProjectionLayers_),
0032         channelEncoding_(iConfig.channelEncoding_),
0033         offsetsStubs_(iConfig.offsetsStubs_),
0034         numSeedingLayers_(iConfig.numSeedingLayers_),
0035         tmMuxOrderInt_(iConfig.tmMuxOrderInt_) {
0036     // consistency check
0037     const int offsetBarrel = setup_->offsetLayerId();
0038     const int numBarrelLayer = setup_->numBarrelLayer();
0039     const int offsetDisk = setup_->offsetLayerDisks() + offsetBarrel;
0040     static constexpr int invalidSeedLayer = -1;
0041     static constexpr int invalidLayerDisk = 0;
0042     const Settings settings;
0043     const auto& seedlayers = settings.seedlayers();
0044     const auto& projlayers = settings.projlayers();
0045     const auto& projdisks = settings.projdisks();
0046     // convert Seetings layer ids into ChannelAssignment layer ids
0047     vector<set<int>> allSeedingLayer(seedlayers.size());
0048     vector<set<int>> allProjectionLayer(seedlayers.size());
0049     for (int iSeed = 0; iSeed < (int)seedlayers.size(); iSeed++)
0050       for (const auto& layer : seedlayers[iSeed])
0051         if (layer != invalidSeedLayer)
0052           allSeedingLayer[iSeed].insert(layer < numBarrelLayer ? layer + offsetBarrel
0053                                                                : layer + offsetDisk - numBarrelLayer);
0054     for (int iSeed = 0; iSeed < (int)projlayers.size(); iSeed++)
0055       for (const auto& layer : projlayers[iSeed])
0056         if (layer != invalidLayerDisk)
0057           allProjectionLayer[iSeed].insert(layer);
0058     for (int iSeed = 0; iSeed < (int)projdisks.size(); iSeed++)
0059       for (const auto& disk : projdisks[iSeed])
0060         if (disk != invalidLayerDisk)
0061           allProjectionLayer[iSeed].insert(disk - offsetBarrel + offsetDisk);
0062     // check if ChannelAssignment seedTypesSeedLayers_ and seedTypesProjectionLayers_ are subsets of Settings pendants
0063     for (int iSubSeed = 0; iSubSeed < numSeedTypes_; iSubSeed++) {
0064       const vector<int>& seedLayers = seedTypesSeedLayers_[iSubSeed];
0065       bool subset(false);
0066       for (int iAllSeed = 0; iAllSeed < (int)seedlayers.size(); iAllSeed++) {
0067         // compare seed layer
0068         const set<int>& asl = allSeedingLayer[iAllSeed];
0069         set<int> sl(seedLayers.begin(), seedLayers.end());
0070         set<int> intersect;
0071         set_intersection(sl.begin(), sl.end(), asl.begin(), asl.end(), inserter(intersect, intersect.begin()));
0072         if (intersect == sl) {
0073           subset = true;
0074           // compare projection layer
0075           const vector<int>& projectionLayers = seedTypesProjectionLayers_[iSubSeed];
0076           const set<int>& apl = allProjectionLayer[iAllSeed];
0077           set<int> pl(projectionLayers.begin(), projectionLayers.end());
0078           set<int> intersect;
0079           set_intersection(pl.begin(), pl.end(), apl.begin(), apl.end(), inserter(intersect, intersect.begin()));
0080           if (intersect == pl)
0081             break;
0082           set<int> difference;
0083           set_difference(
0084               pl.begin(), pl.end(), intersect.begin(), intersect.end(), inserter(difference, difference.begin()));
0085           cms::Exception exception("LogicError.");
0086           exception << "ProjectionLayers ( ";
0087           for (int layer : difference)
0088             exception << layer << " ";
0089           exception << ") are not supported with seed type ( ";
0090           for (int layer : seedLayers)
0091             exception << layer << " ";
0092           exception << ")";
0093           exception.addContext("trklet::ChannelAssignment::ChannelAssignment");
0094           throw exception;
0095         }
0096       }
0097       if (subset)
0098         continue;
0099       cms::Exception exception("LogicError.");
0100       exception << "SeedLayers ( ";
0101       for (int layer : seedLayers)
0102         exception << layer << " ";
0103       exception << ") are not supported";
0104       exception.addContext("trklet::ChannelAssignment::ChannelAssignment");
0105       throw exception;
0106     }
0107     auto bigger = [](const vector<int>& lhs, const vector<int>& rhs) { return lhs.size() < rhs.size(); };
0108     numSeedingLayers_ = max_element(seedTypesSeedLayers_.begin(), seedTypesSeedLayers_.end(), bigger)->size();
0109     maxNumProjectionLayers_ =
0110         max_element(seedTypesProjectionLayers_.begin(), seedTypesProjectionLayers_.end(), bigger)->size();
0111     auto acc = [](int sum, vector<int> ints) { return sum + static_cast<int>(ints.size()); };
0112     offsetsStubs_.reserve(numSeedTypes_);
0113     numChannelsStub_ = accumulate(
0114         seedTypesProjectionLayers_.begin(), seedTypesProjectionLayers_.end(), numSeedingLayers_ * numSeedTypes_, acc);
0115     for (int seed = 0; seed < numSeedTypes_; seed++) {
0116       const auto it = next(seedTypesProjectionLayers_.begin(), seed);
0117       offsetsStubs_.emplace_back(accumulate(seedTypesProjectionLayers_.begin(), it, numSeedingLayers_ * seed, acc));
0118     }
0119   }
0120 
0121   // returns channelId of given TTTrackRef
0122   int ChannelAssignment::channelId(const TTTrackRef& ttTrackRef) const {
0123     const int seedType = ttTrackRef->trackSeedType();
0124     if (seedType >= numSeedTypes_) {
0125       cms::Exception exception("logic_error");
0126       exception << "TTTracks form seed type" << seedType << " not in supported list: (";
0127       for (const auto& s : seedTypeNames_)
0128         exception << s << " ";
0129       exception << ").";
0130       exception.addContext("trklet:ChannelAssignment:channelId");
0131       throw exception;
0132     }
0133     return ttTrackRef->phiSector() * numSeedTypes_ + seedType;
0134   }
0135 
0136   // sets layerId of given TTStubRef and seedType, returns false if seeed stub
0137   bool ChannelAssignment::layerId(int seedType, const TTStubRef& ttStubRef, int& layerId) const {
0138     layerId = -1;
0139     if (seedType < 0 || seedType >= numSeedTypes_) {
0140       cms::Exception exception("logic_error");
0141       exception.addContext("trklet::ChannelAssignment::layerId");
0142       exception << "TTTracks with with seed type " << seedType << " not supported.";
0143       throw exception;
0144     }
0145     const int layer = setup_->layerId(ttStubRef);
0146     const vector<int>& seedingLayers = seedTypesSeedLayers_[seedType];
0147     if (find(seedingLayers.begin(), seedingLayers.end(), layer) != seedingLayers.end())
0148       return false;
0149     const vector<int>& projectingLayers = seedTypesProjectionLayers_[seedType];
0150     const auto pos = find(projectingLayers.begin(), projectingLayers.end(), layer);
0151     if (pos == projectingLayers.end()) {
0152       const string& name = seedTypeNames_[seedType];
0153       cms::Exception exception("logic_error");
0154       exception.addContext("trklet::ChannelAssignment::layerId");
0155       exception << "TTStub from layer " << layer << " (barrel: 1-6; discs: 11-15) from seed type " << name
0156                 << " not supported.";
0157       throw exception;
0158     }
0159     layerId = distance(projectingLayers.begin(), pos);
0160     return true;
0161   }
0162 
0163   // index of first stub channel belonging to given track channel
0164   int ChannelAssignment::offsetStub(int channelTrack) const {
0165     const int region = channelTrack / numChannelsTrack_;
0166     const int channel = channelTrack % numChannelsTrack_;
0167     return region * numChannelsStub_ + offsetsStubs_[channel];
0168   }
0169 
0170   // returns TBout channel Id
0171   int ChannelAssignment::channelId(int seedType, int layerId) const {
0172     const vector<int>& projections = seedTypesProjectionLayers_.at(seedType);
0173     const auto itp = find(projections.begin(), projections.end(), layerId);
0174     if (itp != projections.end())
0175       return distance(projections.begin(), itp);
0176     const vector<int>& seeds = seedTypesSeedLayers_.at(seedType);
0177     const auto its = find(seeds.begin(), seeds.end(), layerId);
0178     if (its != seeds.end())
0179       return (int)projections.size() + distance(seeds.begin(), its);
0180     return -1;
0181   }
0182 
0183   // layers a seed types can project to using default layer id [barrel: 1-6, discs: 11-15]
0184   int ChannelAssignment::layerId(int seedType, int channel) const {
0185     if (channel < numProjectionLayers(seedType))
0186       return seedTypesProjectionLayers_.at(seedType).at(channel);
0187     return seedTypesSeedLayers_.at(seedType).at(channel - numProjectionLayers(seedType));
0188   }
0189 
0190 }  // namespace trklet