1 /**************************************************************************
2 * This file is property of and copyright by the ALICE HLT Project *
3 * All rights reserved. *
6 * Artur Szostak <artursz@iafrica.com> *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
19 /// \class AliMUONRawStreamTriggerHP
21 /// Implementation of a streamer interface to the high performance trigger decoder.
22 /// This is the raw stream class which interfaces between the high performance
23 /// core decoder for MUON trigger chambers and the AliRawReader class.
24 /// To gain the most out of the decoder, the Next() method should be used,
27 /// AliMUONRawStreamTriggerHP* rawStream; // assume initialised
28 /// const AliMUONRawStreamTriggerHP::AliLocalStruct* localStruct;
29 /// while ((localStruct = rawStream->Next()) != NULL)
31 /// // Do something with localStruct here.
35 /// This decoder tries to implement as similar an interface as possible to
36 /// AliMUONRawStreamTrigger where possible. However certain constructs which
37 /// would slow us down too much are avoided.
39 /// \author Artur Szostak <artursz@iafrica.com>
41 #include "AliMUONRawStreamTriggerHP.h"
42 #include "AliMUONDarcHeader.h"
43 #include "AliMUONRegHeader.h"
44 #include "AliMUONLocalStruct.h"
45 #include "AliMUONDDLTrigger.h"
46 #include "AliRawReader.h"
57 ClassImp(AliMUONRawStreamTriggerHP)
60 const Int_t AliMUONRawStreamTriggerHP::fgkMaxDDL = 2;
63 AliMUONRawStreamTriggerHP::AliMUONRawStreamTriggerHP() :
64 AliMUONVRawStreamTrigger(),
68 fBuffer(new UChar_t[8192]),
69 fCurrentLocalStruct(NULL),
75 /// Default constructor.
78 fDecoder.ExitOnError(false);
80 fDecoder.GetHandler().SetMaxStructs(
81 fDecoder.MaxRegionals(),
85 fDecoder.GetHandler().SetRawStream(this);
89 AliMUONRawStreamTriggerHP::AliMUONRawStreamTriggerHP(AliRawReader* rawReader) :
90 AliMUONVRawStreamTrigger(rawReader),
94 fBuffer(new UChar_t[8192]),
95 fCurrentLocalStruct(NULL),
101 /// Constructor with AliRawReader as argument.
104 fDecoder.ExitOnError(false);
106 fDecoder.GetHandler().SetMaxStructs(
107 fDecoder.MaxRegionals(),
111 fDecoder.GetHandler().SetRawStream(this);
115 AliMUONRawStreamTriggerHP::~AliMUONRawStreamTriggerHP()
118 /// Default destructor which cleans up the memory allocated.
125 if (fDDLObject != NULL)
132 void AliMUONRawStreamTriggerHP::First()
134 /// Initialise or reset the iterator.
135 /// The first DDL will be found and decoded.
137 assert( GetReader() != NULL );
145 Bool_t AliMUONRawStreamTriggerHP::NextDDL()
147 /// Read in the next trigger DDL and decode the payload with the
148 /// high performance decoder.
149 /// \return kTRUE if the next DDL was successfully read and kFALSE
152 assert( GetReader() != NULL );
154 // The temporary object if generated in GetDDLTracker, is now stale,
156 if (fDDLObject != NULL)
162 fCurrentLocalStruct = NULL;
164 while (fDDL < GetMaxDDL())
166 GetReader()->Reset();
167 GetReader()->Select("MUONTRG", fDDL, fDDL); // Select the DDL file to be read.
168 if (GetReader()->ReadHeader()) break;
169 AliDebug(3, Form("Skipping DDL %d which does not seem to be there", fDDL+1));
173 // If we reach the end of the DDL list for this event then reset the
174 // DDL counter, mark the iteration as done and exit.
175 if (fDDL >= GetMaxDDL())
186 AliDebug(3, Form("DDL Number %d\n", fDDL));
188 Int_t dataSize = GetReader()->GetDataSize(); // in bytes
189 // Check if we have enough buffer space already in fBuffer. If we do then
190 // just continue reading otherwise we need to resize the buffer.
191 if (fBufferSize < dataSize)
201 fBuffer = new UChar_t[dataSize];
202 fBufferSize = dataSize;
204 catch (const std::bad_alloc&)
206 AliError("Could not allocate more buffer space. Cannot decode DDL.");
211 if (not GetReader()->ReadNext(fBuffer, dataSize))
217 Swap(reinterpret_cast<UInt_t*>(fBuffer), dataSize / sizeof(UInt_t)); // Swap needed for mac power pc.
220 // Check if this is a scalar event.
221 bool scalerEvent = GetReader()->GetDataHeader()->GetL1TriggerMessage() == 0x1;
226 // Since we might allocate memory inside OnNewBuffer in the event
227 // handler we need to trap any memory allocation exception to be robust.
228 result = fDecoder.Decode(fBuffer, dataSize, scalerEvent);
229 fHadError = (result == true ? kFALSE : kTRUE);
231 catch (const std::bad_alloc&)
233 AliError("Could not allocate more buffer space. Cannot decode DDL.");
237 // Update the current local structure pointer.
238 fCurrentLocalStruct = fDecoder.GetHandler().FirstLocalStruct();
240 fDDL++; // Remember to increment index to next DDL.
245 Bool_t AliMUONRawStreamTriggerHP::IsDone() const
247 /// Indicates whether the iteration is finished or not.
248 /// \return kTRUE if we already read all the digits and kFALSE if not.
254 Bool_t AliMUONRawStreamTriggerHP::Next(
255 UChar_t& id, UChar_t& dec, Bool_t& trigY,
256 UChar_t& yPos, UChar_t& sXDev, UChar_t& xDev,
257 UChar_t& xPos, Bool_t& triggerY, Bool_t& triggerX,
258 TArrayS& xPattern, TArrayS& yPattern
261 const AliLocalStruct* localStruct = Next();
262 if (localStruct == NULL) return kFALSE;
264 id = localStruct->GetId();
265 dec = localStruct->GetDec();
266 trigY = localStruct->GetTrigY();
267 yPos = localStruct->GetYPos();
268 sXDev = localStruct->GetSXDev();
269 xDev = localStruct->GetXDev();
270 xPos = localStruct->GetXPos();
272 triggerX = localStruct->GetTriggerX();
273 triggerY = localStruct->GetTriggerY();
275 localStruct->GetXPattern(xPattern);
276 localStruct->GetYPattern(yPattern);
282 AliMUONDDLTrigger* AliMUONRawStreamTriggerHP::GetDDLTrigger() const
284 /// Construct and return a pointer to the DDL payload object.
285 /// \return Pointer to internally constructed AliMUONDDLTrigger object.
286 /// The object is owned by this class and should not be deleted
289 /// \note This method should not be used just to gain access to the DDL
290 /// payload, unless there is a good reason to have the AliMUONDDLTrigger
291 /// object. For example, if you want to modify the data and then save it
292 /// to another DDL stream. Otherwise it can be an order of magnitude
293 /// faster to access the DDL headers and data with the GetHeaders,
294 /// GetRegionalHeader and GetLocalStruct methods for example.
295 /// Refer to the MUONRawStreamTrigger.C macro to see how to use the fast
296 /// decoder interface optimally.
298 if (fDDLObject != NULL) return fDDLObject;
300 fDDLObject = new AliMUONDDLTrigger;
302 // Copy over all DARC, global headers and scalars.
303 AliMUONDarcHeader* darcHeader = fDDLObject->GetDarcHeader();
304 const AliHeader* hdr = GetHeaders();
305 UInt_t word = hdr->GetDarcHeader();
306 memcpy(darcHeader->GetHeader(), &word, sizeof(word));
307 if (hdr->GetDarcScalars() != NULL)
309 memcpy(darcHeader->GetDarcScalers(), hdr->GetDarcScalars(), sizeof(AliMUONDarcScalarsStruct));
311 memcpy(darcHeader->GetGlobalInput(), hdr->GetGlobalHeader(), sizeof(AliMUONGlobalHeaderStruct));
312 if (hdr->GetGlobalScalars() != NULL)
314 memcpy(darcHeader->GetGlobalScalers(), hdr->GetGlobalScalars(), sizeof(AliMUONGlobalScalarsStruct));
317 for (Int_t iReg = 0; iReg < (Int_t)GetRegionalHeaderCount(); iReg++)
319 AliMUONRegHeader regHeader;
320 AliMUONLocalStruct localStruct;
322 const AliRegionalHeader* rh = GetRegionalHeader(iReg);
323 // Copy local structure and scalars and add everything into DDL object.
324 memcpy(regHeader.GetHeader(), rh->GetHeader(), sizeof(AliMUONRegionalHeaderStruct));
325 if (rh->GetScalars() != NULL)
327 memcpy(regHeader.GetScalers(), rh->GetScalars(), sizeof(AliMUONRegionalScalarsStruct));
329 fDDLObject->AddRegHeader(regHeader);
331 const AliLocalStruct* lstruct = rh->GetFirstLocalStruct();
332 while (lstruct != NULL)
334 // Copy local structure and scalars and add everything into DDL object.
335 memcpy(localStruct.GetData(), lstruct->GetData(), sizeof(AliMUONLocalInfoStruct));
336 if (lstruct->GetScalars() != NULL)
338 memcpy(localStruct.GetScalers(), lstruct->GetScalars(), sizeof(AliMUONLocalScalarsStruct));
340 fDDLObject->AddLocStruct(localStruct, iReg);
341 lstruct = lstruct->Next();
349 void AliMUONRawStreamTriggerHP::SetMaxRegAllowed(Int_t reg)
351 /// Set the maximum allowed number of regional cards in the DDL.
353 fDecoder.MaxRegionals( (UInt_t) reg );
355 fDecoder.GetHandler().SetMaxStructs(
356 fDecoder.MaxRegionals(),
362 void AliMUONRawStreamTriggerHP::SetMaxLoc(Int_t loc)
364 /// Sets the maximum number of local cards in the DDL.
366 fDecoder.MaxLocals( (UInt_t) loc );
368 fDecoder.GetHandler().SetMaxStructs(
369 fDecoder.MaxRegionals(),
374 ///////////////////////////////////////////////////////////////////////////////
376 void AliMUONRawStreamTriggerHP::AliHeader::Print() const
378 /// Print DARC header, global header and global scalars to screen.
380 cout << "===== DARC info =====" << endl;
381 cout << "Header bits : 0x" << hex << fDarcHeader << dec << endl;
382 if (fDarcScalars != NULL)
384 cout << "L0R : " << fDarcScalars->fL0R << " (0x"
385 << hex << fDarcScalars->fL0R << dec << ")" << endl;
386 cout << "L1P : " << fDarcScalars->fL1P << " (0x"
387 << hex << fDarcScalars->fL1P << dec << ")" << endl;
388 cout << "L1S : " << fDarcScalars->fL1S << " (0x"
389 << hex << fDarcScalars->fL1S << dec << ")" << endl;
390 cout << "L2A : " << fDarcScalars->fL2A << " (0x"
391 << hex << fDarcScalars->fL2A << dec << ")" << endl;
392 cout << "L2R : " << fDarcScalars->fL2R << " (0x"
393 << hex << fDarcScalars->fL2R << dec << ")" << endl;
394 cout << "Clock : " << fDarcScalars->fClk << " (0x"
395 << hex << fDarcScalars->fClk << dec << ")" << endl;
396 cout << "Hold : " << fDarcScalars->fHold << " (0x"
397 << hex << fDarcScalars->fHold << dec << ")" << endl;
398 cout << "Spare : " << fDarcScalars->fSpare << " (0x"
399 << hex << fDarcScalars->fSpare << dec << ")" << endl;
403 cout << "Scalars == NULL" << endl;
406 cout << "===== Global info =====" << endl;
407 for (int i = 0; i < 4; i++)
409 cout << "Input[" << i << "] : " << fGlobalHeader->fInput[i] << " (0x"
410 << hex << fGlobalHeader->fInput[i] << dec << ")" << endl;
412 cout << "Output : " << fGlobalHeader->fOutput << " (0x"
413 << hex << fGlobalHeader->fOutput << dec << ")" << endl;
414 if (fGlobalScalars != NULL)
416 cout << "L0 : " << fGlobalScalars->fL0 << " (0x"
417 << hex << fGlobalScalars->fL0 << dec << ")" << endl;
418 cout << "Clock : " << fGlobalScalars->fClk << " (0x"
419 << hex << fGlobalScalars->fClk << dec << ")" << endl;
420 for (int j = 0; j < 4; j++)
422 cout << "Scaler[" << j << "] : " << fGlobalScalars->fScaler[j] << " (0x"
423 << hex << fGlobalScalars->fScaler[j] << dec << ")" << endl;
425 cout << "Hold : " << fGlobalScalars->fHold << " (0x"
426 << hex << fGlobalScalars->fHold << dec << ")" << endl;
427 cout << "Spare : " << fGlobalScalars->fSpare << " (0x"
428 << hex << fGlobalScalars->fSpare << dec << ")" << endl;
432 cout << "Scalars == NULL" << endl;
436 void AliMUONRawStreamTriggerHP::AliRegionalHeader::Print() const
438 /// Print the regional header and scalars to screen.
440 cout << "===== Regional card info =====" << endl;
441 cout << "DarcWord : " << fHeader->fDarcWord << " (0x"
442 << hex << fHeader->fDarcWord << dec << ")" << endl;
443 cout << "Word : " << fHeader->fWord << " (0x"
444 << hex << fHeader->fWord << dec << ")" << endl;
445 cout << "Input[0] : " << fHeader->fInput[0] << " (0x"
446 << hex << fHeader->fInput[0] << dec << ")" << endl;
447 cout << "Input[1] : " << fHeader->fInput[1] << " (0x"
448 << hex << fHeader->fInput[1] << dec << ")" << endl;
449 cout << "L0/Mask : " << fHeader->fL0CountAndMask << " (0x"
450 << hex << fHeader->fL0CountAndMask << dec << ")" << endl;
451 if (fScalars != NULL)
453 cout << "Clock : " << fScalars->fClk << " (0x"
454 << hex << fScalars->fClk << dec << ")" << endl;
455 for (int i = 0; i < 8; i++)
457 cout << "Scaler[" << i << "] : " << fScalars->fScaler[i] << " (0x"
458 << hex << fScalars->fScaler[i] << dec << ")" << endl;
460 cout << "Hold : " << fScalars->fHold << " (0x"
461 << hex << fScalars->fHold << dec << ")" << endl;
465 cout << "Scalars == NULL" << endl;
469 void AliMUONRawStreamTriggerHP::AliLocalStruct::Print() const
471 /// Print local trigger structure and scalars to screen.
473 cout << "===== Local card info =====" << endl;
474 cout << "X2X1 : " << fLocalStruct->fX2X1 << " (0x"
475 << hex << fLocalStruct->fX2X1 << dec << ")" << endl;
476 cout << "X4X3 : " << fLocalStruct->fX4X3 << " (0x"
477 << hex << fLocalStruct->fX4X3 << dec << ")" << endl;
478 cout << "Y2Y1 : " << fLocalStruct->fY2Y1 << " (0x"
479 << hex << fLocalStruct->fY2Y1 << dec << ")" << endl;
480 cout << "Y4Y3 : " << fLocalStruct->fY4Y3 << " (0x"
481 << hex << fLocalStruct->fY4Y3 << dec << ")" << endl;
482 cout << "Trigger bits : " << fLocalStruct->fTriggerBits << " (0x"
483 << hex << fLocalStruct->fTriggerBits << dec << ")" << endl;
484 if (fScalars != NULL)
486 cout << "L0 : " << fScalars->fL0 << " (0x"
487 << hex << fScalars->fL0 << dec << ")" << endl;
488 cout << "Hold : " << fScalars->fHold << " (0x"
489 << hex << fScalars->fHold << dec << ")" << endl;
490 cout << "Clock : " << fScalars->fClk << " (0x"
491 << hex << fScalars->fClk << dec << ")" << endl;
492 cout << "LPtNTrig : " << fScalars->fLPtNTrig << " (0x"
493 << hex << fScalars->fLPtNTrig << dec << ")" << endl;
494 cout << "HPtNTrig : " << fScalars->fHPtNTrig << " (0x"
495 << hex << fScalars->fHPtNTrig << dec << ")" << endl;
496 cout << "LPtRTrig : " << fScalars->fLPtRTrig << " (0x"
497 << hex << fScalars->fLPtRTrig << dec << ")" << endl;
498 cout << "HPtRTrig : " << fScalars->fHPtRTrig << " (0x"
499 << hex << fScalars->fHPtRTrig << dec << ")" << endl;
500 cout << "LPtLTrig : " << fScalars->fLPtLTrig << " (0x"
501 << hex << fScalars->fLPtLTrig << dec << ")" << endl;
502 cout << "HPtLTrig : " << fScalars->fHPtLTrig << " (0x"
503 << hex << fScalars->fHPtLTrig << dec << ")" << endl;
504 cout << "LPtSTrig : " << fScalars->fLPtSTrig << " (0x"
505 << hex << fScalars->fLPtSTrig << dec << ")" << endl;
506 cout << "HPtSTrig : " << fScalars->fHPtSTrig << " (0x"
507 << hex << fScalars->fHPtSTrig << dec << ")" << endl;
508 for (int i = 0; i < 8*4; i++)
510 cout << "Scaler[" << i << "] : " << fScalars->fScaler[i] << " (0x"
511 << hex << fScalars->fScaler[i] << dec << ")" << endl;
513 cout << "EOS : " << fScalars->fEOS << " (0x"
514 << hex << fScalars->fEOS << dec << ")" << endl;
515 cout << "Reset : " << fScalars->fReset << " (0x"
516 << hex << fScalars->fReset << dec << ")" << endl;
520 cout << "Scalars == NULL" << endl;
524 ///////////////////////////////////////////////////////////////////////////////
526 AliMUONRawStreamTriggerHP::AliDecoderEventHandler::AliDecoderEventHandler() :
536 fCurrentRegional(NULL),
544 /// Default constructor
548 AliMUONRawStreamTriggerHP::AliDecoderEventHandler::~AliDecoderEventHandler()
550 /// Default destructor cleans up the allocated memory.
552 if (fRegionals != NULL) delete [] fRegionals;
553 if (fLocals != NULL) delete [] fLocals;
557 void AliMUONRawStreamTriggerHP::AliDecoderEventHandler::SetMaxStructs(
558 UInt_t maxRegionals, UInt_t maxLocals
561 /// Sets the maximum number of structures allowed.
563 // Start by clearing the current arrays.
564 if (fRegionals != NULL)
566 delete [] fRegionals;
575 fCurrentRegional = NULL;
576 fCurrentLocal = NULL;
578 // Allocate new memory.
579 fRegionals = new AliRegionalHeader[maxRegionals];
580 fLocals = new AliLocalStruct[maxRegionals*maxLocals];
581 fEndOfLocals = fLocals;
585 void AliMUONRawStreamTriggerHP::AliDecoderEventHandler::OnNewBuffer(
586 const void* buffer, UInt_t /*bufferSize*/
589 /// This is called by the high performance decoder when a new DDL payload
590 /// is about to be decoded.
592 assert( fRawStream != NULL );
594 // remember the start of the buffer to be used in OnError.
595 fBufferStart = buffer;
597 // Reset error counters.
599 fGlobalEoWErrors = 0;
603 // Reset the current pointers which will be used to track where we need to
604 // fill fRegionals and fLocals. We have to subtract one space because we
605 // will increment the pointer the first time in the OnNewRegionalStruct
606 // and OnLocalStruct methods.
607 fCurrentRegional = fRegionals-1;
608 fCurrentLocal = fLocals-1;
613 void AliMUONRawStreamTriggerHP::AliDecoderEventHandler::OnError(
614 ErrorCode error, const void* location
617 /// This is called by the high performance decoder when a error occurs
618 /// when trying to decode the DDL payload. This indicates corruption in
619 /// the data. This method converts the error code to a descriptive message
620 /// and logs this with the raw reader.
621 /// \param error The error code indicating the problem.
622 /// \param location A pointer to the location within the DDL payload buffer
623 /// being decoded where the problem with the data was found.
625 assert( fRawStream != NULL );
626 assert( fRawStream->GetReader() != NULL );
628 Char_t* message = NULL;
633 case kWrongEventType:
634 message = Form("Wrong event type obtained from the Darc header, take the one of CDH");
640 "Wrong end of Darc word %x instead of %x\n",
641 *reinterpret_cast<const UInt_t*>(location),
642 AliMUONTriggerDDLDecoder<AliMUONTriggerDDLDecoderEventHandler>::EndOfDarcWord()
644 fRawStream->GetReader()->AddMajorErrorLog(kDarcEoWErr, message);
647 case kBadEndOfGlobal:
650 "Wrong end of Global word %x instead of %x\n",
651 *reinterpret_cast<const UInt_t*>(location),
652 AliMUONTriggerDDLDecoder<AliMUONTriggerDDLDecoderEventHandler>::EndOfGlobalWord()
654 fRawStream->GetReader()->AddMajorErrorLog(kGlobalEoWErr, message);
657 case kBadEndOfRegional:
660 "Wrong end of Regional word %x instead of %x\n",
661 *reinterpret_cast<const UInt_t*>(location),
662 AliMUONTriggerDDLDecoder<AliMUONTriggerDDLDecoderEventHandler>::EndOfRegionalWord()
664 fRawStream->GetReader()->AddMajorErrorLog(kRegEoWErr, message);
670 "Wrong end of Local word %x instead of %x\n",
671 *reinterpret_cast<const UInt_t*>(location),
672 AliMUONTriggerDDLDecoder<AliMUONTriggerDDLDecoderEventHandler>::EndOfLocalWord()
674 fRawStream->GetReader()->AddMajorErrorLog(kLocalEoWErr, message);
679 "%s (At byte %d in DDL.)",
680 ErrorCodeToMessage(error),
681 (unsigned long)location - (unsigned long)fBufferStart + sizeof(AliRawDataHeader)
683 fRawStream->GetReader()->AddMajorErrorLog(error, message);
690 "AliMUONRawStreamTriggerHP::AliDecoderEventHandler",