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 "AliRawDataHeaderSim.h"
81 #include "AliBitPacking.h"
85 #include "TObjArray.h"
86 #include "TStopwatch.h"
87 #include <Riostream.h>
90 ClassImp(AliMUONRawWriter) // Class implementation in ROOT context
93 //__________________________________________________________________________
94 AliMUONRawWriter::AliMUONRawWriter()
96 fBlockHeader(new AliMUONBlockHeader()),
97 fDspHeader(new AliMUONDspHeader()),
98 fDarcHeader(new AliMUONDarcHeader()),
99 fRegHeader(new AliMUONRegHeader()),
100 fLocalStruct(new AliMUONLocalStruct()),
101 fDDLStore(AliMpDDLStore::Instance()),
102 fScalerEvent(kFALSE),
104 fBufferSize((((43*AliMpConstants::ManuNofChannels() + 4)*5 + 10)*5 + 8)*2),
105 fBuffer(new Int_t [fBufferSize])
107 /// Standard Constructor
109 AliDebug(1,"Standard ctor");
111 // setting data key to default value (only for writting)
112 fBlockHeader->SetDataKey(fBlockHeader->GetDefaultDataKey());
113 fDspHeader->SetDataKey(fDspHeader->GetDefaultDataKey());
117 //__________________________________________________________________________
118 AliMUONRawWriter::~AliMUONRawWriter(void)
132 //____________________________________________________________________
133 void AliMUONRawWriter::LocalWordPacking(UInt_t& word, UInt_t locId, UInt_t locDec,
134 UInt_t trigY, UInt_t posY, UInt_t posX,
135 UInt_t sdevX, UInt_t devX)
137 /// pack local trigger word
139 AliBitPacking::PackWord(locId,word,19,22); //card id number in crate
140 AliBitPacking::PackWord(locDec,word,15,18);
141 AliBitPacking::PackWord(trigY,word,14,14);
142 AliBitPacking::PackWord(posY,word,10,13);
143 AliBitPacking::PackWord(sdevX,word,9,9);
144 AliBitPacking::PackWord(devX,word,5,8);
145 AliBitPacking::PackWord(posX,word,0,4);
149 //____________________________________________________________________
150 Int_t AliMUONRawWriter::Digits2Raw(AliMUONVDigitStore* digitStore,
151 AliMUONVTriggerStore* triggerStore)
153 /// convert digits of the current event to raw data
164 AliCodeTimerAuto("for Tracker")
166 AliMpExMap busPatchMap(true);
168 Int_t nDDLs = AliDAQ::NumberOfDdls("MUONTRK");
170 Int_t nofBusPatches(0);
172 for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
174 AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
175 nofBusPatches += ddl->GetNofBusPatches();
178 busPatchMap.SetSize(nofBusPatches);
180 Digits2BusPatchMap(*digitStore,busPatchMap);
182 for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL )
184 WriteTrackerDDL(busPatchMap,iDDL);
186 AliDebug(1,"Tracker written");
191 AliCodeTimerAuto("for Trigger")
199 strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
200 file[0] = new AliFstream(name);
203 strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
204 file[1] = new AliFstream(name);
206 WriteTriggerDDL(*triggerStore,file);
212 AliDebug(1,"Trigger written");
218 //______________________________________________________________________________
220 AliMUONRawWriter::Digits2BusPatchMap(const AliMUONVDigitStore& digitStore,
221 AliMpExMap& busPatchMap)
223 /// Create bus patch structures corresponding to digits in the store
227 static const Int_t kMAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
229 // DDL event one per half chamber
234 UChar_t channelId = 0;
236 Int_t busPatchId = 0;
239 TIter next(digitStore.CreateTrackerIterator());
240 AliMUONVDigit* digit;
242 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
244 charge = digit->ADC();
245 if ( charge > kMAXADC )
247 // This is most probably an error in the digitizer (which should insure
248 // the adc is below kMAXADC), so make it a (non-fatal) error indeed.
249 AliError(Form("adc value %d above 0x%x for DE %d . Setting to 0x%x. Digit is:",
250 charge,kMAXADC,digit->DetElemId(),kMAXADC));
251 StdoutToAliError(digit->Print());
256 busPatchId = GetBusPatch(*digit);
258 if (busPatchId<0) continue;
260 if ( digit->ManuId() > 0x7FF ||
261 digit->ManuChannel() > 0x3F )
263 StdoutToAliError(digit->Print(););
264 AliFatal("ManuId,ManuChannel are invalid for the digit above.");
267 manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
268 channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
272 AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
273 AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
274 AliBitPacking::PackWord((UInt_t)charge,word,0,11);
278 for (Int_t i = 1; i <= 30; ++i)
280 parity ^= ((word >> i) & 0x1);
282 AliBitPacking::PackWord((UInt_t)parity,word,31,31);
284 AliMUONBusStruct* busStruct =
285 static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(busPatchId));
289 busStruct = new AliMUONBusStruct;
290 busStruct->SetDataKey(busStruct->GetDefaultDataKey());
291 busStruct->SetBusPatchId(busPatchId);
292 busStruct->SetLength(0);
293 busPatchMap.Add(busPatchId,busStruct);
297 busStruct->AddData(word);
301 //______________________________________________________________________________
303 AliMUONRawWriter::WriteTrackerDDL(AliMpExMap& busPatchMap, Int_t iDDL)
305 /// Write DDL file for one tracker DDL
307 // buffer size (max'ed out)
308 // (((43 manus max per bus patch *64 channels + 4 bus patch words) * 5 bus patch
309 // + 10 dsp words)*5 dsps + 8 block words)*2 blocks
313 if (fHeader == 0x0) {
314 AliError("Raw data header must be set");
317 memset(fBuffer,0,fBufferSize*sizeof(Int_t));
319 AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
320 Int_t iDspMax = ddl->GetMaxDsp();
321 Int_t iBusPerDSP[5]; //number of bus patches per DSP
322 ddl->GetBusPerDsp(iBusPerDSP);
325 Int_t totalDDLLength = 0;
329 // two blocks A and B per DDL
330 for (Int_t iBlock = 0; iBlock < 2; ++iBlock)
333 Int_t length = fBlockHeader->GetHeaderLength();
334 memcpy(&fBuffer[index],fBlockHeader->GetHeader(),length*4);
335 Int_t indexBlk = index;
338 // 5 DSP's max per block
339 for (Int_t iDsp = 0; iDsp < iDspMax; ++iDsp)
342 Int_t length = fDspHeader->GetHeaderLength();
343 memcpy(&fBuffer[index],fDspHeader->GetHeader(),length*4);
344 Int_t indexDsp = index;
347 // 5 buspatches max per DSP
348 for (Int_t i = 0; i < iBusPerDSP[iDsp]; ++i)
350 Int_t iBusPatch = ddl->GetBusPatchId(busIter++);
352 // iteration over bus patch in DDL
355 AliWarning(Form("Error in bus itr in DDL %d\n", iDDL));
359 AliMUONBusStruct* busStructPtr = static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(iBusPatch));
361 // check if buspatchid has digit
364 // add bus patch structure header
365 Int_t length = busStructPtr->GetHeaderLength();
366 memcpy(&fBuffer[index],busStructPtr->GetHeader(),length*4);
369 // add bus patch data
370 length = busStructPtr->GetLength();
371 memcpy(&fBuffer[index],busStructPtr->GetData(),length*4);
376 // writting anyhow buspatch structure (empty ones)
377 fBuffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
378 fBuffer[index++] = busStructPtr->GetHeaderLength(); // header length
379 fBuffer[index++] = 0; // raw data length
380 fBuffer[index++] = iBusPatch; // bus patch
384 // check if totalLength even
385 // set padding word in case
386 // Add one word 0xBEEFFACE at the end of DSP structure
387 Int_t totalDspLength = index - indexDsp;
388 if ((totalDspLength % 2) == 1)
390 fBuffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
391 fBuffer[index++] = fDspHeader->GetDefaultPaddingWord();
395 Int_t dspLength = totalDspLength - fDspHeader->GetHeaderLength();
397 fBuffer[indexDsp+1] = totalDspLength; // dsp total length
398 fBuffer[indexDsp+2] = dspLength; // data length
402 Int_t totalBlkLength = index - indexBlk;
403 Int_t blkLength = totalBlkLength - fBlockHeader->GetHeaderLength();
404 totalDDLLength += totalBlkLength;
406 fBuffer[indexBlk+1] = totalBlkLength; // total block length
407 fBuffer[indexBlk+2] = blkLength;
411 // add twice the end of CRT structure data key
412 // hope it's good placed (ChF)
413 fBuffer[index++] = fBlockHeader->GetDdlDataKey();
414 fBuffer[index++] = fBlockHeader->GetDdlDataKey();
417 // writting onto disk
418 // total length in bytes
421 Int_t headerSize = sizeof(AliRawDataHeader)/4;
423 fHeader->fSize = (totalDDLLength + headerSize) * 4;
425 AliFstream* file = new AliFstream(AliDAQ::DdlFileName("MUONTRK",iDDL));
427 file->WriteBuffer((char*)fHeader,headerSize*4);
428 file->WriteBuffer((char*)fBuffer,sizeof(int)*index);
432 //______________________________________________________________________________
433 Int_t AliMUONRawWriter::GetBusPatch(const AliMUONVDigit& digit) const
435 /// Determine the BusPatch this digit belongs to.
437 return fDDLStore->GetBusPatchId(digit.DetElemId(),digit.ManuId());
440 //______________________________________________________________________________
441 Int_t AliMUONRawWriter::WriteTriggerDDL(const AliMUONVTriggerStore& triggerStore, AliFstream* file[2])
443 /// Write trigger DDL
447 if (fHeader == 0x0) {
448 AliError("Raw data header must be set");
452 // DDL event one per half chamber
455 Int_t headerSize = sizeof(AliRawDataHeader)/4;
457 // global trigger for trigger pattern
458 AliMUONGlobalTrigger* gloTrg = triggerStore.Global();
464 Int_t gloTrigResp = gloTrg->GetGlobalResponse();
470 UChar_t locDec, trigY, posY, posX, regOut;
476 UInt_t version = 1; // software version
477 UInt_t eventPhys = 1; // trigger type: 1 for physics, 0 for software
478 UInt_t serialNb = 0xF; // serial nb of card: all bits on for the moment
479 Int_t globalFlag = 0; // set to 1 if global info present in DDL else set to 0
482 static const Int_t kDarcHeaderLength = fDarcHeader->GetDarcHeaderLength();
483 static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
484 static const Int_t kDarcScalerLength = fDarcHeader->GetDarcScalerLength();
485 static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
486 static const Int_t kRegHeaderLength = fRegHeader->GetHeaderLength();
487 static const Int_t kRegScalerLength = fRegHeader->GetScalerLength();
488 static const Int_t kLocHeaderLength = fLocalStruct->GetLength();
489 static const Int_t kLocScalerLength = fLocalStruct->GetScalerLength();
491 // [16(local)*6 words + 6 words]*8(reg) + 8 words = 824
492 static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) + (kRegHeaderLength+1))* 8
493 + kDarcHeaderLength + kGlobalHeaderLength + 2;
495 // [16(local)*51 words + 16 words]*8(reg) + 8 + 10 + 8 words scaler event 6682 words
496 static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength + kLocScalerLength +1) +
497 (kRegHeaderLength + kRegScalerLength +1))* 8 +
498 (kDarcHeaderLength + kDarcScalerLength +
499 kGlobalHeaderLength + kGlobalScalerLength + 2);
501 eventPhys = 0; //set to generate scaler events
502 fHeader->fWord2 |= (0x1 << 14); // set L1SwC bit on
505 buffer = new Int_t [kScalerBufferSize];
507 buffer = new Int_t [kBufferSize];
511 // open DDL file, on per 1/2 chamber
512 for ( Int_t iDDL = 0; iDDL < 2; ++iDDL )
516 if (iDDL == 0) // suppose global info in DDL one
522 // set darc status word
523 // see AliMUONDarcHeader.h for details
524 AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
525 AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
526 AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
527 AliBitPacking::PackWord((UInt_t)version,word,12,19);
528 fDarcHeader->SetWord(word);
530 memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4);
531 index += kDarcHeaderLength;
533 // no global input for the moment....
535 fDarcHeader->SetGlobalOutput(gloTrigResp);
537 fDarcHeader->SetGlobalOutput(0);
540 // 6 DARC scaler words
541 memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
542 index += kDarcScalerLength;
545 buffer[index++] = fDarcHeader->GetEndOfDarc();
547 // 4 words of global board input + Global board output
548 memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4);
549 index += kGlobalHeaderLength;
552 // 10 Global scaler words
553 memcpy(fDarcHeader->GetGlobalScalers(), &buffer[index], kGlobalScalerLength*4);
554 index += kGlobalScalerLength;
557 // end of global word
558 buffer[index++] = fDarcHeader->GetEndOfGlobal();
560 // 8 regional cards per DDL
561 for (Int_t iReg = 0; iReg < 8; ++iReg) {
564 AliMpTriggerCrate* crate = AliMpDDLStore::Instance()->GetTriggerCrate(iDDL, iReg);
567 AliWarning(Form("Missing crate number %d in DDL %d\n", iReg, iDDL));
569 // regional info tree, make sure that no reg card missing
570 AliMUONRegionalTrigger* regTrg = triggerStore.FindRegional(iReg+iDDL*8);
572 // Regional card header
575 // set darc status word
576 fRegHeader->SetDarcWord(word);
578 regOut = regTrg->GetOutput();
579 regInpHpt = regTrg->GetLocalOutput(0);
580 regInpLpt = regTrg->GetLocalOutput(1);
582 // fill darc word, not darc status for the moment (empty)
583 //see AliMUONRegHeader.h for details
584 AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31);
585 AliBitPacking::PackWord((UInt_t)serialNb,word,19,24);
586 AliBitPacking::PackWord((UInt_t)version,word,7,14);
587 AliBitPacking::PackWord((UInt_t)iReg,word,15,18);
588 AliBitPacking::PackWord((UInt_t)regOut,word,0,7);
589 fRegHeader->SetWord(word);
592 // fill header later, need local response
593 Int_t indexReg = index;
594 index += kRegHeaderLength;
596 // 11 regional scaler word
598 memcpy(&buffer[index], fRegHeader->GetScalers(), kRegScalerLength*4);
599 index += kRegScalerLength;
602 // end of regional word
603 buffer[index++] = fRegHeader->GetEndOfReg();
605 // 16 local card per regional board
606 // UShort_t localMask = 0x0;
608 for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
610 // slot zero for Regional card
611 Int_t localBoardId = crate->GetLocalBoardId(iLoc);
613 if (localBoardId) { // if not empty slot
614 AliMpLocalBoard* localBoard = AliMpDDLStore::Instance()->GetLocalBoard(localBoardId);
616 if (localBoard->IsNotified()) {// if notified board
617 AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(localBoardId);
619 locCard = locTrg->LoCircuit();
620 locDec = locTrg->GetLoDecision();
621 trigY = locTrg->LoTrigY();
622 posY = locTrg->LoStripY();
623 posX = locTrg->LoStripX();
624 devX = locTrg->LoDev();
625 sdevX = locTrg->LoSdev();
627 AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n",
628 locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
631 LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
632 (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
634 buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
635 buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
636 buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
637 buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
638 buffer[index++] = (Int_t)word; // data word
642 // fill copy card X-Y inputs from the notified cards
643 if (localBoard->GetInputXfrom() && localBoard->GetInputYfrom())
646 locDec = 0; trigY = 1; posY = 15;
647 posX = 0; devX = 0; sdevX = 1;
648 LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY,
649 (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
651 Int_t localFromId = localBoard->GetInputXfrom();
652 AliMUONLocalTrigger* locTrgfrom = triggerStore.FindLocal(localFromId);
654 buffer[index++] = 0; // copy only X3-4 & Y1-4
655 buffer[index++] = (locTrgfrom->GetX3Pattern() | (locTrgfrom->GetX4Pattern() << 16));
656 buffer[index++] = (locTrgfrom->GetY1Pattern() | (locTrgfrom->GetY2Pattern() << 16));
657 buffer[index++] = (locTrgfrom->GetY3Pattern() | (locTrgfrom->GetY4Pattern() << 16));
658 buffer[index++] = word;
662 // fill with 10CDEAD word for empty slots
663 for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
664 buffer[index++] = fLocalStruct->GetDisableWord();
665 }// condition localBoard
667 // 45 regional scaler word
669 memcpy(&buffer[index], fLocalStruct->GetScalers(), kLocScalerLength*4);
670 index += kLocScalerLength;
673 // end of local structure words
674 buffer[index++] = fLocalStruct->GetEndOfLocal();
677 // fill regional header with local output
678 fRegHeader->SetInput(regInpHpt, 0);
679 fRegHeader->SetInput(regInpHpt, 1);
680 memcpy(&buffer[indexReg],fRegHeader->GetHeader(),kRegHeaderLength*4);
685 // writting onto disk
687 fHeader->fSize = (index + headerSize) * 4;// total length in bytes
688 file[iDDL]->WriteBuffer((char*)fHeader,headerSize*4);
689 file[iDDL]->WriteBuffer((char*)buffer,sizeof(int)*index);
697 //____________________________________________________________________
698 void AliMUONRawWriter::SetScalersNumbers()
700 /// set numbers for scaler events for trigger headers
701 /// since this is provided by the experiment
702 /// put dummy numbers to check the monitoring
704 fDarcHeader->SetScalersNumbers();
705 fRegHeader->SetScalersNumbers();
706 fLocalStruct->SetScalersNumbers();
708 fScalerEvent = kTRUE;