GCC Code Coverage Report


Directory: ./
File: src/detID/cellID.cc
Date: 2025-09-01 06:19:01
Exec Total Coverage
Lines: 83 123 67.5%
Functions: 12 13 92.3%
Branches: 44 87 50.6%

Line Branch Exec Source
1 #include "na64detID/cellID.hh"
2 #include "na64util/str-fmt.hh"
3
4 namespace na64dp {
5
6 125 CellID::CellID( int x, int y, int z ) : id(0x0) {
7
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 assert( x <= idxMax );
8
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 assert( y <= idxMax );
9
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 assert( z <= idxMax );
10 125 id |= ((x+1) & 0x1f)
11 125 | (((y+1) & 0x1f) << 5)
12 125 | (((z+1) & 0x1f) << 10)
13 ;
14 125 }
15
16 void
17 28955 CellID::set_x(unsigned int x){
18
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28955 times.
28955 assert( x <= idxMax );
19 28955 id &= ~0x1f;
20 28955 id |= (x+1) & 0x1f;
21 28955 }
22
23 unsigned int
24 28955 CellID::get_x() const {
25 28955 uint16_t code = 0x1f & (id);
26
1/2
✓ Branch 0 taken 28955 times.
✗ Branch 1 not taken.
28955 return code != 0 ? code - 1 : 0x0;
27 }
28
29 bool
30 125 CellID::is_x_set() const {
31 125 uint16_t code = 0x1f & (id);
32 125 return code;
33 }
34
35 void
36 28955 CellID::set_y(unsigned int y) {
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28955 times.
28955 assert( y <= idxMax );
38 28955 id &= ~(0x1f << 5);
39 28955 id |= ((y+1) << 5);
40 28955 }
41
42 unsigned int
43 28955 CellID::get_y() const {
44 28955 uint16_t code = 0x1f & (id >> 5);
45
1/2
✓ Branch 0 taken 28955 times.
✗ Branch 1 not taken.
28955 return code != 0 ? code - 1 : 0x0;
46 }
47
48 bool
49 125 CellID::is_y_set() const {
50 125 uint16_t code = 0x1f & (id >> 5);
51 125 return code;
52 }
53
54 void
55 28955 CellID::set_z(unsigned int z){
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28955 times.
28955 assert(z <= idxMax);
57 28955 id &= ~(0x1f << 10);
58 28955 id |= ((z+1) << 10);
59 28955 }
60
61 unsigned int
62 28955 CellID::get_z() const {
63 28955 uint16_t code = 0x1f & (id >> 10);
64
1/2
✓ Branch 0 taken 28955 times.
✗ Branch 1 not taken.
28955 return code != 0 ? code - 1 : 0x0;
65 }
66
67 bool
68 125 CellID::is_z_set() const {
69 125 uint16_t code = 0x1f & (id >> 10);
70 125 return code;
71 }
72
73 void
74 CellID::append_completion_context( DetID did
75 , std::map<std::string, std::string> & m ) {
76 if( !did.is_payload_set() ) return;
77 char bf[64];
78 CellID cid(did.payload());
79 if( ! cid.is_set() ) return;
80 if( cid.is_x_set() ) {
81 snprintf(bf, sizeof(bf), "%d", cid.get_x());
82 m["xIdx"] = bf;
83 } else {
84 m["xIdx"] = "-";
85 }
86 if( cid.is_y_set() ) {
87 snprintf(bf, sizeof(bf), "%d", cid.get_y());
88 m["yIdx"] = bf;
89 } else {
90 m["yIdx"] = "-";
91 }
92 if( cid.is_z_set() ) {
93 snprintf(bf, sizeof(bf), "%d", cid.get_z());
94 m["zIdx"] = bf;
95 } else {
96 m["zIdx"] = "-";
97 }
98 // Unique features for Hodoscopes
99 if( cid.get_z() == CellID::idxMax ) {
100 m["proj"] = "Y";
101 } else if( cid.get_z() == CellID::idxMax-1 ) {
102 m["proj"] = "X";
103 }
104 }
105
106 void
107 125 CellID::to_string( DetIDPayload_t pl, char * bf, size_t available ) {
108
2/4
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 125 times.
✗ Branch 3 not taken.
125 assert( bf && available );
109 125 CellID cid(pl);
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 125 times.
125 if( !cid.is_set() ) {
111 bf[0] = '\0';
112 return;
113 }
114
115 125 char xBf[3], yBf[3], zBf[3], noSetBf[2] = "-";
116 char *xStr, *yStr, *zStr;
117
2/3
✓ Branch 1 taken 125 times.
✓ Branch 3 taken 125 times.
✗ Branch 4 not taken.
125 if( cid.is_x_set() ) {
118
1/1
✓ Branch 1 taken 125 times.
125 snprintf(xBf, sizeof(xBf), "%u", cid.get_x());
119 125 xStr = xBf;
120 } else {
121 xStr = noSetBf;
122 }
123
2/3
✓ Branch 1 taken 125 times.
✓ Branch 3 taken 125 times.
✗ Branch 4 not taken.
125 if( cid.is_y_set() ) {
124
1/1
✓ Branch 1 taken 125 times.
125 snprintf(yBf, sizeof(yBf), "%u", cid.get_y());
125 125 yStr = yBf;
126 } else {
127 yStr = noSetBf;
128 }
129
2/3
✓ Branch 1 taken 125 times.
✓ Branch 3 taken 125 times.
✗ Branch 4 not taken.
125 if( cid.is_z_set() ) {
130
1/1
✓ Branch 1 taken 125 times.
125 snprintf(zBf, sizeof(zBf), "%u", cid.get_z());
131 125 zStr = zBf;
132 } else {
133 zStr = noSetBf;
134 }
135
136 125 int used = snprintf( bf, available
137 , "%s-%s-%s"
138 , xStr
139 , yStr
140 , zStr );
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if( (size_t) used >= available ) {
142 NA64DP_RUNTIME_ERROR( "Buffer of length %zu is"
143 " insufficient for CellID string, result truncated."
144 , available );
145 }
146 }
147
148 DetIDPayload_t
149 129 CellID::from_string( const char * cidStr ) {
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 assert(cidStr);
151
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 128 times.
129 if('\0' == *cidStr) return 0x0; // no payload
152
153 long idx;
154 128 CellID cid;
155 char * rest;
156 #if 1 // Special case for hodoscopes (non-standard naming scheme) and TDC
157 {
158 128 bool isX = 'X' == *cidStr
159 128 , isY = 'Y' == *cidStr
160 ;
161
2/4
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 128 times.
128 if( isX || isY ) {
162 if( '\0' == cidStr[1] ) {
163 // This is a native TBName case -- use Z index within payload
164 // to distinguish X/Y planes
165 cid.set_z( isX ? CellID::idxMax-1 : CellID::idxMax );
166 assert(cid.get_z());
167 return cid.id;
168 }
169 #if 1
170 ++cidStr;
171 #else
172 idx = strtol( cidStr+2, &rest, 10 );
173 if( cidStr+2 == rest || '\0' != *rest ) {
174 NA64DP_RUNTIME_ERROR( "Bad suffix (assumed hodoscope's): \"%s\"."
175 , cidStr );
176 }
177 if( idx < 0 || idx > idxMax ) {
178 NA64DP_RUNTIME_ERROR( "Hodoscope %c-index %ld is out of boundaries: [0,%d]."
179 , *cidStr, idx, idxMax );
180 }
181 if( isX ) {
182 cid.set_z(CellID::idxMax-1);
183 } else {
184 cid.set_z(CellID::idxMax );
185 }
186 assert( cid.get_z() );
187 return cid.id;
188 #endif
189 }
190 }
191 #endif
192 // X
193
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 if( '-' != *cidStr ) {
194 128 idx = strtol(cidStr, &rest, 10);
195
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 125 times.
128 if( cidStr == rest ) {
196 3 NA64DP_RUNTIME_ERROR( "Unable to interpret X-index in SADC name suffix \"%s\"."
197 , cidStr );
198 }
199
2/4
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 125 times.
125 if( idx < 0 || idx > idxMax ) {
200 NA64DP_RUNTIME_ERROR( "Cell index X %ld out of boundaries: [0,%d]."
201 , idx, idxMax );
202 }
203
1/1
✓ Branch 1 taken 125 times.
125 cid.set_x(idx);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if( *rest == '\0' ) return cid.id;
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if( *rest != '-' ) {
206 NA64DP_RUNTIME_ERROR( "Unable to parse SADC cell/subdetector ID."
207 " Expected '-' after X-index, got '%c'.", *cidStr );
208 }
209 } else {
210 rest = const_cast<char*>(cidStr) + 2;
211 }
212 // Y
213
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if( '-' != rest[1] ) {
214 125 idx = strtol(rest + 1, &rest, 10);
215
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
✗ Branch 3 not taken.
125 if( idx < 0 && idx > idxMax ) {
216 NA64DP_RUNTIME_ERROR( "Cell index Y %d out of boundaries: [0,%d]."
217 , idx, idxMax );
218 }
219
1/1
✓ Branch 1 taken 125 times.
125 cid.set_y(idx);
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if( *rest == '\0' ) return cid.id;
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if( *rest != '-' ) {
222 NA64DP_RUNTIME_ERROR( "Unable to parse SADC cell/subdetector ID."
223 " Expected '-' after Y-index, got '%c'.", *cidStr );
224 }
225 } else {
226 rest = rest + 2;
227 }
228 // Z
229
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if( '-' != rest[1] ) {
230 125 idx = strtol(rest + 1, &rest, 10);
231
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
✗ Branch 3 not taken.
125 if( idx < 0 && idx > idxMax ) {
232 NA64DP_RUNTIME_ERROR( "Cell index Y %d out of boundaries: [0,%d]."
233 , idx, idxMax );
234 }
235
1/1
✓ Branch 1 taken 125 times.
125 cid.set_z(idx);
236 } else {
237 rest = rest + 2;
238 }
239
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if( *rest == '\0' ) return cid.id;
240 NA64DP_RUNTIME_ERROR( "Cell index parsing error: remainder \"%s\"."
241 , rest );
242 }
243
244 }
245
246