]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTriggerIO.cxx
- Added read/write of global crate file
[u/mrichter/AliRoot.git] / MUON / AliMUONTriggerIO.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 #include "AliMUONTriggerIO.h"
19
20 #include "AliLog.h"
21 #include "AliMpCDB.h"
22 #include "AliMpHelper.h"
23 #include "AliMpConstants.h"
24 #include "AliMpFiles.h"
25 #include "AliMpDDLStore.h"
26 #include "AliMpLocalBoard.h"
27 #include "AliMpTriggerCrate.h"
28 #include "AliMUONTriggerLut.h"
29 #include "AliMUONCalibParamNI.h"
30 #include "AliMUONVStore.h"
31
32
33 #include <Riostream.h>
34 #include <TSystem.h>
35
36 /// \class AliMUONTriggerIO
37 ///
38 /// Handles read/write of masks and LUT to/from online files, 
39 /// to be used by Shuttle and Trigger DA.
40 /// 
41 /// \author Laurent Aphecetche, Christian Finck Subatech
42 /// \author Bogdan Vulpescu, LPC Clermont-Ferrand
43
44 /// \cond CLASSIMP
45 ClassImp(AliMUONTriggerIO)
46 /// \endcond
47
48 //_____________________________________________________________________________
49 AliMUONTriggerIO::AliMUONTriggerIO() 
50     : TObject(), 
51       fLocalBoardIds(), 
52       fNofLocalBoards(0),
53       fTriggerCrates(true),
54       fLocalBoards(true),
55       fGlobalCrate()
56 {
57   /// ctor
58
59     fTriggerCrates.SetOwner(true);
60     fTriggerCrates.SetSize(AliMpConstants::LocalBoardNofChannels());
61
62     fLocalBoards.SetOwner(true);
63     fLocalBoards.SetSize(AliMpConstants::NofLocalBoards()+8); // included non-notified board FIXEME should be put in AliMpConstants
64 }
65
66 //_____________________________________________________________________________
67 AliMUONTriggerIO::AliMUONTriggerIO(const char* regionalFileToRead) 
68     :TObject(), 
69      fLocalBoardIds(), 
70      fNofLocalBoards(0),
71      fTriggerCrates(true),
72      fLocalBoards(true),
73      fGlobalCrate()
74 {
75   /// ctor
76   ReadRegional(regionalFileToRead,0);
77 }
78
79 //_____________________________________________________________________________
80 AliMUONTriggerIO::~AliMUONTriggerIO()
81 {
82   /// dtor
83 }
84
85 //_____________________________________________________________________________
86 void 
87 AliMUONTriggerIO::DeCompAddress(UChar_t &ypos, UChar_t &ytri, UChar_t &xdev, UChar_t &xpos, 
88                                 UShort_t address) const
89 {  
90   /// decompose the 15-bits address
91   
92   UChar_t bitsYpos = 4;
93   UChar_t bitsYtri = 1;
94   UChar_t bitsXdev = 5;
95   //  UChar_t bitsXpos = 5;
96   
97   UShort_t maskYpos = 0x000F; // ...0 00001111
98   UShort_t maskYtri = 0x0001; // ...0 00000001
99   UShort_t maskXdev = 0x001F; // ...0 00011111
100   UShort_t maskXpos = 0x001F; // ...0 00011111
101   
102   ypos =  address                                  & maskYpos;
103   ytri = (address >>  bitsYpos)                    & maskYtri;
104   xdev = (address >> (bitsYpos+bitsYtri))          & maskXdev;
105   xpos = (address >> (bitsYpos+bitsYtri+bitsXdev)) & maskXpos;
106 }
107
108 //_____________________________________________________________________________
109 void 
110 AliMUONTriggerIO::FillLut(AliMUONTriggerLut& lut,
111                           Int_t icirc, UChar_t istripX, UChar_t idev,  
112                           Int_t lutLpt[16][2], Int_t lutHpt[16][2]) 
113 {
114   /// Fill the LUT histograms
115   
116   if (icirc == 0 && istripX == 0 && idev == 0) 
117   {
118     AliDebug(1,"Copy board, not filled ...");
119     return;
120   }
121   
122   Short_t iLptPlus, iLptMinu, iLptUnde;
123   Short_t iHptPlus, iHptMinu, iHptUnde;
124
125   iLptPlus = iLptMinu = iLptUnde = 0;
126   iHptPlus = iHptMinu = iHptUnde = 0;
127   
128   for (Int_t istripY=0; istripY<16; istripY++) 
129   {
130     if (lutLpt[istripY][1] == 0 && lutLpt[istripY][0] ==1)
131       iLptMinu=iLptMinu+(1 << istripY);
132     if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==0)
133       iLptPlus=iLptPlus+(1 << istripY);
134     if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==1)
135       iLptUnde=iLptUnde+(1 << istripY);
136     
137     if (lutHpt[istripY][1] == 0 && lutHpt[istripY][0] ==1)
138       iHptMinu=iHptMinu+(1 << istripY);
139     if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==0)
140       iHptPlus=iHptPlus+(1 << istripY);
141     if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==1)
142       iHptUnde=iHptUnde+(1 << istripY);
143     
144   } // loop on istripY
145   
146   lut.SetContent("LptMinu",icirc,istripX,idev,iLptMinu);
147   lut.SetContent("LptUnde",icirc,istripX,idev,iLptUnde);
148   lut.SetContent("LptPlus",icirc,istripX,idev,iLptPlus);
149
150   lut.SetContent("HptMinu",icirc,istripX,idev,iLptMinu);
151   lut.SetContent("HptUnde",icirc,istripX,idev,iLptUnde);
152   lut.SetContent("HptPlus",icirc,istripX,idev,iLptPlus);
153 }
154
155 //_____________________________________________________________________________
156 Int_t 
157 AliMUONTriggerIO::LocalBoardId(Int_t index) const
158 {
159   /// Return the i-th localBoardId, or -1 if index is out of bounds
160   if ( index >= 0 && index < fNofLocalBoards ) 
161   {
162     return fLocalBoardIds[index];
163   }
164   return -1;
165 }
166
167 //_____________________________________________________________________________
168 Int_t
169 AliMUONTriggerIO::ReadLocalMasks(const char* localFile, AliMUONVStore& localMasks) const
170 {
171   /// Fills the local masks store from file
172   
173   if ( !NofLocalBoards() )
174   {
175     AliError("No local board to read");
176     return 0;
177   }
178   
179   FILE* fp = fopen(gSystem->ExpandPathName(localFile),"r");
180   if (!fp)
181   {
182     AliError(Form("Could not read file %s",localFile));
183     return 0;
184   }
185   
186   UShort_t maskBuffer[8];
187   
188   Int_t nLocalBoards(0);
189   
190   while ( fread ( maskBuffer, 2, 8, fp ) )
191   {
192     Int_t localBoardId = LocalBoardId(nLocalBoards);
193     AliDebug(1,Form("LB %03d X1 %4x X2 %4x X3 %4x X4 %4x "
194                     "Y1 %4x Y2 %4x Y3 %4x Y4 %4x",
195                     localBoardId,
196                     maskBuffer[0],
197                     maskBuffer[1],
198                     maskBuffer[2],
199                     maskBuffer[3],
200                     maskBuffer[4],
201                     maskBuffer[5],
202                     maskBuffer[6],
203                     maskBuffer[7]));
204     
205     if ( localBoardId ) 
206     {
207       AliMUONVCalibParam* localBoard = new AliMUONCalibParamNI(1,8,localBoardId,0,0);
208       for ( Int_t index = 0; index < 8; ++index )
209       {
210         localBoard->SetValueAsInt(index,0,maskBuffer[index]);
211       }
212       localMasks.Add(localBoard);
213     }
214     
215     ++nLocalBoards;
216   }
217   
218   if ( nLocalBoards != NofLocalBoards() ) 
219   {
220     AliError(Form("Read %d out of %d local boards",
221                   nLocalBoards, NofLocalBoards()));
222   }
223   
224   fclose(fp);
225   
226   return nLocalBoards;
227 }
228
229 //_____________________________________________________________________________
230 void
231 AliMUONTriggerIO::ReadLocalLUT(AliMUONTriggerLut& lut,
232                                Int_t localBoardId,
233                                FILE* flut)
234 {
235   /// Read the LUT for one local board from an online file
236
237   UShort_t address;
238   
239   UChar_t buffer;
240   UChar_t mask1 = 0xF0;
241   UChar_t mask2 = 0x0F;
242   UChar_t maskLpt = 0x0C;
243   UChar_t maskHpt = 0x03;
244   UChar_t lh, lpt, hpt;
245   
246   UChar_t xpos, xdev, ypos, ytri;
247   
248   Int_t lutLpt[16][2], lutHpt[16][2];
249
250   Int_t boardnr = localBoardId;
251   
252   AliDebug(1,Form("Reading LUT values for local board %d",boardnr));
253   
254   Int_t ny = 0;
255   
256   // create the 32767 addresses for the 4-bits lpt and hpt half-bytes
257   for (UShort_t ilut = 0; ilut < 0x7FFF; ilut += 2) 
258   {
259     // read two lut addresses at once
260     fread(&buffer,1,1,flut);
261     
262     // 1st 4-bits half-byte
263     address = ilut;   
264     lh = (buffer & mask1) >> 4;
265     
266     // Lpt and Hpt response
267     lpt = (lh & maskLpt) >> 2;
268     hpt =  lh & maskHpt;
269     
270     // decompose the 15-bits address
271     DeCompAddress(ypos,ytri,xdev,xpos,address);
272     
273     // calculate group of y-strips
274     if (ny < 16) 
275     {
276       lutLpt[ny][0] =  lpt & 1;
277       lutLpt[ny][1] = (lpt & 2) >> 1;
278       lutHpt[ny][0] =  hpt & 1;
279       lutHpt[ny][1] = (hpt & 2) >> 1;
280       ny++;
281       if (ny == 16) 
282       {
283         ny = 0;
284         // ytri == 1 means no trigger in y-direction
285         if (ytri == 0) 
286         {
287           FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
288         }
289       }
290     }
291     
292     // 2nd 4-bits half-byte
293     address = ilut+1; 
294     lh = (buffer & mask2);
295     
296     // Lpt and Hpt response
297     lpt = (lh & maskLpt) >> 2;
298     hpt =  lh & maskHpt;
299     
300     // decompose the 15-bits address
301     DeCompAddress(ypos,ytri,xdev,xpos,address);
302     
303     // calculate group of y-strips
304     if (ny < 16) 
305     {
306       lutLpt[ny][0] =  lpt & 1;
307       lutLpt[ny][1] = (lpt & 2) >> 1;
308       lutHpt[ny][0] =  hpt & 1;
309       lutHpt[ny][1] = (hpt & 2) >> 1;
310       ny++;
311       if (ny == 16) 
312       {
313         ny = 0;
314         // ytri == 1 means no trigger in y-direction
315         if (ytri == 0) 
316         {
317           FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
318         }
319       }
320     }
321   }
322 }
323
324 //_____________________________________________________________________________
325 Bool_t 
326 AliMUONTriggerIO::ReadLUT(const char* lutFileToRead, AliMUONTriggerLut& lut)
327 {
328   /// Fill the LUT object from online file
329   
330   if ( !NofLocalBoards() )
331   {
332     AliError("No local board id defined. Must read a regional file first");
333     return kFALSE;
334   }
335   
336   FILE* flut = fopen(gSystem->ExpandPathName(lutFileToRead),"rb");
337   if (!flut) 
338   {
339     AliError(Form("Could not read LUT file %s",lutFileToRead));
340     return kFALSE;
341   }   
342   
343   for ( Int_t i = 0; i < NofLocalBoards(); ++i ) 
344   {
345     ReadLocalLUT(lut,LocalBoardId(i),flut);
346   }
347   
348   fclose(flut);
349   
350   return kTRUE;
351   
352 }
353
354 //_____________________________________________________________________________
355 Bool_t 
356 AliMUONTriggerIO::ReadMasks(const char* localFile,
357                             const char* regionalFile,
358                             const char* globalFile,
359                             AliMUONVStore* localMasks,
360                             AliMUONVStore* regionalMasks,
361                             AliMUONVCalibParam* globalMasks,
362                             Bool_t warn)
363 {
364   /// Fill the various masks store from files
365   
366   if ( !regionalFile ) 
367   {
368     AliError("Must have a regional file name to proceeed");
369     return kFALSE;
370   }
371   
372   Int_t nCrates = ReadRegional(regionalFile,regionalMasks, warn);
373   
374   if (!nCrates) return kFALSE;
375   
376   if (localMasks && localFile)
377   {
378     Int_t nLocal = ReadLocalMasks(localFile,*localMasks);
379     AliDebug(1,Form("Read masks for %d local boards",nLocal));
380   }
381   
382   Int_t nDarc = ReadGlobal(globalFile, globalMasks);
383   AliDebug(1,Form("Read disable for %d DARC boards",nDarc));
384   
385   if (!nDarc) return kFALSE;
386   
387   return kTRUE;
388 }
389
390 //_____________________________________________________________________________
391  Int_t 
392  AliMUONTriggerIO::ReadGlobal(const char* globalFile, AliMUONVCalibParam* globalMasks)
393 {
394   /// read the global crate file and file corresponding mask
395   /// the masks are disable bit for each crate, 8 per darc board
396   /// bit value 0 means enable, 1 means disable                                                 * 
397   
398   Int_t nDarc = 0;
399   if (!AliMpDDLStore::ReadGlobalTrigger(fGlobalCrate, globalFile)) return 0;
400   
401   UChar_t mask    = fGlobalCrate.GetFirstDarcDisable();
402   ULong_t vmeAddr = fGlobalCrate.GetFirstDarcVmeAddr();   
403   if (vmeAddr) nDarc++;
404   globalMasks->SetValueAsInt(0,0,mask);
405   
406   mask    = fGlobalCrate.GetSecondDarcDisable();
407   vmeAddr = fGlobalCrate.GetSecondDarcVmeAddr();    
408   if (vmeAddr) nDarc++;
409   globalMasks->SetValueAsInt(1,0,mask);
410   
411   return nDarc;
412 }
413  
414 //_____________________________________________________________________________
415 Int_t
416 AliMUONTriggerIO::ReadRegional(const char* regionalFile, AliMUONVStore* regionalMasks, Bool_t warn)
417 {
418   /// Read regional file to fill the regional mask store *AND* 
419   /// determine the order in which local boards will appear in local 
420   /// and lut files.
421   
422   fLocalBoardIds.Reset();
423   fNofLocalBoards = 0;
424
425   AliMpLocalBoard* board = 0x0;
426   AliMpTriggerCrate* crate = 0x0;
427
428   std::ifstream in(gSystem->ExpandPathName(regionalFile));
429   if (!in.good()) 
430   {
431     AliError(Form("Cannot read file %s",regionalFile));
432     return 0;
433   }
434
435   char name[80];
436   char line[80];
437   
438   Int_t nCrates(0);
439   
440
441   if (warn)
442   {
443     if (!AliMpDDLStore::Instance(kFALSE))
444     {
445       AliMpCDB::LoadDDLStore();
446     }
447   }
448
449   while (!in.eof())
450   {
451     in.getline(name,80);
452     
453     if (!strlen(name)) break;
454
455     AliDebug(1,Form("Looking for crate %s",name));
456     
457     if (warn)
458     {
459       AliMpTriggerCrate* triggerCrate = AliMpDDLStore::Instance()->GetTriggerCrate(name);
460     
461       if (!triggerCrate)
462       {
463         AliError(Form("Mapping error : could not get crate %s",name));
464         return 0;
465       }
466     }
467     ++nCrates;
468     
469     UShort_t id, mask;
470     Int_t mode, coincidence;
471     
472     in.getline(line,80);    
473     sscanf(line,"%hx",&id);
474
475     in.getline(line,80);
476     sscanf(line,"%d",&mode);
477     
478     in.getline(line,80);
479     sscanf(line,"%d",&coincidence);
480     
481     in.getline(line,80);
482     sscanf(line,"%hx",&mask);
483
484     if (!GetTriggerCrate(name, false)) {
485       crate = new AliMpTriggerCrate(name, id, mask, mode, coincidence);
486       fTriggerCrates.Add(name, crate);
487     }
488
489     if ( regionalMasks ) 
490     {
491       AliMUONVCalibParam* regionalBoard = new AliMUONCalibParamNI(1,1,id,0,0);
492       regionalBoard->SetValueAsInt(0,0,mask);
493       regionalMasks->Add(regionalBoard);
494     }
495     
496     AliDebug(1,Form("Name %s ID %x Mode %d Coin %d Mask %x",
497                     name,id,mode,coincidence,mask));
498     
499     for ( Int_t i = 0; i < 16; ++i ) 
500     {
501       if ( (mask >> i ) & 0x1 )
502       {          
503         in.getline(line,80);
504         Char_t localBoardName[20];
505         Int_t j,localBoardId;
506         UInt_t switches;
507         sscanf(line,"%02d %s %03d %03x",&j,localBoardName,&localBoardId,&switches);
508         AliDebug(1,Form("%02d %s %03d %03x",j,localBoardName,localBoardId,switches));
509
510         // FIXEME should not need this array anymore
511         fLocalBoardIds.Set(fNofLocalBoards+1);
512         fLocalBoardIds[fNofLocalBoards] = localBoardId;
513         ++fNofLocalBoards;
514
515         board = new AliMpLocalBoard(localBoardId, localBoardName, j);
516         board->SetSwitch(switches);
517         board->SetCrate(name);
518         if (localBoardId > AliMpConstants::NofLocalBoards())
519           board->SetNotified(false);
520  
521         // DE list
522         in.getline(line,80);
523         TArrayI list;
524         TString tmp(AliMpHelper::Normalize(line));
525         AliMpHelper::DecodeName(tmp,' ',list);
526         
527         for (Int_t i = 0; i < list.GetSize(); ++i)
528           board->AddDE(list[i]);
529         
530          // set copy number and transverse connector
531         in.getline(line,80);
532         tmp = AliMpHelper::Normalize(line);
533         AliMpHelper::DecodeName(tmp,' ',list);
534   
535         board->SetInputXfrom(list[0]);
536         board->SetInputXto(list[1]);
537         
538         board->SetInputYfrom(list[2]);
539         board->SetInputYto(list[3]);
540         
541         board->SetTC(list[4]);
542         
543         fLocalBoards.Add(localBoardId, board);
544         crate->AddLocalBoard(localBoardId);
545       }
546     }
547   }
548   
549   return nCrates;  
550 }
551
552 //_____________________________________________________________________________
553 Bool_t 
554 AliMUONTriggerIO::WriteLUT(const AliMUONTriggerLut& lut,
555                            const char* lutFileToWrite)
556 {
557   /// Convert an offline lut into an online (binary) lut file
558   
559   if ( !NofLocalBoards() )
560   {
561     AliError("No local board id defined. Must read a regional file first");
562     return kFALSE;
563   }
564   
565   FILE* flut = fopen(gSystem->ExpandPathName(lutFileToWrite),"wb");
566   if (!flut) 
567   {
568     AliError(Form("Could not create output LUT file %s",lutFileToWrite));
569     return kFALSE;
570   }   
571   
572   for ( Int_t i = 0; i < NofLocalBoards(); ++i ) 
573   {
574     WriteLocalLUT(lut,LocalBoardId(i),flut);
575   }
576   
577   fclose(flut);
578   
579   return kTRUE;
580 }
581
582 //_____________________________________________________________________________
583 Bool_t
584 AliMUONTriggerIO::WriteMasks(const char* localFile,
585                              const char* regionalFile,
586                              const char* globalFile,
587                              AliMUONVStore* localMasks,
588                              AliMUONVStore* regionalMasks,
589                              AliMUONVCalibParam* globalMasks) const
590 {
591     /// write mask files
592     Bool_t ok;
593     ok  = WriteLocalMasks(localFile, *localMasks);
594     ok &= WriteRegional(regionalFile, regionalMasks);
595     ok &= WriteGlobal(globalFile, globalMasks);
596     
597     return ok;
598 }
599
600  //_____________________________________________________________________________
601 Bool_t 
602 AliMUONTriggerIO::WriteGlobal(const char* globalFile, AliMUONVCalibParam* globalMasks) const
603 {
604     /// write global file
605     /// if no global masks defined take the one of configuration
606
607   ofstream out;
608   Int_t disable = 0;
609   
610   out.open(globalFile);
611   if (!out.good())
612   {
613     AliError(Form("Could not create output regional file %s", globalFile));
614     return kFALSE;
615   }
616    
617   out << fGlobalCrate.GetName() << endl;
618
619   // Jtag
620   out << fGlobalCrate.GetJtagName() << endl;
621   out << Form("0x%08x", fGlobalCrate.GetJtagVmeAddr()) << endl;
622   out << Form("%d %d %d", fGlobalCrate.GetJtagClockDiv(), 
623               fGlobalCrate.GetJtagRxPhase(), fGlobalCrate.GetJtagRdDelay()) << endl;
624  
625   for (Int_t i = 0; i < fGlobalCrate.GetJtagNofLines(); ++i)
626     out << Form("%d ", fGlobalCrate.GetEnableJtag(i));
627   out << endl;
628
629   
630   for (Int_t i = 0; i < fGlobalCrate.GetJtagNofLines(); ++i)
631   {
632     out << i << endl;
633     for (Int_t j = 0; j < fGlobalCrate.GetJtagNofLines(); ++j)
634       out << Form(" %s", fGlobalCrate.GetJtagCrateName(i,j).Data()) << endl;
635   }
636   
637   // first darc board
638   out << fGlobalCrate.GetFirstDarcName() << endl;
639   out << Form("0x%08x", fGlobalCrate.GetFirstDarcVmeAddr()) << endl;
640   out << fGlobalCrate.GetFirstDarcType() << endl;
641   if (globalMasks != 0x0)
642     disable = globalMasks->ValueAsInt(0);
643   else
644     disable = fGlobalCrate.GetFirstDarcDisable();
645   out << Form("0x%02x", disable) << endl;
646   out << Form("0x%x", fGlobalCrate.GetFirstDarcL0Delay()) << endl;
647   out << Form("0x%x", fGlobalCrate.GetFirstDarcL1TimeOut()) << endl;
648   
649   // second darc board
650   out << fGlobalCrate.GetSecondDarcName() << endl;
651   out << Form("0x%08x", fGlobalCrate.GetSecondDarcVmeAddr()) << endl;
652   out << fGlobalCrate.GetSecondDarcType() << endl;
653   if (globalMasks != 0x0)
654     disable = globalMasks->ValueAsInt(1);
655   else
656     disable = fGlobalCrate.GetSecondDarcDisable();
657   out << Form("0x%02x", disable) << endl;
658   out << Form("0x%x", fGlobalCrate.GetSecondDarcL0Delay()) << endl;
659   out << Form("0x%x", fGlobalCrate.GetSecondDarcL1TimeOut()) << endl; 
660   
661   // global board
662   out << fGlobalCrate.GetGlobalName() << endl;
663   out << Form("0x%08x", fGlobalCrate.GetGlobalVmeAddr()) << endl;
664   for (Int_t i = 0; i < fGlobalCrate.GetGlobalNofRegisters(); ++i)
665     out << Form("0x%x", fGlobalCrate.GetGlobalRegister(i)) << endl;
666   
667   // Fet board
668   out << fGlobalCrate.GetFetName() << endl;
669   out << Form("0x%08x", fGlobalCrate.GetFetVmeAddr()) << endl;
670   for (Int_t i = 0; i < fGlobalCrate.GetFetNofRegisters(); ++i)
671     out << Form("0x%x", fGlobalCrate.GetFetRegister(i)) << endl;
672   
673   return kTRUE;
674 }
675  
676
677 //_____________________________________________________________________________
678 Bool_t
679 AliMUONTriggerIO::WriteRegional(const char* regionalFile, AliMUONVStore* regionalMasks) const
680 {
681
682     /// write regional mask with the current configuration
683    /// if regional masks not defined, take the one from current configuration
684
685     ofstream out;
686     out.open(regionalFile);
687           
688     if (!out.good())
689     {
690       AliError(Form("Could not create output regional file %s",regionalFile));
691       return kFALSE;
692     }
693
694     for (Int_t iCrate = 0; iCrate < fTriggerCrates.GetSize(); ++iCrate) 
695     {
696       AliMpTriggerCrate* crate = static_cast<AliMpTriggerCrate*>(fTriggerCrates.GetObject(iCrate));
697
698       out << crate->GetName()  << endl;
699       out << Form("%02x", crate->GetId())   << endl;
700       out << crate->GetMode()  << endl;
701       out << crate->GetCoinc() << endl;
702       
703       UShort_t masks = 0;
704       if (regionalMasks != 0x0) 
705       {
706         AliMUONVCalibParam* maskParam = 
707             static_cast<AliMUONVCalibParam*>(regionalMasks->FindObject(crate->GetId()));
708         masks = maskParam->ValueAsInt(0,0);
709       } 
710       else
711       {
712         masks = crate->GetMask();
713       } 
714       
715       out << Form("%04x", masks) << endl;
716       
717       for (Int_t iLocal = 0; iLocal < crate->GetNofLocalBoards(); ++iLocal) 
718       {
719         Int_t localBoardId = crate->GetLocalBoardId(iLocal);
720
721         AliMpLocalBoard* board = static_cast<AliMpLocalBoard*>(fLocalBoards.GetValue(localBoardId));
722
723         out << Form("%02d ", board->GetSlot())  
724             << board->GetName() 
725             << Form(" %03d ", localBoardId) 
726             << Form("%03x", board->GetSwitch()) 
727             << endl;
728  
729         out << " ";
730         for (Int_t i = 0; i < board->GetNofDEs(); ++i)
731           out << Form("%4d ", board->GetDEId(i));
732         out << endl;
733           
734           // print copy card numbers
735         out << Form(" %4d %4d", board->GetInputXfrom(), board->GetInputXto());
736         out << Form(" %4d %4d", board->GetInputYfrom(), board->GetInputYto());
737         out << Form(" %4d",     board->GetTC()) << endl;
738       }
739     }
740     out.close();
741     
742     return kTRUE;
743 }
744
745 //_____________________________________________________________________________
746 Bool_t 
747 AliMUONTriggerIO::WriteLocalMasks(const char* localFile, AliMUONVStore& localMasks) const
748 {
749     /// write local mask
750     /// removing/adding enable for a local board need a update of the configuration 
751     /// before calling this method
752     /// mask are written for all boards including the copy card (Ch.F.)
753
754     FILE* fp = fopen(gSystem->ExpandPathName(localFile),"wb");
755     if (!fp) 
756     {
757       AliError(Form("Could not create output local file %s",localFile));
758       return kFALSE;
759     }   
760
761     UShort_t maskBuffer[8];
762
763     for (Int_t iCrate = 0; iCrate < fTriggerCrates.GetSize(); ++iCrate) 
764     {
765       AliMpTriggerCrate* crate = static_cast<AliMpTriggerCrate*>(fTriggerCrates.GetObject(iCrate));
766       
767       UShort_t mask = crate->GetMask(); // getting mask from current config
768
769       for (Int_t iLocal = 0; iLocal < crate->GetNofLocalBoards(); ++iLocal) 
770       {
771         Int_t localBoardId = crate->GetLocalBoardId(iLocal);
772
773         if ( (mask >> iLocal ) & 0x1 ) 
774         {
775           AliMUONVCalibParam* localMask = 
776               static_cast<AliMUONVCalibParam*>(localMasks.FindObject(localBoardId));
777
778           for (Int_t index = 0; index < 8; ++index) 
779           {
780             maskBuffer[index] = localMask->ValueAsInt(index,0); 
781           }
782
783           fwrite ( maskBuffer, 2, 8, fp); 
784         }
785
786       }
787     }
788
789     fclose(fp);
790
791     return kTRUE;
792
793 }
794
795 //_____________________________________________________________________________
796 void
797 AliMUONTriggerIO::WriteLocalLUT(const AliMUONTriggerLut& lut,
798                                 Int_t localBoardId,
799                                 FILE* flut)
800 {
801   /// loop over the address for the 4-bits lpt and hpt decisions
802
803   const Int_t kMaskYpos = 0x0F;
804   const Int_t kMaskYtri = 0x01;
805   const Int_t kMaskXdev = 0x1F;
806   const Int_t kMaskXpos = 0x1F;
807   
808   for (Int_t i = 0; i < 32768; ++i) 
809   {
810     Int_t lutLpt[2] = { 0 };
811     Int_t lutHpt[2] = { 0 };
812     
813     // decompose address
814     Int_t iYpos =   i                    & kMaskYpos;   
815     Int_t iYtri = ( i >>   4           ) & kMaskYtri;
816     Int_t iXdev = ( i >> ( 4 + 1 )     ) & kMaskXdev;
817     Int_t iXpos = ( i >> ( 4 + 1 + 5 ) ) & kMaskXpos;
818     
819     // iYtri == 1 means no trigger in y-direction
820     if (iYtri == 0) 
821     {
822       lut.GetLutOutput(localBoardId,iXpos,iXdev,iYpos,lutLpt,lutHpt);
823     }
824     
825     UChar_t buffer;
826     
827     // fill byte
828     if (i%2 == 0) 
829     {
830       // upper half-byte
831       buffer = 0;           
832       buffer += lutLpt[1] << 7;
833       buffer += lutLpt[0] << 6;
834       buffer += lutHpt[1] << 5;
835       buffer += lutHpt[0] << 4;
836     } else {
837       // lower half-byte
838       buffer += lutLpt[1] << 3;
839       buffer += lutLpt[0] << 2;
840       buffer += lutHpt[1] << 1;
841       buffer += lutHpt[0] << 0;
842       fwrite(&buffer,1,1,flut);
843     }
844   }
845 }  
846   
847 //_____________________________________________________________________________
848 AliMpTriggerCrate*
849 AliMUONTriggerIO::GetTriggerCrate(TString name, Bool_t warn) const
850 {
851 /// Return trigger crate with given name
852
853   AliMpTriggerCrate* crate
854      = (AliMpTriggerCrate*) fTriggerCrates.GetValue(name.Data());
855     
856   if ( ! crate && warn ) {  
857     AliErrorStream() 
858         << "Trigger crate with name = " << name.Data() << " not defined." << endl;
859   }     
860
861   return crate;
862 }  
863
864 //_____________________________________________________________________________
865 AliMpLocalBoard* 
866 AliMUONTriggerIO::GetLocalBoard(Int_t localBoardId, Bool_t warn) const
867 {
868 /// Return bus patch with given Id
869
870   AliMpLocalBoard* localBoard
871     = (AliMpLocalBoard*) fLocalBoards.GetValue(localBoardId);
872     
873   if ( ! localBoard && warn ) {  
874     AliErrorStream() 
875         << "Local board with Id = " << localBoardId << " not defined." << endl;
876   }     
877
878   return localBoard;
879 }
880
881 //_____________________________________________________________________________
882 void 
883 AliMUONTriggerIO::UpdateMapping(Bool_t writeFile) const
884 {
885 /// check if mapping in database different from current Mtg configuration
886 /// Update mapping in databse and read regional crate file in repository (ext .out
887 /// to avoid overwriting). This case has a low probability to happen.
888
889     // Assuming that crates do not change
890
891     if (!AliMpDDLStore::Instance(kFALSE))
892     {
893       AliMpCDB::LoadDDLStore();
894     }
895
896     Bool_t modified = false;
897
898     TExMapIter itr = AliMpDDLStore::Instance()->GetLocalBoardItr();
899
900     Long_t key, value;
901
902     while(itr.Next(key, value))
903     {
904       AliMpLocalBoard* boardMapping =  reinterpret_cast<AliMpLocalBoard*>(value);
905
906       Int_t localBoardId = boardMapping->GetId();
907       AliMpLocalBoard* board = static_cast<AliMpLocalBoard*>(fLocalBoards.GetValue(localBoardId));
908
909       if (board->GetCrate().CompareTo(boardMapping->GetCrate()) != 0) 
910       {
911         AliWarning(Form("Crate Name different for board %d (%s %s)", localBoardId, boardMapping->GetCrate().Data(), 
912                         board->GetCrate().Data()));
913         boardMapping->SetCrate( board->GetCrate() );
914         modified = true;
915       }
916
917       if ((board->GetSlot()) != boardMapping->GetSlot()) 
918       {
919         AliWarning(Form("Slot different for board %d (%d %d)", localBoardId, boardMapping->GetSlot(), board->GetSlot()+1));
920         boardMapping->SetSlot(board->GetSlot());
921         modified = true;
922       }
923           
924       if (boardMapping->GetSwitch() != board->GetSwitch()) {
925         AliWarning(Form("Switch different for board %d (0x%x 0x%x)", localBoardId, 
926                         boardMapping->GetSwitch(), board->GetSwitch()));
927         boardMapping->SetSwitch(board->GetSwitch());
928         modified = true;
929       }
930     }
931     
932     if (modified) 
933     {
934       AliMpCDB::WriteDDLStore(false);
935       AliWarning("Wrote new version of mapping in databse");
936       if (writeFile) 
937       {
938           TString file = AliMpFiles::LocalTriggerBoardMapping();
939           file += ".out";
940           WriteRegional(file.Data(), 0x0);
941           AliWarning(Form("Wrote regional file %s", file.Data()));
942
943       }
944     }
945
946 }