File indexing completed on 2025-08-03 08:20:40
0001 #include "packet_iddigitizerv2.h"
0002
0003 #include <string.h>
0004
0005 using namespace std;
0006
0007 Packet_iddigitizerv2::Packet_iddigitizerv2(PACKET_ptr data)
0008 :Packet_w4 (data)
0009 {
0010
0011 _nsamples = 0;
0012
0013 _evtnr = 0;
0014 _clock_rollover = 0;
0015 _evtrollover = 0;
0016 _module_address = 0;
0017 _clock = 0;
0018 for ( int i = 0; i < 4; i++)
0019 {
0020 _fem_slot[i] = 0;
0021 _fem_evtnr[i] = 0;
0022 _fem_clock[i] = 0;
0023 }
0024
0025 _even_checksum = 0;
0026 _odd_checksum = 0;
0027
0028 _calculated_even_checksum = 0;
0029 _calculated_odd_checksum = 0;
0030
0031 _even_checksum_ok = -1;
0032 _odd_checksum_ok = -1;
0033
0034 _nchannels = 0;
0035 _is_decoded = 0;
0036
0037
0038 }
0039
0040
0041 Packet_iddigitizerv2::~Packet_iddigitizerv2()
0042 {
0043
0044 }
0045
0046
0047
0048 const int offset=9;
0049
0050
0051
0052
0053 int Packet_iddigitizerv2::decode ()
0054 {
0055
0056 if (_is_decoded ) return 0;
0057 _is_decoded = 1;
0058
0059
0060 int *k;
0061
0062
0063
0064
0065 int *SubeventData = (int *) findPacketDataStart(packet);
0066
0067 switch ( getHitFormat() )
0068 {
0069 case IDDIGITIZER_12S:
0070 _nsamples = 12;
0071 break;
0072
0073 case IDDIGITIZER_16S:
0074 _nsamples = 16;
0075 break;
0076
0077 default:
0078 _nsamples = 31;
0079 break;
0080 }
0081
0082
0083 _evtnr = SubeventData[0] & 0xffff;
0084 _clock_rollover = SubeventData[1] & 0xffff;
0085 _evtrollover = SubeventData[2] & 0xffff;
0086 _module_address = SubeventData[3] & 0xffff;
0087 _clock = SubeventData[4] & 0xffff;
0088
0089 _fem_slot[0] = SubeventData[6] & 0xffff;
0090 _fem_evtnr[0] = SubeventData[7] & 0xffff;
0091 _fem_clock[0] = SubeventData[8] & 0xffff;
0092
0093 _evtnr = (_evtrollover << 16) + _evtnr;
0094 _clock = (_clock_rollover << 16) + _clock;
0095
0096
0097 _nr_modules = 0;
0098
0099
0100 int index = getDataLength() -2;
0101 _even_checksum = SubeventData[index] & 0xffff;
0102
0103
0104
0105
0106
0107 index = getDataLength() -1;
0108 _odd_checksum = SubeventData[index] & 0xffff;
0109
0110
0111
0112
0113
0114
0115 k = &SubeventData[offset];
0116
0117 int dlength = getDataLength() - 9 -2;
0118
0119 for ( int index = 0; index < dlength ; index++)
0120 {
0121
0122 int rawtag = ((k[index] >> 16) & 0x3fff);
0123 int realtag = rawtag -9;
0124
0125
0126
0127
0128
0129 int module = 0;
0130 for ( module = 0; module < 4; module++)
0131 {
0132 if ( realtag < (module+1) * 64 * _nsamples + 6*module ) break;
0133 }
0134
0135 if ( module +1 > _nr_modules)
0136 {
0137 _nr_modules = module +1;
0138 _nchannels = _nr_modules * 64;
0139 }
0140
0141
0142
0143 int tag = realtag - 6*module;
0144
0145 int value = k[index] & 0x3fff;
0146
0147
0148
0149
0150 if ( module > 0 && realtag < ( module*64*_nsamples + 6*module ) )
0151 {
0152
0153 if ( realtag == module*64*_nsamples +2 + 6*(module -1) )
0154 {
0155 _fem_slot[module] = k[index+1] & 0xff;
0156 _fem_evtnr[module] = k[index+2] & 0xffff;
0157 _fem_clock[module] = k[index+3] & 0xffff;
0158
0159 }
0160
0161
0162
0163 continue;
0164 }
0165
0166
0167 int channelpair = (tag / _nsamples) & 0xfffe;
0168 int ch = channelpair;
0169 if ( tag & 1) ch++;
0170
0171 int sample = (tag/2) % _nsamples;
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 array[sample][ch] = value;
0183
0184 }
0185
0186
0187 for ( index = 5; index < getDataLength()-3; index+=2)
0188 {
0189 _calculated_even_checksum ^= SubeventData[index] & 0xffff;
0190 _calculated_odd_checksum ^= SubeventData[index+1] & 0xffff;
0191 }
0192
0193 return 0;
0194 }
0195
0196 long long Packet_iddigitizerv2::lValue(const int n, const char *what)
0197 {
0198
0199 decode();
0200
0201 if ( strcmp(what,"CLOCK") == 0 )
0202 {
0203 return (unsigned int) _clock;
0204 }
0205 if ( strcmp(what,"EVTNR") == 0 )
0206 {
0207 return _evtnr;
0208 }
0209 return 0;
0210 }
0211
0212 int Packet_iddigitizerv2::iValue(const int sample, const int ch)
0213 {
0214 decode();
0215
0216 if ( sample >= _nsamples || sample < 0
0217 || ch >= _nchannels || ch < 0 ) return 0;
0218
0219 return array[sample][ch];
0220
0221 }
0222
0223 int Packet_iddigitizerv2::iValue(const int n, const char *what)
0224 {
0225
0226 decode();
0227
0228 if ( strcmp(what,"CLOCK") == 0 )
0229 {
0230 return _clock;
0231 }
0232
0233 if ( strcmp(what,"EVTNR") == 0 )
0234 {
0235 return _evtnr;
0236 }
0237
0238 if ( strcmp(what,"SAMPLES") == 0 )
0239 {
0240 return _nsamples;
0241 }
0242
0243 if ( strcmp(what,"NRMODULES") == 0 )
0244 {
0245 return _nr_modules;
0246 }
0247
0248
0249 if ( strcmp(what,"CHANNELS") == 0 )
0250 {
0251 return _nchannels;
0252 }
0253
0254 if ( strcmp(what,"MODULEADDRESS") == 0 )
0255 {
0256 return _module_address;
0257 }
0258
0259
0260 if ( strcmp(what,"FEMSLOT") == 0 )
0261 {
0262 if ( n < 0 || n >= _nr_modules) return 0;
0263 return _fem_slot[n];
0264 }
0265
0266 if ( strcmp(what,"FEMEVTNR") == 0 )
0267 {
0268 if ( n < 0 || n >= _nr_modules) return 0;
0269 return _fem_evtnr[n];
0270 }
0271
0272 if ( strcmp(what,"FEMCLOCK") == 0 )
0273 {
0274 if ( n < 0 || n >= _nr_modules) return 0;
0275 return _fem_clock[n];
0276 }
0277
0278 if ( strcmp(what,"EVENCHECKSUM") == 0 )
0279 {
0280 return _even_checksum;
0281 }
0282
0283 if ( strcmp(what,"ODDCHECKSUM") == 0 )
0284 {
0285 return _odd_checksum;
0286 }
0287
0288 if ( strcmp(what,"CALCEVENCHECKSUM") == 0 )
0289 {
0290 return _calculated_even_checksum;
0291 }
0292
0293 if ( strcmp(what,"CALCODDCHECKSUM") == 0 )
0294 {
0295 return _calculated_odd_checksum;
0296 }
0297
0298 if ( strcmp(what,"EVENCHECKSUMOK") == 0 )
0299 {
0300 if ( _calculated_even_checksum < 0 ) return -1;
0301 if ( _even_checksum == _calculated_even_checksum) return 1;
0302 return 0;
0303 }
0304
0305 if ( strcmp(what,"ODDCHECKSUMOK") == 0 )
0306 {
0307 if ( _calculated_odd_checksum < 0 ) return -1;
0308 if ( _odd_checksum == _calculated_odd_checksum) return 1;
0309 return 0;
0310 }
0311
0312 if ( strcmp(what,"CHECKSUMOK") == 0 )
0313 {
0314 if ( _calculated_odd_checksum < 0 ) return -1;
0315 if ( _calculated_even_checksum < 0 ) return -1;
0316 if ( _even_checksum == _calculated_even_checksum && _odd_checksum == _calculated_odd_checksum) return 1;
0317
0318 return 0;
0319
0320 }
0321
0322
0323 return 0;
0324
0325 }
0326
0327 void Packet_iddigitizerv2::dump ( OSTREAM& os )
0328 {
0329 identify(os);
0330
0331 os << "Evt Nr: " << iValue(0,"EVTNR") << std::endl;
0332 os << "Clock: 0x" << std::hex << iValue(0,"CLOCK") << std::dec << std::endl;
0333 os << "Nr Modules: " << iValue(0,"NRMODULES") << std::endl;
0334 os << "Channels: " << iValue(0,"CHANNELS") << std::endl;
0335 os << "Samples: " << iValue(0,"SAMPLES") << std::endl;
0336
0337 os << "Mod. Addr: " << hex << "0x" << iValue(0,"MODULEADDRESS") << dec << std::endl;
0338
0339 os << "FEM Slot: ";
0340 for ( int i = 0; i < iValue(0,"NRMODULES"); i++) os << setw(8) << iValue(i,"FEMSLOT");
0341 os << std::endl;
0342
0343 os << "FEM Evt nr: ";
0344 for ( int i = 0; i < iValue(0,"NRMODULES"); i++) os << setw(8) << iValue(i,"FEMEVTNR");
0345 os << std::endl;
0346
0347 os << "FEM Clock: ";
0348 for ( int i = 0; i < iValue(0,"NRMODULES"); i++) os << std::hex << setw(4) << "0x" << iValue(i,"FEMCLOCK") << std::dec;
0349 os << std::endl;
0350
0351 os << "Even chksum: 0x" << hex << iValue(0,"EVENCHECKSUM") << " calculated: 0x" << iValue(0,"CALCEVENCHECKSUM");
0352 if ( iValue(0,"EVENCHECKSUMOK") == 1) os << " ok" << endl;
0353 else if ( iValue(0,"EVENCHECKSUMOK") == 0) os << " **wrong" << endl;
0354
0355 os << "Odd chksum: 0x" << hex << iValue(0,"ODDCHECKSUM") << " calculated: 0x" << iValue(0,"CALCODDCHECKSUM");
0356 if ( iValue(0,"ODDCHECKSUMOK") == 1) os << " ok" << endl;
0357 else if ( iValue(0,"ODDCHECKSUMOK") == 0) os << " **wrong" << endl;
0358 os << dec << endl;
0359
0360 for ( int c = 0; c < _nchannels; c++)
0361 {
0362 os << setw(4) << c << " | ";
0363
0364 os << hex;
0365 for ( int s = 0; s < _nsamples; s++)
0366 {
0367 os << setw(6) << iValue(s,c);
0368 }
0369 os << dec << endl;
0370 }
0371
0372 }