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