Belle II Software light-2406-ragdoll
HitPatternCDC.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <mdst/dataobjects/HitPatternCDC.h>
10
11#include<framework/logging/Logger.h>
12
13#include <string>
14
15using namespace Belle2;
16
17std::bitset<64> sLayerZer(static_cast<std::string>("0000000000000000000000000000000000000000000000000000000011111111"));
18std::bitset<64> sLayerOne(static_cast<std::string>("0000000000000000000000000000000000000000000000000011111100000000"));
19std::bitset<64> sLayerTwo(static_cast<std::string>("0000000000000000000000000000000000000000000011111100000000000000"));
20std::bitset<64> sLayerThr(static_cast<std::string>("0000000000000000000000000000000000000011111100000000000000000000"));
21std::bitset<64> sLayerFou(static_cast<std::string>("0000000000000000000000000000000011111100000000000000000000000000"));
22std::bitset<64> sLayerFiv(static_cast<std::string>("0000000000000000000000000011111100000000000000000000000000000000"));
23std::bitset<64> sLayerSix(static_cast<std::string>("0000000000000000000011111100000000000000000000000000000000000000"));
24std::bitset<64> sLayerSev(static_cast<std::string>("0000000000000011111100000000000000000000000000000000000000000000"));
25std::bitset<64> sLayerEig(static_cast<std::string>("0000000011111100000000000000000000000000000000000000000000000000"));
26
27const std::bitset<64> HitPatternCDC::s_sLayerMasks[9] = {sLayerZer, sLayerOne, sLayerTwo, sLayerThr, sLayerFou,
28 sLayerFiv, sLayerSix, sLayerSev, sLayerEig
29 };
30
31std::bitset<64> infoLayerCDC(static_cast<std::string>("1111111100000000000000000000000000000000000000000000000000000000"));
32
33const std::bitset<64> HitPatternCDC::s_infoLayerMask = infoLayerCDC;
34
35const std::map<unsigned short, std::pair<unsigned short, unsigned short>> HitPatternCDC::s_superLayerIndices = {
36 {0, std::make_pair(0, 7)},
37 {1, std::make_pair(8, 13)},
38 {2, std::make_pair(14, 19)},
39 {3, std::make_pair(20, 25)},
40 {4, std::make_pair(26, 31)},
41 {5, std::make_pair(32, 37)},
42 {6, std::make_pair(38, 43)},
43 {7, std::make_pair(44, 49)},
44 {8, std::make_pair(50, 55)}
45};
46
47unsigned short HitPatternCDC::getNHits() const
48{
49 // Shift the 8 MSBs to the right and return their value as integer.
50 return static_cast<unsigned short int>((m_pattern >> 56).to_ulong());
51}
52
53void HitPatternCDC::setNHits(unsigned short nHits)
54{
55 if (nHits > 256) {
56 // Maximum with 8 available bits
57 nHits = 255;
58 }
59 // Reset the 8 MSBs to zero.
60 m_pattern = m_pattern & ~s_infoLayerMask;
61 // Set the total number of hits as the 8 MSBs
62 std::bitset<64> numberOfHits(nHits);
63 numberOfHits <<= 56;
64 // Set the 8 MSBs to the total number of hits.
65 // The 8 MSBs have to be zero, otherwise this breaks.
66 m_pattern = numberOfHits | m_pattern;
67}
68
70{
71 if constexpr(sizeof(unsigned long) >= 8) {
72 return m_pattern.to_ulong();
73 } else {
74 return m_pattern.to_ullong();
75 }
76}
77
78void HitPatternCDC::setLayer(const unsigned short layer)
79{
80 B2ASSERT("Layer is out of range.", layer <= 55);
81 m_pattern.set(layer);
82}
83
84void HitPatternCDC::resetLayer(const unsigned short layer)
85{
86 B2ASSERT("Layer is out of range.", layer <= 55);
87 m_pattern.reset(layer);
88}
89
90bool HitPatternCDC::hasLayer(const unsigned short layer) const
91{
92 B2ASSERT("Layer is out of range.", layer <= 55);
93 return m_pattern[layer];
94}
95
97{
98 for (unsigned int i = 0; i < m_pattern.size(); ++i) {
99 if ((m_pattern & ~s_infoLayerMask).test(i)) return i;
100 }
101 return -1;
102}
103
105{
106 // m_pattern.size()-8 because the first 8 bits are not pattern
107 for (unsigned int i = m_pattern.size() - 8; i > 0; --i) {
108 // -1 because of the index couting...
109 if ((m_pattern & ~s_infoLayerMask).test(i - 1)) return i - 1;
110 }
111 return -1;
112}
113
114bool HitPatternCDC::hasSLayer(const unsigned short sLayer) const
115{
116 B2ASSERT("Super layer outof range.", sLayer <= 8);
117 return ((m_pattern & s_sLayerMasks[sLayer]).any());
118}
119
120void HitPatternCDC::resetSLayer(const unsigned short sLayer)
121{
122 B2ASSERT("Super layer outof range.", sLayer <= 8);
123 for (unsigned short int ii = 0; ii < m_pattern.size(); ++ii) {
124 if ((s_sLayerMasks[sLayer])[ii]) {resetLayer(ii);}
125 }
126}
127
128std::bitset<64> HitPatternCDC::getSLayerPattern(const unsigned short sLayer)
129{
130 return m_pattern & s_sLayerMasks[sLayer];
131}
132
133unsigned short HitPatternCDC::getSLayerNHits(const unsigned short sLayer) const
134{
135 B2ASSERT("Super layer outof range.", sLayer <= 8);
136 return static_cast<unsigned short>((m_pattern & s_sLayerMasks[sLayer]).count());
137}
138
139unsigned short HitPatternCDC::getLongestContRunInSL(const unsigned short sLayer) const
140{
141 B2ASSERT("Super layer outof range.", sLayer <= 8);
142 unsigned short max = 0;
143 unsigned short counter = 0;
144 std::pair<unsigned short, unsigned short> indices = s_superLayerIndices.at(sLayer);
145 for (unsigned short i = indices.first; i <= indices.second; ++i) {
146 counter += m_pattern[i];
147 if (m_pattern[i] == 0) {
148 if (counter > max) {
149 max = counter;
150 }
151 counter = 0;
152 }
153 }
154 return std::max(max, counter);
155}
156
158{
160 & m_pattern).any();
161}
162
164{
165 return ((s_sLayerMasks[1] | s_sLayerMasks[3] | s_sLayerMasks[5] | s_sLayerMasks[7])
166 & m_pattern).any();
167}
168
169std::string HitPatternCDC::__str__() const
170{
171 return m_pattern.to_string();
172}
std::string __str__() const
String for printing in python.
unsigned short getSLayerNHits(const unsigned short sLayer) const
Getter for the number of hits in one super-layer.
short getLastLayer() const
Returns the index of the last layer with a hit.
std::bitset< 64 > getSLayerPattern(const unsigned short sLayer)
Get the bit pattern in a specific super layer.
std::bitset< 64 > m_pattern
Saves the actual pattern.
static const std::map< unsigned short, std::pair< unsigned short, unsigned short > > s_superLayerIndices
Holds the indices for super layer access.
bool hasSLayer(const unsigned short sLayer) const
Getter for super-layer match.
bool hasAxialLayer() const
True, if at least one axial layer is true.
void setNHits(unsigned short nHits)
Sets the 8 MSBs to the total number of hits in the CDC.
void setLayer(const unsigned short layer)
Set bit corresponding to layer to true.
ULong64_t getInteger() const
Getter for underlying integer type.
bool hasLayer(const unsigned short layer) const
Getter for single layer.
unsigned short getNHits() const
Get the total Number of CDC hits in the fit.
static const std::bitset< 64 > s_sLayerMasks[9]
Masks to zero out all bits from other layers.
short getFirstLayer() const
Returns the index of the first layer with a hit.
void resetLayer(const unsigned short layer)
Set bit corresponding to layer to false.
void resetSLayer(const unsigned short sLayer)
Reset complete superLayer, e.g.
unsigned short getLongestContRunInSL(const unsigned short sLayer) const
Getter for longest run of consecutive layers with hits within the given Super-layer.
bool hasStereoLayer() const
True, if at least one axial layer is true.
static const std::bitset< 64 > s_infoLayerMask
Mask to zero out all bits from other layers.
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24