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 ////////////////////////////////////
18 // MUON Raw Data generator in ALICE-MUON
20 // This class version 1 (further details could be found in Alice-note coming soon right in our direction)
21 // Generates raw data for MUON tracker and finally for trigger
22 // * a simple mapping is used (see below)
23 // * the bus patch id is calculated with an absolute number 0 - 999
24 // * one DDL per 1/2 chamber is created for both cathode.
25 // For trigger there is no mapping (mapping could be found in AliMUONTriggerCircuit)
26 // don't need for digits2raw but needed in raw2Reco
27 // the position are given per local card
28 ////////////////////////////////////
30 #include <TClonesArray.h>
31 #include "AliMUONRawData.h"
32 #include "AliMUONDigit.h"
35 #include "AliMUONChamber.h"
36 #include "AliMUONConstants.h"
37 #include "AliMUONData.h"
38 #include "AliLoader.h"
39 #include "AliBitPacking.h"
40 #include "AliRawReader.h"
42 #include "AliMUONSubEventTrigger.h"
43 #include "AliMUONDDLTracker.h"
44 #include "AliMUONDDLTrigger.h"
46 #include "AliMUONLocalTrigger.h"
47 #include "AliMUONGlobalTrigger.h"
49 #include "AliMUONGeometrySegmentation.h"
54 ClassImp(AliMUONRawData) // Class implementation in ROOT context
56 //__________________________________________________________________________
57 AliMUONRawData::AliMUONRawData(AliLoader* loader)
60 // Standard Constructor
62 // initialize loader's
65 // initialize container
66 fMUONData = new AliMUONData(fLoader,"MUON","MUON");
69 fSubEventArray[0] = new TClonesArray("AliMUONSubEventTracker",1000);
70 fSubEventArray[1] = new TClonesArray("AliMUONSubEventTracker",1000);
72 // initialize array not used for the moment
73 fSubEventTrigArray[0] = new TClonesArray("AliMUONSubEventTrigger",1000);
74 fSubEventTrigArray[1] = new TClonesArray("AliMUONSubEventTrigger",1000);
77 fDDLTracker = new AliMUONDDLTracker();
78 fDDLTrigger = new AliMUONDDLTrigger();
81 //__________________________________________________________________________
82 AliMUONRawData::AliMUONRawData()
87 // Default Constructor
90 //_______________________________________________________________________
91 AliMUONRawData::AliMUONRawData (const AliMUONRawData& rhs)
94 // Protected copy constructor
96 AliFatal("Not implemented.");
99 //_______________________________________________________________________
101 AliMUONRawData::operator=(const AliMUONRawData& rhs)
103 // Protected assignement operator
105 if (this == &rhs) return *this;
107 AliFatal("Not implemented.");
112 //__________________________________________________________________________
113 AliMUONRawData::~AliMUONRawData(void)
117 if (fSubEventArray[0])
118 fSubEventArray[0]->Delete(); //using delete cos allocating memory in copy ctor.
119 if (fSubEventArray[1])
120 fSubEventArray[1]->Delete();
122 if (fSubEventTrigArray[0])
123 fSubEventTrigArray[0]->Delete();
124 if (fSubEventTrigArray[1])
125 fSubEventTrigArray[1]->Delete();
134 //____________________________________________________________________
135 Int_t AliMUONRawData::Digits2Raw()
137 // convert digits of the current event to raw data
142 fLoader->LoadDigits("READ");
144 fMUONData->SetTreeAddress("D,GLT");
149 for (Int_t ich = 0; ich < AliMUONConstants::NTrackingCh(); ich++) {
152 idDDL = ich * 2 + 0x900;
153 sprintf(name, "MUON_%d.ddl",idDDL);
154 fFile1 = fopen(name,"w");
156 idDDL = (ich * 2) + 1 + 0x900;
157 sprintf(name, "MUON_%d.ddl",idDDL);
158 fFile2 = fopen(name,"w");
160 WriteTrackerDDL(ich);
165 fMUONData->ResetDigits();
172 sprintf(name, "MUTR_%d.ddl",idDDL);
173 fFile1 = fopen(name,"w");
176 sprintf(name, "MUTR_%d.ddl",idDDL);
177 fFile2 = fopen(name,"w");
184 fMUONData->ResetTrigger();
186 fLoader->UnloadDigits();
190 //____________________________________________________________________
191 Int_t AliMUONRawData::WriteTrackerDDL(Int_t iCh)
194 TClonesArray* muonDigits = 0;
195 fSubEventArray[0]->Clear();
196 fSubEventArray[1]->Clear();
201 nbInBus[0].Set(5000);
202 nbInBus[1].Set(5000);
208 AliRawDataHeader header = fDDLTracker->GetHeader();
209 Int_t headerSize = fDDLTracker->GetHeaderSize();
211 // DDL event one per half chamber
212 AliMUONSubEventTracker* subEvent;
217 UChar_t channelId = 0;
219 Int_t busPatchId = 0;
232 const AliMUONDigit* digit;
235 // AliMUONChamber* iChamber = 0x0;
236 // AliMUONGeometrySegmentation* segmentation2[2];
238 // pMUON = (AliMUON*) gAlice->GetModule("MUON");
239 // iChamber = &(pMUON->Chamber(iCh));
241 // segmentation2[0]=iChamber->SegmentationModel2(1); // cathode 0
242 // segmentation2[1]=iChamber->SegmentationModel2(2); // cathode 1
244 AliDebug(1, Form("WriteDDL chamber %d\n", iCh+1));
246 // for (Int_t iCath = 0; iCath < 2; iCath++) {
248 fMUONData->ResetDigits();
249 fMUONData->GetDigits();
250 muonDigits = fMUONData->Digits(iCh);
252 nDigits = muonDigits->GetEntriesFast();
253 AliDebug(2,Form("ndigits = %d\n",nDigits));
255 // open DDL file, on per 1/2 chamber
257 for (Int_t idig = 0; idig < nDigits; idig++) {
259 digit = (AliMUONDigit*) muonDigits->UncheckedAt(idig);
261 padX = digit->PadX();
262 padY = digit->PadY();
263 charge = digit->Signal();
265 cathode = digit->Cathode();
266 detElemId = digit->DetElemId();
269 // if (detElemId == 0) {
270 // AliWarning("\ndetElemId = 0, old segmentation !\n");
271 GetDummyMapping(iCh, cathode, digit, busPatchId, manuId, channelId);
273 // // mapping (not far from real one)
274 // AliMUONSegmentManuIndex* connect = segmentation2[iCath]->GetMpConnection(detElemId, padX, padY);
275 // if (connect != 0x0) {
276 // busPatchId = connect->GetBusPatchId();
277 // manuId = connect->GetManuId();
278 // channelId = connect->GetManuChannelId();
279 // AliDebug(3,Form("busPatchId %d, manuId: %d, channelId: %d\n", busPatchId, manuId, channelId));
288 AliBitPacking::PackWord((UInt_t)parity,word,29,31);
289 AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
290 AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
291 AliBitPacking::PackWord((UInt_t)charge,word,0,11);
294 subEvent = new AliMUONSubEventTracker();
295 subEvent->AddData(word);
296 subEvent->SetBusPatchId(busPatchId);
297 if (digit->PadX() > 0) {
298 nbInBus[0][busPatchId]++;
301 nbInBus[1][busPatchId]++;
307 fSubEventArray[0]->Sort();
308 fSubEventArray[1]->Sort();
310 // gather datas from same bus patch
311 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
312 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
314 for (Int_t i = 0; i < nEntries; i++) {
315 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(i);
316 busPatchId = temp->GetBusPatchId();
318 // add bus patch header, length and total length managed by subevent class
319 temp->SetTriggerWord(0xdeadbeef);
320 for (Int_t j = 0; j < nbInBus[iDDL][busPatchId]-1; j++) {
321 AliMUONSubEventTracker* temp1 = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(++i);
322 temp->AddData(temp1->GetData(0));
323 fSubEventArray[iDDL]->RemoveAt(i) ;
326 fSubEventArray[iDDL]->Compress();
328 if (AliLog::GetGlobalDebugLevel() == 3) {
329 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
330 for (Int_t i = 0; i < nEntries; i++) {
331 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(i);
332 printf("busPatchid back %d\n",temp->GetBusPatchId());
333 for (Int_t j = 0; j < temp->GetLength(); j++) {
334 printf("manuId back %d, ",temp->GetManuId(j));
335 printf("channelId back %d, ",temp->GetChannelId(j));
336 printf("charge back %d\n",temp->GetCharge(j));
348 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
352 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
353 buffer = new Int_t [(2048+24)*50]; // 24 words in average for one buspatch and 2048 manu info at most
361 for (Int_t iBlock = 0; iBlock < 2; iBlock++) {
364 // fDDLTracker->SetTotalBlkLength(0xFFFFFFFF);
365 length = fDDLTracker->GetBlkHeaderLength();
366 memcpy(&buffer[index],fDDLTracker->GetBlkHeader(),length*4);
370 for (Int_t iDsp = 0; iDsp < 5; iDsp++) {
373 // fDDLTracker->SetTotalDspLength(0xEEEEEEEE);
374 length = fDDLTracker->GetDspHeaderLength();
375 memcpy(&buffer[index],fDDLTracker->GetDspHeader(),length*4);
379 for (Int_t i = 0; i < 5; i++) {
381 iBusPatch = i + iBlock*25 + iDsp*5 + 50*(2*iCh + iDDL);
383 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(iEntries);
385 busPatchId = temp->GetBusPatchId();
389 if (busPatchId == iBusPatch) {
390 // add bus patch structure
391 length = temp->GetHeaderLength();
392 memcpy(&buffer[index],temp->GetAddress(),length*4);
394 for (Int_t j = 0; j < temp->GetLength(); j++)
395 buffer[index++] = temp->GetData(j);
396 if (iEntries < nEntries-1)
399 buffer[index++] = 4; // total length
400 buffer[index++] = 0; // raw data length
401 buffer[index++] = iBusPatch; // bus patch
402 buffer[index++] = 0xdeadbeef; // trigger word
405 buffer[indexDsp] = index - indexDsp; // dsp length
406 buffer[indexDsp+1] = index - indexDsp - fDDLTracker->GetDspHeaderLength();
407 if ((index - indexDsp) % 2 == 0)
408 buffer[indexDsp+7] = 0;
410 buffer[indexDsp+7] = 1;
412 buffer[indexBlk] = index - indexBlk; // block length
413 buffer[indexBlk+1] = index - indexBlk - fDDLTracker->GetBlkHeaderLength();
417 header.fSize = (index + headerSize) * 4;// total length in bytes
418 fwrite((char*)(&header),headerSize*4,1,fFile1);
419 fwrite(buffer,sizeof(int),index,fFile1);
423 header.fSize = (index + headerSize) * 4;// total length in bytes
424 fwrite((char*)(&header),headerSize*4,1,fFile2);
425 fwrite(buffer,sizeof(int),index,fFile2);
432 //____________________________________________________________________
433 Int_t AliMUONRawData::WriteTriggerDDL()
436 // DDL event one per half chamber
437 AliMUONSubEventTrigger* subEvent = 0x0;
440 // stored local id number
441 TArrayI isFired(256);
446 AliRawDataHeader header = fDDLTrigger->GetHeader();
447 Int_t headerSize = fDDLTrigger->GetHeaderSize();
449 TClonesArray* localTrigger;
450 TClonesArray* globalTrigger;
451 AliMUONGlobalTrigger* gloTrg;
452 AliMUONLocalTrigger* locTrg = 0x0;
454 fMUONData->GetTriggerD();
456 // global trigger for trigger pattern
457 globalTrigger = fMUONData->GlobalTrigger();
458 gloTrg = (AliMUONGlobalTrigger*)globalTrigger->UncheckedAt(0);
459 Int_t gloTrigPat = GetGlobalTriggerPattern(gloTrg);
462 localTrigger = fMUONData->LocalTrigger();
468 Int_t iLocCard, locCard;
469 Char_t locDec, trigY, posY, devX, posX,regOut;
470 Int_t version = 1; // software version
471 Int_t eventType = 1; // trigger type: 1 for physics ?
472 Int_t serialNb = 0xF; // serial nb of card: all bits on for the moment
473 Int_t globalFlag = 1; // set to 2 if global info present in DDL else set to 1
475 Int_t nEntries = (Int_t) (localTrigger->GetEntries());// 234 local cards
476 // stored the local card id that's fired
477 for (Int_t i = 0; i < nEntries; i++) {
478 locTrg = (AliMUONLocalTrigger*)localTrigger->At(i);
479 isFired[locTrg->LoCircuit()] = 1;
483 AliError("No Trigger information available");
485 buffer = new Int_t [672]; // [16(local)*5 words + 3 words]*8(reg) + 8 words = 672
487 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
491 // DDL enhanced header
493 AliBitPacking::PackWord((UInt_t)iDDL+1,word,28,31); //see AliMUONDDLTrigger.h for details
494 AliBitPacking::PackWord((UInt_t)serialNb,word,24,27);
495 AliBitPacking::PackWord((UInt_t)version,word,16,23);
496 AliBitPacking::PackWord((UInt_t)eventType,word,12,15);
498 if (iDDL == 0) // suppose global info in DDL one
502 AliBitPacking::PackWord((UInt_t)globalFlag,word,8,11);
503 fDDLTrigger->SetDDLWord(word);
506 fDDLTrigger->SetGlobalOutput(gloTrigPat);// no global input for the moment....
508 fDDLTrigger->SetGlobalOutput(0);
509 length = fDDLTrigger->GetHeaderLength();
510 memcpy(&buffer[index],fDDLTrigger->GetEnhancedHeader(),length*4);
513 for (Int_t iReg = 0; iReg < 8; iReg++) {
515 subEvent = new AliMUONSubEventTrigger();
517 // Regional card header
520 AliBitPacking::PackWord((UInt_t)serialNb,word,24,28); //see AliMUONSubEventTrigger.h for details
521 AliBitPacking::PackWord((UInt_t)version,word,16,23);
522 AliBitPacking::PackWord((UInt_t)iReg,word,12,15);
523 AliBitPacking::PackWord((UInt_t)regOut,word,0,7); // whenever regional output will be implemented
525 subEvent->SetRegWord(word);
526 memcpy(&buffer[index++],subEvent->GetAddress(),4);
528 buffer[index++] = 0;// 2 words of regional input
531 for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
533 iLocCard = iLoc + iReg*16 + iDDL*128;
535 if (isFired[iLocCard]) {
536 locTrg = (AliMUONLocalTrigger*)localTrigger->At(iEntries);
537 locCard = locTrg->LoCircuit();
538 locDec = locTrg->GetLoDecision();
540 posY = locTrg->LoStripY();
541 posX = locTrg->LoStripX();
542 devX = locTrg->LoDev();
543 AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n",
544 locTrg-> LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
545 } else { //no trigger (see PRR chpt 3.4)
556 AliBitPacking::PackWord((UInt_t)(iLocCard % 16),word,19,22); //card id number in crate
557 AliBitPacking::PackWord((UInt_t)locDec,word,15,18);
558 AliBitPacking::PackWord((UInt_t)trigY,word,14,14);
559 AliBitPacking::PackWord((UInt_t)posY,word,10,13);
560 AliBitPacking::PackWord((UInt_t)devX,word,5,9);
561 AliBitPacking::PackWord((UInt_t)posX,word,0,4);
563 if (locCard == iLocCard) {
564 // add local cards structure
565 buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
566 buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
567 buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
568 buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
569 buffer[index++] = (Int_t)word; // data word
570 if (iEntries < nEntries-1)
573 buffer[index++] = 0; // 4 words for x1, x2, y1, y2
577 buffer[index++] = (Int_t)word; // data word
586 buffer[index++] = fDDLTrigger->GetEoD(); // End of DDL word
587 buffer[index++] = fDDLTrigger->GetEoD(); // End of DDL word for 64 bits transfer purpose
592 header.fSize = (index + headerSize) * 4;// total length in bytes
593 fwrite((char*)(&header),headerSize*4,1,fFile1);
594 fwrite(buffer,sizeof(int),index,fFile1);
598 header.fSize = (index + headerSize) * 4;// total length in bytes
599 fwrite((char*)(&header),headerSize*4,1,fFile2);
600 fwrite(buffer,sizeof(int),index,fFile2);
607 //____________________________________________________________________
608 void AliMUONRawData::GetDummyMapping(Int_t iCh, Int_t iCath, const AliMUONDigit* digit,
609 Int_t &busPatchId, UShort_t &manuId, UChar_t &channelId)
611 // Dummy mapping for tracker
613 Int_t offsetX = 0; // offet row
614 Int_t offsetY = 0; // offset columns
615 Int_t offsetCath = 0; //offset from one cathod to the other
616 Int_t maxChannel = 0; // maximum nb of channel in 1/2 chamber
626 maxChannel = (offsetY * offsetX + 2* offsetY + offsetCath);
637 maxChannel = (256 * offsetX + offsetX + offsetCath);
641 // manu Id directly from a matrix 8*8, same segmentation for B and NB
642 // 50 buspatches for 1/2 chamber
644 id = (TMath::Abs(digit->PadX()) * offsetX + digit->PadY() + offsetY +
646 Int_t chPerBus = maxChannel/50;
647 busPatchId = id/chPerBus; // start at zero
648 if (digit->PadX() > 0)
649 busPatchId += 50*iCh*2;
651 busPatchId += 50*(2*iCh+1);
652 // 64 manu cards for one buspatch
653 manuId = (id % chPerBus)/64; //start at zero
654 manuId &= 0x7FF; // 11 bits
657 channelId = (id % chPerBus) % 64; //start at zero
658 channelId &= 0x3F; // 6 bits
661 AliDebug(2,Form("id: %d, busPatchId %d, manuId: %d, channelId: %d, maxchannel: %d, chPerBus %d\n",
662 id, busPatchId, manuId, channelId, maxChannel, chPerBus));
664 AliDebug(2,Form("id: %d, busPatchId %d, manuId: %d, channelId: %d, padx: %d pady %d, charge %d\n",
665 id, busPatchId, manuId, channelId, digit->PadX(), digit->PadY(), digit->Signal()));
669 //____________________________________________________________________
670 Int_t AliMUONRawData::GetGlobalTriggerPattern(const AliMUONGlobalTrigger* gloTrg)
672 // global trigger pattern calculation
674 Int_t gloTrigPat = 0;
676 if (gloTrg->SinglePlusLpt()) gloTrigPat|= 0x1;
677 if (gloTrg->SinglePlusHpt()) gloTrigPat|= 0x2;
678 if (gloTrg->SinglePlusApt()) gloTrigPat|= 0x4;
680 if (gloTrg->SingleMinusLpt()) gloTrigPat|= 0x8;
681 if (gloTrg->SingleMinusHpt()) gloTrigPat|= 0x10;
682 if (gloTrg->SingleMinusApt()) gloTrigPat|= 0x20;
684 if (gloTrg->SingleUndefLpt()) gloTrigPat|= 0x40;
685 if (gloTrg->SingleUndefHpt()) gloTrigPat|= 0x80;
686 if (gloTrg->SingleUndefApt()) gloTrigPat|= 0x100;
688 if (gloTrg->PairUnlikeLpt()) gloTrigPat|= 0x200;
689 if (gloTrg->PairUnlikeHpt()) gloTrigPat|= 0x400;
690 if (gloTrg->PairUnlikeApt()) gloTrigPat|= 0x800;
692 if (gloTrg->PairLikeLpt()) gloTrigPat|= 0x1000;
693 if (gloTrg->PairLikeHpt()) gloTrigPat|= 0x2000;
694 if (gloTrg->PairLikeApt()) gloTrigPat|= 0x4000;
699 //____________________________________________________________________
700 Int_t AliMUONRawData::Raw2Digits(AliRawReader* /*rawReader*/)
706 //____________________________________________________________________
707 Int_t AliMUONRawData::ReadTrackerDDL(AliRawReader* /*rawReader*/)
713 //____________________________________________________________________
714 void AliMUONRawData:: GetInvDummyMapping(Int_t /*iCh*/, Int_t /*buspatchId*/, UShort_t /*manuId*/,
715 UChar_t /*channelId*/, AliMUONDigit* /*digit*/ )
720 //____________________________________________________________________
721 Int_t AliMUONRawData::ReadTriggerDDL(AliRawReader* /*rawReader*/)