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"
33 #include "AliMUONTriggerDecision.h"
34 #include "AliMUONConstants.h"
35 #include "AliMUONData.h"
37 #include "AliRunLoader.h"
38 #include "AliLoader.h"
39 #include "AliBitPacking.h"
40 #include "AliMUONDDLTracker.h"
41 #include "AliMUONDDLTrigger.h"
44 #include "AliMUONLocalTrigger.h"
45 #include "AliMUONGlobalTrigger.h"
46 #include "AliMUONTriggerCircuit.h"
48 const Int_t AliMUONRawData::fgkDefaultPrintLevel = 0;
50 ClassImp(AliMUONRawData) // Class implementation in ROOT context
52 //__________________________________________________________________________
53 AliMUONRawData::AliMUONRawData(AliLoader* loader)
57 // Standard Constructor
59 fPrintLevel = fgkDefaultPrintLevel;
61 // initialize loader's
64 // initialize container
65 fMUONData = new AliMUONData(fLoader,"MUON","MUON");
67 // trigger decision, temp solution, local & global has to move to Digits Tree
68 // fTrigDec = new AliMUONTriggerDecision(fLoader);
69 // fTrigData = fTrigDec->GetMUONData();
72 fSubEventArray[0] = new TClonesArray("AliMUONSubEventTracker",1000);
73 fSubEventArray[1] = new TClonesArray("AliMUONSubEventTracker",1000);
76 fSubEventTrigArray[0] = new TClonesArray("AliMUONSubEventTrigger",1000);
77 fSubEventTrigArray[1] = new TClonesArray("AliMUONSubEventTrigger",1000);
80 fDDLTracker = new AliMUONDDLTracker();
81 fDDLTrigger= new AliMUONDDLTrigger();
84 //__________________________________________________________________________
85 AliMUONRawData::AliMUONRawData()
88 fPrintLevel(fgkDefaultPrintLevel),
92 // Default Constructor
95 //_______________________________________________________________________
96 AliMUONRawData::AliMUONRawData (const AliMUONRawData& rhs)
99 // Protected copy constructor
101 Fatal("AliMUONRawData", "Not implemented.");
104 //_______________________________________________________________________
106 AliMUONRawData::operator=(const AliMUONRawData& rhs)
108 // Protected assignement operator
110 if (this == &rhs) return *this;
112 Fatal("operator=", "Not implemented.");
117 //__________________________________________________________________________
118 AliMUONRawData::~AliMUONRawData(void)
122 if (fSubEventArray[0])
123 fSubEventArray[0]->Delete(); //using delete cos allocating memory in copy ctor.
124 if (fSubEventArray[1])
125 fSubEventArray[1]->Delete();
127 if (fSubEventTrigArray[0])
128 fSubEventTrigArray[0]->Delete();
129 if (fSubEventTrigArray[1])
130 fSubEventTrigArray[1]->Delete();
139 //____________________________________________________________________
140 Int_t AliMUONRawData::WriteRawData()
142 // convert digits of the current event to raw data
147 fLoader->LoadDigits("READ");
149 fMUONData->SetTreeAddress("D,GLT");
154 for (Int_t ich = 0; ich < AliMUONConstants::NTrackingCh(); ich++) {
157 DDLId = ich * 2 + 0x900;
158 sprintf(name, "MUON_%d.ddl",DDLId);
159 fFile1 = fopen(name,"w");
161 DDLId = (ich * 2) + 1 + 0x900;
162 sprintf(name, "MUON_%d.ddl",DDLId);
163 fFile2 = fopen(name,"w");
165 WriteTrackerDDL(ich);
170 fMUONData->ResetDigits();
177 sprintf(name, "MUTR_%d.ddl",DDLId);
178 fFile1 = fopen(name,"w");
181 sprintf(name, "MUTR_%d.ddl",DDLId);
182 fFile2 = fopen(name,"w");
189 fMUONData->ResetTrigger();
191 fLoader->UnloadDigits();
195 //____________________________________________________________________
196 Int_t AliMUONRawData::WriteTrackerDDL(Int_t iCh)
199 TClonesArray* muonDigits = 0;
200 fSubEventArray[0]->Clear();
201 fSubEventArray[1]->Clear();
206 nbInBus[0].Set(5000);
207 nbInBus[1].Set(5000);
213 AliRawDataHeader header = fDDLTracker->GetHeader();
214 Int_t headerSize = fDDLTracker->GetHeaderSize();
216 // DDL event one per half chamber
217 AliMUONSubEventTracker* subEvent;
222 UChar_t channelId = 0;
224 Int_t busPatchId = 0;
234 const AliMUONDigit* digit;
236 if (fPrintLevel == 1)
237 printf("WriteDDL chamber %d\n", iCh+1);
239 for (Int_t iCath = 0; iCath < 2; iCath++) {
241 fMUONData->ResetDigits();
242 fMUONData->GetCathode(iCath);
243 muonDigits = fMUONData->Digits(iCh);
245 nDigits = muonDigits->GetEntriesFast();
246 if (fPrintLevel == 2)
247 printf("ndigits = %d\n",nDigits);
249 // open DDL file, on per 1/2 chamber
251 for (Int_t idig = 0; idig < nDigits; idig++) {
253 digit = (AliMUONDigit*) muonDigits->UncheckedAt(idig);
256 GetDummyMapping(iCh, iCath, digit, busPatchId, manuId, channelId, charge);
259 AliBitPacking::PackWord((UInt_t)parity,word,29,31);
260 AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
261 AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
262 AliBitPacking::PackWord((UInt_t)charge,word,0,11);
265 subEvent = new AliMUONSubEventTracker();
266 subEvent->AddData(word);
267 subEvent->SetBusPatchId(busPatchId);
268 if (digit->PadX() > 0) {
269 nbInBus[0][busPatchId]++;
272 nbInBus[1][busPatchId]++;
278 fSubEventArray[0]->Sort();
279 fSubEventArray[1]->Sort();
281 // gather datas from same bus patch
282 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
283 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
285 for (Int_t i = 0; i < nEntries; i++) {
286 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(i);
287 busPatchId = temp->GetBusPatchId();
289 // add bus patch header, length and total length managed by subevent class
290 temp->SetTriggerWord(0xdeadbeef);
291 for (Int_t j = 0; j < nbInBus[iDDL][busPatchId]-1; j++) {
292 AliMUONSubEventTracker* temp1 = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(++i);
293 temp->AddData(temp1->GetData(0));
294 fSubEventArray[iDDL]->RemoveAt(i) ;
297 fSubEventArray[iDDL]->Compress();
299 if (fPrintLevel == 3) {
300 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
301 for (Int_t i = 0; i < nEntries; i++) {
302 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(i);
303 printf("busPatchid back %d\n",temp->GetBusPatchId());
304 for (Int_t j = 0; j < temp->GetLength(); j++) {
305 printf("manuId back %d, ",temp->GetManuId(j));
306 printf("channelId back %d, ",temp->GetChannelId(j));
307 printf("charge back %d\n",temp->GetCharge(j));
318 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
322 nEntries = fSubEventArray[iDDL]->GetEntriesFast();
323 buffer = new Int_t [(2048+24)*50]; // 24 words in average for one buspatch and 2048 manu info at most
331 for (Int_t iBlock = 0; iBlock < 2; iBlock++) {
334 fDDLTracker->SetTotalBlkLength(0xFFFFFFFF);
335 memcpy(&buffer[index],fDDLTracker->GetBlkHeader(),32);
339 for (Int_t iDsp = 0; iDsp < 5; iDsp++) {
342 fDDLTracker->SetTotalDspLength(0xEEEEEEEE);
343 memcpy(&buffer[index],fDDLTracker->GetDspHeader(),32);
347 for (Int_t i = 0; i < 5; i++) {
349 iBusPatch = i + iBlock*25 + iDsp*5 + 50*(2*iCh + iDDL);
351 AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray[iDDL]->At(iEntries);
353 busPatchId = temp->GetBusPatchId();
357 if (busPatchId == iBusPatch) {
358 // add bus patch structure
359 memcpy(&buffer[index],temp->GetAddress(),16);
361 for (Int_t j = 0; j < temp->GetLength(); j++)
362 buffer[index++] = temp->GetData(j);
363 if (iEntries < nEntries-1)
366 buffer[index++] = 4; // total length
367 buffer[index++] = 0; // raw data length
368 buffer[index++] = iBusPatch; // bus patch
369 buffer[index++] = 0xdeadbeef; // trigger word
372 buffer[indexDsp] = index - indexDsp;
373 buffer[indexDsp+1] = index - indexDsp -8;
374 if ((index - indexDsp) % 2 == 0)
375 buffer[indexDsp+7] = 0;
377 buffer[indexDsp+7] = 1;
379 buffer[indexBlk] = index - indexBlk;
380 buffer[indexBlk+1] = index - indexBlk -8;
384 header.fSize = index + headerSize;// total length in word
385 fwrite((char*)(&header),headerSize*4,1,fFile1);
386 fwrite(buffer,sizeof(int),index,fFile1);
390 header.fSize = index + headerSize;// total length in word
391 fwrite((char*)(&header),headerSize*4,1,fFile2);
392 fwrite(buffer,sizeof(int),index,fFile2);
399 //____________________________________________________________________
400 Int_t AliMUONRawData::WriteTriggerDDL()
403 // DDL event one per half chamber
404 AliMUONSubEventTrigger* subEvent = 0x0;
407 // stored local id number
408 TArrayI isFired(256);
413 AliRawDataHeader header = fDDLTrigger->GetHeader();
414 Int_t headerSize = fDDLTrigger->GetHeaderSize();
416 TClonesArray* localTrigger;
417 TClonesArray* globalTrigger;
418 AliMUONGlobalTrigger* gloTrg;
419 AliMUONLocalTrigger* locTrg = 0x0;
421 fMUONData->GetTriggerD();
423 // global trigger for trigger pattern
424 globalTrigger = fMUONData->GlobalTrigger();
425 gloTrg = (AliMUONGlobalTrigger*)globalTrigger->UncheckedAt(0);
426 Int_t gloTrigPat = GetGlobalTriggerPattern(gloTrg);
429 localTrigger = fMUONData->LocalTrigger();
435 Int_t iLocCard, locCard;
436 Char_t locDec, trigY, posY, devX, posX;
437 Int_t version = 1; // software version
438 Int_t eventType =1; // trigger type: 1 for physics ?
439 Int_t serialNb = 0xF; // serial nb of card: all bits on for the moment
441 Int_t nEntries = (Int_t) (localTrigger->GetEntries());// 234 local cards
442 // stored the local card id that's fired
443 for (Int_t i = 0; i < nEntries; i++) {
444 locTrg = (AliMUONLocalTrigger*)localTrigger->At(i);
445 isFired[locTrg->LoCircuit()] = 1;
449 Error("AliMUONRawData::WriteTriggerDDL","No Trigger information available");
451 buffer = new Int_t [680]; // [16(local)*5 words + 4 words]*8(reg) + 8 words = 680
453 for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
457 // DDL enhanced header
459 AliBitPacking::PackWord((UInt_t)iDDL+1,word,30,31); //see AliMUONDDLTrigger.h for details
460 AliBitPacking::PackWord((UInt_t)version,word,22,29);
461 AliBitPacking::PackWord((UInt_t)serialNb,word,18,21);
462 AliBitPacking::PackWord((UInt_t)eventType,word,14,17);
464 fDDLTrigger->SetDDLWord(word);
465 fDDLTrigger->SetGlobalOutput(gloTrigPat);
466 memcpy(&buffer[index],fDDLTrigger->GetEnhancedHeader(),24);
469 for (Int_t iReg = 0; iReg < 8; iReg++) {
471 subEvent = new AliMUONSubEventTrigger();
473 // Regional card header
475 AliBitPacking::PackWord((UInt_t)serialNb,word,27,31); //see AliMUONSubEventTrigger.h for details
476 AliBitPacking::PackWord((UInt_t)iReg,word,22,26);
477 AliBitPacking::PackWord((UInt_t)version,word,14,21);
478 subEvent->SetRegWord(word);
479 memcpy(&buffer[index++],subEvent->GetAddress(),4);
481 for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
483 iLocCard = iLoc + iReg*16 + iDDL*128;
485 if (isFired[iLocCard]) {
486 locTrg = (AliMUONLocalTrigger*)localTrigger->At(iEntries);
487 locCard = locTrg->LoCircuit();
488 locDec = locTrg->GetLoDecision();
490 posY = locTrg->LoStripY();
491 posX = locTrg->LoStripX();
492 devX = locTrg->LoDev();
493 if (fPrintLevel == 4)
494 printf("loctrg %d, posX %d, posY %d, devX %d\n",
495 locTrg-> LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev());
496 } else { //no trigger (see PRR chpt 3.4)
507 AliBitPacking::PackWord((UInt_t)(iLocCard % 16),word,19,22); //card id number in crate
508 AliBitPacking::PackWord((UInt_t)locDec,word,15,18);
509 AliBitPacking::PackWord((UInt_t)trigY,word,14,14);
510 AliBitPacking::PackWord((UInt_t)posY,word,10,13);
511 AliBitPacking::PackWord((UInt_t)devX,word,5,9);
512 AliBitPacking::PackWord((UInt_t)posX,word,0,4);
514 if (locCard == iLocCard) {
515 // add local cards structure
516 buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
517 buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
518 buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
519 buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
520 buffer[index++] = (Int_t)word; // data word
521 if (iEntries < nEntries-1)
524 buffer[index++] = 0; // 4 words for x1, x2, y1, y2
528 buffer[index++] = (Int_t)word; // data word
532 buffer[index++] = 0;// 2 words of regional input
534 buffer[index++] = 0;// regional output
540 buffer[index++] = fDDLTrigger->GetEoD(); // End of DDL word
541 buffer[index++] = fDDLTrigger->GetEoD(); // End of DDL word for 64 bits transfer purpose
546 header.fSize = index + headerSize;// total length in word
547 fwrite((char*)(&header),headerSize*4,1,fFile1);
548 fwrite(buffer,sizeof(int),index,fFile1);
552 header.fSize = index + headerSize;// total length in word
553 fwrite((char*)(&header),headerSize*4,1,fFile2);
554 fwrite(buffer,sizeof(int),index,fFile2);
561 //____________________________________________________________________
562 void AliMUONRawData::GetDummyMapping(Int_t iCh, Int_t iCath, const AliMUONDigit* digit,
563 Int_t &busPatchId, UShort_t &manuId, UChar_t &channelId, UShort_t &charge)
566 Int_t offsetX = 0; // offet row
567 Int_t offsetY = 0; // offset columns
568 Int_t offsetCath = 0; //offset from one cathod to the other
569 Int_t maxChannel = 0; // maximum nb of channel in 1/2 chamber
579 maxChannel = (offsetY * offsetX + 2* offsetY + offsetCath);
590 maxChannel = (256 * offsetX + offsetX + offsetCath);
594 // manu Id directly from a matrix 8*8, same segmentation for B and NB
595 // 50 buspatches for 1/2 chamber
597 id = (TMath::Abs(digit->PadX()) * offsetX + digit->PadY() + offsetY +
599 Int_t chPerBus = maxChannel/50;
600 busPatchId = id/chPerBus; // start at zero
601 if (digit->PadX() > 0)
602 busPatchId += 50*iCh*2;
604 busPatchId += 50*(2*iCh+1);
605 // 64 manu cards for one buspatch
606 manuId = (id % chPerBus)/64; //start at zero
607 manuId &= 0x7FF; // 11 bits
610 channelId = (id % chPerBus) % 64; //start at zero
611 channelId &= 0x3F; // 6 bits
613 // id = (TMath::Abs(digit->PadX()) * offsetX + digit->PadY() + offsetY +
614 // offsetCath * iCath);
615 // busPatchId = id/50;
617 // Int_t inBusId = id - (maxChannel/50 * busPatchId);// id channel in buspatch
618 // Int_t manuPerBus = (maxChannel/(50*64)); // number of manus per buspatch
620 // // 64 manu cards for one buspatch
621 // manuId = inBusId/manuPerBus;
622 // manuId &= 0x7FF; // 11 bits
625 // channelId = (inBusId % manuPerBus);
626 // channelId &= 0x3F; // 6 bits
628 if (fPrintLevel == 2)
629 printf("id: %d, busPatchId %d, manuId: %d, channelId: %d, maxchannel: %d, chPerBus %d\n",
630 id, busPatchId, manuId, channelId, maxChannel, chPerBus);
632 charge = digit->Signal();
635 if (fPrintLevel == 2)
636 printf("id: %d, busPatchId %d, manuId: %d, channelId: %d, padx: %d pady %d, charge %d\n",
637 id, busPatchId, manuId, channelId, digit->PadX(), digit->PadY(), digit->Signal());
641 //____________________________________________________________________
642 Int_t AliMUONRawData::GetGlobalTriggerPattern(AliMUONGlobalTrigger* gloTrg)
644 Int_t gloTrigPat = 0;
646 if (gloTrg->SinglePlusLpt()) gloTrigPat|= 0x1;
647 if (gloTrg->SinglePlusHpt()) gloTrigPat|= 0x2;
648 if (gloTrg->SinglePlusApt()) gloTrigPat|= 0x4;
650 if (gloTrg->SingleMinusLpt()) gloTrigPat|= 0x8;
651 if (gloTrg->SingleMinusHpt()) gloTrigPat|= 0x10;
652 if (gloTrg->SingleMinusApt()) gloTrigPat|= 0x20;
654 if (gloTrg->SingleUndefLpt()) gloTrigPat|= 0x40;
655 if (gloTrg->SingleUndefHpt()) gloTrigPat|= 0x80;
656 if (gloTrg->SingleUndefApt()) gloTrigPat|= 0x100;
658 if (gloTrg->PairUnlikeLpt()) gloTrigPat|= 0x200;
659 if (gloTrg->PairUnlikeHpt()) gloTrigPat|= 0x400;
660 if (gloTrg->PairUnlikeApt()) gloTrigPat|= 0x800;
662 if (gloTrg->PairLikeLpt()) gloTrigPat|= 0x1000;
663 if (gloTrg->PairLikeHpt()) gloTrigPat|= 0x2000;
664 if (gloTrg->PairLikeApt()) gloTrigPat|= 0x4000;