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;
208 // Start writing the DDL file
210 AliPMDBlockHeader blHeader;
211 AliPMDDspHeader dspHeader;
212 AliPMDPatchBusHeader pbusHeader;
214 const Int_t kblHLen = blHeader.GetHeaderLength();
215 const Int_t kdspHLen = dspHeader.GetHeaderLength();
216 const Int_t kpbusHLen = pbusHeader.GetHeaderLength();
219 UInt_t dspBlockHeaderWord[8];
220 UInt_t dspHeaderWord[10];
221 UInt_t patchBusHeaderWord[4];
223 UInt_t ddlEndWord[2] = {0xDEADFACE, 0xDEADFACE};
225 for (Int_t iblock = 0; iblock < 2; iblock++)
229 for (Int_t i=0; i<kblHLen; i++)
231 dspBlockHeaderWord[i] = 0;
235 dspBlockHeaderWord[1] = (UInt_t) (dspBlockARDL + kblHLen);
236 dspBlockHeaderWord[2] = (UInt_t) dspBlockARDL;
238 else if (iblock == 1)
240 dspBlockHeaderWord[1] = (UInt_t) (dspBlockBRDL + kblHLen);
241 dspBlockHeaderWord[2] = (UInt_t) dspBlockBRDL;
244 outfile->WriteBuffer((char*)dspBlockHeaderWord,kblHLen*sizeof(UInt_t));
254 else if (iblock == 1)
263 for (Int_t idsp = 0; idsp < 5; idsp++)
270 dspRDL = (UInt_t) dsp[dspno];
272 else if (iblock == 1)
275 dspRDL = (UInt_t) dsp[dspno];
278 for (Int_t i=0; i<kdspHLen; i++)
280 dspHeaderWord[i] = 0;
282 remainder = dspRDL%2;
283 if (remainder == 1) dspRDL++;
285 dspHeaderWord[1] = dspRDL + kdspHLen;
286 dspHeaderWord[2] = dspRDL;
287 dspHeaderWord[3] = dspno;
288 if (remainder == 1) dspHeaderWord[8] = 1; // setting the padding word
291 outfile->WriteBuffer((char*)dspHeaderWord,kdspHLen*sizeof(UInt_t));
293 for (Int_t ibus = 0; ibus < 5; ibus++)
297 Int_t busno = iskip[idsp] + ibus + 1;
298 Int_t patchbusRDL = contentsBus[busno];
302 patchBusHeaderWord[0] = 0;
303 patchBusHeaderWord[1] = (UInt_t) (patchbusRDL + kpbusHLen);
304 patchBusHeaderWord[2] = (UInt_t) patchbusRDL;
305 patchBusHeaderWord[3] = (UInt_t) busno;
307 else if (patchbusRDL == 0)
309 patchBusHeaderWord[0] = 0;
310 patchBusHeaderWord[1] = (UInt_t) kpbusHLen;
311 patchBusHeaderWord[2] = (UInt_t) 0;
312 patchBusHeaderWord[3] = (UInt_t) busno;
316 outfile->WriteBuffer((char*)patchBusHeaderWord,4*sizeof(UInt_t));
318 for (Int_t iword = 0; iword < patchbusRDL; iword++)
320 buffer[iword] = busPatch[busno][iword];
323 outfile->WriteBuffer((char*)buffer,patchbusRDL*sizeof(UInt_t));
325 } // End of patch bus loop
328 // Adding a padding word if the total words odd
331 UInt_t paddingWord = dspHeader.GetDefaultPaddingWord();
332 outfile->WriteBuffer((char*)(&paddingWord),sizeof(UInt_t));
337 // Write two extra word at the end of each DDL file
338 outfile->WriteBuffer((char*)ddlEndWord,2*sizeof(UInt_t));
340 // Write real data header
341 // take the pointer to the beginning of the data header
342 // write the total number of words per ddl and bring the
343 // pointer to the current file position and close it
344 UInt_t cFPosition = outfile->Tellp();
345 sizeRawData = cFPosition - bHPosition - sizeof(header);
347 header.fSize = cFPosition - bHPosition;
348 header.SetAttribute(0); // valid data
349 outfile->Seekp(bHPosition);
350 outfile->WriteBuffer((char*)(&header),sizeof(header));
351 outfile->Seekp(cFPosition);
358 //____________________________________________________________________________
359 void AliPMDDDLRawData::GetUMDigitsData(TTree *treeD, Int_t imodule,
360 Int_t ddlno, Int_t *contentsBus,
361 UInt_t busPatch[][1536])
363 // Retrives digits data UnitModule by UnitModule
368 Int_t det, smn, irow, icol;
371 const Int_t kMaxBus = 51; // BKN
372 Int_t totPatchBus, bPatchBus, ePatchBus;
373 Int_t ibus, totmcm, rows, cols, rowe, cole;
376 Int_t patchBusNo[kMaxBus], mcmperBus[kMaxBus];
377 Int_t startRowBus[kMaxBus], startColBus[kMaxBus];
378 Int_t endRowBus[kMaxBus], endColBus[kMaxBus];
380 Int_t beginPatchBus = -1;
381 Int_t endPatchBus = -1;
382 for(Int_t i = 0; i < kMaxBus; i++)
391 Int_t modulePerDDL = 0;
396 else if (ddlno == 4 || ddlno == 5)
401 TString fileName(gSystem->Getenv("ALICE_ROOT"));
405 fileName += "/PMD/PMD_Mapping_ddl0.dat";
409 fileName += "/PMD/PMD_Mapping_ddl1.dat";
413 fileName += "/PMD/PMD_Mapping_ddl2.dat";
417 fileName += "/PMD/PMD_Mapping_ddl3.dat";
421 fileName += "/PMD/PMD_Mapping_ddl4.dat";
425 fileName += "/PMD/PMD_Mapping_ddl5.dat";
429 infile.open(fileName.Data(), ios::in); // ascii file
431 AliError(Form("Could not read the mapping file for DDL No = %d",ddlno));
433 for (Int_t im = 0; im < modulePerDDL; im++)
436 infile >> totPatchBus >> bPatchBus >> ePatchBus;
438 if (moduleno == imodule)
440 beginPatchBus = bPatchBus;
441 endPatchBus = ePatchBus;
444 for(Int_t i=0; i<totPatchBus; i++)
446 infile >> ibus >> totmcm >> rows >> rowe >> cols >> cole;
448 if (moduleno == imodule)
450 patchBusNo[ibus] = ibus;
451 mcmperBus[ibus] = totmcm;
452 startRowBus[ibus] = rows;
453 startColBus[ibus] = cols;
454 endRowBus[ibus] = rowe;
455 endColBus[ibus] = cole;
463 treeD->GetEntry(imodule);
464 Int_t nentries = fDigits->GetLast();
465 Int_t totword = nentries+1;
467 AliPMDdigit *pmddigit;
469 for (Int_t ient = 0; ient < totword; ient++)
471 pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
473 det = pmddigit->GetDetector();
474 smn = pmddigit->GetSMNumber();
475 irow = pmddigit->GetRow();
476 icol = pmddigit->GetColumn();
477 adc = (UInt_t) pmddigit->GetADC();
479 TransformS2H(smn,irow,icol);
481 GetMCMCh(ddlno, smn, irow, icol, beginPatchBus, endPatchBus,
482 mcmperBus, startRowBus, startColBus,
483 endRowBus, endColBus, busno, mcmno, chno);
486 AliBitPacking::PackWord(adc,baseword,0,11);
487 AliBitPacking::PackWord(chno,baseword,12,17);
488 AliBitPacking::PackWord(mcmno,baseword,18,28);
489 AliBitPacking::PackWord(0,baseword,29,30);
490 parity = ComputeParity(baseword); // generate the parity bit
491 AliBitPacking::PackWord(parity,baseword,31,31);
493 Int_t jj = contentsBus[busno];
494 busPatch[busno][jj] = baseword;
496 contentsBus[busno]++;
501 //____________________________________________________________________________
502 void AliPMDDDLRawData::TransformS2H(Int_t smn, Int_t &irow, Int_t &icol)
504 // Does the Software to Hardware coordinate transformation
510 // First in digits we have all dimension 48x96
511 // Transform into the realistic one, i.e, For SM 0&1 96(row)x48(col)
512 // and for SM 2&3 48(row)x96(col)
519 else if( smn >= 12 && smn < 24)
531 //____________________________________________________________________________
533 void AliPMDDDLRawData::GetMCMCh(Int_t ddlno, Int_t smn, Int_t row, Int_t col,
534 Int_t beginPatchBus, Int_t endPatchBus,
536 Int_t *startRowBus, Int_t *startColBus,
537 Int_t *endRowBus, Int_t *endColBus,
538 Int_t & busno, UInt_t &mcmno, UInt_t &chno)
540 // This converts row col to hardware channel number
541 // This is the final version of mapping supplied by Mriganka
545 static const UInt_t kChDdl01[16][4] = { {6, 4, 5, 7},
563 static const UInt_t kChDdl23[16][4] = { {57, 59, 58, 56},
581 static const UInt_t kChDdl41[16][4] = { {56, 58, 59, 57},
599 static const UInt_t kChDdl42[16][4] = { {7, 5, 4, 6},
617 static const UInt_t kChDdl51[16][4] = { {7, 5, 4, 6},
636 static const UInt_t kChDdl52[16][4] = { {56, 58, 59, 57},
654 for (Int_t i = 0; i < 16; i++)
656 for (Int_t j = 0; j < 4; j++)
658 if (ddlno == 0 || ddlno == 1) iCh[i][j] = kChDdl01[i][j];
659 if (ddlno == 2 || ddlno == 3) iCh[i][j] = kChDdl23[i][j];
661 if (ddlno == 4 && smn < 6) iCh[i][j] = kChDdl41[i][j];
662 if (ddlno == 4 && (smn >= 18 && smn < 24))iCh[i][j] = kChDdl42[i][j];
663 if (ddlno == 5 && (smn >= 12 && smn < 18))iCh[i][j] = kChDdl51[i][j];
664 if (ddlno == 5 && (smn >= 6 && smn < 12))iCh[i][j] = kChDdl52[i][j];
669 Int_t irownew = row%16;
670 Int_t icolnew = col%4;
672 chno = iCh[irownew][icolnew];
675 for (Int_t ibus = beginPatchBus; ibus <= endPatchBus; ibus++)
677 Int_t srow = startRowBus[ibus];
678 Int_t erow = endRowBus[ibus];
679 Int_t scol = startColBus[ibus];
680 Int_t ecol = endColBus[ibus];
681 Int_t tmcm = mcmperBus[ibus];
682 if ((row >= srow && row <= erow) && (col >= scol && col <= ecol))
686 // Find out the MCM Number
689 if (ddlno == 0 || ddlno == 1)
691 // PRE plane, SU Mod = 0, 1
692 mcmno = (col-scol)/4 + 1;
695 else if (ddlno == 2 || ddlno == 3)
697 // PRE plane, SU Mod = 2, 3
698 icolnew = (col - scol)/4;
699 mcmno = tmcm - icolnew;
701 else if (ddlno == 4 )
703 // CPV plane, SU Mod = 0, 3 : ddl = 4
707 Int_t midrow = srow + 16;
708 if(row >= srow && row < midrow)
710 mcmno = 12 + (col-scol)/4 + 1;
712 else if(row >= midrow && row <= erow)
715 mcmno = (col-scol)/4 + 1;
721 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
724 Int_t midrow = srow + 16;
725 if (row >= midrow && row <= erow)
727 mcmno = 12 + (ecol -col)/4 + 1;
729 else if (row >= srow && row < midrow)
731 mcmno = (ecol - col)/4 + 1;
734 else if (rowdiff < 16)
736 mcmno = (ecol - col)/4 + 1;
740 else if ( ddlno == 5)
742 // CPV plane, SU Mod = 1, 2 : ddl = 5
746 Int_t midrow = srow + 16;
747 if(row >= srow && row < midrow)
749 mcmno = 12 + (col-scol)/4 + 1;
751 else if(row >= midrow && row <= erow)
753 mcmno = (col-scol)/4 + 1;
759 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
762 Int_t midrow = srow + 16;
763 if (row >= midrow && row <= erow)
765 mcmno = 12 + (ecol - col)/4 + 1;
767 else if (row >= srow && row < midrow)
769 mcmno = (ecol - col)/4 + 1;
772 else if (rowdiff < 16)
774 mcmno = (ecol - col)/4 + 1;
782 //____________________________________________________________________________
784 Int_t AliPMDDDLRawData::ComputeParity(UInt_t baseword)
786 // Generate the parity bit
789 for(Int_t j=0; j<29; j++)
791 if (baseword & 0x01 ) count++;
794 Int_t parity = count%2;
798 //____________________________________________________________________________