Skip to content

Commit

Permalink
Add pattern files for Layer2 e/g with TMUX18 input
Browse files Browse the repository at this point in the history
  • Loading branch information
cerminar committed Nov 23, 2023
1 parent 3719a07 commit 2f68970
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 52 deletions.
127 changes: 88 additions & 39 deletions L1Trigger/Phase2L1ParticleFlow/plugins/L1TCtL2EgProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class L1TCtL2EgProducer : public edm::global::EDProducer<> {
std::vector<ap_uint<64>> encodeLayer1(const std::vector<EGIsoObjEmu> &photons) const;
std::vector<ap_uint<64>> encodeLayer1(const std::vector<EGIsoEleObjEmu> &electrons) const;

std::vector<ap_uint<64>> encodeLayer1EgObjs(unsigned int nObj,
const std::vector<EGIsoObjEmu> &photons,
const std::vector<EGIsoEleObjEmu> &electrons) const;
void encodeLayer1EgObjs(unsigned int nObj,
std::vector<ap_uint<64>> &data,
const std::vector<EGIsoObjEmu> &photons,
const std::vector<EGIsoEleObjEmu> &electrons) const;

void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override;

Expand All @@ -56,25 +57,25 @@ class L1TCtL2EgProducer : public edm::global::EDProducer<> {
template <class T>
class PFInstanceInputs {
public:
typedef std::vector<std::pair<edm::EDGetTokenT<T>, std::vector<int>>> InputTokenAndChannels;
typedef std::vector<std::pair<edm::EDGetTokenT<T>, std::vector<int>>> InputTokenAndRegions;
PFInstanceInputs(L1TCtL2EgProducer *prod, const std::vector<edm::ParameterSet> &confs) {
for (const auto &conf : confs) {
const auto &producer_tag = conf.getParameter<edm::InputTag>("pfProducer");
tokensAndChannels_.push_back(std::make_pair(
tokensAndRegions_.push_back(std::make_pair(
prod->consumes<T>(edm::InputTag(producer_tag.label(), producer_tag.instance(), producer_tag.process())),
conf.getParameter<std::vector<int>>("channels")));
conf.getParameter<std::vector<int>>("regions")));
}
}

const InputTokenAndChannels &tokensAndChannels() const { return tokensAndChannels_; }
const InputTokenAndRegions &tokensAndRegions() const { return tokensAndRegions_; }

private:
InputTokenAndChannels tokensAndChannels_;
InputTokenAndRegions tokensAndRegions_;
};

class PatternWriter {
public:
PatternWriter(const edm::ParameterSet &conf) : dataWriter_(nullptr) {
PatternWriter(const edm::ParameterSet &conf) : region2link_(5), dataWriter_(nullptr) {
unsigned int nFramesPerBX = conf.getParameter<uint32_t>("nFramesPerBX");

std::map<l1t::demo::LinkId, std::pair<l1t::demo::ChannelSpec, std::vector<size_t>>> channelSpecs;
Expand All @@ -84,11 +85,42 @@ class L1TCtL2EgProducer : public edm::global::EDProducer<> {
unsigned int eventGap =
inTMUX * nFramesPerBX - channelConf.getParameter<uint32_t>("nWords"); // assuming 96bit (= 3/2 word)
// words = TMUX*9-2*3/2*words

std::vector<uint32_t> chns = channelConf.getParameter<std::vector<uint32_t>>("channels");
channelSpecs[l1t::demo::LinkId{channelConf.getParameter<std::string>("interface"),
channelConf.getParameter<uint32_t>("id")}] =
std::make_pair(l1t::demo::ChannelSpec{inTMUX, eventGap},
std::vector<size_t>(std::begin(chns), std::end(chns)));

std::string interface = channelConf.getParameter<std::string>("interface");
unsigned int chId = channelConf.getParameter<uint32_t>("id");
l1t::demo::LinkId linkId{interface, size_t(chId)};
channelSpecs[linkId] = std::make_pair(l1t::demo::ChannelSpec{inTMUX, eventGap},
std::vector<size_t>(std::begin(chns), std::end(chns)));

// NOTE: this could be parametrized from CFG
if (inTMUX == 6) {
if (interface == "eglayer1Barrel" || interface == "eglayer1Endcap") {
// Layer1 TMUX=6 => each one of the regions is mapped to a link
// with the same ID
// # of words = 16 (pho) + 2*16 (ele) = 48
// no filling words needed
region2link_[chId] = RegionLinkMetadata{linkId, 0};
}
} else if (inTMUX == 18) {
if (interface == "eglayer1Barrel") {
// Layer1 TMUX=18 => 3 regions are mapped to the same link
// one every 6 BX x 9 words = 54 words
// # of data words = 16 (pho) + 2*16 (ele) = 48
// # we fill with 54-48 = 6 empty words
region2link_[0] = RegionLinkMetadata{linkId, 6};
region2link_[1] = RegionLinkMetadata{linkId, 6};
region2link_[2] = RegionLinkMetadata{linkId, 0};
} else if (interface == "eglayer1Endcap") {
// Layer1 TMUX=18 => 2 endcap regions are mapped to the same link
// one every 9 BX x 9 words = 81 words
// # of data words = 16 (pho) + 2*16 (ele) = 48
// # we fill with 81-48 = 33 empty words
region2link_[3] = RegionLinkMetadata{linkId, 33};
region2link_[4] = RegionLinkMetadata{linkId, 0};
}
}
}

dataWriter_ = std::make_unique<l1t::demo::BoardDataWriter>(
Expand All @@ -101,11 +133,23 @@ class L1TCtL2EgProducer : public edm::global::EDProducer<> {
channelSpecs);
}

struct RegionLinkMetadata {
l1t::demo::LinkId linkId;
unsigned int nTrailingWords;
};

void addEvent(const l1t::demo::EventData &eventData) { dataWriter_->addEvent(eventData); }

void flush() { dataWriter_->flush(); }

const RegionLinkMetadata &region2Link(unsigned int region) const {
assert(region < region2link_.size());
return region2link_.at(region);
}

private:
std::vector<RegionLinkMetadata> region2link_;

std::unique_ptr<l1t::demo::BoardDataWriter> dataWriter_;
};

Expand All @@ -115,33 +159,33 @@ class L1TCtL2EgProducer : public edm::global::EDProducer<> {
ConstituentPtrVector &constituentsPtrs,
std::unique_ptr<TT> &out) const {
edm::Handle<T> handle;
for (const auto &tokenAndChannel : instance.tokensAndChannels()) {
iEvent.getByToken(tokenAndChannel.first, handle);
populate(out, handle, tokenAndChannel.second, constituentsPtrs);
for (const auto &tokenAndRegions : instance.tokensAndRegions()) {
iEvent.getByToken(tokenAndRegions.first, handle);
populate(out, handle, tokenAndRegions.second, constituentsPtrs);
}
}

template <class TT, class T>
void populate(std::unique_ptr<T> &out,
const edm::Handle<TT> &in,
const std::vector<int> &links,
const std::vector<int> &regions,
ConstituentPtrVector &constituentsPtrs) const {
assert(links.size() == in->nRegions());
assert(regions.size() == in->nRegions());
for (unsigned int iBoard = 0, nBoard = in->nRegions(); iBoard < nBoard; ++iBoard) {
auto region = in->region(iBoard);
int linkID = links[iBoard];
if (linkID < 0)
int regionID = regions[iBoard];
if (regionID < 0)
continue;
// std::cout << "Board eta: " << in->eta(iBoard) << " phi: " << in->phi(iBoard) << " link: " << linkID << std::endl;
// std::cout << "Board eta: " << in->eta(iBoard) << " phi: " << in->phi(iBoard) << " link: " << regionID << std::endl;
for (const auto &obj : region) {
convertToEmu(obj, constituentsPtrs, out->at(linkID));
convertToEmu(obj, constituentsPtrs, out->at(regionID));
}
}
}

void populate(std::unique_ptr<BXVector<l1t::EGamma>> &out,
const edm::Handle<BXVector<l1t::EGamma>> &in,
const std::vector<int> &links,
const std::vector<int> &regions,
ConstituentPtrVector &constituentsPtrs) const {
for (int bx = in->getFirstBX(); bx <= in->getLastBX(); bx++) {
for (auto egee_itr = in->begin(bx); egee_itr != in->end(bx); egee_itr++) {
Expand Down Expand Up @@ -247,19 +291,17 @@ std::vector<ap_uint<64>> L1TCtL2EgProducer::encodeLayer1(const std::vector<EGIso
return ret;
}

std::vector<ap_uint<64>> L1TCtL2EgProducer::encodeLayer1EgObjs(unsigned int nObj,
const std::vector<EGIsoObjEmu> &photons,
const std::vector<EGIsoEleObjEmu> &electrons) const {
std::vector<ap_uint<64>> ret;
void L1TCtL2EgProducer::encodeLayer1EgObjs(unsigned int nObj,
std::vector<ap_uint<64>> &data,
const std::vector<EGIsoObjEmu> &photons,
const std::vector<EGIsoEleObjEmu> &electrons) const {
auto encoded_photons = encodeLayer1(photons);
encoded_photons.resize(nObj, {0});
auto encoded_eles = encodeLayer1(electrons);
encoded_eles.resize(2 * nObj, {0});

std::copy(encoded_photons.begin(), encoded_photons.end(), std::back_inserter(ret));
std::copy(encoded_eles.begin(), encoded_eles.end(), std::back_inserter(ret));

return ret;
std::copy(encoded_photons.begin(), encoded_photons.end(), std::back_inserter(data));
std::copy(encoded_eles.begin(), encoded_eles.end(), std::back_inserter(data));
}

void L1TCtL2EgProducer::produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &) const {
Expand All @@ -269,24 +311,31 @@ void L1TCtL2EgProducer::produce(edm::StreamID, edm::Event &iEvent, const edm::Ev
merge(tkEGInputs_, iEvent, constituents, outEgs);
iEvent.put(std::move(outEgs), tkEGInstanceLabel_);

auto boards = std::make_unique<std::vector<l1ct::OutputBoard>>(l2egsorter.nInputBoards());
auto regions = std::make_unique<std::vector<l1ct::OutputBoard>>(l2egsorter.nInputBoards());

merge(tkEleInputs_, iEvent, constituents, boards);
merge(tkEmInputs_, iEvent, constituents, boards);
merge(tkEleInputs_, iEvent, constituents, regions);
merge(tkEmInputs_, iEvent, constituents, regions);

if (doInPtrn_) {
std::map<unsigned int, l1t::demo::LinkId> regio2link;

l1t::demo::EventData inData;
for (unsigned int ibrd = 0; ibrd < boards->size(); ibrd++) {
inData.add(
{"eglayer1", ibrd},
encodeLayer1EgObjs(l2egsorter.nInputObjPerBoard(), (*boards)[ibrd].egphoton, (*boards)[ibrd].egelectron));
for (unsigned int ireg = 0; ireg < regions->size(); ireg++) {
const auto &linkData = inPtrnWrt_->region2Link(ireg);
std::vector<ap_uint<64>> data;

if (inData.has(linkData.linkId))
data = inData.at(linkData.linkId);
encodeLayer1EgObjs(l2egsorter.nInputObjPerBoard(), data, (*regions)[ireg].egphoton, (*regions)[ireg].egelectron);
data.resize(data.size() + linkData.nTrailingWords, {0});
inData.add(linkData.linkId, data);
}
inPtrnWrt_->addEvent(inData);
}

std::vector<EGIsoObjEmu> out_photons_emu;
std::vector<EGIsoEleObjEmu> out_eles_emu;
l2egsorter.run(*boards, out_photons_emu, out_eles_emu);
l2egsorter.run(*regions, out_photons_emu, out_eles_emu);

// PUPPI isolation
auto &pfObjs = iEvent.get(pfObjsToken_);
Expand Down
83 changes: 71 additions & 12 deletions L1Trigger/Phase2L1ParticleFlow/python/l1ctLayer2EG_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@
tkElectrons=cms.VPSet(
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCal", 'L1TkElePerBoard'),
channels=cms.vint32(3, 4)
regions=cms.vint32(3, 4)
),
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1Barrel", 'L1TkElePerBoard'),
channels=cms.vint32(0, 1, 2)
regions=cms.vint32(0, 1, 2)
),
),
tkEms=cms.VPSet(
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCal", 'L1TkEmPerBoard'),
channels=cms.vint32(3, 4)
regions=cms.vint32(3, 4)
),
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCalNoTK", 'L1TkEmPerBoard'),
channels=cms.vint32(-1)
regions=cms.vint32(-1)
),
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1Barrel", 'L1TkEmPerBoard'),
channels=cms.vint32(0, 1, 2)
regions=cms.vint32(0, 1, 2)
),
),
tkEgs=cms.VPSet(
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCal", 'L1Eg'),
channels=cms.vint32(-1)
regions=cms.vint32(-1)
),
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCalNoTK", 'L1Eg'),
channels=cms.vint32(-1)
regions=cms.vint32(-1)
),
),
l1PFObjects = cms.InputTag("l1tLayer2Deregionizer", "Puppi"),
Expand Down Expand Up @@ -81,35 +81,35 @@
cms.PSet(
TMUX=cms.uint32(6),
nWords=cms.uint32(48), # = 16*2words ele + 16words photons
interface=cms.string("eglayer1"),
interface=cms.string("eglayer1Barrel"),
id=cms.uint32(0),
channels=cms.vuint32(0)
),
cms.PSet(
TMUX=cms.uint32(6),
nWords=cms.uint32(48),
interface=cms.string("eglayer1"),
interface=cms.string("eglayer1Barrel"),
id=cms.uint32(1),
channels=cms.vuint32(1)
),
cms.PSet(
TMUX=cms.uint32(6),
nWords=cms.uint32(48),
interface=cms.string("eglayer1"),
interface=cms.string("eglayer1Barrel"),
id=cms.uint32(2),
channels=cms.vuint32(2)
),
cms.PSet(
TMUX=cms.uint32(6),
nWords=cms.uint32(48),
interface=cms.string("eglayer1"),
interface=cms.string("eglayer1Endcap"),
id=cms.uint32(3),
channels=cms.vuint32(3)
),
cms.PSet(
TMUX=cms.uint32(6),
nWords=cms.uint32(48),
interface=cms.string("eglayer1"),
interface=cms.string("eglayer1Endcap"),
id=cms.uint32(4),
channels=cms.vuint32(4)
),
Expand Down Expand Up @@ -190,6 +190,65 @@
),
)

# EG Layer2 with Layer1 @ TMUX18
l1tLayer2EGTM18 = l1tLayer2EG.clone(
tkElectrons=cms.VPSet(
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCalTM18", 'L1TkElePerBoard'),
regions=cms.vint32(3, 4)
),
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1BarrelSerenityTM18", 'L1TkElePerBoard'),
regions=cms.vint32(0, 1, 2)
),
),
tkEms=cms.VPSet(
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCalTM18", 'L1TkEmPerBoard'),
regions=cms.vint32(3, 4)
),
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCalNoTKTM18", 'L1TkEmPerBoard'),
regions=cms.vint32(-1)
),
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1BarrelSerenityTM18", 'L1TkEmPerBoard'),
regions=cms.vint32(0, 1, 2)
),
),
tkEgs=cms.VPSet(
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCalTM18", 'L1Eg'),
regions=cms.vint32(-1)
),
cms.PSet(
pfProducer=cms.InputTag("l1tLayer1HGCalNoTKTM18", 'L1Eg'),
regions=cms.vint32(-1)
),
),
)

l1tLayer2EGTM18.inPatternFile.outputFilename = "L1TCTL2EG_TMUX18_InPattern"
l1tLayer2EGTM18.inPatternFile.channels = cms.VPSet(
cms.PSet(
TMUX=cms.uint32(18),
nWords=cms.uint32(156), # = (16*2words ele + 16words photons) * 3 (regions) every 6 BX (54 words) = 48+6(empty)+48+6(empty)+48 = 156
interface=cms.string("eglayer1Barrel"),
id=cms.uint32(0),
channels=cms.vuint32(0,2,4)
),
cms.PSet(
TMUX=cms.uint32(18),
nWords=cms.uint32(129), # (16*2words ele + 16words photons) * 2 (regions) every 9 BX (81 words) = 48+33(empty)+48
interface=cms.string("eglayer1Endcap"),
id=cms.uint32(1),
channels=cms.vuint32(1,3,5)
),
)
l1tLayer2EGTM18.outPatternFile.outputFilename = 'L1TCTL2EG_TMUX18_OutPattern'
# FIXME: we need to schedule a new deregionizer for TM18
# l1tLayer2EGTM18.l1PFObjects = cms.InputTag("l1tLayer2Deregionizer", "Puppi"),


L1TLayer2EGTask = cms.Task(
l1tLayer2Deregionizer,
Expand Down
Loading

0 comments on commit 2f68970

Please sign in to comment.