ATLAS Offline Software
Loading...
Searching...
No Matches
CaloCellPackerUtils.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4/**
5 * @file CaloCellPackerUtils.icc
6 * @author scott snyder
7 * @date Nov 2007
8 * @brief Utilities for compacting calorimeter cells.
9 */
10
11
12namespace CaloCellPackerUtils {
13
14
15/**
16 * @brief Shift and mask a value into the bitfield.
17 * @param x The input value.
18 * @return The value shifted and masked to go into the bitfield.
19 */
20inline
21unsigned int Bitfield::in (unsigned int x) const
22{
23 return (x&m_mask)<<m_shift;
24}
25
26
27/**
28 * @brief Extract a value from the bitfield.
29 * @param x The input bitfield.
30 * @return The value extracted from the bitfield.
31 */
32inline
33unsigned int Bitfield::out (unsigned int x) const
34{
35 return (x>>m_shift)&m_mask;
36}
37
38
39/**
40 * @brief Shift and mask a value into the bitfield.
41 * @param x The input value.
42 * @return The converted value shifted and masked to go into the bitfield.
43 */
44inline
45unsigned int Floatfield::in (double x) const
46{
47 // Handle over/underflow.
48 if (x >= m_xmax) [[unlikely]]
49 return Bitfield::in (m_mask);
50 else if (x <= m_xmin) [[unlikely]]
51 return 0;
52
53 // Convert to an int, and pack.
54 return Bitfield::in (static_cast<unsigned int> ((x - m_xmin) * m_ifact));
55}
56
57
58/**
59 * @brief Extract a value from the bitfield.
60 * @param x The input bitfield.
61 * @param underflag[out] Set to 1 if the value was the lowest possible.
62 * @return The value extracted from the bitfield.
63 */
64inline
65double Floatfield::out (unsigned int x, int& underflow) const
66{
67 // Unpack the value.
68 unsigned int xx = Bitfield::out(x);
69
70 // Did we underflow?
71 if (!xx) [[unlikely]] {
72 underflow = 1;
73 return m_xmin;
74 }
75
76 // Convert back to a float.
77 // Note: the cast to int here is important for performance: converting
78 // an unsigned to a double takes much longer than converting an int.
79 underflow = 0;
80 return m_xmin + ((int)xx+0.5) * m_fact;
81}
82
83
84/**
85 * @brief Shift and mask a value into the bitfield.
86 * @param x The input value.
87 * @return The converted value shifted and masked to go into the bitfield.
88 */
89inline
90unsigned int Floatfield2::in (double x) const
91{
92 // Handle over/underflow.
93 if (x >= m_xmax) [[unlikely]]
94 return Bitfield::in (m_mask);
95 else if (x <= 0) [[unlikely]]
96 return 0;
97
98 // Convert to an int, and pack.
99 return Bitfield::in (static_cast<unsigned int> (x * m_ifact));
100}
101
102
103/**
104 * @brief Extract a value from the bitfield.
105 * @param x The input bitfield.
106 * @return The value extracted from the bitfield.
107 */
108inline
109double Floatfield2::out (unsigned int x) const
110{
111 // Unpack the value.
112 unsigned int xx = Bitfield::out(x);
113
114 // Did we underflow?
115 // (The branch hint here gives a small but measureable improvement.)
116 if (xx == 0) [[unlikely]] return 0;
117
118 // Convert back to a float.
119 // Note: the cast to int here is important for performance: converting
120 // an unsigned to a double takes much longer than converting an int.
121 return ((int)xx+0.5) * m_fact;
122}
123
124
125} // namespace CaloCellPackerUtils