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