]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONRawWriter.cxx
New detector numbering scheme (common for DAQ/HLT/Offline). All the subdetectors...
[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;
227     strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
228     fFile[0] = fopen(name,"w");
229
230     //    idDDL = (ich * 2) + 1 + AliDAQ::DdlIDOffset("MUONTRK");
231     idDDL = (ich * 2) + 1;
232     strcpy(name,AliDAQ::DdlFileName("MUONTRK",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 = 0;// MUTR
246   strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
247   fFile[0] = fopen(name,"w");
248
249   idDDL = 1;// MUTR
250   strcpy(name,AliDAQ::DdlFileName("MUONTRG",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     word = 0;
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     // parity word
383     parity = word & 0x1;
384     for (Int_t i = 1; i <= 30; i++) 
385       parity ^=  ((word >> i) & 0x1);
386     AliBitPacking::PackWord((UInt_t)parity,word,31,31);
387
388     // set sub Event
389     fBusStruct->SetLength(0);
390     fBusStruct->AddData(word);
391     fBusStruct->SetBusPatchId(busPatchId);
392        
393     // storing the number of identical buspatches
394     nbInBus[busPatchId]++;
395     AddData(*fBusStruct);
396    
397   }
398
399   // sorting by buspatch
400   fBusArray->Sort();
401
402   // gather datas from same bus patch
403   nEntries = fBusArray->GetEntriesFast();
404
405   for (Int_t i = 0; i < nEntries; i++) {
406     AliMUONBusStruct* temp = (AliMUONBusStruct*)fBusArray->At(i);
407     busPatchId = temp->GetBusPatchId();
408
409     // add bus patch header, length and total length managed by subevent class
410     for (Int_t j = 0; j < nbInBus[busPatchId]-1; j++) {
411       AliMUONBusStruct* temp1 =  (AliMUONBusStruct*)fBusArray->At(++i);
412       temp->AddData(temp1->GetData(0));
413       fBusArray->RemoveAt(i) ;
414     }
415   }
416   fBusArray->Compress();
417
418   if (AliLog::GetGlobalDebugLevel() == 3) {
419     nEntries = fBusArray->GetEntriesFast();
420     for (Int_t i = 0; i < nEntries; i++) {
421       AliMUONBusStruct* temp =  (AliMUONBusStruct*)fBusArray->At(i);
422       printf("busPatchid back %d\n",temp->GetBusPatchId());
423       for (Int_t j = 0; j < temp->GetLength(); j++) {
424         printf("manuId back %d, ",temp->GetManuId(j));
425         printf("channelId back %d, ",temp->GetChannelId(j));
426         printf("charge back %d\n",temp->GetCharge(j));
427       }
428     }
429     printf("\n");
430   }
431   
432   // getting info for the number of buspatches
433   Int_t iBusPatch;
434   Int_t length;
435   Int_t iBusPerDSP[5];//number of bus patches per DSP
436   Int_t iDspMax; //number max of DSP per block
437   Int_t iFile = 0;
438   fBusPatchManager->GetDspInfo(iCh, iDspMax, iBusPerDSP);
439
440   TArrayI* vec = fBusPatchManager->GetBusfromDE((iCh+1)*100);
441
442   Int_t iBus0AtCh = vec->At(0); //get first bus patch id for a given ich
443
444   AliDebug(3,Form("iBus0AtCh %d", iBus0AtCh));
445
446   iBusPatch = iBus0AtCh - 1; // starting point for each chamber
447
448   // nEntries = fBusArray->GetEntriesFast();
449
450   AliMUONBusStruct* busStructPtr = 0x0;
451
452   // open DDL file, on per 1/2 chamber
453   for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
454     
455     totalDDLLength = 0;
456
457     // filling buffer
458     buffer = new Int_t [(2048+24)*50]; // 24 words at most for one buspatch and 2048 manu info at most
459
460     indexBlk = 0;
461     indexDsp = 0;
462     index = 0;
463
464     // two blocks A and B per DDL
465     for (Int_t iBlock = 0; iBlock < 2; iBlock++) {
466
467       // block header
468       length = fBlockHeader->GetHeaderLength();
469       memcpy(&buffer[index],fBlockHeader->GetHeader(),length*4);
470       indexBlk = index;
471       index += length; 
472
473       // 5 DSP's max per block
474       for (Int_t iDsp = 0; iDsp < iDspMax; iDsp++) {
475
476         // DSP header
477         length = fDspHeader->GetHeaderLength();
478         memcpy(&buffer[index],fDspHeader->GetHeader(),length*4);
479         indexDsp = index;
480         index += length; 
481
482         // 5 buspatches max per DSP
483         for (Int_t i = 0; i < iBusPerDSP[iDsp]; i++) {
484
485           iBusPatch++;
486           if ((fBusPatchManager->GetDDLfromBus(iBusPatch) % 2) == 0) // comparing to DDL file
487             iFile = 1;
488           else
489             iFile = 0;
490
491           AliDebug(3,Form("iCh %d iDDL %d iBlock %d iDsp %d busPatchId %d", iCh, iDDL, iBlock, iDsp, iBusPatch));
492
493           nEntries = fBusArray->GetEntriesFast();
494           busPatchId = -1;
495
496           for (Int_t iEntries = 0; iEntries < nEntries; iEntries++) { // method "bourrique"...
497             busStructPtr = (AliMUONBusStruct*)fBusArray->At(iEntries);
498             busPatchId = busStructPtr->GetBusPatchId();
499             if (busPatchId == iBusPatch) break;
500             busPatchId = -1;
501             AliDebug(3,Form("busPatchId %d", busStructPtr->GetBusPatchId()));
502           } 
503          
504           // check if buspatchid has digit
505           if (busPatchId != -1) {
506             // add bus patch structure header
507             length = busStructPtr->GetHeaderLength();
508             memcpy(&buffer[index],busStructPtr->GetHeader(),length*4);
509             index += length;
510
511             // add bus patch data
512             for (Int_t j = 0; j < busStructPtr->GetLength(); j++) {
513               buffer[index++] =  busStructPtr->GetData(j);
514               AliDebug(3,Form("busPatchId %d, manuId %d channelId %d\n", 
515                               busStructPtr->GetBusPatchId(), 
516                               busStructPtr->GetManuId(j), busStructPtr->GetChannelId(j) ));
517             }
518             //        fBusArray->RemoveAt(iEntries);
519             //        fBusArray->Compress();
520           } else {
521             // writting anyhow buspatch structure (empty ones)
522             buffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
523             buffer[index++] = busStructPtr->GetHeaderLength(); // header length
524             buffer[index++] = 0; // raw data length
525             buffer[index++] = iBusPatch; // bus patch
526           }
527         } // bus patch
528
529         // check if totalLength even
530         // set padding word in case
531         // Add one word 0xBEEFFACE at the end of DSP structure
532         totalDspLength  = index - indexDsp;
533         if ((totalDspLength % 2) == 1) { 
534           buffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
535           buffer[index++] = fDspHeader->GetDefaultPaddingWord();
536           totalDspLength++;
537         }
538
539         dspLength          = totalDspLength - fDspHeader->GetHeaderLength();
540
541         buffer[indexDsp+1] = totalDspLength; // dsp total length
542         buffer[indexDsp+2] = dspLength; // data length  
543            
544       } // dsp
545
546       totalBlkLength  = index - indexBlk;
547       blkLength       = totalBlkLength - fBlockHeader->GetHeaderLength();
548       totalDDLLength += totalBlkLength;
549
550       buffer[indexBlk+1] = totalBlkLength; // total block length
551       buffer[indexBlk+2] = blkLength;
552
553     } // block
554     
555     //writting onto disk
556     // write DDL 1 & 2
557     fHeader.fSize = (totalDDLLength + headerSize) * 4;// total length in bytes
558     fwrite((char*)(&fHeader),headerSize*4,1,fFile[iFile]);
559     fwrite(buffer,sizeof(int),index,fFile[iFile]);
560    
561     delete[] buffer;
562   }
563
564   fTrackerTimer.Stop();
565   return kTRUE;
566 }
567
568 //____________________________________________________________________
569 Int_t AliMUONRawWriter::GetBusPatch(const AliMUONDigit& digit)
570 {
571   //
572   // Determine the BusPatch this digit belongs to.
573   //
574   fMappingTimer.Start(kFALSE);
575   
576   Int_t* ptr = 0;
577
578   // information from digits
579   Int_t detElemId  = digit.DetElemId();
580
581   AliMpVSegmentation* seg = 
582     fSegFactory->CreateMpSegmentationByElectronics(detElemId, digit.ManuId());
583   
584   AliMpPlaneType plane = seg->PlaneType();
585
586   AliMpStationType stationType = AliMpDEManager::GetStationType(detElemId);
587
588   if ( stationType == kStation1 || stationType == kStation2 )
589   {
590     if (plane == kBendingPlane) 
591     {
592       ptr = &fgManuPerBusSwp1B[0];
593     }
594     else 
595     {
596       ptr = &fgManuPerBusSwp1NB[0];
597     }
598   }
599   else
600   {
601     if (plane == kBendingPlane)
602     {
603       ptr = &fgManuPerBusSwp2B[0];
604     }
605     else
606     {
607       ptr = &fgManuPerBusSwp2NB[0];
608     }
609   }
610
611   // Getting buspatch id
612   TArrayI* vec = fBusPatchManager->GetBusfromDE(detElemId);
613   Int_t pos = 0;
614
615   Int_t m = ( digit.ManuId() & 0x3FF ); // remove bit 10
616                                 //FIXME : how can we remove that condition
617   // on the 10-th bit ? All the rest need not any knowledge about it,
618   // can't we find a way to get manu<->buspatch transparent to this too ?
619   
620   if ( stationType == kStation1 || stationType == kStation2 )
621   {
622     for (Int_t i = 0; i < 12; i++)
623     {
624       if (m >= *(ptr + pos++)) break;
625     }
626   }
627   else 
628   {
629     // offset of 100 in manuId for following bus patch
630     pos = m/100;
631   }
632
633   if (pos >(Int_t) vec->GetSize())
634   {
635     AliError(Form("pos greater %d than size %d manuId %d detElemId %d \n", 
636                     pos, (Int_t)vec->GetSize(), digit.ManuId(), detElemId));
637     AliError(Form("Chamber %s Plane %s manuId %d m %d",
638                     StationTypeName(stationType).Data(),
639                     PlaneTypeName(plane).Data(),
640                     digit.ManuId(),
641                     m));
642     return -1;
643   }
644   
645   Int_t busPatchId = vec->At(pos);
646
647   fMappingTimer.Stop();
648   
649   return busPatchId;
650 }
651
652 //____________________________________________________________________
653 Int_t AliMUONRawWriter::WriteTriggerDDL()
654 {
655   //
656   // Write trigger DDL
657   //
658   fTriggerTimer.Start(kFALSE);
659   
660  // DDL event one per half chamber
661
662   // stored local id number 
663   TArrayI isFired(256);
664   isFired.Reset();
665
666
667  // DDL header size
668   Int_t headerSize = sizeof(AliRawDataHeader)/4;
669
670   TClonesArray* localTrigger;
671   TClonesArray* globalTrigger;
672   AliMUONGlobalTrigger* gloTrg;
673   AliMUONLocalTrigger* locTrg = 0x0;
674
675   // global trigger for trigger pattern
676   globalTrigger = fMUONData->GlobalTrigger(); 
677   gloTrg = (AliMUONGlobalTrigger*)globalTrigger->UncheckedAt(0);
678   Int_t gloTrigPat = gloTrg->GetGlobalPattern();
679
680   // local trigger 
681   localTrigger = fMUONData->LocalTrigger();    
682
683   UInt_t word;
684   Int_t* buffer = 0;
685   Int_t index;
686   Int_t iEntries = 0;
687   Int_t iLocCard, locCard;
688   Char_t locDec, trigY, posY, posX,regOut;
689   Int_t devX;
690   Int_t version = 1; // software version
691   Int_t eventPhys = 1; // trigger type: 1 for physics, 0 for software
692   Int_t serialNb = 0xF; // serial nb of card: all bits on for the moment
693   Int_t globalFlag = 0; // set to 1 if global info present in DDL else set to 0
694
695   // size of headers
696   static const Int_t kDarcHeaderLength   = fDarcHeader->GetDarcHeaderLength();
697   static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
698   static const Int_t kDarcScalerLength   = fDarcHeader->GetDarcScalerLength();
699   static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
700   static const Int_t kRegHeaderLength    = fRegHeader->GetHeaderLength();
701   static const Int_t kRegScalerLength    = fRegHeader->GetScalerLength();
702   static const Int_t kLocHeaderLength    = fLocalStruct->GetLength();
703   static const Int_t kLocScalerLength    = fLocalStruct->GetScalerLength();
704
705   // [16(local)*6 words + 6 words]*8(reg) + 8 words = 824 
706   static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) +  (kRegHeaderLength+1))* 8 
707       +  kDarcHeaderLength + kGlobalHeaderLength + 2;
708
709   // [16(local)*51 words + 16 words]*8(reg) + 8 + 10 + 8 words scaler event 6682 words
710   static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength +  kLocScalerLength +1) +  
711                                          (kRegHeaderLength + kRegScalerLength +1))* 8 +
712                                          (kDarcHeaderLength + kDarcScalerLength + 
713                                           kGlobalHeaderLength + kGlobalScalerLength + 2);
714   if(fScalerEvent)
715     eventPhys = 0; //set to generate scaler events
716
717   Int_t nEntries = (Int_t) (localTrigger->GetEntries());// 234 local cards
718   // stored the local card id that's fired
719   for (Int_t i = 0; i <  nEntries; i++) {
720     locTrg = (AliMUONLocalTrigger*)localTrigger->At(i);
721     isFired[locTrg->LoCircuit()] = 1; // storing local boards with informations
722   }
723
724   if (!nEntries)
725     AliInfo("No Trigger information available");
726
727   if(fScalerEvent)
728     buffer = new Int_t [kScalerBufferSize];
729   else
730     buffer = new Int_t [kBufferSize];
731
732
733   // open DDL file, on per 1/2 chamber
734   for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
735     
736     index = 0; 
737
738     if (iDDL == 0) // suppose global info in DDL one
739       globalFlag = 1;
740     else 
741       globalFlag = 0;
742
743     word = 0;
744     // set darc status word
745     // see AliMUONDarcHeader.h for details
746     AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
747     AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
748     AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
749     AliBitPacking::PackWord((UInt_t)version,word,12,19);
750     fDarcHeader->SetWord(word);
751
752     memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4); 
753     index += fDarcHeader->GetDarcHeaderLength();
754
755     if (iDDL == 0)
756      fDarcHeader->SetGlobalOutput(gloTrigPat);// no global input for the moment....
757     else 
758      fDarcHeader->SetGlobalOutput(0);
759
760     if (fScalerEvent) {
761       // 6 DARC scaler words
762       memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
763       index += fDarcHeader->GetDarcScalerLength();
764     }
765     // end of darc word
766     buffer[index++] = fDarcHeader->GetEndOfDarc();
767
768     // 4 words of global board input + Global board output
769     memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4); 
770     index += fDarcHeader->GetGlobalHeaderLength(); 
771
772     if (fScalerEvent) {
773       // 10 Global scaler words
774       memcpy(fDarcHeader->GetGlobalScalers(), &buffer[index], kGlobalScalerLength*4);
775       index += fDarcHeader->GetGlobalScalerLength();
776     }
777
778     // end of global word
779     buffer[index++] = fDarcHeader->GetEndOfGlobal();
780
781     // 8 regional cards per DDL
782     for (Int_t iReg = 0; iReg < 8; iReg++) {
783
784       // Regional card header
785       word = 0;
786
787       // set darc status word
788       fRegHeader->SetDarcWord(word);
789
790       regOut  = 0;
791       // fill darc word, not darc status for the moment (empty)
792       //see  AliMUONRegHeader.h for details
793       AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31); 
794       AliBitPacking::PackWord((UInt_t)serialNb,word,19,24); 
795       AliBitPacking::PackWord((UInt_t)version,word,16,23);
796       AliBitPacking::PackWord((UInt_t)iReg,word,15,18);
797       AliBitPacking::PackWord((UInt_t)regOut,word,0,7); // whenever regional output will be implemented
798       fRegHeader->SetWord(word);
799
800       memcpy(&buffer[index],fRegHeader->GetHeader(),fRegHeader->GetHeaderLength()*4);
801       index += fRegHeader->GetHeaderLength();
802
803       // 11 regional scaler word
804       if (fScalerEvent) {
805         memcpy(&buffer[index], fRegHeader->GetScalers(), fRegHeader->GetScalerLength()*4);
806         index += fRegHeader->GetScalerLength();
807       }
808
809       // end of regional word
810       buffer[index++] = fRegHeader->GetEndOfReg();
811
812       // 16 local card per regional board
813       for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
814
815         iLocCard = iLoc + iReg*16 + iDDL*128;
816
817         if (isFired[iLocCard]) {
818           locTrg = (AliMUONLocalTrigger*)localTrigger->At(iEntries);
819           locCard = locTrg->LoCircuit();
820           locDec  = locTrg->GetLoDecision();
821           trigY = 0;
822           posY = locTrg->LoStripY();
823           posX = locTrg->LoStripX();
824           devX = locTrg->LoDev();
825           AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n", 
826                           locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
827         } else { //no trigger (see PRR chpt 3.4)
828           locCard = -1; // not possible on 4 bits
829           locDec = 0;
830           trigY = 1;
831           posY = 15;
832           posX = 0;
833           devX = 0x8;
834         }
835
836         //packing word
837         word = 0;
838         AliBitPacking::PackWord((UInt_t)(iLocCard % 16),word,19,22); //card id number in crate
839         AliBitPacking::PackWord((UInt_t)locDec,word,15,18);
840         AliBitPacking::PackWord((UInt_t)trigY,word,14,14);
841         AliBitPacking::PackWord((UInt_t)posY,word,10,13);
842         AliBitPacking::PackWord((UInt_t)devX,word,5,9);
843         AliBitPacking::PackWord((UInt_t)posX,word,0,4);
844
845         if (locCard == iLocCard) {
846           // add local cards structure
847           buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
848           buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
849           buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
850           buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
851           buffer[index++] = (Int_t)word; // data word
852           if (iEntries < nEntries-1)
853             iEntries++;
854         } else {
855           buffer[index++] = 0; // 4 words for x1, x2, y1, y2
856           buffer[index++] = 0; 
857           buffer[index++] = 0; 
858           buffer[index++] = 0; 
859           buffer[index++] = (Int_t)word; // data word
860
861         }
862         // 45 regional scaler word
863         if (fScalerEvent) {
864           memcpy(&buffer[index], fLocalStruct->GetScalers(), fLocalStruct->GetScalerLength()*4);
865           index += fLocalStruct->GetScalerLength();
866         }
867
868         // end of local structure words
869         buffer[index++] = fLocalStruct->GetEndOfLocal();
870
871       } // local card 
872
873     } // Regional card
874     
875
876     // writting onto disk
877     // write DDL 1
878     fHeader.fSize = (index + headerSize) * 4;// total length in bytes
879     fwrite((char*)(&fHeader),headerSize*4,1,fFile[iDDL]);
880     fwrite(buffer,sizeof(int),index,fFile[iDDL]);
881   
882   }
883   delete[] buffer;
884
885   fTriggerTimer.Stop();
886   
887   return kTRUE;
888 }
889 //____________________________________________________________________
890 void AliMUONRawWriter::SetScalersNumbers()
891 {
892   // set numbers for scaler events for trigger headers
893   // since this is provided by the experiment
894   // put dummy numbers to check the monitoring
895
896   fDarcHeader->SetScalersNumbers();
897   fRegHeader->SetScalersNumbers();
898   fLocalStruct->SetScalersNumbers();
899  
900   fScalerEvent = kTRUE;
901 }