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"
34 #include <Riostream.h>
37 /// \class AliMUONTriggerIO
39 /// Handles read/write of masks and LUT to/from online files,
40 /// to be used by Shuttle and Trigger DA.
42 /// \author Laurent Aphecetche, Christian Finck Subatech
43 /// \author Bogdan Vulpescu, LPC Clermont-Ferrand
46 ClassImp(AliMUONTriggerIO)
49 //_____________________________________________________________________________
50 AliMUONTriggerIO::AliMUONTriggerIO()
58 //_____________________________________________________________________________
59 AliMUONTriggerIO::AliMUONTriggerIO(const char* regionalFileToRead)
65 ReadRegional(regionalFileToRead,0);
68 //_____________________________________________________________________________
69 AliMUONTriggerIO::~AliMUONTriggerIO()
74 //_____________________________________________________________________________
76 AliMUONTriggerIO::DeCompAddress(UChar_t &ypos, UChar_t &ytri, UChar_t &xdev, UChar_t &xpos,
77 UShort_t address) const
79 /// decompose the 15-bits address
84 // UChar_t bitsXpos = 5;
86 UShort_t maskYpos = 0x000F; // ...0 00001111
87 UShort_t maskYtri = 0x0001; // ...0 00000001
88 UShort_t maskXdev = 0x001F; // ...0 00011111
89 UShort_t maskXpos = 0x001F; // ...0 00011111
91 ypos = address & maskYpos;
92 ytri = (address >> bitsYpos) & maskYtri;
93 xdev = (address >> (bitsYpos+bitsYtri)) & maskXdev;
94 xpos = (address >> (bitsYpos+bitsYtri+bitsXdev)) & maskXpos;
96 // convert deviation format
97 // online: sign 1bit , dev 4bit
101 // 0 0 mu+, mu- infinite momentum (unde)
107 // - 15 mu+, mu- infinite momentum (unde)
109 Int_t iXdevOff, iXdevOn, iXdev, sign;
115 iXdevOn += iXdev & 0x0F;
116 sign += (iXdev >> 4) & 0x01;
128 iXdevOff = - iXdevOn + 15; // gives range 0-14
130 iXdevOff = + iXdevOn + 15; // gives range 16-30 !
140 //_____________________________________________________________________________
142 AliMUONTriggerIO::FillLut(AliMUONTriggerLut& lut,
143 Int_t icirc, UChar_t istripX, UChar_t idev,
144 Int_t lutLpt[16][2], Int_t lutHpt[16][2])
146 /// Fill the LUT histograms
148 if (icirc == 0 && istripX == 0 && idev == 0)
150 AliDebug(1,"Copy board, not filled ...");
154 Short_t iLptPlus, iLptMinu, iLptUnde;
155 Short_t iHptPlus, iHptMinu, iHptUnde;
157 iLptPlus = iLptMinu = iLptUnde = 0;
158 iHptPlus = iHptMinu = iHptUnde = 0;
160 for (Int_t istripY=0; istripY<16; istripY++)
162 if (lutLpt[istripY][1] == 0 && lutLpt[istripY][0] ==1)
163 iLptMinu=iLptMinu+(1 << istripY);
164 if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==0)
165 iLptPlus=iLptPlus+(1 << istripY);
166 if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==1)
167 iLptUnde=iLptUnde+(1 << istripY);
169 if (lutHpt[istripY][1] == 0 && lutHpt[istripY][0] ==1)
170 iHptMinu=iHptMinu+(1 << istripY);
171 if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==0)
172 iHptPlus=iHptPlus+(1 << istripY);
173 if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==1)
174 iHptUnde=iHptUnde+(1 << istripY);
178 lut.SetContent("LptMinu",icirc,istripX,idev,iLptMinu);
179 lut.SetContent("LptUnde",icirc,istripX,idev,iLptUnde);
180 lut.SetContent("LptPlus",icirc,istripX,idev,iLptPlus);
182 lut.SetContent("HptMinu",icirc,istripX,idev,iHptMinu);
183 lut.SetContent("HptUnde",icirc,istripX,idev,iHptUnde);
184 lut.SetContent("HptPlus",icirc,istripX,idev,iHptPlus);
187 //_____________________________________________________________________________
189 AliMUONTriggerIO::ReadLocalMasks(const char* localFile, AliMUONVStore& localMasks) const
191 /// Fills the local masks store from file
193 if ( !NofLocalBoards() )
195 AliError("No local board to read");
199 FILE* fp = fopen(gSystem->ExpandPathName(localFile),"r");
202 AliError(Form("Could not read file %s",localFile));
206 UShort_t maskBuffer[8];
208 Int_t nLocalBoards(0);
210 while ( fread ( maskBuffer, 2, 8, fp ) )
212 Int_t localBoardId = LocalBoardId(nLocalBoards);
213 AliDebug(1,Form("LB %03d X1 %4x X2 %4x X3 %4x X4 %4x "
214 "Y1 %4x Y2 %4x Y3 %4x Y4 %4x",
227 AliMUONVCalibParam* localBoard = new AliMUONCalibParamNI(1,8,localBoardId,0,0);
228 for ( Int_t index = 0; index < 8; ++index )
230 localBoard->SetValueAsInt(index,0,maskBuffer[index]);
232 localMasks.Add(localBoard);
238 if ( nLocalBoards != NofLocalBoards() )
240 AliError(Form("Read %d out of %d local boards",
241 nLocalBoards, NofLocalBoards()));
249 //_____________________________________________________________________________
251 AliMUONTriggerIO::ReadLocalLUT(AliMUONTriggerLut& lut,
255 /// Read the LUT for one local board from an online file
260 UChar_t mask1 = 0xF0;
261 UChar_t mask2 = 0x0F;
262 UChar_t maskHpt = 0x0C;
263 UChar_t maskLpt = 0x03;
264 UChar_t lh, lpt, hpt;
266 UChar_t xpos, xdev, ypos, ytri;
268 Int_t lutLpt[16][2], lutHpt[16][2];
270 Int_t boardnr = localBoardId;
272 AliDebug(1,Form("Reading LUT values for local board %d",boardnr));
275 Bool_t trigx = kFALSE;
277 // create the 32767 addresses for the 4-bits lpt and hpt half-bytes
278 for (UShort_t ilut = 0; ilut < 0x7FFF; ilut += 2)
280 // read two lut addresses at once
281 fread(&buffer,1,1,flut);
283 // 1st 4-bits half-byte
285 lh = (buffer & mask1) >> 4;
287 // Lpt and Hpt response
288 hpt = (lh & maskHpt) >> 2;
291 // decompose the 15-bits address
292 trigx = DeCompAddress(ypos,ytri,xdev,xpos,address);
294 // calculate group of y-strips
295 if (trigx && (ny < 16))
297 lutLpt[ny][0] = lpt & 1;
298 lutLpt[ny][1] = (lpt & 2) >> 1;
299 lutHpt[ny][0] = hpt & 1;
300 lutHpt[ny][1] = (hpt & 2) >> 1;
305 // ytri == 1 means no trigger in y-direction
308 FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
313 // 2nd 4-bits half-byte
315 lh = (buffer & mask2);
317 // Lpt and Hpt response
318 hpt = (lh & maskHpt) >> 2;
321 // decompose the 15-bits address
322 trigx = DeCompAddress(ypos,ytri,xdev,xpos,address);
324 // calculate group of y-strips
325 if (trigx && (ny < 16))
327 lutLpt[ny][0] = lpt & 1;
328 lutLpt[ny][1] = (lpt & 2) >> 1;
329 lutHpt[ny][0] = hpt & 1;
330 lutHpt[ny][1] = (hpt & 2) >> 1;
335 // ytri == 1 means no trigger in y-direction
338 FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
345 //_____________________________________________________________________________
347 AliMUONTriggerIO::ReadLUT(const char* lutFileToRead, AliMUONTriggerLut& lut)
349 /// Fill the LUT object from online file
351 if ( !NofLocalBoards() )
353 AliError("No local board id defined. Must read a regional file first");
357 FILE* flut = fopen(gSystem->ExpandPathName(lutFileToRead),"rb");
360 AliError(Form("Could not read LUT file %s",lutFileToRead));
364 for ( Int_t i = 0; i < NofLocalBoards(); ++i )
366 ReadLocalLUT(lut,LocalBoardId(i),flut);
375 //_____________________________________________________________________________
377 AliMUONTriggerIO::ReadMasks(const char* localFile,
378 const char* regionalFile,
379 const char* globalFile,
380 AliMUONVStore* localMasks,
381 AliMUONVStore* regionalMasks,
382 AliMUONVCalibParam* globalMasks,
385 /// Fill the various masks store from files
389 AliError("Must have a regional file name to proceeed");
393 Int_t nCrates = ReadRegional(regionalFile,regionalMasks, warn);
395 if (!nCrates) return kFALSE;
397 if (localMasks && localFile)
399 Int_t nLocal = ReadLocalMasks(localFile,*localMasks);
400 AliDebug(1,Form("Read masks for %d local boards",nLocal));
403 Int_t nDarc = ReadGlobal(globalFile, globalMasks);
404 AliDebug(1,Form("Read disable for %d DARC boards",nDarc));
406 if (!nDarc) return kFALSE;
411 //_____________________________________________________________________________
413 AliMUONTriggerIO::ReadGlobal(const char* globalFile, AliMUONVCalibParam* globalMasks)
415 /// read the global crate file and file corresponding mask
416 /// the masks are disable bit for each crate, 8 per darc board
417 /// bit value 0 means enable, 1 means disable *
420 if ( ! fGlobalCrate.ReadData(globalFile) ) return 0;
422 UChar_t mask = fGlobalCrate.GetFirstDarcDisable();
423 ULong_t vmeAddr = fGlobalCrate.GetFirstDarcVmeAddr();
424 if (vmeAddr) nDarc++;
425 globalMasks->SetValueAsInt(0,0,mask);
427 mask = fGlobalCrate.GetSecondDarcDisable();
428 vmeAddr = fGlobalCrate.GetSecondDarcVmeAddr();
429 if (vmeAddr) nDarc++;
430 globalMasks->SetValueAsInt(1,0,mask);
435 //_____________________________________________________________________________
437 AliMUONTriggerIO::ReadRegional(const char* regionalFile, AliMUONVStore* regionalMasks, Bool_t warn)
439 /// Read regional file to fill the regional mask store *AND*
440 /// determine the order in which local boards will appear in local
443 if ( ! fRegionalTrigger.ReadData(regionalFile) ) return 0;
447 for (Int_t iCrate = 0; iCrate < fRegionalTrigger.GetNofTriggerCrates(); ++iCrate)
450 AliMpTriggerCrate* crate = fRegionalTrigger.GetTriggerCrateFast(iCrate);
454 AliMpTriggerCrate* triggerCrate = AliMpDDLStore::Instance()->GetTriggerCrate(crate->GetName());
458 AliError(Form("Mapping error : could not get crate %s", crate->GetName()));
465 if (regionalMasks != 0x0)
467 masks = crate->GetMask();
469 AliMUONVCalibParam* regionalBoard = new AliMUONCalibParamNI(1, 1, crate->GetId(), 0, 0);
470 regionalBoard->SetValueAsInt(0, 0, masks);
471 regionalMasks->Add(regionalBoard);
478 //_____________________________________________________________________________
480 AliMUONTriggerIO::WriteLUT(const AliMUONTriggerLut& lut,
481 const char* lutFileToWrite)
483 /// Convert an offline lut into an online (binary) lut file
485 if ( !NofLocalBoards() )
487 AliError("No local board id defined. Must read a regional file first");
491 FILE* flut = fopen(gSystem->ExpandPathName(lutFileToWrite),"wb");
494 AliError(Form("Could not create output LUT file %s",lutFileToWrite));
498 for ( Int_t i = 0; i < NofLocalBoards(); ++i )
500 WriteLocalLUT(lut,LocalBoardId(i),flut);
508 //_____________________________________________________________________________
510 AliMUONTriggerIO::WriteMasks(const char* localFile,
511 const char* regionalFile,
512 const char* globalFile,
513 AliMUONVStore* localMasks,
514 AliMUONVStore* regionalMasks,
515 AliMUONVCalibParam* globalMasks) const
519 ok = WriteLocalMasks(localFile, *localMasks);
520 ok &= WriteRegional(regionalFile, regionalMasks);
521 ok &= WriteGlobal(globalFile, globalMasks);
526 //_____________________________________________________________________________
528 AliMUONTriggerIO::WriteGlobal(const char* globalFile, AliMUONVCalibParam* globalMasks) const
530 /// write global file
531 /// if no global masks defined take the one of configuration
536 out.open(globalFile);
539 AliError(Form("Could not create output regional file %s", globalFile));
543 out << fGlobalCrate.GetName() << endl;
546 out << fGlobalCrate.GetJtagName() << endl;
547 out << Form("0x%08x", fGlobalCrate.GetJtagVmeAddr()) << endl;
548 out << Form("%d %d %d", fGlobalCrate.GetJtagClockDiv(),
549 fGlobalCrate.GetJtagRxPhase(), fGlobalCrate.GetJtagRdDelay()) << endl;
551 for (Int_t i = 0; i < fGlobalCrate.GetJtagNofLines(); ++i)
552 out << Form("%d ", fGlobalCrate.GetEnableJtag(i));
556 for (Int_t i = 0; i < fGlobalCrate.GetJtagNofLines(); ++i)
559 for (Int_t j = 0; j < fGlobalCrate.GetJtagNofLines(); ++j)
560 out << Form(" %s", fGlobalCrate.GetJtagCrateName(i,j).Data()) << endl;
564 out << fGlobalCrate.GetFirstDarcName() << endl;
565 out << Form("0x%08x", fGlobalCrate.GetFirstDarcVmeAddr()) << endl;
566 out << fGlobalCrate.GetFirstDarcType() << endl;
567 if (globalMasks != 0x0)
568 disable = globalMasks->ValueAsInt(0);
570 disable = fGlobalCrate.GetFirstDarcDisable();
571 out << Form("0x%02x", disable) << endl;
572 out << Form("0x%x", fGlobalCrate.GetFirstDarcL0Delay()) << endl;
573 out << Form("0x%x", fGlobalCrate.GetFirstDarcL1TimeOut()) << endl;
576 out << fGlobalCrate.GetSecondDarcName() << endl;
577 out << Form("0x%08x", fGlobalCrate.GetSecondDarcVmeAddr()) << endl;
578 out << fGlobalCrate.GetSecondDarcType() << endl;
579 if (globalMasks != 0x0)
580 disable = globalMasks->ValueAsInt(1);
582 disable = fGlobalCrate.GetSecondDarcDisable();
583 out << Form("0x%02x", disable) << endl;
584 out << Form("0x%x", fGlobalCrate.GetSecondDarcL0Delay()) << endl;
585 out << Form("0x%x", fGlobalCrate.GetSecondDarcL1TimeOut()) << endl;
588 out << fGlobalCrate.GetGlobalName() << endl;
589 out << Form("0x%08x", fGlobalCrate.GetGlobalVmeAddr()) << endl;
590 for (Int_t i = 0; i < fGlobalCrate.GetGlobalNofRegisters(); ++i)
591 out << Form("0x%x", fGlobalCrate.GetGlobalRegister(i)) << endl;
594 out << fGlobalCrate.GetFetName() << endl;
595 out << Form("0x%08x", fGlobalCrate.GetFetVmeAddr()) << endl;
596 for (Int_t i = 0; i < fGlobalCrate.GetFetNofRegisters(); ++i)
597 out << Form("0x%x", fGlobalCrate.GetFetRegister(i)) << endl;
603 //_____________________________________________________________________________
605 AliMUONTriggerIO::WriteRegional(const char* regionalFile, AliMUONVStore* regionalMasks) const
608 /// write regional mask with the current configuration
609 /// if regional masks not defined, take the one from current configuration
612 out.open(regionalFile);
616 AliError(Form("Could not create output regional file %s",regionalFile));
620 for (Int_t iCrate = 0; iCrate < fRegionalTrigger.GetNofTriggerCrates(); ++iCrate)
622 AliMpTriggerCrate* crate = fRegionalTrigger.GetTriggerCrateFast(iCrate);
624 out << crate->GetName() << endl;
625 out << Form("%02x", crate->GetId()) << endl;
626 out << crate->GetMode() << endl;
627 out << crate->GetCoinc() << endl;
630 if (regionalMasks != 0x0)
632 AliMUONVCalibParam* maskParam =
633 static_cast<AliMUONVCalibParam*>(regionalMasks->FindObject(crate->GetId()));
634 masks = maskParam->ValueAsInt(0,0);
638 masks = crate->GetMask();
641 out << Form("%04x", masks) << endl;
643 for (Int_t iLocal = 0; iLocal < crate->GetNofLocalBoards(); ++iLocal)
645 Int_t localBoardId = crate->GetLocalBoardId(iLocal);
647 AliMpLocalBoard* board = fRegionalTrigger.FindLocalBoard(localBoardId);
649 out << Form("%02d ", board->GetSlot())
651 << Form(" %03d ", localBoardId)
652 << Form("%03x", board->GetSwitch())
656 for (Int_t i = 0; i < board->GetNofDEs(); ++i)
657 out << Form("%4d ", board->GetDEId(i));
660 // print copy card numbers
661 out << Form(" %4d %4d", board->GetInputXfrom(), board->GetInputXto());
662 out << Form(" %4d %4d", board->GetInputYfrom(), board->GetInputYto());
663 out << Form(" %4d", board->GetTC()) << endl;
671 //_____________________________________________________________________________
673 AliMUONTriggerIO::WriteLocalMasks(const char* localFile, AliMUONVStore& localMasks) const
676 /// removing/adding enable for a local board need a update of the configuration
677 /// before calling this method
678 /// mask are written for all boards including the copy card (Ch.F.)
680 FILE* fp = fopen(gSystem->ExpandPathName(localFile),"wb");
683 AliError(Form("Could not create output local file %s",localFile));
687 UShort_t maskBuffer[8];
689 for (Int_t iCrate = 0; iCrate < fRegionalTrigger.GetNofTriggerCrates(); ++iCrate)
691 AliMpTriggerCrate* crate = fRegionalTrigger.GetTriggerCrateFast(iCrate);
693 UShort_t mask = crate->GetMask(); // getting mask from current config
695 for (Int_t iLocal = 0; iLocal < crate->GetNofLocalBoards(); ++iLocal)
697 Int_t localBoardId = crate->GetLocalBoardId(iLocal);
699 if ( (mask >> iLocal ) & 0x1 )
701 AliMUONVCalibParam* localMask =
702 static_cast<AliMUONVCalibParam*>(localMasks.FindObject(localBoardId));
704 for (Int_t index = 0; index < 8; ++index)
706 maskBuffer[index] = localMask->ValueAsInt(index,0);
709 fwrite ( maskBuffer, 2, 8, fp);
721 //_____________________________________________________________________________
723 AliMUONTriggerIO::WriteLocalLUT(const AliMUONTriggerLut& lut,
727 /// loop over the address for the 4-bits lpt and hpt decisions
729 const Int_t kMaskYpos = 0x0F;
730 const Int_t kMaskYtri = 0x01;
731 const Int_t kMaskXdev = 0x1F;
732 const Int_t kMaskXpos = 0x1F;
734 for (Int_t i = 0; i < 32768; ++i)
736 Int_t lutLpt[2] = { 0 };
737 Int_t lutHpt[2] = { 0 };
740 Int_t iYpos = i & kMaskYpos;
741 Int_t iYtri = ( i >> 4 ) & kMaskYtri;
742 Int_t iXdev = ( i >> ( 4 + 1 ) ) & kMaskXdev;
743 Int_t iXpos = ( i >> ( 4 + 1 + 5 ) ) & kMaskXpos;
745 // convert deviation format
746 // online: sign 1bit , dev 4bit
750 // 0 0 mu+, mu- infinite momentum (unde)
756 // - 15 mu+, mu- infinite momentum (unde)
760 Bool_t trigx = kFALSE;
761 iXdevOn += iXdev & 0x0F;
762 sign += (iXdev >> 4) & 0x01;
774 iXdevOff = - iXdevOn + 15; // gives range 0-14
776 iXdevOff = + iXdevOn + 15; // gives range 16-30 !
781 // iYtri == 1 means no trigger in y-direction
782 if (iYtri == 0 && trigx)
784 lut.GetLutOutput(localBoardId,iXpos,iXdev,iYpos,lutLpt,lutHpt);
794 buffer += lutHpt[1] << 7;
795 buffer += lutHpt[0] << 6;
796 buffer += lutLpt[1] << 5;
797 buffer += lutLpt[0] << 4;
800 buffer += lutHpt[1] << 3;
801 buffer += lutHpt[0] << 2;
802 buffer += lutLpt[1] << 1;
803 buffer += lutLpt[0] << 0;
804 fwrite(&buffer,1,1,flut);
809 //_____________________________________________________________________________
811 AliMUONTriggerIO::LocalBoardId(Int_t index) const
813 /// Return the i-th localBoardId, or -1 if index is out of bounds
815 AliMpLocalBoard* board = fRegionalTrigger.GetLocalBoard(index);
816 if ( ! board ) return -1;
818 return board->GetId();
821 //_____________________________________________________________________________
823 AliMUONTriggerIO::UpdateMapping(Bool_t writeFile) const
825 /// check if mapping in database different from current Mtg configuration
826 /// Update mapping in databse and read regional crate file in repository (ext .out
827 /// to avoid overwriting). This case has a low probability to happen.
829 // Assuming that crates do not change
831 if (!AliMpDDLStore::Instance(kFALSE))
833 AliMpCDB::LoadDDLStore();
836 Bool_t modified = false;
838 TExMapIter itr = AliMpDDLStore::Instance()->GetLocalBoardItr();
842 while(itr.Next(key, value))
844 AliMpLocalBoard* boardMapping = reinterpret_cast<AliMpLocalBoard*>(value);
846 Int_t localBoardId = boardMapping->GetId();
847 AliMpLocalBoard* board = fRegionalTrigger.FindLocalBoard(localBoardId);
849 AliFatal("Board found in mapping but not in regional trigger");
853 if ( board->GetCrate().CompareTo(boardMapping->GetCrate()) != 0 )
855 AliWarning(Form("Crate Name different for board %d (%s %s)", localBoardId, boardMapping->GetCrate().Data(),
856 board->GetCrate().Data()));
857 boardMapping->SetCrate( board->GetCrate() );
861 if ((board->GetSlot()) != boardMapping->GetSlot())
863 AliWarning(Form("Slot different for board %d (%d %d)", localBoardId, boardMapping->GetSlot(), board->GetSlot()+1));
864 boardMapping->SetSlot(board->GetSlot());
868 if (boardMapping->GetSwitch() != board->GetSwitch()) {
869 AliWarning(Form("Switch different for board %d (0x%x 0x%x)", localBoardId,
870 boardMapping->GetSwitch(), board->GetSwitch()));
871 boardMapping->SetSwitch(board->GetSwitch());
878 AliMpDDLStore::Instance()->SetRegionalTrigger(fRegionalTrigger);
879 AliMpCDB::WriteDDLStore(false);
880 AliWarning("Wrote new version of mapping in databse");
883 TString file = AliMpFiles::LocalTriggerBoardMapping();
885 WriteRegional(file.Data(), 0x0);
886 AliWarning(Form("Wrote regional file %s", file.Data()));