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 "AliMUONStopwatchGroup.h"
63 #include "AliMUONStopwatchGroupElement.h"
65 #include "AliMpDDLStore.h"
67 #include "AliMpTriggerCrate.h"
68 #include "AliMpLocalBoard.h"
69 #include "AliMpDetElement.h"
70 #include "AliMpDEManager.h"
71 #include "AliMpExMap.h"
72 #include "AliMpConstants.h"
73 #include "AliMpPlaneType.h"
74 #include "AliMpSegmentation.h"
75 #include "AliMpStationType.h"
76 #include "AliMpVSegmentation.h"
78 #include "AliRawReader.h"
79 #include "AliBitPacking.h"
83 #include "TObjArray.h"
84 #include "TStopwatch.h"
85 #include <Riostream.h>
88 ClassImp(AliMUONRawWriter) // Class implementation in ROOT context
91 //__________________________________________________________________________
92 AliMUONRawWriter::AliMUONRawWriter()
94 fBlockHeader(new AliMUONBlockHeader()),
95 fDspHeader(new AliMUONDspHeader()),
96 fDarcHeader(new AliMUONDarcHeader()),
97 fRegHeader(new AliMUONRegHeader()),
98 fLocalStruct(new AliMUONLocalStruct()),
99 fDDLStore(AliMpDDLStore::Instance()),
100 fScalerEvent(kFALSE),
102 fTimers(new AliMUONStopwatchGroup),
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)
134 //____________________________________________________________________
135 void AliMUONRawWriter::LocalWordPacking(UInt_t& word, UInt_t locId, UInt_t locDec,
136 UInt_t trigY, UInt_t posY, UInt_t posX,
137 UInt_t sdevX, UInt_t devX)
139 /// pack local trigger word
141 AliBitPacking::PackWord(locId,word,19,22); //card id number in crate
142 AliBitPacking::PackWord(locDec,word,15,18);
143 AliBitPacking::PackWord(trigY,word,14,14);
144 AliBitPacking::PackWord(posY,word,10,13);
145 AliBitPacking::PackWord(sdevX,word,9,9);
146 AliBitPacking::PackWord(devX,word,5,8);
147 AliBitPacking::PackWord(posX,word,0,4);
151 //____________________________________________________________________
152 Int_t AliMUONRawWriter::Digits2Raw(AliMUONVDigitStore* digitStore,
153 AliMUONVTriggerStore* triggerStore)
155 /// convert digits of the current event to raw data
157 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONRawWriter::Digits2Raw");
166 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONRawWriter::Digits2Raw for Tracker");
168 AliMpExMap busPatchMap(true);
170 Int_t nDDLs = AliDAQ::NumberOfDdls("MUONTRK");
172 Int_t nofBusPatches(0);
174 for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
176 AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
177 nofBusPatches += ddl->GetNofBusPatches();
180 busPatchMap.SetSize(nofBusPatches);
182 Digits2BusPatchMap(*digitStore,busPatchMap);
184 for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
186 WriteTrackerDDL(busPatchMap,iDDL);
188 AliDebug(1,"Tracker written");
193 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONRawWriter::Digits2Raw for Trigger");
201 strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
202 file[0] = fopen(name,"w");
205 strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
206 file[1] = fopen(name,"w");
208 WriteTriggerDDL(*triggerStore,file);
214 AliDebug(1,"Trigger written");
220 //______________________________________________________________________________
222 AliMUONRawWriter::Digits2BusPatchMap(const AliMUONVDigitStore& digitStore,
223 AliMpExMap& busPatchMap)
225 /// Create bus patch structures corresponding to digits in the store
227 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONRawWriter::Digits2BusPatchMap");
229 static const Int_t kMAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
231 // DDL event one per half chamber
236 UChar_t channelId = 0;
238 Int_t busPatchId = 0;
241 TIter next(digitStore.CreateTrackerIterator());
242 AliMUONVDigit* digit;
244 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
246 charge = digit->ADC();
247 if ( charge > kMAXADC )
249 // This is most probably an error in the digitizer (which should insure
250 // the adc is below kMAXADC), so make it a (non-fatal) error indeed.
251 AliError(Form("adc value %d above %x for DE %d . Setting to %x. Digit is:",
252 digit->DetElemId(),charge,kMAXADC,kMAXADC));
253 StdoutToAliError(digit->Print());
258 busPatchId = GetBusPatch(*digit);
260 if (busPatchId<0) continue;
262 if ( digit->ManuId() > 0x7FF ||
263 digit->ManuChannel() > 0x3F )
265 StdoutToAliError(digit->Print(););
266 AliFatal("ManuId,ManuChannel are invalid for the digit above.");
269 manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
270 channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
274 AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
275 AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
276 AliBitPacking::PackWord((UInt_t)charge,word,0,11);
280 for (Int_t i = 1; i <= 30; ++i)
282 parity ^= ((word >> i) & 0x1);
284 AliBitPacking::PackWord((UInt_t)parity,word,31,31);
286 AliMUONBusStruct* busStruct =
287 static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(busPatchId));
291 busStruct = new AliMUONBusStruct;
292 busStruct->SetDataKey(busStruct->GetDefaultDataKey());
293 busStruct->SetBusPatchId(busPatchId);
294 busStruct->SetLength(0);
295 busPatchMap.Add(busPatchId,busStruct);
299 busStruct->AddData(word);
303 //______________________________________________________________________________
305 AliMUONRawWriter::WriteTrackerDDL(AliMpExMap& busPatchMap, Int_t iDDL)
307 /// Write DDL file for one tracker DDL
309 // buffer size (max'ed out)
310 // (((43 manus max per bus patch *64 channels + 4 bus patch words) * 5 bus patch
311 // + 10 dsp words)*5 dsps + 8 block words)*2 blocks
313 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONRawWriter::WriteTrackerDDL");
315 memset(fBuffer,0,fBufferSize*sizeof(Int_t));
317 AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
318 Int_t iDspMax = ddl->GetMaxDsp();
319 Int_t iBusPerDSP[5]; //number of bus patches per DSP
320 ddl->GetBusPerDsp(iBusPerDSP);
323 Int_t totalDDLLength = 0;
327 // two blocks A and B per DDL
328 for (Int_t iBlock = 0; iBlock < 2; ++iBlock)
331 Int_t length = fBlockHeader->GetHeaderLength();
332 memcpy(&fBuffer[index],fBlockHeader->GetHeader(),length*4);
333 Int_t indexBlk = index;
336 // 5 DSP's max per block
337 for (Int_t iDsp = 0; iDsp < iDspMax; ++iDsp)
340 Int_t length = fDspHeader->GetHeaderLength();
341 memcpy(&fBuffer[index],fDspHeader->GetHeader(),length*4);
342 Int_t indexDsp = index;
345 // 5 buspatches max per DSP
346 for (Int_t i = 0; i < iBusPerDSP[iDsp]; ++i)
348 Int_t iBusPatch = ddl->GetBusPatchId(busIter++);
350 // iteration over bus patch in DDL
353 AliWarning(Form("Error in bus itr in DDL %d\n", iDDL));
357 AliMUONBusStruct* busStructPtr = static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(iBusPatch));
359 // check if buspatchid has digit
362 // add bus patch structure header
363 Int_t length = busStructPtr->GetHeaderLength();
364 memcpy(&fBuffer[index],busStructPtr->GetHeader(),length*4);
367 // add bus patch data
368 length = busStructPtr->GetLength();
369 memcpy(&fBuffer[index],busStructPtr->GetData(),length*4);
374 // writting anyhow buspatch structure (empty ones)
375 fBuffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
376 fBuffer[index++] = busStructPtr->GetHeaderLength(); // header length
377 fBuffer[index++] = 0; // raw data length
378 fBuffer[index++] = iBusPatch; // bus patch
382 // check if totalLength even
383 // set padding word in case
384 // Add one word 0xBEEFFACE at the end of DSP structure
385 Int_t totalDspLength = index - indexDsp;
386 if ((totalDspLength % 2) == 1)
388 fBuffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
389 fBuffer[index++] = fDspHeader->GetDefaultPaddingWord();
393 Int_t dspLength = totalDspLength - fDspHeader->GetHeaderLength();
395 fBuffer[indexDsp+1] = totalDspLength; // dsp total length
396 fBuffer[indexDsp+2] = dspLength; // data length
400 Int_t totalBlkLength = index - indexBlk;
401 Int_t blkLength = totalBlkLength - fBlockHeader->GetHeaderLength();
402 totalDDLLength += totalBlkLength;
404 fBuffer[indexBlk+1] = totalBlkLength; // total block length
405 fBuffer[indexBlk+2] = blkLength;
409 // writting onto disk
410 // total length in bytes
412 Int_t headerSize = sizeof(fHeader)/4;
414 fHeader.fSize = (totalDDLLength + headerSize) * 4;
416 FILE* file = fopen(AliDAQ::DdlFileName("MUONTRK",iDDL),"w");
418 fwrite((char*)(&fHeader),headerSize*4,1,file);
419 fwrite(fBuffer,sizeof(int),index,file);
423 //______________________________________________________________________________
424 Int_t AliMUONRawWriter::GetBusPatch(const AliMUONVDigit& digit) const
426 /// Determine the BusPatch this digit belongs to.
428 return fDDLStore->GetBusPatchId(digit.DetElemId(),digit.ManuId());
431 //______________________________________________________________________________
432 Int_t AliMUONRawWriter::WriteTriggerDDL(const AliMUONVTriggerStore& triggerStore, FILE* file[2])
434 /// Write trigger DDL
436 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONRawWriter::WriteTriggerDDL");
438 // DDL event one per half chamber
441 Int_t headerSize = sizeof(AliRawDataHeader)/4;
443 // global trigger for trigger pattern
444 AliMUONGlobalTrigger* gloTrg = triggerStore.Global();
450 Int_t gloTrigResp = gloTrg->GetGlobalResponse();
456 UChar_t locDec, trigY, posY, posX, regOut;
462 UInt_t version = 1; // software version
463 UInt_t eventPhys = 1; // trigger type: 1 for physics, 0 for software
464 UInt_t serialNb = 0xF; // serial nb of card: all bits on for the moment
465 Int_t globalFlag = 0; // set to 1 if global info present in DDL else set to 0
468 static const Int_t kDarcHeaderLength = fDarcHeader->GetDarcHeaderLength();
469 static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
470 static const Int_t kDarcScalerLength = fDarcHeader->GetDarcScalerLength();
471 static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
472 static const Int_t kRegHeaderLength = fRegHeader->GetHeaderLength();
473 static const Int_t kRegScalerLength = fRegHeader->GetScalerLength();
474 static const Int_t kLocHeaderLength = fLocalStruct->GetLength();
475 static const Int_t kLocScalerLength = fLocalStruct->GetScalerLength();
477 // [16(local)*6 words + 6 words]*8(reg) + 8 words = 824
478 static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) + (kRegHeaderLength+1))* 8
479 + kDarcHeaderLength + kGlobalHeaderLength + 2;
481 // [16(local)*51 words + 16 words]*8(reg) + 8 + 10 + 8 words scaler event 6682 words
482 static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength + kLocScalerLength +1) +
483 (kRegHeaderLength + kRegScalerLength +1))* 8 +
484 (kDarcHeaderLength + kDarcScalerLength +
485 kGlobalHeaderLength + kGlobalScalerLength + 2);
487 eventPhys = 0; //set to generate scaler events
490 buffer = new Int_t [kScalerBufferSize];
492 buffer = new Int_t [kBufferSize];
496 // open DDL file, on per 1/2 chamber
497 for ( Int_t iDDL = 0; iDDL < 2; ++iDDL )
501 if (iDDL == 0) // suppose global info in DDL one
507 // set darc status word
508 // see AliMUONDarcHeader.h for details
509 AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
510 AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
511 AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
512 AliBitPacking::PackWord((UInt_t)version,word,12,19);
513 fDarcHeader->SetWord(word);
515 memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4);
516 index += kDarcHeaderLength;
518 // no global input for the moment....
520 fDarcHeader->SetGlobalOutput(gloTrigResp);
522 fDarcHeader->SetGlobalOutput(0);
525 // 6 DARC scaler words
526 memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
527 index += kDarcScalerLength;
530 buffer[index++] = fDarcHeader->GetEndOfDarc();
532 // 4 words of global board input + Global board output
533 memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4);
534 index += kGlobalHeaderLength;
537 // 10 Global scaler words
538 memcpy(fDarcHeader->GetGlobalScalers(), &buffer[index], kGlobalScalerLength*4);
539 index += kGlobalScalerLength;
542 // end of global word
543 buffer[index++] = fDarcHeader->GetEndOfGlobal();
545 // 8 regional cards per DDL
546 for (Int_t iReg = 0; iReg < 8; ++iReg) {
549 AliMpTriggerCrate* crate = AliMpDDLStore::Instance()->GetTriggerCrate(iDDL, iReg);
552 AliWarning(Form("Missing crate number %d in DDL %d\n", iReg, iDDL));
554 // regional info tree, make sure that no reg card missing
555 AliMUONRegionalTrigger* regTrg = triggerStore.FindRegional(iReg+iDDL*8);
557 // Regional card header
560 // set darc status word
561 fRegHeader->SetDarcWord(word);
563 regOut = regTrg->GetOutput();
564 regInpHpt = regTrg->GetLocalOutput(0);
565 regInpLpt = regTrg->GetLocalOutput(1);
567 // fill darc word, not darc status for the moment (empty)
568 //see AliMUONRegHeader.h for details
569 AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31);
570 AliBitPacking::PackWord((UInt_t)serialNb,word,19,24);
571 AliBitPacking::PackWord((UInt_t)version,word,7,14);
572 AliBitPacking::PackWord((UInt_t)iReg,word,15,18);
573 AliBitPacking::PackWord((UInt_t)regOut,word,0,7);
574 fRegHeader->SetWord(word);
577 // fill header later, need local response
578 Int_t indexReg = index;
579 index += kRegHeaderLength;
581 // 11 regional scaler word
583 memcpy(&buffer[index], fRegHeader->GetScalers(), kRegScalerLength*4);
584 index += kRegScalerLength;
587 // end of regional word
588 buffer[index++] = fRegHeader->GetEndOfReg();
590 // 16 local card per regional board
591 // UShort_t localMask = 0x0;
593 for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
595 // slot zero for Regional card
596 Int_t localBoardId = crate->GetLocalBoardId(iLoc);
598 if (localBoardId) { // if not empty slot
599 AliMpLocalBoard* localBoard = AliMpDDLStore::Instance()->GetLocalBoard(localBoardId);
601 if (localBoard->IsNotified()) {// if notified board
602 AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(localBoardId);
604 locCard = locTrg->LoCircuit();
605 locDec = locTrg->GetLoDecision();
606 trigY = locTrg->LoTrigY();
607 posY = locTrg->LoStripY();
608 posX = locTrg->LoStripX();
609 devX = locTrg->LoDev();
610 sdevX = locTrg->LoSdev();
612 AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n",
613 locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
616 LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
617 (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
619 buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
620 buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
621 buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
622 buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
623 buffer[index++] = (Int_t)word; // data word
627 // fill copy card X-Y inputs from the notified cards
628 if (localBoard->GetInputXfrom() && localBoard->GetInputYfrom())
631 locDec = 0; trigY = 1; posY = 15;
632 posX = 0; devX = 0; sdevX = 1;
633 LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
634 (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
636 Int_t localFromId = localBoard->GetInputXfrom();
637 AliMUONLocalTrigger* locTrgfrom = triggerStore.FindLocal(localFromId);
639 buffer[index++] = 0; // copy only X3-4 & Y1-4
640 buffer[index++] = (locTrgfrom->GetX3Pattern() | (locTrgfrom->GetX4Pattern() << 16));
641 buffer[index++] = (locTrgfrom->GetY1Pattern() | (locTrgfrom->GetY2Pattern() << 16));
642 buffer[index++] = (locTrgfrom->GetY3Pattern() | (locTrgfrom->GetY4Pattern() << 16));
643 buffer[index++] = word;
647 // fill with 10CDEAD word for empty slots
648 for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
649 buffer[index++] = fLocalStruct->GetDisableWord();
650 }// condition localBoard
652 // 45 regional scaler word
654 memcpy(&buffer[index], fLocalStruct->GetScalers(), kLocScalerLength*4);
655 index += kLocScalerLength;
658 // end of local structure words
659 buffer[index++] = fLocalStruct->GetEndOfLocal();
662 // fill regional header with local output
663 fRegHeader->SetInput(regInpHpt, 0);
664 fRegHeader->SetInput(regInpHpt, 1);
665 memcpy(&buffer[indexReg],fRegHeader->GetHeader(),kRegHeaderLength*4);
670 // writting onto disk
672 fHeader.fSize = (index + headerSize) * 4;// total length in bytes
673 fwrite((char*)(&fHeader),headerSize*4,1,file[iDDL]);
674 fwrite(buffer,sizeof(int),index,file[iDDL]);
682 //____________________________________________________________________
683 void AliMUONRawWriter::SetScalersNumbers()
685 /// set numbers for scaler events for trigger headers
686 /// since this is provided by the experiment
687 /// put dummy numbers to check the monitoring
689 fDarcHeader->SetScalersNumbers();
690 fRegHeader->SetScalersNumbers();
691 fLocalStruct->SetScalersNumbers();
693 fScalerEvent = kTRUE;