Changeset 10080


Ignore:
Timestamp:
Jan 5, 2011, 2:12:25 PM (10 years ago)
Author:
ogrimm
Message:
Added amplitude calibration
Location:
fact/FADctrl
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • fact/FADctrl/FAD.cc

    r10077 r10080  
    3232   {"address", &FAD::cmd_address, true, 2, "<range> <value>", "Set addresses in range to value"},
    3333   {"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)"},
    3635   //{"wmode", &FAD::cmd_wmode, 0, "<run|stop>", "Domino wave running or stopped during read out"},
    3736   //{"rmode", &FAD::cmd_rmode, 0, "<first|stop>", "Readout start at first bin or stop position (DRS4)"},
     
    3938   {"cancel", &FAD::cmd_cancel, false, 0, "", "Cancel current operation"},
    4039   {"update", &FAD::cmd_update, false, 1, "<sec>", "Minimum delay between updates to DIM event service"},                 
    41         {"socketmode", &FAD::cmd_socketmode, true, 1, "<com|daq>", "Choose which Sockets are used for data transmission"},               
     40  {"socketmode", &FAD::cmd_socketmode, true, 1, "<com|daq>", "Choose which Sockets are used for data transmission"},             
    4241   {"exit", &FAD::cmd_exit, true, 0, "", "Exit program"},
    4342   {"help", &FAD::cmd_help, false, 0, "", "Print help"}};
     
    105104  int Ret;
    106105
     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
    107111  // Delete all boards (cancels threads automatically)
    108112  for (unsigned int i=0; i<Boards.size(); i++) delete Boards[i];
    109 
    110   // Cancel SIM service thread and wait for it to quit
    111   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   }
    115113
    116114  delete Command;
     
    456454  int Code;
    457455
    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
    459468  Mode = acalib;
    460469  Cancel = false;
     
    472481}
    473482
    474 /*
    475 // Do time calibration
    476 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 file
    503     float Time[NChipsMax][NBins];
    504     char *Filename;
    505     FILE *Calibfile;
    506         bool WriteOK = true;
    507        
    508     // Copy calibration data into array
    509         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 file
    514     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 */
    540483
    541484//
     
    604547        else
    605548          PrintMessage("   DMODE single shot\n");
    606         if (GetBoard(i)->GetCtrlReg() & BIT_ENABLE_TRIGGER1)
    607           PrintMessage("   ENABLE_TRIGGER\n");
    608549        if (GetBoard(i)->GetCtrlReg() & BIT_ACAL_EN)
    609550          PrintMessage("   ACAL enabled\n");
     
    663604void FAD::cmd_help() {
    664605
    665   char Buffer[MAX_COM_SIZE];
     606  char *Buffer;
    666607
    667608  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  }
    671616  PrintMessage(".<command>                  Execute shell command\n\n"
    672617   "Items in <> are mandatory, in [] optional, | indicates mutual exclusive.\n"
     
    706651void FAD::AmplitudeCalibration() {
    707652
    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};
    711656
    712657  /* Procedure
     
    718663  11. Secondary calibration
    719664*/
    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
    724675  // Initialise settings for calibration
    725676  for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    726677
     678        // Invalidate current calibration
     679        Boards[Brd]->ACalibTime = -1;
     680
    727681        // Save initial board status
    728         Status[Brd] = Boards[Brd]->GetStatus();
     682        Status.push_back(Boards[Brd]->GetStatus());
    729683       
    730684    // 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));
    736686
    737687    // Set DAC first value
    738     DACCmd[1] = htons(10);
     688    DACCmd[1] = htons(0);
    739689        Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    740690
    741691        // 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);
    744694  }
    745695
     
    753703        }
    754704  }
     705  printf("Got first average\n");
    755706
    756707  for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    757708        // Determine baseline
    758709        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;
    760711        }
    761712        // Set second DAC value
    762     DACCmd[1] = htons(30000);
     713    DACCmd[1] = htons(50000);
    763714        Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    764715        // Start accumulation
    765     Boards[Brd]->AccumulateSum(ReqNum);
     716    Boards[Brd]->AccumulateSum(NumCalibEvents);
    766717  }
    767718
     
    778729  // Stop triggering, write back original ROI and DAC settings
    779730  for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    780         Boards[Brd]->Send(CMD_Trigger_S);
     731        //Boards[Brd]->Send(CMD_Trigger_S);
    781732
    782733        // Determine gain
    783734        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();
    787739        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]);
    794746        Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    795747       
     
    800752  }
    801753
    802   delete[] Status;
    803  
    804754  PrintMessage("Amplitude calibration of all active boards finished, original ROI and DAC set\n");
    805755  Mode = idle;
     
    897847        for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    898848          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))) {
    900850
    901851                Update = true;
     
    921871                Boards[Brd]->Lock();
    922872                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                  }
    924877                  Count += NBins - S.ROI[Chip][Chan];
    925878                }
  • fact/FADctrl/FAD.h

    r10077 r10080  
    22#define FAD_H_SEEN
    33
    4 //#define SERVER_NAME "FADctrl"       // Name to use in DIM
    5 #define SERVER_NAME "drsdaq"       // Name to use in DIM
     4#define SERVER_NAME "FADctrl"       // Name to use in DIM
     5//#define SERVER_NAME "drsdaq"       // Name to use in DIM
    66#include "Evidence.h"
    77
     
    2121#include "FADBoard.h"
    2222
    23 #define MAX_COM_SIZE 10000
     23const int DEFAULT_NUM_CALIB_EVENTS = 100;
    2424
    2525enum ModeType {idle, acalib, tcalib};
     
    4444        int EventUpdateDelay;
    4545
     46        int NumCalibEvents;
    4647    //const char *fCalibDataPath;
    4748    //char CalibInfoFilename[MAX_PATH];
     
    7071        void cmd_dwrite();              void cmd_domino();
    7172    void cmd_wmode();           void cmd_rmode();
    72     void cmd_dmode();
    73         void cmd_tcalib();              void cmd_dac();
     73    void cmd_dmode();           void cmd_dac();
    7474        void cmd_roi();                 void cmd_address();
    7575        void cmd_phase();               void cmd_send();
  • fact/FADctrl/FADBoard.cc

    r10036 r10080  
    5555  ID << SERVER_NAME"/Board" << setfill('0') << setw(2) << Num << "/";
    5656
    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   DACService = 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);
    6262
    6363  // Initialise mutex for synchronization
     
    7474  // Create thread that receives data
    7575  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));
    7777        Thread = pthread_self();
    7878        return;
     
    9191  // Cancel thread and wait for it to quit
    9292  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));
    9595  }
    9696
    9797  // Delete mutex 
    9898  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 DACService;
    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;
    107107  delete[] Name;
    108108
     
    277277                for (unsigned int Chip=0; Chip<NChips; Chip++) for (unsigned int Chan=0; Chan<NChannels; Chan++) {
    278278                  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];
    280280                  }
    281281                }
     
    288288          // Update DIM services if necessary
    289289          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));
    291291          }
    292292          if (memcmp(PrevStatus.DAC, Status.DAC, sizeof(Status.DAC)) != 0) {
    293                 DACService->updateService(Status.DAC, sizeof(Status.DAC));
     293                DIM_DAC->updateService(Status.DAC, sizeof(Status.DAC));
    294294          } 
    295295          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));
    297297          } 
    298298          if (PrevStatus.BoardID != Status.BoardID) {
    299                 IDService->updateService(&Status.BoardID, sizeof(Status.BoardID));
     299                DIM_ID->updateService(&Status.BoardID, sizeof(Status.BoardID));
    300300          } 
    301301        }
  • fact/FADctrl/FADBoard.h

    r10036 r10080  
    2828        pthread_t Thread;
    2929        pthread_mutex_t Mutex;
    30         DimService *NameService;
    31         DimService *IDService;
    32         DimService *TempService;
    33         DimService *ROIService;
    34         DimService *DACService;
     30        DimService *DIM_Name;
     31        DimService *DIM_ID;
     32        DimService *DIM_Temp;
     33        DimService *DIM_ROI;
     34        DimService *DIM_DAC;
    3535
    3636        void ReadLoop();
     
    5252        } Status;
    5353
    54         unsigned short Data[NChips][NChannels][NBins];
    55         double Baseline[NChips][NChannels][NBins];
     54        short Data[NChips][NChannels][NBins];
     55        short Baseline[NChips][NChannels][NBins];
    5656        double Gain[NChips][NChannels][NBins];
    5757        float ACalibTemp;
    5858        time_t ACalibTime;
    5959
    60         double Sum[NChips][NChannels][NBins];
     60        long int Sum[NChips][NChannels][NBins];
    6161        unsigned int NumForSum;
    6262        bool DoSum;
  • fact/FADctrl/FADctrl.cc

    r10077 r10080  
    4848  sleep(1);
    4949
    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");
    5352  DimClient::sendCommand(SERVER_NAME"/Command", "dac 4-7 28800");
    5453  sleep (1);
     
    105104
    106105  // 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));
    109108}
    110109
     
    169168      if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
    170169      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]);
    172171    }
    173172  }
  • fact/FADctrl/History.txt

    r10036 r10080  
    22
    3322/10/2010      First check-in of FADctrl. Initial version derived from drsdaq revision 10007.
     45/1/2011        First version of amplitude calibration (no secondary calibration, yet)
Note: See TracChangeset for help on using the changeset viewer.