#include "AliMUONRawWriter.h"
#include "AliMUONBlockHeader.h"
#include "AliMUONBusStruct.h"
#include "AliMUONConstants.h"
#include "AliMUONDarcHeader.h"
#include "AliMUONVDigit.h"
#include "AliMUONVDigitStore.h"
#include "AliMUONDspHeader.h"
#include "AliMUONGlobalTrigger.h"
#include "AliMUONLocalStruct.h"
#include "AliMUONLocalTrigger.h"
#include "AliMUONLocalTriggerBoard.h"
#include "AliMUONRegionalTrigger.h"
#include "AliMUONRegHeader.h"
#include "AliMUONVTriggerStore.h"
#include "AliCodeTimer.h"
#include "AliMpCDB.h"
#include "AliMpDDLStore.h"
#include "AliMpDDL.h"
#include "AliMpRegionalTrigger.h"
#include "AliMpTriggerCrate.h"
#include "AliMpLocalBoard.h"
#include "AliMpDetElement.h"
#include "AliMpDEManager.h"
#include "AliMpExMap.h"
#include "AliMpConstants.h"
#include "AliMpPlaneType.h"
#include "AliMpSegmentation.h"
#include "AliMpStationType.h"
#include "AliMpVSegmentation.h"
#include "AliRawReader.h"
#include "AliRawDataHeaderSim.h"
#include "AliBitPacking.h"
#include "AliDAQ.h"
#include "AliLog.h"
#include "TObjArray.h"
#include "TStopwatch.h"
#include <Riostream.h>
ClassImp(AliMUONRawWriter)
AliMUONRawWriter::AliMUONRawWriter()
: TObject(),
fBlockHeader(new AliMUONBlockHeader()),
fDspHeader(new AliMUONDspHeader()),
fDarcHeader(new AliMUONDarcHeader()),
fRegHeader(new AliMUONRegHeader()),
fLocalStruct(new AliMUONLocalStruct()),
fDDLStore(AliMpDDLStore::Instance()),
fScalerEvent(kFALSE),
fHeader(0x0),
fBufferSize((((43*AliMpConstants::ManuNofChannels() + 4)*5 + 10)*5 + 8)*2),
fBuffer(new Int_t [fBufferSize])
{
AliDebug(1,"Standard ctor");
fBlockHeader->SetDataKey(fBlockHeader->GetDefaultDataKey());
fDspHeader->SetDataKey(fDspHeader->GetDefaultDataKey());
if ( ! fDDLStore ) {
if ( ! AliMpCDB::LoadDDLStore() ) {
AliFatal("Could not access mapping from OCDB !");
}
fDDLStore = AliMpDDLStore::Instance();
}
}
AliMUONRawWriter::~AliMUONRawWriter(void)
{
AliDebug(1,"dtor");
delete fBlockHeader;
delete fDspHeader;
delete fDarcHeader;
delete fRegHeader;
delete fLocalStruct;
delete[] fBuffer;
}
void AliMUONRawWriter::LocalWordPacking(UInt_t& word, UInt_t locId, UInt_t locDec,
UInt_t trigY, UInt_t posY, UInt_t posX,
UInt_t sdevX, UInt_t devX)
{
AliBitPacking::PackWord(locId,word,19,22);
AliBitPacking::PackWord(locDec,word,15,18);
AliBitPacking::PackWord(trigY,word,14,14);
AliBitPacking::PackWord(posY,word,10,13);
AliBitPacking::PackWord(sdevX,word,9,9);
AliBitPacking::PackWord(devX,word,5,8);
AliBitPacking::PackWord(posX,word,0,4);
}
Int_t AliMUONRawWriter::Digits2Raw(const AliMUONVDigitStore* digitStore,
const AliMUONVTriggerStore* triggerStore)
{
AliCodeTimerAuto("",0)
Int_t idDDL;
if ( digitStore )
{
AliCodeTimerAuto("for Tracker",1)
AliMpExMap busPatchMap;
Int_t nDDLs = AliDAQ::NumberOfDdls("MUONTRK");
Int_t nofBusPatches(0);
for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
{
AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
nofBusPatches += ddl->GetNofBusPatches();
}
busPatchMap.SetSize(nofBusPatches);
Digits2BusPatchMap(*digitStore,busPatchMap);
for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
{
WriteTrackerDDL(busPatchMap,iDDL);
}
AliDebug(1,"Tracker written");
}
if ( triggerStore )
{
AliCodeTimerAuto("for Trigger",1)
AliFstream* file[2];
idDDL = 0;
file[0] = new AliFstream(AliDAQ::DdlFileName("MUONTRG",idDDL));
idDDL = 1;
file[1] = new AliFstream(AliDAQ::DdlFileName("MUONTRG",idDDL));
WriteTriggerDDL(*triggerStore,file);
delete file[0];
delete file[1];
AliDebug(1,"Trigger written");
}
return kTRUE;
}
void
AliMUONRawWriter::Digits2BusPatchMap(const AliMUONVDigitStore& digitStore,
AliMpExMap& busPatchMap)
{
AliCodeTimerAuto("",0)
static const Int_t kMAXADC = (1<<12)-1;
Char_t parity = 0x4;
UShort_t manuId = 0;
UChar_t channelId = 0;
UShort_t charge = 0;
Int_t busPatchId = 0;
Int_t currentBusPatchId = -1;
UInt_t word;
AliMUONBusStruct* busStruct(0x0);
TIter next(digitStore.CreateTrackerIterator());
AliMUONVDigit* digit;
while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
{
charge = digit->ADC();
if ( charge > kMAXADC )
{
AliError(Form("adc value %d above 0x%x for DE %d . Setting to 0x%x. Digit is:",
charge,kMAXADC,digit->DetElemId(),kMAXADC));
StdoutToAliError(digit->Print());
charge = kMAXADC;
}
busPatchId = GetBusPatch(*digit);
if (busPatchId<0) continue;
if ( digit->ManuId() > 0x7FF ||
digit->ManuChannel() > 0x3F )
{
StdoutToAliError(digit->Print(););
AliFatal("ManuId,ManuChannel are invalid for the digit above.");
}
manuId = ( digit->ManuId() & 0x7FF );
channelId = ( digit->ManuChannel() & 0x3F );
word = 0;
AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
AliBitPacking::PackWord((UInt_t)charge,word,0,11);
parity = word & 0x1;
for (Int_t i = 1; i <= 30; ++i)
{
parity ^= ((word >> i) & 0x1);
}
AliBitPacking::PackWord((UInt_t)parity,word,31,31);
if ( currentBusPatchId != busPatchId )
{
busStruct =
static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(busPatchId));
currentBusPatchId = busPatchId;
}
if (!busStruct)
{
busStruct = new AliMUONBusStruct;
busStruct->SetDataKey(busStruct->GetDefaultDataKey());
busStruct->SetBusPatchId(busPatchId);
busStruct->SetLength(0);
busPatchMap.Add(busPatchId,busStruct);
}
busStruct->AddData(word);
}
}
void
AliMUONRawWriter::WriteTrackerDDL(AliMpExMap& busPatchMap, Int_t iDDL)
{
AliCodeTimerAuto("",0)
if (fHeader == 0x0) {
AliError("Raw data header must be set");
return;
}
memset(fBuffer,0,fBufferSize*sizeof(Int_t));
AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
Int_t iDspMax = ddl->GetMaxDsp();
Int_t iBusPerDSP[5];
ddl->GetBusPerDsp(iBusPerDSP);
Int_t busIter = 0;
Int_t totalDDLLength = 0;
Int_t index = 0;
for (Int_t iBlock = 0; iBlock < 2; ++iBlock)
{
Int_t length = fBlockHeader->GetHeaderLength();
memcpy(&fBuffer[index],fBlockHeader->GetHeader(),length*4);
Int_t indexBlk = index;
index += length;
for (Int_t iDsp = 0; iDsp < iDspMax; ++iDsp)
{
Int_t dspHeaderLength = fDspHeader->GetHeaderLength();
memcpy(&fBuffer[index],fDspHeader->GetHeader(),dspHeaderLength*4);
Int_t indexDsp = index;
index += dspHeaderLength;
for (Int_t i = 0; i < iBusPerDSP[iDsp]; ++i)
{
Int_t iBusPatch = ddl->GetBusPatchId(busIter++);
if (iBusPatch == -1)
{
AliWarning(Form("Error in bus itr in DDL %d\n", iDDL));
continue;
}
AliMUONBusStruct* busStructPtr = static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(iBusPatch));
if (busStructPtr)
{
Int_t busHeaderLength = busStructPtr->GetHeaderLength();
memcpy(&fBuffer[index],busStructPtr->GetHeader(),busHeaderLength*4);
index += busHeaderLength;
Int_t busLength = busStructPtr->GetLength();
memcpy(&fBuffer[index],busStructPtr->GetData(),busLength*4);
index += busLength;
}
else
{
fBuffer[index++] = busStructPtr->GetDefaultDataKey();
fBuffer[index++] = busStructPtr->GetHeaderLength();
fBuffer[index++] = 0;
fBuffer[index++] = iBusPatch;
}
}
Int_t totalDspLength = index - indexDsp;
if ((totalDspLength % 2) == 1)
{
fBuffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
fBuffer[index++] = fDspHeader->GetDefaultPaddingWord();
totalDspLength++;
}
Int_t dspLength = totalDspLength - fDspHeader->GetHeaderLength();
fBuffer[indexDsp+1] = totalDspLength;
fBuffer[indexDsp+2] = dspLength;
}
Int_t totalBlkLength = index - indexBlk;
Int_t blkLength = totalBlkLength - fBlockHeader->GetHeaderLength();
totalDDLLength += totalBlkLength;
fBuffer[indexBlk+1] = totalBlkLength;
fBuffer[indexBlk+2] = blkLength;
}
fBuffer[index++] = fBlockHeader->GetDdlDataKey();
fBuffer[index++] = fBlockHeader->GetDdlDataKey();
totalDDLLength += 2;
Int_t headerSize = sizeof(AliRawDataHeaderV3)/4;
fHeader->fSize = (totalDDLLength + headerSize) * 4;
AliFstream* file = new AliFstream(AliDAQ::DdlFileName("MUONTRK",iDDL));
file->WriteBuffer((char*)fHeader,headerSize*4);
file->WriteBuffer((char*)fBuffer,sizeof(int)*index);
delete file;
}
Int_t AliMUONRawWriter::GetBusPatch(const AliMUONVDigit& digit) const
{
return fDDLStore->GetBusPatchId(digit.DetElemId(),digit.ManuId());
}
Int_t AliMUONRawWriter::WriteTriggerDDL(const AliMUONVTriggerStore& triggerStore, AliFstream* file[2])
{
AliCodeTimerAuto("",0)
if (fHeader == 0x0) {
AliError("Raw data header must be set");
return 0;
}
Int_t headerSize = sizeof(AliRawDataHeaderV3)/4;
AliMUONGlobalTrigger* gloTrg = triggerStore.Global();
if (!gloTrg)
{
return 0;
}
Int_t gloTrigResp = gloTrg->GetGlobalResponse();
UInt_t *gloTrigInput = gloTrg->GetGlobalInput();
UInt_t word;
Int_t* buffer = 0;
Int_t index;
UChar_t locDec, trigY, posY, posX, regOut;
UInt_t regInpLpt;
UInt_t regInpHpt;
UInt_t devX;
UChar_t sdevX;
UInt_t version = 1;
UInt_t eventPhys = 1;
UInt_t serialNb = 0xF;
Int_t globalFlag = 0;
static const Int_t kDarcHeaderLength = fDarcHeader->GetDarcHeaderLength();
static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
static const Int_t kDarcScalerLength = fDarcHeader->GetDarcScalerLength();
static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
static const Int_t kRegHeaderLength = fRegHeader->GetHeaderLength();
static const Int_t kRegScalerLength = fRegHeader->GetScalerLength();
static const Int_t kLocHeaderLength = fLocalStruct->GetLength();
static const Int_t kLocScalerLength = fLocalStruct->GetScalerLength();
static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) + (kRegHeaderLength+1))* 8
+ kDarcHeaderLength + kGlobalHeaderLength + 2;
static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength + kLocScalerLength +1) +
(kRegHeaderLength + kRegScalerLength +1))* 8 +
(kDarcHeaderLength + kDarcScalerLength +
kGlobalHeaderLength + kGlobalScalerLength + 2);
if(fScalerEvent) {
eventPhys = 0;
fHeader->fWord2 |= (0x1 << 14);
}
if(fScalerEvent)
buffer = new Int_t [kScalerBufferSize];
else
buffer = new Int_t [kBufferSize];
for ( Int_t iDDL = 0; iDDL < 2; ++iDDL )
{
index = 0;
if (iDDL == 0)
globalFlag = 1;
else
globalFlag = 0;
word = 0;
AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
AliBitPacking::PackWord((UInt_t)version,word,12,19);
fDarcHeader->SetWord(word);
memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4);
index += kDarcHeaderLength;
if (iDDL == 0) {
fDarcHeader->SetGlobalOutput(gloTrigResp);
for (Int_t ii = 0; ii < 4; ii++) {
fDarcHeader->SetGlobalInput(gloTrigInput[ii],ii);
}
} else {
fDarcHeader->SetGlobalOutput(0);
}
if (fScalerEvent) {
memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
index += kDarcScalerLength;
}
buffer[index++] = fDarcHeader->GetEndOfDarc();
memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4);
index += kGlobalHeaderLength;
if (fScalerEvent) {
memcpy(&buffer[index], fDarcHeader->GetGlobalScalers(), kGlobalScalerLength*4);
index += kGlobalScalerLength;
}
buffer[index++] = fDarcHeader->GetEndOfGlobal();
const AliMpRegionalTrigger* reg = AliMpDDLStore::Instance()->GetRegionalTrigger();
Int_t nCrate = reg->GetNofTriggerCrates()/2;
for (Int_t iReg = 0; iReg < nCrate; ++iReg) {
AliMpTriggerCrate* crate = AliMpDDLStore::Instance()->GetTriggerCrate(iDDL, iReg);
if (!crate) {
AliError(Form("Missing crate number %d in DDL %d\n", iReg, iDDL));
continue;
}
AliMUONRegionalTrigger* regTrg = triggerStore.FindRegional(crate->GetId());
if (!regTrg) {
AliError(Form("Missing regional board %d in trigger Store\n", crate->GetId()));
continue;
}
word = 0;
fRegHeader->SetDarcWord(word);
regOut = regTrg->GetOutput();
regInpLpt = regTrg->GetLocalOutput(0);
regInpHpt = regTrg->GetLocalOutput(1);
AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31);
AliBitPacking::PackWord((UInt_t)serialNb,word,20,25);
AliBitPacking::PackWord((UInt_t)version,word,8,15);
AliBitPacking::PackWord((UInt_t)crate->GetId(),word,16,19);
AliBitPacking::PackWord((UInt_t)regOut,word,0,7);
fRegHeader->SetWord(word);
Int_t indexReg = index;
index += kRegHeaderLength;
if (fScalerEvent) {
memcpy(&buffer[index], fRegHeader->GetScalers(), kRegScalerLength*4);
index += kRegScalerLength;
}
buffer[index++] = fRegHeader->GetEndOfReg();
Int_t nLocalBoard = AliMpConstants::LocalBoardNofChannels();
for (Int_t iLoc = 0; iLoc < nLocalBoard; iLoc++) {
Int_t localBoardId = crate->GetLocalBoardId(iLoc);
if (localBoardId) {
AliMpLocalBoard* localBoard = AliMpDDLStore::Instance()->GetLocalBoard(localBoardId);
if (localBoard->IsNotified()) {
AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(localBoardId);
if (locTrg)
{
locDec = locTrg->GetLoDecision();
trigY = locTrg->LoTrigY();
posY = locTrg->LoStripY();
posX = locTrg->LoStripX();
devX = locTrg->LoDev();
sdevX = locTrg->LoSdev();
AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n",
locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
word = 0;
LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
(UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
buffer[index++] = (Int_t)word;
}
}
if (localBoard->GetInputXfrom() && localBoard->GetInputYfrom())
{
locDec = 0; trigY = 1; posY = 15;
posX = 0; devX = 0; sdevX = 1;
LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
(UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
Int_t localFromId = localBoard->GetInputXfrom();
AliMUONLocalTrigger* locTrgfrom = triggerStore.FindLocal(localFromId);
if ( locTrgfrom )
{
buffer[index++] = 0;
buffer[index++] = (locTrgfrom->GetX3Pattern() | (locTrgfrom->GetX4Pattern() << 16));
buffer[index++] = (locTrgfrom->GetY1Pattern() | (locTrgfrom->GetY2Pattern() << 16));
buffer[index++] = (locTrgfrom->GetY3Pattern() | (locTrgfrom->GetY4Pattern() << 16));
buffer[index++] = word;
}
}
} else {
for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
buffer[index++] = fLocalStruct->GetDisableWord();
}
if (fScalerEvent) {
memcpy(&buffer[index], fLocalStruct->GetScalers(), kLocScalerLength*4);
index += kLocScalerLength;
}
buffer[index++] = fLocalStruct->GetEndOfLocal();
}
fRegHeader->SetInput(regInpLpt, 0);
fRegHeader->SetInput(regInpHpt, 1);
memcpy(&buffer[indexReg],fRegHeader->GetHeader(),kRegHeaderLength*4);
}
fHeader->fSize = (index + headerSize) * 4;
file[iDDL]->WriteBuffer((char*)fHeader,headerSize*4);
file[iDDL]->WriteBuffer((char*)buffer,sizeof(int)*index);
}
delete[] buffer;
return kTRUE;
}
void AliMUONRawWriter::SetScalersNumbers()
{
fDarcHeader->SetScalersNumbers();
fRegHeader->SetScalersNumbers();
fLocalStruct->SetScalersNumbers();
fScalerEvent = kTRUE;
}