Skip to content

Commit

Permalink
feat: CalorimeterHitsMerger factory simplification
Browse files Browse the repository at this point in the history
This applies the #666 treatment to the CalorimeterHitsMerger algorithms:

- put all configuration in a single configuration struct for use with
WithPodConfig mixin,
- create a templated factory-factory to use in the plugins,
- replace all old explicit CalorimeterHit factories with the new
factory-factory.
  • Loading branch information
wdconinc committed Jul 28, 2023
1 parent bcc2375 commit f246571
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 265 deletions.
32 changes: 19 additions & 13 deletions src/algorithms/calorimetry/CalorimeterHitsMerger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,40 @@

#include "algorithms/calorimetry/CalorimeterHitsMerger.h"

void CalorimeterHitsMerger::initialize() {
namespace eicrecon {

if (m_readout.empty()) {
void CalorimeterHitsMerger::init(const dd4hep::Detector* detector, std::shared_ptr<spdlog::logger>& logger) {
m_detector = detector;
m_converter = std::make_shared<const dd4hep::rec::CellIDPositionConverter>(const_cast<dd4hep::Detector&>(*detector));
m_log = logger;

if (m_cfg.readout.empty()) {
m_log->error("readoutClass is not provided, it is needed to know the fields in readout ids");
return;
}

try {
auto id_desc = m_geoSvc->detector()->readout(m_readout).idSpec();
auto id_desc = m_detector->readout(m_cfg.readout).idSpec();
id_mask = 0;
std::vector<std::pair<std::string, int>> ref_fields;
for (size_t i = 0; i < u_fields.size(); ++i) {
id_mask |= id_desc.field(u_fields[i])->mask();
for (size_t i = 0; i < m_cfg.fields.size(); ++i) {
id_mask |= id_desc.field(m_cfg.fields[i])->mask();
// use the provided id number to find ref cell, or use 0
int ref = i < u_refs.size() ? u_refs[i] : 0;
ref_fields.emplace_back(u_fields[i], ref);
int ref = i < m_cfg.refs.size() ? m_cfg.refs[i] : 0;
ref_fields.emplace_back(m_cfg.fields[i], ref);
}
ref_mask = id_desc.encode(ref_fields);
// debug() << fmt::format("Reference id mask for the fields {:#064b}", ref_mask) << endmsg;
} catch (...) {
auto mess = fmt::format("Failed to load ID decoder for {}", m_readout);
auto mess = fmt::format("Failed to load ID decoder for {}", m_cfg.readout);
m_log->warn(mess);
// throw std::runtime_error(mess);
}
id_mask = ~id_mask;
m_log->info(fmt::format("ID mask in {:s}: {:#064b}", m_readout, id_mask));
m_log->info(fmt::format("ID mask in {:s}: {:#064b}", m_cfg.readout, id_mask));
}

std::unique_ptr<edm4eic::CalorimeterHitCollection> CalorimeterHitsMerger::execute(const edm4eic::CalorimeterHitCollection &input) {
std::unique_ptr<edm4eic::CalorimeterHitCollection> CalorimeterHitsMerger::process(const edm4eic::CalorimeterHitCollection &input) {
auto output = std::make_unique<edm4eic::CalorimeterHitCollection>();

// find the hits that belong to the same group (for merging)
Expand All @@ -60,14 +65,13 @@ std::unique_ptr<edm4eic::CalorimeterHitCollection> CalorimeterHitsMerger::execut

// reconstruct info for merged hits
// dd4hep decoders
auto poscon = m_geoSvc->cellIDPositionConverter();
auto volman = m_geoSvc->detector()->volumeManager();
auto volman = m_detector->volumeManager();

for (const auto &[id, ixs] : merge_map) {
// reference fields id
const uint64_t ref_id = id | ref_mask;
// global positions
const auto gpos = poscon->position(ref_id);
const auto gpos = m_converter->position(ref_id);
// local positions
auto alignment = volman.lookupDetElement(ref_id).nominal();
const auto pos = alignment.worldToLocal(dd4hep::Position(gpos.x(), gpos.y(), gpos.z()));
Expand Down Expand Up @@ -115,3 +119,5 @@ std::unique_ptr<edm4eic::CalorimeterHitCollection> CalorimeterHitsMerger::execut

return output;
}

} // namespace eicrecon
32 changes: 17 additions & 15 deletions src/algorithms/calorimetry/CalorimeterHitsMerger.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@
#include <tuple>
#include <unordered_map>

#include "services/geometry/dd4hep/JDD4hep_service.h"
#include <Evaluator/DD4hepUnits.h>
#include <DD4hep/Detector.h>
#include <DDRec/CellIDPositionConverter.h>

#include <edm4eic/CalorimeterHitCollection.h>
#include <edm4eic/vector_utils.h>
#include <spdlog/spdlog.h>

#include "algorithms/interfaces/WithPodConfig.h"
#include "CalorimeterHitsMergerConfig.h"

namespace eicrecon {

class CalorimeterHitsMerger {
public:
class CalorimeterHitsMerger : public WithPodConfig<CalorimeterHitsMergerConfig> {

std::string m_readout;
std::vector<std::string> u_fields;
std::vector<int> u_refs;
public:
void init(const dd4hep::Detector* detector, std::shared_ptr<spdlog::logger>& logger);
std::unique_ptr<edm4eic::CalorimeterHitCollection> process(const edm4eic::CalorimeterHitCollection &input);

std::shared_ptr<JDD4hep_service> m_geoSvc;
private:
uint64_t id_mask{0}, ref_mask{0};

private:
const dd4hep::Detector* m_detector;
std::shared_ptr<const dd4hep::rec::CellIDPositionConverter> m_converter;
std::shared_ptr<spdlog::logger> m_log;

public:
CalorimeterHitsMerger() = default;
~CalorimeterHitsMerger() {}

void initialize();
std::unique_ptr<edm4eic::CalorimeterHitCollection> execute(const edm4eic::CalorimeterHitCollection &input);
};

}; // class CalorimeterHitsMerger
} // namespace eicrecon
19 changes: 19 additions & 0 deletions src/algorithms/calorimetry/CalorimeterHitsMergerConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Subject to the terms in the LICENSE file found in the top-level directory.
//

#pragma once

#include <string>
#include <vector>

namespace eicrecon {

struct CalorimeterHitsMergerConfig {

std::string readout{""};
std::vector<std::string> fields{};
std::vector<int> refs{};

};

} // eicrecon
3 changes: 2 additions & 1 deletion src/detectors/BEMC/BEMC.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include "factories/calorimetry/CalorimeterClusterRecoCoG_factoryT.h"
#include "factories/calorimetry/CalorimeterHitDigi_factoryT.h"
#include "factories/calorimetry/CalorimeterHitReco_factoryT.h"
#include "factories/calorimetry/CalorimeterHitsMerger_factoryT.h"

#include "ProtoCluster_factory_EcalBarrelSciGlassTruthProtoClusters.h"
#include "ProtoCluster_factory_EcalBarrelSciGlassProtoClusters.h"
#include "Cluster_factory_EcalBarrelSciGlassMergedTruthClusters.h"

#include "CalorimeterHit_factory_EcalBarrelScFiMergedHits.h"
#include "ProtoCluster_factory_EcalBarrelScFiProtoClusters.h"

#include "CalorimeterHit_factory_EcalBarrelImagingRecHits.h"
Expand All @@ -28,6 +28,7 @@ namespace eicrecon {
using RawCalorimeterHit_factory_EcalBarrelImagingRawHits = CalorimeterHitDigi_factoryT<>;
using CalorimeterHit_factory_EcalBarrelSciGlassRecHits = CalorimeterHitReco_factoryT<>;
using CalorimeterHit_factory_EcalBarrelScFiRecHits = CalorimeterHitReco_factoryT<>;
using CalorimeterHit_factory_EcalBarrelScFiMergedHits = CalorimeterHitsMerger_factoryT<>;
using Cluster_factory_EcalBarrelSciGlassTruthClusters = CalorimeterClusterRecoCoG_factoryT<>;
using Cluster_factory_EcalBarrelSciGlassClusters = CalorimeterClusterRecoCoG_factoryT<>;
using Cluster_factory_EcalBarrelScFiClusters = CalorimeterClusterRecoCoG_factoryT<>;
Expand Down

This file was deleted.

57 changes: 0 additions & 57 deletions src/detectors/EHCAL/CalorimeterHit_factory_HcalEndcapNMergedHits.h

This file was deleted.

13 changes: 10 additions & 3 deletions src/detectors/EHCAL/EHCAL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
#include "factories/calorimetry/CalorimeterClusterRecoCoG_factoryT.h"
#include "factories/calorimetry/CalorimeterHitDigi_factoryT.h"
#include "factories/calorimetry/CalorimeterHitReco_factoryT.h"
#include "factories/calorimetry/CalorimeterHitsMerger_factoryT.h"

#include "CalorimeterHit_factory_HcalEndcapNMergedHits.h"
#include "ProtoCluster_factory_HcalEndcapNTruthProtoClusters.h"
#include "ProtoCluster_factory_HcalEndcapNIslandProtoClusters.h"


namespace eicrecon {
using RawCalorimeterHit_factory_HcalEndcapNRawHits = CalorimeterHitDigi_factoryT<>;
using CalorimeterHit_factory_HcalEndcapNRecHits = CalorimeterHitReco_factoryT<>;
using CalorimeterHit_factory_HcalEndcapNMergedHits = CalorimeterHitsMerger_factoryT<>;
using Cluster_factory_HcalEndcapNTruthClusters = CalorimeterClusterRecoCoG_factoryT<>;
using Cluster_factory_HcalEndcapNClusters = CalorimeterClusterRecoCoG_factoryT<>;
}
Expand Down Expand Up @@ -57,8 +58,14 @@ extern "C" {
},
app // TODO: Remove me once fixed
));
app->Add(new JChainFactoryGeneratorT<CalorimeterHit_factory_HcalEndcapNMergedHits>(
{"HcalEndcapNRecHits"}, "HcalEndcapNMergedHits"
app->Add(new JChainMultifactoryGeneratorT<CalorimeterHit_factory_HcalEndcapNMergedHits>(
"HcalEndcapNMergedHits", {"HcalEndcapNRecHits"}, {"HcalEndcapNMergedHits"},
{
.readout = "HcalEndcapNHits",
.fields = {"layer", "slice"},
.refs = {1, 0},
},
app // TODO: Remove me once fixed
));
app->Add(new JChainFactoryGeneratorT<ProtoCluster_factory_HcalEndcapNTruthProtoClusters>(
{"HcalEndcapNRecHits", "HcalEndcapNHits"}, "HcalEndcapNTruthProtoClusters"
Expand Down

This file was deleted.

Loading

0 comments on commit f246571

Please sign in to comment.