ATLAS Offline Software
Loading...
Searching...
No Matches
ParticleSortingTool.h
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5*/
6
7// ParticleSortingTool.h
8// Header file for class ParticleSortingTool
9// Author: Karsten Koeneke <karsten.koeneke@cern.ch>
11#ifndef EVENTUTILS_PARTICLESORTINGTOOL_H
12#define EVENTUTILS_PARTICLESORTINGTOOL_H 1
13
14// FrameWork includes
17#include "CxxUtils/fpcompare.h"
18
19// EDM inlcudes
20#include "xAODBase/IParticle.h"
23
24// STL includes
25#include <vector>
26#include <string>
27#include <cmath>
28
31 public ::AthAlgTool
32{
33
35 // Public methods:
37public:
38
39 // Copy constructor:
40
42 ParticleSortingTool( const std::string& type,
43 const std::string& name,
44 const IInterface* parent );
45
47 virtual ~ParticleSortingTool();
48
50 virtual StatusCode initialize() override;
51
53 virtual StatusCode finalize() override;
54
55
57 virtual StatusCode addBranches(const EventContext& ctx) const final override;
58
59
60
61// Private methods
62private:
63
65 StatusCode doSort( xAOD::IParticleContainer* cont ) const;
66
68 template<class CONTAINERTYPE>
69 StatusCode doSortConst( ConstDataVector<CONTAINERTYPE>* cont ) const;
70
72 bool comparePt( const xAOD::IParticle* partA, const xAOD::IParticle* partB ) const;
73
75 bool compareEta( const xAOD::IParticle* partA, const xAOD::IParticle* partB ) const;
76
78 bool comparePhi( const xAOD::IParticle* partA, const xAOD::IParticle* partB ) const;
79
81 bool compareMass( const xAOD::IParticle* partA, const xAOD::IParticle* partB ) const;
82
84 bool compareEnergy( const xAOD::IParticle* partA, const xAOD::IParticle* partB ) const;
85
87 bool compareRapidity( const xAOD::IParticle* partA, const xAOD::IParticle* partB ) const;
88
90 bool compareAuxData( const xAOD::IParticle* partA, const xAOD::IParticle* partB ) const;
91
93 inline bool compareDouble( double a, double b ) const;
94
95
97 // Private data:
99private:
100
102 StringProperty m_inCollKey{ this, "InputContainer", "", "Input container name" };
103
105 StringProperty m_outCollKey{ this, "OutputContainer", "", "The name of the output container (with SG::VIEW_ELEMENTS) with the sorted copy of input objects" };
106
108 StringProperty m_sortVar{ this, "SortVariable", "pt", "Define by what parameter to sort (default: 'pt'; allowed: 'pt', 'eta', 'phi', 'm', 'e', 'rapidity')" };
109
111 BooleanProperty m_sortDescending{ this, "SortDescending", true, "Define if the container should be sorted in a descending order (default=true)" };
112
114 int m_sortID{0};
115
117 mutable std::atomic<unsigned long> m_nEventsProcessed{0};
118
119};
120
121
122inline bool ParticleSortingTool::compareDouble( double a, double b ) const
123{
124 if ( m_sortID < 0 ) { return CxxUtils::fpcompare::greater(a,b); }
125 else { return CxxUtils::fpcompare::less(a,b); }
126}
127
128
129template<class CONTAINERTYPE>
131{
132 if ( !cont ) {
133 ATH_MSG_ERROR("No ConstDataVector to be sorted");
134 return StatusCode::FAILURE;
135 }
136 // Actually do the sorting, using a C++11 lambda function construct
137 // to be able to use the member function here
138 if ( std::abs(m_sortID) == 1 ) {
139 cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
140 return this->comparePt(a,b);
141 } );
142 }
143 else if ( std::abs(m_sortID) == 2 ) {
144 cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
145 return this->compareEta(a,b);
146 } );
147 }
148 else if ( std::abs(m_sortID) == 3 ) {
149 cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
150 return this->comparePhi(a,b);
151 } );
152 }
153 else if ( std::abs(m_sortID) == 4 ) {
154 cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
155 return this->compareMass(a,b);
156 } );
157 }
158 else if ( std::abs(m_sortID) == 5 ) {
159 cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
160 return this->compareEnergy(a,b);
161 } );
162 }
163 else if ( std::abs(m_sortID) == 6 ) {
164 cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
165 return this->compareRapidity(a,b);
166 } );
167 }
168 else if ( std::abs(m_sortID) == 7 ) {
169 cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
170 return this->compareAuxData(a,b);
171 } );
172 }
173
174 return StatusCode::SUCCESS;
175}
176
177
178
179#endif //> !EVENTUTILS_PARTICLESORTINGTOOL_H
#define ATH_MSG_ERROR(x)
DataVector adapter that acts like it holds const pointers.
static Double_t a
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
DataVector adapter that acts like it holds const pointers.
StatusCode doSortConst(ConstDataVector< CONTAINERTYPE > *cont) const
Helper method to sort a ConstDataVector.
StatusCode doSort(xAOD::IParticleContainer *cont) const
Helper method that implements the call to the right sort function.
StringProperty m_inCollKey
Input container name.
bool compareEnergy(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's energy.
virtual ~ParticleSortingTool()
Destructor:
bool compareAuxData(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare an auxdata member of the particle.
BooleanProperty m_sortDescending
Define if the container should be sorted in a descending order (default=true)
int m_sortID
Internal identifier for the type of sorting.
std::atomic< unsigned long > m_nEventsProcessed
Internal event counter.
StringProperty m_sortVar
Define by what parameter to sort (default: 'pt')
bool comparePt(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's pt.
ParticleSortingTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
virtual StatusCode addBranches(const EventContext &ctx) const final override
Implement the method from the ISkimmingTool interface.
bool compareRapidity(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's rapidity.
bool compareEta(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's eta.
StringProperty m_outCollKey
The name of the output container (with SG::VIEW_ELEMENTS) with the sorted copy of input objects.
virtual StatusCode initialize() override
Athena algtool's initialize.
bool compareDouble(double a, double b) const
Method to compare two doubles.
bool compareMass(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's mass.
virtual StatusCode finalize() override
Athena algtool's finalize.
bool comparePhi(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's phi.
Class providing the definition of the 4-vector interface.
Workaround x86 precision issues for FP inequality comparisons.
bool greater(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:140
bool less(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:166
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.