ATLAS Offline Software
TrackParticleCompressorTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 // Local include(s):
7 #include <memory>
8 
9 
10 
12 
13 namespace xAODMaker {
14 
16  TrackParticleCompressorTool( const std::string& type,
17  const std::string& name,
18  const IInterface* parent )
19  : AthAlgTool( type, name, parent ) {
20 
21  // Declare the interface implemented by the tool
22  declareInterface< xAODMaker::ITrackParticleCompressorTool >( this );
23 
24  // Declare the properties of the tool:
25  declareProperty( "OffDiagCovMatrixBits", m_offDiagCovMatrixBits = 23,
26  "Bits kept for the off-diagonal covariance matrix "
27  "elements" );
28  declareProperty( "DiagonalCovMatrixBits", m_diagCovMatrixBits = 23,
29  "Bits kept for the diagonal covariance matrix "
30  "elements" );
31  declareProperty( "UseOffDiagCompr", m_useOffDiagCompr = false,
32  "Flag to control the off-diagonal compression "
33  "scheme" );
34  }
35 
37 
38  // Greet the user:
39  ATH_MSG_INFO( "Initialising");
40  ATH_MSG_INFO( " OffDiagCovMatrixBits = " << m_offDiagCovMatrixBits );
41  ATH_MSG_INFO( " DiagCovMatrixBits = " << m_diagCovMatrixBits );
42  ATH_MSG_INFO( " UseOffDiagCompr = " << m_useOffDiagCompr );
43 
44 
45  // Create the helper objects:
46  m_diagCovMatrixCompressor = std::make_unique<CxxUtils::FloatCompressor>(
48 
49  // Return gracefully:
50  return StatusCode::SUCCESS;
51  }
52 
55 
56  xAOD::ParametersCovMatrix_t covMatrix = tp.definingParametersCovMatrix();
57  tp.setDefiningParametersCovMatrix(covMatrix); //Separate diag and offdiag cov matrix vectors may not have been set before
58  if(m_useOffDiagCompr) tp.compressDefiningParametersCovMatrixOffDiag();
59 
60  //Check if determinant got negative after compression
61  //If so, undo
62  xAOD::ParametersCovMatrix_t covMatrixCompr = tp.definingParametersCovMatrix();
63  if(covMatrixCompr.determinant()<=0.) tp.setDefiningParametersCovMatrix(covMatrix);
64 
65  const std::vector< float > diagVec = tp.definingParametersCovMatrixDiagVec();
66  std::vector< float > diagVecCompr;
67 
68  diagVecCompr.reserve(diagVec.size());
69  for (float i : diagVec) {
70  diagVecCompr.push_back(
71  m_diagCovMatrixCompressor->reduceFloatPrecision(i));
72  }
73 
74  tp.setDefiningParametersCovMatrixDiagVec( diagVecCompr );
75 
76  auto compressOffDiag = []( const std::vector< float >& offDiagVec, int bits ) -> std::vector< float > {
77  CxxUtils::FloatCompressor fc( bits );
78  std::vector< float > offDiagVecCompr;
79  offDiagVecCompr.reserve( offDiagVec.size() );
80  for( float element : offDiagVec ) {
81  offDiagVecCompr.push_back( fc.reduceFloatPrecision( element ) );
82  }
83  return offDiagVecCompr;
84  };
85 
86  const std::vector< float > offDiagVec = tp.definingParametersCovMatrixOffDiagVec();
87  tp.setDefiningParametersCovMatrixOffDiagVec( compressOffDiag( offDiagVec, m_offDiagCovMatrixBits ) );
88  int offDiagComprBits = m_offDiagCovMatrixBits;
89 
90  // Need to check determinant of the matrix + its inverse to prevent any precision issue in inverse computation
91  while( ( tp.definingParametersCovMatrix().determinant() <= 0.0 || tp.definingParametersCovMatrix().inverse().determinant() <= 0.0 ) &&
92  ( ++offDiagComprBits <= m_diagCovMatrixBits ) ) {
93  tp.setDefiningParametersCovMatrixOffDiagVec( compressOffDiag( offDiagVec, offDiagComprBits ) );
94  }
95 
96  // Return gracefully:
97  return StatusCode::SUCCESS;
98  }
99 
100 } // namespace xAODMaker
TrackParticleCompressorTool.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
xAODMaker::TrackParticleCompressorTool::m_diagCovMatrixCompressor
std::unique_ptr< CxxUtils::FloatCompressor > m_diagCovMatrixCompressor
Helper object for compressing the on-diagonal covariance matrix elements.
Definition: TrackParticleCompressorTool.h:63
ParticleTest.tp
tp
Definition: ParticleTest.py:25
xAODMaker
Definition: StoreGateSvc.h:72
CxxUtils::FloatCompressor
Class implementing a lossy float compression.
Definition: FloatCompressor.h:30
xAODMaker::TrackParticleCompressorTool::m_useOffDiagCompr
bool m_useOffDiagCompr
Definition: TrackParticleCompressorTool.h:59
lumiFormat.i
int i
Definition: lumiFormat.py:92
CxxUtils::FloatCompressor::reduceFloatPrecision
float reduceFloatPrecision(float value) const
Function returning a reduced precision float value.
Definition: FloatCompressor.cxx:98
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAOD::covMatrix
covMatrix
Definition: TrackMeasurement_v1.cxx:19
xAODMaker::TrackParticleCompressorTool::m_diagCovMatrixBits
int m_diagCovMatrixBits
The number of mantissa bits to keep for the diagonal covariance matrix elements.
Definition: TrackParticleCompressorTool.h:57
test_pyathena.parent
parent
Definition: test_pyathena.py:15
xAODMaker::TrackParticleCompressorTool::TrackParticleCompressorTool
TrackParticleCompressorTool(const std::string &type, const std::string &name, const IInterface *parent)
Regular AlgTool constructor.
Definition: TrackParticleCompressorTool.cxx:16
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
AthAlgTool
Definition: AthAlgTool.h:26
xAODMaker::TrackParticleCompressorTool::compress
StatusCode compress(xAOD::TrackParticle &tp) const override
The function doing the heavy lifting.
Definition: TrackParticleCompressorTool.cxx:54
xAODMaker::TrackParticleCompressorTool::initialize
StatusCode initialize() override
Function initialising the tool.
Definition: TrackParticleCompressorTool.cxx:36
xAODMaker::TrackParticleCompressorTool::m_offDiagCovMatrixBits
int m_offDiagCovMatrixBits
The number of mantissa bits to keep for off-diagonal covariance matrix elements.
Definition: TrackParticleCompressorTool.h:54