Skip to content

Commit

Permalink
Refactoring geometry testing infrastructure (cms-sw#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsauvan authored Aug 2, 2024
1 parent cb42bf7 commit 7724d3d
Show file tree
Hide file tree
Showing 14 changed files with 1,495 additions and 4 deletions.
75 changes: 75 additions & 0 deletions L1Trigger/L1THGCal/interface/HGCalTriggerGeoTesterBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef __L1Trigger_L1THGCal_HGCalTriggerGeoTesterBase_h__
#define __L1Trigger_L1THGCal_HGCalTriggerGeoTesterBase_h__

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "TTree.h"
#include <unordered_map>
#include <string>

namespace HepPDT {
class ParticleDataTable;
}
class MagneticField;
class HGCalTriggerGeometryBase;

struct HGCalTriggerGeoTesterEventSetup {
edm::ESHandle<HGCalTriggerGeometryBase> geometry;
};

class HGCalTriggerGeoTesterErrors {
public:
enum ErrorCode {
CellValidity = 0,
MissingCellInTC,
InvalidCellInTC,
MissingTCInModule,
InvalidTCInModule,
ConnectedModuleWithoutLpgbt,
ModuleSplitInStage1,
MissingModuleInStage1FPGA,
InvalidModuleInStage1FPGA,
MissingStage1InStage2FPGA,
InvalidStage1InStage2FPGA
};
static const std::unordered_map<ErrorCode, std::string> messages;

void fill(ErrorCode error, unsigned detid) {
auto itr_err = error_detids_.insert({error, {}}).first;
itr_err->second.insert(detid);
auto itr_id = detid_errors_.insert({detid, {}}).first;
itr_id->second.insert(error);
}
const std::unordered_map<ErrorCode, std::set<unsigned>>& errors() const { return error_detids_; }
const std::unordered_map<unsigned, std::set<ErrorCode>>& detids() const { return detid_errors_; }

private:
std::unordered_map<ErrorCode, std::set<unsigned>> error_detids_;
std::unordered_map<unsigned, std::set<ErrorCode>> detid_errors_;
};

class HGCalTriggerGeoTesterBase {
public:
HGCalTriggerGeoTesterBase(const edm::ParameterSet& conf) : name_(conf.getParameter<std::string>("TesterName")){};
virtual ~HGCalTriggerGeoTesterBase(){};
const std::string& name() const { return name_; }
virtual void initialize(TTree*, const edm::ParameterSet&) = 0;
virtual void check(const HGCalTriggerGeoTesterEventSetup&) = 0;
virtual void fill(const HGCalTriggerGeoTesterEventSetup&) = 0;
const HGCalTriggerGeoTesterErrors& errors() { return errors_; }

protected:
virtual void clear() = 0;
const std::string name_;
HGCalTriggerGeoTesterErrors errors_;
TTree* tree_;
};

#include "FWCore/PluginManager/interface/PluginFactory.h"
typedef edmplugin::PluginFactory<HGCalTriggerGeoTesterBase*(const edm::ParameterSet&)> HGCalTriggerGeoTesterFactory;

#endif
1 change: 1 addition & 0 deletions L1Trigger/L1THGCal/interface/HGCalTriggerGeometryBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class HGCalTriggerGeometryBase {
virtual unsigned getStage2FpgaFromStage1Link(const unsigned) const = 0;
virtual geom_set getStage1LinksFromStage1Fpga(const unsigned) const = 0;
virtual std::vector<unsigned> getLpgbtsFromStage1Fpga(const unsigned stage1_id) const = 0;
virtual geom_set getModulesFromStage1Fpga(const unsigned stage1_id) const = 0;
virtual unsigned getStage1FpgaFromLpgbt(const unsigned lpgbt_id) const = 0;
virtual geom_set getModulesFromLpgbt(const unsigned lpgbt_id) const = 0;
virtual geom_set getLpgbtsFromModule(const unsigned module_id) const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class HGCalTriggerGeometryV9Imp2 : public HGCalTriggerGeometryBase {
unsigned getStage2FpgaFromStage1Link(const unsigned) const final;
geom_set getStage1LinksFromStage1Fpga(const unsigned) const final;
std::vector<unsigned> getLpgbtsFromStage1Fpga(const unsigned) const final;
geom_set getModulesFromStage1Fpga(const unsigned) const final;
unsigned getStage1FpgaFromLpgbt(const unsigned) const final;
geom_set getModulesFromLpgbt(const unsigned) const final;
geom_set getLpgbtsFromModule(const unsigned) const final;
Expand Down Expand Up @@ -869,6 +870,11 @@ std::vector<unsigned> HGCalTriggerGeometryV9Imp2::getLpgbtsFromStage1Fpga(const
return lpgbt_ids;
}

HGCalTriggerGeometryBase::geom_set HGCalTriggerGeometryV9Imp2::getModulesFromStage1Fpga(const unsigned) const {
geom_set modules;
return modules;
}

unsigned HGCalTriggerGeometryV9Imp2::getStage1FpgaFromLpgbt(const unsigned) const {
unsigned stage1_id = 0;
return stage1_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class HGCalTriggerGeometryV9Imp3 : public HGCalTriggerGeometryBase {
unsigned getStage2FpgaFromStage1Link(const unsigned) const final;
geom_set getStage1LinksFromStage1Fpga(const unsigned) const final;
std::vector<unsigned> getLpgbtsFromStage1Fpga(const unsigned) const final;
geom_set getModulesFromStage1Fpga(const unsigned) const final;
unsigned getStage1FpgaFromLpgbt(const unsigned) const final;
geom_set getModulesFromLpgbt(const unsigned) const final;
geom_set getLpgbtsFromModule(const unsigned) const final;
Expand Down Expand Up @@ -671,6 +672,14 @@ std::vector<unsigned> HGCalTriggerGeometryV9Imp3::getLpgbtsFromStage1Fpga(const
return lpgbt_ids;
}

HGCalTriggerGeometryBase::geom_set HGCalTriggerGeometryV9Imp3::getModulesFromStage1Fpga(const unsigned stage1_id) const {
auto lpgbts = getLpgbtsFromStage1Fpga(stage1_id);
geom_set modules;
for (auto lpgbt : lpgbts) {
modules.merge(getModulesFromLpgbt(lpgbt));
}
return modules;
}
unsigned HGCalTriggerGeometryV9Imp3::getStage1FpgaFromLpgbt(const unsigned lpgbt_id) const {
HGCalTriggerBackendDetId id(lpgbt_id);
unsigned stage1_label = lpgbt_to_stage1_.at(id.label());
Expand Down
16 changes: 16 additions & 0 deletions L1Trigger/L1THGCal/src/HGCalTriggerGeoTesterBase.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "L1Trigger/L1THGCal/interface/HGCalTriggerGeoTesterBase.h"

const std::unordered_map<HGCalTriggerGeoTesterErrors::ErrorCode, std::string> HGCalTriggerGeoTesterErrors::messages = {
{HGCalTriggerGeoTesterErrors::CellValidity, "Found invalid cell(s)"},
{HGCalTriggerGeoTesterErrors::MissingCellInTC, "Found missing cell(s) in trigger cell"},
{HGCalTriggerGeoTesterErrors::InvalidCellInTC, "Found invalid cell(s) in trigger cell"},
{HGCalTriggerGeoTesterErrors::MissingTCInModule, "Found missing trigger cell(s) in module"},
{HGCalTriggerGeoTesterErrors::InvalidTCInModule, "Found invalid trigger cell(s) in module"},
{HGCalTriggerGeoTesterErrors::ConnectedModuleWithoutLpgbt, "Found module without lpGBT but flagged as connected"},
{HGCalTriggerGeoTesterErrors::ModuleSplitInStage1, "Found module with lpGBTs connected to several Stage 1 FPGAs"},
{HGCalTriggerGeoTesterErrors::MissingModuleInStage1FPGA, "Found missing module(s) in Stage 1 FPGA"},
{HGCalTriggerGeoTesterErrors::InvalidModuleInStage1FPGA, "Found invalid module(s) in Stage 1 FPGA"},
{HGCalTriggerGeoTesterErrors::MissingStage1InStage2FPGA, "Found missing stage1(s) in Stage 2 FPGA"},
{HGCalTriggerGeoTesterErrors::InvalidStage1InStage2FPGA, "Found invalid stage1(s) in Stage 2 FPGA"}};

EDM_REGISTER_PLUGINFACTORY(HGCalTriggerGeoTesterFactory, "HGCalTriggerGeoTesterFactory");
16 changes: 12 additions & 4 deletions L1Trigger/L1THGCal/test/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<use name="Geometry/HGCalGeometry"/>
<use name="L1Trigger/L1THGCal"/>
<use name="Geometry/Records"/>
<use name="CommonTools/UtilAlgos"/>
<library name="testL1TriggerL1THGCal" file="HGCalTriggerGeomTesterV9Imp2.cc,HGCalTriggerGeomTesterV9Imp3.cc,HGCalBackendStage1ParameterExtractor.cc">
<use name="Geometry/HGCalGeometry"/>
<use name="L1Trigger/L1THGCal"/>
<use name="Geometry/Records"/>
<use name="CommonTools/UtilAlgos"/>
<flags EDM_PLUGIN="1"/>
</library>

<library name="testL1TriggerL1THGCal_geometry" file="geometry/*.cc">
<use name="Geometry/HGCalGeometry"/>
<use name="L1Trigger/L1THGCal"/>
<use name="Geometry/Records"/>
<use name="CommonTools/UtilAlgos"/>
<flags EDM_PLUGIN="1"/>
</library>
152 changes: 152 additions & 0 deletions L1Trigger/L1THGCal/test/geometry/HGCalTriggerGeoTesterBackendStage1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@

#include "Geometry/Records/interface/CaloGeometryRecord.h"
#include "DataFormats/ForwardDetId/interface/HGCalTriggerModuleDetId.h"
#include "DataFormats/ForwardDetId/interface/HGCalTriggerBackendDetId.h"
#include "L1Trigger/L1THGCal/interface/HGCalTriggerGeometryBase.h"
#include "L1Trigger/L1THGCal/interface/HGCalTriggerGeoTesterBase.h"
#include "L1Trigger/L1THGCal/interface/HGCalTriggerTools.h"

class HGCalTriggerGeoTesterBackendStage1 : public HGCalTriggerGeoTesterBase {
public:
HGCalTriggerGeoTesterBackendStage1(const edm::ParameterSet& conf);
~HGCalTriggerGeoTesterBackendStage1() override{};
void initialize(TTree*, const edm::ParameterSet&) final;
void check(const HGCalTriggerGeoTesterEventSetup& es) final;
void fill(const HGCalTriggerGeoTesterEventSetup& es) final;

private:
void clear() final;

HGCalTriggerTools triggerTools_;

unsigned id_ = 0;
unsigned errorbits_ = 0;
int label_ = 0;
int zside_ = 0;
int sector_ = 0;
int type_ = 0;
int lpgbts_n_ = 0;
int modules_n_ = 0;
std::vector<uint32_t> lpgbts_;
std::vector<uint32_t> modules_;
};

DEFINE_EDM_PLUGIN(HGCalTriggerGeoTesterFactory,
HGCalTriggerGeoTesterBackendStage1,
"HGCalTriggerGeoTesterBackendStage1");

HGCalTriggerGeoTesterBackendStage1::HGCalTriggerGeoTesterBackendStage1(const edm::ParameterSet& conf)
: HGCalTriggerGeoTesterBase(conf) {}

void HGCalTriggerGeoTesterBackendStage1::initialize(TTree* tree, const edm::ParameterSet& conf) {
tree_ = tree;

tree_->Branch("id", &id_, "id/i");
tree_->Branch("errorbits", &errorbits_, "errorbits/i");
tree_->Branch("zside", &zside_, "zside/I");
tree_->Branch("sector", &sector_, "sector/I");
tree_->Branch("label", &label_, "label/I");
tree_->Branch("type", &type_, "type/i");
tree_->Branch("lpgbts_n", &lpgbts_n_, "lpgbts_n/I");
tree_->Branch("lpgbts", &lpgbts_);
tree_->Branch("modules_n", &modules_n_, "modules_n/I");
tree_->Branch("modules", &modules_);
}

void HGCalTriggerGeoTesterBackendStage1::fill(const HGCalTriggerGeoTesterEventSetup& es) {
clear();
edm::LogPrint("TreeFilling") << "Filling Backend Stage 1 tree";
// Create list of Stage 1 boards from valid cells
std::unordered_set<uint32_t> modules;
for (const auto& id : es.geometry->eeGeometry()->getValidDetIds()) {
modules.insert(es.geometry->getModuleFromCell(id));
}
for (const auto& id : es.geometry->hsiGeometry()->getValidDetIds()) {
modules.insert(es.geometry->getModuleFromCell(id));
}
for (const auto& id : es.geometry->hscGeometry()->getValidDetIds()) {
modules.insert(es.geometry->getModuleFromCell(id));
}
std::unordered_set<uint32_t> stage1s;
for (const auto& module : modules) {
if (es.geometry->disconnectedModule(module))
continue;
stage1s.insert(es.geometry->getStage1FpgaFromModule(module));
}
for (const auto& id : stage1s) {
HGCalTriggerBackendDetId detid(id);
const auto error_itr = errors_.detids().find(id);
if (error_itr != errors_.detids().end()) {
for (const auto& error : error_itr->second) {
errorbits_ |= (0x1 << error);
}
}
id_ = id;
zside_ = detid.zside();
type_ = detid.type();
sector_ = detid.sector();
label_ = detid.label();
auto lpgbts = es.geometry->getLpgbtsFromStage1Fpga(id);
lpgbts_n_ = lpgbts.size();
lpgbts_.resize(lpgbts.size());
std::copy(lpgbts.begin(), lpgbts.end(), lpgbts_.begin());
auto modules = es.geometry->getModulesFromStage1Fpga(id);
modules_n_ = modules.size();
modules_.resize(modules.size());
std::copy(modules.begin(), modules.end(), modules_.begin());

tree_->Fill();
clear();
}
}

void HGCalTriggerGeoTesterBackendStage1::check(const HGCalTriggerGeoTesterEventSetup& es) {
edm::LogPrint("GeoTesterBackendStage1") << "Checking Stage 1";
// Create list of modules from valid cells
std::unordered_set<uint32_t> modules;
for (const auto& id : es.geometry->eeGeometry()->getValidDetIds()) {
modules.insert(es.geometry->getModuleFromCell(id));
}
for (const auto& id : es.geometry->hsiGeometry()->getValidDetIds()) {
modules.insert(es.geometry->getModuleFromCell(id));
}
for (const auto& id : es.geometry->hscGeometry()->getValidDetIds()) {
modules.insert(es.geometry->getModuleFromCell(id));
}
std::unordered_map<uint32_t, std::unordered_set<uint32_t>> stage1_to_modules;
for (const auto& id : modules) {
if (es.geometry->disconnectedModule(id))
continue;
auto stage1 = es.geometry->getStage1FpgaFromModule(id);
auto itr_insert = stage1_to_modules.emplace(stage1, std::unordered_set<uint32_t>());
itr_insert.first->second.emplace(id);
}

// Check consistency of trigger cells included in modules
for (const auto& [stage1id, modules] : stage1_to_modules) {
HGCalTriggerGeometryBase::geom_set modules_from_stage1 = es.geometry->getModulesFromStage1Fpga(stage1id);
for (auto module : modules) {
if (modules_from_stage1.find(module) == modules_from_stage1.end()) {
errors_.fill(HGCalTriggerGeoTesterErrors::MissingModuleInStage1FPGA, stage1id);
}
}
for (auto module : modules_from_stage1) {
if (modules.find(module) == modules.end()) {
errors_.fill(HGCalTriggerGeoTesterErrors::InvalidModuleInStage1FPGA, stage1id);
}
}
}
}

void HGCalTriggerGeoTesterBackendStage1::clear() {
id_ = 0;
errorbits_ = 0;
label_ = 0;
zside_ = 0;
sector_ = 0;
type_ = 0;
lpgbts_n_ = 0;
lpgbts_.clear();
modules_n_ = 0;
modules_.clear();
}
Loading

0 comments on commit 7724d3d

Please sign in to comment.