1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 //-----------------------------------------------------------------------------
19 /// \class AliMUONRawWriter
20 /// MUON Raw Data generaton in ALICE-MUON
21 /// Raw data structure could be found in Alice-note.
23 /// Implemented non-constant buspatch numbers for tracking
24 /// with correct DDL id (first guess)
25 /// (Ch. Finck, dec 2005)
28 /// Generates raw data for MUON tracker and finally for trigger
29 /// Using real mapping (inverse) for tracker
30 /// For trigger there is no mapping (mapping could be found in AliMUONTriggerCircuit)
31 /// Ch. Finck, July 04
32 /// Use memcpy instead of assignment elt by elt
33 /// Introducing variable DSP numbers, real manu numbers per buspatch for st12
34 /// Implemented scaler event for Trigger
35 /// Ch. Finck, Jan. 06
36 /// Using bus itr in DDL instead of simple incrementation
37 /// treat correctly the DDL & buspatch for station 3.
38 /// Using informations from AliMUONTriggerCrateStore for
39 /// empty slots and non-notified cards in trigger crates.
40 /// Ch. Finck, August 06.
41 /// Using AliMpDDLStore::GetBusPatchId.
43 /// \author Ch. Finck, Feb. 07.
44 //-----------------------------------------------------------------------------
47 #include "AliMUONRawWriter.h"
49 #include "AliMUONBlockHeader.h"
50 #include "AliMUONBusStruct.h"
51 #include "AliMUONConstants.h"
52 #include "AliMUONDarcHeader.h"
53 #include "AliMUONVDigit.h"
54 #include "AliMUONVDigitStore.h"
55 #include "AliMUONDspHeader.h"
56 #include "AliMUONGlobalTrigger.h"
57 #include "AliMUONLocalStruct.h"
58 #include "AliMUONLocalTrigger.h"
59 #include "AliMUONLocalTriggerBoard.h"
60 #include "AliMUONRegionalTrigger.h"
61 #include "AliMUONRegHeader.h"
63 #include "AliMUONVTriggerStore.h"
64 #include "AliCodeTimer.h"
66 #include "AliMpDDLStore.h"
68 #include "AliMpTriggerCrate.h"
69 #include "AliMpLocalBoard.h"
70 #include "AliMpDetElement.h"
71 #include "AliMpDEManager.h"
72 #include "AliMpExMap.h"
73 #include "AliMpConstants.h"
74 #include "AliMpPlaneType.h"
75 #include "AliMpSegmentation.h"
76 #include "AliMpStationType.h"
77 #include "AliMpVSegmentation.h"
79 #include "AliRawReader.h"
80 #include "AliBitPacking.h"
84 #include "TObjArray.h"
85 #include "TStopwatch.h"
86 #include <Riostream.h>
89 ClassImp(AliMUONRawWriter) // Class implementation in ROOT context
92 //__________________________________________________________________________
93 AliMUONRawWriter::AliMUONRawWriter()
95 fBlockHeader(new AliMUONBlockHeader()),
96 fDspHeader(new AliMUONDspHeader()),
97 fDarcHeader(new AliMUONDarcHeader()),
98 fRegHeader(new AliMUONRegHeader()),
99 fLocalStruct(new AliMUONLocalStruct()),
100 fDDLStore(AliMpDDLStore::Instance()),
101 fScalerEvent(kFALSE),
103 fBufferSize((((43*AliMpConstants::ManuNofChannels() + 4)*5 + 10)*5 + 8)*2),
104 fBuffer(new Int_t [fBufferSize])
106 /// Standard Constructor
108 AliDebug(1,"Standard ctor");
110 // setting data key to default value (only for writting)
111 fBlockHeader->SetDataKey(fBlockHeader->GetDefaultDataKey());
112 fDspHeader->SetDataKey(fDspHeader->GetDefaultDataKey());
116 //__________________________________________________________________________
117 AliMUONRawWriter::~AliMUONRawWriter(void)
131 //____________________________________________________________________
132 void AliMUONRawWriter::LocalWordPacking(UInt_t& word, UInt_t locId, UInt_t locDec,
133 UInt_t trigY, UInt_t posY, UInt_t posX,
134 UInt_t sdevX, UInt_t devX)
136 /// pack local trigger word
138 AliBitPacking::PackWord(locId,word,19,22); //card id number in crate
139 AliBitPacking::PackWord(locDec,word,15,18);
140 AliBitPacking::PackWord(trigY,word,14,14);
141 AliBitPacking::PackWord(posY,word,10,13);
142 AliBitPacking::PackWord(sdevX,word,9,9);
143 AliBitPacking::PackWord(devX,word,5,8);
144 AliBitPacking::PackWord(posX,word,0,4);
148 //____________________________________________________________________
149 Int_t AliMUONRawWriter::Digits2Raw(AliMUONVDigitStore* digitStore,
150 AliMUONVTriggerStore* triggerStore)
152 /// convert digits of the current event to raw data
163 AliCodeTimerAuto("for Tracker")
165 AliMpExMap busPatchMap(true);
167 Int_t nDDLs = AliDAQ::NumberOfDdls("MUONTRK");
169 Int_t nofBusPatches(0);
171 for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
173 AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
174 nofBusPatches += ddl->GetNofBusPatches();
177 busPatchMap.SetSize(nofBusPatches);
179 Digits2BusPatchMap(*digitStore,busPatchMap);
181 for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
183 WriteTrackerDDL(busPatchMap,iDDL);
185 AliDebug(1,"Tracker written");
190 AliCodeTimerAuto("for Trigger")
198 strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
199 file[0] = new AliFstream(name);
202 strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
203 file[1] = new AliFstream(name);
205 WriteTriggerDDL(*triggerStore,file);
211 AliDebug(1,"Trigger written");
217 //______________________________________________________________________________
219 AliMUONRawWriter::Digits2BusPatchMap(const AliMUONVDigitStore& digitStore,
220 AliMpExMap& busPatchMap)
222 /// Create bus patch structures corresponding to digits in the store
226 static const Int_t kMAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
228 // DDL event one per half chamber
233 UChar_t channelId = 0;
235 Int_t busPatchId = 0;
238 TIter next(digitStore.CreateTrackerIterator());
239 AliMUONVDigit* digit;
241 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
243 charge = digit->ADC();
244 if ( charge > kMAXADC )
246 // This is most probably an error in the digitizer (which should insure
247 // the adc is below kMAXADC), so make it a (non-fatal) error indeed.
248 AliError(Form("adc value %d above 0x%x for DE %d . Setting to 0x%x. Digit is:",
249 charge,kMAXADC,digit->DetElemId(),kMAXADC));
250 StdoutToAliError(digit->Print());
255 busPatchId = GetBusPatch(*digit);
257 if (busPatchId<0) continue;
259 if ( digit->ManuId() > 0x7FF ||
260 digit->ManuChannel() > 0x3F )
262 StdoutToAliError(digit->Print(););
263 AliFatal("ManuId,ManuChannel are invalid for the digit above.");
266 manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
267 channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
271 AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
272 AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
273 AliBitPacking::PackWord((UInt_t)charge,word,0,11);
277 for (Int_t i = 1; i <= 30; ++i)
279 parity ^= ((word >> i) & 0x1);
281 AliBitPacking::PackWord((UInt_t)parity,word,31,31);
283 AliMUONBusStruct* busStruct =
284 static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(busPatchId));
288 busStruct = new AliMUONBusStruct;
289 busStruct->SetDataKey(busStruct->GetDefaultDataKey());
290 busStruct->SetBusPatchId(busPatchId);
291 busStruct->SetLength(0);
292 busPatchMap.Add(busPatchId,busStruct);
296 busStruct->AddData(word);
300 //______________________________________________________________________________
302 AliMUONRawWriter::WriteTrackerDDL(AliMpExMap& busPatchMap, Int_t iDDL)
304 /// Write DDL file for one tracker DDL
306 // buffer size (max'ed out)
307 // (((43 manus max per bus patch *64 channels + 4 bus patch words) * 5 bus patch
308 // + 10 dsp words)*5 dsps + 8 block words)*2 blocks
312 memset(fBuffer,0,fBufferSize*sizeof(Int_t));
314 AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
315 Int_t iDspMax = ddl->GetMaxDsp();
316 Int_t iBusPerDSP[5]; //number of bus patches per DSP
317 ddl->GetBusPerDsp(iBusPerDSP);
320 Int_t totalDDLLength = 0;
324 // two blocks A and B per DDL
325 for (Int_t iBlock = 0; iBlock < 2; ++iBlock)
328 Int_t length = fBlockHeader->GetHeaderLength();
329 memcpy(&fBuffer[index],fBlockHeader->GetHeader(),length*4);
330 Int_t indexBlk = index;
333 // 5 DSP's max per block
334 for (Int_t iDsp = 0; iDsp < iDspMax; ++iDsp)
337 Int_t length = fDspHeader->GetHeaderLength();
338 memcpy(&fBuffer[index],fDspHeader->GetHeader(),length*4);
339 Int_t indexDsp = index;
342 // 5 buspatches max per DSP
343 for (Int_t i = 0; i < iBusPerDSP[iDsp]; ++i)
345 Int_t iBusPatch = ddl->GetBusPatchId(busIter++);
347 // iteration over bus patch in DDL
350 AliWarning(Form("Error in bus itr in DDL %d\n", iDDL));
354 AliMUONBusStruct* busStructPtr = static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(iBusPatch));
356 // check if buspatchid has digit
359 // add bus patch structure header
360 Int_t length = busStructPtr->GetHeaderLength();
361 memcpy(&fBuffer[index],busStructPtr->GetHeader(),length*4);
364 // add bus patch data
365 length = busStructPtr->GetLength();
366 memcpy(&fBuffer[index],busStructPtr->GetData(),length*4);
371 // writting anyhow buspatch structure (empty ones)
372 fBuffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
373 fBuffer[index++] = busStructPtr->GetHeaderLength(); // header length
374 fBuffer[index++] = 0; // raw data length
375 fBuffer[index++] = iBusPatch; // bus patch
379 // check if totalLength even
380 // set padding word in case
381 // Add one word 0xBEEFFACE at the end of DSP structure
382 Int_t totalDspLength = index - indexDsp;
383 if ((totalDspLength % 2) == 1)
385 fBuffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
386 fBuffer[index++] = fDspHeader->GetDefaultPaddingWord();
390 Int_t dspLength = totalDspLength - fDspHeader->GetHeaderLength();
392 fBuffer[indexDsp+1] = totalDspLength; // dsp total length
393 fBuffer[indexDsp+2] = dspLength; // data length
397 Int_t totalBlkLength = index - indexBlk;
398 Int_t blkLength = totalBlkLength - fBlockHeader->GetHeaderLength();
399 totalDDLLength += totalBlkLength;
401 fBuffer[indexBlk+1] = totalBlkLength; // total block length
402 fBuffer[indexBlk+2] = blkLength;
406 // writting onto disk
407 // total length in bytes
409 Int_t headerSize = sizeof(fHeader)/4;
411 fHeader.fSize = (totalDDLLength + headerSize) * 4;
413 AliFstream* file = new AliFstream(AliDAQ::DdlFileName("MUONTRK",iDDL));
415 file->WriteBuffer((char*)(&fHeader),headerSize*4);
416 file->WriteBuffer((char*)fBuffer,sizeof(int)*index);
420 //______________________________________________________________________________
421 Int_t AliMUONRawWriter::GetBusPatch(const AliMUONVDigit& digit) const
423 /// Determine the BusPatch this digit belongs to.
425 return fDDLStore->GetBusPatchId(digit.DetElemId(),digit.ManuId());
428 //______________________________________________________________________________
429 Int_t AliMUONRawWriter::WriteTriggerDDL(const AliMUONVTriggerStore& triggerStore, AliFstream* file[2])
431 /// Write trigger DDL
435 // DDL event one per half chamber
438 Int_t headerSize = sizeof(AliRawDataHeader)/4;
440 // global trigger for trigger pattern
441 AliMUONGlobalTrigger* gloTrg = triggerStore.Global();
447 Int_t gloTrigResp = gloTrg->GetGlobalResponse();
453 UChar_t locDec, trigY, posY, posX, regOut;
459 UInt_t version = 1; // software version
460 UInt_t eventPhys = 1; // trigger type: 1 for physics, 0 for software
461 UInt_t serialNb = 0xF; // serial nb of card: all bits on for the moment
462 Int_t globalFlag = 0; // set to 1 if global info present in DDL else set to 0
465 static const Int_t kDarcHeaderLength = fDarcHeader->GetDarcHeaderLength();
466 static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
467 static const Int_t kDarcScalerLength = fDarcHeader->GetDarcScalerLength();
468 static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
469 static const Int_t kRegHeaderLength = fRegHeader->GetHeaderLength();
470 static const Int_t kRegScalerLength = fRegHeader->GetScalerLength();
471 static const Int_t kLocHeaderLength = fLocalStruct->GetLength();
472 static const Int_t kLocScalerLength = fLocalStruct->GetScalerLength();
474 // [16(local)*6 words + 6 words]*8(reg) + 8 words = 824
475 static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) + (kRegHeaderLength+1))* 8
476 + kDarcHeaderLength + kGlobalHeaderLength + 2;
478 // [16(local)*51 words + 16 words]*8(reg) + 8 + 10 + 8 words scaler event 6682 words
479 static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength + kLocScalerLength +1) +
480 (kRegHeaderLength + kRegScalerLength +1))* 8 +
481 (kDarcHeaderLength + kDarcScalerLength +
482 kGlobalHeaderLength + kGlobalScalerLength + 2);
484 eventPhys = 0; //set to generate scaler events
487 buffer = new Int_t [kScalerBufferSize];
489 buffer = new Int_t [kBufferSize];
493 // open DDL file, on per 1/2 chamber
494 for ( Int_t iDDL = 0; iDDL < 2; ++iDDL )
498 if (iDDL == 0) // suppose global info in DDL one
504 // set darc status word
505 // see AliMUONDarcHeader.h for details
506 AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
507 AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
508 AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
509 AliBitPacking::PackWord((UInt_t)version,word,12,19);
510 fDarcHeader->SetWord(word);
512 memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4);
513 index += kDarcHeaderLength;
515 // no global input for the moment....
517 fDarcHeader->SetGlobalOutput(gloTrigResp);
519 fDarcHeader->SetGlobalOutput(0);
522 // 6 DARC scaler words
523 memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
524 index += kDarcScalerLength;
527 buffer[index++] = fDarcHeader->GetEndOfDarc();
529 // 4 words of global board input + Global board output
530 memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4);
531 index += kGlobalHeaderLength;
534 // 10 Global scaler words
535 memcpy(fDarcHeader->GetGlobalScalers(), &buffer[index], kGlobalScalerLength*4);
536 index += kGlobalScalerLength;
539 // end of global word
540 buffer[index++] = fDarcHeader->GetEndOfGlobal();
542 // 8 regional cards per DDL
543 for (Int_t iReg = 0; iReg < 8; ++iReg) {
546 AliMpTriggerCrate* crate = AliMpDDLStore::Instance()->GetTriggerCrate(iDDL, iReg);
549 AliWarning(Form("Missing crate number %d in DDL %d\n", iReg, iDDL));
551 // regional info tree, make sure that no reg card missing
552 AliMUONRegionalTrigger* regTrg = triggerStore.FindRegional(iReg+iDDL*8);
554 // Regional card header
557 // set darc status word
558 fRegHeader->SetDarcWord(word);
560 regOut = regTrg->GetOutput();
561 regInpHpt = regTrg->GetLocalOutput(0);
562 regInpLpt = regTrg->GetLocalOutput(1);
564 // fill darc word, not darc status for the moment (empty)
565 //see AliMUONRegHeader.h for details
566 AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31);
567 AliBitPacking::PackWord((UInt_t)serialNb,word,19,24);
568 AliBitPacking::PackWord((UInt_t)version,word,7,14);
569 AliBitPacking::PackWord((UInt_t)iReg,word,15,18);
570 AliBitPacking::PackWord((UInt_t)regOut,word,0,7);
571 fRegHeader->SetWord(word);
574 // fill header later, need local response
575 Int_t indexReg = index;
576 index += kRegHeaderLength;
578 // 11 regional scaler word
580 memcpy(&buffer[index], fRegHeader->GetScalers(), kRegScalerLength*4);
581 index += kRegScalerLength;
584 // end of regional word
585 buffer[index++] = fRegHeader->GetEndOfReg();
587 // 16 local card per regional board
588 // UShort_t localMask = 0x0;
590 for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
592 // slot zero for Regional card
593 Int_t localBoardId = crate->GetLocalBoardId(iLoc);
595 if (localBoardId) { // if not empty slot
596 AliMpLocalBoard* localBoard = AliMpDDLStore::Instance()->GetLocalBoard(localBoardId);
598 if (localBoard->IsNotified()) {// if notified board
599 AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(localBoardId);
601 locCard = locTrg->LoCircuit();
602 locDec = locTrg->GetLoDecision();
603 trigY = locTrg->LoTrigY();
604 posY = locTrg->LoStripY();
605 posX = locTrg->LoStripX();
606 devX = locTrg->LoDev();
607 sdevX = locTrg->LoSdev();
609 AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n",
610 locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
613 LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
614 (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
616 buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
617 buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
618 buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
619 buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
620 buffer[index++] = (Int_t)word; // data word
624 // fill copy card X-Y inputs from the notified cards
625 if (localBoard->GetInputXfrom() && localBoard->GetInputYfrom())
628 locDec = 0; trigY = 1; posY = 15;
629 posX = 0; devX = 0; sdevX = 1;
630 LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
631 (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
633 Int_t localFromId = localBoard->GetInputXfrom();
634 AliMUONLocalTrigger* locTrgfrom = triggerStore.FindLocal(localFromId);
636 buffer[index++] = 0; // copy only X3-4 & Y1-4
637 buffer[index++] = (locTrgfrom->GetX3Pattern() | (locTrgfrom->GetX4Pattern() << 16));
638 buffer[index++] = (locTrgfrom->GetY1Pattern() | (locTrgfrom->GetY2Pattern() << 16));
639 buffer[index++] = (locTrgfrom->GetY3Pattern() | (locTrgfrom->GetY4Pattern() << 16));
640 buffer[index++] = word;
644 // fill with 10CDEAD word for empty slots
645 for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
646 buffer[index++] = fLocalStruct->GetDisableWord();
647 }// condition localBoard
649 // 45 regional scaler word
651 memcpy(&buffer[index], fLocalStruct->GetScalers(), kLocScalerLength*4);
652 index += kLocScalerLength;
655 // end of local structure words
656 buffer[index++] = fLocalStruct->GetEndOfLocal();
659 // fill regional header with local output
660 fRegHeader->SetInput(regInpHpt, 0);
661 fRegHeader->SetInput(regInpHpt, 1);
662 memcpy(&buffer[indexReg],fRegHeader->GetHeader(),kRegHeaderLength*4);
667 // writting onto disk
669 fHeader.fSize = (index + headerSize) * 4;// total length in bytes
670 file[iDDL]->WriteBuffer((char*)(&fHeader),headerSize*4);
671 file[iDDL]->WriteBuffer((char*)buffer,sizeof(int)*index);
679 //____________________________________________________________________
680 void AliMUONRawWriter::SetScalersNumbers()
682 /// set numbers for scaler events for trigger headers
683 /// since this is provided by the experiment
684 /// put dummy numbers to check the monitoring
686 fDarcHeader->SetScalersNumbers();
687 fRegHeader->SetScalersNumbers();
688 fLocalStruct->SetScalersNumbers();
690 fScalerEvent = kTRUE;