Skip to content

Commit

Permalink
ScatteredElectronsEMinusPz: refactor to use algorithms interface and …
Browse files Browse the repository at this point in the history
…ParticleSvc (#1615)

### Briefly, what does this PR introduce?
Refactor to use algorithms interface and ParticleSvc.

### What kind of change does this PR introduce?
- [ ] Bug fix (issue #__)
- [ ] New feature (issue #__)
- [ ] Documentation update
- [ ] Other: __

### Please check if this PR fulfills the following:
- [ ] Tests for the changes have been added
- [ ] Documentation has been added / updated
- [ ] Changes have been communicated to collaborators

### Does this PR introduce breaking changes? What changes might users
need to make to their code?
No

### Does this PR change default behavior?
No
  • Loading branch information
veprbl authored Sep 12, 2024
1 parent af290dc commit 0e1566c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 58 deletions.
64 changes: 26 additions & 38 deletions src/algorithms/reco/ScatteredElectronsEMinusPz.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024 Daniel Brandenburg
// Copyright (C) 2024 Daniel Brandenburg, Dmitry Kalinkin

#include <Math/GenVector/LorentzVector.h>
#include <Math/GenVector/PxPyPzM4D.h>
Expand All @@ -8,7 +8,8 @@
#include <edm4hep/Vector3f.h>
#include <fmt/core.h>
#include <podio/ObjectID.h>
#include <iterator>
#include <functional>
#include <gsl/pointers>
#include <map>
#include <utility>

Expand All @@ -22,12 +23,8 @@ namespace eicrecon {

/**
* @brief Initialize the ScatteredElectronsEMinusPz Algorithm
*
* @param logger
*/
void ScatteredElectronsEMinusPz::init(std::shared_ptr<spdlog::logger>& logger) {
m_log = logger;
}
void ScatteredElectronsEMinusPz::init() { }

/**
* @brief Produce a list of scattered electron candidates
Expand All @@ -36,26 +33,24 @@ namespace eicrecon {
* @param rcele - input collection of all electron candidates
* @return std::unique_ptr<edm4eic::ReconstructedParticleCollection>
*/
std::unique_ptr<edm4eic::ReconstructedParticleCollection> ScatteredElectronsEMinusPz::execute(
const edm4eic::ReconstructedParticleCollection *rcparts,
const edm4eic::ReconstructedParticleCollection *rcele
){
void ScatteredElectronsEMinusPz::process(
const ScatteredElectronsEMinusPz::Input& input,
const ScatteredElectronsEMinusPz::Output& output
) const {
auto [rcparts, rcele] = input;
auto [out_electrons] = output;

static const auto m_electron = m_particleSvc.particle(11).mass;
static const auto m_pion = m_particleSvc.particle(211).mass;

// this map will store intermediate results
// so that we can sort them before filling output
// collection
std::map<double, edm4eic::ReconstructedParticle> scatteredElectronsMap;
std::map<double, edm4eic::ReconstructedParticle, std::greater<double>> scatteredElectronsMap;

// our output collection of scattered electrons
// ordered by E-Pz
auto out_electrons = std::make_unique<
edm4eic::ReconstructedParticleCollection
>();
out_electrons->setSubsetCollection();

m_log->trace( "We have {} candidate electrons",
rcele->size()
);
trace("We have {} candidate electrons", rcele->size());

// Lorentz Vector for the scattered electron,
// hadronic final state, and individual hadron
Expand Down Expand Up @@ -95,7 +90,7 @@ namespace eicrecon {
// Sum hadronic final state
vHadronicFinalState += vHadron;
} else {
m_log->trace( "Skipping electron in hadronic final state" );
trace("Skipping electron in hadronic final state");
}
} // hadron loop (reconstructed particles)

Expand All @@ -105,22 +100,20 @@ namespace eicrecon {
// candidates but we will rank them by their E-Pz
double EPz=(vScatteredElectron+vHadronicFinalState).E()
- (vScatteredElectron+vHadronicFinalState).Pz();
m_log->trace( "\tE-Pz={}", EPz );
m_log->trace( "\tScatteredElectron has Pxyz=( {}, {}, {} )", e.getMomentum().x, e.getMomentum().y, e.getMomentum().z );
trace("\tE-Pz={}", EPz);
trace("\tScatteredElectron has Pxyz=( {}, {}, {} )", e.getMomentum().x, e.getMomentum().y, e.getMomentum().z);

// Store the result of this calculation
scatteredElectronsMap[ EPz ] = e;
} // electron loop

m_log->trace( "Selecting candidates with {} < E-Pz < {}", m_cfg.minEMinusPz, m_cfg.maxEMinusPz );
trace("Selecting candidates with {} < E-Pz < {}", m_cfg.minEMinusPz, m_cfg.maxEMinusPz);

// map sorts in descending order by default
// sort by descending
bool first = true;
// for (auto kv : scatteredElectronsMap) {
for (auto kv = scatteredElectronsMap.rbegin(); kv != scatteredElectronsMap.rend(); ++kv) {
// map defined with std::greater<> will be iterated in descending order by the key
for (auto kv : scatteredElectronsMap) {

double EMinusPz = kv->first;
double EMinusPz = kv.first;
// Do not save electron candidates that
// are not within range
if ( EMinusPz > m_cfg.maxEMinusPz
Expand All @@ -131,18 +124,13 @@ namespace eicrecon {
// For logging and development
// report the highest E-Pz candidate chosen
if ( first ){
m_log->trace( "Max E-Pz Candidate:" );
m_log->trace( "\tE-Pz={}", EMinusPz );
m_log->trace( "\tScatteredElectron has Pxyz=( {}, {}, {} )", kv->second.getMomentum().x, kv->second.getMomentum().y, kv->second.getMomentum().z );
trace("Max E-Pz Candidate:");
trace("\tE-Pz={}", EMinusPz);
trace("\tScatteredElectron has Pxyz=( {}, {}, {} )", kv.second.getMomentum().x, kv.second.getMomentum().y, kv.second.getMomentum().z);
first = false;
}
out_electrons->push_back( kv->second );
out_electrons->push_back( kv.second );
} // reverse loop on scatteredElectronsMap


// Return Electron candidates ranked
// in order from largest E-Pz to smallest
return out_electrons;
}

} // namespace eicrecon
44 changes: 29 additions & 15 deletions src/algorithms/reco/ScatteredElectronsEMinusPz.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024 Daniel Brandenburg
// Copyright (C) 2024 Daniel Brandenburg, Dmitry Kalinkin

#pragma once

#include <algorithms/algorithm.h>
#include <edm4eic/ReconstructedParticleCollection.h>
#include <spdlog/logger.h>
#include <memory>
#include <string>
#include <string_view>

#include "algorithms/reco/ScatteredElectronsEMinusPzConfig.h"
#include "algorithms/interfaces/ParticleSvc.h"
#include "algorithms/interfaces/WithPodConfig.h"
#include "algorithms/reco/ScatteredElectronsEMinusPzConfig.h"


namespace eicrecon {

class ScatteredElectronsEMinusPz : public WithPodConfig<ScatteredElectronsEMinusPzConfig>{
using ScatteredElectronsEMinusPzAlgorithm = algorithms::Algorithm<
algorithms::Input<
edm4eic::ReconstructedParticleCollection,
edm4eic::ReconstructedParticleCollection
>,
algorithms::Output<
edm4eic::ReconstructedParticleCollection
>
>;

class ScatteredElectronsEMinusPz : public ScatteredElectronsEMinusPzAlgorithm, WithPodConfig<ScatteredElectronsEMinusPzConfig>{

public:

public:
ScatteredElectronsEMinusPz(std::string_view name)
: ScatteredElectronsEMinusPzAlgorithm{name,
{"inputParticles", "inputElectronCandidates"},
{"outputElectrons"},
"Outputs DIS electrons ordered in decreasing E-pz"} {}
void init() final;
void process(const Input&, const Output&) const final;

void init(std::shared_ptr<spdlog::logger>& logger);
std::unique_ptr<edm4eic::ReconstructedParticleCollection> execute(
const edm4eic::ReconstructedParticleCollection *rcparts,
const edm4eic::ReconstructedParticleCollection *rcele
);
private:
const algorithms::ParticleSvc& m_particleSvc = algorithms::ParticleSvc::instance();

private:
std::shared_ptr<spdlog::logger> m_log;
double m_electron{0.000510998928}, m_pion{0.13957};
};

};
} // namespace eicrecon
12 changes: 7 additions & 5 deletions src/global/reco/ScatteredElectronsEMinusPz_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ class ScatteredElectronsEMinusPz_factory :
// Declare outputs
PodioOutput<edm4eic::ReconstructedParticle> m_out_reco_particles {this};

Service<AlgorithmsInit_service> m_algorithmsInit {this};

public:
void Configure() {
m_algo = std::make_unique<AlgoT>();
m_algo->init(logger());
m_algo->applyConfig(config());
m_algo = std::make_unique<AlgoT>(GetPrefix());
m_algo->level(static_cast<algorithms::LogLevel>(logger()->level()));
m_algo->init();
}

void ChangeRun(int64_t run_number) {
}

void Process(int64_t run_number, uint64_t event_number) {
auto output = m_algo->execute(m_rc_particles_input(), m_rc_electrons_input());
m_out_reco_particles() = std::move(output);
m_algo->process({m_rc_particles_input(), m_rc_electrons_input()},
{m_out_reco_particles().get()});
}
};

Expand Down

0 comments on commit 0e1566c

Please sign in to comment.