Changeset 99 for drsdaq/HVFeedback.cc
- Timestamp:
- 08/12/09 12:01:25 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
drsdaq/HVFeedback.cc
r92 r99 16 16 #include <signal.h> 17 17 18 #define MAX_RETRY 519 18 #define PIXMAP_LOCATION "../config/PixelMap.txt" 20 19 … … 64 63 ReadCard("MaxCmdAckDelay", &fMaxCmdAckDelay, 'I', File); 65 64 ReadCard("DefaultResponse", Response, 'f', File, m->NumBoards*kNumberOfChips*kNumberOfChannels); 65 ReadCard("DefaultTarget", Target, 'f', File, m->NumBoards*kNumberOfChips*kNumberOfChannels); 66 66 fclose(File); 67 67 } … … 115 115 116 116 // 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) 118 118 return false; 119 119 … … 123 123 for (k=0; k<kNumberOfChannels; k++) { 124 124 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; 126 126 Integral /= 2*fIntHalfWidth+1; 127 127 Average[i][j][k] += Integral; … … 129 129 } 130 130 131 // Check if acquired number of event requires action 131 132 if (++Count<NumAverages) return false; 132 133 133 // Acquired number of event requires action134 // Make entry in slow data file 134 135 switch (FBMode) { 135 136 case FB_Active: SlowDataClass->NewEntry("Average"); … … 143 144 } 144 145 146 // Feedback action 145 147 for (i=m->FirstBoard; i<=m->LastBoard; i++) { 146 148 for (j=0; j<kNumberOfChips; j++) { … … 153 155 EffectiveGain = Gain*pow(0.5*(1+(Difference/Sigma[i][j][k]-1)/(Difference/Sigma[i][j][k]+1)),2); 154 156 Correction = -Difference*Response[i][j][k]*EffectiveGain; 157 if (fabs(Correction) > 0.1) Correction = fabs(Correction)/Correction*0.1; // Limit changes to 100 mV 155 158 if(Correction!=0 && Target[i][j][k]!=0 && !PixMap->DRS_to_Pixel(i,j,k).empty()) { 156 159 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); … … 261 264 262 265 // 263 // Set feedback mode and clear averages266 // Get feedback mode 264 267 // 265 268 FBState HVFeedback::GetFBMode() { … … 295 298 void HVFeedback::GetTargets() { 296 299 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 } 300 305 } 301 306 … … 305 310 void HVFeedback::MeasureResponse(float U) { 306 311 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); 320 328 } 321 329 … … 324 332 // 325 333 void 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 } 330 341 } 331 342 … … 336 347 char Textbuffer[MAX_COM_SIZE]; 337 348 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"); 368 378 return false; 369 379 }
Note:
See TracChangeset
for help on using the changeset viewer.