ATLAS Offline Software
ParticleSortingTool.h
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2022 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;
103 
105  StringProperty m_outCollKey;
106 
108  StringProperty m_sortVar;
109 
111  BooleanProperty m_sortDescending;
112 
113 
115  int m_sortID;
116 
118  mutable std::atomic<unsigned long> m_nEventsProcessed;
119 
120 };
121 
122 
123 inline bool ParticleSortingTool::compareDouble( double a, double b ) const
124 {
125  if ( m_sortID < 0 ) { return CxxUtils::fpcompare::greater(a,b); }
126  else { return CxxUtils::fpcompare::less(a,b); }
127 }
128 
129 
130 template<class CONTAINERTYPE>
132 {
133  if ( !cont ) {
134  ATH_MSG_ERROR("No ConstDataVector to be sorted");
135  return StatusCode::FAILURE;
136  }
137  // Actually do the sorting, using a C++11 lambda function construct
138  // to be able to use the member function here
139  if ( std::abs(m_sortID) == 1 ) {
140  cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
141  return this->comparePt(a,b);
142  } );
143  }
144  else if ( std::abs(m_sortID) == 2 ) {
145  cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
146  return this->compareEta(a,b);
147  } );
148  }
149  else if ( std::abs(m_sortID) == 3 ) {
150  cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
151  return this->comparePhi(a,b);
152  } );
153  }
154  else if ( std::abs(m_sortID) == 4 ) {
155  cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
156  return this->compareMass(a,b);
157  } );
158  }
159  else if ( std::abs(m_sortID) == 5 ) {
160  cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
161  return this->compareEnergy(a,b);
162  } );
163  }
164  else if ( std::abs(m_sortID) == 6 ) {
165  cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
166  return this->compareRapidity(a,b);
167  } );
168  }
169  else if ( std::abs(m_sortID) == 7 ) {
170  cont->sort( [this](const xAOD::IParticle* a, const xAOD::IParticle* b) {
171  return this->compareAuxData(a,b);
172  } );
173  }
174 
175  return StatusCode::SUCCESS;
176 }
177 
178 
179 
180 #endif //> !EVENTUTILS_PARTICLESORTINGTOOL_H
ParticleSortingTool::addBranches
virtual StatusCode addBranches() const final override
Implement the method from the ISkimmingTool interface.
Definition: ParticleSortingTool.cxx:139
ParticleSortingTool::~ParticleSortingTool
virtual ~ParticleSortingTool()
Destructor:
Definition: ParticleSortingTool.cxx:62
ParticleSortingTool::compareMass
bool compareMass(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's mass.
Definition: ParticleSortingTool.cxx:284
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:310
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:115
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:131
ParticleSortingTool::comparePt
bool comparePt(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's pt.
Definition: ParticleSortingTool.cxx:257
ParticleSortingTool::m_nEventsProcessed
std::atomic< unsigned long > m_nEventsProcessed
Internal event counter.
Definition: ParticleSortingTool.h:118
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:302
ParticleSortingTool::initialize
virtual StatusCode initialize() override
Athena algtool's initialize.
Definition: ParticleSortingTool.cxx:69
CxxUtils::fpcompare::greater
bool greater(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:140
ParticleSortingTool::comparePhi
bool comparePhi(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's phi.
Definition: ParticleSortingTool.cxx:275
DerivationFramework::IAugmentationTool
Definition: IAugmentationTool.h:24
ParticleSortingTool::compareEta
bool compareEta(const xAOD::IParticle *partA, const xAOD::IParticle *partB) const
The method to compare the particle's eta.
Definition: ParticleSortingTool.cxx:266
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:293
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:228
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
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:123
a
TList * a
Definition: liststreamerinfos.cxx:10
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
ParticleSortingTool::doSort
StatusCode doSort(xAOD::IParticleContainer *cont) const
Helper method that implements the call to the right sort function.
Definition: ParticleSortingTool.cxx:209
ParticleSortingTool::finalize
virtual StatusCode finalize() override
Athena algtool's finalize.
Definition: ParticleSortingTool.cxx:101
AthAlgTool
Definition: AthAlgTool.h:26