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];
106 // open the ddl file info to know the modules per DDL
107 TString ddlinfofileName(gSystem->Getenv("ALICE_ROOT"));
108 ddlinfofileName += "/PMD/PMD_ddl_info.dat";
111 infileddl.open(ddlinfofileName.Data(), ios::in); // ascii file
112 if(!infileddl) AliError("Could not read the ddl info file");
119 Int_t *modulenoddl = 0x0;
121 for(Int_t iddl = 0; iddl < kDDL; iddl++)
123 if (infileddl.eof()) break;
124 infileddl >> ddlno >> modulePerDDL;
126 if (modulePerDDL == 0) continue;
128 modulenoddl = new Int_t [modulePerDDL];
129 for (Int_t im = 0; im < modulePerDDL; im++)
132 modulenoddl[im] = modno;
135 strcpy(filename,AliDAQ::DdlFileName("PMD",iddl));
137 outfile = new AliFstream(filename);
139 // Write the Dummy Data Header into the file
140 Int_t bHPosition = outfile->Tellp();
141 outfile->WriteBuffer((char*)(&header),sizeof(header));
143 for (Int_t ibus = 0; ibus < kbusSize; ibus++)
145 contentsBus[ibus] = 0;
146 for (Int_t ich = 0; ich < kSize; ich++)
148 busPatch[ibus][ich] = 0;
152 for(Int_t ium = 0; ium < modulePerDDL; ium++)
154 //if (iddl == 4 && ium == 6) mmodule = 42;
156 // Extract energy deposition per cell and pack it
157 // in a 32-bit word and returns all the total words
158 // per one unit-module
160 mmodule = modulenoddl[ium];
161 GetUMDigitsData(treeD, mmodule, iddl, modulePerDDL, contentsBus, busPatch);
167 for (Int_t i = 0; i < 10; i++)
171 for (Int_t ibus=0; ibus < 5; ibus++)
174 if (contentsBus[ij] > 0)
176 dsp[i] += contentsBus[ij];
180 // Add the patch Bus header to the DSP contents
181 dsp[i] += 4*dspBus[i];
184 Int_t dspBlockARDL = 0;
185 Int_t dspBlockBRDL = 0;
189 for (Int_t i = 0; i < 5; i++)
192 Int_t iodd = 2*i + 1;
195 dspBlockARDL += dsp[ieven];
196 remainder = dsp[ieven]%2;
204 dspBlockBRDL += dsp[iodd];
205 remainder = dsp[iodd]%2;
216 // Start writing the DDL file
218 AliPMDBlockHeader blHeader;
219 AliPMDDspHeader dspHeader;
220 AliPMDPatchBusHeader pbusHeader;
222 const Int_t kblHLen = blHeader.GetHeaderLength();
223 const Int_t kdspHLen = dspHeader.GetHeaderLength();
224 const Int_t kpbusHLen = pbusHeader.GetHeaderLength();
227 UInt_t dspBlockHeaderWord[8];
228 UInt_t dspHeaderWord[10];
229 UInt_t patchBusHeaderWord[4];
231 UInt_t ddlEndWord[2] = {0xDEADFACE, 0xDEADFACE};
236 for (Int_t iblock = 0; iblock < 2; iblock++)
240 for (Int_t i=0; i<kblHLen; i++)
242 dspBlockHeaderWord[i] = 0;
246 dspBlockHeaderWord[1] = (UInt_t) (dspBlockARDL + kblHLen);
247 dspBlockHeaderWord[2] = (UInt_t) dspBlockARDL;
249 else if (iblock == 1)
251 dspBlockHeaderWord[1] = (UInt_t) (dspBlockBRDL + kblHLen);
252 dspBlockHeaderWord[2] = (UInt_t) dspBlockBRDL;
255 outfile->WriteBuffer((char*)dspBlockHeaderWord,kblHLen*sizeof(UInt_t));
265 else if (iblock == 1)
274 for (Int_t idsp = 0; idsp < 5; idsp++)
281 dspRDL = (UInt_t) dsp[dspno];
283 else if (iblock == 1)
286 dspRDL = (UInt_t) dsp[dspno];
289 for (Int_t i=0; i<kdspHLen; i++)
291 dspHeaderWord[i] = 0;
293 remainder = dspRDL%2;
294 if (remainder == 1) dspRDL++;
296 dspHeaderWord[1] = dspRDL + kdspHLen;
297 dspHeaderWord[2] = dspRDL;
298 dspHeaderWord[3] = dspno;
299 if (remainder == 1) dspHeaderWord[8] = 1; // setting the padding word
302 outfile->WriteBuffer((char*)dspHeaderWord,kdspHLen*sizeof(UInt_t));
304 for (Int_t ibus = 0; ibus < 5; ibus++)
308 Int_t busno = iskip[idsp] + ibus + 1;
309 Int_t patchbusRDL = contentsBus[busno];
313 patchBusHeaderWord[0] = 0;
314 patchBusHeaderWord[1] = (UInt_t) (patchbusRDL + kpbusHLen);
315 patchBusHeaderWord[2] = (UInt_t) patchbusRDL;
316 patchBusHeaderWord[3] = (UInt_t) busno;
318 else if (patchbusRDL == 0)
320 patchBusHeaderWord[0] = 0;
321 patchBusHeaderWord[1] = (UInt_t) kpbusHLen;
322 patchBusHeaderWord[2] = (UInt_t) 0;
323 patchBusHeaderWord[3] = (UInt_t) busno;
327 outfile->WriteBuffer((char*)patchBusHeaderWord,4*sizeof(UInt_t));
329 bknJunk += patchbusRDL;
331 for (Int_t iword = 0; iword < patchbusRDL; iword++)
333 buffer[iword] = busPatch[busno][iword];
336 outfile->WriteBuffer((char*)buffer,patchbusRDL*sizeof(UInt_t));
338 } // End of patch bus loop
341 // Adding a padding word if the total words odd
344 UInt_t paddingWord = dspHeader.GetDefaultPaddingWord();
345 outfile->WriteBuffer((char*)(&paddingWord),sizeof(UInt_t));
350 // Write two extra word at the end of each DDL file
351 outfile->WriteBuffer((char*)ddlEndWord,2*sizeof(UInt_t));
353 // Write real data header
354 // take the pointer to the beginning of the data header
355 // write the total number of words per ddl and bring the
356 // pointer to the current file position and close it
357 UInt_t cFPosition = outfile->Tellp();
358 sizeRawData = cFPosition - bHPosition - sizeof(header);
360 header.fSize = cFPosition - bHPosition;
361 header.SetAttribute(0); // valid data
362 outfile->Seekp(bHPosition);
363 outfile->WriteBuffer((char*)(&header),sizeof(header));
364 outfile->Seekp(cFPosition);
369 delete [] modulenoddl;
372 //____________________________________________________________________________
373 void AliPMDDDLRawData::GetUMDigitsData(TTree *treeD, Int_t imodule,
374 Int_t ddlno, Int_t modulePerDDL,
376 UInt_t busPatch[][1536])
378 // Retrieves digits data UnitModule by UnitModule
380 const Int_t kMaxBus = 51;
383 UInt_t mcmno = 0, chno = 0;
385 Int_t det = 0, smn = 0, irow = 0, icol = 0;
388 Int_t totPatchBus = 0, bPatchBus = 0, ePatchBus = 0;
389 Int_t ibus = 0, totmcm = 0, rows = 0, cols = 0, rowe = 0, cole = 0;
392 Int_t patchBusNo[kMaxBus], mcmperBus[kMaxBus];
393 Int_t startRowBus[kMaxBus], startColBus[kMaxBus];
394 Int_t endRowBus[kMaxBus], endColBus[kMaxBus];
396 Int_t beginPatchBus = -1;
397 Int_t endPatchBus = -1;
398 for(Int_t i = 0; i < kMaxBus; i++)
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 (totPatchBus == 0) continue; // BKN
448 if (moduleno == imodule)
450 beginPatchBus = bPatchBus;
451 endPatchBus = ePatchBus;
454 for(Int_t i=0; i<totPatchBus; i++)
456 infile >> ibus >> totmcm >> rows >> rowe >> cols >> cole;
458 if (moduleno == imodule)
460 patchBusNo[ibus] = ibus;
461 mcmperBus[ibus] = totmcm;
462 startRowBus[ibus] = rows;
463 startColBus[ibus] = cols;
464 endRowBus[ibus] = rowe;
465 endColBus[ibus] = cole;
473 // Read if some chains are off
474 TString rchainName(gSystem->Getenv("ALICE_ROOT"));
475 rchainName += "/PMD/PMD_removed_chains.dat";
478 rchainfile.open(rchainName.Data(), ios::in); // ascii file
479 if(!rchainfile)AliError("Could not read the removed cahins file");
481 Int_t srowoff1[2][24], erowoff1[2][24];
482 Int_t scoloff1[2][24], ecoloff1[2][24];
483 Int_t srowoff2[2][24], erowoff2[2][24];
484 Int_t scoloff2[2][24], ecoloff2[2][24];
486 Int_t rows1 = 0, rowe1 = 0, cols1 = 0, cole1 = 0;
487 Int_t rows2 = 0, rowe2 = 0, cols2 = 0, cole2 = 0;
489 for (Int_t im = 0; im < 48; im++)
491 rchainfile >> det >> smn >> rows1 >> rowe1 >> cols1 >> cole1
492 >> rows2 >> rowe2 >> cols2 >> cole2;
494 srowoff1[det][smn] = rows1;
495 erowoff1[det][smn] = rowe1;
496 scoloff1[det][smn] = cols1;
497 ecoloff1[det][smn] = cole1;
498 srowoff2[det][smn] = rows2;
499 erowoff2[det][smn] = rowe2;
500 scoloff2[det][smn] = cols2;
501 ecoloff2[det][smn] = cole2;
506 treeD->GetEntry(imodule);
507 Int_t nentries = fDigits->GetLast();
508 Int_t totword = nentries+1;
510 AliPMDdigit *pmddigit = 0x0;
512 for (Int_t ient = 0; ient < totword; ient++)
514 pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
516 det = pmddigit->GetDetector();
517 smn = pmddigit->GetSMNumber();
518 irow = pmddigit->GetRow();
519 icol = pmddigit->GetColumn();
520 adc = (UInt_t) pmddigit->GetADC();
522 TransformS2H(smn,irow,icol);
524 // remove the non-existence channels
526 //printf("%d %d %d %d\n",det,smn,irow,icol);
527 //printf("--- %d %d %d %d\n",srowoff[det][smn],erowoff[det][smn],
528 // scoloff[det][smn],ecoloff[det][smn]);
530 if (irow >= srowoff1[det][smn] && irow <= erowoff1[det][smn])
532 if (icol >= scoloff1[det][smn] && icol <= ecoloff1[det][smn])
537 if (irow >= srowoff2[det][smn] && irow <= erowoff2[det][smn])
539 if (icol >= scoloff2[det][smn] && icol <= ecoloff2[det][smn])
546 GetMCMCh(imodule, irow, icol, beginPatchBus, endPatchBus,
547 mcmperBus, startRowBus, startColBus,
548 endRowBus, endColBus, busno, mcmno, chno);
551 AliBitPacking::PackWord(adc,baseword,0,11);
552 AliBitPacking::PackWord(chno,baseword,12,17);
553 AliBitPacking::PackWord(mcmno,baseword,18,28);
554 AliBitPacking::PackWord(0,baseword,29,30);
555 parity = ComputeParity(baseword); // generate the parity bit
556 AliBitPacking::PackWord(parity,baseword,31,31);
558 Int_t jj = contentsBus[busno];
559 busPatch[busno][jj] = baseword;
561 contentsBus[busno]++;
566 //____________________________________________________________________________
567 void AliPMDDDLRawData::TransformS2H(Int_t smn, Int_t &irow, Int_t &icol)
569 // Does the Software to Hardware coordinate transformation
575 // First in digits we have all dimension 48x96
576 // Transform into the realistic one, i.e, For SM 0&1 96(row)x48(col)
577 // and for SM 2&3 48(row)x96(col)
584 else if( smn >= 12 && smn < 24)
596 //____________________________________________________________________________
598 void AliPMDDDLRawData::GetMCMCh(Int_t imodule, Int_t row, Int_t col,
599 Int_t beginPatchBus, Int_t endPatchBus,
601 Int_t *startRowBus, Int_t *startColBus,
602 Int_t *endRowBus, Int_t *endColBus,
603 Int_t & busno, UInt_t &mcmno, UInt_t &chno)
605 // This converts row col to hardware channel number
606 // This is the final version of mapping supplied by Mriganka
610 static const UInt_t kChDdl01[16][4] = { {6, 4, 5, 7},
628 static const UInt_t kChDdl23[16][4] = { {57, 59, 58, 56},
646 static const UInt_t kChDdl41[16][4] = { {56, 58, 59, 57},
664 static const UInt_t kChDdl42[16][4] = { {7, 5, 4, 6},
682 static const UInt_t kChDdl51[16][4] = { {7, 5, 4, 6},
701 static const UInt_t kChDdl52[16][4] = { {56, 58, 59, 57},
719 for (Int_t i = 0; i < 16; i++)
721 for (Int_t j = 0; j < 4; j++)
724 if(imodule < 6) iCh[i][j] = kChDdl01[i][j];
725 if(imodule >= 6 && imodule <= 11) iCh[i][j] = kChDdl01[i][j];
726 if(imodule >= 12 && imodule <= 17) iCh[i][j] = kChDdl23[i][j];
727 if(imodule >= 18 && imodule <= 23) iCh[i][j] = kChDdl23[i][j];
729 if(imodule >= 24 && imodule <= 29) iCh[i][j] = kChDdl41[i][j];
730 if(imodule >= 42 && imodule <= 47) iCh[i][j] = kChDdl42[i][j];
731 if(imodule >= 36 && imodule <= 41) iCh[i][j] = kChDdl51[i][j];
732 if(imodule >= 30 && imodule <= 35) iCh[i][j] = kChDdl52[i][j];
738 Int_t irownew = row%16;
739 Int_t icolnew = col%4;
741 chno = iCh[irownew][icolnew];
744 for (Int_t ibus = beginPatchBus; ibus <= endPatchBus; ibus++)
746 Int_t srow = startRowBus[ibus];
747 Int_t erow = endRowBus[ibus];
748 Int_t scol = startColBus[ibus];
749 Int_t ecol = endColBus[ibus];
750 Int_t tmcm = mcmperBus[ibus];
752 if ((row >= srow && row <= erow) && (col >= scol && col <= ecol))
756 // Find out the MCM Number
759 if (imodule < 6) mcmno = (col-scol)/4 + 1;
760 if (imodule >= 6 && imodule < 12) mcmno = (col-scol)/4 + 1;
762 if (imodule >= 12 && imodule < 18)
764 icolnew = (col - scol)/4;
765 mcmno = tmcm - icolnew;
767 if (imodule >= 18 && imodule < 24)
769 icolnew = (col - scol)/4;
770 mcmno = tmcm - icolnew;
774 if (imodule >= 24 && imodule < 30)
778 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
781 Int_t midrow = srow + 16;
782 if(row >= srow && row < midrow)
784 mcmno = 12 + (col-scol)/4 + 1;
786 else if(row >= midrow && row <= erow)
789 mcmno = (col-scol)/4 + 1;
792 else if (rowdiff < 16)
794 mcmno = (col-scol)/4 + 1;
798 if (imodule >= 42 && imodule < 48)
800 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
803 Int_t midrow = srow + 16;
804 if (row >= midrow && row <= erow)
806 mcmno = 12 + (ecol -col)/4 + 1;
808 else if (row >= srow && row < midrow)
810 mcmno = (ecol - col)/4 + 1;
813 else if (rowdiff < 16)
815 mcmno = (ecol - col)/4 + 1;
820 if (imodule >= 30 && imodule < 36)
822 // CPV plane, SU Mod = 1, 2 : ddl = 5
825 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
828 Int_t midrow = srow + 16;
829 if(row >= srow && row < midrow)
831 mcmno = 12 + (col-scol)/4 + 1;
833 else if(row >= midrow && row <= erow)
835 mcmno = (col-scol)/4 + 1;
838 else if(rowdiff < 16)
840 mcmno = (col-scol)/4 + 1;
844 if (imodule >= 36 && imodule < 42)
846 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
849 Int_t midrow = srow + 16;
850 if (row >= midrow && row <= erow)
852 mcmno = 12 + (ecol - col)/4 + 1;
854 else if (row >= srow && row < midrow)
856 mcmno = (ecol - col)/4 + 1;
859 else if (rowdiff < 16)
861 mcmno = (ecol - col)/4 + 1;
869 //____________________________________________________________________________
871 Int_t AliPMDDDLRawData::ComputeParity(UInt_t baseword)
873 // Generate the parity bit
876 for(Int_t j=0; j<29; j++)
878 if (baseword & 0x01 ) count++;
881 Int_t parity = count%2;
885 //____________________________________________________________________________