Changeset 10080
- Timestamp:
- 01/05/11 14:12:25 (14 years ago)
- Location:
- fact/FADctrl
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/FADctrl/FAD.cc
r10077 r10080 32 32 {"address", &FAD::cmd_address, true, 2, "<range> <value>", "Set addresses in range to value"}, 33 33 {"send", &FAD::cmd_send, true, 1, "<value>", "Set arbitrary data to board"}, 34 {"acalib", &FAD::cmd_acalib, true, 0, "", "Amplitude calibration"}, 35 //{"tcalib", &FAD::cmd_tcalib, 1, "<trig rate> ", "Time calibration"}, 34 {"acalib", &FAD::cmd_acalib, true, 0, "[n]", "Amplitude calibration (n events)"}, 36 35 //{"wmode", &FAD::cmd_wmode, 0, "<run|stop>", "Domino wave running or stopped during read out"}, 37 36 //{"rmode", &FAD::cmd_rmode, 0, "<first|stop>", "Readout start at first bin or stop position (DRS4)"}, … … 39 38 {"cancel", &FAD::cmd_cancel, false, 0, "", "Cancel current operation"}, 40 39 {"update", &FAD::cmd_update, false, 1, "<sec>", "Minimum delay between updates to DIM event service"}, 41 40 {"socketmode", &FAD::cmd_socketmode, true, 1, "<com|daq>", "Choose which Sockets are used for data transmission"}, 42 41 {"exit", &FAD::cmd_exit, true, 0, "", "Exit program"}, 43 42 {"help", &FAD::cmd_help, false, 0, "", "Print help"}}; … … 105 104 int Ret; 106 105 106 // Wait for DIM service thread to quit 107 if (pthread_equal(Thread, pthread_self()) == 0) { 108 if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed in ~FAD() (%s)", strerror(Ret)); 109 } 110 107 111 // Delete all boards (cancels threads automatically) 108 112 for (unsigned int i=0; i<Boards.size(); i++) delete Boards[i]; 109 110 // Cancel SIM service thread and wait for it to quit111 if (pthread_equal(Thread, pthread_self()) == 0) {112 if ((Ret = pthread_cancel(Thread)) != 0) Message(ERROR, "Could not request thread cancellation (%s)", strerror(Ret));113 if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed (%s)", strerror(Ret));114 }115 113 116 114 delete Command; … … 456 454 int Code; 457 455 458 // Set mode before lunching thread 456 // Invalidate calibration? 457 if (Parameter.size() == 2 && Match(Parameter[1], "invalidate")) { 458 for (unsigned int i=0; i<Boards.size(); i++) Boards[i]->ACalibTime = -1; 459 return; 460 } 461 462 // Set number of events required for calibration 463 if (Parameter.size()==1 || !ConvertToInt(Parameter[1], &NumCalibEvents) || NumCalibEvents<=0) { 464 NumCalibEvents = DEFAULT_NUM_CALIB_EVENTS; 465 } 466 467 // Set mode before launching thread 459 468 Mode = acalib; 460 469 Cancel = false; … … 472 481 } 473 482 474 /*475 // Do time calibration476 void FAD::cmd_tcalib() {477 478 if (!IsDRSFreqSet()) {479 PrintMessage("Set sampling frequency for all boards first\n");480 return;481 }482 if (!ReadCalibration()) {483 PrintMessage("Amplitude calibration has to be done first\n");484 return;485 }486 487 for (int i=FirstBoard; i<=LastBoard; i++) {488 if (GetBoard(i)->GetDRSType() != 4 || GetBoard(i)->GetFirmwareVersion() < 13279) {489 PrintMessage("Time calibration needs DRS4 and minimum firmware version 13279, skipping board %d\n", i);490 continue;491 }492 PrintMessage("Creating time calibration of board %d (serial #%04d)\n Note: No input signals should be connected\n", i, GetBoard(i)->GetBoardSerialNumber());493 494 GetBoard(i)->SetFrequency(DRSFreq[i], true);495 if (GetBoard(i)->CalibrateTiming(this) != 1) {496 PrintMessage("Time calibration method returned error status, stopping calibration\n");497 return;498 }499 500 TCalib[i] = true;501 502 // Write calibration data to file503 float Time[NChipsMax][NBins];504 char *Filename;505 FILE *Calibfile;506 bool WriteOK = true;507 508 // Copy calibration data into array509 for (int Chip=0; Chip<GetBoard(i)->GetNumberOfChips(); Chip++) {510 GetBoard(i)->GetTime(Chip, Time[Chip], true, false);511 }512 513 // Write calibration data to file514 if (asprintf(&Filename, "%s/TCalib_%d_%.2fGHz.txt", fCalibDataPath, GetBoard(i)->GetBoardSerialNumber(), DRSFreq[i]) == -1) {515 PrintMessage("Error: asprintf() failed, cannot generate filename (%s)\n", strerror(errno));516 return;517 }518 if ((Calibfile=fopen(Filename,"w")) == NULL) {519 PrintMessage("Error: Could not open file '%s' \n", Filename);520 }521 else {522 if(fprintf(Calibfile, "# DRS time calibration\n") == -1) WriteOK = false;523 524 for (int Bin=0; Bin<NBins; Bin++) {525 for (int Chip=0; Chip<GetBoard(i)->GetNumberOfChips(); Chip++) {526 if(fprintf(Calibfile, "%.2f ", Time[Chip][Bin]) == -1) WriteOK = false;527 }528 if(fprintf(Calibfile, "\n") == -1) WriteOK = false;529 }530 if (fclose(Calibfile) != 0) PrintMessage("Error closing file '%s'\n", Filename);531 }532 if (!WriteOK) PrintMessage("Error writing to file '%s'\n", Filename);533 else PrintMessage("Calibration written to file '%s'\n", Filename);534 535 free(Filename);536 }537 PrintMessage("Time calibration finished\n");538 }539 */540 483 541 484 // … … 604 547 else 605 548 PrintMessage(" DMODE single shot\n"); 606 if (GetBoard(i)->GetCtrlReg() & BIT_ENABLE_TRIGGER1)607 PrintMessage(" ENABLE_TRIGGER\n");608 549 if (GetBoard(i)->GetCtrlReg() & BIT_ACAL_EN) 609 550 PrintMessage(" ACAL enabled\n"); … … 663 604 void FAD::cmd_help() { 664 605 665 char Buffer[MAX_COM_SIZE];606 char *Buffer; 666 607 667 608 for(unsigned int i=0; i<sizeof(CommandList)/sizeof(CL_Struct); i++) { 668 snprintf(Buffer, sizeof(Buffer), "%s %s", CommandList[i].Name, CommandList[i].Parameters); 669 PrintMessage("%-28s%s\n", Buffer, CommandList[i].Help); 670 } 609 if (asprintf(&Buffer, "%s %s", CommandList[i].Name, CommandList[i].Parameters) == -1) { 610 PrintMessage("Error printing help, asprintf() failed\n"); 611 break; 612 } 613 else PrintMessage("%-28s%s\n", Buffer, CommandList[i].Help); 614 free(Buffer); 615 } 671 616 PrintMessage(".<command> Execute shell command\n\n" 672 617 "Items in <> are mandatory, in [] optional, | indicates mutual exclusive.\n" … … 706 651 void FAD::AmplitudeCalibration() { 707 652 708 static unsigned int ReqNum = 10;709 unsigned short Buffer[2*NChips*NChannels];710 unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 0)), 0};653 vector<struct FADBoard::BoardStatus> Status; 654 vector<unsigned short> ROICmd; 655 unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 2)), 0}; 711 656 712 657 /* Procedure … … 718 663 11. Secondary calibration 719 664 */ 720 struct FADBoard::BoardStatus *Status = new struct FADBoard::BoardStatus [Boards.size()]; 721 722 PrintMessage("Staring amplitude calibration of all active boards/n Note: No input signals must be connected\n"); 723 665 666 PrintMessage("Staring amplitude calibration of all active boards (%d events)\n" 667 " Note: No input signals must be connected\n", NumCalibEvents); 668 669 // Prepare command to set all ROIs to 1024 670 for (unsigned int i=0; i<NChips*NChannels; i++) { 671 ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i))); 672 ROICmd.push_back(htons(NBins)); 673 } 674 724 675 // Initialise settings for calibration 725 676 for (unsigned int Brd=0; Brd<Boards.size(); Brd++) { 726 677 678 // Invalidate current calibration 679 Boards[Brd]->ACalibTime = -1; 680 727 681 // Save initial board status 728 Status [Brd] = Boards[Brd]->GetStatus();682 Status.push_back(Boards[Brd]->GetStatus()); 729 683 730 684 // Set all ROI to 1024 731 for (unsigned int i=0; i<NChips*NChannels; i++) { 732 Buffer[2*i] = htons(CMD_Write | (BADDR_ROI + i)); 733 Buffer[2*i+1] = htons(NBins); 734 } 735 Boards[Brd]->Send(Buffer, sizeof(Buffer)); 685 Boards[Brd]->Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short)); 736 686 737 687 // Set DAC first value 738 DACCmd[1] = htons( 10);688 DACCmd[1] = htons(0); 739 689 Boards[Brd]->Send(DACCmd, sizeof(DACCmd)); 740 690 741 691 // Start accumulation 742 Boards[Brd]->AccumulateSum( ReqNum);743 Boards[Brd]->Send(CMD_Trigger_C);692 Boards[Brd]->AccumulateSum(NumCalibEvents); 693 //Boards[Brd]->Send(CMD_Trigger_C); 744 694 } 745 695 … … 753 703 } 754 704 } 705 printf("Got first average\n"); 755 706 756 707 for (unsigned int Brd=0; Brd<Boards.size(); Brd++) { 757 708 // Determine baseline 758 709 for (unsigned int i=0; i<NChips; i++) for (unsigned int j=0; j<NChannels; j++) { 759 for (unsigned int k=0; k<NBins; k++) Boards[Brd]->Baseline[i][j][k] = Boards[Brd]->Sum[i][j][k] / 10;710 for (unsigned int k=0; k<NBins; k++) Boards[Brd]->Baseline[i][j][k] = Boards[Brd]->Sum[i][j][k] / NumCalibEvents; 760 711 } 761 712 // Set second DAC value 762 DACCmd[1] = htons( 30000);713 DACCmd[1] = htons(50000); 763 714 Boards[Brd]->Send(DACCmd, sizeof(DACCmd)); 764 715 // Start accumulation 765 Boards[Brd]->AccumulateSum( ReqNum);716 Boards[Brd]->AccumulateSum(NumCalibEvents); 766 717 } 767 718 … … 778 729 // Stop triggering, write back original ROI and DAC settings 779 730 for (unsigned int Brd=0; Brd<Boards.size(); Brd++) { 780 Boards[Brd]->Send(CMD_Trigger_S);731 //Boards[Brd]->Send(CMD_Trigger_S); 781 732 782 733 // Determine gain 783 734 for (unsigned int i=0; i<NChips; i++) for (unsigned int j=0; j<NChannels; j++) { 784 for (unsigned int k=0; k<NBins; k++) Boards[Brd]->Gain[i][j][k] = (Boards[Brd]->Sum[i][j][k] / 10)/Boards[Brd]->Baseline[i][j][k]; 785 } 786 735 for (unsigned int k=0; k<NBins; k++) Boards[Brd]->Gain[i][j][k] = ((Boards[Brd]->Sum[i][j][k] / NumCalibEvents)-Boards[Brd]->Baseline[i][j][k])/3000; 736 } 737 738 ROICmd.clear(); 787 739 for (unsigned int i=0; i<NChips*NChannels; i++) { 788 Buffer[2*i] = htons(CMD_Write | (BADDR_ROI + i));789 Buffer[2*i+1] = htons(Status[Brd].ROI[i/NChannels][i%NChannels]);790 } 791 Boards[Brd]->Send( Buffer, sizeof(Buffer));792 793 DACCmd[1] = htons(Status[Brd].DAC[ 0]);740 ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i))); 741 ROICmd.push_back(htons(Status[Brd].ROI[i/NChannels][i%NChannels])); 742 } 743 Boards[Brd]->Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short)); 744 745 DACCmd[1] = htons(Status[Brd].DAC[2]); 794 746 Boards[Brd]->Send(DACCmd, sizeof(DACCmd)); 795 747 … … 800 752 } 801 753 802 delete[] Status;803 804 754 PrintMessage("Amplitude calibration of all active boards finished, original ROI and DAC set\n"); 805 755 Mode = idle; … … 897 847 for (unsigned int Brd=0; Brd<Boards.size(); Brd++) { 898 848 S = Boards[Brd]->GetStatus(); 899 if (S.Update.tv_sec>LastUpdate.tv_sec || ((S.Update.tv_sec==LastUpdate.tv_sec) && (S.Update.tv_ sec>LastUpdate.tv_sec))) {849 if (S.Update.tv_sec>LastUpdate.tv_sec || ((S.Update.tv_sec==LastUpdate.tv_sec) && (S.Update.tv_usec>LastUpdate.tv_usec))) { 900 850 901 851 Update = true; … … 921 871 Boards[Brd]->Lock(); 922 872 for (unsigned int Chip=0; Chip<NChips; Chip++) for (unsigned int Chan=0; Chan<NChannels; Chan++) { 923 for (int i=0; i<S.ROI[Chip][Chan]; i++) Data[Count++] = Boards[Brd]->Data[Chip][Chan][i]; 873 for (int i=0; i<S.ROI[Chip][Chan]; i++) { 874 if (Boards[Brd]->ACalibTime == -1) Data[Count++] = Boards[Brd]->Data[Chip][Chan][i]; 875 else Data[Count++] = (Boards[Brd]->Data[Chip][Chan][i] - Boards[Brd]->Baseline[Chip][Chan][(i+S.TriggerCell[Chip])%NBins]) * Boards[Brd]->Gain[Chip][Chan][(i+S.TriggerCell[Chip])%NBins]; 876 } 924 877 Count += NBins - S.ROI[Chip][Chan]; 925 878 } -
fact/FADctrl/FAD.h
r10077 r10080 2 2 #define FAD_H_SEEN 3 3 4 //#define SERVER_NAME "FADctrl" // Name to use in DIM5 #define SERVER_NAME "drsdaq" // Name to use in DIM4 #define SERVER_NAME "FADctrl" // Name to use in DIM 5 //#define SERVER_NAME "drsdaq" // Name to use in DIM 6 6 #include "Evidence.h" 7 7 … … 21 21 #include "FADBoard.h" 22 22 23 #define MAX_COM_SIZE 10000 23 const int DEFAULT_NUM_CALIB_EVENTS = 100; 24 24 25 25 enum ModeType {idle, acalib, tcalib}; … … 44 44 int EventUpdateDelay; 45 45 46 int NumCalibEvents; 46 47 //const char *fCalibDataPath; 47 48 //char CalibInfoFilename[MAX_PATH]; … … 70 71 void cmd_dwrite(); void cmd_domino(); 71 72 void cmd_wmode(); void cmd_rmode(); 72 void cmd_dmode(); 73 void cmd_tcalib(); void cmd_dac(); 73 void cmd_dmode(); void cmd_dac(); 74 74 void cmd_roi(); void cmd_address(); 75 75 void cmd_phase(); void cmd_send(); -
fact/FADctrl/FADBoard.cc
r10036 r10080 55 55 ID << SERVER_NAME"/Board" << setfill('0') << setw(2) << Num << "/"; 56 56 57 NameService = new DimService((ID.str()+"Server").c_str(), Name);58 IDService= new DimService((ID.str()+"BoardID").c_str(), (char *) "S", NULL, 0);59 TempService= new DimService((ID.str()+"Temperature").c_str(), (char *) "F", NULL, 0);60 D ACService= new DimService((ID.str()+"DAC").c_str(), (char *) "S", NULL, 0);61 ROIService= new DimService((ID.str()+"ROI").c_str(), (char *) "S", NULL, 0);57 DIM_Name = new DimService((ID.str()+"Server").c_str(), Name); 58 DIM_ID = new DimService((ID.str()+"BoardID").c_str(), (char *) "S", NULL, 0); 59 DIM_Temp = new DimService((ID.str()+"Temperature").c_str(), (char *) "F", NULL, 0); 60 DIM_DAC = new DimService((ID.str()+"DAC").c_str(), (char *) "S", NULL, 0); 61 DIM_ROI = new DimService((ID.str()+"ROI").c_str(), (char *) "S", NULL, 0); 62 62 63 63 // Initialise mutex for synchronization … … 74 74 // Create thread that receives data 75 75 if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchThread,(void *) this)) != 0) { 76 m-> PrintMessage("pthread_create() failed(%s)\n", strerror(Ret));76 m->Message(m->ERROR, "pthread_create() failed in FADBoard() (%s)\n", strerror(Ret)); 77 77 Thread = pthread_self(); 78 78 return; … … 91 91 // Cancel thread and wait for it to quit 92 92 if (pthread_equal(Thread, pthread_self()) == 0) { 93 if ((Ret = pthread_cancel(Thread)) != 0) m-> PrintMessage("Error: Could not request thread cancellation (%s)\n", strerror(Ret));94 if ((Ret = pthread_join(Thread, NULL)) != 0) m-> PrintMessage("pthread_join() failed (%s)\n", strerror(Ret));93 if ((Ret = pthread_cancel(Thread)) != 0) m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret)); 94 if ((Ret = pthread_join(Thread, NULL)) != 0) m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret)); 95 95 } 96 96 97 97 // Delete mutex 98 98 if (InitOK && ((Ret = pthread_mutex_destroy(&Mutex)) != 0)) { 99 m-> PrintMessage("pthread_mutex_destroy() failed (%s)", strerror(Ret));100 } 101 102 delete NameService;103 delete IDService;104 delete TempService;105 delete D ACService;106 delete ROIService;99 m->Message(m->ERROR, "pthread_mutex_destroy() failed in ~FADBoard (%s)", strerror(Ret)); 100 } 101 102 delete DIM_Name; 103 delete DIM_ID; 104 delete DIM_Temp; 105 delete DIM_DAC; 106 delete DIM_ROI; 107 107 delete[] Name; 108 108 … … 277 277 for (unsigned int Chip=0; Chip<NChips; Chip++) for (unsigned int Chan=0; Chan<NChannels; Chan++) { 278 278 for (int i=0; i<Status.ROI[Chip][Chan]; i++) { 279 Sum[Chip][Chan][(i+ ntohs(Channel[Chip+NChips*Chan]->start_cell)) % NBins]= Data[Chip][Chan][i];279 Sum[Chip][Chan][(i+Status.TriggerCell[Chip]) % NBins] += Data[Chip][Chan][i]; 280 280 } 281 281 } … … 288 288 // Update DIM services if necessary 289 289 if (memcmp(PrevStatus.Temp, Status.Temp, sizeof(Status.Temp)) != 0) { 290 TempService->updateService(Status.Temp, sizeof(Status.Temp));290 DIM_Temp->updateService(Status.Temp, sizeof(Status.Temp)); 291 291 } 292 292 if (memcmp(PrevStatus.DAC, Status.DAC, sizeof(Status.DAC)) != 0) { 293 D ACService->updateService(Status.DAC, sizeof(Status.DAC));293 DIM_DAC->updateService(Status.DAC, sizeof(Status.DAC)); 294 294 } 295 295 if (memcmp(PrevStatus.ROI, Status.ROI, sizeof(Status.ROI)) != 0) { 296 ROIService->updateService(Status.ROI, sizeof(Status.ROI));296 DIM_ROI->updateService(Status.ROI, sizeof(Status.ROI)); 297 297 } 298 298 if (PrevStatus.BoardID != Status.BoardID) { 299 IDService->updateService(&Status.BoardID, sizeof(Status.BoardID));299 DIM_ID->updateService(&Status.BoardID, sizeof(Status.BoardID)); 300 300 } 301 301 } -
fact/FADctrl/FADBoard.h
r10036 r10080 28 28 pthread_t Thread; 29 29 pthread_mutex_t Mutex; 30 DimService * NameService;31 DimService * IDService;32 DimService * TempService;33 DimService * ROIService;34 DimService *D ACService;30 DimService *DIM_Name; 31 DimService *DIM_ID; 32 DimService *DIM_Temp; 33 DimService *DIM_ROI; 34 DimService *DIM_DAC; 35 35 36 36 void ReadLoop(); … … 52 52 } Status; 53 53 54 unsignedshort Data[NChips][NChannels][NBins];55 doubleBaseline[NChips][NChannels][NBins];54 short Data[NChips][NChannels][NBins]; 55 short Baseline[NChips][NChannels][NBins]; 56 56 double Gain[NChips][NChannels][NBins]; 57 57 float ACalibTemp; 58 58 time_t ACalibTime; 59 59 60 doubleSum[NChips][NChannels][NBins];60 long int Sum[NChips][NChannels][NBins]; 61 61 unsigned int NumForSum; 62 62 bool DoSum; -
fact/FADctrl/FADctrl.cc
r10077 r10080 48 48 sleep(1); 49 49 50 DimClient::sendCommand(SERVER_NAME"/Command", "dac 0 21000"); 51 DimClient::sendCommand(SERVER_NAME"/Command", "dac 1 0"); 52 DimClient::sendCommand(SERVER_NAME"/Command", "dac 2-3 5000"); 50 DimClient::sendCommand(SERVER_NAME"/Command", "dac 0 25000"); 51 DimClient::sendCommand(SERVER_NAME"/Command", "dac 1-3 0"); 53 52 DimClient::sendCommand(SERVER_NAME"/Command", "dac 4-7 28800"); 54 53 sleep (1); … … 105 104 106 105 // Terminate thread for other sockets 107 if ((Ret = pthread_cancel(Thread)) != 0) printf("Error: Could not request thread cancellation (%s)\n", strerror(Ret));108 if ((Ret = pthread_join(Thread, NULL)) != 0) printf("pthread_join() failed (%s)\n", strerror(Ret));106 if ((Ret = pthread_cancel(Thread)) != 0) printf("Error: Could not request thread cancellation in main() (%s)\n", strerror(Ret)); 107 if ((Ret = pthread_join(Thread, NULL)) != 0) printf("pthread_join() failed in main () (%s)\n", strerror(Ret)); 109 108 } 110 109 … … 169 168 if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]); 170 169 else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno)); 171 else printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]);170 else ;//printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]); 172 171 } 173 172 } -
fact/FADctrl/History.txt
r10036 r10080 2 2 3 3 22/10/2010 First check-in of FADctrl. Initial version derived from drsdaq revision 10007. 4 5/1/2011 First version of amplitude calibration (no secondary calibration, yet)
Note:
See TracChangeset
for help on using the changeset viewer.