Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate ParticlesWithPID_factory to JOmniFactory #1178

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions src/algorithms/pid/ParticlesWithPID.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <exception>
#include <limits>
#include <map>
#include <mutex>
#include <utility>
#include <vector>

#include "algorithms/pid/ConvertParticleID.h"
#include "algorithms/pid/ParticlesWithPIDConfig.h"
Expand All @@ -32,22 +33,18 @@ namespace eicrecon {

void ParticlesWithPID::init(std::shared_ptr<spdlog::logger> logger) {
m_log = logger;

}

ParticlesWithAssociation ParticlesWithPID::process(
const edm4hep::MCParticleCollection* mc_particles,
const edm4eic::TrajectoryCollection* trajectories,
std::vector<const edm4eic::CherenkovParticleIDCollection*> cherenkov_pid_collections
const edm4eic::CherenkovParticleIDCollection* drich_cherenkov_pid_collections
) {

// input collection

/// Resulting reconstructed particles
ParticlesWithAssociation out_colls;
out_colls.parts = std::make_unique<edm4eic::ReconstructedParticleCollection>();
out_colls.assocs = std::make_unique<edm4eic::MCRecoParticleAssociationCollection>();
out_colls.pids = std::make_unique<edm4hep::ParticleIDCollection>();
auto parts = std::make_unique<edm4eic::ReconstructedParticleCollection>();
auto assocs = std::make_unique<edm4eic::MCRecoParticleAssociationCollection>();
auto pids = std::make_unique<edm4hep::ParticleIDCollection>();

const double sinPhiOver2Tolerance = sin(0.5 * m_cfg.phiTolerance);
tracePhiToleranceOnce(sinPhiOver2Tolerance, m_cfg.phiTolerance);
Expand Down Expand Up @@ -140,7 +137,7 @@ namespace eicrecon {
}
}
}
auto rec_part = out_colls.parts->create();
auto rec_part = parts->create();
int32_t best_pid = 0;
auto referencePoint = rec_part.referencePoint();
// float time = 0;
Expand Down Expand Up @@ -169,18 +166,16 @@ namespace eicrecon {
// rec_part.covMatrix() // @TODO: covariance matrix on 4-momentum

// link Cherenkov PID objects
for (const auto& cherenkov_pids : cherenkov_pid_collections) {
auto success = linkCherenkovPID(rec_part, *cherenkov_pids, *(out_colls.pids));
if (success)
m_log->trace(" true PDG vs. CherenkovPID PDG: {:>10} vs. {:<10}",
best_pid,
rec_part.getParticleIDUsed().isAvailable() ? rec_part.getParticleIDUsed().getPDG() : 0
);
}
auto success = linkCherenkovPID(rec_part, *drich_cherenkov_pid_collections, *pids);
if (success)
m_log->trace(" true PDG vs. CherenkovPID PDG: {:>10} vs. {:<10}",
best_pid,
rec_part.getParticleIDUsed().isAvailable() ? rec_part.getParticleIDUsed().getPDG() : 0
);

// Also write MC <--> truth particle association if match was found
if (best_match >= 0) {
auto rec_assoc = out_colls.assocs->create();
auto rec_assoc = assocs->create();
rec_assoc.setRecID(rec_part.getObjectID().index);
rec_assoc.setSimID((*mc_particles)[best_match].getObjectID().index);
rec_assoc.setWeight(1);
Expand Down Expand Up @@ -216,7 +211,7 @@ namespace eicrecon {
}
}

return out_colls;
return std::make_tuple(std::move(parts), std::move(assocs), std::move(pids));
}

void ParticlesWithPID::tracePhiToleranceOnce(const double sinPhiOver2Tolerance, double phiTolerance) {
Expand Down
14 changes: 7 additions & 7 deletions src/algorithms/pid/ParticlesWithPID.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
#include <edm4hep/ParticleIDCollection.h>
#include <spdlog/logger.h>
#include <memory>
#include <vector>
#include <tuple>

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


namespace eicrecon {

struct ParticlesWithAssociation {
std::unique_ptr<edm4eic::ReconstructedParticleCollection> parts;
std::unique_ptr<edm4eic::MCRecoParticleAssociationCollection> assocs;
std::unique_ptr<edm4hep::ParticleIDCollection> pids;
};
using ParticlesWithAssociation = std::tuple<
std::unique_ptr<edm4eic::ReconstructedParticleCollection>,
std::unique_ptr<edm4eic::MCRecoParticleAssociationCollection>,
std::unique_ptr<edm4hep::ParticleIDCollection>
>;

class ParticlesWithPID : public WithPodConfig<ParticlesWithPIDConfig> {

Expand All @@ -35,7 +35,7 @@ namespace eicrecon {
ParticlesWithAssociation process(
const edm4hep::MCParticleCollection* mc_particles,
const edm4eic::TrajectoryCollection* track_params,
std::vector<const edm4eic::CherenkovParticleIDCollection*> cherenkov_pid_collections
const edm4eic::CherenkovParticleIDCollection* drich_cherenkov_pid_collections
);

private:
Expand Down
38 changes: 0 additions & 38 deletions src/global/pid/ParticlesWithPID_factory.cc

This file was deleted.

60 changes: 31 additions & 29 deletions src/global/pid/ParticlesWithPID_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,40 @@

#include "algorithms/pid/ParticlesWithPID.h"
#include "algorithms/pid/ParticlesWithPIDConfig.h"
#include "extensions/jana/JChainMultifactoryT.h"
#include "extensions/spdlog/SpdlogMixin.h"
#include "extensions/jana/JOmniFactory.h"


namespace eicrecon {

class ParticlesWithPID_factory :
public JChainMultifactoryT<ParticlesWithPIDConfig>,
public SpdlogMixin {

public:
explicit ParticlesWithPID_factory( std::string tag,
const std::vector<std::string>& input_tags,
const std::vector<std::string>& output_tags,
ParticlesWithPIDConfig cfg):
JChainMultifactoryT<ParticlesWithPIDConfig>(std::move(tag), input_tags, output_tags, cfg) {

if (GetOutputTags().size() != 3)
throw JException("incorrect number of input tags");
DeclarePodioOutput<edm4eic::ReconstructedParticle>(GetOutputTags().at(0));
DeclarePodioOutput<edm4eic::MCRecoParticleAssociation>(GetOutputTags().at(1));
DeclarePodioOutput<edm4hep::ParticleID>(GetOutputTags().at(2));
}

/** One time initialization **/
void Init() override;

/** Event by event processing **/
void Process(const std::shared_ptr<const JEvent> &event) override;

private:
eicrecon::ParticlesWithPID m_matching_algo;
};
class ParticlesWithPID_factory : public JOmniFactory<ParticlesWithPID_factory, ParticlesWithPIDConfig> {

private:
ParticlesWithPID m_algo;

PodioInput<edm4hep::MCParticle> m_mc_particles_input {this};
PodioInput<edm4eic::Trajectory> m_trajectories_input {this};
PodioInput<edm4eic::CherenkovParticleID> m_drich_particle_id_input {this};

PodioOutput<edm4eic::ReconstructedParticle> m_particles_output {this};
PodioOutput<edm4eic::MCRecoParticleAssociation> m_particles_assoc_output {this};
PodioOutput<edm4hep::ParticleID> m_particle_id_output {this};

ParameterRef<double> m_momentumRelativeTolerance {this, "momentumRelativeTolerance", config().momentumRelativeTolerance};
ParameterRef<double> m_phiTolerance {this, "phiTolerance", config().phiTolerance};
ParameterRef<double> m_etaTolerance {this, "etaTolerance", config().etaTolerance};

public:
void Configure() {
m_algo.applyConfig(config());
m_algo.init(logger());
}

void ChangeRun(int64_t run_number) {
}

void Process(int64_t run_number, uint64_t event_number) {
std::tie(m_particles_output(), m_particles_assoc_output(), m_particle_id_output()) = m_algo.process(m_mc_particles_input(), m_trajectories_input(), m_drich_particle_id_input());
}
};

} // eicrecon
6 changes: 3 additions & 3 deletions src/global/pid/pid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "algorithms/interfaces/WithPodConfig.h"
// algorithm configurations
#include "algorithms/pid/ParticlesWithPIDConfig.h"
#include "extensions/jana/JChainMultifactoryGeneratorT.h"
#include "extensions/jana/JOmniFactoryGeneratorT.h"
// factories
#include "global/pid/ParticlesWithPID_factory.h"

Expand All @@ -31,7 +31,7 @@ extern "C" {
// clang-format off

// link charged particles to PID and to MC truth
app->Add(new JChainMultifactoryGeneratorT<ParticlesWithPID_factory>(
app->Add(new JOmniFactoryGeneratorT<ParticlesWithPID_factory>(
"ChargedParticlesWithAssociations",
{
"MCParticles", // edm4hep::MCParticle
Expand All @@ -47,7 +47,7 @@ extern "C" {
app
));

app->Add(new JChainMultifactoryGeneratorT<ParticlesWithPID_factory>(
app->Add(new JOmniFactoryGeneratorT<ParticlesWithPID_factory>(
"ChargedParticlesWithAssociations",
{
"MCParticles", // edm4hep::MCParticle
Expand Down
Loading