Changeset 14174 for fact


Ignore:
Timestamp:
06/13/12 15:44:01 (12 years ago)
Author:
ogrimm
Message:
Fixed hanging signal handler in Evidence before abort() was called. Added command to determine dynamic range to FADctrl.
Location:
fact
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • fact/Evidence/Evidence.cc

    r12910 r14174  
    360360  // If invoked twice, call exit()
    361361  if (Count == 2) {
    362         This->Message(WARN, "Signal handler called twice, invoking exit() (signal %d)", Signal);
     362        This->Message(WARN, "Signal handler called twice, invoking exit(). Will abort() on next signal without message. (signal %d)", Signal);
    363363        exit(EXIT_FAILURE);
    364364  }
    365365
    366366  // If invoked more than twice, call abort()
    367   This->Message(WARN, "Signal handler called %d times, invoking abort(). Good bye. (signal %d)", Count, Signal);
     367  // Does not use Message() again, as this might be blocked
    368368  abort();
    369369}
  • fact/Evidence/readme.txt

    r12940 r14174  
    6565                        Edd much faster now. Made a new version Edd:LP for La Palma. History server publishes a list of
    6666                        services subscribed to.
     6713/6/2012       Removed calling Message() before abort() in signal handler, as Message() might get stuck
  • fact/FADctrl/FAD.cc

    r12891 r14174  
    617617void FAD::cmd_dynrange() {
    618618
    619   // Set number of events required for measurement
    620   if (Parameter.size()==1 || !ConvertToInt(Parameter[1], &NumEventsRequested) || NumEventsRequested<=0) {
    621         NumEventsRequested = DEFAULT_NUM_CALIB_EVENTS;
    622   }
    623 
    624   // Start calibration by setting mode
    625619  Mode = dynrange;
    626   Message(INFO, "Starting determination of dynamic range with %d events", NumEventsRequested);
     620  Message(INFO, "Starting determination of dynamic range");
    627621}
    628622
     
    667661        else if (Mode == acalib) PrintMessage("Current mode is ACALIB (3x%d events, slowest board %d has %d events)\n", NumEventsRequested, SlowestBoard, MinCount);
    668662        else if (Mode == datarun) PrintMessage("Current mode is DATARUN (%d events requested, %d events taken)\n", NumEventsRequested, NumEvents);
    669         else if (Mode == dynrange) PrintMessage("Current mode is DYNRANGE (%d events requested)\n", NumEventsRequested);
     663        else if (Mode == dynrange) PrintMessage("Current mode is DYNRANGE\n");
    670664        return;
    671665  }   
     
    957951
    958952        // If amplitude calibration mode, check if all boards finished procedure
    959         if (Mode == acalib) {
     953        if (Mode == acalib || Mode == dynrange) {
    960954          bool Done = true;
    961955          for (unsigned int i=0; i<Boards.size(); i++) {
     
    965959          // Amplitude calibration finished?
    966960          if (Done) {
    967             SaveAmplitudeCalibration();
     961            if (Mode == acalib) {
     962                  SaveAmplitudeCalibration();
     963                  Message(INFO, "Amplitude calibration done, mode set to IDLE");
     964                }
     965            if (Mode == dynrange) {
     966                  Message(INFO, "Dynamic range measurement done, mode set to IDLE");
     967                }               
    968968                Mode = idle;
    969                 Message(INFO, "Amplitude calibration done, mode set to IDLE");
    970969          }
    971970        }
  • fact/FADctrl/FADBoard.cc

    r12891 r14174  
    168168  unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0, htons(CMD_Execute) };
    169169  string Message = string("ACALIBDONE")+Name+"\n";
    170 
     170 
    171171  switch (State) {
     172  // *******************************************************************
     173  // ******************   AMPLITUDE CALIBRATION    *********************   
     174  // *******************************************************************
     175
    172176  // ====== Part A: Check if amplitude calibration should start and initialise =====
    173177  case standbye:
    174         if (m->Mode != m->acalib) break;
    175 
    176         // Invalidate current calibration
    177         ACalib.Time = -1;
    178         Count = 0;
     178        if (m->Mode != m->acalib && m->Mode != m->dynrange) break;
    179179
    180180        // Save initial board status, set all ROIs to 1024 and set DAC values (no triggers while setting ROI)
     
    188188        Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
    189189
    190         DACCmd[1] = htons(0);
    191         DACCmd[3] = htons(0);
    192         DACCmd[5] = htons(0);
    193         Send(DACCmd, sizeof(DACCmd));
    194 
    195         // Clear sum vector and set state to accumulate
    196         memset(Sum, 0, sizeof(Sum));
    197         State = baseline;
    198         SetStatus("Starting calilbration");
     190        if (m->Mode == m->acalib) {
     191          // Set DAC to zero
     192          DACCmd[1] = htons(0);
     193          DACCmd[3] = htons(0);
     194          DACCmd[5] = htons(0);
     195          Send(DACCmd, sizeof(DACCmd));
     196
     197          // Invalidate current calibration
     198          ACalib.Time = -1;
     199          Count = 0;
     200
     201          // Clear sum vector and set state to accumulate
     202          memset(Sum, 0, sizeof(Sum));
     203          State = baseline;
     204          SetStatus("Starting calilbration");
     205        }
     206
     207        if (m->Mode == m->dynrange) {
     208          // No amplitude calibration allowed!
     209          DAC_DR = 0;
     210          Delta_DAC = 1000;
     211          State = setdac;
     212        }
     213       
    199214        break;
    200215
     
    317332        for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
    318333
    319         // Inform event thread that calibration is finished for this board
    320         if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
    321           m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
    322         }
    323 
    324         SetStatus("Finished calibration");
    325         State = cleanup;
    326         break;
    327 
    328   // ====== Part E: Write back original ROI and DAC settings =====
    329   case cleanup:
    330     // ROI values
    331 
    332         ROICmd.clear();
    333         for (unsigned int i=0; i<NChips*NChannels; i++) {
    334           ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
    335           ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
    336         }
    337         ROICmd.push_back(htons(CMD_Execute));
    338         Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
    339 
    340         // DAC values
    341         DACCmd[1] = htons(InitialStatus.DAC[1]);
    342         DACCmd[3] = htons(InitialStatus.DAC[2]);
    343         DACCmd[5] = htons(InitialStatus.DAC[3]);
    344         Send(DACCmd, sizeof(DACCmd));
    345        
    346334        // Update DIM service with calibration information
    347335        for (unsigned int i=0; i<NChips; i++) {
     
    356344        DIM_ACalData->updateService(ACalData, 3*NChips*NChannels*NBins*sizeof(float));
    357345
     346        SetStatus("Finished calibration");
     347        State = cleanup;
     348        break;
     349
     350  // ====== Part E: Write back original ROI and DAC settings =====
     351  case cleanup:
     352    // ROI values
     353
     354        ROICmd.clear();
     355        for (unsigned int i=0; i<NChips*NChannels; i++) {
     356          ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
     357          ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
     358        }
     359        ROICmd.push_back(htons(CMD_Execute));
     360        Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
     361
     362        // DAC values
     363        DACCmd[1] = htons(InitialStatus.DAC[1]);
     364        DACCmd[3] = htons(InitialStatus.DAC[2]);
     365        DACCmd[5] = htons(InitialStatus.DAC[3]);
     366        Send(DACCmd, sizeof(DACCmd));
     367
     368        // Inform event thread that calibration is finished for this board
     369        if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
     370          m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
     371        }
     372
     373        SetStatus("Cleaning up");       
    358374        State = wait;
    359375        break;
     
    363379        if (m->Mode == m->idle) State = standbye;
    364380        break;
    365   }
    366 
     381 
     382 
     383  // ************************************************************************
     384  // ******************   DYNAMIC RANGE DETERMINATION   *********************   
     385  // ************************************************************************
     386 
     387  // ====== Set calibration DACs 1-3 =====
     388  case setdac:
     389        // Check for stopping
     390        if (m->Mode != m->dynrange) {
     391          State = cleanup;
     392          break;
     393        }
     394
     395        // Set new DAC values
     396        DACCmd[1] = htons(DAC_DR);
     397        DACCmd[3] = htons(DAC_DR);
     398        DACCmd[5] = htons(DAC_DR);
     399        Send(DACCmd, sizeof(DACCmd));
     400
     401        SetStatus("Dynamic range: DACs 1-3 at %u", DAC_DR);
     402        State = measure;
     403        break;
     404
     405  // ====== Determine mean and sigma =====
     406  case measure:
     407        // Check for stopping
     408        if (m->Mode != m->dynrange) {
     409          State = cleanup;
     410          break;
     411        }
     412
     413        // Check if current event has correct DAC values
     414    if (Status.DAC[1] != DAC_DR || Status.DAC[2] != DAC_DR || Status.DAC[3] != DAC_DR) break;
     415
     416        // Discard first few events with correct DAC setting (can still have wrong voltage)
     417        if (Count_DR++ < 2) break;
     418        Count_DR = 0;
     419       
     420        bool Clip;
     421
     422        // Evaluate current event for all channels
     423        for (unsigned int Chip=0; Chip<NChips; Chip++) {
     424          for (unsigned int Chan=0; Chan<NChannels; Chan++) {
     425            Clip = false;
     426                Mean[Chip][Chan] = 0;
     427               
     428                // Determine mean value and check if CLIP_LEVEL exceeded
     429                for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
     430                  Mean[Chip][Chan] += Data[Chip][Chan][i] / Status.ROI[Chip][Chan];
     431                  if (abs(Data[Chip][Chan][i]) >= CLIP_LEVEL) Clip = true;
     432                }
     433          }
     434        }
     435       
     436        // If clipping occurred, continue to increase/decrease DAC until at 16-bit limit
     437        if (Clip) {
     438          if (DAC_DR + Delta_DAC < 0 || DAC_DR + Delta_DAC > 65535) State = cleanup;
     439          else {
     440                DAC_DR += Delta_DAC;
     441                State = setdac;
     442          }
     443          break;
     444        }
     445
     446        // Start again from maximum DAC value downwards
     447        if (Delta_DAC > 0) {
     448          for (unsigned int Chip=0; Chip<NChips; Chip++) {
     449                for (unsigned int Chan=0; Chan<NChannels; Chan++) Mean_low[Chip][Chan] = Mean[Chip][Chan];
     450          }
     451          DAC_low = DAC_DR;
     452          DAC_DR = 65535;
     453          Delta_DAC = -1000;
     454          State = setdac;
     455          break;
     456        }
     457
     458        // Procedure finished
     459        double Sigma;
     460        printf("Extrapolated DAC values for range with 1-sigma\nclearance to ADC count level +/-%u\n", CLIP_LEVEL);
     461
     462        for (unsigned int Chip=0; Chip<NChips; Chip++) {
     463          for (unsigned int Chan=0; Chan<NChannels; Chan++) {
     464         
     465            // Calculate baseline sigma for current event
     466                Sigma = 0;
     467                for (int i=0; i<Status.ROI[Chip][Chan]; i++) Sigma += pow(Data[Chip][Chan][i] - Mean[Chip][Chan], 2);
     468                if (Status.ROI[Chip][Chan] > 1) Sigma = sqrt(Sigma / (Status.ROI[Chip][Chan] - 1));
     469
     470                // Extrapolate to find DAC values corresponding to 1-sigma clearance to CLIP_LEVEL
     471                DR_low[Chip][Chan] = DAC_low - (Mean_low[Chip][Chan] + (CLIP_LEVEL-Sigma))* (DAC_DR-DAC_low) / (Mean[Chip][Chan]-Mean_low[Chip][Chan]);
     472                DR_high[Chip][Chan] =  DAC_DR + (CLIP_LEVEL - Sigma - Mean[Chip][Chan]) * (DAC_DR-DAC_low) / (Mean[Chip][Chan]-Mean_low[Chip][Chan]);
     473                 
     474                printf("Chip %u, chan %u:  DAC Min %6d   Max %d   Range %6d\n", Chip, Chan, DR_low[Chip][Chan], DR_high[Chip][Chan], DR_high[Chip][Chan]-DR_low[Chip][Chan]); 
     475          }
     476        }
     477         
     478        State = cleanup;
     479        break;
     480  }
     481}
    367482 
    368483//
     
    723838  }
    724839}
    725 
    726 
    727 
    728 //
    729 // Dynamic range determination
    730 //
    731 void FADBoard::DynamicRange() {
    732 
    733   #if 0
    734   vector<unsigned short> ROICmd;
    735   unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0, htons(CMD_Execute) };
    736   string Message = string("ACALIBDONE")+Name+"\n";
    737 
    738   switch (StateDR) {
    739   // ====== Check if dynamic range measurement should start and initialise =====
    740   case standbye:
    741         if (m->Mode != m->dynrange) break;
    742 
    743         // Save initial board status, and set all ROIs to 1024
    744         InitialStatus = GetStatus();
    745 
    746         for (unsigned int i=0; i<NChips*NChannels; i++) {
    747           ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
    748           ROICmd.push_back(htons(NBins));
    749         }
    750         ROICmd.push_back(htons(CMD_Execute));
    751         Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
    752 
    753         // Start measurement
    754         DAC_DR = 0;
    755         StateDR = setdac;
    756         SetStatus("Starting dynamic range measurement");
    757         break;
    758 
    759   // ====== Set calibration DAC =====
    760   case setdac:
    761         // Check for stopping
    762         if (m->Mode != m->dynrange) {
    763           StateDR = cleanup;
    764           break;
    765         }
    766 
    767         if (DAC_DR > 66000) {
    768           StateDR = cleanup;
    769           break;
    770         }
    771 
    772         // Set new DAC values
    773         DACCmd[1] = htons(DAC_DR);
    774         DACCmd[3] = htons(DAC_DR);
    775         DACCmd[5] = htons(DAC_DR);
    776         Send(DACCmd, sizeof(DACCmd));
    777 
    778         StateDR = measure;
    779         break;
    780 
    781   // ====== Determine mean and sigma =====
    782   case measure:
    783         // Check for stopping
    784         if (m->Mode != m->dynrange) {
    785           StateDR = cleanup;
    786           break;
    787         }
    788 
    789         Mean = 0;
    790         Sigma = 0;
    791         for (unsigned int Chip=0; Chip<NChips; Chip++) {
    792           for (unsigned int Chan=0; Chan<NChannels; Chan++) {
    793                 for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
    794                   Mean += Data[Chip][Chan][i];
    795                   Sigma += Data[Chip][Chan][i]*Data[Chip][Chan][i];
    796                 }
    797                 Mean /= Status.ROI[Chip][Chan];
    798                 Sigma = sqrt(Status.ROI[Chip][Chan]/(Status.ROI[Chip][Chan]-1)*(Sigma/Status.ROI[Chip][Chan]-Mean*Mean));               
    799           }
    800         }
    801        
    802         DAC_DR += 1000;
    803 
    804         StateDR = setdac;
    805         break;
    806 
    807   // ====== Write back original ROI and DAC settings =====
    808   case cleanup:
    809     // ROI values
    810         ROICmd.clear();
    811         for (unsigned int i=0; i<NChips*NChannels; i++) {
    812           ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
    813           ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
    814         }
    815         ROICmd.push_back(htons(CMD_Execute));
    816         Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
    817 
    818         // DAC values
    819         DACCmd[1] = htons(InitialStatus.DAC[1]);
    820         DACCmd[3] = htons(InitialStatus.DAC[2]);
    821         DACCmd[5] = htons(InitialStatus.DAC[3]);
    822         Send(DACCmd, sizeof(DACCmd));
    823        
    824         StateDR = wait;
    825         break;
    826 
    827   // ====== Wait for Mode not being idle =====
    828   case wait:
    829         if (m->Mode == m->idle) StateDR = standbye;
    830         break;
    831   }
    832   #endif
    833 
  • fact/FADctrl/FADBoard.h

    r12891 r14174  
    9696        void Lock();
    9797        void Unlock();
    98         void DynamicRange();
    9998
    10099        unsigned short Port;
     
    110109
    111110  private:
    112         enum StateType {standbye, baseline, gain, secondary, cleanup, wait};
     111        enum StateType {standbye, baseline, gain, secondary, cleanup, wait, setdac, measure};
     112        StateType State;
     113
    113114        struct BoardStatus InitialStatus;
    114         StateType State;
     115        uint16_t DAC_DR, DAC_low;
     116        int16_t Delta_DAC;
     117        unsigned int Count_DR;
     118    static const short CLIP_LEVEL = 2000;
     119        double Mean[NChips][NChannels], Mean_low[NChips][NChannels];
     120        int DR_low[NChips][NChannels], DR_high[NChips][NChannels];
    115121};
    116122
  • fact/FADctrl/History.txt

    r11385 r14174  
    353524/6/2011       PLL lock status published as DIM service
    36367/7/2011        Fixed error in setting the active boards in EventThread()
    37 13/7/2911   FAD board time published as DIM service
     3713/7/2011   FAD board time published as DIM service
     3813/6/2012   New command to determine dynamic range using the calibration DACs
Note: See TracChangeset for help on using the changeset viewer.