AliMpSegFactory replaced with AliMpSegmentation
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitMaker.cxx
CommitLineData
a3283a4c 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////////////////////////////////////
17///
8c51a32f 18/// MUON Digit maker from rawdata.
a3283a4c 19///
20/// Raw2Digits:
21/// Using real mapping for tracker
22/// Indranil Das (Adapted for runloader: Ch. Finck) july 05
8c51a32f 23///
24/// Implemented non-constant buspatch numbers for tracking
25/// with correct DDL id.
26/// (Ch. Finck, dec 05)
27///
a3283a4c 28/// Add reader for scaler trigger events
29/// Use memcpy instead of assignment elt by elt
30/// (Ch. Finck, Jan 06)
8c51a32f 31///
32/// Using new interface with AliMUONRawStreamTracker(Trigger)
33/// (New interface of AliMUONRawReader class)
34/// (further details could be found in Alice-note)
35/// (Ch. Finck, March 06)
36///
37/// Add (S)Digit maker tracker (for free)
38/// and for trigger. Create trigger inverse mapping.
39/// (Ch. Finck, oct 06)
a3283a4c 40////////////////////////////////////
41
42#include <fstream>
43#include <string>
44
45#include <TClonesArray.h>
d2d759cf 46#include <TList.h>
a3283a4c 47
48#include "AliRawReader.h"
49#include "AliRawDataHeader.h"
50#include "AliLog.h"
51#include "AliRun.h"
52
84ceeb06 53#include "AliMpBusPatch.h"
a3283a4c 54#include "AliMUON.h"
55#include "AliMUONDigitMaker.h"
56#include "AliMUONDigit.h"
57
58#include "AliMUONConstants.h"
59#include "AliMUONData.h"
60
61#include "AliMUONRawStreamTracker.h"
62#include "AliMUONDDLTracker.h"
63#include "AliMUONDspHeader.h"
64#include "AliMUONBlockHeader.h"
65#include "AliMUONBusStruct.h"
66
67#include "AliMUONRawStreamTrigger.h"
68#include "AliMUONDDLTrigger.h"
69#include "AliMUONDarcHeader.h"
70#include "AliMUONRegHeader.h"
71#include "AliMUONLocalStruct.h"
72
d53fb0de 73#include "AliMUONTriggerCrateStore.h"
74#include "AliMUONTriggerCrate.h"
75#include "AliMUONLocalTriggerBoard.h"
a3283a4c 76#include "AliMUONLocalTrigger.h"
77#include "AliMUONGlobalTrigger.h"
d2d759cf 78#include "AliMUONTriggerCircuitNew.h"
a3283a4c 79#include "AliMpSegFactory.h"
80#include "AliMpVSegmentation.h"
81#include "AliMpPad.h"
82#include "AliMpDEManager.h"
83
84ClassImp(AliMUONDigitMaker) // Class implementation in ROOT context
85//__________________________________________________________________________
241560c2 86AliMUONDigitMaker::AliMUONDigitMaker(Bool_t flag)
a3283a4c 87 : TObject(),
aa6ecf89 88 fMUONData(0x0),
57d8c1ae 89 fSegFactory(0x0),
9f5dcca3 90 fBusPatchManager(new AliMpBusPatch()),
91 fScalerEvent(kFALSE),
241560c2 92 fDigitFlag(flag),
9f5dcca3 93 fRawStreamTracker(new AliMUONRawStreamTracker()),
94 fRawStreamTrigger(new AliMUONRawStreamTrigger()),
95 fDigit(new AliMUONDigit()),
96 fLocalTrigger(new AliMUONLocalTrigger()),
97 fGlobalTrigger(new AliMUONGlobalTrigger()),
fe5010dd 98 fCrateManager(0x0),
9f5dcca3 99 fTrackerTimer(),
100 fTriggerTimer(),
101 fMappingTimer()
a3283a4c 102{
103 //
104 // ctor with AliMUONData as argument
105 // for reconstruction
106 //
107
108 AliDebug(1,"");
109
110 // Standard Constructor
111
a3283a4c 112 // bus patch
a3283a4c 113 fBusPatchManager->ReadBusPatchFile();
114
fe5010dd 115
a3283a4c 116 fTrackerTimer.Start(kTRUE); fTrackerTimer.Stop();
117 fTriggerTimer.Start(kTRUE); fTriggerTimer.Stop();
118 fMappingTimer.Start(kTRUE); fMappingTimer.Stop();
119
120}
121
122//__________________________________________________________________________
9f5dcca3 123AliMUONDigitMaker::~AliMUONDigitMaker()
a3283a4c 124{
125 //
126 // clean up
127 // and time processing measure
128 //
a3283a4c 129 delete fRawStreamTracker;
130 delete fRawStreamTrigger;
131
132 delete fDigit;
133 delete fLocalTrigger;
134 delete fGlobalTrigger;
135
136 delete fBusPatchManager;
137
a7b01aa5 138 AliDebug(1, Form("Execution time for MUON tracker : R:%.2fs C:%.2fs",
a3283a4c 139 fTrackerTimer.RealTime(),fTrackerTimer.CpuTime()));
a7b01aa5 140 AliDebug(1, Form(" Execution time for MUON tracker (mapping calls part) "
a3283a4c 141 ": R:%.2fs C:%.2fs",
142 fMappingTimer.RealTime(),fMappingTimer.CpuTime()));
a7b01aa5 143 AliDebug(1, Form("Execution time for MUON trigger : R:%.2fs C:%.2fs",
a3283a4c 144 fTriggerTimer.RealTime(),fTriggerTimer.CpuTime()));
145
146 return;
147}
148
149//____________________________________________________________________
150Int_t AliMUONDigitMaker::Raw2Digits(AliRawReader* rawReader)
151{
152 // Main method to creates digit
153 // for tracker
154 // and trigger
155
156 // generate digits
157 ReadTrackerDDL(rawReader);
158
159 // generate trigger
d2d759cf 160 ReadTriggerDDL(rawReader);
a3283a4c 161
162 return kTRUE;
163
164}
165
166//____________________________________________________________________
167Int_t AliMUONDigitMaker::ReadTrackerDDL(AliRawReader* rawReader)
168{
169
170 // reading tracker DDL
171 // filling the TClonesArray in MUONData
172 //
173 fTrackerTimer.Start(kFALSE);
174
175 // elex info
176 Int_t buspatchId;
177 UChar_t channelId;
178 UShort_t manuId;
179 Char_t parity;
180 UShort_t charge;
181 Int_t dataSize;
182
e6e1f642 183 Int_t iChamber;
184
a3283a4c 185 AliMUONDDLTracker* ddlTracker = 0x0;
186 AliMUONBlockHeader* blkHeader = 0x0;
187 AliMUONDspHeader* dspHeader = 0x0;
188 AliMUONBusStruct* busStruct = 0x0;
189
190
191 fRawStreamTracker->SetReader(rawReader);
192
193 while(fRawStreamTracker->NextDDL()) {
194
195 ddlTracker = fRawStreamTracker->GetDDLTracker();
196
197 Int_t nBlock = ddlTracker->GetBlkHeaderEntries();
198 for(Int_t iBlock = 0; iBlock < nBlock ;iBlock++){
199
200 blkHeader = ddlTracker->GetBlkHeaderEntry(iBlock);
201
202 Int_t nDsp = blkHeader->GetDspHeaderEntries();
203
204 for(Int_t iDsp = 0; iDsp < nDsp ;iDsp++){ //DSP loop
205
206 dspHeader = blkHeader->GetDspHeaderEntry(iDsp);
207
208 Int_t nBusPatch = dspHeader->GetBusPatchEntries();
209
210 for(Int_t iBusPatch = 0; iBusPatch < nBusPatch; iBusPatch++) {
211
212 busStruct = dspHeader->GetBusPatchEntry(iBusPatch);
213
214 dataSize = busStruct->GetLength();
215 buspatchId = busStruct->GetBusPatchId();
216
217 for (Int_t iData = 0; iData < dataSize; iData++) {
218
219 // digits info
220 parity = busStruct->GetParity(iData); // test later for parity
221 manuId = busStruct->GetManuId(iData);
222 channelId = busStruct->GetChannelId(iData);
223 charge = busStruct->GetCharge(iData);
224 // set charge
225 fDigit->SetSignal(charge);
226 fDigit->SetPhysicsSignal(charge);
227 fDigit->SetADC(charge);
228
229 // Get Back the hits at pads
230 Int_t error = GetMapping(buspatchId,manuId,channelId,fDigit);
84ceeb06 231 if (error) {
e6e1f642 232 AliWarning("Mapping Error\n");
84ceeb06 233 continue;
234 }
a3283a4c 235 // debugging
236 if (AliLog::GetGlobalDebugLevel() == 3) {
237 Int_t padX = fDigit->PadX();
238 Int_t padY = fDigit->PadY();
239 Int_t iCath = fDigit->Cathode();
240 Int_t idDE = fDigit->DetElemId();
241
242 AliDebug(1,Form("output IdDE %d busPatchid %d PadX %d PadY %d iCath %d \n",
243 idDE, buspatchId, padX, padY, iCath));
244
245 AliDebug(3,Form("idDE %d Padx %d Pady %d, Cath %d, charge %d",
246 idDE, padX, padY, iCath, charge));
247 }
248
249 // fill digits
66f4c572 250 iChamber = AliMpDEManager::GetChamberId(fDigit->DetElemId());
241560c2 251
252 if (fDigitFlag)
253 fMUONData->AddDigit(iChamber, *fDigit);
254 else
255 fMUONData->AddSDigit(iChamber, *fDigit);
256
a3283a4c 257
258 } // iData
259 } // iBusPatch
260 } // iDsp
261 } // iBlock
262 } // NextDDL
263
264 fTrackerTimer.Stop();
265
266 return kTRUE;
267}
268//____________________________________________________________________
269Int_t AliMUONDigitMaker::GetMapping(Int_t busPatchId, UShort_t manuId,
270 UChar_t channelId, AliMUONDigit* digit )
271{
272 //
273 // mapping for tracker
274 //
275 fMappingTimer.Start(kFALSE);
276
277 // getting DE from buspatch
e6e1f642 278 Int_t detElemId = fBusPatchManager->GetDEfromBus(busPatchId);
a3283a4c 279 AliDebug(3,Form("detElemId: %d busPatchId %d\n", detElemId, busPatchId));
280
281 AliMpVSegmentation* seg = fSegFactory->CreateMpSegmentationByElectronics(detElemId, manuId);
282 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,channelId),kTRUE);
283
284 if (!pad.IsValid())
285 {
286 AliWarning(Form("No pad for detElemId: %d, busPatchId %d, manuId: %d, channelId: %d\n",
287 detElemId, busPatchId, manuId, channelId));
288 fMappingTimer.Stop();
289 return kTRUE;
290 } // return error
291
292 // Getting padX, padY and cathode number.
293 Int_t padX = pad.GetIndices().GetFirst();
294 Int_t padY = pad.GetIndices().GetSecond();
295 Int_t iCath = AliMpDEManager::GetCathod(detElemId,seg->PlaneType());
296
297 // storing into digits
298 digit->SetPadX(padX);
299 digit->SetPadY(padY);
300 digit->SetCathode(iCath);
301 digit->SetDetElemId(detElemId);
302 digit->SetElectronics(manuId,channelId);
303
304 AliDebug(3,Form("detElemId: %d, busPatchId %d, manuId: %d, channelId: %d, padx: %d pady %d\n",
305 detElemId, busPatchId, manuId, channelId, padX, padY));
306 StdoutToAliDebug(3,digit->Print(););
307
308 fMappingTimer.Stop();
309 return kFALSE;
310}
311
312//____________________________________________________________________
313Int_t AliMUONDigitMaker::ReadTriggerDDL(AliRawReader* rawReader)
314{
315 // reading tracker DDL
316 // filling the TClonesArray in MUONData
317 //
318
319 AliMUONDDLTrigger* ddlTrigger = 0x0;
320 AliMUONDarcHeader* darcHeader = 0x0;
321 AliMUONRegHeader* regHeader = 0x0;
322 AliMUONLocalStruct* localStruct = 0x0;
323
324 Int_t loCircuit;
d2d759cf 325 TList digitList;
326
a3283a4c 327
328 fTriggerTimer.Start(kFALSE);
329
330 fRawStreamTrigger->SetReader(rawReader);
331
332 while(fRawStreamTrigger->NextDDL()) {
333
334 ddlTrigger = fRawStreamTrigger->GetDDLTrigger();
335 darcHeader = ddlTrigger->GetDarcHeader();
336
d2d759cf 337 // fill global trigger information in Digit Tree
338 if (fDigitFlag) {
339 if (darcHeader->GetGlobalFlag()) {
340 fGlobalTrigger->SetFromGlobalResponse(darcHeader->GetGlobalOutput());
341 fMUONData->AddGlobalTrigger(*fGlobalTrigger);
342 }
a3283a4c 343 }
344
345 Int_t nReg = darcHeader->GetRegHeaderEntries();
346
347 for(Int_t iReg = 0; iReg < nReg ;iReg++){ //reg loop
348
fe5010dd 349 // crate info
350 if (!fCrateManager) AliFatal("Crate Store not defined");
aa6ecf89 351 AliMUONTriggerCrate* crate = fCrateManager->Crate(fRawStreamTrigger->GetDDL(), iReg);
d53fb0de 352
353 if (!crate)
354 AliWarning(Form("Missing crate number %d in DDL %d\n", iReg, fRawStreamTrigger->GetDDL()));
355
356 TObjArray *boards = crate->Boards();
357
358
a3283a4c 359 regHeader = darcHeader->GetRegHeaderEntry(iReg);
360
361 Int_t nLocal = regHeader->GetLocalEntries();
a3283a4c 362 for(Int_t iLocal = 0; iLocal < nLocal; iLocal++) {
363
364 localStruct = regHeader->GetLocalEntry(iLocal);
365
d53fb0de 366 // if card has triggered
a3283a4c 367 if (localStruct->GetTriggerY() == 0) {
d53fb0de 368
d2d759cf 369
d53fb0de 370 AliMUONLocalTriggerBoard* localBoard =
371 (AliMUONLocalTriggerBoard*)boards->At(localStruct->GetId()+1);
372
373 loCircuit = localBoard->GetNumber();
d2d759cf 374
375 if (fDigitFlag) {
376 // fill local trigger
377 fLocalTrigger->SetLocalStruct(loCircuit, *localStruct);
378
379 fMUONData->AddLocalTrigger(*fLocalTrigger);
380
381 } else {
382 // Make SDigit
383
384 digitList.Clear();
a3283a4c 385
d2d759cf 386 if( TriggerDigits(localBoard, localStruct, digitList) ) {
387
388 for (Int_t iEntry = 0; iEntry < digitList.GetEntries(); iEntry++) {
389
390 AliMUONDigit* digit = (AliMUONDigit*)digitList.At(iEntry);
391
392 // filling S container
66f4c572 393 Int_t iChamber = AliMpDEManager::GetChamberId(digit->DetElemId());
d2d759cf 394 fMUONData->AddSDigit(iChamber, *digit);
395
396 }
397
398 } // trigger digits
399 } // S flag
a3283a4c 400
a3283a4c 401 } // if triggerY
402 } // iLocal
403 } // iReg
404 } // NextDDL
405
406 fTriggerTimer.Stop();
407
408 return kTRUE;
409
410}
d2d759cf 411//____________________________________________________________________
412void AliMUONDigitMaker::GetTriggerChamber(AliMUONLocalStruct* localStruct, Int_t& xyPattern,
413 Int_t& iChamber, Int_t& iCath, Int_t icase)
414{
a3283a4c 415
8c51a32f 416 // get chamber & cathode number, (chamber starts at 0 !)
d2d759cf 417 switch(icase) {
418 case 0:
419 xyPattern = localStruct->GetX1();
420 iCath = 0;
8c51a32f 421 iChamber = 10;
d2d759cf 422 break;
423 case 1:
424 xyPattern = localStruct->GetX2();
425 iCath = 0;
8c51a32f 426 iChamber = 11;
d2d759cf 427 break;
428 case 2:
429 xyPattern = localStruct->GetX3();
430 iCath = 0;
8c51a32f 431 iChamber = 12;
d2d759cf 432 break;
433 case 3:
434 xyPattern = localStruct->GetX4();
435 iCath = 0;
8c51a32f 436 iChamber = 13;
d2d759cf 437 break;
438 case 4:
439 xyPattern = localStruct->GetY1();
440 iCath = 1;
8c51a32f 441 iChamber = 10;
d2d759cf 442 break;
443 case 5:
444 xyPattern = localStruct->GetY2();
445 iCath = 1;
8c51a32f 446 iChamber = 11;
d2d759cf 447 break;
448 case 6:
449 xyPattern = localStruct->GetY3();
450 iCath = 1;
8c51a32f 451 iChamber = 12;
d2d759cf 452 break;
453 case 7:
454 xyPattern = localStruct->GetY4();
455 iCath = 1;
8c51a32f 456 iChamber = 13;
d2d759cf 457 break;
458 }
459}
460//____________________________________________________________________
461Int_t AliMUONDigitMaker::TriggerDigits(AliMUONLocalTriggerBoard* localBoard,
462 AliMUONLocalStruct* localStruct,
463 TList& digitList)
464{
465 //
466 // make (S)Digit for trigger
467
468 Int_t detElemId;
469 Int_t nBoard;
470 Int_t iCath = -1;
471 Int_t iChamber = 0;
472 Int_t xyPattern = 0;
473
474 // loop over x1-4 and y1-4
475 for (Int_t icase = 0; icase < 8; icase++) {
476
477 // get chamber, cathode and associated trigger response pattern
478 GetTriggerChamber(localStruct, xyPattern, iChamber, iCath, icase);
479
480 if (!xyPattern) continue;
481
482 // get detElemId
483 AliMUONTriggerCircuitNew triggerCircuit;
484 detElemId = triggerCircuit.DetElemId(iChamber, localBoard->GetName());
485 nBoard = localBoard->GetNumber();
486
487 AliMpVSegmentation* seg = fSegFactory->CreateMpSegmentation(detElemId, iCath);
488
489 // loop over the 16 bits of pattern
490 for (Int_t ibitxy = 0; ibitxy < 16; ibitxy++) {
491
492 if ((xyPattern >> ibitxy) & 0x1) {
493
494 // not quite sure about this
495 Int_t offset = 0;
496 if (iCath && localBoard->GetSwitch(6)) offset = -8;
497
498 AliMpPad pad = seg->PadByLocation(AliMpIntPair(nBoard,ibitxy+offset),kTRUE);
499
500 AliMUONDigit* digit = new AliMUONDigit();
501 if (!pad.IsValid()) {
502 AliWarning(Form("No pad for detElemId: %d, nboard %d, ibitxy: %d\n",
503 detElemId, nBoard, ibitxy));
504 continue;
505 } //
506
507 Int_t padX = pad.GetIndices().GetFirst();
508 Int_t padY = pad.GetIndices().GetSecond();
509
510 // file digit
511 digit->SetPadX(padX);
512 digit->SetPadY(padY);
513 digit->SetCathode(iCath);
514 digit->SetDetElemId(detElemId);
515 digit->SetElectronics(nBoard, ibitxy);
516 digitList.Add(digit);
517
518 }// xyPattern
519 }// ibitxy
520 }// case
521
522 return kTRUE;
523}
d53fb0de 524//____________________________________________________________________
525void AliMUONDigitMaker::GetCrateName(Char_t* name, Int_t iDDL, Int_t iReg)
526{
527 // set crate name from DDL & reg number
528 // method same as in RawWriter, not so nice
529 // should be put in AliMUONTriggerCrateStore
530
531 switch(iReg) {
532 case 0:
533 case 1:
534 sprintf(name,"%d", iReg+1);
535 break;
536 case 2:
537 strcpy(name, "2-3");
538 break;
539 case 3:
540 case 4:
541 case 5:
542 case 6:
543 case 7:
544 sprintf(name,"%d", iReg);
545 break;
546 }
547
548 // crate Right for first DDL
549 if (iDDL == 0)
550 strcat(name, "R");
551 else
552 strcat(name, "L");
553}