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"
41 #include "AliMUONSubEventTrigger.h"
42 #include "AliMUONDDLTracker.h"
43 #include "AliMUONDDLTrigger.h"
45 #include "AliMUONLocalTrigger.h"
46 #include "AliMUONGlobalTrigger.h"
48 #include "AliMUONGeometrySegmentation.h"
49 #include "AliMUONSegmentManuIndex.h"
53 const Int_t AliMUONRawData::fgkDefaultPrintLevel = 0;
55 ClassImp(AliMUONRawData) // Class implementation in ROOT context
57 //__________________________________________________________________________
58 AliMUONRawData::AliMUONRawData(AliLoader* loader)
62 // Standard Constructor
64 fPrintLevel = fgkDefaultPrintLevel;
66 // initialize loader's
69 // initialize container
70 fMUONData = new AliMUONData(fLoader,"MUON","MUON");
73 fSubEventArray[0] = new TClonesArray("AliMUONSubEventTracker",1000);
74 fSubEventArray[1] = new TClonesArray("AliMUONSubEventTracker",1000);
76 // initialize array not used for the moment
77 fSubEventTrigArray[0] = new TClonesArray("AliMUONSubEventTrigger",1000);
78 fSubEventTrigArray[1] = new TClonesArray("AliMUONSubEventTrigger",1000);
81 fDDLTracker = new AliMUONDDLTracker();
82 fDDLTrigger = new AliMUONDDLTrigger();
85 //__________________________________________________________________________
86 AliMUONRawData::AliMUONRawData()
89 fPrintLevel(fgkDefaultPrintLevel),
93 // Default Constructor
96 //_______________________________________________________________________
97 AliMUONRawData::AliMUONRawData (const AliMUONRawData& rhs)
100 // Protected copy constructor
102 AliFatal("Not implemented.");
105 //_______________________________________________________________________
107 AliMUONRawData::operator=(const AliMUONRawData& rhs)
109 // Protected assignement operator
111 if (this == &rhs) return *this;
113 AliFatal("Not implemented.");
118 //__________________________________________________________________________
119 AliMUONRawData::~AliMUONRawData(void)
123 if (fSubEventArray[0])
124 fSubEventArray[0]->Delete(); //using delete cos allocating memory in copy ctor.
125 if (fSubEventArray[1])
126 fSubEventArray[1]->Delete();
128 if (fSubEventTrigArray[0])
129 fSubEventTrigArray[0]->Delete();
130 if (fSubEventTrigArray[1])
131 fSubEventTrigArray[1]->Delete();
140 //____________________________________________________________________
141 Int_t AliMUONRawData::WriteRawData()
143 // convert digits of the current event to raw data
148 fLoader->LoadDigits("READ");
150 fMUONData->SetTreeAddress("D,GLT");
155 for (Int_t ich = 0; ich < AliMUONConstants::NTrackingCh(); ich++) {
158 idDDL = ich * 2 + 0x900;
159 sprintf(name, "MUON_%d.ddl",idDDL);
160 fFile1 = fopen(name,"w");
162 idDDL = (ich * 2) + 1 + 0x900;
163 sprintf(name, "MUON_%d.ddl",idDDL);
164 fFile2 = fopen(name,"w");
166 WriteTrackerDDL(ich);
171 fMUONData->ResetDigits();
178 sprintf(name, "MUTR_%d.ddl",idDDL);
179 fFile1 = fopen(name,"w");
182 sprintf(name, "MUTR_%d.ddl",idDDL);
183 fFile2 = fopen(name,"w");
190 fMUONData->ResetTrigger();
192 fLoader->UnloadDigits();
196 //____________________________________________________________________
197 Int_t AliMUONRawData::WriteTrackerDDL(Int_t iCh)
200 TClonesArray* muonDigits = 0;
201 fSubEventArray[0]->Clear();
202 fSubEventArray[1]->Clear();
207 nbInBus[0].Set(5000);
208 nbInBus[1].Set(5000);
214 AliRawDataHeader header = fDDLTracker->GetHeader();
215 Int_t headerSize = fDDLTracker->GetHeaderSize();
217 // DDL event one per half chamber
218 AliMUONSubEventTracker* subEvent;
223 UChar_t channelId = 0;
225 Int_t busPatchId = 0;
238 const AliMUONDigit* digit;
241 // AliMUONChamber* iChamber = 0x0;
242 // AliMUONGeometrySegmentation* segmentation2[2];
244 // pMUON = (AliMUON*) gAlice->GetModule("MUON");
245 // iChamber = &(pMUON->Chamber(iCh));
247 // segmentation2[0]=iChamber->SegmentationModel2(1); // cathode 0
248 // segmentation2[1]=iChamber->SegmentationModel2(2); // cathode 1
250 AliDebug(1, Form("WriteDDL chamber %d\n", iCh+1));
252 for (Int_t iCath = 0; iCath < 2; iCath++) {
254 fMUONData->ResetDigits();
255 fMUONData->GetCathode(iCath);
256 muonDigits = fMUONData->Digits(iCh);
258 nDigits = muonDigits->GetEntriesFast();
259 if (fPrintLevel == 2)
260 printf("ndigits = %d\n",nDigits);
262 // open DDL file, on per 1/2 chamber
264 for (Int_t idig = 0; idig < nDigits; idig++) {
266 digit = (AliMUONDigit*) muonDigits->UncheckedAt(idig);
268 padX = digit->PadX();
269 padY = digit->PadY();
270 charge = digit->Signal();
272 cathode = digit->Cathode();
273 detElemId = digit->DetElemId();
276 // if (detElemId == 0) {
277 // AliWarning("\ndetElemId = 0, old segmentation !\n");
278 GetDummyMapping(iCh, iCath, digit, busPatchId, manuId, channelId);
280 // // mapping (not far from real one)
281 // AliMUONSegmentManuIndex* connect = segmentation2[iCath]->GetMpConnection(detElemId, padX, padY);
282 // if (connect != 0x0) {
283 // busPatchId = connect->GetBusPatchId();
284 // manuId = connect->GetManuId();
285 // channelId = connect->GetManuChannelId();
286 // AliDebug(3,Form("busPatchId %d, manuId: %d, channelId: %d\n", busPatchId, manuId, channelId));
295 AliBitPacking::PackWord((UInt_t)parity,word,29,31);
296 AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
297 AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
298 AliBitPacking::PackWord((UInt_t)charge,word,0,11);
301 subEvent = new AliMUONSubEventTracker();
302 subEvent->AddData(word);
303 subEvent->SetBusPatchId(busPatchId);
304 if (digit->PadX() > 0) {
305 nbInBus[0][busPatchId]++;
308 nbInBus[1][busPatchId]++;
314 fSubEventArray[0]->Sort();
315 fSubEventArray[1]->Sort();
317 // gather datas from same bus patch
318 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
319 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
321 for (Int_t i = 0; i < nEntries; i++) {
322 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(i);
323 busPatchId = temp->GetBusPatchId();
325 // add bus patch header, length and total length managed by subevent class
326 temp->SetTriggerWord(0xdeadbeef);
327 for (Int_t j = 0; j < nbInBus[iDDL][busPatchId]-1; j++) {
328 AliMUONSubEventTracker* temp1 = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(++i);
329 temp->AddData(temp1->GetData(0));
330 fSubEventArray[iDDL]->RemoveAt(i) ;
333 fSubEventArray[iDDL]->Compress();
335 if (fPrintLevel == 3) {
336 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
337 for (Int_t i = 0; i < nEntries; i++) {
338 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(i);
339 printf("busPatchid back %d\n",temp->GetBusPatchId());
340 for (Int_t j = 0; j < temp->GetLength(); j++) {
341 printf("manuId back %d, ",temp->GetManuId(j));
342 printf("channelId back %d, ",temp->GetChannelId(j));
343 printf("charge back %d\n",temp->GetCharge(j));
355 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
359 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
360 buffer = new Int_t [(2048+24)*50]; // 24 words in average for one buspatch and 2048 manu info at most
368 for (Int_t iBlock = 0; iBlock < 2; iBlock++) {
371 // fDDLTracker->SetTotalBlkLength(0xFFFFFFFF);
372 length = fDDLTracker->GetBlkHeaderLength();
373 memcpy(&buffer[index],fDDLTracker->GetBlkHeader(),length*4);
377 for (Int_t iDsp = 0; iDsp < 5; iDsp++) {
380 // fDDLTracker->SetTotalDspLength(0xEEEEEEEE);
381 length = fDDLTracker->GetDspHeaderLength();
382 memcpy(&buffer[index],fDDLTracker->GetDspHeader(),length*4);
386 for (Int_t i = 0; i < 5; i++) {
388 iBusPatch = i + iBlock*25 + iDsp*5 + 50*(2*iCh + iDDL);
390 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(iEntries);
392 busPatchId = temp->GetBusPatchId();
396 if (busPatchId == iBusPatch) {
397 // add bus patch structure
398 length = temp->GetHeaderLength();
399 memcpy(&buffer[index],temp->GetAddress(),length*4);
401 for (Int_t j = 0; j < temp->GetLength(); j++)
402 buffer[index++] = temp->GetData(j);
403 if (iEntries < nEntries-1)
406 buffer[index++] = 4; // total length
407 buffer[index++] = 0; // raw data length
408 buffer[index++] = iBusPatch; // bus patch
409 buffer[index++] = 0xdeadbeef; // trigger word
412 buffer[indexDsp] = index - indexDsp; // dsp length
413 buffer[indexDsp+1] = index - indexDsp - fDDLTracker->GetDspHeaderLength();
414 if ((index - indexDsp) % 2 == 0)
415 buffer[indexDsp+7] = 0;
417 buffer[indexDsp+7] = 1;
419 buffer[indexBlk] = index - indexBlk; // block length
420 buffer[indexBlk+1] = index - indexBlk - fDDLTracker->GetBlkHeaderLength();
424 header.fSize = (index + headerSize) * 4;// total length in bytes
425 fwrite((char*)(&header),headerSize*4,1,fFile1);
426 fwrite(buffer,sizeof(int),index,fFile1);
430 header.fSize = (index + headerSize) * 4;// total length in bytes
431 fwrite((char*)(&header),headerSize*4,1,fFile2);
432 fwrite(buffer,sizeof(int),index,fFile2);
439 //____________________________________________________________________
440 Int_t AliMUONRawData::WriteTriggerDDL()
443 // DDL event one per half chamber
444 AliMUONSubEventTrigger* subEvent = 0x0;
447 // stored local id number
448 TArrayI isFired(256);
453 AliRawDataHeader header = fDDLTrigger->GetHeader();
454 Int_t headerSize = fDDLTrigger->GetHeaderSize();
456 TClonesArray* localTrigger;
457 TClonesArray* globalTrigger;
458 AliMUONGlobalTrigger* gloTrg;
459 AliMUONLocalTrigger* locTrg = 0x0;
461 fMUONData->GetTriggerD();
463 // global trigger for trigger pattern
464 globalTrigger = fMUONData->GlobalTrigger();
465 gloTrg = (AliMUONGlobalTrigger*)globalTrigger->UncheckedAt(0);
466 Int_t gloTrigPat = GetGlobalTriggerPattern(gloTrg);
469 localTrigger = fMUONData->LocalTrigger();
475 Int_t iLocCard, locCard;
476 Char_t locDec, trigY, posY, devX, posX,regOut;
477 Int_t version = 1; // software version
478 Int_t eventType = 1; // trigger type: 1 for physics ?
479 Int_t serialNb = 0xF; // serial nb of card: all bits on for the moment
480 Int_t globalFlag = 1; // set to 2 if global info present in DDL else set to 1
482 Int_t nEntries = (Int_t) (localTrigger->GetEntries());// 234 local cards
483 // stored the local card id that's fired
484 for (Int_t i = 0; i < nEntries; i++) {
485 locTrg = (AliMUONLocalTrigger*)localTrigger->At(i);
486 isFired[locTrg->LoCircuit()] = 1;
490 AliError("No Trigger information available");
492 buffer = new Int_t [672]; // [16(local)*5 words + 3 words]*8(reg) + 8 words = 672
494 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
498 // DDL enhanced header
500 AliBitPacking::PackWord((UInt_t)iDDL+1,word,28,31); //see AliMUONDDLTrigger.h for details
501 AliBitPacking::PackWord((UInt_t)serialNb,word,24,27);
502 AliBitPacking::PackWord((UInt_t)version,word,16,23);
503 AliBitPacking::PackWord((UInt_t)eventType,word,12,15);
505 if (iDDL == 0) // suppose global info in DDL one
509 AliBitPacking::PackWord((UInt_t)globalFlag,word,8,11);
510 fDDLTrigger->SetDDLWord(word);
513 fDDLTrigger->SetGlobalOutput(gloTrigPat);// no global input for the moment....
515 fDDLTrigger->SetGlobalOutput(0);
516 length = fDDLTrigger->GetHeaderLength();
517 memcpy(&buffer[index],fDDLTrigger->GetEnhancedHeader(),length*4);
520 for (Int_t iReg = 0; iReg < 8; iReg++) {
522 subEvent = new AliMUONSubEventTrigger();
524 // Regional card header
527 AliBitPacking::PackWord((UInt_t)serialNb,word,24,28); //see AliMUONSubEventTrigger.h for details
528 AliBitPacking::PackWord((UInt_t)version,word,16,23);
529 AliBitPacking::PackWord((UInt_t)iReg,word,12,15);
530 AliBitPacking::PackWord((UInt_t)regOut,word,0,7); // whenever regional output will be implemented
532 subEvent->SetRegWord(word);
533 memcpy(&buffer[index++],subEvent->GetAddress(),4);
535 buffer[index++] = 0;// 2 words of regional input
538 for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
540 iLocCard = iLoc + iReg*16 + iDDL*128;
542 if (isFired[iLocCard]) {
543 locTrg = (AliMUONLocalTrigger*)localTrigger->At(iEntries);
544 locCard = locTrg->LoCircuit();
545 locDec = locTrg->GetLoDecision();
547 posY = locTrg->LoStripY();
548 posX = locTrg->LoStripX();
549 devX = locTrg->LoDev();
550 if (fPrintLevel == 4)
551 printf("loctrg %d, posX %d, posY %d, devX %d\n",
552 locTrg-> LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev());
553 } else { //no trigger (see PRR chpt 3.4)
564 AliBitPacking::PackWord((UInt_t)(iLocCard % 16),word,19,22); //card id number in crate
565 AliBitPacking::PackWord((UInt_t)locDec,word,15,18);
566 AliBitPacking::PackWord((UInt_t)trigY,word,14,14);
567 AliBitPacking::PackWord((UInt_t)posY,word,10,13);
568 AliBitPacking::PackWord((UInt_t)devX,word,5,9);
569 AliBitPacking::PackWord((UInt_t)posX,word,0,4);
571 if (locCard == iLocCard) {
572 // add local cards structure
573 buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
574 buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
575 buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
576 buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
577 buffer[index++] = (Int_t)word; // data word
578 if (iEntries < nEntries-1)
581 buffer[index++] = 0; // 4 words for x1, x2, y1, y2
585 buffer[index++] = (Int_t)word; // data word
594 buffer[index++] = fDDLTrigger->GetEoD(); // End of DDL word
595 buffer[index++] = fDDLTrigger->GetEoD(); // End of DDL word for 64 bits transfer purpose
600 header.fSize = (index + headerSize) * 4;// total length in bytes
601 fwrite((char*)(&header),headerSize*4,1,fFile1);
602 fwrite(buffer,sizeof(int),index,fFile1);
606 header.fSize = (index + headerSize) * 4;// total length in bytes
607 fwrite((char*)(&header),headerSize*4,1,fFile2);
608 fwrite(buffer,sizeof(int),index,fFile2);
615 //____________________________________________________________________
616 void AliMUONRawData::GetDummyMapping(Int_t iCh, Int_t iCath, const AliMUONDigit* digit,
617 Int_t &busPatchId, UShort_t &manuId, UChar_t &channelId)
619 // Dummy mapping for tracker
621 Int_t offsetX = 0; // offet row
622 Int_t offsetY = 0; // offset columns
623 Int_t offsetCath = 0; //offset from one cathod to the other
624 Int_t maxChannel = 0; // maximum nb of channel in 1/2 chamber
634 maxChannel = (offsetY * offsetX + 2* offsetY + offsetCath);
645 maxChannel = (256 * offsetX + offsetX + offsetCath);
649 // manu Id directly from a matrix 8*8, same segmentation for B and NB
650 // 50 buspatches for 1/2 chamber
652 id = (TMath::Abs(digit->PadX()) * offsetX + digit->PadY() + offsetY +
654 Int_t chPerBus = maxChannel/50;
655 busPatchId = id/chPerBus; // start at zero
656 if (digit->PadX() > 0)
657 busPatchId += 50*iCh*2;
659 busPatchId += 50*(2*iCh+1);
660 // 64 manu cards for one buspatch
661 manuId = (id % chPerBus)/64; //start at zero
662 manuId &= 0x7FF; // 11 bits
665 channelId = (id % chPerBus) % 64; //start at zero
666 channelId &= 0x3F; // 6 bits
669 AliDebug(2,Form("id: %d, busPatchId %d, manuId: %d, channelId: %d, maxchannel: %d, chPerBus %d\n",
670 id, busPatchId, manuId, channelId, maxChannel, chPerBus));
672 AliDebug(2,Form("id: %d, busPatchId %d, manuId: %d, channelId: %d, padx: %d pady %d, charge %d\n",
673 id, busPatchId, manuId, channelId, digit->PadX(), digit->PadY(), digit->Signal()));
677 //____________________________________________________________________
678 Int_t AliMUONRawData::GetGlobalTriggerPattern(const AliMUONGlobalTrigger* gloTrg)
680 // global trigger pattern calculation
682 Int_t gloTrigPat = 0;
684 if (gloTrg->SinglePlusLpt()) gloTrigPat|= 0x1;
685 if (gloTrg->SinglePlusHpt()) gloTrigPat|= 0x2;
686 if (gloTrg->SinglePlusApt()) gloTrigPat|= 0x4;
688 if (gloTrg->SingleMinusLpt()) gloTrigPat|= 0x8;
689 if (gloTrg->SingleMinusHpt()) gloTrigPat|= 0x10;
690 if (gloTrg->SingleMinusApt()) gloTrigPat|= 0x20;
692 if (gloTrg->SingleUndefLpt()) gloTrigPat|= 0x40;
693 if (gloTrg->SingleUndefHpt()) gloTrigPat|= 0x80;
694 if (gloTrg->SingleUndefApt()) gloTrigPat|= 0x100;
696 if (gloTrg->PairUnlikeLpt()) gloTrigPat|= 0x200;
697 if (gloTrg->PairUnlikeHpt()) gloTrigPat|= 0x400;
698 if (gloTrg->PairUnlikeApt()) gloTrigPat|= 0x800;
700 if (gloTrg->PairLikeLpt()) gloTrigPat|= 0x1000;
701 if (gloTrg->PairLikeHpt()) gloTrigPat|= 0x2000;
702 if (gloTrg->PairLikeApt()) gloTrigPat|= 0x4000;