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];
175 // Add the patch Bus header to the DSP contents
176 dsp[i] += 4*dspBus[i];
179 Int_t dspBlockARDL = 0;
180 Int_t dspBlockBRDL = 0;
184 for (Int_t i = 0; i < 5; i++)
187 Int_t iodd = 2*i + 1;
190 dspBlockARDL += dsp[ieven];
191 remainder = dsp[ieven]%2;
199 dspBlockBRDL += dsp[iodd];
200 remainder = dsp[iodd]%2;
211 // Start writing the DDL file
213 AliPMDBlockHeader blHeader;
214 AliPMDDspHeader dspHeader;
215 AliPMDPatchBusHeader pbusHeader;
217 const Int_t kblHLen = blHeader.GetHeaderLength();
218 const Int_t kdspHLen = dspHeader.GetHeaderLength();
219 const Int_t kpbusHLen = pbusHeader.GetHeaderLength();
222 UInt_t dspBlockHeaderWord[8];
223 UInt_t dspHeaderWord[10];
224 UInt_t patchBusHeaderWord[4];
226 UInt_t ddlEndWord[2] = {0xDEADFACE, 0xDEADFACE};
231 for (Int_t iblock = 0; iblock < 2; iblock++)
235 for (Int_t i=0; i<kblHLen; i++)
237 dspBlockHeaderWord[i] = 0;
241 dspBlockHeaderWord[1] = (UInt_t) (dspBlockARDL + kblHLen);
242 dspBlockHeaderWord[2] = (UInt_t) dspBlockARDL;
244 else if (iblock == 1)
246 dspBlockHeaderWord[1] = (UInt_t) (dspBlockBRDL + kblHLen);
247 dspBlockHeaderWord[2] = (UInt_t) dspBlockBRDL;
250 outfile->WriteBuffer((char*)dspBlockHeaderWord,kblHLen*sizeof(UInt_t));
260 else if (iblock == 1)
269 for (Int_t idsp = 0; idsp < 5; idsp++)
276 dspRDL = (UInt_t) dsp[dspno];
278 else if (iblock == 1)
281 dspRDL = (UInt_t) dsp[dspno];
284 for (Int_t i=0; i<kdspHLen; i++)
286 dspHeaderWord[i] = 0;
288 remainder = dspRDL%2;
289 if (remainder == 1) dspRDL++;
291 dspHeaderWord[1] = dspRDL + kdspHLen;
292 dspHeaderWord[2] = dspRDL;
293 dspHeaderWord[3] = dspno;
294 if (remainder == 1) dspHeaderWord[8] = 1; // setting the padding word
297 outfile->WriteBuffer((char*)dspHeaderWord,kdspHLen*sizeof(UInt_t));
299 for (Int_t ibus = 0; ibus < 5; ibus++)
303 Int_t busno = iskip[idsp] + ibus + 1;
304 Int_t patchbusRDL = contentsBus[busno];
308 patchBusHeaderWord[0] = 0;
309 patchBusHeaderWord[1] = (UInt_t) (patchbusRDL + kpbusHLen);
310 patchBusHeaderWord[2] = (UInt_t) patchbusRDL;
311 patchBusHeaderWord[3] = (UInt_t) busno;
313 else if (patchbusRDL == 0)
315 patchBusHeaderWord[0] = 0;
316 patchBusHeaderWord[1] = (UInt_t) kpbusHLen;
317 patchBusHeaderWord[2] = (UInt_t) 0;
318 patchBusHeaderWord[3] = (UInt_t) busno;
322 outfile->WriteBuffer((char*)patchBusHeaderWord,4*sizeof(UInt_t));
324 bknJunk += patchbusRDL;
326 for (Int_t iword = 0; iword < patchbusRDL; iword++)
328 buffer[iword] = busPatch[busno][iword];
331 outfile->WriteBuffer((char*)buffer,patchbusRDL*sizeof(UInt_t));
333 } // End of patch bus loop
336 // Adding a padding word if the total words odd
339 UInt_t paddingWord = dspHeader.GetDefaultPaddingWord();
340 outfile->WriteBuffer((char*)(&paddingWord),sizeof(UInt_t));
345 // Write two extra word at the end of each DDL file
346 outfile->WriteBuffer((char*)ddlEndWord,2*sizeof(UInt_t));
348 // Write real data header
349 // take the pointer to the beginning of the data header
350 // write the total number of words per ddl and bring the
351 // pointer to the current file position and close it
352 UInt_t cFPosition = outfile->Tellp();
353 sizeRawData = cFPosition - bHPosition - sizeof(header);
355 header.fSize = cFPosition - bHPosition;
356 header.SetAttribute(0); // valid data
357 outfile->Seekp(bHPosition);
358 outfile->WriteBuffer((char*)(&header),sizeof(header));
359 outfile->Seekp(cFPosition);
366 //____________________________________________________________________________
367 void AliPMDDDLRawData::GetUMDigitsData(TTree *treeD, Int_t imodule,
368 Int_t ddlno, Int_t *contentsBus,
369 UInt_t busPatch[][1536])
371 // Retrives digits data UnitModule by UnitModule
376 Int_t det, smn, irow, icol;
379 const Int_t kMaxBus = 51; // BKN
380 Int_t totPatchBus, bPatchBus, ePatchBus;
381 Int_t ibus, totmcm, rows, cols, rowe, cole;
384 Int_t patchBusNo[kMaxBus], mcmperBus[kMaxBus];
385 Int_t startRowBus[kMaxBus], startColBus[kMaxBus];
386 Int_t endRowBus[kMaxBus], endColBus[kMaxBus];
388 Int_t beginPatchBus = -1;
389 Int_t endPatchBus = -1;
390 for(Int_t i = 0; i < kMaxBus; i++)
399 Int_t modulePerDDL = 0;
404 else if (ddlno == 4 || ddlno == 5)
409 TString fileName(gSystem->Getenv("ALICE_ROOT"));
413 fileName += "/PMD/PMD_Mapping_ddl0.dat";
417 fileName += "/PMD/PMD_Mapping_ddl1.dat";
421 fileName += "/PMD/PMD_Mapping_ddl2.dat";
425 fileName += "/PMD/PMD_Mapping_ddl3.dat";
429 fileName += "/PMD/PMD_Mapping_ddl4.dat";
433 fileName += "/PMD/PMD_Mapping_ddl5.dat";
437 infile.open(fileName.Data(), ios::in); // ascii file
439 AliError(Form("Could not read the mapping file for DDL No = %d",ddlno));
441 for (Int_t im = 0; im < modulePerDDL; im++)
444 infile >> totPatchBus >> bPatchBus >> ePatchBus;
446 if (moduleno == imodule)
448 beginPatchBus = bPatchBus;
449 endPatchBus = ePatchBus;
452 for(Int_t i=0; i<totPatchBus; i++)
454 infile >> ibus >> totmcm >> rows >> rowe >> cols >> cole;
456 if (moduleno == imodule)
458 patchBusNo[ibus] = ibus;
459 mcmperBus[ibus] = totmcm;
460 startRowBus[ibus] = rows;
461 startColBus[ibus] = cols;
462 endRowBus[ibus] = rowe;
463 endColBus[ibus] = cole;
471 treeD->GetEntry(imodule);
472 Int_t nentries = fDigits->GetLast();
473 Int_t totword = nentries+1;
475 AliPMDdigit *pmddigit;
477 for (Int_t ient = 0; ient < totword; ient++)
479 pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
481 det = pmddigit->GetDetector();
482 smn = pmddigit->GetSMNumber();
483 irow = pmddigit->GetRow();
484 icol = pmddigit->GetColumn();
485 adc = (UInt_t) pmddigit->GetADC();
487 TransformS2H(smn,irow,icol);
489 GetMCMCh(ddlno, smn, irow, icol, beginPatchBus, endPatchBus,
490 mcmperBus, startRowBus, startColBus,
491 endRowBus, endColBus, busno, mcmno, chno);
494 AliBitPacking::PackWord(adc,baseword,0,11);
495 AliBitPacking::PackWord(chno,baseword,12,17);
496 AliBitPacking::PackWord(mcmno,baseword,18,28);
497 AliBitPacking::PackWord(0,baseword,29,30);
498 parity = ComputeParity(baseword); // generate the parity bit
499 AliBitPacking::PackWord(parity,baseword,31,31);
501 Int_t jj = contentsBus[busno];
502 busPatch[busno][jj] = baseword;
504 contentsBus[busno]++;
509 //____________________________________________________________________________
510 void AliPMDDDLRawData::TransformS2H(Int_t smn, Int_t &irow, Int_t &icol)
512 // Does the Software to Hardware coordinate transformation
518 // First in digits we have all dimension 48x96
519 // Transform into the realistic one, i.e, For SM 0&1 96(row)x48(col)
520 // and for SM 2&3 48(row)x96(col)
527 else if( smn >= 12 && smn < 24)
539 //____________________________________________________________________________
541 void AliPMDDDLRawData::GetMCMCh(Int_t ddlno, Int_t smn, Int_t row, Int_t col,
542 Int_t beginPatchBus, Int_t endPatchBus,
544 Int_t *startRowBus, Int_t *startColBus,
545 Int_t *endRowBus, Int_t *endColBus,
546 Int_t & busno, UInt_t &mcmno, UInt_t &chno)
548 // This converts row col to hardware channel number
549 // This is the final version of mapping supplied by Mriganka
553 static const UInt_t kChDdl01[16][4] = { {6, 4, 5, 7},
571 static const UInt_t kChDdl23[16][4] = { {57, 59, 58, 56},
589 static const UInt_t kChDdl41[16][4] = { {56, 58, 59, 57},
607 static const UInt_t kChDdl42[16][4] = { {7, 5, 4, 6},
625 static const UInt_t kChDdl51[16][4] = { {7, 5, 4, 6},
644 static const UInt_t kChDdl52[16][4] = { {56, 58, 59, 57},
662 for (Int_t i = 0; i < 16; i++)
664 for (Int_t j = 0; j < 4; j++)
666 if (ddlno == 0 || ddlno == 1) iCh[i][j] = kChDdl01[i][j];
667 if (ddlno == 2 || ddlno == 3) iCh[i][j] = kChDdl23[i][j];
669 if (ddlno == 4 && smn < 6) iCh[i][j] = kChDdl41[i][j];
670 if (ddlno == 4 && (smn >= 18 && smn < 24))iCh[i][j] = kChDdl42[i][j];
671 if (ddlno == 5 && (smn >= 12 && smn < 18))iCh[i][j] = kChDdl51[i][j];
672 if (ddlno == 5 && (smn >= 6 && smn < 12))iCh[i][j] = kChDdl52[i][j];
677 Int_t irownew = row%16;
678 Int_t icolnew = col%4;
680 chno = iCh[irownew][icolnew];
683 for (Int_t ibus = beginPatchBus; ibus <= endPatchBus; ibus++)
685 Int_t srow = startRowBus[ibus];
686 Int_t erow = endRowBus[ibus];
687 Int_t scol = startColBus[ibus];
688 Int_t ecol = endColBus[ibus];
689 Int_t tmcm = mcmperBus[ibus];
690 if ((row >= srow && row <= erow) && (col >= scol && col <= ecol))
694 // Find out the MCM Number
697 if (ddlno == 0 || ddlno == 1)
699 // PRE plane, SU Mod = 0, 1
700 mcmno = (col-scol)/4 + 1;
703 else if (ddlno == 2 || ddlno == 3)
705 // PRE plane, SU Mod = 2, 3
706 icolnew = (col - scol)/4;
707 mcmno = tmcm - icolnew;
709 else if (ddlno == 4 )
711 // CPV plane, SU Mod = 0, 3 : ddl = 4
715 Int_t midrow = srow + 16;
716 if(row >= srow && row < midrow)
718 mcmno = 12 + (col-scol)/4 + 1;
720 else if(row >= midrow && row <= erow)
723 mcmno = (col-scol)/4 + 1;
729 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
732 Int_t midrow = srow + 16;
733 if (row >= midrow && row <= erow)
735 mcmno = 12 + (ecol -col)/4 + 1;
737 else if (row >= srow && row < midrow)
739 mcmno = (ecol - col)/4 + 1;
742 else if (rowdiff < 16)
744 mcmno = (ecol - col)/4 + 1;
748 else if ( ddlno == 5)
750 // CPV plane, SU Mod = 1, 2 : ddl = 5
754 Int_t midrow = srow + 16;
755 if(row >= srow && row < midrow)
757 mcmno = 12 + (col-scol)/4 + 1;
759 else if(row >= midrow && row <= erow)
761 mcmno = (col-scol)/4 + 1;
767 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
770 Int_t midrow = srow + 16;
771 if (row >= midrow && row <= erow)
773 mcmno = 12 + (ecol - col)/4 + 1;
775 else if (row >= srow && row < midrow)
777 mcmno = (ecol - col)/4 + 1;
780 else if (rowdiff < 16)
782 mcmno = (ecol - col)/4 + 1;
790 //____________________________________________________________________________
792 Int_t AliPMDDDLRawData::ComputeParity(UInt_t baseword)
794 // Generate the parity bit
797 for(Int_t j=0; j<29; j++)
799 if (baseword & 0x01 ) count++;
802 Int_t parity = count%2;
806 //____________________________________________________________________________