Changeset 99 for drsdaq/HVFeedback.cc


Ignore:
Timestamp:
08/12/09 12:01:25 (15 years ago)
Author:
ogrimm
Message:
Streamlined DRS2 reponse calibration interface, updated HV feedback socket communication
File:
1 edited

Legend:

Unmodified
Added
Removed
  • drsdaq/HVFeedback.cc

    r92 r99  
    1616#include <signal.h>
    1717
    18 #define MAX_RETRY 5
    1918#define PIXMAP_LOCATION "../config/PixelMap.txt"
    2019
     
    6463    ReadCard("MaxCmdAckDelay",      &fMaxCmdAckDelay,    'I', File);
    6564    ReadCard("DefaultResponse",      Response,           'f', File, m->NumBoards*kNumberOfChips*kNumberOfChannels);
     65    ReadCard("DefaultTarget",        Target,             'f', File, m->NumBoards*kNumberOfChips*kNumberOfChannels);
    6666    fclose(File);
    6767  }
     
    115115 
    116116  // Check for LED trigger channel on given channel and if feedback running
    117   if (FBMode==FB_Off || m->WaveForm[fLedTrigBoard][fLedTrigChip][fLedTrigChannel][fLedTrigSample] < fLedTrigThreshold)
     117  if (FBMode==FB_Off || m->WaveForm[fLedTrigBoard][fLedTrigChip][fLedTrigChannel][(fLedTrigSample+m->TriggerCell[fLedTrigBoard][fLedTrigChip])%kNumberOfBins] < fLedTrigThreshold)
    118118    return false;
    119119 
     
    123123      for (k=0; k<kNumberOfChannels; k++) {
    124124        for (Integral=0, q=-fIntHalfWidth; q<=(int) fIntHalfWidth; q++)
    125           Integral += (m->WaveForm[i][j][k][fLedSignalSample+q] - m->WaveForm[i][j][k][fLedBaselineSample+q])*m->BStruct[i].ScaleFactor;
     125          Integral += (m->WaveForm[i][j][k][(fLedSignalSample+q+m->TriggerCell[i][j])%kNumberOfBins] - m->WaveForm[i][j][k][(fLedBaselineSample+q+m->TriggerCell[i][j])%kNumberOfBins])*m->BStruct[i].ScaleFactor;
    126126        Integral /= 2*fIntHalfWidth+1;
    127127        Average[i][j][k] += Integral;
     
    129129      }
    130130
     131  // Check if acquired number of event requires action
    131132  if (++Count<NumAverages) return false;
    132133
    133   // Acquired number of event requires action
     134  // Make entry in slow data file
    134135  switch (FBMode) {
    135136    case FB_Active: SlowDataClass->NewEntry("Average");
     
    143144  }                     
    144145
     146  // Feedback action
    145147  for (i=m->FirstBoard; i<=m->LastBoard; i++) {
    146148    for (j=0; j<kNumberOfChips; j++) {
     
    153155            EffectiveGain = Gain*pow(0.5*(1+(Difference/Sigma[i][j][k]-1)/(Difference/Sigma[i][j][k]+1)),2);
    154156            Correction = -Difference*Response[i][j][k]*EffectiveGain;
     157            if (fabs(Correction) > 0.1) Correction = fabs(Correction)/Correction*0.1;   // Limit changes to 100 mV
    155158            if(Correction!=0 && Target[i][j][k]!=0 && !PixMap->DRS_to_Pixel(i,j,k).empty()) {
    156159              printf("Average of board %d, chip %d, channel %d is %.2f +/- %.2f    Correction %.3f\n",i,j,k,Average[i][j][k],Sigma[i][j][k],Correction);
     
    261264
    262265//
    263 // Set feedback mode and clear averages
     266// Get feedback mode
    264267//
    265268FBState HVFeedback::GetFBMode() {
     
    295298void HVFeedback::GetTargets() {
    296299  for (int i=m->FirstBoard; i<=m->LastBoard; i++)
    297     for (int j=0; j<kNumberOfChips; j++)
    298       for (int k=0; k<kNumberOfChannels; k++)
    299          m->PrintMessage("Board %d, chip %d, channel %d: %.2f\n",i,j,k,Target[i][j][k]);
     300    for (int j=0; j<kNumberOfChips; j++) {
     301      m->PrintMessage("Board %d, chip %d:",i,j);
     302      for (int k=0; k<kNumberOfChannels; k++) m->PrintMessage(" %.2f",Target[i][j][k]);
     303      m->PrintMessage("\n\r");
     304    }
    300305}
    301306
     
    305310void HVFeedback::MeasureResponse(float U) {
    306311
    307   if (U==0) m->PrintMessage("HV Feedback: Error, voltage difference must not non-zero.\n");
    308   else {
    309     for (int i=m->FirstBoard; i<=m->LastBoard; i++)
    310       for (int j=0; j<kNumberOfChips; j++)
    311         for (int k=0; k<kNumberOfChannels-2; k++) {
    312           if(PixMap->DRS_to_Pixel(i,j,k).empty()) m->PrintMessage("Could not find pixel ID of board %d, chip %d, channel %d\n",i,j,k);
    313           else WriteHVCommand("hvdiff %s %f\n",PixMap->DRS_to_Pixel(i,j,k).c_str(), -U/2);
    314         }
    315     DiffVoltage = U;
    316     FBMode = FB_ResponseFirst;
    317     ClearAverages(); 
    318     m->PrintMessage("HV Feedback: Decreasing voltages by %f for response measurement, acquiring data.\n",DiffVoltage/2);
    319   }
     312  if (U==0) {
     313    m->PrintMessage("HV Feedback: Error, voltage difference must not non-zero.\n");
     314    return;
     315  }
     316
     317  for (int i=m->FirstBoard; i<=m->LastBoard; i++)
     318    for (int j=0; j<kNumberOfChips; j++)
     319      for (int k=0; k<kNumberOfChannels; k++) {
     320        if(!PixMap->DRS_to_Pixel(i,j,k).empty()) {
     321          WriteHVCommand("hvdiff %s %f\n",PixMap->DRS_to_Pixel(i,j,k).c_str(), -U/2);
     322        }
     323      }
     324  DiffVoltage = U;
     325  FBMode = FB_ResponseFirst;
     326  ClearAverages(); 
     327  m->PrintMessage("HV Feedback: Decreasing voltages by %f for response measurement, acquiring data.\n",DiffVoltage/2);
    320328}
    321329
     
    324332//
    325333void HVFeedback::GetResponse() {
    326   for (int i=m->FirstBoard; i<=m->LastBoard; i++)
    327     for (int j=0; j<kNumberOfChips; j++)
    328       for (int k=0; k<kNumberOfChannels; k++)
    329          m->PrintMessage("Board %d, chip %d, channel %d: %.3f\n",i,j,k,Response[i][j][k]);
     334  for (int i=m->FirstBoard; i<=m->LastBoard; i++) {
     335    for (int j=0; j<kNumberOfChips; j++) {
     336      m->PrintMessage("Board %d, chip %d:",i,j);
     337      for (int k=0; k<kNumberOfChannels; k++) m->PrintMessage(" %.3f",Response[i][j][k]);
     338      m->PrintMessage("\n\r");
     339    }
     340  }
    330341}
    331342
     
    336347  char Textbuffer[MAX_COM_SIZE];
    337348  fd_set SelectDescriptor;
    338   int RetryCount=0;
    339  
    340   do {
    341     va_list ArgumentPointer;  va_start(ArgumentPointer, Format);
    342     vsnprintf(Textbuffer, sizeof(Textbuffer), Format, ArgumentPointer);
    343 
    344     // Write command to socket
    345     if(write(SocketDescriptor, Textbuffer, strlen(Textbuffer)+1)!=(int) strlen(Textbuffer)+1) {
    346       m->PrintMessage("Error: Could not write (entire) command to HV socket (%s)\n", strerror(errno));
    347       return false;
    348     }
    349 
    350     // Wait for command acknowledge from hvcontrol program
    351     FD_ZERO(&SelectDescriptor);   FD_SET(SocketDescriptor, &SelectDescriptor);
    352     struct timeval WaitTime = {fMaxCmdAckDelay, 0};
    353     if (select(((int) SocketDescriptor)+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
    354       m->PrintMessage("Error with select() in command acknowledge (%s)\n", strerror(errno));
    355       return false;
    356     }
    357     if (!FD_ISSET(SocketDescriptor, &SelectDescriptor)) { // Time-out expired
    358       m->PrintMessage("Time-out of %d seconds expired before receiving acknowledge from HV socket.\n", fMaxCmdAckDelay);
    359       return false;
    360     }
    361     if (read(SocketDescriptor, Textbuffer, MAX_COM_SIZE) == -1) {
    362       m->PrintMessage("Error reading acknowledge from HV socket (%s)\n", strerror(errno));
    363       return false;
    364     }
    365   } while (strstr(Textbuffer,"WC ok")==NULL && ++RetryCount<=MAX_RETRY);
    366   if(RetryCount==MAX_RETRY) {
    367     m->PrintMessage("Could not set high voltage after %d tries.\n", MAX_RETRY);
     349 
     350  va_list ArgumentPointer;  va_start(ArgumentPointer, Format);
     351  vsnprintf(Textbuffer, sizeof(Textbuffer), Format, ArgumentPointer);
     352
     353  // Write command to socket
     354  if(write(SocketDescriptor, Textbuffer, strlen(Textbuffer)+1)!=(int) strlen(Textbuffer)+1) {
     355    m->PrintMessage("Error: Could not write (entire) command to HV socket (%s)\n", strerror(errno));
     356    return false;
     357  }
     358
     359  // Wait for command acknowledge from hvcontrol program
     360  FD_ZERO(&SelectDescriptor);   FD_SET(SocketDescriptor, &SelectDescriptor);
     361  struct timeval WaitTime = {fMaxCmdAckDelay, 0};
     362  if (select(((int) SocketDescriptor)+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
     363    m->PrintMessage("Error with select() in command acknowledge (%s)\n", strerror(errno));
     364    return false;
     365  }
     366 
     367  // Evaluate response
     368  if (!FD_ISSET(SocketDescriptor, &SelectDescriptor)) { // Time-out
     369    m->PrintMessage("Time-out of %d seconds expired before receiving acknowledge from HV socket.\n", fMaxCmdAckDelay);
     370    return false;
     371  }
     372  if (read(SocketDescriptor, Textbuffer, MAX_COM_SIZE) == -1) { // Could not read
     373    m->PrintMessage("Error reading acknowledge from HV socket (%s)\n", strerror(errno));
     374    return false;
     375  }
     376  if (strncmp(Textbuffer, "OK", 2) != 0) { // ERROR response
     377    m->PrintMessage("Did not received OK from hvcontrol.\n");
    368378    return false;
    369379  }
Note: See TracChangeset for help on using the changeset viewer.