ATLAS Offline Software
ParticleSortingTool.h
Go to the documentation of this file.
1 
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:
37 public:
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 final override;
58 
59 
60 
61 // Private methods
62 private:
63 
66 
68  template<class CONTAINERTYPE>
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:
99 private:
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 
122 inline 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 
129 template<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
ParticleSortingTool::addBranches
virtual StatusCode addBranches() const final override
Implement the method from the ISkimmingTool interface.
Definition: ParticleSortingTool.cxx:122
ParticleSortingTool::~ParticleSortingTool
virtual ~ParticleSortingTool()
Destructor:
Definition: ParticleSortingTool.cxx:45
ParticleSortingTool::compareMass
bool compareMass(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's mass.
Definition: ParticleSortingTool.cxx:267
ParticleSortingTool::compareAuxData
bool compareAuxData(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare an auxdata member of the particle.
Definition: ParticleSortingTool.cxx:293
IParticle.h
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
ParticleSortingTool::m_outCollKey
StringProperty m_outCollKey
The name of the output container (with SG::VIEW_ELEMENTS) with the sorted copy of input objects.
Definition: ParticleSortingTool.h:105
IAugmentationTool.h
ParticleSortingTool::m_sortID
int m_sortID
Internal identifier for the type of sorting.
Definition: ParticleSortingTool.h:114
ParticleSortingTool::m_sortVar
StringProperty m_sortVar
Define by what parameter to sort (default: 'pt')
Definition: ParticleSortingTool.h:108
ParticleSortingTool::doSortConst
StatusCode doSortConst(ConstDataVector< CONTAINERTYPE > *cont) const
Helper method to sort a ConstDataVector.
Definition: ParticleSortingTool.h:130
ParticleSortingTool::comparePt
bool comparePt(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's pt.
Definition: ParticleSortingTool.cxx:240
ParticleSortingTool::m_nEventsProcessed
std::atomic< unsigned long > m_nEventsProcessed
Internal event counter.
Definition: ParticleSortingTool.h:117
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
ParticleSortingTool::compareRapidity
bool compareRapidity(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's rapidity.
Definition: ParticleSortingTool.cxx:285
ParticleSortingTool::initialize
virtual StatusCode initialize() override
Athena algtool's initialize.
Definition: ParticleSortingTool.cxx:52
CxxUtils::fpcompare::greater
bool greater(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:140
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
ParticleSortingTool::comparePhi
bool comparePhi(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's phi.
Definition: ParticleSortingTool.cxx:258
DerivationFramework::IAugmentationTool
Definition: IAugmentationTool.h:22
ParticleSortingTool::compareEta
bool compareEta(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's eta.
Definition: ParticleSortingTool.cxx:249
ParticleSortingTool::ParticleSortingTool
ParticleSortingTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Definition: ParticleSortingTool.cxx:34
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
IParticleContainer.h
fpcompare.h
Workaround x86 precision issues for FP inequality comparisons.
AthAlgTool.h
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ParticleSortingTool::compareEnergy
bool compareEnergy(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's energy.
Definition: ParticleSortingTool.cxx:276
ParticleSortingTool::m_sortDescending
BooleanProperty m_sortDescending
Define if the container should be sorted in a descending order (default=true)
Definition: ParticleSortingTool.h:111
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
ParticleSortingTool
Definition: ParticleSortingTool.h:32
ParticleSortingTool::m_inCollKey
StringProperty m_inCollKey
Input container name.
Definition: ParticleSortingTool.h:102
CxxUtils::fpcompare::less
bool less(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:166
ParticleSortingTool::compareDouble
bool compareDouble(double a, double b) const
Method to compare two doubles.
Definition: ParticleSortingTool.h:122
a
TList * a
Definition: liststreamerinfos.cxx:10
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
ParticleSortingTool::doSort
StatusCode doSort(xAOD::IParticleContainer *cont) const
Helper method that implements the call to the right sort function.
Definition: ParticleSortingTool.cxx:192
ParticleSortingTool::finalize
virtual StatusCode finalize() override
Athena algtool's finalize.
Definition: ParticleSortingTool.cxx:84
AthAlgTool
Definition: AthAlgTool.h:26