]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONRawWriter.cxx
The Root libraries should not be addedt to ELIBS
[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 /// \class AliMUONRawWriter
20 /// MUON Raw Data generaton in ALICE-MUON
21 /// Raw data structure could be found in Alice-note.
22 ///
23 /// Implemented non-constant buspatch numbers for tracking
24 /// with correct DDL id (first guess)
25 /// (Ch. Finck, dec 2005)
26 ///
27 /// Digits2Raw:
28 /// Generates raw data for MUON tracker and finally for trigger
29 /// Using real mapping (inverse) for tracker
30 /// For trigger there is no mapping (mapping could be found in AliMUONTriggerCircuit)
31 /// Ch. Finck, July 04
32 /// Use memcpy instead of assignment elt by elt
33 /// Introducing variable DSP numbers, real manu numbers per buspatch for st12
34 /// Implemented scaler event for Trigger
35 /// Ch. Finck, Jan. 06
36 /// Using bus itr in DDL instead of simple incrementation
37 /// treat correctly the DDL & buspatch for station 3.
38 /// Using informations from AliMUONTriggerCrateStore for 
39 /// empty slots and non-notified cards in trigger crates.
40 /// Ch. Finck, August 06.
41 /// Using AliMpDDLStore::GetBusPatchId.
42 ///
43 /// \author Ch. Finck, Feb. 07.
44 //-----------------------------------------------------------------------------
45
46
47 #include "AliMUONRawWriter.h"
48
49 #include "AliMUONBlockHeader.h"
50 #include "AliMUONBusStruct.h"
51 #include "AliMUONConstants.h"
52 #include "AliMUONDarcHeader.h"
53 #include "AliMUONVDigit.h"
54 #include "AliMUONVDigitStore.h"
55 #include "AliMUONDspHeader.h"
56 #include "AliMUONGlobalTrigger.h"
57 #include "AliMUONLocalStruct.h"
58 #include "AliMUONLocalTrigger.h"
59 #include "AliMUONLocalTriggerBoard.h"
60 #include "AliMUONRegionalTrigger.h"
61 #include "AliMUONRegHeader.h"
62
63 #include "AliMUONVTriggerStore.h"
64 #include "AliCodeTimer.h"
65
66 #include "AliMpCDB.h"
67 #include "AliMpDDLStore.h"
68 #include "AliMpDDL.h"
69 #include "AliMpRegionalTrigger.h"
70 #include "AliMpTriggerCrate.h"
71 #include "AliMpLocalBoard.h"
72 #include "AliMpDetElement.h"
73 #include "AliMpDEManager.h"
74 #include "AliMpExMap.h"
75 #include "AliMpConstants.h"
76 #include "AliMpPlaneType.h"
77 #include "AliMpSegmentation.h"
78 #include "AliMpStationType.h"
79 #include "AliMpVSegmentation.h"
80
81 #include "AliRawReader.h"
82 #include "AliRawDataHeaderSim.h"
83 #include "AliBitPacking.h" 
84 #include "AliDAQ.h"
85 #include "AliLog.h"
86
87 #include "TObjArray.h"
88 #include "TStopwatch.h"
89 #include <Riostream.h>
90
91 /// \cond CLASSIMP
92 ClassImp(AliMUONRawWriter) // Class implementation in ROOT context
93 /// \endcond
94
95 //__________________________________________________________________________
96 AliMUONRawWriter::AliMUONRawWriter()
97   : TObject(),
98     fBlockHeader(new AliMUONBlockHeader()),
99     fDspHeader(new AliMUONDspHeader()),
100     fDarcHeader(new AliMUONDarcHeader()),
101     fRegHeader(new AliMUONRegHeader()),
102     fLocalStruct(new AliMUONLocalStruct()),
103     fDDLStore(AliMpDDLStore::Instance()),
104     fScalerEvent(kFALSE),
105     fHeader(0x0),
106     fBufferSize((((43*AliMpConstants::ManuNofChannels() + 4)*5 + 10)*5 + 8)*2),
107     fBuffer(new Int_t [fBufferSize])
108 {
109   /// Standard Constructor
110
111   AliDebug(1,"Standard ctor");
112
113   // setting data key to default value (only for writting)
114   fBlockHeader->SetDataKey(fBlockHeader->GetDefaultDataKey());
115   fDspHeader->SetDataKey(fDspHeader->GetDefaultDataKey());
116
117   // Load mapping
118   if ( ! fDDLStore ) {
119     if ( ! AliMpCDB::LoadDDLStore() ) {
120       AliFatal("Could not access mapping from OCDB !");
121     }
122     fDDLStore = AliMpDDLStore::Instance();
123   }  
124 }
125
126 //__________________________________________________________________________
127 AliMUONRawWriter::~AliMUONRawWriter(void)
128 {
129   /// Destructor
130
131   AliDebug(1,"dtor");
132   
133   delete fBlockHeader;
134   delete fDspHeader;
135   delete fDarcHeader;
136   delete fRegHeader;
137   delete fLocalStruct;
138   delete[] fBuffer;
139 }
140
141 //____________________________________________________________________
142 void  AliMUONRawWriter::LocalWordPacking(UInt_t& word, UInt_t locId, UInt_t locDec, 
143                                          UInt_t trigY, UInt_t posY, UInt_t posX, 
144                                          UInt_t sdevX, UInt_t devX)
145 {
146 /// pack local trigger word
147
148     AliBitPacking::PackWord(locId,word,19,22); //card id number in crate
149     AliBitPacking::PackWord(locDec,word,15,18);
150     AliBitPacking::PackWord(trigY,word,14,14);
151     AliBitPacking::PackWord(posY,word,10,13);
152     AliBitPacking::PackWord(sdevX,word,9,9);
153     AliBitPacking::PackWord(devX,word,5,8);
154     AliBitPacking::PackWord(posX,word,0,4);
155
156 }
157
158 //____________________________________________________________________
159 Int_t AliMUONRawWriter::Digits2Raw(const AliMUONVDigitStore* digitStore,
160                                    const AliMUONVTriggerStore* triggerStore)
161 {
162   /// convert digits of the current event to raw data
163
164   AliCodeTimerAuto("",0)
165   
166   Int_t idDDL;
167   Char_t name[255];
168
169   // tracking chambers
170   
171   if ( digitStore ) 
172   {
173     AliCodeTimerAuto("for Tracker",1)
174
175     AliMpExMap busPatchMap;
176
177     Int_t nDDLs = AliDAQ::NumberOfDdls("MUONTRK");
178     
179     Int_t nofBusPatches(0);
180     
181     for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL ) 
182     {
183       AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
184       nofBusPatches += ddl->GetNofBusPatches();
185     }
186     
187     busPatchMap.SetSize(nofBusPatches);
188     
189     Digits2BusPatchMap(*digitStore,busPatchMap);
190
191     for (Int_t iDDL = 0; iDDL < nDDLs; ++iDDL ) 
192     {
193       WriteTrackerDDL(busPatchMap,iDDL);
194     }
195     AliDebug(1,"Tracker written");
196   }
197  
198   if ( triggerStore )
199   {
200     AliCodeTimerAuto("for Trigger",1)
201
202     // trigger chambers
203     
204     AliFstream* file[2];
205     
206     // open files
207     idDDL = 0;// MUTR
208     strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
209     file[0] = new AliFstream(name);
210     
211     idDDL = 1;// MUTR
212     strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
213     file[1] = new AliFstream(name);
214       
215     WriteTriggerDDL(*triggerStore,file);
216       
217     // reset and close
218     delete file[0];
219     delete file[1];
220       
221     AliDebug(1,"Trigger written");
222   }
223   
224   return kTRUE;
225 }
226
227 //______________________________________________________________________________
228 void 
229 AliMUONRawWriter::Digits2BusPatchMap(const AliMUONVDigitStore& digitStore,
230                                      AliMpExMap& busPatchMap)
231 {
232   /// Create bus patch structures corresponding to digits in the store
233   
234   AliCodeTimerAuto("",0)
235   
236   static const Int_t kMAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
237     
238   // DDL event one per half chamber
239   
240   // raw data
241   Char_t parity = 0x4;
242   UShort_t manuId = 0;
243   UChar_t channelId = 0;
244   UShort_t charge = 0;
245   Int_t busPatchId = 0;
246   Int_t currentBusPatchId = -1;
247   UInt_t word;
248   
249   AliMUONBusStruct* busStruct(0x0);
250   
251   TIter next(digitStore.CreateTrackerIterator());
252   AliMUONVDigit* digit;
253   
254   while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
255   {
256     charge = digit->ADC();
257     if ( charge > kMAXADC )
258     {
259       // This is most probably an error in the digitizer (which should insure
260       // the adc is below kMAXADC), so make it a (non-fatal) error indeed.
261       AliError(Form("adc value %d above 0x%x for DE %d . Setting to 0x%x. Digit is:",
262                     charge,kMAXADC,digit->DetElemId(),kMAXADC));
263       StdoutToAliError(digit->Print());
264       charge = kMAXADC;
265     }
266     
267     // inverse mapping
268     busPatchId = GetBusPatch(*digit);
269
270     if (busPatchId<0) continue;
271     
272     if ( digit->ManuId() > 0x7FF ||
273          digit->ManuChannel() > 0x3F )
274     {
275       StdoutToAliError(digit->Print(););
276       AliFatal("ManuId,ManuChannel are invalid for the digit above.");
277     }
278     
279     manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
280     channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
281     
282     //packing word
283     word = 0;
284     AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
285     AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
286     AliBitPacking::PackWord((UInt_t)charge,word,0,11);
287     
288     // parity word
289     parity = word & 0x1;
290     for (Int_t i = 1; i <= 30; ++i) 
291     {
292       parity ^=  ((word >> i) & 0x1);
293     }
294     AliBitPacking::PackWord((UInt_t)parity,word,31,31);
295
296     if ( currentBusPatchId != busPatchId ) 
297     {
298       busStruct = 
299         static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(busPatchId));
300       currentBusPatchId = busPatchId;
301     }
302     
303     if (!busStruct)
304     {
305       busStruct = new AliMUONBusStruct;
306       busStruct->SetDataKey(busStruct->GetDefaultDataKey());
307       busStruct->SetBusPatchId(busPatchId);
308       busStruct->SetLength(0);
309       busPatchMap.Add(busPatchId,busStruct);
310     }
311     
312     // set sub Event
313     busStruct->AddData(word);
314   }
315 }
316
317 //______________________________________________________________________________
318 void
319 AliMUONRawWriter::WriteTrackerDDL(AliMpExMap& busPatchMap, Int_t iDDL)
320 {
321   /// Write DDL file for one tracker DDL
322   
323   // buffer size (max'ed out)
324   // (((43 manus max per bus patch *64 channels + 4 bus patch words) * 5 bus patch 
325   //   + 10 dsp words)*5 dsps + 8 block words)*2 blocks 
326  
327   AliCodeTimerAuto("",0)
328
329   if (fHeader == 0x0) {
330     AliError("Raw data header must be set");
331     return;
332   }
333   memset(fBuffer,0,fBufferSize*sizeof(Int_t));
334   
335   AliMpDDL* ddl = fDDLStore->GetDDL(iDDL);
336   Int_t iDspMax = ddl->GetMaxDsp();
337   Int_t iBusPerDSP[5]; //number of bus patches per DSP
338   ddl->GetBusPerDsp(iBusPerDSP);
339   Int_t busIter = 0;
340   
341   Int_t totalDDLLength = 0;
342   
343   Int_t index = 0;
344   
345   // two blocks A and B per DDL
346   for (Int_t iBlock = 0; iBlock < 2; ++iBlock) 
347   {
348     // block header
349     Int_t length = fBlockHeader->GetHeaderLength();
350     memcpy(&fBuffer[index],fBlockHeader->GetHeader(),length*4);
351     Int_t indexBlk = index;
352     index += length; 
353     
354     // 5 DSP's max per block
355     for (Int_t iDsp = 0; iDsp < iDspMax; ++iDsp) 
356     {
357       // DSP header
358       Int_t dspHeaderLength = fDspHeader->GetHeaderLength();
359       memcpy(&fBuffer[index],fDspHeader->GetHeader(),dspHeaderLength*4);
360       Int_t indexDsp = index;
361       index += dspHeaderLength; 
362       
363       // 5 buspatches max per DSP
364       for (Int_t i = 0; i < iBusPerDSP[iDsp]; ++i) 
365       {
366         Int_t iBusPatch = ddl->GetBusPatchId(busIter++);
367         
368         // iteration over bus patch in DDL
369         if (iBusPatch == -1) 
370         {
371           AliWarning(Form("Error in bus itr in DDL %d\n", iDDL));
372           continue;
373         }
374         
375         AliMUONBusStruct* busStructPtr = static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(iBusPatch));
376         
377         // check if buspatchid has digit
378         if (busStructPtr) 
379         {
380           // add bus patch structure header
381           Int_t busHeaderLength = busStructPtr->GetHeaderLength();
382           memcpy(&fBuffer[index],busStructPtr->GetHeader(),busHeaderLength*4);
383           index += busHeaderLength;
384           
385           // add bus patch data
386           Int_t busLength = busStructPtr->GetLength();
387           memcpy(&fBuffer[index],busStructPtr->GetData(),busLength*4);
388           index += busLength;
389         } 
390         else 
391         {
392           // writting anyhow buspatch structure (empty ones)
393           fBuffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
394           fBuffer[index++] = busStructPtr->GetHeaderLength(); // header length
395           fBuffer[index++] = 0; // raw data length
396           fBuffer[index++] = iBusPatch; // bus patch
397         }
398       } // bus patch
399       
400       // check if totalLength even
401       // set padding word in case
402       // Add one word 0xBEEFFACE at the end of DSP structure
403       Int_t totalDspLength  = index - indexDsp;
404       if ((totalDspLength % 2) == 1) 
405       { 
406         fBuffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
407         fBuffer[index++] = fDspHeader->GetDefaultPaddingWord();
408         totalDspLength++;
409       }
410       
411       Int_t dspLength     = totalDspLength - fDspHeader->GetHeaderLength();
412       
413       fBuffer[indexDsp+1] = totalDspLength; // dsp total length
414       fBuffer[indexDsp+2] = dspLength; // data length  
415       
416     } // dsp
417     
418     Int_t totalBlkLength  = index - indexBlk;
419     Int_t blkLength       = totalBlkLength - fBlockHeader->GetHeaderLength();
420     totalDDLLength       += totalBlkLength;
421     
422     fBuffer[indexBlk+1] = totalBlkLength; // total block length
423     fBuffer[indexBlk+2] = blkLength;
424         
425   } // block
426   
427     // add twice the end of CRT structure data key
428     // hope it's good placed (ChF)
429     fBuffer[index++] = fBlockHeader->GetDdlDataKey();
430     fBuffer[index++] = fBlockHeader->GetDdlDataKey();
431     totalDDLLength  += 2;
432   
433   // writting onto disk
434   // total length in bytes
435   // DDL header
436
437   Int_t headerSize = sizeof(AliRawDataHeader)/4;
438   
439   fHeader->fSize = (totalDDLLength + headerSize) * 4;
440   
441   AliFstream* file = new AliFstream(AliDAQ::DdlFileName("MUONTRK",iDDL));
442   
443   file->WriteBuffer((char*)fHeader,headerSize*4);
444   file->WriteBuffer((char*)fBuffer,sizeof(int)*index);
445   delete file;
446 }
447
448 //______________________________________________________________________________
449 Int_t AliMUONRawWriter::GetBusPatch(const AliMUONVDigit& digit) const
450 {
451   /// Determine the BusPatch this digit belongs to.
452
453     return fDDLStore->GetBusPatchId(digit.DetElemId(),digit.ManuId());
454 }
455
456 //______________________________________________________________________________
457 Int_t AliMUONRawWriter::WriteTriggerDDL(const AliMUONVTriggerStore& triggerStore, AliFstream* file[2])
458 {
459   /// Write trigger DDL
460   
461   AliCodeTimerAuto("",0)
462
463   if (fHeader == 0x0) {
464     AliError("Raw data header must be set");
465     return 0;
466   }
467
468  // DDL event one per half chamber
469
470  // DDL header size
471   Int_t headerSize = sizeof(AliRawDataHeader)/4;
472
473   // global trigger for trigger pattern
474   AliMUONGlobalTrigger* gloTrg = triggerStore.Global();
475   if (!gloTrg) 
476   {
477     return 0;
478   }
479   
480   Int_t gloTrigResp = gloTrg->GetGlobalResponse();
481   UInt_t *gloTrigInput = gloTrg->GetGlobalInput();
482
483   UInt_t word;
484   Int_t* buffer = 0;
485   Int_t index;
486   Int_t locCard;
487   UChar_t locDec, trigY, posY, posX, regOut;
488   UInt_t regInpLpt;
489   UInt_t regInpHpt;
490
491   UInt_t devX;
492   UChar_t sdevX;
493   UInt_t version = 1; // software version
494   UInt_t eventPhys = 1; // trigger type: 1 for physics, 0 for software
495   UInt_t serialNb = 0xF; // serial nb of card: all bits on for the moment
496   Int_t globalFlag = 0; // set to 1 if global info present in DDL else set to 0
497
498   // size of headers
499   static const Int_t kDarcHeaderLength   = fDarcHeader->GetDarcHeaderLength();
500   static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
501   static const Int_t kDarcScalerLength   = fDarcHeader->GetDarcScalerLength();
502   static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
503   static const Int_t kRegHeaderLength    = fRegHeader->GetHeaderLength();
504   static const Int_t kRegScalerLength    = fRegHeader->GetScalerLength();
505   static const Int_t kLocHeaderLength    = fLocalStruct->GetLength();
506   static const Int_t kLocScalerLength    = fLocalStruct->GetScalerLength();
507
508   // [16(local)*6 words + 6 words]*8(reg) + 8 words = 824 
509   static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) +  (kRegHeaderLength+1))* 8 
510       +  kDarcHeaderLength + kGlobalHeaderLength + 2;
511
512   // [16(local)*51 words + 16 words]*8(reg) + 8 + 10 + 8 words scaler event 6682 words
513   static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength +  kLocScalerLength +1) +  
514                                          (kRegHeaderLength + kRegScalerLength +1))* 8 +
515                                          (kDarcHeaderLength + kDarcScalerLength + 
516                                           kGlobalHeaderLength + kGlobalScalerLength + 2);
517   if(fScalerEvent) {
518     eventPhys = 0; //set to generate scaler events
519     fHeader->fWord2 |= (0x1 << 14); // set L1SwC bit on
520   }
521   if(fScalerEvent)
522     buffer = new Int_t [kScalerBufferSize];
523   else
524     buffer = new Int_t [kBufferSize];
525
526   // reset crate
527
528   // open DDL file, on per 1/2 chamber
529   for ( Int_t iDDL = 0; iDDL < 2; ++iDDL ) 
530   {
531     index = 0; 
532
533     if (iDDL == 0) // suppose global info in DDL one
534       globalFlag = 1;
535     else 
536       globalFlag = 0;
537
538     word = 0;
539     // set darc status word
540     // see AliMUONDarcHeader.h for details
541     AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
542     AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
543     AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
544     AliBitPacking::PackWord((UInt_t)version,word,12,19);
545     fDarcHeader->SetWord(word);
546
547     memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4); 
548     index += kDarcHeaderLength;
549
550     // no global input for the moment....
551     if (iDDL == 0) {
552       fDarcHeader->SetGlobalOutput(gloTrigResp);
553       for (Int_t ii = 0; ii < 4; ii++) {
554         fDarcHeader->SetGlobalInput(gloTrigInput[ii],ii);
555       }
556     } else {
557       fDarcHeader->SetGlobalOutput(0);
558     }
559
560     if (fScalerEvent) {
561       // 6 DARC scaler words
562       memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
563       index += kDarcScalerLength;
564     }
565     // end of darc word
566     buffer[index++] = fDarcHeader->GetEndOfDarc();
567
568     // 4 words of global board input + Global board output
569     memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4); 
570     index += kGlobalHeaderLength; 
571
572     if (fScalerEvent) {
573       // 10 Global scaler words
574       memcpy(&buffer[index], fDarcHeader->GetGlobalScalers(), kGlobalScalerLength*4);
575       index += kGlobalScalerLength;
576     }
577
578     // end of global word
579     buffer[index++] = fDarcHeader->GetEndOfGlobal();
580     const AliMpRegionalTrigger* reg = AliMpDDLStore::Instance()->GetRegionalTrigger(); 
581
582     Int_t nCrate = reg->GetNofTriggerCrates()/2;
583     // 8 regional cards per DDL
584     for (Int_t iReg = 0; iReg < nCrate; ++iReg) {
585
586         // crate info
587       AliMpTriggerCrate* crate = AliMpDDLStore::Instance()->GetTriggerCrate(iDDL, iReg);
588
589       if (!crate) 
590         AliWarning(Form("Missing crate number %d in DDL %d\n", iReg, iDDL));
591
592       // regional info tree, make sure that no reg card missing
593       AliMUONRegionalTrigger* regTrg  = triggerStore.FindRegional(crate->GetId());
594       if (!regTrg) 
595         AliError(Form("Missing regional board %d in trigger Store\n", crate->GetId()));
596     
597       // Regional card header
598       word = 0;
599
600       // set darc status word
601       fRegHeader->SetDarcWord(word);
602
603       regOut    = regTrg->GetOutput();
604       regInpLpt = regTrg->GetLocalOutput(0);
605       regInpHpt = regTrg->GetLocalOutput(1);
606
607       // fill darc word, not darc status for the moment (empty)
608       //see  AliMUONRegHeader.h for details
609       AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31); 
610       AliBitPacking::PackWord((UInt_t)serialNb,word,20,25); 
611       AliBitPacking::PackWord((UInt_t)version,word,8,15);
612       AliBitPacking::PackWord((UInt_t)crate->GetId(),word,16,19);
613       AliBitPacking::PackWord((UInt_t)regOut,word,0,7); 
614       fRegHeader->SetWord(word);
615
616
617       // fill header later, need local response
618       Int_t indexReg = index;
619       index += kRegHeaderLength;
620
621       // 11 regional scaler word
622       if (fScalerEvent) {
623         memcpy(&buffer[index], fRegHeader->GetScalers(), kRegScalerLength*4);
624         index += kRegScalerLength;
625       }
626
627       // end of regional word
628       buffer[index++] = fRegHeader->GetEndOfReg();
629       
630       // 16 local card per regional board
631       //      UShort_t localMask = 0x0;
632       
633       Int_t nLocalBoard = AliMpConstants::LocalBoardNofChannels();
634
635       for (Int_t iLoc = 0; iLoc < nLocalBoard; iLoc++) {
636           
637         // slot zero for Regional card
638         Int_t localBoardId = crate->GetLocalBoardId(iLoc);
639
640         if (localBoardId) { // if not empty slot
641           AliMpLocalBoard* localBoard = AliMpDDLStore::Instance()->GetLocalBoard(localBoardId);
642
643           if (localBoard->IsNotified()) {// if notified board 
644             AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(localBoardId);
645
646             locCard = locTrg->LoCircuit();
647             locDec  = locTrg->GetLoDecision();
648             trigY   = locTrg->LoTrigY();
649             posY    = locTrg->LoStripY();
650             posX    = locTrg->LoStripX();
651             devX    = locTrg->LoDev();
652             sdevX   = locTrg->LoSdev();
653                   
654             AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n", 
655                             locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));  
656             //packing word
657             word = 0;
658             LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY, 
659                              (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
660
661             buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
662             buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
663             buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
664             buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
665             buffer[index++] = (Int_t)word; // data word
666                       
667                 
668           }
669           // fill copy card X-Y inputs from the notified cards 
670           if (localBoard->GetInputXfrom() && localBoard->GetInputYfrom()) 
671           {
672             // not triggered
673             locDec = 0; trigY = 1; posY = 15;    
674             posX   = 0; devX  = 0; sdevX = 1;
675             LocalWordPacking(word, (UInt_t)iLoc, (UInt_t)locDec, (UInt_t)trigY, (UInt_t)posY, 
676                              (UInt_t)posX, (UInt_t)sdevX, (UInt_t)devX);
677
678             Int_t localFromId = localBoard->GetInputXfrom();
679             AliMUONLocalTrigger* locTrgfrom  = triggerStore.FindLocal(localFromId);
680
681             buffer[index++] = 0; // copy only X3-4 & Y1-4
682             buffer[index++] = (locTrgfrom->GetX3Pattern() | (locTrgfrom->GetX4Pattern() << 16));
683             buffer[index++] = (locTrgfrom->GetY1Pattern() | (locTrgfrom->GetY2Pattern() << 16));
684             buffer[index++] = (locTrgfrom->GetY3Pattern() | (locTrgfrom->GetY4Pattern() << 16));
685             buffer[index++] = word;
686           }
687
688         } else { 
689           // fill with 10CDEAD word for empty slots
690           for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
691               buffer[index++] = fLocalStruct->GetDisableWord(); 
692         }// condition localBoard
693           
694         // 45 regional scaler word
695         if (fScalerEvent) {
696           memcpy(&buffer[index], fLocalStruct->GetScalers(), kLocScalerLength*4);
697           index += kLocScalerLength;
698         }
699           
700         // end of local structure words
701         buffer[index++] = fLocalStruct->GetEndOfLocal();
702           
703       } // local card 
704       // fill regional header with local output
705       fRegHeader->SetInput(regInpLpt, 0);
706       fRegHeader->SetInput(regInpHpt, 1);
707       memcpy(&buffer[indexReg],fRegHeader->GetHeader(),kRegHeaderLength*4);
708       
709     } // Regional card
710     
711
712     // writting onto disk
713     // write DDL's
714     fHeader->fSize = (index + headerSize) * 4;// total length in bytes
715     file[iDDL]->WriteBuffer((char*)fHeader,headerSize*4);
716     file[iDDL]->WriteBuffer((char*)buffer,sizeof(int)*index);
717   
718   }
719   delete[] buffer;
720
721   return kTRUE;
722 }
723
724 //____________________________________________________________________
725 void AliMUONRawWriter::SetScalersNumbers()
726 {
727   /// set numbers for scaler events for trigger headers
728   /// since this is provided by the experiment
729   /// put dummy numbers to check the monitoring
730
731   fDarcHeader->SetScalersNumbers();
732   fRegHeader->SetScalersNumbers();
733   fLocalStruct->SetScalersNumbers();
734  
735   fScalerEvent = kTRUE;
736 }
737