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
383 Int_t det, smn, irow, icol;
386 const Int_t kMaxBus = 51; // BKN
387 Int_t totPatchBus, bPatchBus, ePatchBus;
388 Int_t ibus, totmcm, rows, cols, rowe, cole;
391 Int_t patchBusNo[kMaxBus], mcmperBus[kMaxBus];
392 Int_t startRowBus[kMaxBus], startColBus[kMaxBus];
393 Int_t endRowBus[kMaxBus], endColBus[kMaxBus];
395 Int_t beginPatchBus = -1;
396 Int_t endPatchBus = -1;
397 for(Int_t i = 0; i < kMaxBus; i++)
408 TString fileName(gSystem->Getenv("ALICE_ROOT"));
412 fileName += "/PMD/PMD_Mapping_ddl0.dat";
416 fileName += "/PMD/PMD_Mapping_ddl1.dat";
420 fileName += "/PMD/PMD_Mapping_ddl2.dat";
424 fileName += "/PMD/PMD_Mapping_ddl3.dat";
428 fileName += "/PMD/PMD_Mapping_ddl4.dat";
432 fileName += "/PMD/PMD_Mapping_ddl5.dat";
436 infile.open(fileName.Data(), ios::in); // ascii file
438 AliError(Form("Could not read the mapping file for DDL No = %d",ddlno));
440 for (Int_t im = 0; im < modulePerDDL; im++)
443 infile >> totPatchBus >> bPatchBus >> ePatchBus;
445 if (totPatchBus == 0) continue; // BKN
447 if (moduleno == imodule)
449 beginPatchBus = bPatchBus;
450 endPatchBus = ePatchBus;
453 for(Int_t i=0; i<totPatchBus; i++)
455 infile >> ibus >> totmcm >> rows >> rowe >> cols >> cole;
457 if (moduleno == imodule)
459 patchBusNo[ibus] = ibus;
460 mcmperBus[ibus] = totmcm;
461 startRowBus[ibus] = rows;
462 startColBus[ibus] = cols;
463 endRowBus[ibus] = rowe;
464 endColBus[ibus] = cole;
472 // Read if some chains are off
473 TString rchainName(gSystem->Getenv("ALICE_ROOT"));
474 rchainName += "/PMD/PMD_removed_chains.dat";
477 rchainfile.open(rchainName.Data(), ios::in); // ascii file
478 if(!rchainfile)AliError("Could not read the removed cahins file");
480 Int_t srowoff1[2][24], erowoff1[2][24];
481 Int_t scoloff1[2][24], ecoloff1[2][24];
482 Int_t srowoff2[2][24], erowoff2[2][24];
483 Int_t scoloff2[2][24], ecoloff2[2][24];
485 Int_t rows1, rowe1, cols1, cole1;
486 Int_t rows2, rowe2, cols2, cole2;
488 for (Int_t im = 0; im < 48; im++)
490 rchainfile >> det >> smn >> rows1 >> rowe1 >> cols1 >> cole1
491 >> rows2 >> rowe2 >> cols2 >> cole2;
493 srowoff1[det][smn] = rows1;
494 erowoff1[det][smn] = rowe1;
495 scoloff1[det][smn] = cols1;
496 ecoloff1[det][smn] = cole1;
497 srowoff2[det][smn] = rows2;
498 erowoff2[det][smn] = rowe2;
499 scoloff2[det][smn] = cols2;
500 ecoloff2[det][smn] = cole2;
505 treeD->GetEntry(imodule);
506 Int_t nentries = fDigits->GetLast();
507 Int_t totword = nentries+1;
509 AliPMDdigit *pmddigit;
511 for (Int_t ient = 0; ient < totword; ient++)
513 pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
515 det = pmddigit->GetDetector();
516 smn = pmddigit->GetSMNumber();
517 irow = pmddigit->GetRow();
518 icol = pmddigit->GetColumn();
519 adc = (UInt_t) pmddigit->GetADC();
521 TransformS2H(smn,irow,icol);
523 // remove the non-existence channels
525 //printf("%d %d %d %d\n",det,smn,irow,icol);
526 //printf("--- %d %d %d %d\n",srowoff[det][smn],erowoff[det][smn],
527 // scoloff[det][smn],ecoloff[det][smn]);
529 if (irow >= srowoff1[det][smn] && irow <= erowoff1[det][smn])
531 if (icol >= scoloff1[det][smn] && icol <= ecoloff1[det][smn])
536 if (irow >= srowoff2[det][smn] && irow <= erowoff2[det][smn])
538 if (icol >= scoloff2[det][smn] && icol <= ecoloff2[det][smn])
545 GetMCMCh(imodule, irow, icol, beginPatchBus, endPatchBus,
546 mcmperBus, startRowBus, startColBus,
547 endRowBus, endColBus, busno, mcmno, chno);
550 AliBitPacking::PackWord(adc,baseword,0,11);
551 AliBitPacking::PackWord(chno,baseword,12,17);
552 AliBitPacking::PackWord(mcmno,baseword,18,28);
553 AliBitPacking::PackWord(0,baseword,29,30);
554 parity = ComputeParity(baseword); // generate the parity bit
555 AliBitPacking::PackWord(parity,baseword,31,31);
557 Int_t jj = contentsBus[busno];
558 busPatch[busno][jj] = baseword;
560 contentsBus[busno]++;
565 //____________________________________________________________________________
566 void AliPMDDDLRawData::TransformS2H(Int_t smn, Int_t &irow, Int_t &icol)
568 // Does the Software to Hardware coordinate transformation
574 // First in digits we have all dimension 48x96
575 // Transform into the realistic one, i.e, For SM 0&1 96(row)x48(col)
576 // and for SM 2&3 48(row)x96(col)
583 else if( smn >= 12 && smn < 24)
595 //____________________________________________________________________________
597 void AliPMDDDLRawData::GetMCMCh(Int_t imodule, Int_t row, Int_t col,
598 Int_t beginPatchBus, Int_t endPatchBus,
600 Int_t *startRowBus, Int_t *startColBus,
601 Int_t *endRowBus, Int_t *endColBus,
602 Int_t & busno, UInt_t &mcmno, UInt_t &chno)
604 // This converts row col to hardware channel number
605 // This is the final version of mapping supplied by Mriganka
609 static const UInt_t kChDdl01[16][4] = { {6, 4, 5, 7},
627 static const UInt_t kChDdl23[16][4] = { {57, 59, 58, 56},
645 static const UInt_t kChDdl41[16][4] = { {56, 58, 59, 57},
663 static const UInt_t kChDdl42[16][4] = { {7, 5, 4, 6},
681 static const UInt_t kChDdl51[16][4] = { {7, 5, 4, 6},
700 static const UInt_t kChDdl52[16][4] = { {56, 58, 59, 57},
718 for (Int_t i = 0; i < 16; i++)
720 for (Int_t j = 0; j < 4; j++)
723 if(imodule < 6) iCh[i][j] = kChDdl01[i][j];
724 if(imodule >= 6 && imodule <= 11) iCh[i][j] = kChDdl01[i][j];
725 if(imodule >= 12 && imodule <= 17) iCh[i][j] = kChDdl23[i][j];
726 if(imodule >= 18 && imodule <= 23) iCh[i][j] = kChDdl23[i][j];
728 if(imodule >= 24 && imodule <= 29) iCh[i][j] = kChDdl41[i][j];
729 if(imodule >= 42 && imodule <= 47) iCh[i][j] = kChDdl42[i][j];
730 if(imodule >= 36 && imodule <= 41) iCh[i][j] = kChDdl51[i][j];
731 if(imodule >= 30 && imodule <= 35) iCh[i][j] = kChDdl52[i][j];
737 Int_t irownew = row%16;
738 Int_t icolnew = col%4;
740 chno = iCh[irownew][icolnew];
743 for (Int_t ibus = beginPatchBus; ibus <= endPatchBus; ibus++)
745 Int_t srow = startRowBus[ibus];
746 Int_t erow = endRowBus[ibus];
747 Int_t scol = startColBus[ibus];
748 Int_t ecol = endColBus[ibus];
749 Int_t tmcm = mcmperBus[ibus];
751 if ((row >= srow && row <= erow) && (col >= scol && col <= ecol))
755 // Find out the MCM Number
758 if (imodule < 6) mcmno = (col-scol)/4 + 1;
759 if (imodule >= 6 && imodule < 12) mcmno = (col-scol)/4 + 1;
761 if (imodule >= 12 && imodule < 18)
763 icolnew = (col - scol)/4;
764 mcmno = tmcm - icolnew;
766 if (imodule >= 18 && imodule < 24)
768 icolnew = (col - scol)/4;
769 mcmno = tmcm - icolnew;
773 if (imodule >= 24 && imodule < 30)
777 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
780 Int_t midrow = srow + 16;
781 if(row >= srow && row < midrow)
783 mcmno = 12 + (col-scol)/4 + 1;
785 else if(row >= midrow && row <= erow)
788 mcmno = (col-scol)/4 + 1;
791 else if (rowdiff < 16)
793 mcmno = (col-scol)/4 + 1;
797 if (imodule >= 42 && imodule < 48)
799 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
802 Int_t midrow = srow + 16;
803 if (row >= midrow && row <= erow)
805 mcmno = 12 + (ecol -col)/4 + 1;
807 else if (row >= srow && row < midrow)
809 mcmno = (ecol - col)/4 + 1;
812 else if (rowdiff < 16)
814 mcmno = (ecol - col)/4 + 1;
819 if (imodule >= 30 && imodule < 36)
821 // CPV plane, SU Mod = 1, 2 : ddl = 5
824 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
827 Int_t midrow = srow + 16;
828 if(row >= srow && row < midrow)
830 mcmno = 12 + (col-scol)/4 + 1;
832 else if(row >= midrow && row <= erow)
834 mcmno = (col-scol)/4 + 1;
837 else if(rowdiff < 16)
839 mcmno = (col-scol)/4 + 1;
843 if (imodule >= 36 && imodule < 42)
845 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
848 Int_t midrow = srow + 16;
849 if (row >= midrow && row <= erow)
851 mcmno = 12 + (ecol - col)/4 + 1;
853 else if (row >= srow && row < midrow)
855 mcmno = (ecol - col)/4 + 1;
858 else if (rowdiff < 16)
860 mcmno = (ecol - col)/4 + 1;
868 //____________________________________________________________________________
870 Int_t AliPMDDDLRawData::ComputeParity(UInt_t baseword)
872 // Generate the parity bit
875 for(Int_t j=0; j<29; j++)
877 if (baseword & 0x01 ) count++;
880 Int_t parity = count%2;
884 //____________________________________________________________________________