]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONRawWriter.cxx
Updates needed for current FLUKA version.
[u/mrichter/AliRoot.git] / MUON / AliMUONRawWriter.cxx
CommitLineData
a19e2543 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
da8d4b6d 16/* $Id$ */
17
a19e2543 18////////////////////////////////////
19//
ced309a5 20// MUON Raw Data generaton in ALICE-MUON
a19e2543 21// This class version 3 (further details could be found in Alice-note)
22//
23// Implemented non-constant buspatch numbers for tracking
24// with correct DDL id (first guess)
25// (Ch. Finck, dec 2005)
26//
27// Digits2Raw:
28// Generates raw data for MUON tracker and finally for trigger
29// Using real mapping (inverse) for tracker
30// For trigger there is no mapping (mapping could be found in AliMUONTriggerCircuit)
2cf44ef3 31// Ch. Finck, July 04
ced309a5 32// Use memcpy instead of assignment elt by elt
33// Introducing variable DSP numbers, real manu numbers per buspatch for st12
34// Implemented scaler event for Trigger
2cf44ef3 35// Ch. Finck, Jan. 06
a19e2543 36//
37////////////////////////////////////
38
2cbb173f 39#include "AliMUONRawWriter.h"
a19e2543 40
a19e2543 41#include "AliBitPacking.h"
2cf44ef3 42#include "AliRawReader.h"
9e378ff4 43#include "AliDAQ.h"
a19e2543 44#include "AliLog.h"
a19e2543 45#include "AliMUON.h"
a19e2543 46#include "AliMUONConstants.h"
2cf44ef3 47
48#include "AliMUONDarcHeader.h"
49#include "AliMUONRegHeader.h"
50#include "AliMUONLocalStruct.h"
51#include "AliMUONDspHeader.h"
52#include "AliMUONBlockHeader.h"
53
2cbb173f 54#include "AliMUONData.h"
55#include "AliMUONDigit.h"
2cbb173f 56#include "AliMUONGlobalTrigger.h"
57#include "AliMUONLocalTrigger.h"
2cf44ef3 58
2cbb173f 59#include "AliMpBusPatch.h"
60#include "AliMpDEManager.h"
61#include "AliMpPad.h"
a19e2543 62#include "AliMpPlaneType.h"
2cbb173f 63#include "AliMpSegFactory.h"
64#include "AliMpStationType.h"
a19e2543 65#include "AliMpVSegmentation.h"
2cf44ef3 66
2cbb173f 67#include "TClonesArray.h"
2cf44ef3 68
a19e2543 69ClassImp(AliMUONRawWriter) // Class implementation in ROOT context
ced309a5 70
71Int_t AliMUONRawWriter::fgManuPerBusSwp1B[12] = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 224, 232};
72Int_t AliMUONRawWriter::fgManuPerBusSwp1NB[12] = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 225, 233};
73
74Int_t AliMUONRawWriter::fgManuPerBusSwp2B[12] = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 226, 246};
75Int_t AliMUONRawWriter::fgManuPerBusSwp2NB[12] = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 227, 245};
76
77
a19e2543 78//__________________________________________________________________________
2cbb173f 79AliMUONRawWriter::AliMUONRawWriter(AliMUONData* data)
80: TObject(),
81 fScalerEvent(kFALSE)
a19e2543 82{
2cf44ef3 83 //
a19e2543 84 // Standard Constructor
2cf44ef3 85 //
2cbb173f 86 AliDebug(1,"Standard ctor");
87
a19e2543 88 // initialize container
a19e2543 89 fMUONData = data;
90
91 // initialize array
2cf44ef3 92 fBusArray = new TClonesArray("AliMUONBusStruct",1000);
93 fBusArray->SetOwner(kTRUE);
a19e2543 94
84ceeb06 95 // ddl tracker pointers
2cf44ef3 96 fBlockHeader = new AliMUONBlockHeader();
97 fDspHeader = new AliMUONDspHeader();
98 fBusStruct = new AliMUONBusStruct();
99
84ceeb06 100 // setting data key to default value (only for writting)
101 fBlockHeader->SetDataKey(fBlockHeader->GetDefaultDataKey());
102 fDspHeader->SetDataKey(fDspHeader->GetDefaultDataKey());
103 fBusStruct->SetDataKey(fBusStruct->GetDefaultDataKey());
104
84ceeb06 105 // ddl trigger pointers
2cf44ef3 106 fDarcHeader = new AliMUONDarcHeader();
107 fRegHeader = new AliMUONRegHeader();
108 fLocalStruct = new AliMUONLocalStruct();
a19e2543 109
84ceeb06 110 // bus patch & Seg managers
a19e2543 111 fBusPatchManager = new AliMpBusPatch();
112 fBusPatchManager->ReadBusPatchFile();
2cbb173f 113
114 fSegFactory = new AliMpSegFactory();
115
84ceeb06 116 // timers
2cbb173f 117 fTrackerTimer.Start(kTRUE); fTrackerTimer.Stop();
118 fTriggerTimer.Start(kTRUE); fTriggerTimer.Stop();
119 fMappingTimer.Start(kTRUE); fMappingTimer.Stop();
120
a19e2543 121}
122
123//__________________________________________________________________________
124AliMUONRawWriter::AliMUONRawWriter()
125 : TObject(),
126 fMUONData(0),
2cf44ef3 127 fBlockHeader(0),
128 fDspHeader(0),
129 fBusStruct(0),
130 fDarcHeader(0),
131 fRegHeader(0),
132 fLocalStruct(0),
ced309a5 133 fBusPatchManager(0),
2cbb173f 134 fScalerEvent(kFALSE),
135 fSegFactory(0x0)
a19e2543 136{
2cf44ef3 137 //
a19e2543 138 // Default Constructor
2cf44ef3 139 //
2cbb173f 140 AliDebug(1,"Default ctor");
141 fFile[0] = fFile[1] = 0x0;
142 fTrackerTimer.Start(kTRUE); fTrackerTimer.Stop();
143 fTriggerTimer.Start(kTRUE); fTriggerTimer.Stop();
144 fMappingTimer.Start(kTRUE); fMappingTimer.Stop();
a19e2543 145}
146
147//_______________________________________________________________________
148AliMUONRawWriter::AliMUONRawWriter (const AliMUONRawWriter& rhs)
149 : TObject(rhs)
150{
2cf44ef3 151 //
152 // Protected copy constructor
153 //
a19e2543 154 AliFatal("Not implemented.");
155}
156
157//_______________________________________________________________________
158AliMUONRawWriter &
159AliMUONRawWriter::operator=(const AliMUONRawWriter& rhs)
160{
2cf44ef3 161 //
162 // Protected assignement operator
163 //
a19e2543 164 if (this == &rhs) return *this;
165
166 AliFatal("Not implemented.");
167
168 return *this;
169}
170
171//__________________________________________________________________________
172AliMUONRawWriter::~AliMUONRawWriter(void)
173{
2cf44ef3 174 //
175 // Destructor
176 //
2cbb173f 177 AliDebug(1,"dtor");
178
2cf44ef3 179 delete fBusArray;
2cbb173f 180
2cf44ef3 181 delete fBlockHeader;
182 delete fDspHeader;
183 delete fBusStruct;
184 delete fDarcHeader;
185 delete fRegHeader;
186 delete fLocalStruct;
a19e2543 187
2cbb173f 188 delete fBusPatchManager;
189
190 delete fSegFactory;
191
192 AliInfo(Form("Execution time for MUON tracker : R:%.2fs C:%.2fs",
193 fTrackerTimer.RealTime(),fTrackerTimer.CpuTime()));
194 AliInfo(Form(" Execution time for MUON tracker (mapping calls part) "
195 ": R:%.2fs C:%.2fs",
196 fMappingTimer.RealTime(),fMappingTimer.CpuTime()));
197 AliInfo(Form("Execution time for MUON trigger : R:%.2fs C:%.2fs",
198 fTriggerTimer.RealTime(),fTriggerTimer.CpuTime()));
a19e2543 199}
2cbb173f 200
a19e2543 201//____________________________________________________________________
202Int_t AliMUONRawWriter::Digits2Raw()
203{
2cf44ef3 204 //
205 // convert digits of the current event to raw data
206 //
a19e2543 207 Int_t idDDL;
208 Char_t name[20];
209
2cbb173f 210 fMUONData->GetLoader()->LoadDigits("READ");
a19e2543 211
212 fMUONData->SetTreeAddress("D,GLT");
213
2cbb173f 214 fMUONData->ResetDigits();
215 fMUONData->ResetTrigger();
216
217 // This will get both tracker and trigger digits.
218 fMUONData->GetDigits();
219
a19e2543 220 // tracking chambers
221
2cbb173f 222 for (Int_t ich = 0; ich < AliMUONConstants::NTrackingCh(); ich++)
223 {
a19e2543 224 // open files
9e378ff4 225 idDDL = ich * 2 + AliDAQ::DdlIDOffset("MUONTRK");
a19e2543 226 sprintf(name, "MUON_%d.ddl",idDDL);
227 fFile[0] = fopen(name,"w");
228
9e378ff4 229 idDDL = (ich * 2) + 1 + AliDAQ::DdlIDOffset("MUONTRK");
a19e2543 230 sprintf(name, "MUON_%d.ddl",idDDL);
231 fFile[1] = fopen(name,"w");
2cbb173f 232
a19e2543 233 WriteTrackerDDL(ich);
234
235 // reset and close
236 fclose(fFile[0]);
237 fclose(fFile[1]);
a19e2543 238 }
239
240 // trigger chambers
241
242 // open files
9e378ff4 243 idDDL = AliDAQ::DdlIDOffset("MUONTRG");
a19e2543 244 sprintf(name, "MUTR_%d.ddl",idDDL);
245 fFile[0] = fopen(name,"w");
246
9e378ff4 247 idDDL = AliDAQ::DdlIDOffset("MUONTRG") + 1;
a19e2543 248 sprintf(name, "MUTR_%d.ddl",idDDL);
249 fFile[1] = fopen(name,"w");
250
251 WriteTriggerDDL();
252
253 // reset and close
254 fclose(fFile[0]);
255 fclose(fFile[1]);
2cbb173f 256
257 fMUONData->ResetDigits();
258 fMUONData->ResetTrigger();
259 fMUONData->GetLoader()->UnloadDigits();
a19e2543 260
261 return kTRUE;
262}
2cbb173f 263
a19e2543 264//____________________________________________________________________
265Int_t AliMUONRawWriter::WriteTrackerDDL(Int_t iCh)
266{
267 // writing DDL for tracker
268 // used inverse mapping
2cf44ef3 269 //
2cbb173f 270 fTrackerTimer.Start(kFALSE);
271
2cf44ef3 272
84aac932 273 static const Int_t kMAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
2cf44ef3 274
a19e2543 275 // resets
276 TClonesArray* muonDigits = 0;
2cf44ef3 277
278 fBusArray->Delete();
279
a19e2543 280
281 //
2cf44ef3 282 TArrayI nbInBus;
283
284 nbInBus.Set(5000);
285
a19e2543 286 nbInBus.Reset();
287
288 // DDL header
2cf44ef3 289 Int_t headerSize = sizeof(fHeader)/4;
290
291 // DDL event one per half chamber
a19e2543 292
2cf44ef3 293 // raw data
a19e2543 294 Char_t parity = 0x4;
295 UShort_t manuId = 0;
296 UChar_t channelId = 0;
297 UShort_t charge = 0;
298 Int_t busPatchId = 0;
a19e2543 299 UInt_t word;
2cf44ef3 300
84ceeb06 301
302 // Dsp length
303 Int_t totalDspLength;
304 Int_t dspLength;
305
2cf44ef3 306 // block length
307 Int_t totalBlkLength;
308 Int_t blkLength;
309
310 // total DDL length
311 Int_t totalDDLLength;
312
313 // indexes
a19e2543 314 Int_t index;
315 Int_t indexDsp;
316 Int_t indexBlk;
2cf44ef3 317
318 // digits
319 Int_t nEntries = 0;
320 Int_t* buffer = 0;
a19e2543 321 Int_t padX;
322 Int_t padY;
323 Int_t cathode = 0;
324 Int_t detElemId;
325 Int_t nDigits;
326
327 const AliMUONDigit* digit;
328
329 AliDebug(3, Form("WriteDDL chamber %d\n", iCh+1));
330
a19e2543 331 muonDigits = fMUONData->Digits(iCh);
332
333 nDigits = muonDigits->GetEntriesFast();
334 AliDebug(3,Form("ndigits = %d\n",nDigits));
335
336 // loop over digit
337 for (Int_t idig = 0; idig < nDigits; idig++) {
338
339 digit = (AliMUONDigit*) muonDigits->UncheckedAt(idig);
340
341 padX = digit->PadX();
342 padY = digit->PadY();
ce13d60d 343 charge = digit->ADC();
84aac932 344 if ( charge > kMAXADC )
2cbb173f 345 {
346 // This is most probably an error in the digitizer (which should insure
84aac932 347 // the adc is below kMAXADC), so make it a (non-fatal) error indeed.
2cbb173f 348 AliError(Form("adc value %d above %x. Setting to %x",
84aac932 349 charge,kMAXADC,kMAXADC));
350 charge = kMAXADC;
2cbb173f 351 }
a19e2543 352 cathode = digit->Cathode();
353 detElemId = digit->DetElemId();
354
355 // inverse mapping
2cbb173f 356 busPatchId = GetBusPatch(*digit);
357 if (busPatchId<0) continue;
358
359 if ( digit->ManuId() > 0x7FF || digit->ManuId() < 0 ||
360 digit->ManuChannel() > 0x3F || digit->ManuChannel() < 0 )
361 {
362 StdoutToAliError(digit->Print(););
363 AliFatal("ManuId,ManuChannel are invalid for the digit above.");
364 }
365
366 manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
367 channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
2cf44ef3 368
369 AliDebug(3,Form("input IdDE %d busPatchId %d PadX %d PadY %d iCath %d \n",
a19e2543 370 detElemId, busPatchId, padX, padY, cathode));
2cf44ef3 371
a19e2543 372 AliDebug(3,Form("busPatchId %d, manuId %d channelId %d\n", busPatchId, manuId, channelId ));
2cf44ef3 373
a19e2543 374 //packing word
6dee95a7 375 word = 0;
a19e2543 376 AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
377 AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
378 AliBitPacking::PackWord((UInt_t)charge,word,0,11);
379
6dee95a7 380 // parity word
381 parity = word & 0x1;
382 for (Int_t i = 1; i <= 30; i++)
383 parity ^= ((word >> i) & 0x1);
384 AliBitPacking::PackWord((UInt_t)parity,word,31,31);
6dee95a7 385
a19e2543 386 // set sub Event
2cf44ef3 387 fBusStruct->SetLength(0);
388 fBusStruct->AddData(word);
389 fBusStruct->SetBusPatchId(busPatchId);
a19e2543 390
391 // storing the number of identical buspatches
392 nbInBus[busPatchId]++;
2cf44ef3 393 AddData(*fBusStruct);
394
a19e2543 395 }
396
397 // sorting by buspatch
2cf44ef3 398 fBusArray->Sort();
a19e2543 399
400 // gather datas from same bus patch
2cf44ef3 401 nEntries = fBusArray->GetEntriesFast();
a19e2543 402
2cf44ef3 403 for (Int_t i = 0; i < nEntries; i++) {
404 AliMUONBusStruct* temp = (AliMUONBusStruct*)fBusArray->At(i);
a19e2543 405 busPatchId = temp->GetBusPatchId();
406
407 // add bus patch header, length and total length managed by subevent class
2cf44ef3 408 for (Int_t j = 0; j < nbInBus[busPatchId]-1; j++) {
409 AliMUONBusStruct* temp1 = (AliMUONBusStruct*)fBusArray->At(++i);
a19e2543 410 temp->AddData(temp1->GetData(0));
2cf44ef3 411 fBusArray->RemoveAt(i) ;
a19e2543 412 }
413 }
2cf44ef3 414 fBusArray->Compress();
a19e2543 415
2cf44ef3 416 if (AliLog::GetGlobalDebugLevel() == 3) {
417 nEntries = fBusArray->GetEntriesFast();
418 for (Int_t i = 0; i < nEntries; i++) {
419 AliMUONBusStruct* temp = (AliMUONBusStruct*)fBusArray->At(i);
a19e2543 420 printf("busPatchid back %d\n",temp->GetBusPatchId());
2cf44ef3 421 for (Int_t j = 0; j < temp->GetLength(); j++) {
422 printf("manuId back %d, ",temp->GetManuId(j));
423 printf("channelId back %d, ",temp->GetChannelId(j));
424 printf("charge back %d\n",temp->GetCharge(j));
a19e2543 425 }
426 }
427 printf("\n");
428 }
429
430 // getting info for the number of buspatches
431 Int_t iBusPatch;
432 Int_t length;
433 Int_t iBusPerDSP[5];//number of bus patches per DSP
434 Int_t iDspMax; //number max of DSP per block
a19e2543 435 Int_t iFile = 0;
436 fBusPatchManager->GetDspInfo(iCh, iDspMax, iBusPerDSP);
437
438 TArrayI* vec = fBusPatchManager->GetBusfromDE((iCh+1)*100);
439
440 Int_t iBus0AtCh = vec->At(0); //get first bus patch id for a given ich
441
442 AliDebug(3,Form("iBus0AtCh %d", iBus0AtCh));
443
444 iBusPatch = iBus0AtCh - 1; // starting point for each chamber
445
2cf44ef3 446 // nEntries = fBusArray->GetEntriesFast();
84ceeb06 447
448 AliMUONBusStruct* busStructPtr = 0x0;
2cf44ef3 449
a19e2543 450 // open DDL file, on per 1/2 chamber
2cf44ef3 451 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
452
453 totalDDLLength = 0;
454
a19e2543 455 // filling buffer
84ceeb06 456 buffer = new Int_t [(2048+24)*50]; // 24 words at most for one buspatch and 2048 manu info at most
2cf44ef3 457
a19e2543 458 indexBlk = 0;
459 indexDsp = 0;
460 index = 0;
2cf44ef3 461
a19e2543 462 // two blocks A and B per DDL
2cf44ef3 463 for (Int_t iBlock = 0; iBlock < 2; iBlock++) {
464
a19e2543 465 // block header
2cf44ef3 466 length = fBlockHeader->GetHeaderLength();
467 memcpy(&buffer[index],fBlockHeader->GetHeader(),length*4);
a19e2543 468 indexBlk = index;
469 index += length;
2cf44ef3 470
a19e2543 471 // 5 DSP's max per block
2cf44ef3 472 for (Int_t iDsp = 0; iDsp < iDspMax; iDsp++) {
473
474 // DSP header
475 length = fDspHeader->GetHeaderLength();
476 memcpy(&buffer[index],fDspHeader->GetHeader(),length*4);
477 indexDsp = index;
478 index += length;
479
480 // 5 buspatches max per DSP
481 for (Int_t i = 0; i < iBusPerDSP[iDsp]; i++) {
482
483 iBusPatch++;
484 if ((fBusPatchManager->GetDDLfromBus(iBusPatch) % 2) == 0) // comparing to DDL file
485 iFile = 1;
486 else
487 iFile = 0;
488
489 AliDebug(3,Form("iCh %d iDDL %d iBlock %d iDsp %d busPatchId %d", iCh, iDDL, iBlock, iDsp, iBusPatch));
490
491 nEntries = fBusArray->GetEntriesFast();
cd640dc7 492 busPatchId = -1;
84ceeb06 493
2cf44ef3 494 for (Int_t iEntries = 0; iEntries < nEntries; iEntries++) { // method "bourrique"...
84ceeb06 495 busStructPtr = (AliMUONBusStruct*)fBusArray->At(iEntries);
496 busPatchId = busStructPtr->GetBusPatchId();
2cf44ef3 497 if (busPatchId == iBusPatch) break;
498 busPatchId = -1;
84ceeb06 499 AliDebug(3,Form("busPatchId %d", busStructPtr->GetBusPatchId()));
2cf44ef3 500 }
501
502 // check if buspatchid has digit
503 if (busPatchId != -1) {
84ceeb06 504 // add bus patch structure header
505 length = busStructPtr->GetHeaderLength();
506 memcpy(&buffer[index],busStructPtr->GetHeader(),length*4);
2cf44ef3 507 index += length;
84ceeb06 508
509 // add bus patch data
510 for (Int_t j = 0; j < busStructPtr->GetLength(); j++) {
511 buffer[index++] = busStructPtr->GetData(j);
512 AliDebug(3,Form("busPatchId %d, manuId %d channelId %d\n",
513 busStructPtr->GetBusPatchId(),
514 busStructPtr->GetManuId(j), busStructPtr->GetChannelId(j) ));
2cf44ef3 515 }
516 // fBusArray->RemoveAt(iEntries);
517 // fBusArray->Compress();
518 } else {
519 // writting anyhow buspatch structure (empty ones)
84ceeb06 520 buffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
521 buffer[index++] = busStructPtr->GetHeaderLength(); // header length
2cf44ef3 522 buffer[index++] = 0; // raw data length
523 buffer[index++] = iBusPatch; // bus patch
2cf44ef3 524 }
525 } // bus patch
526
86400e61 527 // check if totalLength even
528 // set padding word in case
529 // Add one word 0xBEEFFACE at the end of DSP structure
530 totalDspLength = index - indexDsp;
531 if ((totalDspLength % 2) == 1) {
532 buffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
533 buffer[index++] = fDspHeader->GetDefaultPaddingWord();
534 totalDspLength++;
535 }
536
84ceeb06 537 dspLength = totalDspLength - fDspHeader->GetHeaderLength();
538
539 buffer[indexDsp+1] = totalDspLength; // dsp total length
540 buffer[indexDsp+2] = dspLength; // data length
84ceeb06 541
a19e2543 542 } // dsp
2cf44ef3 543
544 totalBlkLength = index - indexBlk;
545 blkLength = totalBlkLength - fBlockHeader->GetHeaderLength();
546 totalDDLLength += totalBlkLength;
547
84ceeb06 548 buffer[indexBlk+1] = totalBlkLength; // total block length
549 buffer[indexBlk+2] = blkLength;
2cf44ef3 550
551 } // block
a19e2543 552
553 //writting onto disk
554 // write DDL 1 & 2
2cf44ef3 555 fHeader.fSize = (totalDDLLength + headerSize) * 4;// total length in bytes
556 fwrite((char*)(&fHeader),headerSize*4,1,fFile[iFile]);
a19e2543 557 fwrite(buffer,sizeof(int),index,fFile[iFile]);
2cf44ef3 558
a19e2543 559 delete[] buffer;
560 }
561
2cbb173f 562 fTrackerTimer.Stop();
a19e2543 563 return kTRUE;
564}
ced309a5 565
566//____________________________________________________________________
2cbb173f 567Int_t AliMUONRawWriter::GetBusPatch(const AliMUONDigit& digit)
ced309a5 568{
2cf44ef3 569 //
2cbb173f 570 // Determine the BusPatch this digit belongs to.
2cf44ef3 571 //
2cbb173f 572 fMappingTimer.Start(kFALSE);
573
ced309a5 574 Int_t* ptr = 0;
575
576 // information from digits
2cbb173f 577 Int_t detElemId = digit.DetElemId();
578
579 AliMpVSegmentation* seg =
580 fSegFactory->CreateMpSegmentationByElectronics(detElemId, digit.ManuId());
581
582 AliMpPlaneType plane = seg->PlaneType();
ced309a5 583
2cbb173f 584 AliMpStationType stationType = AliMpDEManager::GetStationType(detElemId);
ced309a5 585
2cbb173f 586 if ( stationType == kStation1 || stationType == kStation2 )
587 {
588 if (plane == kBendingPlane)
589 {
590 ptr = &fgManuPerBusSwp1B[0];
591 }
592 else
593 {
594 ptr = &fgManuPerBusSwp1NB[0];
ced309a5 595 }
596 }
2cbb173f 597 else
598 {
599 if (plane == kBendingPlane)
600 {
601 ptr = &fgManuPerBusSwp2B[0];
602 }
603 else
604 {
605 ptr = &fgManuPerBusSwp2NB[0];
606 }
ced309a5 607 }
608
ced309a5 609 // Getting buspatch id
2cbb173f 610 TArrayI* vec = fBusPatchManager->GetBusfromDE(detElemId);
ced309a5 611 Int_t pos = 0;
612
2cbb173f 613 Int_t m = ( digit.ManuId() & 0x3FF ); // remove bit 10
614 //FIXME : how can we remove that condition
615 // on the 10-th bit ? All the rest need not any knowledge about it,
616 // can't we find a way to get manu<->buspatch transparent to this too ?
617
618 if ( stationType == kStation1 || stationType == kStation2 )
619 {
ced309a5 620 for (Int_t i = 0; i < 12; i++)
2cbb173f 621 {
622 if (m >= *(ptr + pos++)) break;
623 }
624 }
625 else
626 {
ced309a5 627 // offset of 100 in manuId for following bus patch
2cbb173f 628 pos = m/100;
ced309a5 629 }
630
631 if (pos >(Int_t) vec->GetSize())
2cbb173f 632 {
633 AliError(Form("pos greater %d than size %d manuId %d detElemId %d \n",
634 pos, (Int_t)vec->GetSize(), digit.ManuId(), detElemId));
635 AliError(Form("Chamber %s Plane %s manuId %d m %d",
636 StationTypeName(stationType).Data(),
637 PlaneTypeName(plane).Data(),
638 digit.ManuId(),
639 m));
640 return -1;
641 }
ced309a5 642
2cbb173f 643 Int_t busPatchId = vec->At(pos);
ced309a5 644
2cbb173f 645 fMappingTimer.Stop();
646
647 return busPatchId;
ced309a5 648}
649
a19e2543 650//____________________________________________________________________
651Int_t AliMUONRawWriter::WriteTriggerDDL()
652{
2cf44ef3 653 //
654 // Write trigger DDL
655 //
2cbb173f 656 fTriggerTimer.Start(kFALSE);
657
a19e2543 658 // DDL event one per half chamber
a19e2543 659
a19e2543 660 // stored local id number
661 TArrayI isFired(256);
662 isFired.Reset();
663
664
2cf44ef3 665 // DDL header size
666 Int_t headerSize = sizeof(AliRawDataHeader)/4;
ced309a5 667
a19e2543 668 TClonesArray* localTrigger;
669 TClonesArray* globalTrigger;
670 AliMUONGlobalTrigger* gloTrg;
671 AliMUONLocalTrigger* locTrg = 0x0;
672
a19e2543 673 // global trigger for trigger pattern
674 globalTrigger = fMUONData->GlobalTrigger();
675 gloTrg = (AliMUONGlobalTrigger*)globalTrigger->UncheckedAt(0);
2cf44ef3 676 Int_t gloTrigPat = gloTrg->GetGlobalPattern();
a19e2543 677
678 // local trigger
679 localTrigger = fMUONData->LocalTrigger();
680
681 UInt_t word;
682 Int_t* buffer = 0;
683 Int_t index;
684 Int_t iEntries = 0;
685 Int_t iLocCard, locCard;
686 Char_t locDec, trigY, posY, posX,regOut;
687 Int_t devX;
688 Int_t version = 1; // software version
689 Int_t eventType = 1; // trigger type: 1 for physics ?
690 Int_t serialNb = 0xF; // serial nb of card: all bits on for the moment
691 Int_t globalFlag = 1; // set to 2 if global info present in DDL else set to 1
692
ced309a5 693 if(fScalerEvent)
694 eventType = 2; //set to generate scaler events
695
a19e2543 696 Int_t nEntries = (Int_t) (localTrigger->GetEntries());// 234 local cards
697 // stored the local card id that's fired
698 for (Int_t i = 0; i < nEntries; i++) {
699 locTrg = (AliMUONLocalTrigger*)localTrigger->At(i);
700 isFired[locTrg->LoCircuit()] = 1; // storing local boards with informations
701 }
702
703 if (!nEntries)
62c04fc3 704 AliInfo("No Trigger information available");
a19e2543 705
ced309a5 706 if(fScalerEvent)
00f56161 707 // [16(local)*51 words + 16 words]*8(reg) + 6 + 12 + 6 words scaler event 6672 words
708 buffer = new Int_t [6680];
ced309a5 709 else
00f56161 710 // [16(local)*6 words + 5 words]*8(reg) + 10 words = 818
711 buffer = new Int_t [818];
ced309a5 712
a19e2543 713
714 // open DDL file, on per 1/2 chamber
715 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
716
717 index = 0;
718
a19e2543 719 word = 0;
00f56161 720 // set darc status word
a19e2543 721 AliBitPacking::PackWord((UInt_t)iDDL+1,word,28,31); //see AliMUONDDLTrigger.h for details
722 AliBitPacking::PackWord((UInt_t)serialNb,word,24,27);
723 AliBitPacking::PackWord((UInt_t)version,word,16,23);
724 AliBitPacking::PackWord((UInt_t)eventType,word,12,15);
725
726 if (iDDL == 0) // suppose global info in DDL one
727 globalFlag = 2;
728 else
729 globalFlag = 1;
ced309a5 730
a19e2543 731 AliBitPacking::PackWord((UInt_t)globalFlag,word,8,11);
2cf44ef3 732 fDarcHeader->SetWord(word);
18d3ded7 733
734 memcpy(&buffer[index], fDarcHeader->GetHeader(), (fDarcHeader->GetDarcHeaderLength())*4);
735 index += fDarcHeader->GetDarcHeaderLength();
a19e2543 736
737 if (iDDL == 0)
2cf44ef3 738 fDarcHeader->SetGlobalOutput(gloTrigPat);// no global input for the moment....
a19e2543 739 else
2cf44ef3 740 fDarcHeader->SetGlobalOutput(0);
ced309a5 741
742 if (fScalerEvent) {
743 // 6 DARC scaler words
2cf44ef3 744 memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),fDarcHeader->GetDarcScalerLength()*4);
745 index += fDarcHeader->GetDarcScalerLength();
ced309a5 746 }
2cf44ef3 747 // end of darc word
748 buffer[index++] = fDarcHeader->GetEndOfDarc();
ced309a5 749
750 // 4 words of global board input + Global board output
18d3ded7 751 memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (fDarcHeader->GetGlobalHeaderLength())*4);
752 index += fDarcHeader->GetGlobalHeaderLength();
ced309a5 753
754 if (fScalerEvent) {
755 // 10 Global scaler words
2cf44ef3 756 memcpy(fDarcHeader->GetGlobalScalers(), &buffer[index], fDarcHeader->GetGlobalScalerLength()*4);
757 index += fDarcHeader->GetGlobalScalerLength();
ced309a5 758 }
a19e2543 759
2cf44ef3 760 // end of global word
761 buffer[index++] = fDarcHeader->GetEndOfGlobal();
762
a19e2543 763 // 8 regional cards per DDL
764 for (Int_t iReg = 0; iReg < 8; iReg++) {
765
a19e2543 766 // Regional card header
767 word = 0;
00f56161 768
769 // set darc status word
770 fRegHeader->SetDarcWord(word);
771
a19e2543 772 regOut = 0;
2cf44ef3 773 AliBitPacking::PackWord((UInt_t)serialNb,word,24,28); //see AliMUONLocalStruct.h for details
a19e2543 774 AliBitPacking::PackWord((UInt_t)version,word,16,23);
775 AliBitPacking::PackWord((UInt_t)iReg,word,12,15);
776 AliBitPacking::PackWord((UInt_t)regOut,word,0,7); // whenever regional output will be implemented
777
2cf44ef3 778 fRegHeader->SetWord(word);
779 memcpy(&buffer[index],fRegHeader->GetHeader(),fRegHeader->GetHeaderLength()*4);
780 index += fRegHeader->GetHeaderLength();
a19e2543 781
ced309a5 782 // 11 regional scaler word
783 if (fScalerEvent) {
2cf44ef3 784 memcpy(&buffer[index], fRegHeader->GetScalers(), fRegHeader->GetScalerLength()*4);
785 index += fRegHeader->GetScalerLength();
ced309a5 786 }
a19e2543 787
2cf44ef3 788 // end of regional word
789 buffer[index++] = fRegHeader->GetEndOfReg();
790
a19e2543 791 // 16 local card per regional board
792 for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
793
794 iLocCard = iLoc + iReg*16 + iDDL*128;
795
796 if (isFired[iLocCard]) {
797 locTrg = (AliMUONLocalTrigger*)localTrigger->At(iEntries);
798 locCard = locTrg->LoCircuit();
799 locDec = locTrg->GetLoDecision();
800 trigY = 0;
801 posY = locTrg->LoStripY();
802 posX = locTrg->LoStripX();
803 devX = locTrg->LoDev();
804 AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n",
805 locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
806 } else { //no trigger (see PRR chpt 3.4)
2cf44ef3 807 locCard = -1; // not possible on 4 bits
a19e2543 808 locDec = 0;
809 trigY = 1;
810 posY = 15;
811 posX = 0;
812 devX = 0x8;
813 }
814
815 //packing word
816 word = 0;
817 AliBitPacking::PackWord((UInt_t)(iLocCard % 16),word,19,22); //card id number in crate
818 AliBitPacking::PackWord((UInt_t)locDec,word,15,18);
819 AliBitPacking::PackWord((UInt_t)trigY,word,14,14);
820 AliBitPacking::PackWord((UInt_t)posY,word,10,13);
821 AliBitPacking::PackWord((UInt_t)devX,word,5,9);
822 AliBitPacking::PackWord((UInt_t)posX,word,0,4);
823
824 if (locCard == iLocCard) {
825 // add local cards structure
826 buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
827 buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
828 buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
829 buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
830 buffer[index++] = (Int_t)word; // data word
831 if (iEntries < nEntries-1)
832 iEntries++;
833 } else {
834 buffer[index++] = 0; // 4 words for x1, x2, y1, y2
835 buffer[index++] = 0;
836 buffer[index++] = 0;
837 buffer[index++] = 0;
838 buffer[index++] = (Int_t)word; // data word
839
840 }
ced309a5 841 // 45 regional scaler word
842 if (fScalerEvent) {
2cf44ef3 843 memcpy(&buffer[index], fLocalStruct->GetScalers(), fLocalStruct->GetScalerLength()*4);
844 index += fLocalStruct->GetScalerLength();
ced309a5 845 }
846
2cf44ef3 847 // end of local structure words
848 buffer[index++] = fLocalStruct->GetEndOfLocal();
a19e2543 849
2cf44ef3 850 } // local card
a19e2543 851
852 } // Regional card
853
a19e2543 854
855 // writting onto disk
856 // write DDL 1
2cf44ef3 857 fHeader.fSize = (index + headerSize) * 4;// total length in bytes
858 fwrite((char*)(&fHeader),headerSize*4,1,fFile[iDDL]);
a19e2543 859 fwrite(buffer,sizeof(int),index,fFile[iDDL]);
860
861 }
862 delete[] buffer;
863
2cbb173f 864 fTriggerTimer.Stop();
865
ced309a5 866 return kTRUE;
a19e2543 867}
84ceeb06 868//____________________________________________________________________
869void AliMUONRawWriter::SetScalersNumbers()
870{
871 // set numbers for scaler events for trigger headers
872 // since this is provided by the experiment
873 // put dummy numbers to check the monitoring
874
875 fDarcHeader->SetScalersNumbers();
876 fRegHeader->SetScalersNumbers();
877 fLocalStruct->SetScalersNumbers();
878
879 fScalerEvent = kTRUE;
880}