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"
19 #include "AliMUONTriggerLut.h"
20 #include "AliMUONCalibParamNI.h"
21 #include "AliMUONVStore.h"
24 #include "AliMpHelper.h"
25 #include "AliMpConstants.h"
27 #include "AliMpFiles.h"
28 #include "AliMpDDLStore.h"
29 #include "AliMpLocalBoard.h"
30 #include "AliMpTriggerCrate.h"
31 #include "AliMUONGlobalCrateConfig.h"
32 #include "AliMUONRegionalTriggerConfig.h"
33 #include "AliMUONTriggerCrateConfig.h"
37 #include <Riostream.h>
40 /// \class AliMUONTriggerIO
42 /// Handles read/write of masks and LUT to/from online files,
43 /// to be used by Shuttle and Trigger DA.
45 /// \author Laurent Aphecetche, Christian Finck Subatech
46 /// \author Bogdan Vulpescu, LPC Clermont-Ferrand
49 ClassImp(AliMUONTriggerIO)
53 const UInt_t AliMUONTriggerIO::fgkLocalLutSize = 1 << 14; // 16384
55 //_____________________________________________________________________________
56 AliMUONTriggerIO::AliMUONTriggerIO()
63 //_____________________________________________________________________________
64 AliMUONTriggerIO::AliMUONTriggerIO(const char* regionalFileToRead)
69 ReadRegionalConfig(regionalFileToRead,0);
72 //_____________________________________________________________________________
73 AliMUONTriggerIO::~AliMUONTriggerIO()
78 //_____________________________________________________________________________
80 AliMUONTriggerIO::DeCompAddress(UChar_t &ypos, UChar_t &ytri, UChar_t &xdev, UChar_t &xpos,
81 UShort_t address) const
83 /// decompose the 15-bits address
88 // UChar_t bitsXpos = 5;
90 UShort_t maskYpos = 0x000F; // ...0 00001111
91 UShort_t maskYtri = 0x0001; // ...0 00000001
92 UShort_t maskXdev = 0x001F; // ...0 00011111
93 UShort_t maskXpos = 0x001F; // ...0 00011111
95 ypos = address & maskYpos;
96 ytri = (address >> bitsYpos) & maskYtri;
97 xdev = (address >> (bitsYpos+bitsYtri)) & maskXdev;
98 xpos = (address >> (bitsYpos+bitsYtri+bitsXdev)) & maskXpos;
100 // convert deviation format
101 // online: sign 1bit , dev 4bit
105 // 0 0 mu+, mu- infinite momentum (unde)
111 // - 15 mu+, mu- infinite momentum (unde)
113 Int_t iXdevOff, iXdevOn, iXdev, sign;
119 iXdevOn += iXdev & 0x0F;
120 sign += (iXdev >> 4) & 0x01;
132 iXdevOff = - iXdevOn + 15; // gives range 0-14
134 iXdevOff = + iXdevOn + 15; // gives range 16-30 !
144 //_____________________________________________________________________________
146 AliMUONTriggerIO::FillLut(AliMUONTriggerLut& lut,
147 Int_t icirc, UChar_t istripX, UChar_t idev,
148 Int_t lutLpt[16][2], Int_t lutHpt[16][2])
150 /// Fill the LUT histograms
152 if (icirc == 0 && istripX == 0 && idev == 0)
154 AliDebug(1,"Copy board, not filled ...");
158 Short_t iLptPlus, iLptMinu, iLptUnde;
159 Short_t iHptPlus, iHptMinu, iHptUnde;
161 iLptPlus = iLptMinu = iLptUnde = 0;
162 iHptPlus = iHptMinu = iHptUnde = 0;
164 for (Int_t istripY=0; istripY<16; istripY++)
166 if (lutLpt[istripY][1] == 0 && lutLpt[istripY][0] ==1)
167 iLptMinu=iLptMinu+(1 << istripY);
168 if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==0)
169 iLptPlus=iLptPlus+(1 << istripY);
170 if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==1)
171 iLptUnde=iLptUnde+(1 << istripY);
173 if (lutHpt[istripY][1] == 0 && lutHpt[istripY][0] ==1)
174 iHptMinu=iHptMinu+(1 << istripY);
175 if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==0)
176 iHptPlus=iHptPlus+(1 << istripY);
177 if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==1)
178 iHptUnde=iHptUnde+(1 << istripY);
182 lut.SetContent("LptMinu",icirc,istripX,idev,iLptMinu);
183 lut.SetContent("LptUnde",icirc,istripX,idev,iLptUnde);
184 lut.SetContent("LptPlus",icirc,istripX,idev,iLptPlus);
186 lut.SetContent("HptMinu",icirc,istripX,idev,iHptMinu);
187 lut.SetContent("HptUnde",icirc,istripX,idev,iHptUnde);
188 lut.SetContent("HptPlus",icirc,istripX,idev,iHptPlus);
191 //_____________________________________________________________________________
193 AliMUONTriggerIO::ReadLocalMasks(const char* localFile, AliMUONVStore& localMasks) const
195 /// Fills the local masks store from file
197 if ( !NofLocalBoards() )
199 AliError("No local board to read");
203 FILE* fp = fopen(gSystem->ExpandPathName(localFile),"r");
206 AliError(Form("Could not read file %s",localFile));
210 UShort_t maskBuffer[8];
212 Int_t localBoardIndex(0);
214 while ( fread ( maskBuffer, 2, 8, fp ) )
216 Int_t localBoardId = fRegionalTrigger.LocalBoardId(localBoardIndex);
217 AliDebug(1,Form("LB %03d X1 %4x X2 %4x X3 %4x X4 %4x "
218 "Y1 %4x Y2 %4x Y3 %4x Y4 %4x",
229 if ( localBoardId > 0 )
231 AliMUONVCalibParam* localBoard = new AliMUONCalibParamNI(1,8,localBoardId,0,0);
232 for ( Int_t index = 0; index < 8; ++index )
234 localBoard->SetValueAsInt(index,0,maskBuffer[index]);
236 localMasks.Add(localBoard);
240 AliError(Form("Oups. Got localBoardId=%d for index=%d",localBoardId,localBoardIndex));
246 if ( localBoardIndex != NofLocalBoards() )
248 AliError(Form("Read %d out of %d local boards",
249 localBoardIndex, NofLocalBoards()));
254 return localBoardIndex+1;
257 //_____________________________________________________________________________
259 AliMUONTriggerIO::ReadLocalLUT(AliMUONTriggerLut& lut,
263 /// Read the LUT for one local board from an online file
267 UChar_t buffer[fgkLocalLutSize]; // 32768 hpt/lpt addresses divided by two
268 UChar_t mask1 = 0xF0;
269 UChar_t mask2 = 0x0F;
270 UChar_t maskHpt = 0x0C;
271 UChar_t maskLpt = 0x03;
272 UChar_t lh, lpt, hpt;
274 UChar_t xpos, xdev, ypos, ytri;
276 Int_t lutLpt[16][2], lutHpt[16][2];
278 Int_t boardnr = localBoardId;
280 AliDebug(1,Form("Reading LUT values for local board %d",boardnr));
283 Bool_t trigx = kFALSE;
285 // read two lut addresses at once, 32768/2=16384 times
286 fread(buffer,fgkLocalLutSize,1,flut);
288 // create the 32767 addresses for the 4-bits lpt and hpt half-bytes
289 for (UShort_t ilut = 0; ilut < fgkLocalLutSize*2; ilut += 2)
292 // 1st 4-bits half-byte
294 lh = (buffer[ilut/2] & mask1) >> 4;
296 // Lpt and Hpt response
297 hpt = (lh & maskHpt) >> 2;
300 // decompose the 15-bits address
301 trigx = DeCompAddress(ypos,ytri,xdev,xpos,address);
303 // calculate group of y-strips
304 if (trigx && (ny < 16))
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;
314 // ytri == 1 means no trigger in y-direction
317 FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
322 // 2nd 4-bits half-byte
324 lh = (buffer[ilut/2] & mask2);
326 // Lpt and Hpt response
327 hpt = (lh & maskHpt) >> 2;
330 // decompose the 15-bits address
331 trigx = DeCompAddress(ypos,ytri,xdev,xpos,address);
333 // calculate group of y-strips
334 if (trigx && (ny < 16))
336 lutLpt[ny][0] = lpt & 1;
337 lutLpt[ny][1] = (lpt & 2) >> 1;
338 lutHpt[ny][0] = hpt & 1;
339 lutHpt[ny][1] = (hpt & 2) >> 1;
344 // ytri == 1 means no trigger in y-direction
347 FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
354 //_____________________________________________________________________________
356 AliMUONTriggerIO::ReadLUT(const char* lutFileToRead, AliMUONTriggerLut& lut)
358 /// Fill the LUT object from online file
360 if ( !NofLocalBoards() )
362 AliError("No local board id defined. Must read a regional file first");
366 FILE* flut = fopen(gSystem->ExpandPathName(lutFileToRead),"rb");
369 AliError(Form("Could not read LUT file %s",lutFileToRead));
373 for ( Int_t i = 0; i < NofLocalBoards(); ++i )
375 ReadLocalLUT(lut,fRegionalTrigger.LocalBoardId(i),flut);
384 //_____________________________________________________________________________
386 AliMUONTriggerIO::ReadConfig(const char* localFile,
387 const char* regionalFile,
388 const char* globalFile,
389 AliMUONVStore* localMasks,
390 AliMUONRegionalTriggerConfig* regionalConfig,
391 AliMUONGlobalCrateConfig* globalConfig)
393 /// Fill the various masks store from files
395 if ( !regionalFile || strlen(regionalFile)==0 )
397 AliError("Must have a regional file name to proceeed");
401 AliDebug(1,Form("regionalConfig=%p",regionalConfig));
403 Int_t nCrates = ReadRegionalConfig(regionalFile, regionalConfig);
407 AliError("nCrates=0 !");
411 if (localMasks && localFile && strlen(localFile) > 0 )
413 Int_t nLocal = ReadLocalMasks(localFile,*localMasks);
414 AliDebug(1,Form("Read masks for %d local boards",nLocal));
417 Int_t nDarc = ReadGlobalConfig(globalFile, globalConfig);
418 AliDebug(1,Form("Read config for %d DARC boards",nDarc));
420 if (!nDarc) return kFALSE;
427 //_____________________________________________________________________________
429 AliMUONTriggerIO::ReadGlobalConfig(const char* globalFile, AliMUONGlobalCrateConfig* globalConfig) const
431 /// read the global crate file
432 /// the masks are disable bit for each crate, 8 per darc board
433 /// bit value 0 means enable, 1 means disable *
436 if ( !(nDarc = globalConfig->ReadData(globalFile)) ) return 0;
441 //_____________________________________________________________________________
443 AliMUONTriggerIO::ReadRegionalConfig(const char* regionalFile, AliMUONRegionalTriggerConfig* regionalConfig)
445 /// Read regional file to fill
447 AliDebug(1,Form("regionalConfig=%p",regionalConfig));
450 if ( !(nCrates = regionalConfig->ReadData(regionalFile)) ) return 0;
452 // read the mapping file also
453 if ( ! fRegionalTrigger.ReadData(regionalFile) ) return 0;
459 //_____________________________________________________________________________
461 AliMUONTriggerIO::WriteLUT(const AliMUONTriggerLut& lut,
462 const char* lutFileToWrite)
464 /// Convert an offline lut into an online (binary) lut file
466 if ( !NofLocalBoards() )
468 AliError("No local board id defined. Must read a regional file first");
472 FILE* flut = fopen(gSystem->ExpandPathName(lutFileToWrite),"wb");
475 AliError(Form("Could not create output LUT file %s",lutFileToWrite));
479 for ( Int_t i = 0; i < NofLocalBoards(); ++i )
481 WriteLocalLUT(lut,fRegionalTrigger.LocalBoardId(i),flut);
490 //_____________________________________________________________________________
492 AliMUONTriggerIO::WriteConfig(const char* localFile,
493 const char* regionalFile,
494 const char* globalFile,
495 AliMUONVStore* localMasks,
496 AliMUONRegionalTriggerConfig* regionalConfig,
497 AliMUONGlobalCrateConfig* globalConfig) const
499 /// write config files
502 ok = WriteLocalMasks(localFile, *localMasks, regionalConfig);
503 ok &= WriteRegionalConfig(regionalFile, regionalConfig);
504 ok &= WriteGlobalConfig(globalFile, globalConfig);
512 //_____________________________________________________________________________
514 AliMUONTriggerIO::WriteGlobalConfig(const char* globalFile, AliMUONGlobalCrateConfig* globalConfig) const
516 /// write global config
521 out.open(globalFile);
524 AliError(Form("Could not create output global file %s", globalFile));
528 out << globalConfig->GetName() << endl;
529 out << Form("0x%x",globalConfig->GetGlobalCrateEnable()) << endl;
532 out << globalConfig->GetJtagName() << endl;
533 out << Form("0x%08x", globalConfig->GetJtagVmeAddr()) << endl;
534 out << Form("%d %d %d", globalConfig->GetJtagClockDiv(),
535 globalConfig->GetJtagRxPhase(), globalConfig->GetJtagRdDelay()) << endl;
537 for (Int_t i = 0; i < globalConfig->GetJtagNofLines(); ++i)
538 out << Form("%d ", globalConfig->GetEnableJtag(i));
542 for (Int_t i = 0; i < globalConfig->GetJtagNofLines(); ++i)
545 for (Int_t j = 0; j < globalConfig->GetJtagNofLines(); ++j)
546 out << Form(" %s", globalConfig->GetJtagCrateName(i,j).Data()) << endl;
550 out << globalConfig->GetFirstDarcName() << endl;
551 out << Form("0x%08x", globalConfig->GetFirstDarcVmeAddr()) << endl;
552 out << globalConfig->GetFirstDarcType() << endl;
553 disable = globalConfig->GetFirstDarcDisable();
554 out << Form("0x%02x", disable) << endl;
555 out << Form("0x%x", globalConfig->GetFirstDarcL0Delay()) << endl;
556 out << Form("0x%x", globalConfig->GetFirstDarcL1TimeOut()) << endl;
557 out << Form("0x%x", globalConfig->GetFirstDarcGlobalL0()) << endl;
558 out << Form("0x%x", globalConfig->GetFirstDarcConfig()) << endl;
561 out << globalConfig->GetSecondDarcName() << endl;
562 out << Form("0x%08x", globalConfig->GetSecondDarcVmeAddr()) << endl;
563 out << globalConfig->GetSecondDarcType() << endl;
564 disable = globalConfig->GetSecondDarcDisable();
565 out << Form("0x%02x", disable) << endl;
566 out << Form("0x%x", globalConfig->GetSecondDarcL0Delay()) << endl;
567 out << Form("0x%x", globalConfig->GetSecondDarcL1TimeOut()) << endl;
568 out << Form("0x%x", globalConfig->GetSecondDarcGlobalL0()) << endl;
569 out << Form("0x%x", globalConfig->GetSecondDarcConfig()) << endl;
572 out << globalConfig->GetGlobalName() << endl;
573 out << Form("0x%08x", globalConfig->GetGlobalVmeAddr()) << endl;
574 for (Int_t i = 0; i < globalConfig->GetGlobalNofRegisters(); ++i)
575 out << Form("0x%x", globalConfig->GetGlobalRegister(i)) << endl;
578 out << globalConfig->GetFetName() << endl;
579 out << Form("0x%08x", globalConfig->GetFetVmeAddr()) << endl;
580 for (Int_t i = 0; i < globalConfig->GetFetNofRegisters(); ++i)
581 out << Form("0x%x", globalConfig->GetFetRegister(i)) << endl;
586 //_____________________________________________________________________________
588 AliMUONTriggerIO::WriteRegionalConfig(const char* regionalFile, AliMUONRegionalTriggerConfig* regionalConfig) const
591 /// write regional mask with the current configuration
592 /// if regional masks not defined, take the one from current configuration
595 out.open(regionalFile);
599 AliError(Form("Could not create output regional file %s",regionalFile));
603 Int_t nCrate = fRegionalTrigger.GetNofTriggerCrates();
606 AliError("Could not write regional no configuration in memory");
612 AliMpTriggerCrate* crate;
613 for (Int_t ddlId = 0; ddlId < 2; ddlId++) // right & left side
615 for (Int_t crateId = 0; crateId < 8; crateId++) // 8 crates/regional boards for each side.
618 name = AliMpTriggerCrate::GenerateName(crateId, ddlId, nofDDLs);
620 crate = fRegionalTrigger.FindTriggerCrate(name, false);
622 AliMUONTriggerCrateConfig* crateConfig = regionalConfig->FindTriggerCrate(crate->GetName());
625 AliError(Form("Cannot find crate %s in CDB", crate->GetName()));
629 out << crate->GetName() << endl;
630 out << Form("%02x", crate->GetId()) << endl;
631 out << crateConfig->GetMode() << endl;
632 out << crateConfig->GetCoinc() << endl;
633 out << Form("%04x", crateConfig->GetMask()) << endl;
634 out << Form("%02d",crate->GetNofLocalBoards()) << endl;
636 for (Int_t iLocal = 0; iLocal < crate->GetNofLocalBoards(); ++iLocal)
638 Int_t localBoardId = crate->GetLocalBoardId(iLocal);
640 AliMpLocalBoard* board = fRegionalTrigger.FindLocalBoard(localBoardId);
642 out << Form("%02d ", board->GetSlot())
644 << Form(" %03d ", localBoardId)
645 << Form("%03x", board->GetSwitch())
650 if (board->IsNotified()) {
651 for (Int_t i = 0; i < board->GetNofDEs(); ++i)
652 out << Form("%4d ", board->GetDEId(i));
654 out << Form("%4d ", 0);
658 // print copy card numbers & TC
659 out << Form(" %4d %4d", board->GetInputXfrom(), board->GetInputXto());
660 out << Form(" %4d %4d", board->GetInputYfrom(), board->GetInputYto());
661 out << Form(" %4d", board->GetTC()) << endl;
672 //_____________________________________________________________________________
674 AliMUONTriggerIO::WriteLocalMasks(const char* localFile, AliMUONVStore& localMasks, AliMUONRegionalTriggerConfig* regionalConfig) const
677 /// removing/adding enable for a local board need a update of the configuration
678 /// before calling this method
679 /// mask are written for all boards including the copy card (Ch.F.)
681 FILE* fp = fopen(gSystem->ExpandPathName(localFile),"wb");
684 AliError(Form("Could not create output local file %s",localFile));
688 UShort_t maskBuffer[8];
689 Int_t localBoardIndex(0);
690 while (localBoardIndex < NofLocalBoards()) {
692 Int_t localBoardId = fRegionalTrigger.LocalBoardId(localBoardIndex);
694 AliMUONVCalibParam* localMask =
695 static_cast<AliMUONVCalibParam*>(localMasks.FindObject(localBoardId));
697 for (Int_t index = 0; index < 8; ++index)
699 maskBuffer[index] = localMask->ValueAsInt(index,0);
702 fwrite ( maskBuffer, 2, 8, fp);
714 //_____________________________________________________________________________
716 AliMUONTriggerIO::WriteLocalLUT(const AliMUONTriggerLut& lut,
720 /// loop over the address for the 4-bits lpt and hpt decisions
722 const Int_t kMaskYpos = 0x0F;
723 const Int_t kMaskYtri = 0x01;
724 const Int_t kMaskXdev = 0x1F;
725 const Int_t kMaskXpos = 0x1F;
727 UChar_t buffer[fgkLocalLutSize]; // 32768 hpt/lpt addresses divided by two
730 for (UInt_t i = 0; i < fgkLocalLutSize*2; ++i)
732 Int_t lutLpt[2] = { 0 };
733 Int_t lutHpt[2] = { 0 };
736 Int_t iYpos = i & kMaskYpos;
737 Int_t iYtri = ( i >> 4 ) & kMaskYtri;
738 Int_t iXdev = ( i >> ( 4 + 1 ) ) & kMaskXdev;
739 Int_t iXpos = ( i >> ( 4 + 1 + 5 ) ) & kMaskXpos;
741 // convert deviation format
742 // online: sign 1bit , dev 4bit
746 // 0 0 mu+, mu- infinite momentum (unde)
752 // - 15 mu+, mu- infinite momentum (unde)
756 Bool_t trigx = kFALSE;
757 iXdevOn += iXdev & 0x0F;
758 sign += (iXdev >> 4) & 0x01;
770 iXdevOff = - iXdevOn + 15; // gives range 0-14
772 iXdevOff = + iXdevOn + 15; // gives range 16-30 !
777 // iYtri == 1 means no trigger in y-direction
778 if (iYtri == 0 && trigx)
780 lut.GetLutOutput(localBoardId,iXpos,iXdev,iYpos,lutLpt,lutHpt);
788 buffer[bc] += lutHpt[1] << 7;
789 buffer[bc] += lutHpt[0] << 6;
790 buffer[bc] += lutLpt[1] << 5;
791 buffer[bc] += lutLpt[0] << 4;
794 buffer[bc] += lutHpt[1] << 3;
795 buffer[bc] += lutHpt[0] << 2;
796 buffer[bc] += lutLpt[1] << 1;
797 buffer[bc] += lutLpt[0] << 0;
801 fwrite(&buffer,bc,1,flut);
804 //_____________________________________________________________________________
806 AliMUONTriggerIO::LocalBoardId(Int_t index) const
808 /// Return the i-th localBoardId, or -1 if index is out of bounds
810 return fRegionalTrigger.LocalBoardId(index);
814 //______________________________________________________________________________
816 Int_t AliMUONTriggerIO::LocalBoardId(Int_t ddlId, Int_t crateId, Int_t localId) const
818 /// Return local board id from crate and local indexes.
821 TString name = AliMpTriggerCrate::GenerateName(crateId, ddlId, nofDDLs);
823 AliMpTriggerCrate* crate = fRegionalTrigger.FindTriggerCrate(name, false);
824 return crate->GetLocalBoardId(localId);