/*============================================================================= Name: meaENoload Abs: Measure the Klystron Energy E-Noload by doing an unweighted linear fit of the SLED output. Return Enoload and results that can be used to plot the fitted data. Args: Type Name Access Description ----------------- ----------- --------- ---------------------------- double [] scaled_data_a read-only waveform of scaled SLED output that we will fit double [] scaled_x_a read-only time in nanoseconds for waveform sample (850ns) int scaled_count read-only count of data points in scaled_data and scaled_x double fit_x_start read-only optional start Xunits for the linear fit double fit_x_end read-only optional (non-zero) end Xunits for the linear fit int sled_tuned read-only 1 indicates Klystron is SLED tuned double sigma_tol read-only sigma tolerance (SIGMA_TOL from IOC db) double scaled_data_lopr read-only LOPR of scaled data, used in EDM plot results double scaled_data_hopr read-only HOPR of scaled data, used in EDM plot results double scaled_data_egu read-only EGU of scaled data, used in EDM plot results int debug_flag read-only indicates whether to errlog diagnostic messages double * energy_gain_p write-only calculated energy gain (EACT) double * slope_p write-only Slope of linear fit (SLOPE) double * sigma_p write-only sigma of fit (SIGMA) double * energy_slope_p write-only fall time calculation from fit (ESLO) double * fitted_edge_p write-only right edge of fitted data (FITTED_EDGE) double * fitted_width_p write_only width of fitted data (FITTED_WIDTH) double [] fitted_data_a write-only results as used by William's EDM plots (FITTED_DATA) double [] fitted_x_a write-only results as used by William's EDM plots (FITTED_X) int fitted_count read-only count of fitted_data_a and fitted_x_a (size of fitted arrays) char * cursor_string_p write-only "Cursor =" string on EDM plot (CURSOR_STRING) int cursor_string_size read-only size in bytes of *cursor_string_p double [] cursor_y_a write-only range of y-axis (CURSOR_Y) [0..1] double [] cursor_x_a write-only range of x-axis (CURSOR_X) [0..1] double [] window_y_a write-only range of y-axis (WINDOW_Y) [0..1] double [] window_x_a write-only range of x-axis (WINDOW_X) [0..1] Rem: Measure the Klystron Energy E-Noload by doing a linear fit of the SLED output. Return Enoload and results that can be used to plot the fitted data. Use gsl_multifit_linear (gnu scientific library) to fit with an unweighted linear fit. Side: if debugFlag is nonzero, prints diagnostic messages (using errlogPrintf). Ret: Integer indicating status of measuring Energy E Noload =============================================================================*/ #include // memset prototype #include // sprintf #include // errlogPrintf #include // std #include // sleep #include "mulparmlinfit.h" // MultiParamLinearFit int meaENoload (double scaled_data_a[], double scaled_x_a[], int scaled_count, double fit_x_start, double fit_x_end, int sled_tuned, double sigma_tol, double scaled_data_lopr, double scaled_data_hopr, const char *scaled_data_egu, int debug_flag, double *energy_gain_p, double *slope_p, double *sigma_p, double *energy_slope_p, double *fitted_edge_p, double *fitted_width_p, double fitted_data_a[], double fitted_x_a[], int fitted_count, char cursor_string_p[], int cursor_string_size, double cursor_y_a[], double cursor_x_a[], double window_y_a[], double window_x_a[] ) { #define DEGREE_OF_FIT 2 // we are doing a linear fit #define MIN(a,b) ((ab) ? a : b) #define interpolate(X0, X, X1, Y0, Y1) ((0 == ((X1) - (X0))) ? (Y0) : ((Y0) + (((X) - (X0)) * (((Y1) - (Y0))/((X1) - (X0)))))) #define ABS(a) (((a)<0) ? (-1.0 * (a)) : (a)) #define SIGN(a,b) (((b)<0) ? (-1.0 * ABS(a)) : (ABS(a))) #define MIN_FIT_POINTS 6 // minimun number of points that we need to do a fit /* local variables */ int iss; // return status of executing this function int min_x; // min index on x-axis to look for area to fit int max_x; // max index on x-axis to look for area to fit int i; // index of for loop int num_points; // number of points in area to fit double y_fullscale; double t_psk_xaxis; // Usec start of sample time double t_psk_peak; double peak; int b_peak; int b_half; double y_psk_peak; double r_1; double r_2; double m_rise; int lo_bin; int hi_bin; double fourteen; int sleep_time = 1; // sleep time used after debug print statements /* variables to adjust for nsec uTCA vs usec PIOP */ int min_signal_range; // we expect the peak to be within the middle of the sample data int max_signal_range; int half_peak_bins; // within how many bins do we expect to find the half peak int rise_time; // expected rise time int fit_requested_range_fl; // flag to indicate if we fit the requested range (or use fall time to find data to fit) double start_fill_time; // x-locn of start of fill time double end_fill_time; // x_locn of end of fill time char xunits; // assume nseconds /********************** resolve these entries: for now copy values from KLYS:LI28 Are these variables needed for the new hardware? ************************/ double pado = -43; // PAD Offset? force to -43 for now double ECVT[3] = {0.039296,20,1}; // fake values so I can test double PLEN = 0.000003301; // copy from linac 28-2 for testing /* For fit.c */ int ndata; // number of data points that we will fit double x_step; // size of x-axis time step double fit_results[5]; // the results of the fit double fit_offset; // intercept of linear fit /*************************************************************/ if (debug_flag) { errlogPrintf("MeaENoLoad: Starting SLEDtuned=%d and waveform size = %d; first 5 values are: %f,%f,%f,%f,%f\n", sled_tuned,scaled_count,scaled_data_a[0], scaled_data_a[1],scaled_data_a[2],scaled_data_a[3],scaled_data_a[4] ); } /* initialize results before fitting the data */ /* doubles are 8 bytes per element */ memset (fitted_data_a, 0, fitted_count*8); memset (fitted_x_a, 0, fitted_count*8); memset (cursor_y_a, 0, 2*8); memset (cursor_x_a, 0, 2*8); memset (window_y_a, 0, 2*8); memset (window_x_a, 0, 2*8); *cursor_string_p = '\0'; *energy_gain_p = 0; *slope_p = 0; *sigma_p = 0; *energy_slope_p = 0; *fitted_edge_p = 0; *fitted_width_p = 0; /* find the x-axis range of where look for the peak that we will fit */ if (fit_x_end == 0) { min_x = 0; max_x = scaled_count-1; /* use all the data points */ fit_requested_range_fl = 0; /* no range requested, so look for data to fit */ } else { /* user specified what portion of data to find peak & fit * * find bins of data that we will fit */ x_step = ABS(scaled_x_a[1] - scaled_x_a[0]); min_x = fit_x_start/x_step; max_x = fit_x_end/x_step; fit_requested_range_fl = 1; /* range requested, telling us which bins to fit */ } num_points = max_x - min_x + 1; if ((num_points < MIN_FIT_POINTS) || (num_points > scaled_count)) { /* must have a minimum number of points to fit */ if (debug_flag) errlogPrintf("MeaENoLoad: not enough points %d for min = %d, max = %d OR too many points for scaled_count = %d\n", num_points, min_x, max_x, scaled_count ); iss = 0; goto error_egress; } /* for now, use same formula as for PIOP Klystrong */ // y_fullscale = sqrt(MAX(.0001,ECVT[0])) * ECVT[1] * ECVT[2] * 100.0; // for uTCA LLRF, set y_fullscale to 14% higher than sample peak y_fullscale = 22200; if (debug_flag) { errlogPrintf("MeaENoLoad: Look for Peak over %d points from %d to %d with Yfullscale =%f\n", num_points,min_x,max_x,y_fullscale); } if (sled_tuned) { // figure out when psk came // find the peak bin in our data set. note that b_peak=0 for no data. peak = 0; b_peak = 0; for (i = min_x; i <= max_x; i++) if (scaled_data_a[i] > peak) { peak = scaled_data_a[i]; b_peak = i; } // note that finding the peak between bins 7 & 50 assumes 64 data points from the if ((fit_x_end != 0) && (fit_x_end < 64)) { min_signal_range = 7; max_signal_range = 50; half_peak_bins = 5; rise_time = 0.100; // 100 ns rise time start_fill_time = -0.825; // us end_fill_time = 0; xunits = 'u'; } else { min_signal_range = 50; max_signal_range = 450; half_peak_bins = 45; rise_time = 8.4; // rise time start_fill_time = fit_x_start; // nsec. We don't have the beam fill time, so use requested start/end time end_fill_time = fit_x_end; pado = ( (start_fill_time + (end_fill_time - start_fill_time)/2) ) * 119.0; xunits = 'n'; } if ((peak < y_fullscale/4) || (b_peak < min_signal_range) || (b_peak > max_signal_range)) { /// force b_peak to be in the middle of the data: b_peak = MAX(MIN(b_peak, max_signal_range), min_signal_range); y_psk_peak = peak; // this is the best we can do w/ bad data if (debug_flag) errlogPrintf("MeaENoLoad: waveform peak not found. y_fullscale/4 = %f peak =%f at x-axis=%d \n", y_fullscale/4, peak, b_peak); iss = 0; goto error_egress; } else { // peak ok b_half = b_peak; // best guess // find the bin whose value is close to peak/2. we do this by finding a // bin whose n point is > half and n-1 is < half. choose the [n-1|n] closest to half. for (i = b_peak; i >= (b_peak-half_peak_bins); i=i-1) { r_1 = scaled_data_a[i-1] - peak/2; r_2 = scaled_data_a[i] - peak/2; if ((r_1 < 0) && (r_2 >= 0)) { if (ABS(r_1) < ABS(r_2)) { b_half = i-1; } else { b_half = i; } break; } } // from the peak, predict our leading edge slope (units/time) m_rise = peak / rise_time; // extrapolate to find the x-axis intercept and peak time t_psk_xaxis = scaled_x_a[b_half] - scaled_data_a[b_half] / m_rise; t_psk_peak = t_psk_xaxis + rise_time; } // peak ok } // sled_tuned else { // not sled_tuned t_psk_xaxis = start_fill_time - 0.100; // us start of sample time t_psk_peak = start_fill_time; b_half = 1; // this has no real meaning, but is used later. } // not sled_tuned if (debug_flag) { errlogPrintf("MeaENoLoad: peak=%f b_peak=%d b_half=%d\n", peak, b_peak, b_half); } if (fit_requested_range_fl) { lo_bin = min_x; hi_bin = max_x; } else { // figure out which points fall on the falling edge of the sled waveform. // or are the sample points for non-sleded operation. same critera. // critera are: // a) assume 100 ns rise time // b) no points within 50 ns of the peak (pad doesn't measure these well // c) no points beyond 825 ns of peak // d) no points which saturated the adc. (<= 92.5% full scale) r_1 = t_psk_xaxis + rise_time + .050; r_2 = t_psk_xaxis + rise_time + .825; for (i = b_half; i= (y_fullscale*.925)) lo_bin = i+1; } } // make sure there are at least 6 points left. (400 ns of data) if ((hi_bin - lo_bin) < MIN_FIT_POINTS) { errlogPrintf("MeaENoLoad: unable to find rise/fall from peak.\n"); if (debug_flag) { errlogPrintf("MeaENoLoad: find fall - r1=%f r2=%f hi_bin=%d, low_bin=%d\n", r_1, r_2, hi_bin, lo_bin); iss = 0; goto error_egress; } } else { // rise fall peak ok // find the slope and sigma ndata = hi_bin - lo_bin + 1; *slope_p = 0; *sigma_p = 0; if (debug_flag) { errlogPrintf("MeaENoLoad about to fit using gsl_multifit_linear, ndata=%d\n", ndata); for (i=lo_bin; i< (lo_bin+6); i++) { errlogPrintf("MeasENoLoad: scaled_x_a[%d]=%f scaled_data_a[%d]=%f\n", i, scaled_x_a[i], i, scaled_data_a[i]); sleep(sleep_time); } } MultiParamLinearFit(ndata, DEGREE_OF_FIT, &scaled_x_a[lo_bin], &scaled_data_a[lo_bin], fit_results); *slope_p = fit_results[1]; //a1 fit_offset = fit_results[0]; //a0 if (debug_flag) { errlogPrintf("MeaENoLoad MultiParamLinearFit.c done fit results = %f %f \n", fit_results[0], fit_results[1]); } // sled - we want the peak, else the "average" if (sled_tuned) { y_psk_peak = fit_offset + (*slope_p * t_psk_peak); } else { y_psk_peak = fit_offset + (*slope_p * (t_psk_peak + .412)); } // verify the data. the fall time should be of order 1.7 us for sled, greater than 10 for non-sled. *energy_slope_p = -1.0 * SIGN((y_psk_peak / MAX(ABS(*slope_p), .0001)), *slope_p); if (debug_flag) errlogPrintf("MeaENoLoad: Checking Fall Times calculation = %f\n", *energy_slope_p); if ( (fit_requested_range_fl == 0) && ( ((sled_tuned) && (ABS(*energy_slope_p - 1.7) > 0.5)) || ((!sled_tuned) && (ABS(*energy_slope_p) < 10)) ) ) { errlogPrintf("MeaENoLoad: analysis fall time unreasonable = %f \n", *energy_slope_p); iss = 0; goto error_egress; } else { // fall time reasonable // and sigma should be very good if ((*sigma_p/MAX(.1,y_psk_peak)) > sigma_tol) { errlogPrintf("MeaENoLoad: analysis sigma unreasonable, sigma=%f y_psk+peak=%f calc=%f > %f\n", *sigma_p, y_psk_peak, (*sigma_p/MAX(.1,y_psk_peak)), sigma_tol); iss = 0; goto error_egress; } else { // sigma ok // calculate the no-load energy gain. we do this as follows: // sled: // john sheppard reports that the no-load energy gain of a // typical non-sledded station is ~20 times the sqrt(power), // and about e4.6 times the peak power (sqrt). the constant ~20 // is station geometry dependent, and for now, we assume that the // ratio of 14... to 20 is constant. by now, the result...ydata(n) // contains the instantious power (the 20 number) from the station. // we compute the ~14.n number (pulse length dependent) from the, // and scale dependent on the ~20 (list.ecvt(2)) value. from the peak // we get the no-load gain. // non-sled // john reports that the "y_psk_peak" (which for non-sled, is the // 825ns average) is multiplied by the 20 number (above). (note: // data has been pre-scaled wrt to the 20 number.) // john reported values of the constant 14 as a function of plen in a // memo dated 1/27/86 "peak rf power to energy conversion". the following // fit computes 14 to a sigma of 0.0027 over the range of 2.5 to 5 us. if (sled_tuned) { fourteen = 13.070 + 0.89678 * PLEN*1.e6 - 0.17678 * pow((PLEN*1.e6),2) + 0.012623 * pow((PLEN*1.e6),3); *energy_gain_p = y_psk_peak * fourteen / 20; } else { *energy_gain_p = y_psk_peak; } if (debug_flag) { errlogPrintf("MeaENoLoad: eact=%f eslo=%f y_psk_peak=%f t_psk_peak=%f m_rise=%f\n", *energy_gain_p, *energy_slope_p, y_psk_peak, t_psk_peak, m_rise); } if (fitted_count < 4) { errlogPrintf("MeaENoLoad: Fitted count %d < 4 is a failure\n",fitted_count); iss = 0; goto error_egress; } /* show fit - edm plot draws GREEN hatches */ fitted_x_a[0] = t_psk_xaxis + rise_time; fitted_data_a[0] = scaled_data_lopr; fitted_x_a[1] = t_psk_xaxis + rise_time; fitted_data_a[1] = y_psk_peak; fitted_x_a[2] = t_psk_xaxis + ABS(end_fill_time) - ABS(start_fill_time) + rise_time; fitted_data_a[2] = y_psk_peak + (ABS(end_fill_time) - ABS(start_fill_time)) * *slope_p; fitted_x_a[3] = fitted_x_a[2]; fitted_data_a[3] = scaled_data_lopr; /* save the fitted edge and width */ *fitted_edge_p = fitted_x_a[3]; *fitted_width_p = fitted_x_a[3] - fitted_x_a[1]; // to make display look nice, we'll draw the last point repeatedly if (fitted_count >= 5) for (i=4; i= pado/119.0) { sprintf (cursor_string_p, "cursor = %.3f %s at %.4f ", interpolate (scaled_x_a[i-1], pado/119.0, scaled_x_a[i], scaled_data_a[i-1], scaled_data_a[i]), scaled_data_egu, pado/119.0); break; // exit for loop after we find the first x-axis value > PADO & set value of cursor_string } /* set values to plot PADO offset time. EDM plot uses BROWN line*/ cursor_x_a[0] = pado/119.0; cursor_x_a[1] = cursor_x_a[0]; cursor_y_a[0] = scaled_data_lopr; cursor_y_a[1] = scaled_data_hopr; /* Fill time code is reused from linac upgrade where they didn't really know how to calculate the "fill" time. * SCP uses a function called tnominal given micro & beam code {} */ window_y_a[0] = scaled_data_lopr; window_y_a[1] = scaled_data_lopr; window_x_a[0] = start_fill_time; /* us */ window_x_a[1] = end_fill_time; /* show success */ iss = 1; error_egress: return iss; }