Skip to content

Commit

Permalink
added PrimaryVertices factory (subCollection of CentralTrackVertices) (
Browse files Browse the repository at this point in the history
…#1609)

### Briefly, what does this PR introduce?


### What kind of change does this PR introduce?
- [ ] Bug fix (issue #__)
- [X] 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?

### Does this PR change default behavior?

---------

Co-authored-by: Xin Dong <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Xin Dong <[email protected]>
Co-authored-by: Dmitry Kalinkin <[email protected]>
  • Loading branch information
5 people authored Sep 17, 2024
1 parent f833c1b commit ca9579e
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/algorithms/reco/PrimaryVertices.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024 Daniel Brandenburg, Xin Dong

#include <edm4eic/ReconstructedParticle.h>
#include <edm4eic/VertexCollection.h>
#include <edm4eic/unit_system.h>
#include <edm4hep/Vector4f.h>
#include <fmt/core.h>
#include <podio/RelationRange.h>
#include <cmath>
#include <functional>
#include <gsl/pointers>
#include <map>

#include "algorithms/reco/PrimaryVertices.h"
#include "algorithms/reco/PrimaryVerticesConfig.h"

namespace eicrecon {

/**
* @brief Initialize the PrimaryVertices Algorithm
*
*/
void PrimaryVertices::init() {
}

/**
* @brief Produce a list of primary vertex candidates
*
* @param rcvtx - input collection of all vertex candidates
* @return edm4eic::VertexCollection
*/
void PrimaryVertices::process(const PrimaryVertices::Input& input,
const PrimaryVertices::Output& output) const {
const auto [rcvtx] = input;
auto [out_primary_vertices] = output;

// this multimap will store intermediate results
// so that we can sort them before filling output
// collection
std::multimap<int, edm4eic::Vertex, std::greater<int>> primaryVertexMap;

// our output collection of primary vertex
// ordered by N_trk = associatedParticle array size
out_primary_vertices->setSubsetCollection();

trace("We have {} candidate vertices", rcvtx->size());

for (const auto& vtx: *rcvtx) {
const auto &v = vtx.getPosition();

// some basic vertex selection
if (sqrt( v.x*v.x + v.y*v.y ) / edm4eic::unit::mm > m_cfg.maxVr ||
fabs( v.z ) / edm4eic::unit::mm > m_cfg.maxVz )
continue;

if (vtx.getChi2() > m_cfg.maxChi2) continue;

int N_trk = vtx.getAssociatedParticles().size();
trace( "\t N_trk = {}", N_trk );
primaryVertexMap.insert({N_trk, vtx});
} // vertex loop

bool first = true;
// map defined with std::greater<> will be iterated in descending order by the key
for (auto [N_trk, vertex] : primaryVertexMap) {
// Do not save primary candidates that
// are not within range
if ( N_trk > m_cfg.maxNtrk
|| N_trk < m_cfg.minNtrk ){
continue;
}

// For logging and development
// report the highest N_trk candidate chosen
if (first) {
trace("Max N_trk Candidate:");
trace("\t N_trk = {}", N_trk);
trace("\t Primary vertex has xyz=( {}, {}, {} )", vertex.getPosition().x, vertex.getPosition().y, vertex.getPosition().z);
first = false;
}
out_primary_vertices->push_back(vertex);
} // loop on primaryVertexMap
}
} // namespace eicrecon
38 changes: 38 additions & 0 deletions src/algorithms/reco/PrimaryVertices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024 Daniel Brandenburg, Xin Dong

#pragma once

#include <algorithms/algorithm.h>
#include <edm4eic/VertexCollection.h>
#include <string> // for basic_string
#include <string_view> // for string_view

#include "algorithms/interfaces/WithPodConfig.h"
#include "algorithms/reco/PrimaryVerticesConfig.h"


namespace eicrecon {

using PrimaryVerticesAlgorithm = algorithms::Algorithm<
algorithms::Input<edm4eic::VertexCollection>,
algorithms::Output<edm4eic::VertexCollection>>;

class PrimaryVertices :
public PrimaryVerticesAlgorithm,
public WithPodConfig<PrimaryVerticesConfig>{

public:
PrimaryVertices(std::string_view name)
: PrimaryVerticesAlgorithm{name,
{"inputVertices"},
{"outputPrimaryVertices"},
"Sort reconstructed vertices in PrimaryVertices collection"} {}

void init() final;
void process(const Input&, const Output&) const final;

private:

};
} // namespace eicrecon
23 changes: 23 additions & 0 deletions src/algorithms/reco/PrimaryVerticesConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024 Xin Dong

#pragma once

#include <string>
#include <DD4hep/DD4hepUnits.h>

namespace eicrecon {

struct PrimaryVerticesConfig {

// For now these are wide open
// In the future the cut should depend
// on the generator settings
float maxVr = 50.0; // mm
float maxVz = 500.0; // mm
float maxChi2 = 10000.0; //
int minNtrk = 1; // >=
int maxNtrk = 1000000; // <=
};

} // end eicrecon namespace
51 changes: 51 additions & 0 deletions src/global/reco/PrimaryVertices_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024 Xin Dong

#pragma once

#include <JANA/JEvent.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "algorithms/reco/PrimaryVertices.h"
#include "algorithms/reco/PrimaryVerticesConfig.h"
#include "services/algorithms_init/AlgorithmsInit_service.h"
#include "extensions/jana/JOmniFactory.h"

namespace eicrecon {

class PrimaryVertices_factory :
public JOmniFactory<PrimaryVertices_factory, PrimaryVerticesConfig> {

public:
using AlgoT = eicrecon::PrimaryVertices;
private:
std::unique_ptr<AlgoT> m_algo;

PodioInput<edm4eic::Vertex> m_rc_vertices_input {this};

// Declare outputs
PodioOutput<edm4eic::Vertex> m_primary_vertices_output {this};

Service<AlgorithmsInit_service> m_algorithmsInit {this};

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

m_algo->applyConfig(config());
m_algo->init();
}

void ChangeRun(int64_t run_number) {
}

void Process(int64_t run_number, uint64_t event_number) {
m_algo->process({m_rc_vertices_input()}, {m_primary_vertices_output().get()});
}
};

} // eicrecon
15 changes: 15 additions & 0 deletions src/global/reco/reco.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <edm4eic/MCRecoParticleAssociation.h>
#include <edm4eic/ReconstructedParticle.h>
#include <edm4hep/MCParticle.h>
#include <fmt/core.h>
#include <map>
#include <memory>

Expand Down Expand Up @@ -44,6 +45,7 @@
#include "global/reco/ChargedReconstructedParticleSelector_factory.h"
#include "global/reco/MC2SmearedParticle_factory.h"
#include "global/reco/MatchClusters_factory.h"
#include "global/reco/PrimaryVertices_factory.h"
#include "global/reco/ReconstructedElectrons_factory.h"
#include "global/reco/ScatteredElectronsEMinusPz_factory.h"
#include "global/reco/ScatteredElectronsTruth_factory.h"
Expand Down Expand Up @@ -384,6 +386,19 @@ void InitPlugin(JApplication *app) {
app
));

app->Add(new JOmniFactoryGeneratorT<PrimaryVertices_factory>(
"PrimaryVertices",
{
"CentralTrackVertices"
},
{
"PrimaryVertices"
},
{
},
app
));


}
} // extern "C"
1 change: 1 addition & 0 deletions src/services/io/podio/JEventProcessorPODIO.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ JEventProcessorPODIO::JEventProcessorPODIO() {
"ReconstructedElectrons",
"ScatteredElectronsTruth",
"ScatteredElectronsEMinusPz",
"PrimaryVertices",
#if EDM4EIC_VERSION_MAJOR >= 6
"HadronicFinalState",
#endif
Expand Down

0 comments on commit ca9579e

Please sign in to comment.