]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONRawWriter.cxx
And finally the last effc++ changes ...
[u/mrichter/AliRoot.git] / MUON / AliMUONRawWriter.cxx
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 /* $Id$ */
17
18 ////////////////////////////////////
19 //
20 // MUON Raw Data generaton in ALICE-MUON
21 // This class version 3 (further details could be found in Alice-note)
22 //
23 // Implemented non-constant buspatch numbers for tracking
24 // with correct DDL id (first guess)
25 // (Ch. Finck, dec 2005)
26 //
27 // Digits2Raw:
28 // Generates raw data for MUON tracker and finally for trigger
29 // Using real mapping (inverse) for tracker
30 // For trigger there is no mapping (mapping could be found in AliMUONTriggerCircuit)
31 // Ch. Finck, July 04
32 // Use memcpy instead of assignment elt by elt
33 // Introducing variable DSP numbers, real manu numbers per buspatch for st12
34 // Implemented scaler event for Trigger
35 // Ch. Finck, Jan. 06
36 // Using bus itr in DDL instead of simple incrementation
37 // treat correctly the DDL & buspatch for station 3.
38 // Using informations from AliMUONTriggerCrateStore for 
39 // empty slots and non-notified cards in trigger crates.
40 // Ch. Finck, August 06.
41 // 
42 ////////////////////////////////////
43
44 #include "AliMUONRawWriter.h"
45
46 #include "AliBitPacking.h" 
47 #include "AliRawReader.h"
48 #include "AliDAQ.h"
49 #include "AliLog.h"
50 #include "AliMUON.h"
51 #include "AliMUONConstants.h"
52
53 #include "AliMUONDarcHeader.h"
54 #include "AliMUONRegHeader.h"
55 #include "AliMUONLocalStruct.h"
56 #include "AliMUONDspHeader.h"
57 #include "AliMUONBlockHeader.h"
58
59 #include "AliMUONData.h"
60 #include "AliMUONDigit.h"
61 #include "AliMUONGlobalTrigger.h"
62 #include "AliMUONLocalTrigger.h"
63
64 #include "AliMpBusPatch.h"
65 #include "AliMUONTriggerCrateStore.h"
66 #include "AliMUONTriggerCrate.h"
67 #include "AliMUONLocalTriggerBoard.h"
68
69 #include "AliMpDEManager.h"
70 #include "AliMpPad.h"
71 #include "AliMpPlaneType.h"
72 #include "AliMpSegFactory.h"
73 #include "AliMpStationType.h"
74 #include "AliMpVSegmentation.h"
75
76 #include "TClonesArray.h"
77 #include "TObjArray.h"
78
79 ClassImp(AliMUONRawWriter) // Class implementation in ROOT context
80
81 Int_t AliMUONRawWriter::fgManuPerBusSwp1B[12]  = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 224, 232};
82 Int_t AliMUONRawWriter::fgManuPerBusSwp1NB[12] = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 225, 233};
83
84 Int_t AliMUONRawWriter::fgManuPerBusSwp2B[12]  = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 226, 246};
85 Int_t AliMUONRawWriter::fgManuPerBusSwp2NB[12] = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 227, 245};
86
87
88 //__________________________________________________________________________
89 AliMUONRawWriter::AliMUONRawWriter(AliMUONData* data)
90 : TObject(),
91   fScalerEvent(kFALSE)
92 {
93   //
94   // Standard Constructor
95   //
96   AliDebug(1,"Standard ctor");
97       
98   // initialize container
99   fMUONData  = data;
100
101   // initialize array
102   fBusArray = new TClonesArray("AliMUONBusStruct",1000);
103   fBusArray->SetOwner(kTRUE);
104
105   // ddl tracker pointers
106   fBlockHeader     = new AliMUONBlockHeader();
107   fDspHeader       = new AliMUONDspHeader();
108   fBusStruct       = new AliMUONBusStruct();
109
110   // setting data key to default value (only for writting)
111   fBlockHeader->SetDataKey(fBlockHeader->GetDefaultDataKey());
112   fDspHeader->SetDataKey(fDspHeader->GetDefaultDataKey());
113   fBusStruct->SetDataKey(fBusStruct->GetDefaultDataKey());
114
115   // ddl trigger pointers
116   fDarcHeader      = new AliMUONDarcHeader();
117   fRegHeader       = new AliMUONRegHeader();
118   fLocalStruct     = new AliMUONLocalStruct();
119
120   // bus patch managers
121   fBusPatchManager = new AliMpBusPatch();
122   fBusPatchManager->ReadBusPatchFile();
123
124   // Crate manager
125   fCrateManager = new AliMUONTriggerCrateStore();
126   fCrateManager->ReadFromFile();
127
128   //Seg factory
129   fSegFactory = new AliMpSegFactory();
130
131   // timers
132   fTrackerTimer.Start(kTRUE); fTrackerTimer.Stop();
133   fTriggerTimer.Start(kTRUE); fTriggerTimer.Stop();
134   fMappingTimer.Start(kTRUE); fMappingTimer.Stop();
135   
136 }
137
138 //__________________________________________________________________________
139 AliMUONRawWriter::AliMUONRawWriter()
140   : TObject(),
141     fMUONData(0),
142     fBlockHeader(0),
143     fDspHeader(0),
144     fBusStruct(0),
145     fDarcHeader(0),
146     fRegHeader(0),
147     fLocalStruct(0),
148     fBusPatchManager(0),
149     fCrateManager(0x0),
150     fScalerEvent(kFALSE),
151     fSegFactory(0x0)
152 {
153   //
154   // Default Constructor
155   //
156   AliDebug(1,"Default ctor");   
157   fFile[0] = fFile[1] = 0x0;  
158   fFile[2] = fFile[3] = 0x0;  
159
160   fTrackerTimer.Start(kTRUE); fTrackerTimer.Stop();
161   fTriggerTimer.Start(kTRUE); fTriggerTimer.Stop();
162   fMappingTimer.Start(kTRUE); fMappingTimer.Stop();
163 }
164
165 //_______________________________________________________________________
166 AliMUONRawWriter::AliMUONRawWriter (const AliMUONRawWriter& rhs)
167   : TObject(rhs)
168 {
169   //
170   // Protected copy constructor
171   //
172   AliFatal("Not implemented.");
173 }
174
175 //_______________________________________________________________________
176 AliMUONRawWriter & 
177 AliMUONRawWriter::operator=(const AliMUONRawWriter& rhs)
178 {
179   //
180   // Protected assignement operator
181   //
182   if (this == &rhs) return *this;
183
184   AliFatal("Not implemented.");
185     
186   return *this;  
187 }
188
189 //__________________________________________________________________________
190 AliMUONRawWriter::~AliMUONRawWriter(void)
191 {
192   //
193   // Destructor
194   //
195   AliDebug(1,"dtor");
196   
197   delete fBusArray;
198   
199   delete fBlockHeader;
200   delete fDspHeader;
201   delete fBusStruct;
202   delete fDarcHeader;
203   delete fRegHeader;
204   delete fLocalStruct;
205
206   delete fBusPatchManager;
207   delete fCrateManager;
208
209   delete fSegFactory;
210   
211   AliInfo(Form("Execution time for MUON tracker : R:%.2fs C:%.2fs",
212                fTrackerTimer.RealTime(),fTrackerTimer.CpuTime()));
213   AliInfo(Form("   Execution time for MUON tracker (mapping calls part) "
214                ": R:%.2fs C:%.2fs",
215                fMappingTimer.RealTime(),fMappingTimer.CpuTime()));
216   AliInfo(Form("Execution time for MUON trigger : R:%.2fs C:%.2fs",
217                fTriggerTimer.RealTime(),fTriggerTimer.CpuTime()));
218 }
219
220 //____________________________________________________________________
221 Int_t AliMUONRawWriter::Digits2Raw()
222 {
223   //
224   // convert digits of the current event to raw data
225   //
226   Int_t idDDL;
227   Char_t name[255];
228
229   fMUONData->GetLoader()->LoadDigits("READ");
230
231   fMUONData->SetTreeAddress("D,GLT");
232
233   fMUONData->ResetDigits();
234   fMUONData->ResetTrigger();
235   
236   // This will get both tracker and trigger digits.
237   fMUONData->GetDigits();
238   
239   // tracking chambers
240
241   for (Int_t iSt = 0; iSt < AliMUONConstants::NTrackingCh()/2; iSt++) {
242
243     // open files for one station
244     // cos station 3, 1/4 of DE's from 2 chambers has same DDL number 
245     idDDL = iSt * 4;
246     strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
247     fFile[0] = fopen(name,"w");
248
249     idDDL = (iSt * 4) + 1;
250     strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
251     fFile[1] = fopen(name,"w");
252
253     idDDL =  (iSt * 4) + 2;;
254     strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
255     fFile[2] = fopen(name,"w");
256
257     idDDL =  (iSt * 4) + 3;
258     strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
259     fFile[3] = fopen(name,"w");
260
261     WriteTrackerDDL(iSt);
262   
263     // reset and close when station has been processed
264     fclose(fFile[0]);
265     fclose(fFile[1]);
266     fclose(fFile[2]);
267     fclose(fFile[3]);
268      
269   }
270  
271   // trigger chambers
272  
273   // open files
274   idDDL = 0;// MUTR
275   strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
276   fFile[0] = fopen(name,"w");
277
278   idDDL = 1;// MUTR
279   strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
280   fFile[1] = fopen(name,"w");
281
282   WriteTriggerDDL();
283   
284   // reset and close
285   fclose(fFile[0]);
286   fclose(fFile[1]);
287
288   fMUONData->ResetDigits();
289   fMUONData->ResetTrigger();  
290   fMUONData->GetLoader()->UnloadDigits();
291
292   return kTRUE;
293 }
294
295 //____________________________________________________________________
296 Int_t AliMUONRawWriter::WriteTrackerDDL(Int_t iSt)
297 {
298   // writing DDL for tracker
299   // used inverse mapping
300   //
301   fTrackerTimer.Start(kFALSE);
302   
303
304   static const Int_t kMAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
305
306   // resets
307   TClonesArray* muonDigits = 0;
308
309   fBusArray->Delete();
310
311
312   //
313   TArrayI nbInBus;
314
315   nbInBus.Set(5000);
316
317   nbInBus.Reset();
318
319   // DDL header
320   Int_t headerSize = sizeof(fHeader)/4;
321
322   // DDL event one per half chamber
323
324   // raw data
325   Char_t parity = 0x4;
326   UShort_t manuId = 0;
327   UChar_t channelId = 0;
328   UShort_t charge = 0;
329   Int_t busPatchId = 0;
330   UInt_t word;
331
332
333   // Dsp length
334   Int_t totalDspLength;
335   Int_t dspLength;
336
337   // block length
338   Int_t totalBlkLength;
339   Int_t blkLength; 
340   
341   // total DDL length
342   Int_t totalDDLLength;
343
344   // indexes
345   Int_t index;
346   Int_t indexDsp;
347   Int_t indexBlk;
348
349   // digits
350   Int_t nEntries = 0;
351   Int_t* buffer = 0;
352   Int_t padX;
353   Int_t padY;
354   Int_t cathode = 0;
355   Int_t detElemId;
356   Int_t nDigits;
357
358   const AliMUONDigit* digit;
359
360   for (Int_t iCh = iSt*2; iCh <= iSt*2 + 1; iCh++) {
361
362     muonDigits = fMUONData->Digits(iCh);
363
364     nDigits = muonDigits->GetEntriesFast();
365     AliDebug(3,Form("ndigits = %d\n",nDigits));
366  
367     // loop over digit
368     for (Int_t idig = 0; idig < nDigits; idig++) {
369
370       digit = (AliMUONDigit*) muonDigits->UncheckedAt(idig);
371
372       padX = digit->PadX();
373       padY = digit->PadY();
374       charge = digit->ADC();
375       if ( charge > kMAXADC )
376         {
377           // This is most probably an error in the digitizer (which should insure
378           // the adc is below kMAXADC), so make it a (non-fatal) error indeed.
379           AliError(Form("adc value %d above %x. Setting to %x",
380                         charge,kMAXADC,kMAXADC));
381           charge = kMAXADC;
382         }
383       cathode = digit->Cathode();
384       detElemId = digit->DetElemId();
385
386       // inverse mapping
387       busPatchId = GetBusPatch(*digit);
388       if (busPatchId<0) continue;
389
390       if ( digit->ManuId() > 0x7FF || digit->ManuId() < 0 ||
391            digit->ManuChannel() > 0x3F || digit->ManuChannel() < 0 )
392         {
393           StdoutToAliError(digit->Print(););
394           AliFatal("ManuId,ManuChannel are invalid for the digit above.");
395         }
396     
397       manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
398       channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
399
400       AliDebug(3,Form("input  IdDE %d busPatchId %d PadX %d PadY %d iCath %d \n", 
401                       detElemId, busPatchId, padX, padY, cathode));
402
403       AliDebug(3,Form("busPatchId %d, manuId %d channelId %d\n", busPatchId, manuId, 
404                       channelId ));
405
406       //packing word
407       word = 0;
408       AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
409       AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
410       AliBitPacking::PackWord((UInt_t)charge,word,0,11);
411
412       // parity word
413       parity = word & 0x1;
414       for (Int_t i = 1; i <= 30; i++) 
415         parity ^=  ((word >> i) & 0x1);
416       AliBitPacking::PackWord((UInt_t)parity,word,31,31);
417
418       // set sub Event
419       fBusStruct->SetLength(0);
420       fBusStruct->AddData(word);
421       fBusStruct->SetBusPatchId(busPatchId);
422        
423       // storing the number of identical buspatches
424       nbInBus[busPatchId]++;
425       AddData(*fBusStruct);
426    
427     }
428   } // loop over chamber in station
429
430   // sorting by buspatch
431   fBusArray->Sort();
432
433   // gather datas from same bus patch
434   nEntries = fBusArray->GetEntriesFast();
435
436   for (Int_t i = 0; i < nEntries; i++) {
437     AliMUONBusStruct* temp = (AliMUONBusStruct*)fBusArray->At(i);
438     busPatchId = temp->GetBusPatchId();
439
440     // add bus patch header, length and total length managed by subevent class
441     for (Int_t j = 0; j < nbInBus[busPatchId]-1; j++) {
442       AliMUONBusStruct* temp1 =  (AliMUONBusStruct*)fBusArray->At(++i);
443       temp->AddData(temp1->GetData(0));
444       fBusArray->RemoveAt(i) ;
445     }
446   }
447   fBusArray->Compress();
448
449   if (AliLog::GetGlobalDebugLevel() == 3) {
450     nEntries = fBusArray->GetEntriesFast();
451     for (Int_t i = 0; i < nEntries; i++) {
452       AliMUONBusStruct* temp =  (AliMUONBusStruct*)fBusArray->At(i);
453       printf("busPatchid back %d\n",temp->GetBusPatchId());
454       for (Int_t j = 0; j < temp->GetLength(); j++) {
455         printf("manuId back %d, ",temp->GetManuId(j));
456         printf("channelId back %d, ",temp->GetChannelId(j));
457         printf("charge back %d\n",temp->GetCharge(j));
458       }
459     }
460     printf("\n");
461   }
462   // end of TreeD reading and storing in TClonesArray
463   
464   // getting info for the number of buspatches
465   Int_t iBusPatch;
466   Int_t length;
467   Int_t iBusPerDSP[5];//number of bus patches per DSP
468   Int_t iDspMax; //number max of DSP per block
469   Int_t iFile = 0;
470
471   Int_t rEntry = -1;
472   AliMUONBusStruct* busStructPtr = 0x0;
473
474   // open DDL files, 4 per station
475   for (Int_t iDDL = iSt*4; iDDL < 4 + iSt*4; iDDL++) {
476
477     fBusPatchManager->ResetBusItr(iDDL);
478     fBusPatchManager->GetDspInfo(iDDL, iDspMax, iBusPerDSP);
479
480     totalDDLLength = 0;
481
482     // buffer size
483     // ((43 manus max*64 ch + 4 bus word) * 5 DSPs + 10 DSP word) * 2 blocks + 8 block words = 27588
484     static const Int_t kBufferSize = ((43*64 + 4)*5 + 10)*2 + 8;
485
486     // buffer allocation
487     buffer = new Int_t [kBufferSize];
488
489     indexBlk = 0;
490     indexDsp = 0;
491     index = 0;
492
493     // two blocks A and B per DDL
494     for (Int_t iBlock = 0; iBlock < 2; iBlock++) {
495
496       // block header
497       length = fBlockHeader->GetHeaderLength();
498       memcpy(&buffer[index],fBlockHeader->GetHeader(),length*4);
499       indexBlk = index;
500       index += length; 
501
502       // 5 DSP's max per block
503       for (Int_t iDsp = 0; iDsp < iDspMax; iDsp++) {
504
505         // DSP header
506         length = fDspHeader->GetHeaderLength();
507         memcpy(&buffer[index],fDspHeader->GetHeader(),length*4);
508         indexDsp = index;
509         index += length; 
510
511         // 5 buspatches max per DSP
512         for (Int_t i = 0; i < iBusPerDSP[iDsp]; i++) {
513
514           // iteration over bus patch in DDL
515           if ((iBusPatch = fBusPatchManager->NextBusInDDL(iDDL)) == -1) {
516             AliWarning(Form("Error in bus itr in DDL %d\n", iDDL));
517             continue;
518           }
519
520           // 4 DDL's per station, condition needed for station 3
521           iFile = iDDL - iSt*4; // works only if DDL begins at zero (as it should be) !!!
522
523           AliDebug(3,Form("iSt %d iDDL %d iBlock %d iDsp %d busPatchId %d", iSt, iDDL, iBlock, 
524                           iDsp, iBusPatch));
525
526           nEntries = fBusArray->GetEntriesFast();
527           busPatchId = -1;
528
529           // checking buspatch structure not empty
530           for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) { // method "bourrique"...
531             busStructPtr = (AliMUONBusStruct*)fBusArray->At(iEntry);
532             busPatchId = busStructPtr->GetBusPatchId();
533             if (busPatchId == iBusPatch) {
534               rEntry = iEntry;
535               break;
536             }
537             busPatchId = -1;
538             AliDebug(3,Form("busPatchId %d", busStructPtr->GetBusPatchId()));
539           } 
540          
541           // check if buspatchid has digit
542           if (busPatchId != -1) {
543             // add bus patch structure header
544             length = busStructPtr->GetHeaderLength();
545             memcpy(&buffer[index],busStructPtr->GetHeader(),length*4);
546             index += length;
547
548             // add bus patch data
549             length = busStructPtr->GetLength();
550             memcpy(&buffer[index],busStructPtr->GetData(),length*4);
551             index += length;
552
553             if (AliLog::GetGlobalDebugLevel() == 3) {
554               for (Int_t j = 0; j < busStructPtr->GetLength(); j++) {
555                 printf("busPatchId %d, manuId %d channelId %d\n", busStructPtr->GetBusPatchId(), 
556                        busStructPtr->GetManuId(j), busStructPtr->GetChannelId(j));
557               }
558             }
559             
560             fBusArray->RemoveAt(rEntry);
561             fBusArray->Compress();
562           } else {
563             // writting anyhow buspatch structure (empty ones)
564             buffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
565             buffer[index++] = busStructPtr->GetHeaderLength(); // header length
566             buffer[index++] = 0; // raw data length
567             buffer[index++] = iBusPatch; // bus patch
568           }
569         } // bus patch
570
571         // check if totalLength even
572         // set padding word in case
573         // Add one word 0xBEEFFACE at the end of DSP structure
574         totalDspLength  = index - indexDsp;
575         if ((totalDspLength % 2) == 1) { 
576           buffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
577           buffer[index++] = fDspHeader->GetDefaultPaddingWord();
578           totalDspLength++;
579         }
580
581         dspLength          = totalDspLength - fDspHeader->GetHeaderLength();
582
583         buffer[indexDsp+1] = totalDspLength; // dsp total length
584         buffer[indexDsp+2] = dspLength; // data length  
585            
586       } // dsp
587
588       totalBlkLength  = index - indexBlk;
589       blkLength       = totalBlkLength - fBlockHeader->GetHeaderLength();
590       totalDDLLength += totalBlkLength;
591
592       buffer[indexBlk+1] = totalBlkLength; // total block length
593       buffer[indexBlk+2] = blkLength;
594
595     } // block
596     
597     //writting onto disk
598     // write DDL 1 - 4
599     fHeader.fSize = (totalDDLLength + headerSize) * 4;// total length in bytes
600     fwrite((char*)(&fHeader),headerSize*4,1,fFile[iFile]);
601     fwrite(buffer,sizeof(int),index,fFile[iFile]);
602    
603     delete[] buffer;
604   }
605
606   fTrackerTimer.Stop();
607   return kTRUE;
608 }
609
610 //____________________________________________________________________
611 Int_t AliMUONRawWriter::GetBusPatch(const AliMUONDigit& digit)
612 {
613   //
614   // Determine the BusPatch this digit belongs to.
615   //
616   fMappingTimer.Start(kFALSE);
617   
618   Int_t* ptr = 0;
619
620   // information from digits
621   Int_t detElemId  = digit.DetElemId();
622
623   AliMpVSegmentation* seg = 
624     fSegFactory->CreateMpSegmentationByElectronics(detElemId, digit.ManuId());
625   
626   AliMpPlaneType plane = seg->PlaneType();
627
628   AliMpStationType stationType = AliMpDEManager::GetStationType(detElemId);
629
630   if ( stationType == kStation1 || stationType == kStation2 )
631   {
632     if (plane == kBendingPlane) 
633     {
634       ptr = &fgManuPerBusSwp1B[0];
635     }
636     else 
637     {
638       ptr = &fgManuPerBusSwp1NB[0];
639     }
640   }
641   else
642   {
643     if (plane == kBendingPlane)
644     {
645       ptr = &fgManuPerBusSwp2B[0];
646     }
647     else
648     {
649       ptr = &fgManuPerBusSwp2NB[0];
650     }
651   }
652
653   // Getting buspatch id
654   TArrayI* vec = fBusPatchManager->GetBusfromDE(detElemId);
655   Int_t pos = 0;
656
657   Int_t m = ( digit.ManuId() & 0x3FF ); // remove bit 10
658                                 //FIXME : how can we remove that condition
659   // on the 10-th bit ? All the rest need not any knowledge about it,
660   // can't we find a way to get manu<->buspatch transparent to this too ?
661   
662   if ( stationType == kStation1 || stationType == kStation2 )
663   {
664     for (Int_t i = 0; i < 12; i++)
665     {
666       if (m >= *(ptr + pos++)) break;
667     }
668   }
669   else 
670   {
671     // offset of 100 in manuId for following bus patch
672     pos = m/100;
673   }
674
675   if (pos >(Int_t) vec->GetSize())
676   {
677     AliError(Form("pos greater %d than size %d manuId %d detElemId %d \n", 
678                     pos, (Int_t)vec->GetSize(), digit.ManuId(), detElemId));
679     AliError(Form("Chamber %s Plane %s manuId %d m %d",
680                     StationTypeName(stationType).Data(),
681                     PlaneTypeName(plane).Data(),
682                     digit.ManuId(),
683                     m));
684     return -1;
685   }
686   
687   Int_t busPatchId = vec->At(pos);
688
689   fMappingTimer.Stop();
690   
691   return busPatchId;
692 }
693
694 //____________________________________________________________________
695 Int_t AliMUONRawWriter::WriteTriggerDDL()
696 {
697   //
698   // Write trigger DDL
699   //
700   fTriggerTimer.Start(kFALSE);
701   
702  // DDL event one per half chamber
703
704   // stored local id number 
705   TArrayI isFired(256);
706   isFired.Reset();
707
708
709  // DDL header size
710   Int_t headerSize = sizeof(AliRawDataHeader)/4;
711
712   TClonesArray* localTrigger;
713   TClonesArray* globalTrigger;
714   AliMUONGlobalTrigger* gloTrg;
715   AliMUONLocalTrigger* locTrg = 0x0;
716
717   // global trigger for trigger pattern
718   globalTrigger = fMUONData->GlobalTrigger(); 
719   gloTrg = (AliMUONGlobalTrigger*)globalTrigger->UncheckedAt(0);
720   Int_t gloTrigPat = gloTrg->GetGlobalPattern();
721
722   // local trigger 
723   localTrigger = fMUONData->LocalTrigger();    
724
725   UInt_t word;
726   Int_t* buffer = 0;
727   Int_t index;
728   Int_t iEntries = 0;
729   Int_t iLocCard, locCard;
730   Char_t locDec, trigY, posY, posX,regOut;
731   Int_t devX;
732   Int_t version = 1; // software version
733   Int_t eventPhys = 1; // trigger type: 1 for physics, 0 for software
734   Int_t serialNb = 0xF; // serial nb of card: all bits on for the moment
735   Int_t globalFlag = 0; // set to 1 if global info present in DDL else set to 0
736
737   // size of headers
738   static const Int_t kDarcHeaderLength   = fDarcHeader->GetDarcHeaderLength();
739   static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
740   static const Int_t kDarcScalerLength   = fDarcHeader->GetDarcScalerLength();
741   static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
742   static const Int_t kRegHeaderLength    = fRegHeader->GetHeaderLength();
743   static const Int_t kRegScalerLength    = fRegHeader->GetScalerLength();
744   static const Int_t kLocHeaderLength    = fLocalStruct->GetLength();
745   static const Int_t kLocScalerLength    = fLocalStruct->GetScalerLength();
746
747   // [16(local)*6 words + 6 words]*8(reg) + 8 words = 824 
748   static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) +  (kRegHeaderLength+1))* 8 
749       +  kDarcHeaderLength + kGlobalHeaderLength + 2;
750
751   // [16(local)*51 words + 16 words]*8(reg) + 8 + 10 + 8 words scaler event 6682 words
752   static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength +  kLocScalerLength +1) +  
753                                          (kRegHeaderLength + kRegScalerLength +1))* 8 +
754                                          (kDarcHeaderLength + kDarcScalerLength + 
755                                           kGlobalHeaderLength + kGlobalScalerLength + 2);
756   if(fScalerEvent)
757     eventPhys = 0; //set to generate scaler events
758
759   Int_t nEntries = (Int_t) (localTrigger->GetEntries());// 234 local cards
760   // stored the local card id that's fired
761   for (Int_t i = 0; i <  nEntries; i++) {
762     locTrg = (AliMUONLocalTrigger*)localTrigger->At(i);
763     isFired[locTrg->LoCircuit()] = 1; // storing local boards with informations
764   }
765
766   if (!nEntries)
767     AliInfo("No Trigger information available");
768
769   if(fScalerEvent)
770     buffer = new Int_t [kScalerBufferSize];
771   else
772     buffer = new Int_t [kBufferSize];
773
774   // reset crate
775
776   // open DDL file, on per 1/2 chamber
777   for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
778
779     index = 0; 
780
781     if (iDDL == 0) // suppose global info in DDL one
782       globalFlag = 1;
783     else 
784       globalFlag = 0;
785
786     word = 0;
787     // set darc status word
788     // see AliMUONDarcHeader.h for details
789     AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
790     AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
791     AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
792     AliBitPacking::PackWord((UInt_t)version,word,12,19);
793     fDarcHeader->SetWord(word);
794
795     memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4); 
796     index += fDarcHeader->GetDarcHeaderLength();
797
798     if (iDDL == 0)
799      fDarcHeader->SetGlobalOutput(gloTrigPat);// no global input for the moment....
800     else 
801      fDarcHeader->SetGlobalOutput(0);
802
803     if (fScalerEvent) {
804       // 6 DARC scaler words
805       memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
806       index += fDarcHeader->GetDarcScalerLength();
807     }
808     // end of darc word
809     buffer[index++] = fDarcHeader->GetEndOfDarc();
810
811     // 4 words of global board input + Global board output
812     memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4); 
813     index += fDarcHeader->GetGlobalHeaderLength(); 
814
815     if (fScalerEvent) {
816       // 10 Global scaler words
817       memcpy(fDarcHeader->GetGlobalScalers(), &buffer[index], kGlobalScalerLength*4);
818       index += fDarcHeader->GetGlobalScalerLength();
819     }
820
821     // end of global word
822     buffer[index++] = fDarcHeader->GetEndOfGlobal();
823
824     // 8 regional cards per DDL
825     for (Int_t iReg = 0; iReg < 8; iReg++) {
826
827       // crate info
828       Char_t crateName[10];
829       GetCrateName(crateName, iDDL, iReg);
830       AliMUONTriggerCrate* crate = fCrateManager->Crate(crateName);
831
832       if (!crate) 
833         AliWarning(Form("Missing crate number %d in DDL %d\n", iReg, iDDL));
834
835       // Regional card header
836       word = 0;
837
838       // set darc status word
839       fRegHeader->SetDarcWord(word);
840
841       regOut  = 0;
842       // fill darc word, not darc status for the moment (empty)
843       //see  AliMUONRegHeader.h for details
844       AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31); 
845       AliBitPacking::PackWord((UInt_t)serialNb,word,19,24); 
846       AliBitPacking::PackWord((UInt_t)version,word,16,23);
847       AliBitPacking::PackWord((UInt_t)iReg,word,15,18);
848       AliBitPacking::PackWord((UInt_t)regOut,word,0,7); // whenever regional output will be implemented
849       fRegHeader->SetWord(word);
850
851       memcpy(&buffer[index],fRegHeader->GetHeader(),fRegHeader->GetHeaderLength()*4);
852       index += fRegHeader->GetHeaderLength();
853
854       // 11 regional scaler word
855       if (fScalerEvent) {
856         memcpy(&buffer[index], fRegHeader->GetScalers(), fRegHeader->GetScalerLength()*4);
857         index += fRegHeader->GetScalerLength();
858       }
859
860       // end of regional word
861       buffer[index++] = fRegHeader->GetEndOfReg();
862       
863       TObjArray *boards = crate->Boards();
864
865
866       // 16 local card per regional board
867       for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
868
869         // slot zero for Regional card
870         AliMUONLocalTriggerBoard* localBoard = (AliMUONLocalTriggerBoard*)boards->At(iLoc+1);
871
872         if (localBoard) { // if not empty slot
873
874           if ((iLocCard = localBoard->GetNumber()) != 0) {// if notified board
875
876             if (isFired[iLocCard]) { // if card has triggered
877               locTrg = (AliMUONLocalTrigger*)localTrigger->At(iEntries);
878               locCard = locTrg->LoCircuit();
879               locDec  = locTrg->GetLoDecision();
880               trigY = 0;
881               posY = locTrg->LoStripY();
882               posX = locTrg->LoStripX();
883               devX = locTrg->LoDev();
884               AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n", 
885                               locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
886             } else { //no trigger (see PRR chpt 3.4)
887               locCard = -1; // set to -1
888               locDec = 0;
889               trigY = 1;
890               posY = 15;
891               posX = 0;
892               devX = 0x8;
893             }
894
895             //packing word
896             word = 0;
897             AliBitPacking::PackWord((UInt_t)iLoc,word,19,22); //card id number in crate
898             AliBitPacking::PackWord((UInt_t)locDec,word,15,18);
899             AliBitPacking::PackWord((UInt_t)trigY,word,14,14);
900             AliBitPacking::PackWord((UInt_t)posY,word,10,13);
901             AliBitPacking::PackWord((UInt_t)devX,word,5,9);
902             AliBitPacking::PackWord((UInt_t)posX,word,0,4);
903
904             if (locCard == iLocCard) {
905               // add local cards structure
906               buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
907               buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
908               buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
909               buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
910               buffer[index++] = (Int_t)word; // data word
911               if (iEntries < nEntries-1)
912                 iEntries++;
913             } else {
914               buffer[index++] = 0; // 4 words for x1, x2, y1, y2
915               buffer[index++] = 0; 
916               buffer[index++] = 0; 
917               buffer[index++] = 0; 
918               buffer[index++] = (Int_t)word; // data word
919
920             }
921           } else {// number!=0
922           // fill with 10CDEAD word for 'non-notified' slots
923           for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
924             buffer[index++] = fLocalStruct->GetDisableWord(); 
925           }
926         } else { 
927           // fill with 10CDEAD word for empty slots
928           for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
929             buffer[index++] = fLocalStruct->GetDisableWord(); 
930         }// condition localBoard
931
932         // 45 regional scaler word
933         if (fScalerEvent) {
934           memcpy(&buffer[index], fLocalStruct->GetScalers(), fLocalStruct->GetScalerLength()*4);
935           index += fLocalStruct->GetScalerLength();
936         }
937
938         // end of local structure words
939         buffer[index++] = fLocalStruct->GetEndOfLocal();
940
941       } // local card 
942
943     } // Regional card
944     
945
946     // writting onto disk
947     // write DDL's
948     fHeader.fSize = (index + headerSize) * 4;// total length in bytes
949     fwrite((char*)(&fHeader),headerSize*4,1,fFile[iDDL]);
950     fwrite(buffer,sizeof(int),index,fFile[iDDL]);
951   
952   }
953   delete[] buffer;
954
955   fTriggerTimer.Stop();
956   
957   return kTRUE;
958 }
959 //____________________________________________________________________
960 void AliMUONRawWriter::SetScalersNumbers()
961 {
962   // set numbers for scaler events for trigger headers
963   // since this is provided by the experiment
964   // put dummy numbers to check the monitoring
965
966   fDarcHeader->SetScalersNumbers();
967   fRegHeader->SetScalersNumbers();
968   fLocalStruct->SetScalersNumbers();
969  
970   fScalerEvent = kTRUE;
971 }
972 //____________________________________________________________________
973 void  AliMUONRawWriter::GetCrateName(Char_t* name, Int_t iDDL, Int_t iReg)
974 {
975   // set crate name from DDL & reg number
976
977       switch(iReg) {
978       case 0:
979       case 1:
980         sprintf(name,"%d", iReg+1);
981         break;
982       case 2:
983         strcpy(name, "2-3");
984         break;
985       case 3:
986       case 4:
987       case 5:
988       case 6:
989       case 7:
990         sprintf(name,"%d", iReg);
991         break;
992       }
993
994       // crate Right for first DDL
995       if (iDDL == 0)
996         strcat(name, "R");
997       else 
998         strcat(name, "L"); 
999 }