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