ATLAS Offline Software
Loading...
Searching...
No Matches
Normalizations.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5// $Id: Normalizations.icc 667905 2015-05-18 19:07:55Z wsfreund $
6#ifndef RINGERSELECTORTOOLS_NORMALIZATIONS_ICC
7#define RINGERSELECTORTOOLS_NORMALIZATIONS_ICC
8
9#include "Normalizations.h"
10#include <cmath>
11#include <limits>
12#include "AsgMessaging/MsgStream.h"
13
14/**
15 * @brief Namespace dedicated for Ringer utilities
16 **/
17namespace Ringer
18{
19
20/**
21* @brief Namespace dedicated for Ringer pre-processing utilities
22**/
23namespace PreProcessing
24{
25
26/**
27 * @brief Namespace dedicated for Ringer normalization utilities
28 **/
29namespace Norm {
30
31/**
32 * @brief Helper functions:
33 **/
34namespace {
35
36// =============================================================================
37inline
38float getMax( std::vector<float> &inputSpace ) {
39 float max = std::numeric_limits<float>::min();
40 for ( std::vector<float>::const_iterator itr = inputSpace.begin();
41 itr != inputSpace.end();
42 ++itr){
43 if (std::abs(*itr) > max) {
44 max = *itr;
45 }
46 }
47 return std::abs(max);
48}
49
50// =============================================================================
51inline
52float getNorm( std::vector<float> &inputSpace ){
53 float norm = 0;
54 for ( std::vector<float>::const_iterator itr = inputSpace.begin();
55 itr != inputSpace.end();
56 ++itr){
57 norm += *itr;
58 }
59 return std::abs(norm);
60}
61
62// =============================================================================
63inline
64float getNorm( std::vector<float> &inputSpace, const unsigned int p ){
65 if ( p == 1) return getNorm(inputSpace);
66 float norm = 0;
67 for ( std::vector<float>::const_iterator itr = inputSpace.begin();
68 itr != inputSpace.end();
69 ++itr){
70 norm += std::pow(*itr,p);
71 }
72 return std::abs(std::pow(norm,1/p));
73}
74
75// =============================================================================
76inline
77void applyNorm( std::vector<float> &inputSpace, const float norm ){
78 if (!norm) return;
79 float invNorm = 1/norm;
80 for ( std::vector<float>::iterator itr = inputSpace.begin();
81 itr != inputSpace.end();
82 ++itr){
83 *itr *= invNorm;
84 }
85}
86
87// =============================================================================
88inline
89void applyInvNorm( std::vector<float> &inputSpace, const float invNorm ){
90 for ( std::vector<float>::iterator itr = inputSpace.begin();
91 itr != inputSpace.end();
92 ++itr){
93 *itr *= invNorm;
94 }
95}
96
97// =============================================================================
98inline
99void applyInvNorm( std::vector<float> &inputSpace,
100 const std::vector<float> &invNorm )
101{
102 std::vector<float>::const_iterator itr2 = invNorm.begin();
103 for ( std::vector<float>::iterator itr = inputSpace.begin();
104 itr != inputSpace.end();
105 ++itr, ++itr2)
106 {
107 *itr *= *itr2;
108 }
109}
110
111// =============================================================================
112inline
113void applyDeslocation( std::vector<float> &inputSpace, const float deslocation )
114{
115 for ( std::vector<float>::iterator itr = inputSpace.begin();
116 itr != inputSpace.end();
117 ++itr)
118 {
119 *itr += deslocation;
120 }
121}
122
123// =============================================================================
124inline
125void applyDeslocation( std::vector<float> &inputSpace,
126 const std::vector<float>& deslocation )
127{
128 std::vector<float>::const_iterator itr2 = deslocation.begin();
129 for ( std::vector<float>::iterator itr = inputSpace.begin();
130 itr != inputSpace.end();
131 ++itr, ++itr2)
132 {
133 *itr += *itr2;
134 }
135}
136
137} // Private namespace
138
139/// Normalization interfaces:
140/// @{
141// =============================================================================
142inline
143void Norm1::execute(std::vector<float> &inputSpace) const {
144#ifndef NDEBUG
145 ATH_MSG_DEBUG("Applying Norm1. Input space is: " << inputSpace);
146#endif
147 float norm1 = getNorm(inputSpace,1);
148#ifndef NDEBUG
149 ATH_MSG_DEBUG("Normalization factor is " << norm1 );
150#endif
151 applyNorm(inputSpace,norm1);
152#ifndef NDEBUG
153 ATH_MSG_DEBUG("Pattern space is: " << inputSpace);
154#endif
155}
156
157// =============================================================================
158inline
159void Norm2::execute(std::vector<float> &inputSpace) const {
160#ifndef NDEBUG
161 ATH_MSG_DEBUG("Applying Norm2. Input space is: " << inputSpace);
162#endif
163 float norm2 = getNorm(inputSpace,2);
164#ifndef NDEBUG
165 ATH_MSG_DEBUG("Normalization factor is " << norm2 );
166#endif
167 applyNorm(inputSpace,norm2);
168#ifndef NDEBUG
169 ATH_MSG_DEBUG("Pattern space is: " << inputSpace);
170#endif
171}
172
173// =============================================================================
174inline
175void Sqrt::execute(std::vector<float> &inputSpace) const {
176#ifndef NDEBUG
177 ATH_MSG_DEBUG("Applying Sqrt. Input space is: " << inputSpace);
178#endif
179 float sqrtNorm = std::sqrt(std::fabs(getNorm(inputSpace,1)));
180#ifndef NDEBUG
181 ATH_MSG_DEBUG("Normalization factor is " << sqrtNorm );
182#endif
183 applyNorm(inputSpace,sqrtNorm);
184#ifndef NDEBUG
185 ATH_MSG_DEBUG("Pattern space is: " << inputSpace);
186#endif
187}
188
189// =============================================================================
190inline
191void ConstantValue::execute(std::vector<float> &inputSpace) const {
192#ifndef NDEBUG
193 ATH_MSG_DEBUG("Applying ConstantValue(Value: " << 1/m_constantInv
194 << "). Input space is: " << inputSpace);
195#endif
196 applyInvNorm(inputSpace,m_constantInv);
197#ifndef NDEBUG
198 ATH_MSG_DEBUG("Pattern space is: " << inputSpace);
199#endif
200}
201
202// =============================================================================
203inline
204void Spherization::execute(std::vector<float> &inputSpace) const {
205#ifndef NDEBUG
206 ATH_MSG_DEBUG("Applying Spherization. Input space is: " << inputSpace);
207#endif
208 if ( inputSpace.size() != m_dim ){
209 throw std::runtime_error(std::string( "Input size (") +
210 std::to_string(inputSpace.size()) + ") does not match "
211 " this pre-processing inputSpace dimension size(" +
212 std::to_string(m_dim) + ".");
213 }
214 applyDeslocation(inputSpace,m_deslocation);
215 applyInvNorm(inputSpace,m_normInv);
216#ifndef NDEBUG
217 ATH_MSG_DEBUG("Pattern space is: " << inputSpace);
218#endif
219}
220
221// =============================================================================
222inline
223void MinMax::execute(std::vector<float> &inputSpace) const {
224#ifndef NDEBUG
225 ATH_MSG_DEBUG("Applying MinMax. Input space is: " << inputSpace);
226#endif
227 if ( inputSpace.size() != m_dim ){
228 throw std::runtime_error(std::string( "Input size (") +
229 std::to_string(inputSpace.size()) + ") does not match "
230 " this pre-processing inputSpace dimension size(" +
231 std::to_string(m_dim) + ".");
232 }
233 applyDeslocation(inputSpace,m_deslocation);
234 applyInvNorm(inputSpace,m_normInv);
235#ifndef NDEBUG
236 ATH_MSG_DEBUG("Pattern space is: " << inputSpace);
237#endif
238}
239/// @}
240
241} // namespace Norm
242} // namespace Discrimination
243} // namespace Ringer
244
245#endif // RINGERSELECTORTOOLS_NORMALIZATIONS_ICC
246// vim: filetype=cpp :