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 /// \class AliMUONRawWriter
19 /// MUON Raw Data generaton in ALICE-MUON
20 /// Raw data structure could be found in Alice-note.
22 /// Implemented non-constant buspatch numbers for tracking
23 /// with correct DDL id (first guess)
24 /// (Ch. Finck, dec 2005)
27 /// Generates raw data for MUON tracker and finally for trigger
28 /// Using real mapping (inverse) for tracker
29 /// For trigger there is no mapping (mapping could be found in AliMUONTriggerCircuit)
30 /// Ch. Finck, July 04
31 /// Use memcpy instead of assignment elt by elt
32 /// Introducing variable DSP numbers, real manu numbers per buspatch for st12
33 /// Implemented scaler event for Trigger
34 /// Ch. Finck, Jan. 06
35 /// Using bus itr in DDL instead of simple incrementation
36 /// treat correctly the DDL & buspatch for station 3.
37 /// Using informations from AliMUONTriggerCrateStore for
38 /// empty slots and non-notified cards in trigger crates.
39 /// Ch. Finck, August 06.
40 /// Using AliMpDDLStore::GetBusPatchId.
42 /// \author Ch. Finck, Feb. 07.
45 #include "AliMUONRawWriter.h"
47 #include "AliMUONBlockHeader.h"
48 #include "AliMUONBusStruct.h"
49 #include "AliMUONConstants.h"
50 #include "AliMUONDarcHeader.h"
51 #include "AliMUONVDigit.h"
52 #include "AliMUONVDigitStore.h"
53 #include "AliMUONDspHeader.h"
54 #include "AliMUONGlobalTrigger.h"
55 #include "AliMUONLocalStruct.h"
56 #include "AliMUONLocalTrigger.h"
57 #include "AliMUONLocalTriggerBoard.h"
58 #include "AliMUONRegionalTrigger.h"
59 #include "AliMUONRegHeader.h"
61 #include "AliMUONVTriggerStore.h"
62 #include "AliCodeTimer.h"
64 #include "AliMpDDLStore.h"
66 #include "AliMpTriggerCrate.h"
67 #include "AliMpLocalBoard.h"
68 #include "AliMpDetElement.h"
69 #include "AliMpDEManager.h"
70 #include "AliMpExMap.h"
71 #include "AliMpConstants.h"
72 #include "AliMpPlaneType.h"
73 #include "AliMpSegmentation.h"
74 #include "AliMpStationType.h"
75 #include "AliMpVSegmentation.h"
77 #include "AliRawReader.h"
78 #include "AliBitPacking.h"
82 #include "TObjArray.h"
83 #include "TStopwatch.h"
84 #include <Riostream.h>
87 ClassImp(AliMUONRawWriter) // Class implementation in ROOT context
90 //__________________________________________________________________________
91 AliMUONRawWriter::AliMUONRawWriter()
93 fBlockHeader(new AliMUONBlockHeader()),
94 fDspHeader(new AliMUONDspHeader()),
95 fDarcHeader(new AliMUONDarcHeader()),
96 fRegHeader(new AliMUONRegHeader()),
97 fLocalStruct(new AliMUONLocalStruct()),
98 fDDLStore(AliMpDDLStore::Instance()),
101 fBufferSize((((43*AliMpConstants::ManuNofChannels() + 4)*5 + 10)*5 + 8)*2),
102 fBuffer(new Int_t [fBufferSize])
104 /// Standard Constructor
106 AliDebug(1,"Standard ctor");
108 // setting data key to default value (only for writting)
109 fBlockHeader->SetDataKey(fBlockHeader->GetDefaultDataKey());
110 fDspHeader->SetDataKey(fDspHeader->GetDefaultDataKey());
114 //__________________________________________________________________________
115 AliMUONRawWriter::~AliMUONRawWriter(void)
129 //____________________________________________________________________
130 void AliMUONRawWriter::LocalWordPacking(UInt_t& word, UInt_t locId, UInt_t locDec,
131 UInt_t trigY, UInt_t posY, UInt_t posX,
132 UInt_t sdevX, UInt_t devX)
134 /// pack local trigger word
136 AliBitPacking::PackWord(locId,word,19,22); //card id number in crate
137 AliBitPacking::PackWord(locDec,word,15,18);
138 AliBitPacking::PackWord(trigY,word,14,14);
139 AliBitPacking::PackWord(posY,word,10,13);
140 AliBitPacking::PackWord(sdevX,word,9,9);
141 AliBitPacking::PackWord(devX,word,5,8);
142 AliBitPacking::PackWord(posX,word,0,4);
146 //____________________________________________________________________
147 Int_t AliMUONRawWriter::Digits2Raw(AliMUONVDigitStore* digitStore,
148 AliMUONVTriggerStore* triggerStore)
150 /// convert digits of the current event to raw data
161 AliCodeTimerAuto("for Tracker")
163 AliMpExMap busPatchMap(true);
165 Int_t nDDLs = AliDAQ::NumberOfDdls("MUONTRK");
167 Int_t nofBusPatches(0);
169 for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
171 AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
172 nofBusPatches += ddl->GetNofBusPatches();
175 busPatchMap.SetSize(nofBusPatches);
177 Digits2BusPatchMap(*digitStore,busPatchMap);
179 for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
181 WriteTrackerDDL(busPatchMap,iDDL);
183 AliDebug(1,"Tracker written");
188 AliCodeTimerAuto("for Trigger")
196 strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
197 file[0] = new AliFstream(name);
200 strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
201 file[1] = new AliFstream(name);
203 WriteTriggerDDL(*triggerStore,file);
209 AliDebug(1,"Trigger written");
215 //______________________________________________________________________________
217 AliMUONRawWriter::Digits2BusPatchMap(const AliMUONVDigitStore& digitStore,
218 AliMpExMap& busPatchMap)
220 /// Create bus patch structures corresponding to digits in the store
224 static const Int_t kMAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
226 // DDL event one per half chamber
231 UChar_t channelId = 0;
233 Int_t busPatchId = 0;
236 TIter next(digitStore.CreateTrackerIterator());
237 AliMUONVDigit* digit;
239 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
241 charge = digit->ADC();
242 if ( charge > kMAXADC )
244 // This is most probably an error in the digitizer (which should insure
245 // the adc is below kMAXADC), so make it a (non-fatal) error indeed.
246 AliError(Form("adc value %d above %x for DE %d . Setting to %x. Digit is:",
247 digit->DetElemId(),charge,kMAXADC,kMAXADC));
248 StdoutToAliError(digit->Print());
253 busPatchId = GetBusPatch(*digit);
255 if (busPatchId<0) continue;
257 if ( digit->ManuId() > 0x7FF ||
258 digit->ManuChannel() > 0x3F )
260 StdoutToAliError(digit->Print(););
261 AliFatal("ManuId,ManuChannel are invalid for the digit above.");
264 manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
265 channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
269 AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
270 AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
271 AliBitPacking::PackWord((UInt_t)charge,word,0,11);
275 for (Int_t i = 1; i <= 30; ++i)
277 parity ^= ((word >> i) & 0x1);
279 AliBitPacking::PackWord((UInt_t)parity,word,31,31);
281 AliMUONBusStruct* busStruct =
282 static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(busPatchId));
286 busStruct = new AliMUONBusStruct;
287 busStruct->SetDataKey(busStruct->GetDefaultDataKey());
288 busStruct->SetBusPatchId(busPatchId);
289 busStruct->SetLength(0);
290 busPatchMap.Add(busPatchId,busStruct);
294 busStruct->AddData(word);
298 //______________________________________________________________________________
300 AliMUONRawWriter::WriteTrackerDDL(AliMpExMap& busPatchMap, Int_t iDDL)
302 /// Write DDL file for one tracker DDL
304 // buffer size (max'ed out)
305 // (((43 manus max per bus patch *64 channels + 4 bus patch words) * 5 bus patch
306 // + 10 dsp words)*5 dsps + 8 block words)*2 blocks
310 memset(fBuffer,0,fBufferSize*sizeof(Int_t));
312 AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
313 Int_t iDspMax = ddl->GetMaxDsp();
314 Int_t iBusPerDSP[5]; //number of bus patches per DSP
315 ddl->GetBusPerDsp(iBusPerDSP);
318 Int_t totalDDLLength = 0;
322 // two blocks A and B per DDL
323 for (Int_t iBlock = 0; iBlock < 2; ++iBlock)
326 Int_t length = fBlockHeader->GetHeaderLength();
327 memcpy(&fBuffer[index],fBlockHeader->GetHeader(),length*4);
328 Int_t indexBlk = index;
331 // 5 DSP's max per block
332 for (Int_t iDsp = 0; iDsp < iDspMax; ++iDsp)
335 Int_t length = fDspHeader->GetHeaderLength();
336 memcpy(&fBuffer[index],fDspHeader->GetHeader(),length*4);
337 Int_t indexDsp = index;
340 // 5 buspatches max per DSP
341 for (Int_t i = 0; i < iBusPerDSP[iDsp]; ++i)
343 Int_t iBusPatch = ddl->GetBusPatchId(busIter++);
345 // iteration over bus patch in DDL
348 AliWarning(Form("Error in bus itr in DDL %d\n", iDDL));
352 AliMUONBusStruct* busStructPtr = static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(iBusPatch));
354 // check if buspatchid has digit
357 // add bus patch structure header
358 Int_t length = busStructPtr->GetHeaderLength();
359 memcpy(&fBuffer[index],busStructPtr->GetHeader(),length*4);
362 // add bus patch data
363 length = busStructPtr->GetLength();
364 memcpy(&fBuffer[index],busStructPtr->GetData(),length*4);
369 // writting anyhow buspatch structure (empty ones)
370 fBuffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
371 fBuffer[index++] = busStructPtr->GetHeaderLength(); // header length
372 fBuffer[index++] = 0; // raw data length
373 fBuffer[index++] = iBusPatch; // bus patch
377 // check if totalLength even
378 // set padding word in case
379 // Add one word 0xBEEFFACE at the end of DSP structure
380 Int_t totalDspLength = index - indexDsp;
381 if ((totalDspLength % 2) == 1)
383 fBuffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
384 fBuffer[index++] = fDspHeader->GetDefaultPaddingWord();
388 Int_t dspLength = totalDspLength - fDspHeader->GetHeaderLength();
390 fBuffer[indexDsp+1] = totalDspLength; // dsp total length
391 fBuffer[indexDsp+2] = dspLength; // data length
395 Int_t totalBlkLength = index - indexBlk;
396 Int_t blkLength = totalBlkLength - fBlockHeader->GetHeaderLength();
397 totalDDLLength += totalBlkLength;
399 fBuffer[indexBlk+1] = totalBlkLength; // total block length
400 fBuffer[indexBlk+2] = blkLength;
404 // writting onto disk
405 // total length in bytes
407 Int_t headerSize = sizeof(fHeader)/4;
409 fHeader.fSize = (totalDDLLength + headerSize) * 4;
411 AliFstream* file = new AliFstream(AliDAQ::DdlFileName("MUONTRK",iDDL));
413 file->WriteBuffer((char*)(&fHeader),headerSize*4);
414 file->WriteBuffer((char*)fBuffer,sizeof(int)*index);
418 //______________________________________________________________________________
419 Int_t AliMUONRawWriter::GetBusPatch(const AliMUONVDigit& digit) const
421 /// Determine the BusPatch this digit belongs to.
423 return fDDLStore->GetBusPatchId(digit.DetElemId(),digit.ManuId());
426 //______________________________________________________________________________
427 Int_t AliMUONRawWriter::WriteTriggerDDL(const AliMUONVTriggerStore& triggerStore, AliFstream* file[2])
429 /// Write trigger DDL
433 // DDL event one per half chamber
436 Int_t headerSize = sizeof(AliRawDataHeader)/4;
438 // global trigger for trigger pattern
439 AliMUONGlobalTrigger* gloTrg = triggerStore.Global();
445 Int_t gloTrigResp = gloTrg->GetGlobalResponse();
451 UChar_t locDec, trigY, posY, posX, regOut;
457 UInt_t version = 1; // software version
458 UInt_t eventPhys = 1; // trigger type: 1 for physics, 0 for software
459 UInt_t serialNb = 0xF; // serial nb of card: all bits on for the moment
460 Int_t globalFlag = 0; // set to 1 if global info present in DDL else set to 0
463 static const Int_t kDarcHeaderLength = fDarcHeader->GetDarcHeaderLength();
464 static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
465 static const Int_t kDarcScalerLength = fDarcHeader->GetDarcScalerLength();
466 static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
467 static const Int_t kRegHeaderLength = fRegHeader->GetHeaderLength();
468 static const Int_t kRegScalerLength = fRegHeader->GetScalerLength();
469 static const Int_t kLocHeaderLength = fLocalStruct->GetLength();
470 static const Int_t kLocScalerLength = fLocalStruct->GetScalerLength();
472 // [16(local)*6 words + 6 words]*8(reg) + 8 words = 824
473 static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) + (kRegHeaderLength+1))* 8
474 + kDarcHeaderLength + kGlobalHeaderLength + 2;
476 // [16(local)*51 words + 16 words]*8(reg) + 8 + 10 + 8 words scaler event 6682 words
477 static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength + kLocScalerLength +1) +
478 (kRegHeaderLength + kRegScalerLength +1))* 8 +
479 (kDarcHeaderLength + kDarcScalerLength +
480 kGlobalHeaderLength + kGlobalScalerLength + 2);
482 eventPhys = 0; //set to generate scaler events
485 buffer = new Int_t [kScalerBufferSize];
487 buffer = new Int_t [kBufferSize];
491 // open DDL file, on per 1/2 chamber
492 for ( Int_t iDDL = 0; iDDL < 2; ++iDDL )
496 if (iDDL == 0) // suppose global info in DDL one
502 // set darc status word
503 // see AliMUONDarcHeader.h for details
504 AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
505 AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
506 AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
507 AliBitPacking::PackWord((UInt_t)version,word,12,19);
508 fDarcHeader->SetWord(word);
510 memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4);
511 index += kDarcHeaderLength;
513 // no global input for the moment....
515 fDarcHeader->SetGlobalOutput(gloTrigResp);
517 fDarcHeader->SetGlobalOutput(0);
520 // 6 DARC scaler words
521 memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
522 index += kDarcScalerLength;
525 buffer[index++] = fDarcHeader->GetEndOfDarc();
527 // 4 words of global board input + Global board output
528 memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4);
529 index += kGlobalHeaderLength;
532 // 10 Global scaler words
533 memcpy(fDarcHeader->GetGlobalScalers(), &buffer[index], kGlobalScalerLength*4);
534 index += kGlobalScalerLength;
537 // end of global word
538 buffer[index++] = fDarcHeader->GetEndOfGlobal();
540 // 8 regional cards per DDL
541 for (Int_t iReg = 0; iReg < 8; ++iReg) {
544 AliMpTriggerCrate* crate = AliMpDDLStore::Instance()->GetTriggerCrate(iDDL, iReg);
547 AliWarning(Form("Missing crate number %d in DDL %d\n", iReg, iDDL));
549 // regional info tree, make sure that no reg card missing
550 AliMUONRegionalTrigger* regTrg = triggerStore.FindRegional(iReg+iDDL*8);
552 // Regional card header
555 // set darc status word
556 fRegHeader->SetDarcWord(word);
558 regOut = regTrg->GetOutput();
559 regInpHpt = regTrg->GetLocalOutput(0);
560 regInpLpt = regTrg->GetLocalOutput(1);
562 // fill darc word, not darc status for the moment (empty)
563 //see AliMUONRegHeader.h for details
564 AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31);
565 AliBitPacking::PackWord((UInt_t)serialNb,word,19,24);
566 AliBitPacking::PackWord((UInt_t)version,word,7,14);
567 AliBitPacking::PackWord((UInt_t)iReg,word,15,18);
568 AliBitPacking::PackWord((UInt_t)regOut,word,0,7);
569 fRegHeader->SetWord(word);
572 // fill header later, need local response
573 Int_t indexReg = index;
574 index += kRegHeaderLength;
576 // 11 regional scaler word
578 memcpy(&buffer[index], fRegHeader->GetScalers(), kRegScalerLength*4);
579 index += kRegScalerLength;
582 // end of regional word
583 buffer[index++] = fRegHeader->GetEndOfReg();
585 // 16 local card per regional board
586 // UShort_t localMask = 0x0;
588 for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
590 // slot zero for Regional card
591 Int_t localBoardId = crate->GetLocalBoardId(iLoc);
593 if (localBoardId) { // if not empty slot
594 AliMpLocalBoard* localBoard = AliMpDDLStore::Instance()->GetLocalBoard(localBoardId);
596 if (localBoard->IsNotified()) {// if notified board
597 AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(localBoardId);
599 locCard = locTrg->LoCircuit();
600 locDec = locTrg->GetLoDecision();
601 trigY = locTrg->LoTrigY();
602 posY = locTrg->LoStripY();
603 posX = locTrg->LoStripX();
604 devX = locTrg->LoDev();
605 sdevX = locTrg->LoSdev();
607 AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n",
608 locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
611 LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
612 (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
614 buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
615 buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
616 buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
617 buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
618 buffer[index++] = (Int_t)word; // data word
622 // fill copy card X-Y inputs from the notified cards
623 if (localBoard->GetInputXfrom() && localBoard->GetInputYfrom())
626 locDec = 0; trigY = 1; posY = 15;
627 posX = 0; devX = 0; sdevX = 1;
628 LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
629 (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
631 Int_t localFromId = localBoard->GetInputXfrom();
632 AliMUONLocalTrigger* locTrgfrom = triggerStore.FindLocal(localFromId);
634 buffer[index++] = 0; // copy only X3-4 & Y1-4
635 buffer[index++] = (locTrgfrom->GetX3Pattern() | (locTrgfrom->GetX4Pattern() << 16));
636 buffer[index++] = (locTrgfrom->GetY1Pattern() | (locTrgfrom->GetY2Pattern() << 16));
637 buffer[index++] = (locTrgfrom->GetY3Pattern() | (locTrgfrom->GetY4Pattern() << 16));
638 buffer[index++] = word;
642 // fill with 10CDEAD word for empty slots
643 for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
644 buffer[index++] = fLocalStruct->GetDisableWord();
645 }// condition localBoard
647 // 45 regional scaler word
649 memcpy(&buffer[index], fLocalStruct->GetScalers(), kLocScalerLength*4);
650 index += kLocScalerLength;
653 // end of local structure words
654 buffer[index++] = fLocalStruct->GetEndOfLocal();
657 // fill regional header with local output
658 fRegHeader->SetInput(regInpHpt, 0);
659 fRegHeader->SetInput(regInpHpt, 1);
660 memcpy(&buffer[indexReg],fRegHeader->GetHeader(),kRegHeaderLength*4);
665 // writting onto disk
667 fHeader.fSize = (index + headerSize) * 4;// total length in bytes
668 file[iDDL]->WriteBuffer((char*)(&fHeader),headerSize*4);
669 file[iDDL]->WriteBuffer((char*)buffer,sizeof(int)*index);
677 //____________________________________________________________________
678 void AliMUONRawWriter::SetScalersNumbers()
680 /// set numbers for scaler events for trigger headers
681 /// since this is provided by the experiment
682 /// put dummy numbers to check the monitoring
684 fDarcHeader->SetScalersNumbers();
685 fRegHeader->SetScalersNumbers();
686 fLocalStruct->SetScalersNumbers();
688 fScalerEvent = kTRUE;