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 **************************************************************************/
16 #include <Riostream.h>
17 #include <TClonesArray.h>
25 #include "AliRawDataHeaderSim.h"
26 #include "AliBitPacking.h"
27 #include "AliPMDdigit.h"
28 #include "AliPMDBlockHeader.h"
29 #include "AliPMDDspHeader.h"
30 #include "AliPMDPatchBusHeader.h"
31 #include "AliPMDRawStream.h"
32 #include "AliPMDDDLRawData.h"
34 #include "AliFstream.h"
36 ClassImp(AliPMDDDLRawData)
38 AliPMDDDLRawData::AliPMDDDLRawData():
39 fDigits(new TClonesArray("AliPMDdigit", 1000))
41 // Default Constructor
45 //____________________________________________________________________________
46 AliPMDDDLRawData::AliPMDDDLRawData(const AliPMDDDLRawData& ddlraw):
48 fDigits(ddlraw.fDigits)
52 //____________________________________________________________________________
53 AliPMDDDLRawData & AliPMDDDLRawData::operator=(const AliPMDDDLRawData& ddlraw)
58 fDigits = ddlraw.fDigits;
62 //____________________________________________________________________________
64 AliPMDDDLRawData::~AliPMDDDLRawData()
71 //____________________________________________________________________________
72 void AliPMDDDLRawData::WritePMDRawData(TTree *treeD)
74 // write digits into raw data format
78 TBranch *branch = treeD->GetBranch("PMDDigit");
81 AliError("PMD Digit branch not found");
84 branch->SetAddress(&fDigits);
86 Int_t nmodules = (Int_t) treeD->GetEntries();
87 AliDebug(1,Form("Number of modules inside treeD = %d",nmodules));
89 const Int_t kDDL = AliDAQ::NumberOfDdls("PMD");
90 Int_t modulePerDDL = 0;
93 AliRawDataHeaderSim header;
94 UInt_t sizeRawData = 0;
96 const Int_t kbusSize = 51;
97 const Int_t kSize = 1536;
100 UInt_t busPatch[kbusSize][1536];
102 Int_t contentsBus[kbusSize];
108 for(Int_t iddl = 0; iddl < kDDL; iddl++)
110 strcpy(filename,AliDAQ::DdlFileName("PMD",iddl));
112 outfile = new AliFstream(filename);
132 // Write the Dummy Data Header into the file
133 Int_t bHPosition = outfile->Tellp();
134 outfile->WriteBuffer((char*)(&header),sizeof(header));
136 for (Int_t ibus = 0; ibus < kbusSize; ibus++)
138 contentsBus[ibus] = 0;
139 for (Int_t ich = 0; ich < kSize; ich++)
141 busPatch[ibus][ich] = 0;
145 for(Int_t ium = 0; ium < modulePerDDL; ium++)
147 if (iddl == 4 && ium == 6) mmodule = 42;
149 // Extract energy deposition per cell and pack it
150 // in a 32-bit word and returns all the total words
151 // per one unit-module
153 GetUMDigitsData(treeD, mmodule, iddl, contentsBus, busPatch);
162 for (Int_t i = 0; i < 10; i++)
166 for (Int_t ibus=0; ibus < 5; ibus++)
169 if (contentsBus[ij] > 0)
171 dsp[i] += contentsBus[ij];
176 // Add the patch Bus header to the DSP contents
177 dsp[i] += 4*dspBus[i];
180 Int_t dspBlockARDL = 0;
181 Int_t dspBlockBRDL = 0;
185 for (Int_t i = 0; i < 5; i++)
188 Int_t iodd = 2*i + 1;
191 dspBlockARDL += dsp[ieven];
192 remainder = dsp[ieven]%2;
200 dspBlockBRDL += dsp[iodd];
201 remainder = dsp[iodd]%2;
209 // Start writing the DDL file
211 AliPMDBlockHeader blHeader;
212 AliPMDDspHeader dspHeader;
213 AliPMDPatchBusHeader pbusHeader;
215 const Int_t kblHLen = blHeader.GetHeaderLength();
216 const Int_t kdspHLen = dspHeader.GetHeaderLength();
217 const Int_t kpbusHLen = pbusHeader.GetHeaderLength();
220 UInt_t dspBlockHeaderWord[8];
221 UInt_t dspHeaderWord[10];
222 UInt_t patchBusHeaderWord[4];
224 UInt_t ddlEndWord[2] = {0xDEADFACE, 0xDEADFACE};
229 for (Int_t iblock = 0; iblock < 2; iblock++)
233 for (Int_t i=0; i<kblHLen; i++)
235 dspBlockHeaderWord[i] = 0;
239 dspBlockHeaderWord[1] = (UInt_t) (dspBlockARDL + kblHLen);
240 dspBlockHeaderWord[2] = (UInt_t) dspBlockARDL;
242 else if (iblock == 1)
244 dspBlockHeaderWord[1] = (UInt_t) (dspBlockBRDL + kblHLen);
245 dspBlockHeaderWord[2] = (UInt_t) dspBlockBRDL;
248 outfile->WriteBuffer((char*)dspBlockHeaderWord,kblHLen*sizeof(UInt_t));
258 else if (iblock == 1)
267 for (Int_t idsp = 0; idsp < 5; idsp++)
274 dspRDL = (UInt_t) dsp[dspno];
276 else if (iblock == 1)
279 dspRDL = (UInt_t) dsp[dspno];
282 for (Int_t i=0; i<kdspHLen; i++)
284 dspHeaderWord[i] = 0;
286 remainder = dspRDL%2;
287 if (remainder == 1) dspRDL++;
289 dspHeaderWord[1] = dspRDL + kdspHLen;
290 dspHeaderWord[2] = dspRDL;
291 dspHeaderWord[3] = dspno;
292 if (remainder == 1) dspHeaderWord[8] = 1; // setting the padding word
295 outfile->WriteBuffer((char*)dspHeaderWord,kdspHLen*sizeof(UInt_t));
297 for (Int_t ibus = 0; ibus < 5; ibus++)
301 Int_t busno = iskip[idsp] + ibus + 1;
302 Int_t patchbusRDL = contentsBus[busno];
306 patchBusHeaderWord[0] = 0;
307 patchBusHeaderWord[1] = (UInt_t) (patchbusRDL + kpbusHLen);
308 patchBusHeaderWord[2] = (UInt_t) patchbusRDL;
309 patchBusHeaderWord[3] = (UInt_t) busno;
311 else if (patchbusRDL == 0)
313 patchBusHeaderWord[0] = 0;
314 patchBusHeaderWord[1] = (UInt_t) kpbusHLen;
315 patchBusHeaderWord[2] = (UInt_t) 0;
316 patchBusHeaderWord[3] = (UInt_t) busno;
320 outfile->WriteBuffer((char*)patchBusHeaderWord,4*sizeof(UInt_t));
322 bknJunk += patchbusRDL;
324 for (Int_t iword = 0; iword < patchbusRDL; iword++)
326 buffer[iword] = busPatch[busno][iword];
329 outfile->WriteBuffer((char*)buffer,patchbusRDL*sizeof(UInt_t));
331 } // End of patch bus loop
334 // Adding a padding word if the total words odd
337 UInt_t paddingWord = dspHeader.GetDefaultPaddingWord();
338 outfile->WriteBuffer((char*)(&paddingWord),sizeof(UInt_t));
343 // Write two extra word at the end of each DDL file
344 outfile->WriteBuffer((char*)ddlEndWord,2*sizeof(UInt_t));
346 // Write real data header
347 // take the pointer to the beginning of the data header
348 // write the total number of words per ddl and bring the
349 // pointer to the current file position and close it
350 UInt_t cFPosition = outfile->Tellp();
351 sizeRawData = cFPosition - bHPosition - sizeof(header);
353 header.fSize = cFPosition - bHPosition;
354 header.SetAttribute(0); // valid data
355 outfile->Seekp(bHPosition);
356 outfile->WriteBuffer((char*)(&header),sizeof(header));
357 outfile->Seekp(cFPosition);
364 //____________________________________________________________________________
365 void AliPMDDDLRawData::GetUMDigitsData(TTree *treeD, Int_t imodule,
366 Int_t ddlno, Int_t *contentsBus,
367 UInt_t busPatch[][1536])
369 // Retrives digits data UnitModule by UnitModule
374 Int_t det, smn, irow, icol;
377 const Int_t kMaxBus = 51; // BKN
378 Int_t totPatchBus, bPatchBus, ePatchBus;
379 Int_t ibus, totmcm, rows, cols, rowe, cole;
382 Int_t patchBusNo[kMaxBus], mcmperBus[kMaxBus];
383 Int_t startRowBus[kMaxBus], startColBus[kMaxBus];
384 Int_t endRowBus[kMaxBus], endColBus[kMaxBus];
386 Int_t beginPatchBus = -1;
387 Int_t endPatchBus = -1;
388 for(Int_t i = 0; i < kMaxBus; i++)
397 Int_t modulePerDDL = 0;
402 else if (ddlno == 4 || ddlno == 5)
407 TString fileName(gSystem->Getenv("ALICE_ROOT"));
411 fileName += "/PMD/PMD_Mapping_ddl0.dat";
415 fileName += "/PMD/PMD_Mapping_ddl1.dat";
419 fileName += "/PMD/PMD_Mapping_ddl2.dat";
423 fileName += "/PMD/PMD_Mapping_ddl3.dat";
427 fileName += "/PMD/PMD_Mapping_ddl4.dat";
431 fileName += "/PMD/PMD_Mapping_ddl5.dat";
435 infile.open(fileName.Data(), ios::in); // ascii file
437 AliError(Form("Could not read the mapping file for DDL No = %d",ddlno));
439 for (Int_t im = 0; im < modulePerDDL; im++)
442 infile >> totPatchBus >> bPatchBus >> ePatchBus;
444 if (moduleno == imodule)
446 beginPatchBus = bPatchBus;
447 endPatchBus = ePatchBus;
450 for(Int_t i=0; i<totPatchBus; i++)
452 infile >> ibus >> totmcm >> rows >> rowe >> cols >> cole;
454 if (moduleno == imodule)
456 patchBusNo[ibus] = ibus;
457 mcmperBus[ibus] = totmcm;
458 startRowBus[ibus] = rows;
459 startColBus[ibus] = cols;
460 endRowBus[ibus] = rowe;
461 endColBus[ibus] = cole;
469 treeD->GetEntry(imodule);
470 Int_t nentries = fDigits->GetLast();
471 Int_t totword = nentries+1;
473 AliPMDdigit *pmddigit;
475 for (Int_t ient = 0; ient < totword; ient++)
477 pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
479 det = pmddigit->GetDetector();
480 smn = pmddigit->GetSMNumber();
481 irow = pmddigit->GetRow();
482 icol = pmddigit->GetColumn();
483 adc = (UInt_t) pmddigit->GetADC();
485 TransformS2H(smn,irow,icol);
487 GetMCMCh(ddlno, smn, irow, icol, beginPatchBus, endPatchBus,
488 mcmperBus, startRowBus, startColBus,
489 endRowBus, endColBus, busno, mcmno, chno);
492 AliBitPacking::PackWord(adc,baseword,0,11);
493 AliBitPacking::PackWord(chno,baseword,12,17);
494 AliBitPacking::PackWord(mcmno,baseword,18,28);
495 AliBitPacking::PackWord(0,baseword,29,30);
496 parity = ComputeParity(baseword); // generate the parity bit
497 AliBitPacking::PackWord(parity,baseword,31,31);
499 Int_t jj = contentsBus[busno];
500 busPatch[busno][jj] = baseword;
502 contentsBus[busno]++;
507 //____________________________________________________________________________
508 void AliPMDDDLRawData::TransformS2H(Int_t smn, Int_t &irow, Int_t &icol)
510 // Does the Software to Hardware coordinate transformation
516 // First in digits we have all dimension 48x96
517 // Transform into the realistic one, i.e, For SM 0&1 96(row)x48(col)
518 // and for SM 2&3 48(row)x96(col)
525 else if( smn >= 12 && smn < 24)
537 //____________________________________________________________________________
539 void AliPMDDDLRawData::GetMCMCh(Int_t ddlno, Int_t smn, Int_t row, Int_t col,
540 Int_t beginPatchBus, Int_t endPatchBus,
542 Int_t *startRowBus, Int_t *startColBus,
543 Int_t *endRowBus, Int_t *endColBus,
544 Int_t & busno, UInt_t &mcmno, UInt_t &chno)
546 // This converts row col to hardware channel number
547 // This is the final version of mapping supplied by Mriganka
551 static const UInt_t kChDdl01[16][4] = { {6, 4, 5, 7},
569 static const UInt_t kChDdl23[16][4] = { {57, 59, 58, 56},
587 static const UInt_t kChDdl41[16][4] = { {56, 58, 59, 57},
605 static const UInt_t kChDdl42[16][4] = { {7, 5, 4, 6},
623 static const UInt_t kChDdl51[16][4] = { {7, 5, 4, 6},
642 static const UInt_t kChDdl52[16][4] = { {56, 58, 59, 57},
660 for (Int_t i = 0; i < 16; i++)
662 for (Int_t j = 0; j < 4; j++)
664 if (ddlno == 0 || ddlno == 1) iCh[i][j] = kChDdl01[i][j];
665 if (ddlno == 2 || ddlno == 3) iCh[i][j] = kChDdl23[i][j];
667 if (ddlno == 4 && smn < 6) iCh[i][j] = kChDdl41[i][j];
668 if (ddlno == 4 && (smn >= 18 && smn < 24))iCh[i][j] = kChDdl42[i][j];
669 if (ddlno == 5 && (smn >= 12 && smn < 18))iCh[i][j] = kChDdl51[i][j];
670 if (ddlno == 5 && (smn >= 6 && smn < 12))iCh[i][j] = kChDdl52[i][j];
675 Int_t irownew = row%16;
676 Int_t icolnew = col%4;
678 chno = iCh[irownew][icolnew];
681 for (Int_t ibus = beginPatchBus; ibus <= endPatchBus; ibus++)
683 Int_t srow = startRowBus[ibus];
684 Int_t erow = endRowBus[ibus];
685 Int_t scol = startColBus[ibus];
686 Int_t ecol = endColBus[ibus];
687 Int_t tmcm = mcmperBus[ibus];
688 if ((row >= srow && row <= erow) && (col >= scol && col <= ecol))
692 // Find out the MCM Number
695 if (ddlno == 0 || ddlno == 1)
697 // PRE plane, SU Mod = 0, 1
698 mcmno = (col-scol)/4 + 1;
701 else if (ddlno == 2 || ddlno == 3)
703 // PRE plane, SU Mod = 2, 3
704 icolnew = (col - scol)/4;
705 mcmno = tmcm - icolnew;
707 else if (ddlno == 4 )
709 // CPV plane, SU Mod = 0, 3 : ddl = 4
713 Int_t midrow = srow + 16;
714 if(row >= srow && row < midrow)
716 mcmno = 12 + (col-scol)/4 + 1;
718 else if(row >= midrow && row <= erow)
721 mcmno = (col-scol)/4 + 1;
727 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
730 Int_t midrow = srow + 16;
731 if (row >= midrow && row <= erow)
733 mcmno = 12 + (ecol -col)/4 + 1;
735 else if (row >= srow && row < midrow)
737 mcmno = (ecol - col)/4 + 1;
740 else if (rowdiff < 16)
742 mcmno = (ecol - col)/4 + 1;
746 else if ( ddlno == 5)
748 // CPV plane, SU Mod = 1, 2 : ddl = 5
752 Int_t midrow = srow + 16;
753 if(row >= srow && row < midrow)
755 mcmno = 12 + (col-scol)/4 + 1;
757 else if(row >= midrow && row <= erow)
759 mcmno = (col-scol)/4 + 1;
765 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
768 Int_t midrow = srow + 16;
769 if (row >= midrow && row <= erow)
771 mcmno = 12 + (ecol - col)/4 + 1;
773 else if (row >= srow && row < midrow)
775 mcmno = (ecol - col)/4 + 1;
778 else if (rowdiff < 16)
780 mcmno = (ecol - col)/4 + 1;
788 //____________________________________________________________________________
790 Int_t AliPMDDDLRawData::ComputeParity(UInt_t baseword)
792 // Generate the parity bit
795 for(Int_t j=0; j<29; j++)
797 if (baseword & 0x01 ) count++;
800 Int_t parity = count%2;
804 //____________________________________________________________________________