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;
97 //_____________________________________________________________________________
99 AliMUONTriggerIO::FillLut(AliMUONTriggerLut& lut,
100 Int_t icirc, UChar_t istripX, UChar_t idev,
101 Int_t lutLpt[16][2], Int_t lutHpt[16][2])
103 /// Fill the LUT histograms
105 if (icirc == 0 && istripX == 0 && idev == 0)
107 AliDebug(1,"Copy board, not filled ...");
111 Short_t iLptPlus, iLptMinu, iLptUnde;
112 Short_t iHptPlus, iHptMinu, iHptUnde;
114 iLptPlus = iLptMinu = iLptUnde = 0;
115 iHptPlus = iHptMinu = iHptUnde = 0;
117 for (Int_t istripY=0; istripY<16; istripY++)
119 if (lutLpt[istripY][1] == 0 && lutLpt[istripY][0] ==1)
120 iLptMinu=iLptMinu+(1 << istripY);
121 if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==0)
122 iLptPlus=iLptPlus+(1 << istripY);
123 if (lutLpt[istripY][1] == 1 && lutLpt[istripY][0] ==1)
124 iLptUnde=iLptUnde+(1 << istripY);
126 if (lutHpt[istripY][1] == 0 && lutHpt[istripY][0] ==1)
127 iHptMinu=iHptMinu+(1 << istripY);
128 if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==0)
129 iHptPlus=iHptPlus+(1 << istripY);
130 if (lutHpt[istripY][1] == 1 && lutHpt[istripY][0] ==1)
131 iHptUnde=iHptUnde+(1 << istripY);
135 lut.SetContent("LptMinu",icirc,istripX,idev,iLptMinu);
136 lut.SetContent("LptUnde",icirc,istripX,idev,iLptUnde);
137 lut.SetContent("LptPlus",icirc,istripX,idev,iLptPlus);
139 lut.SetContent("HptMinu",icirc,istripX,idev,iLptMinu);
140 lut.SetContent("HptUnde",icirc,istripX,idev,iLptUnde);
141 lut.SetContent("HptPlus",icirc,istripX,idev,iLptPlus);
144 //_____________________________________________________________________________
146 AliMUONTriggerIO::ReadLocalMasks(const char* localFile, AliMUONVStore& localMasks) const
148 /// Fills the local masks store from file
150 if ( !NofLocalBoards() )
152 AliError("No local board to read");
156 FILE* fp = fopen(gSystem->ExpandPathName(localFile),"r");
159 AliError(Form("Could not read file %s",localFile));
163 UShort_t maskBuffer[8];
165 Int_t nLocalBoards(0);
167 while ( fread ( maskBuffer, 2, 8, fp ) )
169 Int_t localBoardId = LocalBoardId(nLocalBoards);
170 AliDebug(1,Form("LB %03d X1 %4x X2 %4x X3 %4x X4 %4x "
171 "Y1 %4x Y2 %4x Y3 %4x Y4 %4x",
184 AliMUONVCalibParam* localBoard = new AliMUONCalibParamNI(1,8,localBoardId,0,0);
185 for ( Int_t index = 0; index < 8; ++index )
187 localBoard->SetValueAsInt(index,0,maskBuffer[index]);
189 localMasks.Add(localBoard);
195 if ( nLocalBoards != NofLocalBoards() )
197 AliError(Form("Read %d out of %d local boards",
198 nLocalBoards, NofLocalBoards()));
206 //_____________________________________________________________________________
208 AliMUONTriggerIO::ReadLocalLUT(AliMUONTriggerLut& lut,
212 /// Read the LUT for one local board from an online file
217 UChar_t mask1 = 0xF0;
218 UChar_t mask2 = 0x0F;
219 UChar_t maskLpt = 0x0C;
220 UChar_t maskHpt = 0x03;
221 UChar_t lh, lpt, hpt;
223 UChar_t xpos, xdev, ypos, ytri;
225 Int_t lutLpt[16][2], lutHpt[16][2];
227 Int_t boardnr = localBoardId;
229 AliDebug(1,Form("Reading LUT values for local board %d",boardnr));
233 // create the 32767 addresses for the 4-bits lpt and hpt half-bytes
234 for (UShort_t ilut = 0; ilut < 0x7FFF; ilut += 2)
236 // read two lut addresses at once
237 fread(&buffer,1,1,flut);
239 // 1st 4-bits half-byte
241 lh = (buffer & mask1) >> 4;
243 // Lpt and Hpt response
244 lpt = (lh & maskLpt) >> 2;
247 // decompose the 15-bits address
248 DeCompAddress(ypos,ytri,xdev,xpos,address);
250 // calculate group of y-strips
253 lutLpt[ny][0] = lpt & 1;
254 lutLpt[ny][1] = (lpt & 2) >> 1;
255 lutHpt[ny][0] = hpt & 1;
256 lutHpt[ny][1] = (hpt & 2) >> 1;
261 // ytri == 1 means no trigger in y-direction
264 FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
269 // 2nd 4-bits half-byte
271 lh = (buffer & mask2);
273 // Lpt and Hpt response
274 lpt = (lh & maskLpt) >> 2;
277 // decompose the 15-bits address
278 DeCompAddress(ypos,ytri,xdev,xpos,address);
280 // calculate group of y-strips
283 lutLpt[ny][0] = lpt & 1;
284 lutLpt[ny][1] = (lpt & 2) >> 1;
285 lutHpt[ny][0] = hpt & 1;
286 lutHpt[ny][1] = (hpt & 2) >> 1;
291 // ytri == 1 means no trigger in y-direction
294 FillLut(lut,boardnr,xpos,xdev,lutLpt,lutHpt);
301 //_____________________________________________________________________________
303 AliMUONTriggerIO::ReadLUT(const char* lutFileToRead, AliMUONTriggerLut& lut)
305 /// Fill the LUT object from online file
307 if ( !NofLocalBoards() )
309 AliError("No local board id defined. Must read a regional file first");
313 FILE* flut = fopen(gSystem->ExpandPathName(lutFileToRead),"rb");
316 AliError(Form("Could not read LUT file %s",lutFileToRead));
320 for ( Int_t i = 0; i < NofLocalBoards(); ++i )
322 ReadLocalLUT(lut,LocalBoardId(i),flut);
331 //_____________________________________________________________________________
333 AliMUONTriggerIO::ReadMasks(const char* localFile,
334 const char* regionalFile,
335 const char* globalFile,
336 AliMUONVStore* localMasks,
337 AliMUONVStore* regionalMasks,
338 AliMUONVCalibParam* globalMasks,
341 /// Fill the various masks store from files
345 AliError("Must have a regional file name to proceeed");
349 Int_t nCrates = ReadRegional(regionalFile,regionalMasks, warn);
351 if (!nCrates) return kFALSE;
353 if (localMasks && localFile)
355 Int_t nLocal = ReadLocalMasks(localFile,*localMasks);
356 AliDebug(1,Form("Read masks for %d local boards",nLocal));
359 Int_t nDarc = ReadGlobal(globalFile, globalMasks);
360 AliDebug(1,Form("Read disable for %d DARC boards",nDarc));
362 if (!nDarc) return kFALSE;
367 //_____________________________________________________________________________
369 AliMUONTriggerIO::ReadGlobal(const char* globalFile, AliMUONVCalibParam* globalMasks)
371 /// read the global crate file and file corresponding mask
372 /// the masks are disable bit for each crate, 8 per darc board
373 /// bit value 0 means enable, 1 means disable *
376 if ( ! fGlobalCrate.ReadData(globalFile) ) return 0;
378 UChar_t mask = fGlobalCrate.GetFirstDarcDisable();
379 ULong_t vmeAddr = fGlobalCrate.GetFirstDarcVmeAddr();
380 if (vmeAddr) nDarc++;
381 globalMasks->SetValueAsInt(0,0,mask);
383 mask = fGlobalCrate.GetSecondDarcDisable();
384 vmeAddr = fGlobalCrate.GetSecondDarcVmeAddr();
385 if (vmeAddr) nDarc++;
386 globalMasks->SetValueAsInt(1,0,mask);
391 //_____________________________________________________________________________
393 AliMUONTriggerIO::ReadRegional(const char* regionalFile, AliMUONVStore* regionalMasks, Bool_t warn)
395 /// Read regional file to fill the regional mask store *AND*
396 /// determine the order in which local boards will appear in local
399 if ( ! fRegionalTrigger.ReadData(regionalFile) ) return 0;
403 for (Int_t iCrate = 0; iCrate < fRegionalTrigger.GetNofTriggerCrates(); ++iCrate)
406 AliMpTriggerCrate* crate = fRegionalTrigger.GetTriggerCrateFast(iCrate);
410 AliMpTriggerCrate* triggerCrate = AliMpDDLStore::Instance()->GetTriggerCrate(crate->GetName());
414 AliError(Form("Mapping error : could not get crate %s", crate->GetName()));
421 if (regionalMasks != 0x0)
423 masks = crate->GetMask();
425 AliMUONVCalibParam* regionalBoard = new AliMUONCalibParamNI(1, 1, crate->GetId(), 0, 0);
426 regionalBoard->SetValueAsInt(0, 0, masks);
427 regionalMasks->Add(regionalBoard);
434 //_____________________________________________________________________________
436 AliMUONTriggerIO::WriteLUT(const AliMUONTriggerLut& lut,
437 const char* lutFileToWrite)
439 /// Convert an offline lut into an online (binary) lut file
441 if ( !NofLocalBoards() )
443 AliError("No local board id defined. Must read a regional file first");
447 FILE* flut = fopen(gSystem->ExpandPathName(lutFileToWrite),"wb");
450 AliError(Form("Could not create output LUT file %s",lutFileToWrite));
454 for ( Int_t i = 0; i < NofLocalBoards(); ++i )
456 WriteLocalLUT(lut,LocalBoardId(i),flut);
464 //_____________________________________________________________________________
466 AliMUONTriggerIO::WriteMasks(const char* localFile,
467 const char* regionalFile,
468 const char* globalFile,
469 AliMUONVStore* localMasks,
470 AliMUONVStore* regionalMasks,
471 AliMUONVCalibParam* globalMasks) const
475 ok = WriteLocalMasks(localFile, *localMasks);
476 ok &= WriteRegional(regionalFile, regionalMasks);
477 ok &= WriteGlobal(globalFile, globalMasks);
482 //_____________________________________________________________________________
484 AliMUONTriggerIO::WriteGlobal(const char* globalFile, AliMUONVCalibParam* globalMasks) const
486 /// write global file
487 /// if no global masks defined take the one of configuration
492 out.open(globalFile);
495 AliError(Form("Could not create output regional file %s", globalFile));
499 out << fGlobalCrate.GetName() << endl;
502 out << fGlobalCrate.GetJtagName() << endl;
503 out << Form("0x%08x", fGlobalCrate.GetJtagVmeAddr()) << endl;
504 out << Form("%d %d %d", fGlobalCrate.GetJtagClockDiv(),
505 fGlobalCrate.GetJtagRxPhase(), fGlobalCrate.GetJtagRdDelay()) << endl;
507 for (Int_t i = 0; i < fGlobalCrate.GetJtagNofLines(); ++i)
508 out << Form("%d ", fGlobalCrate.GetEnableJtag(i));
512 for (Int_t i = 0; i < fGlobalCrate.GetJtagNofLines(); ++i)
515 for (Int_t j = 0; j < fGlobalCrate.GetJtagNofLines(); ++j)
516 out << Form(" %s", fGlobalCrate.GetJtagCrateName(i,j).Data()) << endl;
520 out << fGlobalCrate.GetFirstDarcName() << endl;
521 out << Form("0x%08x", fGlobalCrate.GetFirstDarcVmeAddr()) << endl;
522 out << fGlobalCrate.GetFirstDarcType() << endl;
523 if (globalMasks != 0x0)
524 disable = globalMasks->ValueAsInt(0);
526 disable = fGlobalCrate.GetFirstDarcDisable();
527 out << Form("0x%02x", disable) << endl;
528 out << Form("0x%x", fGlobalCrate.GetFirstDarcL0Delay()) << endl;
529 out << Form("0x%x", fGlobalCrate.GetFirstDarcL1TimeOut()) << endl;
532 out << fGlobalCrate.GetSecondDarcName() << endl;
533 out << Form("0x%08x", fGlobalCrate.GetSecondDarcVmeAddr()) << endl;
534 out << fGlobalCrate.GetSecondDarcType() << endl;
535 if (globalMasks != 0x0)
536 disable = globalMasks->ValueAsInt(1);
538 disable = fGlobalCrate.GetSecondDarcDisable();
539 out << Form("0x%02x", disable) << endl;
540 out << Form("0x%x", fGlobalCrate.GetSecondDarcL0Delay()) << endl;
541 out << Form("0x%x", fGlobalCrate.GetSecondDarcL1TimeOut()) << endl;
544 out << fGlobalCrate.GetGlobalName() << endl;
545 out << Form("0x%08x", fGlobalCrate.GetGlobalVmeAddr()) << endl;
546 for (Int_t i = 0; i < fGlobalCrate.GetGlobalNofRegisters(); ++i)
547 out << Form("0x%x", fGlobalCrate.GetGlobalRegister(i)) << endl;
550 out << fGlobalCrate.GetFetName() << endl;
551 out << Form("0x%08x", fGlobalCrate.GetFetVmeAddr()) << endl;
552 for (Int_t i = 0; i < fGlobalCrate.GetFetNofRegisters(); ++i)
553 out << Form("0x%x", fGlobalCrate.GetFetRegister(i)) << endl;
559 //_____________________________________________________________________________
561 AliMUONTriggerIO::WriteRegional(const char* regionalFile, AliMUONVStore* regionalMasks) const
564 /// write regional mask with the current configuration
565 /// if regional masks not defined, take the one from current configuration
568 out.open(regionalFile);
572 AliError(Form("Could not create output regional file %s",regionalFile));
576 for (Int_t iCrate = 0; iCrate < fRegionalTrigger.GetNofTriggerCrates(); ++iCrate)
578 AliMpTriggerCrate* crate = fRegionalTrigger.GetTriggerCrateFast(iCrate);
580 out << crate->GetName() << endl;
581 out << Form("%02x", crate->GetId()) << endl;
582 out << crate->GetMode() << endl;
583 out << crate->GetCoinc() << endl;
586 if (regionalMasks != 0x0)
588 AliMUONVCalibParam* maskParam =
589 static_cast<AliMUONVCalibParam*>(regionalMasks->FindObject(crate->GetId()));
590 masks = maskParam->ValueAsInt(0,0);
594 masks = crate->GetMask();
597 out << Form("%04x", masks) << endl;
599 for (Int_t iLocal = 0; iLocal < crate->GetNofLocalBoards(); ++iLocal)
601 Int_t localBoardId = crate->GetLocalBoardId(iLocal);
603 AliMpLocalBoard* board = fRegionalTrigger.FindLocalBoard(localBoardId);
605 out << Form("%02d ", board->GetSlot())
607 << Form(" %03d ", localBoardId)
608 << Form("%03x", board->GetSwitch())
612 for (Int_t i = 0; i < board->GetNofDEs(); ++i)
613 out << Form("%4d ", board->GetDEId(i));
616 // print copy card numbers
617 out << Form(" %4d %4d", board->GetInputXfrom(), board->GetInputXto());
618 out << Form(" %4d %4d", board->GetInputYfrom(), board->GetInputYto());
619 out << Form(" %4d", board->GetTC()) << endl;
627 //_____________________________________________________________________________
629 AliMUONTriggerIO::WriteLocalMasks(const char* localFile, AliMUONVStore& localMasks) const
632 /// removing/adding enable for a local board need a update of the configuration
633 /// before calling this method
634 /// mask are written for all boards including the copy card (Ch.F.)
636 FILE* fp = fopen(gSystem->ExpandPathName(localFile),"wb");
639 AliError(Form("Could not create output local file %s",localFile));
643 UShort_t maskBuffer[8];
645 for (Int_t iCrate = 0; iCrate < fRegionalTrigger.GetNofTriggerCrates(); ++iCrate)
647 AliMpTriggerCrate* crate = fRegionalTrigger.GetTriggerCrateFast(iCrate);
649 UShort_t mask = crate->GetMask(); // getting mask from current config
651 for (Int_t iLocal = 0; iLocal < crate->GetNofLocalBoards(); ++iLocal)
653 Int_t localBoardId = crate->GetLocalBoardId(iLocal);
655 if ( (mask >> iLocal ) & 0x1 )
657 AliMUONVCalibParam* localMask =
658 static_cast<AliMUONVCalibParam*>(localMasks.FindObject(localBoardId));
660 for (Int_t index = 0; index < 8; ++index)
662 maskBuffer[index] = localMask->ValueAsInt(index,0);
665 fwrite ( maskBuffer, 2, 8, fp);
677 //_____________________________________________________________________________
679 AliMUONTriggerIO::WriteLocalLUT(const AliMUONTriggerLut& lut,
683 /// loop over the address for the 4-bits lpt and hpt decisions
685 const Int_t kMaskYpos = 0x0F;
686 const Int_t kMaskYtri = 0x01;
687 const Int_t kMaskXdev = 0x1F;
688 const Int_t kMaskXpos = 0x1F;
690 for (Int_t i = 0; i < 32768; ++i)
692 Int_t lutLpt[2] = { 0 };
693 Int_t lutHpt[2] = { 0 };
696 Int_t iYpos = i & kMaskYpos;
697 Int_t iYtri = ( i >> 4 ) & kMaskYtri;
698 Int_t iXdev = ( i >> ( 4 + 1 ) ) & kMaskXdev;
699 Int_t iXpos = ( i >> ( 4 + 1 + 5 ) ) & kMaskXpos;
701 // iYtri == 1 means no trigger in y-direction
704 lut.GetLutOutput(localBoardId,iXpos,iXdev,iYpos,lutLpt,lutHpt);
714 buffer += lutLpt[1] << 7;
715 buffer += lutLpt[0] << 6;
716 buffer += lutHpt[1] << 5;
717 buffer += lutHpt[0] << 4;
720 buffer += lutLpt[1] << 3;
721 buffer += lutLpt[0] << 2;
722 buffer += lutHpt[1] << 1;
723 buffer += lutHpt[0] << 0;
724 fwrite(&buffer,1,1,flut);
729 //_____________________________________________________________________________
731 AliMUONTriggerIO::LocalBoardId(Int_t index) const
733 /// Return the i-th localBoardId, or -1 if index is out of bounds
735 AliMpLocalBoard* board = fRegionalTrigger.GetLocalBoard(index);
736 if ( ! board ) return -1;
738 return board->GetId();
741 //_____________________________________________________________________________
743 AliMUONTriggerIO::UpdateMapping(Bool_t writeFile) const
745 /// check if mapping in database different from current Mtg configuration
746 /// Update mapping in databse and read regional crate file in repository (ext .out
747 /// to avoid overwriting). This case has a low probability to happen.
749 // Assuming that crates do not change
751 if (!AliMpDDLStore::Instance(kFALSE))
753 AliMpCDB::LoadDDLStore();
756 Bool_t modified = false;
758 TExMapIter itr = AliMpDDLStore::Instance()->GetLocalBoardItr();
762 while(itr.Next(key, value))
764 AliMpLocalBoard* boardMapping = reinterpret_cast<AliMpLocalBoard*>(value);
766 Int_t localBoardId = boardMapping->GetId();
767 AliMpLocalBoard* board = fRegionalTrigger.FindLocalBoard(localBoardId);
769 AliFatal("Board found in mapping but not in regional trigger");
773 if ( board->GetCrate().CompareTo(boardMapping->GetCrate()) != 0 )
775 AliWarning(Form("Crate Name different for board %d (%s %s)", localBoardId, boardMapping->GetCrate().Data(),
776 board->GetCrate().Data()));
777 boardMapping->SetCrate( board->GetCrate() );
781 if ((board->GetSlot()) != boardMapping->GetSlot())
783 AliWarning(Form("Slot different for board %d (%d %d)", localBoardId, boardMapping->GetSlot(), board->GetSlot()+1));
784 boardMapping->SetSlot(board->GetSlot());
788 if (boardMapping->GetSwitch() != board->GetSwitch()) {
789 AliWarning(Form("Switch different for board %d (0x%x 0x%x)", localBoardId,
790 boardMapping->GetSwitch(), board->GetSwitch()));
791 boardMapping->SetSwitch(board->GetSwitch());
798 AliMpDDLStore::Instance()->SetRegionalTrigger(fRegionalTrigger);
799 AliMpCDB::WriteDDLStore(false);
800 AliWarning("Wrote new version of mapping in databse");
803 TString file = AliMpFiles::LocalTriggerBoardMapping();
805 WriteRegional(file.Data(), 0x0);
806 AliWarning(Form("Wrote regional file %s", file.Data()));