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 "AliRawDataHeader.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"
35 ClassImp(AliPMDDDLRawData)
37 AliPMDDDLRawData::AliPMDDDLRawData():
38 fDigits(new TClonesArray("AliPMDdigit", 1000))
40 // Default Constructor
44 //____________________________________________________________________________
45 AliPMDDDLRawData::AliPMDDDLRawData(const AliPMDDDLRawData& ddlraw):
47 fDigits(ddlraw.fDigits)
51 //____________________________________________________________________________
52 AliPMDDDLRawData & AliPMDDDLRawData::operator=(const AliPMDDDLRawData& ddlraw)
57 fDigits = ddlraw.fDigits;
61 //____________________________________________________________________________
63 AliPMDDDLRawData::~AliPMDDDLRawData()
70 //____________________________________________________________________________
71 void AliPMDDDLRawData::WritePMDRawData(TTree *treeD)
73 // write digits into raw data format
77 TBranch *branch = treeD->GetBranch("PMDDigit");
80 AliError("PMD Digit branch not found");
83 branch->SetAddress(&fDigits);
85 Int_t nmodules = (Int_t) treeD->GetEntries();
86 AliDebug(1,Form("Number of modules inside treeD = %d",nmodules));
88 const Int_t kDDL = AliDAQ::NumberOfDdls("PMD");
89 Int_t modulePerDDL = 0;
92 AliRawDataHeader header;
93 UInt_t sizeRawData = 0;
95 const Int_t kSize = 1536;
98 UInt_t busPatch[50][1536];
100 Int_t contentsBus[50];
106 for(Int_t iddl = 0; iddl < kDDL; iddl++)
108 strcpy(filename,AliDAQ::DdlFileName("PMD",iddl));
110 outfile.open(filename,ios::binary);
112 outfile.open(filename);
133 // Write the Dummy Data Header into the file
134 Int_t bHPosition = outfile.tellp();
135 outfile.write((char*)(&header),sizeof(header));
137 for (Int_t ibus = 0; ibus < 50; ibus++)
139 contentsBus[ibus] = 0;
140 for (Int_t ich = 0; ich < 1536; ich++)
142 busPatch[ibus][ich] = 0;
146 for(Int_t ium = 0; ium < modulePerDDL; ium++)
148 if (iddl == 4 && ium == 6) mmodule = 42;
150 // Extract energy deposition per cell and pack it
151 // in a 32-bit word and returns all the total words
152 // per one unit-module
154 GetUMDigitsData(treeD, mmodule, iddl, contentsBus, busPatch);
163 for (Int_t i = 0; i < 10; i++)
167 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;
183 for (Int_t i = 0; i < 5; i++)
186 Int_t iodd = 2*i + 1;
189 dspBlockARDL += dsp[ieven];
190 Int_t remainder = dsp[ieven]%2;
198 dspBlockBRDL += dsp[iodd];
199 Int_t remainder = dsp[ieven]%2;
207 // Start writing the DDL file
209 AliPMDBlockHeader blHeader;
210 AliPMDDspHeader dspHeader;
211 AliPMDPatchBusHeader pbusHeader;
213 const Int_t kblHLen = blHeader.GetHeaderLength();
214 const Int_t kdspHLen = dspHeader.GetHeaderLength();
215 const Int_t kpbusHLen = pbusHeader.GetHeaderLength();
218 UInt_t dspBlockHeaderWord[8];
219 UInt_t dspHeaderWord[10];
220 UInt_t patchBusHeaderWord[4];
224 for (Int_t iblock = 0; iblock < 2; iblock++)
228 for (Int_t i=0; i<kblHLen; i++)
230 dspBlockHeaderWord[i] = 0;
234 dspBlockHeaderWord[1] = (UInt_t) (dspBlockARDL + kblHLen);
235 dspBlockHeaderWord[2] = (UInt_t) dspBlockARDL;
237 else if (iblock == 1)
239 dspBlockHeaderWord[1] = (UInt_t) (dspBlockBRDL + kblHLen);
240 dspBlockHeaderWord[2] = (UInt_t) dspBlockBRDL;
243 outfile.write((char*)dspBlockHeaderWord,kblHLen*sizeof(UInt_t));
253 else if (iblock == 1)
262 for (Int_t idsp = 0; idsp < 5; idsp++)
269 dspRDL = (UInt_t) dsp[dspno];
271 else if (iblock == 1)
274 dspRDL = (UInt_t) dsp[dspno];
277 for (Int_t i=0; i<kdspHLen; i++)
279 dspHeaderWord[i] = 0;
281 Int_t remainder = dspRDL%2;
282 if (remainder == 1) dspRDL++;
284 dspHeaderWord[1] = dspRDL + kdspHLen;
285 dspHeaderWord[2] = dspRDL;
286 dspHeaderWord[3] = dspno;
287 if (remainder == 1) dspHeaderWord[8] = 1; // setting the padding word
288 outfile.write((char*)dspHeaderWord,kdspHLen*sizeof(UInt_t));
290 for (Int_t ibus = 0; ibus < 5; ibus++)
293 Int_t busno = iskip[idsp] + ibus;
294 Int_t patchbusRDL = contentsBus[busno];
298 patchBusHeaderWord[0] = 0;
299 patchBusHeaderWord[1] = (UInt_t) (patchbusRDL + kpbusHLen);
300 patchBusHeaderWord[2] = (UInt_t) patchbusRDL;
301 patchBusHeaderWord[3] = (UInt_t) busno;
303 else if (patchbusRDL == 0)
305 patchBusHeaderWord[0] = 0;
306 patchBusHeaderWord[1] = (UInt_t) kpbusHLen;
307 patchBusHeaderWord[2] = (UInt_t) 0;
308 patchBusHeaderWord[3] = (UInt_t) busno;
312 outfile.write((char*)patchBusHeaderWord,4*sizeof(UInt_t));
315 for (Int_t iword = 0; iword < patchbusRDL; iword++)
317 buffer[iword] = busPatch[busno][iword];
320 outfile.write((char*)buffer,patchbusRDL*sizeof(UInt_t));
322 } // End of patch bus loop
325 // Adding a padding word if the total words odd
328 UInt_t paddingWord = dspHeader.GetDefaultPaddingWord();
329 outfile.write((char*)(&paddingWord),sizeof(UInt_t));
334 // Write real data header
335 // take the pointer to the beginning of the data header
336 // write the total number of words per ddl and bring the
337 // pointer to the current file position and close it
338 UInt_t cFPosition = outfile.tellp();
339 sizeRawData = cFPosition - bHPosition - sizeof(header);
341 header.fSize = cFPosition - bHPosition;
342 header.SetAttribute(0); // valid data
343 outfile.seekp(bHPosition);
344 outfile.write((char*)(&header),sizeof(header));
345 outfile.seekp(cFPosition);
352 //____________________________________________________________________________
353 void AliPMDDDLRawData::GetUMDigitsData(TTree *treeD, Int_t imodule,
354 Int_t ddlno, Int_t *contentsBus,
355 UInt_t busPatch[][1536])
357 // Retrives digits data UnitModule by UnitModule
362 Int_t det, smn, irow, icol;
365 const Int_t kMaxBus = 50;
366 Int_t totPatchBus, bPatchBus, ePatchBus;
367 Int_t ibus, totmcm, rows, cols, rowe, cole;
370 Int_t patchBusNo[kMaxBus], mcmperBus[kMaxBus];
371 Int_t startRowBus[kMaxBus], startColBus[kMaxBus];
372 Int_t endRowBus[kMaxBus], endColBus[kMaxBus];
374 Int_t beginPatchBus = -1;
375 Int_t endPatchBus = -1;
376 for(Int_t i = 0; i < kMaxBus; i++)
385 Int_t modulePerDDL = 0;
390 else if (ddlno == 4 || ddlno == 5)
395 TString fileName(gSystem->Getenv("ALICE_ROOT"));
399 fileName += "/PMD/PMD_Mapping_ddl0.dat";
403 fileName += "/PMD/PMD_Mapping_ddl1.dat";
407 fileName += "/PMD/PMD_Mapping_ddl2.dat";
411 fileName += "/PMD/PMD_Mapping_ddl3.dat";
415 fileName += "/PMD/PMD_Mapping_ddl4.dat";
419 fileName += "/PMD/PMD_Mapping_ddl5.dat";
423 infile.open(fileName.Data(), ios::in); // ascii file
425 AliError(Form("Could not read the mapping file for DDL No = %d",ddlno));
427 for (Int_t im = 0; im < modulePerDDL; im++)
430 infile >> totPatchBus >> bPatchBus >> ePatchBus;
432 if (moduleno == imodule)
434 beginPatchBus = bPatchBus;
435 endPatchBus = ePatchBus;
438 for(Int_t i=0; i<totPatchBus; i++)
440 infile >> ibus >> totmcm >> rows >> rowe >> cols >> cole;
442 if (moduleno == imodule)
444 patchBusNo[ibus] = ibus;
445 mcmperBus[ibus] = totmcm;
446 startRowBus[ibus] = rows;
447 startColBus[ibus] = cols;
448 endRowBus[ibus] = rowe;
449 endColBus[ibus] = cole;
457 treeD->GetEntry(imodule);
458 Int_t nentries = fDigits->GetLast();
459 Int_t totword = nentries+1;
461 AliPMDdigit *pmddigit;
463 for (Int_t ient = 0; ient < totword; ient++)
465 pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
467 det = pmddigit->GetDetector();
468 smn = pmddigit->GetSMNumber();
469 irow = pmddigit->GetRow();
470 icol = pmddigit->GetColumn();
471 adc = (UInt_t) pmddigit->GetADC();
473 TransformS2H(smn,irow,icol);
475 GetMCMCh(ddlno, irow, icol, beginPatchBus, endPatchBus,
476 mcmperBus, startRowBus, startColBus,
477 endRowBus, endColBus, busno, mcmno, chno);
480 AliBitPacking::PackWord(adc,baseword,0,11);
481 AliBitPacking::PackWord(chno,baseword,12,17);
482 AliBitPacking::PackWord(mcmno,baseword,18,28);
483 AliBitPacking::PackWord(0,baseword,29,30);
484 parity = ComputeParity(baseword); // generate the parity bit
485 AliBitPacking::PackWord(parity,baseword,31,31);
487 Int_t jj = contentsBus[busno];
488 busPatch[busno][jj] = baseword;
490 contentsBus[busno]++;
495 //____________________________________________________________________________
496 void AliPMDDDLRawData::TransformS2H(Int_t smn, Int_t &irow, Int_t &icol)
498 // Does the Software to Hardware coordinate transformation
504 // First in digits we have all dimension 48x96
505 // Transform into the realistic one, i.e, For SM 0&1 96(row)x48(col)
506 // and for SM 2&3 48(row)x96(col)
513 else if( smn >= 12 && smn < 24)
525 //____________________________________________________________________________
527 void AliPMDDDLRawData::GetMCMCh(Int_t ddlno, Int_t row, Int_t col,
528 Int_t beginPatchBus, Int_t endPatchBus,
530 Int_t *startRowBus, Int_t *startColBus,
531 Int_t *endRowBus, Int_t *endColBus,
532 Int_t & busno, UInt_t &mcmno, UInt_t &chno)
534 // This converts row col to hardware channel number
535 // This is the final version of mapping supplied by Mriganka
537 static const UInt_t kCh[16][4] = { {56, 58, 59, 57},
554 Int_t irownew = row%16;
555 Int_t icolnew = col%4;
557 chno = kCh[irownew][icolnew];
560 for (Int_t ibus = beginPatchBus; ibus <= endPatchBus; ibus++)
562 Int_t srow = startRowBus[ibus];
563 Int_t erow = endRowBus[ibus];
564 Int_t scol = startColBus[ibus];
565 Int_t ecol = endColBus[ibus];
566 Int_t tmcm = mcmperBus[ibus];
567 if ((row >= srow && row <= erow) && (col >= scol && col <= ecol))
571 // Find out the MCM Number
574 if (ddlno == 0 || ddlno == 1)
576 // PRE plane, SU Mod = 0, 1
577 mcmno = (col-scol)/4;
580 else if (ddlno == 2 || ddlno == 3)
582 // PRE plane, SU Mod = 2, 3
583 Int_t icolnew = (col - scol)/4;
584 mcmno = tmcm - 1 - icolnew;
586 else if (ddlno == 4 )
588 // CPV plane, SU Mod = 0, 3 : ddl = 4
592 Int_t midrow = srow + 16;
593 if(row >= srow && row < midrow)
595 mcmno = 12 + (col-scol)/4;
597 else if(row >= midrow && row <= erow)
600 mcmno = (col-scol)/4;
606 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
609 Int_t midrow = srow + 16;
610 if (row >= midrow && row <= erow)
612 mcmno = 12 + (ecol -col)/4;
614 else if (row >= srow && row < midrow)
616 mcmno = (ecol - col)/4;
619 else if (rowdiff < 16)
621 mcmno = (ecol - col)/4;
625 else if ( ddlno == 5)
627 // CPV plane, SU Mod = 0, 3 : ddl = 4
631 Int_t midrow = srow + 16;
632 if(row >= srow && row < midrow)
634 mcmno = 12 + (col-scol)/4;
636 else if(row >= midrow && row <= erow)
638 mcmno = (col-scol)/4;
644 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
647 Int_t midrow = srow + 16;
648 if (row >= midrow && row <= erow)
650 mcmno = 12 + (ecol -col)/4;
652 else if (row >= srow && row < midrow)
654 mcmno = (ecol - col)/4;
657 else if (rowdiff < 16)
659 mcmno = (ecol - col)/4;
667 //____________________________________________________________________________
669 Int_t AliPMDDDLRawData::ComputeParity(UInt_t baseword)
671 // Generate the parity bit
674 for(Int_t j=0; j<29; j++)
676 if (baseword & 0x01 ) count++;
679 Int_t parity = count%2;
683 //____________________________________________________________________________