New file, moved from alice-macros/.
[u/mrichter/AliRoot.git] / EVE / Alieve / MUONData.cxx
CommitLineData
3626c858 1//
2// Sources:
3//
4// GetTrackerMapping = AliMUONDigitMaker::GetMapping
5// GetTriggerMapping = AliMUONDigitMaker::TriggerDigits
6// GetTriggerChamber = AliMUONDigitMaker::GetTriggerChamber
7// LoadRawTracker = MUONRawStreamTracker.C
8// LoadRawTrigger = MUONRawStreamTrigger.C
9//
10
11#include "MUONData.h"
12
13#include <Alieve/MUONChamberData.h>
14#include <Alieve/EventAlieve.h>
15
16#include <AliRawReader.h>
17#include <AliRawReaderFile.h>
18#include <AliRawReaderDate.h>
19#include <AliRawReaderRoot.h>
20
361fa326 21#include <AliLog.h>
3626c858 22
3626c858 23#include <AliMUONDigit.h>
24#include <AliMUONRawStreamTracker.h>
25#include <AliMUONRawStreamTrigger.h>
26#include <AliMUONDDLTracker.h>
27#include <AliMUONBlockHeader.h>
28#include <AliMUONDspHeader.h>
29#include <AliMUONBusStruct.h>
30#include <AliMUONDDLTrigger.h>
31#include <AliMUONDarcHeader.h>
32#include <AliMUONRegHeader.h>
33#include <AliMUONLocalStruct.h>
34#include <AliMUONTriggerCrateStore.h>
35#include <AliMUONTriggerCrate.h>
36#include <AliMUONLocalTriggerBoard.h>
37#include <AliMUONTriggerCircuit.h>
361fa326 38#include <mapping/AliMpDDLStore.h>
3626c858 39#include <mapping/AliMpVSegmentation.h>
40#include <mapping/AliMpSegmentation.h>
41#include <mapping/AliMpPad.h>
42#include <mapping/AliMpDEManager.h>
43
44#include "TTree.h"
45#include "TString.h"
3626c858 46#include "TClonesArray.h"
47
48using namespace Reve;
49using namespace Alieve;
50
51//______________________________________________________________________
52// MUONData
53//
54
55ClassImp(MUONData)
56
57AliRawReader* MUONData::fgRawReader = 0;
58AliMUONRawStreamTracker* MUONData::fgRawStreamTracker = 0;
59AliMUONRawStreamTrigger* MUONData::fgRawStreamTrigger = 0;
361fa326 60AliMpDDLStore* MUONData::fgBusPatchManager = 0;
3626c858 61
62//______________________________________________________________________
63MUONData::MUONData() :
2674c15a 64 fChambers(14)
3626c858 65{
66 //
67 // Constructor
68 //
69
70 CreateAllChambers();
71
72}
73
74//______________________________________________________________________
75MUONData::~MUONData()
76{
77 //
78 // Destructor
79 //
80
81 DeleteAllChambers();
82
3626c858 83}
84
85//______________________________________________________________________
86MUONData::MUONData(const MUONData &mdata) :
e4204218 87 TObject(mdata),
88 Reve::ReferenceCount()
3626c858 89{
90 //
91 // Copy constructor
92 //
93
94}
95
96//______________________________________________________________________
97MUONData& MUONData::operator=(const MUONData &mdata)
98{
99 //
100 // Assignment operator
101 //
102
103 if (this != &mdata) {
104
105 }
106
107 return *this;
108
109}
110
111//______________________________________________________________________
112void MUONData::CreateChamber(Int_t chamber)
113{
114 //
115 // create data for the chamber with id=chamber (0 to 13)
116 //
117
118 if (fChambers[chamber] == 0)
119 fChambers[chamber] = new MUONChamberData(chamber);
120
121}
122
123//______________________________________________________________________
124void MUONData::CreateAllChambers()
125{
126 //
127 // create all 14 chambers data
128 //
129
130 for (Int_t c = 0; c < 14; ++c)
131 CreateChamber(c);
132
133}
134
135//______________________________________________________________________
136void MUONData::DropAllChambers()
137{
138 //
139 // release data from all chambers
140 //
141
142 for (Int_t c = 0; c < 14; ++c) {
143
144 if (fChambers[c] != 0)
145 fChambers[c]->DropData();
146
147 }
148
149}
150
151//______________________________________________________________________
152void MUONData::DeleteAllChambers()
153{
154 //
155 // delete all chambers data
156 //
157
158 for (Int_t c = 0; c < 14; ++c) {
159
160 delete fChambers[c];
161 fChambers[c] = 0;
162
163 }
164
165}
166
3626c858 167//______________________________________________________________________
168void MUONData::LoadDigits(TTree* tree)
169{
170 //
171 // load digits from the TreeD
172 //
173
174 Char_t branchname[30];
175 TClonesArray *digits = 0;
176 Int_t ndigits;
177 AliMUONDigit *mdig;
178 Int_t cathode, detElemId, ix, iy, charge;
179
180 for (Int_t c = 0; c < 14; ++c) {
181
182 if (fChambers[c] == 0) continue;
183 sprintf(branchname,"MUONDigits%d",c+1);
184 tree->SetBranchAddress(branchname,&digits);
185 tree->GetEntry(0);
186
187 ndigits = digits->GetEntriesFast();
188
189 for (Int_t id = 0; id < ndigits; id++) {
190 mdig = (AliMUONDigit*)digits->UncheckedAt(id);
191
192 cathode = mdig->Cathode();
193 ix = mdig->PadX();
194 iy = mdig->PadY();
195 detElemId = mdig->DetElemId();
196 charge = (Int_t)mdig->Signal();
197
198 if (c > 9) {
199 //printf("cha %d deid %d cath %1d ix %d iy %d q %d \n",c,detElemId,cathode,ix,iy,charge);
200 }
201
202 fChambers[c]->RegisterDigit(detElemId,cathode,ix,iy,charge);
203
204 } // end digits loop
205
206 }
207
208}
209
2674c15a 210//______________________________________________________________________
03db5cdf 211void MUONData::LoadRecPoints(TTree* /*tree*/)
2674c15a 212{
213 //
214 // load reconstructed points from the TreeR
215 // load local trigger information
216 //
217
218}
219
3626c858 220//______________________________________________________________________
221void MUONData::LoadRaw(TString fileName)
222{
223 //
224 // load raw data from fileName; tracker and trigger data
225 //
226
227 if (fgRawReader == 0) {
228 // check extention to choose the rawdata file format
229 if (fileName.EndsWith("/")) {
230 fgRawReader = new AliRawReaderFile(fileName); // DDL files
231 } else if (fileName.EndsWith(".root")) {
232 fgRawReader = new AliRawReaderRoot(fileName); // ROOT file
233 } else if (!fileName.IsNull()) {
234 fgRawReader = new AliRawReaderDate(fileName); // DATE file
235 }
236 fgRawStreamTracker = new AliMUONRawStreamTracker(fgRawReader);
237 fgRawStreamTrigger = new AliMUONRawStreamTrigger(fgRawReader);
361fa326 238 fgBusPatchManager = AliMpDDLStore::Instance();
3626c858 239 }
240
241 LoadRawTracker();
242 LoadRawTrigger();
243
244}
245
246//______________________________________________________________________
247void MUONData::LoadRawTracker()
248{
249 //
250 // load raw data for the tracking chambers
251 //
252
253 fgRawReader->RewindEvents();
254
255 AliMUONDigit* digit = new AliMUONDigit();
256
257 Int_t maxEvent = 1000;
258 Int_t minDDL = 0, maxDDL = 19;
259 Int_t cathode, detElemId, ix, iy, iChamber;
260
261 AliMUONDDLTracker* ddlTracker = 0x0;
262 AliMUONBlockHeader* blkHeader = 0x0;
263 AliMUONDspHeader* dspHeader = 0x0;
264 AliMUONBusStruct* busStruct = 0x0;
265
266 Int_t iEvent = 0;
267 Int_t dataSize, buspatchId;
268
269 Event* aevent = Alieve::gEvent;
270
271 while (fgRawReader->NextEvent()) {
272
273 if (iEvent != aevent->GetEventId()) {
274 iEvent++;
275 continue;
276 }
277
278 if (iEvent == maxEvent)
279 break;
280
281 // read DDL while < 20 DDL
282 while(fgRawStreamTracker->NextDDL()) {
283
284 if (fgRawStreamTracker->GetDDL() < minDDL ||
285 fgRawStreamTracker->GetDDL() > maxDDL)
286 continue;
287
288 //printf("\niDDL %d\n", fgRawStreamTracker->GetDDL());
289
290 ddlTracker = fgRawStreamTracker->GetDDLTracker();
291
292 // loop over block structure
293 Int_t nBlock = ddlTracker->GetBlkHeaderEntries();
294 for(Int_t iBlock = 0; iBlock < nBlock ;iBlock++){
295
296 blkHeader = ddlTracker->GetBlkHeaderEntry(iBlock);
297 //printf("Block Total length %d\n",blkHeader->GetTotalLength());
298
299 // loop over DSP structure
300 Int_t nDsp = blkHeader->GetDspHeaderEntries();
301 for(Int_t iDsp = 0; iDsp < nDsp ;iDsp++){ //DSP loop
302
303 dspHeader = blkHeader->GetDspHeaderEntry(iDsp);
304 // printf("Dsp length %d even word %d\n",dspHeader->GetTotalLength(), dspHeader->GetEventWord());
305
306 // loop over BusPatch structure
307 Int_t nBusPatch = dspHeader->GetBusPatchEntries();
308 for(Int_t iBusPatch = 0; iBusPatch < nBusPatch; iBusPatch++) {
309
310 busStruct = dspHeader->GetBusPatchEntry(iBusPatch);
311
312 //printf("busPatchId %d", busStruct->GetBusPatchId());
313 //printf(" BlockId %d", busStruct->GetBlockId());
314 //printf(" DspId %d\n", busStruct->GetDspId());
315
316 // loop over data
317 dataSize = busStruct->GetLength();
318 buspatchId = busStruct->GetBusPatchId();
319 for (Int_t iData = 0; iData < dataSize; iData++) {
320
321 Int_t manuId = busStruct->GetManuId(iData);
322 Int_t channelId = busStruct->GetChannelId(iData);
323 Int_t charge = busStruct->GetCharge(iData);
324 //printf("manuId: %d, channelId: %d charge: %d\n", manuId, channelId, charge);
325 // set digit charge
326 digit->SetSignal(charge);
327 digit->SetPhysicsSignal(charge);
328 digit->SetADC(charge);
329 // Get Back the hits at pads
330 Int_t error;
331 error = GetTrackerMapping(buspatchId,manuId,channelId,digit);
332 if (error) {
333 printf("Mapping Error\n");
334 continue;
335 }
336
337 cathode = digit->Cathode();
338 ix = digit->PadX();
339 iy = digit->PadY();
340 detElemId = digit->DetElemId();
341 charge = (Int_t)digit->Signal();
342 iChamber = detElemId/100 - 1;
343
344 fChambers[iChamber]->RegisterDigit(detElemId,cathode,ix,iy,charge);
345
346 } // iData
347 } // iBusPatch
348 } // iDsp
349 } // iBlock
350 } // NextDDL
351
352 break;
353
354 } // end event loop
355
356 delete digit;
357
358}
359
360//______________________________________________________________________
361void MUONData::LoadRawTrigger()
362{
363 //
364 // load raw data for the trigger chambers
365 //
366
367 fgRawReader->RewindEvents();
368
369 Int_t maxEvent = 1000;
370 Int_t minDDL = 0, maxDDL = 1;
371 Int_t detElemId, iChamber, cathode, charge, ix, iy;
372
373 AliMUONDDLTrigger* ddlTrigger = 0x0;
374 AliMUONDarcHeader* darcHeader = 0x0;
375 AliMUONRegHeader* regHeader = 0x0;
376 AliMUONLocalStruct* localStruct = 0x0;
377
378 // crate manager
379 AliMUONTriggerCrateStore* crateManager = new AliMUONTriggerCrateStore();
380 crateManager->ReadFromFile();
381
382 // Loop over events
383 Int_t iEvent = 0;
384 TList digitList;
385
386 Event* aevent = Alieve::gEvent;
387
388 while (fgRawReader->NextEvent()) {
389
390 if (iEvent != aevent->GetEventId()) {
391 iEvent++;
392 continue;
393 }
394
395 if (iEvent == maxEvent)
396 break;
397
398 // read DDL while < 2 DDL
399 while(fgRawStreamTrigger->NextDDL()) {
400
401 if (fgRawStreamTrigger->GetDDL() < minDDL ||
402 fgRawStreamTrigger->GetDDL() > maxDDL)
403 continue;
404
405 //printf("\niDDL %d\n", fgRawStreamTrigger->GetDDL());
406
407 ddlTrigger = fgRawStreamTrigger->GetDDLTrigger();
408 darcHeader = ddlTrigger->GetDarcHeader();
409
410 //printf("Global output %x\n", (Int_t)darcHeader->GetGlobalOutput());
411
412 // loop over regional structures
413 Int_t nReg = darcHeader->GetRegHeaderEntries();
414 for(Int_t iReg = 0; iReg < nReg ;iReg++){ //REG loop
415
416 //printf("RegionalId %d\n", iReg);
417
418 regHeader = darcHeader->GetRegHeaderEntry(iReg);
419 // printf("Reg length %d\n",regHeader->GetHeaderLength());
420
421 // crate info
422 AliMUONTriggerCrate* crate = crateManager->Crate(fgRawStreamTrigger->GetDDL(), iReg);
423 TObjArray *boards = crate->Boards();
424
425 // loop over local structures
426 Int_t nLocal = regHeader->GetLocalEntries();
427 for(Int_t iLocal = 0; iLocal < nLocal; iLocal++) {
428
429 localStruct = regHeader->GetLocalEntry(iLocal);
430
431 // check if trigger
432 if (localStruct->GetTriggerY() == 0) { // no empty data
433
434 // local trigger circuit number
435 AliMUONLocalTriggerBoard* localBoard = (AliMUONLocalTriggerBoard*)boards->At(iLocal+1);
436
437 //printf("LocalId %d\n", localStruct->GetId());
438 /*
439 Int_t iLocCard = localBoard->GetNumber();
440 Int_t loStripX = (Int_t)localStruct->GetXPos();
441 Int_t loStripY = (Int_t)localStruct->GetYPos();
442 Int_t loDev = (Int_t)localStruct->GetXDev();
443 */
444 //printf("iLocCard: %d, XPos: %d, YPos: %d Dev: %d\n", iLocCard, loStripX, loStripY, loDev);
445
446 digitList.Clear();
447 if ( GetTriggerMapping(localBoard, localStruct, digitList) ) {
448 for (Int_t iEntry = 0; iEntry < digitList.GetEntries(); iEntry++) {
449
450 AliMUONDigit* digit = (AliMUONDigit*)digitList.At(iEntry);
451 cathode = digit->Cathode();
452 ix = digit->PadX();
453 iy = digit->PadY();
454 detElemId = digit->DetElemId();
455 charge = (Int_t)digit->Signal();
456 iChamber = detElemId/100 - 1;
457
458 //printf("cha %d deid %d cath %1d ix %d iy %d q %d \n",iChamber,detElemId,cathode,ix,iy,charge);
459
460 fChambers[iChamber]->RegisterDigit(detElemId,cathode,ix,iy,charge);
461
462 }
463
464 }
465
466 }
467 } // iLocal
468 } // iReg
469 } // NextDDL
470
471 break;
472
473 } // end event loop
474
475 delete crateManager;
476
477}
478
479//______________________________________________________________________
480Int_t MUONData::GetTrackerMapping(Int_t buspatchId, UShort_t manuId, UChar_t channelId, AliMUONDigit* digit)
481{
482 //
483 // decode digits mapping for the tracking chambers
484 //
485
486 // getting DE from buspatch
487 Int_t detElemId = fgBusPatchManager->GetDEfromBus(buspatchId);
488 //AliDebug(3,Form("detElemId: %d busPatchId %d\n", detElemId, buspatchId));
489
490 const AliMpVSegmentation* seg = AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId, manuId);
491 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,channelId),kTRUE);
492
493 if (!pad.IsValid())
494 {
495 printf("No pad for detElemId: %d, busPatchId %d, manuId: %d, channelId: %d\n",detElemId, buspatchId, manuId, channelId);
496
497 return 1;
498 } // return error
499
500 // Getting padX, padY and cathode number.
501 Int_t padX = pad.GetIndices().GetFirst();
502 Int_t padY = pad.GetIndices().GetSecond();
503 Int_t iCath = AliMpDEManager::GetCathod(detElemId,seg->PlaneType());
504
505 // storing into digits
506 digit->SetPadX(padX);
507 digit->SetPadY(padY);
508 digit->SetCathode(iCath);
509 digit->SetDetElemId(detElemId);
510 digit->SetElectronics(manuId,channelId);
511
512 //printf("detElemId: %d, busPatchId %d, manuId: %d, channelId: %d, padx: %d pady %d\n",detElemId, buspatchId, manuId, channelId, padX, padY);
513
514 return 0;
515
516}
517
518//______________________________________________________________________
519Int_t MUONData::GetTriggerMapping(AliMUONLocalTriggerBoard* localBoard,
520 AliMUONLocalStruct* localStruct,
521 TList& digitList)
522{
523 //
524 // decode digits mapping for the trigger chambers
525 //
526
527 Int_t detElemId;
528 Int_t nBoard;
529 Int_t iCath = -1;
530 Int_t iChamber = 0;
531 Int_t xyPattern = 0;
532
533 // loop over x1-4 and y1-4
534 for (Int_t icase = 0; icase < 8; icase++) {
535
536 // get chamber, cathode and associated trigger response pattern
537 GetTriggerChamber(localStruct, xyPattern, iChamber, iCath, icase);
538
539 if (!xyPattern) continue;
540
541 // get detElemId
542 AliMUONTriggerCircuit triggerCircuit;
543 detElemId = triggerCircuit.DetElemId(iChamber, localBoard->GetName());
544 nBoard = localBoard->GetNumber();
545
361fa326 546 const AliMpVSegmentation* seg
547 = AliMpSegmentation::Instance()
548 ->GetMpSegmentation(detElemId, AliMp::GetCathodType(iCath));
3626c858 549
550 // loop over the 16 bits of pattern
551 for (Int_t ibitxy = 0; ibitxy < 16; ibitxy++) {
552
553 if ((xyPattern >> ibitxy) & 0x1) {
554
555 // not quite sure about this
556 Int_t offset = 0;
557 if (iCath && localBoard->GetSwitch(6)) offset = -8;
558
559 AliMpPad pad = seg->PadByLocation(AliMpIntPair(nBoard,ibitxy+offset),kTRUE);
560
561 AliMUONDigit* digit = new AliMUONDigit();
562 if (!pad.IsValid()) {
563 AliWarning(Form("No pad for detElemId: %d, nboard %d, ibitxy: %d\n",
564 detElemId, nBoard, ibitxy));
565 continue;
566 } //
567
568 Int_t padX = pad.GetIndices().GetFirst();
569 Int_t padY = pad.GetIndices().GetSecond();
570
571 // file digit
572 digit->SetSignal(1);
573 digit->SetPadX(padX);
574 digit->SetPadY(padY);
575 digit->SetCathode(iCath);
576 digit->SetDetElemId(detElemId);
577 digit->SetElectronics(nBoard, ibitxy);
578 digitList.Add(digit);
579
580 }// xyPattern
581 }// ibitxy
582 }// case
583
584 return 1;
585
586}
587
588//____________________________________________________________________
589void MUONData::GetTriggerChamber(AliMUONLocalStruct* localStruct, Int_t& xyPattern, Int_t& iChamber, Int_t& iCath, Int_t icase)
590{
591 //
592 // extract digits pattern
593 //
594
595 // get chamber & cathode number, (chamber starts at 0 !)
596 switch(icase) {
597 case 0:
598 xyPattern = localStruct->GetX1();
599 iCath = 0;
600 iChamber = 10;
601 break;
602 case 1:
603 xyPattern = localStruct->GetX2();
604 iCath = 0;
605 iChamber = 11;
606 break;
607 case 2:
608 xyPattern = localStruct->GetX3();
609 iCath = 0;
610 iChamber = 12;
611 break;
612 case 3:
613 xyPattern = localStruct->GetX4();
614 iCath = 0;
615 iChamber = 13;
616 break;
617 case 4:
618 xyPattern = localStruct->GetY1();
619 iCath = 1;
620 iChamber = 10;
621 break;
622 case 5:
623 xyPattern = localStruct->GetY2();
624 iCath = 1;
625 iChamber = 11;
626 break;
627 case 6:
628 xyPattern = localStruct->GetY3();
629 iCath = 1;
630 iChamber = 12;
631 break;
632 case 7:
633 xyPattern = localStruct->GetY4();
634 iCath = 1;
635 iChamber = 13;
636 break;
637 }
638
639}
640
641//______________________________________________________________________
642MUONChamberData* MUONData::GetChamberData(Int_t chamber)
643{
644 //
645 // return chamber data
646 //
647
648 if (chamber < 0 || chamber > 13) return 0;
649
650 //if (fChambers[chamber] == 0) CreateChamber(chamber);
651
652 return fChambers[chamber];
653
654}