]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONTrackerDDLDecoder.h
In MUONRecoCheck.C:
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerDDLDecoder.h
CommitLineData
e3a2b9c9 1#ifndef ALIMUONTRACKERDDLDECODER_H
2#define ALIMUONTRACKERDDLDECODER_H
3/**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * All rights reserved. *
6 * *
7 * Primary Authors: *
8 * Artur Szostak <artursz@iafrica.com> *
9 * *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
18
19/* $Id$ */
20
21///
22/// \file AliMUONTrackerDDLDecoder.h
23/// \author Artur Szostak <artursz@iafrica.com>
24/// \date 28-11-2007
25/// \brief Implementation of a high performance DDL decoder for the muon tracking stations.
26///
27/// This file implementes the AliMUONTrackerDDLDecoder class, which contains
24a0fe9f 28/// the core logic for decoding the payload in DDL streams coming from the muon
e3a2b9c9 29/// spectrometer's tracking chambers in a very efficient manner.
30///
31/// This implementation is derived from work done by Christian Finck for the
32/// AliMUONPayloadTracker.
33///
34/// Note to maintainers: Please remember that this file is used by the online
35/// dHLT system. As an online system, the dHLT requires the fastest code possible
36/// in the decoders to satisfy its timing constraints. The performance impact
37/// must be checked before any proposed modification is made to this file.
38///
39
40#include "AliMUONTrackerDDLDecoderEventHandler.h"
41
e3a2b9c9 42/// \ingroup raw
43/// \class AliMUONTrackerDDLDecoder
44/// \brief A high performance decoder class for MUON tracking DDL data.
45///
46/// This class implements a high performance decoder for decoding DDL payload
47/// data coming from the muon spectrometers tracking chambers.
24a0fe9f 48/// It has been implemented using the event driven paradigm with templates,
49/// which allows us to minimise the number of method calls made in the inner
50/// loops of the algorithm and minimise the memory footprint. At least for
51/// optimised production compilations.
e3a2b9c9 52/// The decoder class only contains the basic decoding and error checking logic.
53/// It calls methods such as OnNewBlock, OnNewBusPatch, OnData etc in
54/// the event handler during the decoding to return the decoded data.
55/// The event handler class is nothing more than a callback interface to deliver
56/// the next chunks of decoded data.
24a0fe9f 57/// To actually do something with the data, one needs to implement a custom
e3a2b9c9 58/// event handler (callback) class by inheriting from AliMUONTrackerDDLDecoderEventHandler
59/// and overriding the callback methods like so:
60/// \code
61/// class MyCustomHandler : public AliMUONTrackerDDLDecoderEventHandler
62/// {
63/// public:
24a0fe9f 64/// void OnData(UInt_t data, bool parityError)
e3a2b9c9 65/// {
24a0fe9f 66/// // I can do something with 'data' here and check if there was
67/// // a parity error with 'parityError'.
e3a2b9c9 68/// }
69/// };
70/// \endcode
71///
24a0fe9f 72/// Once the custom handler is written then the decoder is instantiated as
73/// shown below, to use your new custom handler. Also to start decoding one needs
e3a2b9c9 74/// to call the Decode() method of the decoder.
75/// \code
76/// AliMUONTrackerDDLDecoder<MyCustomHandler> myDecoder;
77/// muDecoder.Decoder(buffer, bufferSize);
78/// \endcode
79///
80/// Note that this class was written as a template on purpose. To maximise the
24a0fe9f 81/// compilers chance to make optimisations and inline the code we must use a template.
82/// Depending on exactly what you do inside your handler, the decoder could be
83/// significantly slower if run time polymorphism was used, i.e. making the class
e3a2b9c9 84/// AliMUONTrackerDDLDecoderEventHandler abstract and using virtual methods.
85///
29b6be6a 86/// There has been a change to the data format that the real detector generates.
87/// Two trailer words are added to the end of the DDL payload which indicated
88/// the end of data. The decoder is initialised by default to automatically
89/// check for these and deal with it correctly, if they exist or not.
90/// However, if you want to override this behaviour then set the flag
91/// fAutoDetectTrailer to false with AutoDetectTrailer(false). Then if you have
92/// data with the old data format you should set fCheckForTrailer to false with
93/// CheckForTrailer(false), otherwise for real data it should be
94/// fCheckForTrailer = true. Only when fAutoDetectTrailer is true will the
95/// fCheckForTrailer flag be ignored and no warnings will be generated for an
96/// incorrect data format.
97///
e3a2b9c9 98/// \author Artur Szostak <artursz@iafrica.com>
99
100template <class EventHandler>
101class AliMUONTrackerDDLDecoder
102{
103public:
104
105 /// Default contructor.
106 AliMUONTrackerDDLDecoder() :
107 fExitOnError(true), fTryRecover(false),
108 fSendDataOnParityError(false), fHadError(false),
29b6be6a 109 fAutoDetectTrailer(true), fCheckForTrailer(true),
e3a2b9c9 110 fMaxBlocks(2), fMaxDSPs(5), fMaxBusPatches(5), fHandler()
111 {}
112
113 /// Constant method to return the event handler instance.
114 const EventHandler& GetHandler() const { return fHandler; }
115
116 /// Returns the event handler instance.
117 EventHandler& GetHandler() { return fHandler; }
118
119 /// Returns the "exit on error" flag.
120 /// i.e. should the decoder stop on the very first error found.
121 bool ExitOnError() const { return fExitOnError; }
122
123 /// Sets the "exit on error" flag.
124 /// i.e. should the decoder stop on the very first error found.
125 void ExitOnError(bool value) { fExitOnError = value; }
126
127 /// Returns the "try to recover from errors" flag.
128 /// i.e. should the decoder try to recover from errors found in the
29b6be6a 129 /// payload headers or trailers.
e3a2b9c9 130 bool TryRecover() const { return fTryRecover; }
131
132 /// Sets the "try to recover from errors" flag.
133 /// i.e. should the decoder try to recover from errors found in the
29b6be6a 134 /// payload headers or trailers.
e3a2b9c9 135 void TryRecover(bool value) { fTryRecover = value; }
136
137 /// Returns the flag indicating if the raw data words in the bus patches
138 /// that failed their parity tests (i.e. parity error / bit flip in the
139 /// raw data word) will be sent to the event handler anyway through OnData.
140 bool SendDataOnParityError() const { return fSendDataOnParityError; }
141
142 /// Sets the flag indicating if the raw data words in the bus patches
143 /// that failed their parity tests (i.e. parity error / bit flip in the
144 /// raw data word) will be sent to the event handler anyway through OnData.
145 void SendDataOnParityError(bool value) { fSendDataOnParityError = value; }
146
147 /// Returns the maximum block count expected in the DDL payload.
148 UInt_t MaxBlocks() const { return fMaxBlocks; }
149
150 /// Sets the maximum block count expected in the DDL payload.
151 void MaxBlocks(UInt_t n) { fMaxBlocks = n; }
152
153 /// Returns the maximum DSP header count expected in any given block
154 /// structure within the DDL payload.
155 UInt_t MaxDSPs() const { return fMaxDSPs; }
156
157 /// Sets the maximum DSP header count expected in any given block structure
158 /// within the DDL payload.
159 void MaxDSPs(UInt_t n) { fMaxDSPs = n; }
160
161 /// Returns the maximum number of bus patches expected in any given DSP
162 /// structure within the DDL payload.
163 UInt_t MaxBusPatches() const { return fMaxBusPatches; }
164
165 /// Sets the maximum number of bus patches expected in any given DSP
166 /// structure within the DDL payload.
167 void MaxBusPatches(UInt_t n) { fMaxBusPatches = n; }
168
29b6be6a 169 /// Returns the value of the auto-detect trailer flag.
170 bool AutoDetectTrailer() const { return fAutoDetectTrailer; }
171
172 /// Sets the value of the auto-detect trailer flag.
173 void AutoDetectTrailer(bool value) { fAutoDetectTrailer = value; }
174
175 /// Returns the value of the flag to check for the end of DDL trailer.
176 bool CheckForTrailer() const { return fCheckForTrailer; }
177
178 /// Sets the value of the flag to check for the end of DDL trailer.
179 void CheckForTrailer(bool value) { fCheckForTrailer = value; }
180
e3a2b9c9 181 /// This method decodes the DDL payload contained in the buffer.
182 bool Decode(const void* buffer, UInt_t bufferSize);
183
1788245f 184 /// Returns the block marker key.
185 static UInt_t BlockDataKeyWord() { return fgkBlockDataKey; }
186
187 /// Returns the DSP marker key.
188 static UInt_t DspDataKeyWord() { return fgkDSPDataKey; }
189
190 /// Returns the bus patch marker key.
191 static UInt_t BusPatchDataKeyWord() { return fgkBusPatchDataKey; }
192
193 /// Returns the expected padding word value.
194 static UInt_t PaddingWord() { return fgkPaddingWord; }
195
196 /// Returns the expected end of DDL marker.
197 static UInt_t EndOfDDLWord() { return fgkEndOfDDL; }
198
e3a2b9c9 199private:
200
201 bool fExitOnError; ///< Indicates if we should exit on the very first error.
29b6be6a 202 bool fTryRecover; ///< Indicates if we should try recover from a corrupt structure header or DDL trailer.
e3a2b9c9 203 bool fSendDataOnParityError; ///< If set to true then we issue a OnData() event even if the data word had a parity error.
204 bool fHadError; ///< Indicates if we had an error decoding the data.
29b6be6a 205 bool fAutoDetectTrailer; ///< Indicates if we should automatically check for the end of DDL trailer (Default = true).
206 bool fCheckForTrailer; ///< Indicates if we should check for the end of DDL trailer (Default = true). This flag is ignored if fAutoDetectTrailer is true.
e3a2b9c9 207 UInt_t fMaxBlocks; ///< Maximum number of block structures allowed in a DDL stream.
208 UInt_t fMaxDSPs; ///< Maximum number of DSP structures allowed in a DDL stream.
209 UInt_t fMaxBusPatches; ///< Maximum number of bus patch structures allowed in a DDL stream.
210 EventHandler fHandler; ///< The event handler which deals with parsing events.
211
8a0dae7c 212 void DecodeBuffer(const UChar_t* start, const UChar_t* end);
213
e3a2b9c9 214 bool DecodeBlockData(
215 const AliMUONBlockHeaderStruct* blockHeader,
216 const UChar_t* start, const UChar_t* end
217 );
218
219 bool DecodeDSPData(const UChar_t* start, const UChar_t* end);
220
221 bool DecodeBusPatchData(const UChar_t* start, const UChar_t* end);
222
223 /// Possible results that can be returned by the TryRecoverStruct method.
224 enum RecoverResult
225 {
226 kRecoverFailed, ///< The recovery failed. Cannot continue parsing.
227 kStructRecovered, ///< Indicates that we recovered from a corrupt structure header and can continue processing the given structure.
228 kContinueToNextStruct ///< Must continue parsing the next structure and ignore the current one.
229 };
230
231 RecoverResult TryRecoverStruct(
232 UInt_t expectedKey,
233 UInt_t headerSize,
234 UInt_t totalLength,
235 UInt_t length,
236 const UChar_t* structStart,
237 const UChar_t* bufferEnd,
238 const UChar_t*& dataEnd,
239 const UChar_t*& structEnd,
240 const UChar_t*& current
241 );
242
243 const UChar_t* FindKey(
244 UInt_t key, const UChar_t* start, const UChar_t* end
245 );
246
247 bool ParityIsOk(UInt_t data);
248
249 static const UInt_t fgkBlockDataKey; ///< The key word expected to identify block structure headers.
250 static const UInt_t fgkDSPDataKey; ///< The key word expected to identify DSP structure headers.
251 static const UInt_t fgkBusPatchDataKey; ///< The key word expected to identify bus patch headers.
252 static const UInt_t fgkPaddingWord; ///< The expected format of the padding word in the DDL payload.
29b6be6a 253 static const UInt_t fgkEndOfDDL; ///< The end of DDL trailer word.
e3a2b9c9 254};
255
256//_____________________________________________________________________________
257
258// The following are the structure header keys which are used to identify the kind
259// of structure header we are dealing with: block, DSP or bus patch header.
260template <class EventHandler>
261const UInt_t AliMUONTrackerDDLDecoder<EventHandler>::fgkBlockDataKey = 0xFC0000FC;
262template <class EventHandler>
263const UInt_t AliMUONTrackerDDLDecoder<EventHandler>::fgkDSPDataKey = 0xF000000F;
264template <class EventHandler>
265const UInt_t AliMUONTrackerDDLDecoder<EventHandler>::fgkBusPatchDataKey = 0xB000000B;
266template <class EventHandler>
267const UInt_t AliMUONTrackerDDLDecoder<EventHandler>::fgkPaddingWord = 0xBEEFFACE;
29b6be6a 268template <class EventHandler>
269const UInt_t AliMUONTrackerDDLDecoder<EventHandler>::fgkEndOfDDL = 0xD000000D;
e3a2b9c9 270
271
e3a2b9c9 272template <class EventHandler>
273bool AliMUONTrackerDDLDecoder<EventHandler>::Decode(const void* buffer, UInt_t bufferSize)
274{
275 /// This method should be called to actually decode the DDL payload
276 /// contained in a memory buffer. The payload should be for a muon tracking
277 /// chamber DDL stream.
278 /// As the decoder progresses it will make method calls to the event handler
279 /// instance (which can be accessed with the GetHandler() method) to indicate
280 /// the start of the new block, DSP and bus patch headers. For every raw
281 /// data word the OnData method of the event handler is called.
282 ///
283 /// If an error occurs during the parse because the data is corrupt then
284 /// the OnError method is called indicating what the problem was.
285 /// Decoding will stop at this point unless the fExitOnError flag is set
286 /// to false. Also raw data words which contain a parity error are only
287 /// sent to the event handler with OnData if the fSendDataOnParityError
288 /// flag is set to true. There is also an optional flag fTryRecover which
289 /// can enable logic which will attempt to recover the header structures found
290 /// in the DDL payload if they are found to be inconsistent (assumed corrupt).
29b6be6a 291 /// fTryRecover set to true will also enable recovery from a corrupt
292 /// DDL trailer marking the end of DDL payload.
e3a2b9c9 293 ///
294 /// \param buffer This is the pointer to the start of the memory buffer
295 /// containing the DDL payload. Remember that this must be the start of
296 /// the payload and not the DDL stream. That is, this pointer should be
297 /// equal to: DDL start pointer + 8 * sizeof(UInt_t).
298 /// \param bufferSize This is the pointer to the first byte just past the
299 /// end of the block structure.
300 /// \return Returns false if there was any problem with decoding the data,
301 /// and true otherwise. Note: the data may have been partially decoded
302 /// even if false was returned, which would be indicated by at least one
303 /// call to the event handlers OnData method.
304
305 assert( buffer != NULL );
306
307 fHadError = false;
308
309 // We are basically implementing something like a recursive decent parser.
8a0dae7c 310 // So start by marking the start of buffer position and end of buffer.
311 const UChar_t* start = reinterpret_cast<const UChar_t*>(buffer);
312 const UChar_t* end = start + bufferSize;
e3a2b9c9 313
e3a2b9c9 314 fHandler.OnNewBuffer(buffer, bufferSize);
8a0dae7c 315 DecodeBuffer(start, end);
316 fHandler.OnEndOfBuffer(buffer, bufferSize);
317 return not fHadError;
318}
319
320
321template <class EventHandler>
322void AliMUONTrackerDDLDecoder<EventHandler>::DecodeBuffer(
323 const UChar_t* start, const UChar_t* end
324 )
325{
326 /// This method decodes the buffer's payload data. It unpacks the block
327 /// structures contained inside and then for each block it calls the
328 /// OnNewBlock method for the event handler to signal the start of each new
329 /// block structure. OnEndOfBlock is called once each block is processed.
330 /// \param start This is the pointer to the start of the buffer.
331 /// \param end This is the pointer to the first byte just past the
332 /// end of the buffer.
7795d339 333 /// fHadError is set to true if there were any errors decoding the buffer
334 /// and the OnError method of the callback event handler is called for
335 /// each error.
8a0dae7c 336
337 const UChar_t* current = start;
29b6be6a 338 const UInt_t* bufferStart = reinterpret_cast<const UInt_t*>(start);
339 const UInt_t* bufferEnd = reinterpret_cast<const UInt_t*>(end);
340 bool problemWithTrailer = false;
341
342 // The DDL payload normally has a 2 word trailer which contains the end of
343 // DDL markers 0xD000000D. But this is not the case for older simulated
24a0fe9f 344 // data so if we are auto-detecting the trailer then we need to carefully
29b6be6a 345 // check if these words are there or not.
346 const UChar_t* endOfBlocks = end;
347 const UInt_t* trailerWords = reinterpret_cast<const UInt_t*>(end) - 2;
348 if (fAutoDetectTrailer)
349 {
350 if (trailerWords >= bufferStart and *trailerWords == fgkEndOfDDL
351 and *(trailerWords+1) == fgkEndOfDDL
352 )
353 {
354 // Found the trailer so reposition the end of blocks marker.
355 endOfBlocks = reinterpret_cast<const UChar_t*>(trailerWords);
356 }
357 // else assume we are dealing with the older data format.
358 }
359 else if (fCheckForTrailer)
360 {
361 if (trailerWords >= bufferStart and *trailerWords == fgkEndOfDDL
362 and *(trailerWords+1) == fgkEndOfDDL
363 )
364 {
365 // Found the trailer so reposition the end of blocks marker.
366 endOfBlocks = reinterpret_cast<const UChar_t*>(trailerWords);
367 }
368 else
369 {
370 if (trailerWords+1 >= bufferStart and *(trailerWords+1) == fgkEndOfDDL)
371 fHandler.OnError(EventHandler::kTooFewDDLTrailerWords, trailerWords+1);
372 else if (trailerWords >= bufferStart and *(trailerWords) == fgkEndOfDDL)
373 fHandler.OnError(EventHandler::kTooFewDDLTrailerWords, trailerWords);
374 else
375 fHandler.OnError(EventHandler::kNoDDLTrailerWords, end);
376
377 // Stop the decoding if so requested by the user, otherwise
378 // remember about the error so that we return false from the
379 // Decode() method and continue decoding.
380 fHadError = true;
381 if (fExitOnError) return;
382
383 // Mark that there was a problem with the trailer so that
24a0fe9f 384 // for subsequent errors we try to deal with this better.
29b6be6a 385 problemWithTrailer = true;
386
387 // We can also try figure out how many trailer words there
388 // actually are and move the end of blocks marker back.
389
390 if (fTryRecover)
391 {
392 trailerWords = bufferEnd;
393 // There should only be a max of 2 trailer words.
394 if (*(trailerWords-1) == fgkEndOfDDL)
395 trailerWords--;
396 else if (*(trailerWords-1) == fgkEndOfDDL)
397 trailerWords--;
398 endOfBlocks = reinterpret_cast<const UChar_t*>(trailerWords);
399 }
400 }
401 }
e3a2b9c9 402
403 UInt_t blockCount = 0; // Indicates the number of blocks decoded.
29b6be6a 404 while (current < endOfBlocks)
e3a2b9c9 405 {
406 // Mark the start of the block structure.
407 const UChar_t* blockStart = current;
408
409 // Get the block header, move the current pointer just past the end
410 // of the header and check that we have not overflowed the buffer.
411 const AliMUONBlockHeaderStruct* blockHeader
412 = reinterpret_cast<const AliMUONBlockHeaderStruct*>(blockStart);
413 current += sizeof(AliMUONBlockHeaderStruct);
29b6be6a 414 if (current > endOfBlocks)
e3a2b9c9 415 {
29b6be6a 416 // We first check if we actually hit the end of DDL markers
417 // If we did then either we did not/could not recover from
418 // a corrupt trailer or we did not detect a correct trailer
419 // in auto-detect mode.
420 trailerWords = reinterpret_cast<const UInt_t*>(blockHeader);
421 // The "trailerWords+1 <= bufferEnd" checks that we are
422 // not reading beyond the end of the buffer.
423 if (trailerWords+1 <= bufferEnd and *trailerWords == fgkEndOfDDL)
424 {
425 // If we aready knew the trailer was corrupt then just
426 // return because the error was already announced.
427 if (problemWithTrailer) return;
428
429 if (fAutoDetectTrailer)
430 {
431 // If we got here then there is at least one correct trailer
24a0fe9f 432 // word, but since we did not detect a correct trailer then
29b6be6a 433 // there must be only one. Announce the error and exit.
434 fHandler.OnError(EventHandler::kTooFewDDLTrailerWords, trailerWords);
435 fHadError = true;
436 return;
437 }
438 }
439
e3a2b9c9 440 // So we only got part of a block header at the very end
441 // of the buffer. Nothing to do but report the error and exit.
442 if (blockCount == fMaxBlocks)
443 // Special case where we got all the blocks we
444 // expected, so the remaining data must be rubbish.
445 fHandler.OnError(EventHandler::kBufferTooBig, blockHeader);
446 else
447 fHandler.OnError(EventHandler::kNoBlockHeader, blockHeader);
8a0dae7c 448 fHadError = true;
449 return;
e3a2b9c9 450 }
451
452 // The header fits the buffer so we can mark the data start and
453 // read from the header to find the end of data and block pointers.
454 const UChar_t* dataStart = current;
455 current += blockHeader->fLength * sizeof(UInt_t);
456 const UChar_t* dataEnd = current;
457 const UChar_t* blockEnd = blockStart
458 + blockHeader->fTotalLength * sizeof(UInt_t);
459
460 // Now we need to check for the following things:
461 // 1) Is the end of block or end of data pointer outside the buffer
462 // boundaries.
463 // 2) Are the values for these pointers the same.
464 // 3) Is the expected data key in the header present.
465 // If any of the above fail then we know there is a problem with
466 // the block header. It must be corrupted somehow.
467 if (blockHeader->fDataKey != fgkBlockDataKey
29b6be6a 468 or dataEnd > endOfBlocks or blockEnd > endOfBlocks or dataEnd != blockEnd)
e3a2b9c9 469 {
470 // So let us see what exactly is wrong and report this.
471 if (blockCount == fMaxBlocks)
472 {
473 // Special case where we got all the blocks we
474 // expected, so the remaining data must be rubbish.
475 // Don't even bother trying to recover the data.
476 fHandler.OnError(EventHandler::kBufferTooBig, blockHeader);
8a0dae7c 477 fHadError = true;
478 return;
e3a2b9c9 479 }
480 if (blockHeader->fDataKey != fgkBlockDataKey)
481 fHandler.OnError(EventHandler::kBadBlockKey, &blockHeader->fDataKey);
29b6be6a 482 if (blockEnd > endOfBlocks)
e3a2b9c9 483 fHandler.OnError(EventHandler::kBadBlockLength, &blockHeader->fLength);
29b6be6a 484 if (dataEnd > endOfBlocks)
e3a2b9c9 485 fHandler.OnError(EventHandler::kBadBlockTotalLength, &blockHeader->fTotalLength);
486 if (dataEnd != blockEnd)
487 fHandler.OnError(EventHandler::kBlockLengthMismatch, blockHeader);
488
489 // Stop the decoding if so requested by the user, otherwise
8a0dae7c 490 // remember about the error so that we return false from the
491 // Decode() method and continue decoding.
492 fHadError = true;
493 if (fExitOnError) return;
e3a2b9c9 494
495 // Try to recover from the corrupt header.
496 RecoverResult result = TryRecoverStruct(
497 fgkBlockDataKey, sizeof(AliMUONBlockHeaderStruct),
498 blockHeader->fTotalLength, blockHeader->fLength,
29b6be6a 499 blockStart, endOfBlocks, dataEnd, blockEnd, current
e3a2b9c9 500 );
501 if (result == kContinueToNextStruct)
502 continue; // Try the next block at 'current'.
8a0dae7c 503 if (result == kRecoverFailed) return;
e3a2b9c9 504 }
505
506 // At this point we certainly have a valid block header, so we
507 // need to check if we have more blocks than we expected. If not
508 // then we can indicate we have another block and decode its data.
509 if (++blockCount > fMaxBlocks)
510 {
511 fHandler.OnError(EventHandler::kTooManyBlocks, current);
512
513 // In this case we stop the decoding because clearly
514 // something is seriously wrong with the data if we are
515 // getting more blocks than expected.
8a0dae7c 516 fHadError = true;
517 return;
e3a2b9c9 518 }
519
520 fHandler.OnNewBlock(blockHeader, dataStart);
8a0dae7c 521 if (not DecodeBlockData(blockHeader, dataStart, dataEnd))
522 {
523 // At this point we had a problem decoding the block structure's
524 // data. Thus we should stop further decoding if so requested by
525 // the user. Note the fHadError flag is already marked inside
526 // DecodeBlockData.
527 if (fExitOnError)
528 {
529 fHandler.OnEndOfBlock(blockHeader, dataStart);
530 return;
531 }
532 }
533 fHandler.OnEndOfBlock(blockHeader, dataStart);
e3a2b9c9 534 }
e3a2b9c9 535}
536
537
538template <class EventHandler>
539bool AliMUONTrackerDDLDecoder<EventHandler>::DecodeBlockData(
540 const AliMUONBlockHeaderStruct* blockHeader,
541 const UChar_t* start, const UChar_t* end
542 )
543{
544 /// This method decodes a block structure's data payload. It unpacks the
545 /// DSP structures contained inside and then for each DSP it calls the
546 /// OnNewDSP method for the event handler to signal the start of each new
547 /// DSP structure.
548 /// \param blockHeader
549 /// \param start This is the pointer to the start of the block
550 /// structure's data.
551 /// \param end This is the pointer to the first byte just past the
552 /// end of the block structure.
553 /// \return If the block structure's data was decoded without errors
554 /// or we could recover from the errors, then true is returned.
555 /// False is returned otherwise.
556
557 const UChar_t* current = start;
558
559 UInt_t dspCount = 0; // Indicates the number of DSPs decoded.
560 while (current < end)
561 {
562 // Mark the start of the DSP structure.
563 const UChar_t* dspStart = current;
564
565 // Get the DSP header, move the current pointer just past the end
566 // of the header and check that we have not overflowed the buffer.
567 const AliMUONDSPHeaderStruct* dspHeader
568 = reinterpret_cast<const AliMUONDSPHeaderStruct*>(dspStart);
569 current += sizeof(AliMUONDSPHeaderStruct);
570 if (current > end)
571 {
572 // So we only got part of a DSP header at the very end of
573 // the block structure buffer. Nothing to do but report the
574 // error and exit. Set fHadError in case of further decoding.
575 fHandler.OnError(EventHandler::kNoDSPHeader, dspHeader);
576 fHadError = true;
577 return false;
578 }
579
580 // The header fits the buffer so we can mark the data start and
581 // read from the header to find the end of data and DSP pointers.
582 const UChar_t* dataStart = current;
583 current += dspHeader->fLength * sizeof(UInt_t);
584 const UChar_t* dataEnd = current;
585 const UChar_t* dspEnd = dspStart + dspHeader->fTotalLength * sizeof(UInt_t);
586
587 // Now we need to check for the following things:
588 // 1) Is the end of DSP or end of data pointer outside the buffer
589 // boundaries.
590 // 2) Are the values for these pointers the same.
591 // 3) Is the expected data key in the header present.
592 // If any of the above fail then we know there is a problem with
593 // the DSP header. It must be corrupted somehow.
594 if (dspHeader->fDataKey != fgkDSPDataKey
595 or dataEnd > end or dspEnd > end or dataEnd != dspEnd)
596 {
597 // So let us see what exactly is wrong and report this.
598 if (dspHeader->fDataKey != fgkDSPDataKey)
599 fHandler.OnError(EventHandler::kBadDSPKey, &dspHeader->fDataKey);
600 if (dspEnd > end)
601 fHandler.OnError(EventHandler::kBadDSPLength, &dspHeader->fLength);
602 if (dataEnd > end)
603 fHandler.OnError(EventHandler::kBadDSPTotalLength, &dspHeader->fTotalLength);
604 if (dataEnd != dspEnd)
605 fHandler.OnError(EventHandler::kDSPLengthMismatch, dspHeader);
606
607 // Indicate we had and error and stop the decoding if so
608 // requested by the user.
609 fHadError = true;
610 if (fExitOnError) return false;
611
612 // Try to recover from the corrupt header.
613 RecoverResult result = TryRecoverStruct(
614 fgkDSPDataKey, sizeof(AliMUONDSPHeaderStruct),
615 dspHeader->fTotalLength, dspHeader->fLength,
616 dspStart, end, dataEnd, dspEnd, current
617 );
618 if (result == kContinueToNextStruct)
619 continue; // Try the next DSP at 'current'.
620 if (result == kRecoverFailed) return false;
621 }
622
623 // At this point we certainly have a valid DSP header, so we
624 // need to check if we have more DSPs than we expected. If not
625 // then we can indicate we have another DSP and decode its data.
626 if (++dspCount > fMaxDSPs)
627 {
628 fHandler.OnError(EventHandler::kTooManyDSPs, current);
629
630 // In this case we stop further decoding of the block
631 // structure data because clearly something is seriously
632 // wrong if we are getting more DSPs than expected.
633 // Indicate that we had an error so the Decode() method
634 // returns false.
635 fHadError = true;
636 return false;
637 }
638
639 fHandler.OnNewDSP(dspHeader, dataStart);
640
641 // Check the error word in the header.
74585128 642 if (dspHeader->fErrorWord != 0x0)
e3a2b9c9 643 {
74585128 644 if (dspHeader->fErrorWord == (0x000000B1 | blockHeader->fDSPId)
645 or dspHeader->fErrorWord == (0x00000091 | blockHeader->fDSPId)
646 )
647 {
648 // An event with a glitch in the readout has been detected.
649 // It means that somewhere a 1 byte word has been randomly
650 // inserted and all the readout sequence is shifted until
651 // the next event.
652 fHandler.OnError(EventHandler::kGlitchFound, &dspHeader->fErrorWord);
653 }
654 else if ((dspHeader->fErrorWord & 0x0000FFF0) == 0x220)
655 {
656 // Detected a TOKEN_LOST error which can affect the dead time in the DAQ.
657 fHandler.OnError(EventHandler::kTokenLost, &dspHeader->fErrorWord);
658 }
659 else
660 {
661 // The DSP error code is non-zero but has an unknown code.
662 fHandler.OnError(EventHandler::kUnknownDspError, &dspHeader->fErrorWord);
663 }
664
e3a2b9c9 665 fHadError = true;
8a0dae7c 666 if (fExitOnError)
667 {
668 fHandler.OnEndOfDSP(dspHeader, dataStart);
669 return false;
670 }
e3a2b9c9 671
672 // Try recover by finding the very next DSP and continue
673 // decoding from there. Note: to achieve all we have to do
674 // is continue to the next iteration, because the logic
675 // will land up calling the FindKey method within the
676 // TryRecoverStruct method above.
677 if (fTryRecover) continue;
678 }
679
680 // Check if we are padding. If we are, then the bus patch data is
681 // actually 4 bytes smaller and the last word is a padding word.
682 if (dspHeader->fPaddingWord == 1)
683 {
684 dataEnd -= sizeof(UInt_t);
685
686 // Check the pad word is correct.
687 const UInt_t* padWord = reinterpret_cast<const UInt_t*>(dataEnd);
688 if (*padWord != fgkPaddingWord)
689 {
690 fHandler.OnError(EventHandler::kBadPaddingWord, padWord);
691 fHadError = true;
8a0dae7c 692 if (fExitOnError)
693 {
694 fHandler.OnEndOfDSP(dspHeader, dataStart);
695 return false;
696 }
e3a2b9c9 697 }
698 }
699
8a0dae7c 700 if (not DecodeDSPData(dataStart, dataEnd))
701 {
702 // At this point we had a problem decoding the DSP structure's
703 // data, thus we should stop further decoding if so requested by
704 // the user. Note the fHadError flag is already marked inside
705 // DecodeDSPData.
706 if (fExitOnError)
707 {
708 fHandler.OnEndOfDSP(dspHeader, dataStart);
709 return false;
710 }
711 }
712 fHandler.OnEndOfDSP(dspHeader, dataStart);
e3a2b9c9 713 }
714
715 return true;
716}
717
718
719template <class EventHandler>
720bool AliMUONTrackerDDLDecoder<EventHandler>::DecodeDSPData(
721 const UChar_t* start, const UChar_t* end
722 )
723{
724 /// This method decodes a DSP structure's data payload. It finds all the
725 /// bus patches found inside and for each it calls the OnNewBusPatch method
726 /// for the event handler to signal the start of each new bus patch.
727 /// \param start This is the pointer to the start of the DSP structure's data.
728 /// \param end This is the pointer to the first byte just past the
729 /// end of the DSP structure.
730 /// \return If the DSP structure's data was decoded without errors
731 /// or we could recover from the errors, then true is returned.
732 /// False is returned otherwise.
733
734 const UChar_t* current = start;
735
736 UInt_t busPatchCount = 0; // Indicates the number of bus patches decoded.
737 while (current < end)
738 {
739 // Mark the start of the bus patch structure.
740 const UChar_t* busPatchStart = current;
741
742 // Get the bus patch header, move the current pointer just past
743 // the end of the header and check that we have not overflowed
744 // the buffer.
745 const AliMUONBusPatchHeaderStruct* busPatchHeader
746 = reinterpret_cast<const AliMUONBusPatchHeaderStruct*>(busPatchStart);
747 current += sizeof(AliMUONBusPatchHeaderStruct);
748 if (current > end)
749 {
750 // So we only got part of a bus patch header at the very
751 // end of the DSP structure buffer. Nothing to do but
752 // report the error and exit. Set fHadError in case of
753 // further decoding.
754 fHandler.OnError(EventHandler::kNoBusPatchHeader, busPatchHeader);
755 fHadError = true;
756 return false;
757 }
758
759 // The header fits the buffer so we can mark the data start and
760 // read from the header to find the end of data and bus patch
761 // structure pointers.
762 const UChar_t* dataStart = current;
763 current += busPatchHeader->fLength * sizeof(UInt_t);
764 const UChar_t* dataEnd = current;
765 const UChar_t* busPatchEnd = busPatchStart
766 + busPatchHeader->fTotalLength * sizeof(UInt_t);
767
768 // Now we need to check for the following things:
769 // 1) Is the end of bus patch structure or end of data pointer
770 // outside the buffer boundaries.
771 // 2) Are the values for these pointers the same.
772 // 3) Is the expected data key in the header present.
773 // If any of the above fail then we know there is a problem with
774 // the bus patch header. It must be corrupted somehow.
775 if (busPatchHeader->fDataKey != fgkBusPatchDataKey
776 or dataEnd > end or busPatchEnd > end or dataEnd != busPatchEnd)
777 {
778 // So let us see what exactly is wrong and report this.
779 if (busPatchHeader->fDataKey != fgkBusPatchDataKey)
780 fHandler.OnError(EventHandler::kBadBusPatchKey, &busPatchHeader->fDataKey);
781 if (busPatchEnd > end)
782 fHandler.OnError(EventHandler::kBadBusPatchLength, &busPatchHeader->fLength);
783 if (dataEnd > end)
784 fHandler.OnError(EventHandler::kBadBusPatchTotalLength, &busPatchHeader->fTotalLength);
785 if (dataEnd != busPatchEnd)
786 fHandler.OnError(EventHandler::kBusPatchLengthMismatch, busPatchHeader);
787
788 // Indicate we had and error and stop the decoding if so
789 // requested by the user.
790 fHadError = true;
791 if (fExitOnError) return false;
792
793 // Try to recover from the corrupt header.
794 RecoverResult result = TryRecoverStruct(
795 fgkBusPatchDataKey, sizeof(AliMUONBusPatchHeaderStruct),
796 busPatchHeader->fTotalLength, busPatchHeader->fLength,
797 busPatchStart, end, dataEnd, busPatchEnd, current
798 );
799 if (result == kContinueToNextStruct)
800 continue; // Try the next bus patch at 'current'.
801 if (result == kRecoverFailed) return false;
802 }
803
804 // At this point we certainly have a valid bus patch header, so
805 // we need to check if we have more bus patches than we expected.
806 // If not then we can indicate we have another bus patch and
807 // decode its data.
808 if (++busPatchCount > fMaxBusPatches)
809 {
810 fHandler.OnError(EventHandler::kTooManyBusPatches, current);
811
812 // In this case we stop further decoding of the DSP
813 // structure's data because clearly something is seriously
814 // wrong if we are getting more bus patches than expected.
815 // Indicate that we had an error so the Decode() method
816 // returns false.
817 fHadError = true;
818 return false;
819 }
820
821 fHandler.OnNewBusPatch(busPatchHeader, dataStart);
8a0dae7c 822 if (not DecodeBusPatchData(dataStart, dataEnd))
823 {
824 // At this point we had a problem decoding the bus patch data,
825 // thus we should stop further decoding if so requested by the
826 // user. Note the fHadError flag is already marked inside
827 // DecodeBusPatchData.
828 if (fExitOnError)
829 {
830 fHandler.OnEndOfBusPatch(busPatchHeader, dataStart);
831 return false;
832 }
833 }
834 fHandler.OnEndOfBusPatch(busPatchHeader, dataStart);
e3a2b9c9 835 }
836
837 return true;
838}
839
840
841template <class EventHandler>
842bool AliMUONTrackerDDLDecoder<EventHandler>::DecodeBusPatchData(
843 const UChar_t* start, const UChar_t* end
844 )
845{
846 /// This method decodes a single bus patch's data payload.
847 /// It will check the parity of the raw data words and send them
848 /// to the event handler instance with calls to OnData.
849 /// \param start This is the pointer to the start of the bus patch
850 /// structure's data.
851 /// \param end This is the pointer to the first byte just past the
852 /// end of the bus patch structure.
853 /// \return If the bus patch's data was decoded without errors
854 /// or we could recover from the errors, then true is returned.
855 /// False is returned otherwise.
856
857 // Assert that 'end' is always larger than start by n*sizeof(UInt_t)
858 // where n is a positive integer. This should be the case because we
859 // always add multiples of sizeof(UInt_t) to the 'current' pointer in
860 // all the DecodeXYZ methods.
861 assert( UInt_t(end - start) % 4 == 0 );
862
863 // Now step through all the data words and issue OnData events.
864 // We also need to check parity and signal OnError if it is not valid
865 // for any of the data words.
866 const UInt_t* data = reinterpret_cast<const UInt_t*>(start);
867 const UInt_t* dataEnd = reinterpret_cast<const UInt_t*>(end);
868 for (; data < dataEnd; data++)
869 {
870 if (ParityIsOk(*data))
871 {
8a0dae7c 872 fHandler.OnData(*data, false);
e3a2b9c9 873 }
874 else
875 {
876 // Indicate we had a parity error and exit immediately
877 // if the user so requested.
878 fHandler.OnError(EventHandler::kParityError, data);
879 fHadError = true;
880 if (fExitOnError) return false;
881
882 if (fSendDataOnParityError)
8a0dae7c 883 fHandler.OnData(*data, true);
e3a2b9c9 884 }
885 }
886
887 return true;
888}
889
890
891template <class EventHandler>
892typename AliMUONTrackerDDLDecoder<EventHandler>::RecoverResult
893AliMUONTrackerDDLDecoder<EventHandler>::TryRecoverStruct(
894 UInt_t expectedKey,
895 UInt_t headerSize,
896 UInt_t totalLength,
897 UInt_t length,
898 const UChar_t* structStart,
899 const UChar_t* bufferEnd,
900 const UChar_t*& dataEnd,
901 const UChar_t*& structEnd,
902 const UChar_t*& current
903 )
904{
905 /// This method attempts to recover from a corrupt structure header by
906 /// figuring out which of the structure size indicators is correct.
907 /// This is possible because each header has some redundant information.
908 /// The recovery procedure is only attempted if fTryRecover was set to
909 /// true. If the recovery procedure is successful then this method will
910 /// also update the pointers indicating the start of data, end of structure
911 /// and current parsing position with the correct values.
912 ///
913 /// [in] \param expectedKey This is the expected block key for the header
914 /// currently being processed.
915 /// [in] \param headerSize The expected header size as given by the sizeof
916 /// operator for example.
917 /// [in] \param totalLength The total length as given by the fTotalLength
918 /// field in the current header being handled.
919 /// [in] \param length The data length as given by the fLength field
920 /// in the current header being handled.
921 /// [in] \param structStart A pointer to the start of the structure header.
922 /// [in] \param bufferEnd A pointer to the first byte just past the end
923 /// of the buffer. This could be the pointer to the first byte
924 /// just past the end of the parent structure if we are dealing
925 /// with a DSP structure or bus patch. The parent structure for
926 /// the DSP is a block structure and for a bus patch it is a DSP.
927 /// [out] \param dataEnd This is the pointer to the first byte just past
928 /// the end of the structure being processed. It should be equal to
929 /// structStart + sizeof(structure header) + fLength, where fLength
930 /// is the field found in the structure's header itself. This value
931 /// will be corrected and updated if we could recover from the
932 /// corruption in the header.
933 /// [out] \param structEnd A pointer to the first byte just past the end of
934 /// the structure. This value should be set equal to
935 /// structStart + fTotalLength * sizeof(UInt_t), where fTotalLength
936 /// is the field found in the structure's header itself. This value
937 /// will be corrected and updated if we could recover from the
938 /// corruption in the header.
939 /// [out] \param current This is the pointer to the current location in
940 /// the DDL payload being parsed. It should in principle point
941 /// to the start of the structures data. This value will be
942 /// corrected and updated if we could recover from the corruption
943 /// in the header.
944 ///
945 /// \return Returns the result of the recovery attempt, which can be one
946 /// of the following:
947 /// kRecoverFailed - The recovery failed completely so the caller
948 /// cannot continue parsing any more structures. If the failure
949 /// is within a DSP then one could still continue parsing
950 /// from the next block. Similarly for bus patches, parsing could
951 /// continue from the next DSP structure.
952 /// kStructRecovered - Indicates that we recovered from a corrupt
953 /// structure header and can continue processing the data of the
954 /// structure in question.
955 /// kContinueToNextStruct - Either fTryRecover was set to false or we
956 /// could not recover from the corrupt header but we did find the
957 /// start of another header matching the expected key so parsing
958 /// can continue from the updated current position.
959
960 // Check if the user wants us to try and recover from a corrupt header.
961 if (not fTryRecover) return kContinueToNextStruct;
962
963 // If the user wants us to try recover, then try to recover what the
964 // correct values for dataEnd, structEnd and current were supposed to be.
965 // The recovery procedure is as follows: We have 4 conditions for a correct
966 // header:
967 // 1) The header key is what we expect.
968 // 2) The totalLength equals length + headerSize.
969 // 3) The word at dataEnd contains a valid key. (implies length is
970 // correct.)
971 // 4) The word at structEnd contains a valid key. (implies totalLength
972 // is correct.)
973 // If any 2 of these conditions hold then we know that only one of the
974 // header fields is corrupt and we have enough information to reconstruct
975 // the third field. Note that if conditions 3 and 4 are true then this
976 // implies 2 is also true. (not necessarily the other way around though.)
977 // The valid key mentioned above at dataEnd and structEnd should be:
978 // a) A bus patch key, DSP key or end of buffer if expectedKey indicates
979 // a buspatch.
980 // b) A DSP key, block structure key or end of buffer if expectedKey
981 // indicates a DSP.
982 // c) A block structure key or end of buffer if expectedKey indicates
983 // a DSP.
984 const UInt_t* headerKey = reinterpret_cast<const UInt_t*>(structStart);
985 bool headerKeyOk = (expectedKey == *headerKey);
986
cc696a72 987 bool lengthsMatch = (totalLength*4 == length*4 + headerSize);
e3a2b9c9 988
989 bool lengthIsCorrect = false;
990 bool totalLengthIsCorrect = false;
991 const UInt_t* keyAtDataEnd = reinterpret_cast<const UInt_t*>(dataEnd);
992 const UInt_t* keyAtStructEnd = reinterpret_cast<const UInt_t*>(structEnd);
993
a0fea27d 994
995 if ( expectedKey == fgkBlockDataKey )
996 {
e3a2b9c9 997 if (dataEnd == bufferEnd)
998 {
999 // Are we at the end of the buffer?
1000 lengthIsCorrect = true;
1001 }
1002 else
1003 {
1004 // Must check that we can read another 4 bytes before
1005 // checking the key at dataEnd.
1006 if (dataEnd + sizeof(UInt_t) <= bufferEnd)
1007 {
1008 if (*keyAtDataEnd == fgkBlockDataKey)
1009 lengthIsCorrect = true;
1010 }
1011 }
1012
1013 if (structEnd == bufferEnd)
1014 {
1015 // Are we at the end of the buffer?
1016 totalLengthIsCorrect = true;
1017 }
1018 else
1019 {
1020 // Must check that we can read another 4 bytes before
1021 // checking the key at structEnd.
1022 if (structEnd + sizeof(UInt_t) <= bufferEnd)
1023 {
1024 if (*keyAtStructEnd == fgkBlockDataKey)
1025 totalLengthIsCorrect = true;
1026 }
1027 }
a0fea27d 1028 }
e3a2b9c9 1029
a0fea27d 1030 else if ( expectedKey == fgkDSPDataKey )
1031 {
e3a2b9c9 1032 if (dataEnd == bufferEnd)
1033 {
1034 // Are we at the end of the buffer?
1035 lengthIsCorrect = true;
1036 }
1037 else
1038 {
1039 // Must check that we can read another 4 bytes before
1040 // checking the key at dataEnd.
1041 if (dataEnd + sizeof(UInt_t) <= bufferEnd)
1042 {
1043 if (*keyAtDataEnd == fgkBlockDataKey
1044 or *keyAtDataEnd == fgkDSPDataKey)
1045 lengthIsCorrect = true;
1046 }
1047 }
1048
1049 if (structEnd == bufferEnd)
1050 {
1051 // Are we at the end of the buffer?
1052 totalLengthIsCorrect = true;
1053 }
1054 else
1055 {
1056 // Must check that we can read another 4 bytes before
1057 // checking the key at structEnd.
1058 if (structEnd + sizeof(UInt_t) <= bufferEnd)
1059 {
1060 if (*keyAtStructEnd == fgkBlockDataKey
1061 or *keyAtStructEnd == fgkDSPDataKey)
1062 totalLengthIsCorrect = true;
1063 }
1064 }
a0fea27d 1065 }
1066 else if ( expectedKey == fgkBusPatchDataKey )
1067 {
e3a2b9c9 1068 if (dataEnd == bufferEnd)
1069 {
1070 // Are we at the end of the buffer?
1071 lengthIsCorrect = true;
1072 }
1073 else
1074 {
1075 // Must check that we can read another 4 bytes before
1076 // checking the key at dataEnd.
1077 if (dataEnd + sizeof(UInt_t) <= bufferEnd)
1078 {
1079 if (*keyAtDataEnd == fgkDSPDataKey
1080 or *keyAtDataEnd == fgkBusPatchDataKey)
1081 lengthIsCorrect = true;
1082 }
1083 }
1084
1085 if (structEnd == bufferEnd)
1086 {
1087 // Are we at the end of the buffer?
1088 totalLengthIsCorrect = true;
1089 }
1090 else
1091 {
1092 // Must check that we can read another 4 bytes before
1093 // checking the key at structEnd.
1094 if (structEnd + sizeof(UInt_t) <= bufferEnd)
1095 {
1096 if (*keyAtStructEnd == fgkDSPDataKey
1097 or *keyAtStructEnd == fgkBusPatchDataKey)
1098 totalLengthIsCorrect = true;
1099 }
1100 }
a0fea27d 1101 }
e3a2b9c9 1102
1103 if (headerKeyOk and lengthIsCorrect)
1104 {
1105 // totalLength was wrong, dataEnd is correct.
1106 structEnd = dataEnd;
1107 current = dataEnd;
1108 return kStructRecovered;
1109 }
1110 if (headerKeyOk and totalLengthIsCorrect)
1111 {
1112 // Length was wrong, structEnd is correct.
1113 dataEnd = structEnd;
1114 current = structEnd;
1115 return kStructRecovered;
1116 }
1117 if (lengthsMatch and lengthIsCorrect and totalLengthIsCorrect)
1118 {
1119 // The header's key was wrong but the lengths and pointers are OK.
1120 return kStructRecovered;
1121 }
1122
1123 // Could not recover the header from the available information, so find
1124 // the next key in the stream that is the same as the currently expected
1125 // one and continue decoding from there.
1126 const UChar_t* location = FindKey(
1127 expectedKey, structStart + sizeof(UInt_t), bufferEnd
1128 );
1129 if (location != NULL)
1130 {
1131 current = location;
1132 return kContinueToNextStruct;
1133 }
1134
1135 return kRecoverFailed;
1136}
1137
1138
1139template <class EventHandler>
1140const UChar_t* AliMUONTrackerDDLDecoder<EventHandler>::FindKey(
1141 UInt_t key, const UChar_t* start, const UChar_t* end
1142 )
1143{
1144 /// Searches for the first occurrence of the key value in the buffer marked by
1145 /// 'start' and 'end'. 'start' should point to the start of the buffer and 'end'
1146 /// should point to 'start + bufferSize', i.e. just past the last byte of the
1147 /// buffer. If the key was found then the pointer to that location is returned
1148 /// otherwise NULL is returned.
1149
1150 const UChar_t* current = start;
1151 while (current + sizeof(UInt_t) <= end)
1152 {
1153 UInt_t data = * reinterpret_cast<const UInt_t*>(current);
1154 if (data == key) return current;
1155 current++;
1156 }
1157 return NULL;
1158}
1159
1160
1161template <class EventHandler>
1162bool AliMUONTrackerDDLDecoder<EventHandler>::ParityIsOk(UInt_t data)
1163{
1164 /// Optimised parity check addapted from:
1165 /// http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
1166
1167 // parity of the 32 bits must be zero if the last bit is equal
1168 // to the parity of the first 31 bits.
1169 // Reason: the parity bit xor the parity of the first 31 bits must give
1170 // zero, unless there was a bit error.
1171 data ^= data >> 16;
1172 data ^= data >> 8;
1173 data ^= data >> 4;
1174 data &= 0xf;
1175 data = ((0x6996 >> data) & 1);
1176 return data == 0;
1177}
1178
1179#endif // ALIMUONTRACKERDDLDECODER_H