Typo corrected
[u/mrichter/AliRoot.git] / PMD / AliPMDDDLRawData.cxx
CommitLineData
28328eeb 1/***************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
16#include <Riostream.h>
17#include <TClonesArray.h>
18#include <TTree.h>
19#include <TBranch.h>
20#include <TMath.h>
95ec17e6 21#include <TString.h>
22#include <TSystem.h>
28328eeb 23
ecee2a1a 24#include "AliLog.h"
a4e4efaa 25#include "AliRawDataHeader.h"
cff75ba7 26#include "AliBitPacking.h"
28328eeb 27#include "AliPMDdigit.h"
0a666212 28#include "AliPMDBlockHeader.h"
29#include "AliPMDDspHeader.h"
30#include "AliPMDPatchBusHeader.h"
a4e4efaa 31#include "AliPMDRawStream.h"
28328eeb 32#include "AliPMDDDLRawData.h"
362c9d61 33#include "AliDAQ.h"
28328eeb 34
35ClassImp(AliPMDDDLRawData)
36
37AliPMDDDLRawData::AliPMDDDLRawData():
38 fDigits(new TClonesArray("AliPMDdigit", 1000))
39{
40 // Default Constructor
41 //
a4e4efaa 42
28328eeb 43}
44//____________________________________________________________________________
a48edddd 45AliPMDDDLRawData::AliPMDDDLRawData(const AliPMDDDLRawData& ddlraw):
46 TObject(ddlraw),
47 fDigits(ddlraw.fDigits)
48{
49 //Copy Constructor
50}
51//____________________________________________________________________________
52AliPMDDDLRawData & AliPMDDDLRawData::operator=(const AliPMDDDLRawData& ddlraw)
53{
54 //Assignment operator
55 if(this != &ddlraw)
56 {
57 fDigits = ddlraw.fDigits;
58 }
59 return *this;
60}
a48edddd 61//____________________________________________________________________________
28328eeb 62
63AliPMDDDLRawData::~AliPMDDDLRawData()
64{
65 // Default Destructor
66 //
67
68}
69
70//____________________________________________________________________________
a4e4efaa 71void AliPMDDDLRawData::WritePMDRawData(TTree *treeD)
28328eeb 72{
73 // write digits into raw data format
74
75 ofstream outfile;
76
77 TBranch *branch = treeD->GetBranch("PMDDigit");
ecee2a1a 78 if (!branch)
79 {
80 AliError("PMD Digit branch not found");
81 return;
82 }
28328eeb 83 branch->SetAddress(&fDigits);
84
ecee2a1a 85 Int_t nmodules = (Int_t) treeD->GetEntries();
86 AliDebug(1,Form("Number of modules inside treeD = %d",nmodules));
28328eeb 87
362c9d61 88 const Int_t kDDL = AliDAQ::NumberOfDdls("PMD");
28328eeb 89 Int_t modulePerDDL = 0;
90
91
a4e4efaa 92 AliRawDataHeader header;
28328eeb 93 UInt_t sizeRawData = 0;
95ec17e6 94
95 const Int_t kSize = 1536;
28328eeb 96 UInt_t buffer[kSize];
97
95ec17e6 98 UInt_t busPatch[50][1536];
99
100 Int_t contentsBus[50];
101
28328eeb 102 Char_t filename[80];
103
95ec17e6 104
28328eeb 105 Int_t mmodule = 0;
106 for(Int_t iddl = 0; iddl < kDDL; iddl++)
107 {
362c9d61 108 strcpy(filename,AliDAQ::DdlFileName("PMD",iddl));
28328eeb 109#ifndef __DECCXX
110 outfile.open(filename,ios::binary);
111#else
112 outfile.open(filename);
113#endif
114
115 if (iddl < 4)
116 {
117 modulePerDDL = 6;
118 mmodule = 6*iddl;
28328eeb 119 }
120 else if (iddl == 4)
121 {
122 modulePerDDL = 12;
123 mmodule = 24;
28328eeb 124 }
125 else if (iddl == 5)
126 {
127 modulePerDDL = 12;
a4e4efaa 128 mmodule = 30;
28328eeb 129 }
130
28328eeb 131
95ec17e6 132
a4e4efaa 133 // Write the Dummy Data Header into the file
28328eeb 134 Int_t bHPosition = outfile.tellp();
a4e4efaa 135 outfile.write((char*)(&header),sizeof(header));
28328eeb 136
95ec17e6 137 for (Int_t ibus = 0; ibus < 50; ibus++)
138 {
139 contentsBus[ibus] = 0;
140 for (Int_t ich = 0; ich < 1536; ich++)
141 {
142 busPatch[ibus][ich] = 0;
143 }
144 }
28328eeb 145
146 for(Int_t ium = 0; ium < modulePerDDL; ium++)
147 {
95ec17e6 148 if (iddl == 4 && ium == 6) mmodule = 42;
a4e4efaa 149
28328eeb 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
153
95ec17e6 154 GetUMDigitsData(treeD, mmodule, iddl, contentsBus, busPatch);
28328eeb 155 mmodule++;
95ec17e6 156 }
28328eeb 157
95ec17e6 158
159
160 Int_t ij = 0;
161 Int_t dsp[10];
162 Int_t dspBus[10];
163 for (Int_t i = 0; i < 10; i++)
164 {
165 dsp[i] = 0;
166 dspBus[i] = 0;
167 for (Int_t ibus=0; ibus < 5; ibus++)
168 {
169 if (contentsBus[ij] > 0)
170 {
171 dsp[i] += contentsBus[ij];
172 dspBus[i]++;
173 }
174 ij++;
175 }
176 // Add the patch Bus header to the DSP contents
177 dsp[i] += 4*dspBus[i];
178 }
179
0a666212 180 Int_t dspBlockARDL = 0;
181 Int_t dspBlockBRDL = 0;
182
95ec17e6 183 for (Int_t i = 0; i < 5; i++)
184 {
185 Int_t ieven = 2*i;
186 Int_t iodd = 2*i + 1;
187 if (dsp[ieven] > 0)
188 {
189 dspBlockARDL += dsp[ieven];
0a666212 190 Int_t remainder = dsp[ieven]%2;
191 if (remainder == 1)
192 {
193 dspBlockARDL++;
194 }
95ec17e6 195 }
196 if (dsp[iodd] > 0)
197 {
198 dspBlockBRDL += dsp[iodd];
0a666212 199 Int_t remainder = dsp[ieven]%2;
200 if (remainder == 1)
201 {
202 dspBlockARDL++;
203 }
95ec17e6 204 }
28328eeb 205 }
95ec17e6 206
207 // Start writing the DDL file
0a666212 208
209 AliPMDBlockHeader blHeader;
210 AliPMDDspHeader dspHeader;
211 AliPMDPatchBusHeader pbusHeader;
212
213 const Int_t kblHLen = blHeader.GetHeaderLength();
214 const Int_t kdspHLen = dspHeader.GetHeaderLength();
215 const Int_t kpbusHLen = pbusHeader.GetHeaderLength();
216
95ec17e6 217 UInt_t dspRDL = 0;
0a666212 218 UInt_t dspBlockHeaderWord[8];
219 UInt_t dspHeaderWord[10];
220 UInt_t patchBusHeaderWord[4];
95ec17e6 221 Int_t iskip[5];
222
0a666212 223
95ec17e6 224 for (Int_t iblock = 0; iblock < 2; iblock++)
225 {
226 // DSP Block Header
227
0a666212 228 for (Int_t i=0; i<kblHLen; i++)
95ec17e6 229 {
0a666212 230 dspBlockHeaderWord[i] = 0;
231 }
232 if (iblock == 0)
233 {
234 dspBlockHeaderWord[1] = (UInt_t) (dspBlockARDL + kblHLen);
235 dspBlockHeaderWord[2] = (UInt_t) dspBlockARDL;
236 }
237 else if (iblock == 1)
238 {
239 dspBlockHeaderWord[1] = (UInt_t) (dspBlockBRDL + kblHLen);
240 dspBlockHeaderWord[2] = (UInt_t) dspBlockBRDL;
95ec17e6 241 }
242
0a666212 243 outfile.write((char*)dspBlockHeaderWord,kblHLen*sizeof(UInt_t));
95ec17e6 244
245 if (iblock == 0)
246 {
247 iskip[0] = 0;
248 iskip[1] = 10;
249 iskip[2] = 20;
250 iskip[3] = 30;
251 iskip[4] = 40;
252 }
253 else if (iblock == 1)
254 {
255 iskip[0] = 5;
256 iskip[1] = 15;
257 iskip[2] = 25;
258 iskip[3] = 35;
259 iskip[4] = 45;
260 }
28328eeb 261
95ec17e6 262 for (Int_t idsp = 0; idsp < 5; idsp++)
263 {
264 // DSP Header
265 Int_t dspno = 0;
266 if (iblock == 0)
267 {
268 dspno = 2*idsp;
269 dspRDL = (UInt_t) dsp[dspno];
270 }
271 else if (iblock == 1)
272 {
273 dspno = 2*idsp + 1;
274 dspRDL = (UInt_t) dsp[dspno];
275 }
276
0a666212 277 for (Int_t i=0; i<kdspHLen; i++)
95ec17e6 278 {
0a666212 279 dspHeaderWord[i] = 0;
95ec17e6 280 }
0a666212 281 Int_t remainder = dspRDL%2;
282 if (remainder == 1) dspRDL++;
283
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));
289
95ec17e6 290 for (Int_t ibus = 0; ibus < 5; ibus++)
291 {
292 // Patch Bus Header
293 Int_t busno = iskip[idsp] + ibus;
294 Int_t patchbusRDL = contentsBus[busno];
0a666212 295
296 if (patchbusRDL > 0)
95ec17e6 297 {
0a666212 298 patchBusHeaderWord[0] = 0;
299 patchBusHeaderWord[1] = (UInt_t) (patchbusRDL + kpbusHLen);
300 patchBusHeaderWord[2] = (UInt_t) patchbusRDL;
301 patchBusHeaderWord[3] = (UInt_t) busno;
95ec17e6 302 }
0a666212 303 else if (patchbusRDL == 0)
304 {
305 patchBusHeaderWord[0] = 0;
306 patchBusHeaderWord[1] = (UInt_t) kpbusHLen;
307 patchBusHeaderWord[2] = (UInt_t) 0;
308 patchBusHeaderWord[3] = (UInt_t) busno;
309 }
310
311
312 outfile.write((char*)patchBusHeaderWord,4*sizeof(UInt_t));
95ec17e6 313
95ec17e6 314
315 for (Int_t iword = 0; iword < patchbusRDL; iword++)
316 {
317 buffer[iword] = busPatch[busno][iword];
318 }
319
320 outfile.write((char*)buffer,patchbusRDL*sizeof(UInt_t));
0a666212 321
322 } // End of patch bus loop
323
324
325 // Adding a padding word if the total words odd
326 if (remainder == 1)
327 {
328 UInt_t paddingWord = dspHeader.GetDefaultPaddingWord();
329 outfile.write((char*)(&paddingWord),sizeof(UInt_t));
95ec17e6 330 }
331 }
332 }
0a666212 333
a4e4efaa 334 // Write real data header
335 // take the pointer to the beginning of the data header
28328eeb 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();
a4e4efaa 339 sizeRawData = cFPosition - bHPosition - sizeof(header);
95ec17e6 340
a4e4efaa 341 header.fSize = cFPosition - bHPosition;
342 header.SetAttribute(0); // valid data
28328eeb 343 outfile.seekp(bHPosition);
a4e4efaa 344 outfile.write((char*)(&header),sizeof(header));
28328eeb 345 outfile.seekp(cFPosition);
346
347 outfile.close();
348 } // DDL Loop over
349
350
351}
352//____________________________________________________________________________
95ec17e6 353void AliPMDDDLRawData::GetUMDigitsData(TTree *treeD, Int_t imodule,
354 Int_t ddlno, Int_t *contentsBus,
355 UInt_t busPatch[][1536])
28328eeb 356{
e54787da 357 // Retrives digits data UnitModule by UnitModule
93622f4a 358
cff75ba7 359 UInt_t baseword;
28328eeb 360 UInt_t mcmno, chno;
361 UInt_t adc;
28328eeb 362 Int_t det, smn, irow, icol;
a98f9d26 363 Int_t parity;
a0a6a406 364
95ec17e6 365 const Int_t kMaxBus = 50;
366 Int_t totPatchBus, bPatchBus, ePatchBus;
367 Int_t ibus, totmcm, rows, cols, rowe, cole;
368 Int_t moduleno;
369 Int_t busno = 0;
370 Int_t patchBusNo[kMaxBus], mcmperBus[kMaxBus];
371 Int_t startRowBus[kMaxBus], startColBus[kMaxBus];
372 Int_t endRowBus[kMaxBus], endColBus[kMaxBus];
373
374 Int_t beginPatchBus = -1;
375 Int_t endPatchBus = -1;
376 for(Int_t i = 0; i < kMaxBus; i++)
377 {
378 patchBusNo[i] = -1;
379 mcmperBus[i] = -1;
380 startRowBus[i] = -1;
381 startColBus[i] = -1;
382 endRowBus[i] = -1;
383 endColBus[i] = -1;
384 }
385 Int_t modulePerDDL = 0;
386 if (ddlno < 4)
387 {
388 modulePerDDL = 6;
389 }
390 else if (ddlno == 4 || ddlno == 5)
391 {
392 modulePerDDL = 12;
393 }
394
95ec17e6 395 TString fileName(gSystem->Getenv("ALICE_ROOT"));
396
397 if(ddlno == 0)
398 {
399 fileName += "/PMD/PMD_Mapping_ddl0.dat";
400 }
401 else if(ddlno == 1)
402 {
403 fileName += "/PMD/PMD_Mapping_ddl1.dat";
404 }
405 else if(ddlno == 2)
406 {
407 fileName += "/PMD/PMD_Mapping_ddl2.dat";
408 }
409 else if(ddlno == 3)
410 {
411 fileName += "/PMD/PMD_Mapping_ddl3.dat";
412 }
413 else if(ddlno == 4)
414 {
415 fileName += "/PMD/PMD_Mapping_ddl4.dat";
416 }
417 else if(ddlno == 5)
418 {
419 fileName += "/PMD/PMD_Mapping_ddl5.dat";
420 }
421
422 ifstream infile;
423 infile.open(fileName.Data(), ios::in); // ascii file
424 if(!infile)
425 AliError(Form("Could not read the mapping file for DDL No = %d",ddlno));
426
427 for (Int_t im = 0; im < modulePerDDL; im++)
428 {
429 infile >> moduleno;
430 infile >> totPatchBus >> bPatchBus >> ePatchBus;
431
432 if (moduleno == imodule)
433 {
434 beginPatchBus = bPatchBus;
435 endPatchBus = ePatchBus;
436 }
437
438 for(Int_t i=0; i<totPatchBus; i++)
439 {
440 infile >> ibus >> totmcm >> rows >> rowe >> cols >> cole;
441
442 if (moduleno == imodule)
443 {
444 patchBusNo[ibus] = ibus;
445 mcmperBus[ibus] = totmcm;
446 startRowBus[ibus] = rows;
447 startColBus[ibus] = cols;
448 endRowBus[ibus] = rowe;
449 endColBus[ibus] = cole;
450 }
451 }
452
453 }
454
455 infile.close();
456
28328eeb 457 treeD->GetEntry(imodule);
458 Int_t nentries = fDigits->GetLast();
95ec17e6 459 Int_t totword = nentries+1;
28328eeb 460
0a666212 461 AliPMDdigit *pmddigit;
a48edddd 462
28328eeb 463 for (Int_t ient = 0; ient < totword; ient++)
464 {
0a666212 465 pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
28328eeb 466
0a666212 467 det = pmddigit->GetDetector();
468 smn = pmddigit->GetSMNumber();
469 irow = pmddigit->GetRow();
470 icol = pmddigit->GetColumn();
471 adc = (UInt_t) pmddigit->GetADC();
28328eeb 472
e54787da 473 TransformS2H(smn,irow,icol);
95ec17e6 474
475 GetMCMCh(ddlno, irow, icol, beginPatchBus, endPatchBus,
476 mcmperBus, startRowBus, startColBus,
477 endRowBus, endColBus, busno, mcmno, chno);
28328eeb 478
28328eeb 479 baseword = 0;
cff75ba7 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);
a98f9d26 484 parity = ComputeParity(baseword); // generate the parity bit
485 AliBitPacking::PackWord(parity,baseword,31,31);
95ec17e6 486
487 Int_t jj = contentsBus[busno];
488 busPatch[busno][jj] = baseword;
489
490 contentsBus[busno]++;
28328eeb 491 }
492
e54787da 493}
95ec17e6 494
e54787da 495//____________________________________________________________________________
496void AliPMDDDLRawData::TransformS2H(Int_t smn, Int_t &irow, Int_t &icol)
497{
498 // Does the Software to Hardware coordinate transformation
499 //
95ec17e6 500
e54787da 501 Int_t irownew = 0;
502 Int_t icolnew = 0;
e54787da 503
504 // First in digits we have all dimension 48x96
95ec17e6 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)
e54787da 507 //
508 if(smn < 12)
509 {
95ec17e6 510 irownew = icol;
511 icolnew = irow;
e54787da 512 }
513 else if( smn >= 12 && smn < 24)
514 {
95ec17e6 515 irownew = irow;
516 icolnew = icol;
e54787da 517 }
518
519 irow = irownew;
520 icol = icolnew;
28328eeb 521
522}
e54787da 523
524
28328eeb 525//____________________________________________________________________________
e54787da 526
95ec17e6 527void AliPMDDDLRawData::GetMCMCh(Int_t ddlno, Int_t row, Int_t col,
528 Int_t beginPatchBus, Int_t endPatchBus,
529 Int_t *mcmperBus,
530 Int_t *startRowBus, Int_t *startColBus,
531 Int_t *endRowBus, Int_t *endColBus,
532 Int_t & busno, UInt_t &mcmno, UInt_t &chno)
28328eeb 533{
9ac124a2 534 // This converts row col to hardware channel number
535 // This is the final version of mapping supplied by Mriganka
536
537 static const UInt_t kCh[16][4] = { {56, 58, 59, 57},
538 {54, 62, 61, 53},
539 {52, 60, 63, 51},
540 {48, 50, 55, 49},
541 {46, 40, 45, 47},
542 {44, 32, 35, 43},
543 {42, 34, 33, 41},
544 {38, 36, 37, 39},
545 {24, 26, 27, 25},
546 {22, 30, 29, 21},
547 {20, 28, 31, 19},
548 {16, 18, 23, 17},
549 {14, 8, 13, 15},
550 {12, 0, 3, 11},
551 {10, 2, 1, 9},
552 {6, 4, 5, 7} };
28328eeb 553
95ec17e6 554 Int_t irownew = row%16;
555 Int_t icolnew = col%4;
556
557 chno = kCh[irownew][icolnew];
a0a6a406 558
559
95ec17e6 560 for (Int_t ibus = beginPatchBus; ibus <= endPatchBus; ibus++)
561 {
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))
a4e4efaa 568 {
95ec17e6 569 busno = ibus;
a0a6a406 570
95ec17e6 571 // Find out the MCM Number
572 //
a0a6a406 573
95ec17e6 574 if (ddlno == 0 || ddlno == 1)
575 {
576 // PRE plane, SU Mod = 0, 1
a0a6a406 577 mcmno = (col-scol)/4;
578
93622f4a 579 }
a0a6a406 580 else if (ddlno == 2 || ddlno == 3)
95ec17e6 581 {
a0a6a406 582 // PRE plane, SU Mod = 2, 3
95ec17e6 583 Int_t icolnew = (col - scol)/4;
584 mcmno = tmcm - 1 - icolnew;
93622f4a 585 }
586 else if (ddlno == 4 )
95ec17e6 587 {
588 // CPV plane, SU Mod = 0, 3 : ddl = 4
589
a0a6a406 590 if(ibus <= 17)
95ec17e6 591 {
a0a6a406 592 Int_t midrow = srow + 16;
593 if(row >= srow && row < midrow)
95ec17e6 594 {
a0a6a406 595 mcmno = 12 + (col-scol)/4;
95ec17e6 596 }
93622f4a 597 else if(row >= midrow && row <= erow)
598
95ec17e6 599 {
600 mcmno = (col-scol)/4;
601 }
602 }
95ec17e6 603
a0a6a406 604 else if (ibus > 17)
95ec17e6 605 {
606 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
607 if(rowdiff > 16)
608 {
609 Int_t midrow = srow + 16;
93622f4a 610 if (row >= midrow && row <= erow)
95ec17e6 611 {
93622f4a 612 mcmno = 12 + (ecol -col)/4;
95ec17e6 613 }
a0a6a406 614 else if (row >= srow && row < midrow)
95ec17e6 615 {
93622f4a 616 mcmno = (ecol - col)/4;
95ec17e6 617 }
618 }
93622f4a 619 else if (rowdiff < 16)
95ec17e6 620 {
93622f4a 621 mcmno = (ecol - col)/4;
a0a6a406 622 }
95ec17e6 623 }
624 }
93622f4a 625 else if ( ddlno == 5)
626 {
627 // CPV plane, SU Mod = 0, 3 : ddl = 4
628
629 if(ibus <= 17)
630 {
631 Int_t midrow = srow + 16;
632 if(row >= srow && row < midrow)
633 {
634 mcmno = 12 + (col-scol)/4;
635 }
636 else if(row >= midrow && row <= erow)
637 {
638 mcmno = (col-scol)/4;
639 }
640 }
641
642 else if (ibus > 17)
643 {
644 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
645 if(rowdiff > 16)
646 {
647 Int_t midrow = srow + 16;
648 if (row >= midrow && row <= erow)
649 {
650 mcmno = 12 + (ecol -col)/4;
651 }
652 else if (row >= srow && row < midrow)
653 {
654 mcmno = (ecol - col)/4;
655 }
656 }
657 else if (rowdiff < 16)
658 {
659 mcmno = (ecol - col)/4;
660 }
661 }
662 }
663 }
95ec17e6 664 }
a0a6a406 665}
666
28328eeb 667//____________________________________________________________________________
668
a98f9d26 669Int_t AliPMDDDLRawData::ComputeParity(UInt_t baseword)
670{
671 // Generate the parity bit
672
673 Int_t count = 0;
674 for(Int_t j=0; j<29; j++)
675 {
676 if (baseword & 0x01 ) count++;
677 baseword >>= 1;
678 }
679 Int_t parity = count%2;
680 return parity;
681}
682
683//____________________________________________________________________________