1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 #include "AliMUONTriggerIO.h"
22 #include "AliMpDDLStore.h"
23 #include "AliMpTriggerCrate.h"
24 #include "AliMUONTriggerLut.h"
25 #include "AliMUONCalibParamNI.h"
26 #include "AliMUONVStore.h"
27 #include <Riostream.h>
30 /// \class AliMUONTriggerIO
32 /// Handles read/write of masks and LUT to/from online files,
33 /// to be used by Shuttle and Trigger DA.
35 /// \author Laurent Aphecetche, Subatech
36 /// \author Bogdan Vulpescu, LPC Clermont-Ferrand
39 ClassImp(AliMUONTriggerIO)
42 //_____________________________________________________________________________
43 AliMUONTriggerIO::AliMUONTriggerIO() :
44 TObject(), fLocalBoardIds(), fNofLocalBoards(0)
49 //_____________________________________________________________________________
50 AliMUONTriggerIO::AliMUONTriggerIO(const char* regionalFileToRead) :
51 TObject(), fLocalBoardIds(), fNofLocalBoards(0)
54 ReadRegional(regionalFileToRead,0);
57 //_____________________________________________________________________________
58 AliMUONTriggerIO::~AliMUONTriggerIO()
63 //_____________________________________________________________________________
65 AliMUONTriggerIO::DeCompAddress(UChar_t &ypos, UChar_t &ytri, UChar_t &xdev, UChar_t &xpos,
66 UShort_t address) const
68 /// decompose the 15-bits address
73 // UChar_t bitsXpos = 5;
75 UShort_t maskYpos = 0x000F; // ...0 00001111
76 UShort_t maskYtri = 0x0001; // ...0 00000001
77 UShort_t maskXdev = 0x001F; // ...0 00011111
78 UShort_t maskXpos = 0x001F; // ...0 00011111
80 ypos = address & maskYpos;
81 ytri = (address >> bitsYpos) & maskYtri;
82 xdev = (address >> (bitsYpos+bitsYtri)) & maskXdev;
83 xpos = (address >> (bitsYpos+bitsYtri+bitsXdev)) & maskXpos;
86 //_____________________________________________________________________________
88 AliMUONTriggerIO::FillLut(AliMUONTriggerLut& lut,
89 Int_t icirc, UChar_t istripX, UChar_t idev,
90 Int_t lutLpt[16][2], Int_t lutHpt[16][2])
92 /// Fill the LUT histograms
94 if (icirc == 0 && istripX == 0 && idev == 0)
96 AliDebug(1,"Copy board, not filled ...");
100 Short_t iLptPlus, iLptMinu, iLptUnde;
101 Short_t iHptPlus, iHptMinu, iHptUnde;
103 iLptPlus = iLptMinu = iLptUnde = 0;
104 iHptPlus = iHptMinu = iHptUnde = 0;
106 for (Int_t istripY=0; istripY<16; istripY++)
108 if (lutLpt[istripY][1] == 0 && lutLpt[istripY][0] ==1)
109 iLptMinu=iLptMinu+(1 << istripY);
110 if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==0)
111 iLptPlus=iLptPlus+(1 << istripY);
112 if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==1)
113 iLptUnde=iLptUnde+(1 << istripY);
115 if (lutHpt[istripY][1] == 0 && lutHpt[istripY][0] ==1)
116 iHptMinu=iHptMinu+(1 << istripY);
117 if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==0)
118 iHptPlus=iHptPlus+(1 << istripY);
119 if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==1)
120 iHptUnde=iHptUnde+(1 << istripY);
124 lut.SetContent("LptMinu",icirc,istripX,idev,iLptMinu);
125 lut.SetContent("LptUnde",icirc,istripX,idev,iLptUnde);
126 lut.SetContent("LptPlus",icirc,istripX,idev,iLptPlus);
128 lut.SetContent("HptMinu",icirc,istripX,idev,iLptMinu);
129 lut.SetContent("HptUnde",icirc,istripX,idev,iLptUnde);
130 lut.SetContent("HptPlus",icirc,istripX,idev,iLptPlus);
133 //_____________________________________________________________________________
135 AliMUONTriggerIO::LocalBoardId(Int_t index) const
137 /// Return the i-th localBoardId, or -1 if index is out of bounds
138 if ( index >= 0 && index < fNofLocalBoards )
140 return fLocalBoardIds[index];
145 //_____________________________________________________________________________
147 AliMUONTriggerIO::ReadLocalMasks(const char* localFile, AliMUONVStore& localMasks) const
149 /// Fills the local masks store from file
151 if ( !NofLocalBoards() )
153 AliError("No local board to read");
157 FILE* fp = fopen(gSystem->ExpandPathName(localFile),"r");
160 AliError(Form("Could not read file %s",localFile));
164 UShort_t maskBuffer[8];
166 Int_t nLocalBoards(0);
168 while ( fread ( maskBuffer, 2, 8, fp ) )
170 Int_t localBoardId = LocalBoardId(nLocalBoards);
171 AliDebug(1,Form("LB %03d X1 %4x X2 %4x X3 %4x X4 %4x "
172 "Y1 %4x Y2 %4x Y3 %4x Y4 %4x",
185 AliMUONVCalibParam* localBoard = new AliMUONCalibParamNI(1,8,localBoardId,0,0);
186 for ( Int_t x = 0; x < 2; ++x )
188 for ( Int_t y = 0; y < 4; ++y )
191 localBoard->SetValueAsInt(index,0,maskBuffer[index]);
194 localMasks.Add(localBoard);
200 if ( nLocalBoards != NofLocalBoards() )
202 AliError(Form("Read %d out of %d local boards",
203 nLocalBoards, NofLocalBoards()));
211 //_____________________________________________________________________________
213 AliMUONTriggerIO::ReadLocalLUT(AliMUONTriggerLut& lut,
217 /// Read the LUT for one local board from an online file
222 UChar_t mask1 = 0xF0;
223 UChar_t mask2 = 0x0F;
224 UChar_t maskLpt = 0x0C;
225 UChar_t maskHpt = 0x03;
226 UChar_t lh, lpt, hpt;
228 UChar_t xpos, xdev, ypos, ytri;
230 Int_t lutLpt[16][2], lutHpt[16][2];
232 Int_t boardnr = localBoardId;
234 AliDebug(1,Form("Reading LUT values for local board %d",boardnr));
238 // create the 32767 addresses for the 4-bits lpt and hpt half-bytes
239 for (UShort_t ilut = 0; ilut < 0x7FFF; ilut += 2)
241 // read two lut addresses at once
242 fread(&buffer,1,1,flut);
244 // 1st 4-bits half-byte
246 lh = (buffer & mask1) >> 4;
248 // Lpt and Hpt response
249 lpt = (lh & maskLpt) >> 2;
252 // decompose the 15-bits address
253 DeCompAddress(ypos,ytri,xdev,xpos,address);
255 // calculate group of y-strips
258 lutLpt[ny][0] = lpt & 1;
259 lutLpt[ny][1] = (lpt & 2) >> 1;
260 lutHpt[ny][0] = hpt & 1;
261 lutHpt[ny][1] = (hpt & 2) >> 1;
266 // ytri == 1 means no trigger in y-direction
269 FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
274 // 2nd 4-bits half-byte
276 lh = (buffer & mask2);
278 // Lpt and Hpt response
279 lpt = (lh & maskLpt) >> 2;
282 // decompose the 15-bits address
283 DeCompAddress(ypos,ytri,xdev,xpos,address);
285 // calculate group of y-strips
288 lutLpt[ny][0] = lpt & 1;
289 lutLpt[ny][1] = (lpt & 2) >> 1;
290 lutHpt[ny][0] = hpt & 1;
291 lutHpt[ny][1] = (hpt & 2) >> 1;
296 // ytri == 1 means no trigger in y-direction
299 FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
306 //_____________________________________________________________________________
308 AliMUONTriggerIO::ReadLUT(const char* lutFileToRead, AliMUONTriggerLut& lut)
310 /// Fill the LUT object from online file
312 if ( !NofLocalBoards() )
314 AliError("No local board id defined. Must read a regional file first");
318 FILE* flut = fopen(gSystem->ExpandPathName(lutFileToRead),"rb");
321 AliError(Form("Could not create output LUT file %s",lutFileToRead));
325 for ( Int_t i = 0; i < NofLocalBoards(); ++i )
327 ReadLocalLUT(lut,LocalBoardId(i),flut);
336 //_____________________________________________________________________________
338 AliMUONTriggerIO::ReadMasks(const char* localFile,
339 const char* regionalFile,
340 const char* /* globalFile */,
341 AliMUONVStore* localMasks,
342 AliMUONVStore* regionalMasks,
343 AliMUONVCalibParam* /* globalMasks */)
345 /// Fill the various masks store from files
349 AliError("Must have a regional file name to proceeed");
353 Int_t nCrates = ReadRegional(regionalFile,regionalMasks);
355 if (!nCrates) return kFALSE;
357 if (localMasks && localFile)
359 Int_t nLocal = ReadLocalMasks(localFile,*localMasks);
360 AliDebug(1,Form("Read masks for %d local boards",nLocal));
366 //_____________________________________________________________________________
368 AliMUONTriggerIO::ReadRegional(const char* regionalFile, AliMUONVStore* regionalMasks)
370 /// Read regional file to fill the regional mask store *AND*
371 /// determine the order in which local boards will appear in local
374 fLocalBoardIds.Reset();
377 std::ifstream in(gSystem->ExpandPathName(regionalFile));
380 AliError(Form("Cannot read file %s",regionalFile));
389 if (!AliMpDDLStore::Instance(kFALSE))
391 AliMpCDB::LoadDDLStore();
398 if (!strlen(name)) break;
400 AliDebug(1,Form("Looking for crate %s",name));
402 AliMpTriggerCrate* triggerCrate = AliMpDDLStore::Instance()->GetTriggerCrate(name);
406 AliError(Form("Mapping error : could not get crate %s",name));
413 Int_t mode, coincidence;
416 sscanf(line,"%hx",&id);
419 sscanf(line,"%d",&mode);
422 sscanf(line,"%d",&coincidence);
425 sscanf(line,"%hx",&mask);
429 AliMUONVCalibParam* regionalBoard = new AliMUONCalibParamNI(1,16,id,0,0);
430 regionalBoard->SetValueAsInt(0,0,mask);
431 regionalMasks->Add(regionalBoard);
432 //FIXME: lines below should not be needed, as regionalMask should be only 1 16 bits word, not 16 16 bits word...
433 for ( Int_t j = 1; j < 16; ++j )
435 regionalBoard->SetValueAsInt(j,0,0x3F);
439 AliDebug(1,Form("Name %s ID %x Mode %d Coin %d Mask %x",
440 name,id,mode,coincidence,mask));
442 for ( Int_t i = 0; i < 16; ++i )
444 if ( (mask >> i ) & 0x1 )
447 char localBoardName[20];
448 int j,localBoardId,switches;
449 sscanf(line,"%02d %s %03d %03x",&j,localBoardName,&localBoardId,&switches);
450 AliDebug(1,Form("%02d %s %03d %03x",j,localBoardName,localBoardId,switches));
451 fLocalBoardIds.Set(fNofLocalBoards+1);
452 fLocalBoardIds[fNofLocalBoards] = localBoardId;
461 //_____________________________________________________________________________
463 AliMUONTriggerIO::WriteLUT(const AliMUONTriggerLut& lut,
464 const char* lutFileToWrite)
466 /// Convert an offline lut into an online (binary) lut file
468 if ( !NofLocalBoards() )
470 AliError("No local board id defined. Must read a regional file first");
474 FILE* flut = fopen(gSystem->ExpandPathName(lutFileToWrite),"wb");
477 AliError(Form("Could not create output LUT file %s",lutFileToWrite));
481 for ( Int_t i = 0; i < NofLocalBoards(); ++i )
483 WriteLocalLUT(lut,LocalBoardId(i),flut);
491 //_____________________________________________________________________________
493 AliMUONTriggerIO::WriteLocalLUT(const AliMUONTriggerLut& lut,
497 /// loop over the address for the 4-bits lpt and hpt decisions
499 const Int_t kMaskYpos = 0x0F;
500 const Int_t kMaskYtri = 0x01;
501 const Int_t kMaskXdev = 0x1F;
502 const Int_t kMaskXpos = 0x1F;
504 for (Int_t i = 0; i < 32768; ++i)
506 Int_t lutLpt[2] = { 0 };
507 Int_t lutHpt[2] = { 0 };
510 Int_t iYpos = i & kMaskYpos;
511 Int_t iYtri = ( i >> 4 ) & kMaskYtri;
512 Int_t iXdev = ( i >> ( 4 + 1 ) ) & kMaskXdev;
513 Int_t iXpos = ( i >> ( 4 + 1 + 5 ) ) & kMaskXpos;
515 // iYtri == 1 means no trigger in y-direction
518 lut.GetLutOutput(localBoardId,iXpos,iXdev,iYpos,lutLpt,lutHpt);
528 buffer += lutLpt[1] << 7;
529 buffer += lutLpt[0] << 6;
530 buffer += lutHpt[1] << 5;
531 buffer += lutHpt[0] << 4;
534 buffer += lutLpt[1] << 3;
535 buffer += lutLpt[0] << 2;
536 buffer += lutHpt[1] << 1;
537 buffer += lutHpt[0] << 0;
538 fwrite(&buffer,1,1,flut);