예제 #1
0
	def makeLinearFit(self):
		self.phase_fit_gd.removeAllPoints()	
		self.derivative = 0.
		if(self.phase_gd.getNumbOfPoints() > 1):
			GraphDataOperations.polynomialFit(self.phase_gd,self.phase_fit_gd,1)
			self.phase_fit_gd.setGraphColor(Color.RED)
			nP = self.phase_fit_gd.getNumbOfPoints()
			self.derivative = 0.
			if(nP > 1):
				X0 = self.phase_fit_gd.getX(0)
				X1 = self.phase_fit_gd.getX(nP-1)
				self.derivative = self.phase_fit_gd.getValueDerivativeY((X0+X1)/2.0)
			return true
		return false
def calculateEneregyVsPhase(cav_wrapper,scl_long_tuneup_controller,bpm_wrappers_good_arr):
	# This function will calculate output energy vs. cav. phase by using known BPM offsets
	# It will return false if the cavity cannot be analysed 
	eKin_in = cav_wrapper.eKin_in	
	# make cav_wrapper.energy_guess_harm_funcion harmonic function
	res = makeEnergyGuessHarmFunc(eKin_in,cav_wrapper,scl_long_tuneup_controller)
	if(not res): return false
	mass = scl_long_tuneup_controller.mass/1.0e+6			
	c_light = scl_long_tuneup_controller.c_light	
	bpm_freq = scl_long_tuneup_controller.bpm_freq
	coeff_init = 360.0*bpm_freq/c_light
	phaseDiffPlot = cav_wrapper.phaseDiffPlot
	cav_wrapper.eKinOutPlot.removeAllPoints()
	cav_wrapper.eKinOutPlotTh.removeAllPoints()
	for ip in range(phaseDiffPlot.getNumbOfPoints()):
		cav_phase = phaseDiffPlot.getX(ip)
		ekin_guess =  cav_wrapper.energy_guess_harm_funcion.getValue(cav_phase)
		beta_guess = math.sqrt(ekin_guess*(ekin_guess+2*mass))/(ekin_guess+mass)
		coeff = coeff_init/beta_guess
		# let's make bpm_phase(z) points for good BPMs
		gd = BasicGraphData()
		base_bpm_wrapper = bpm_wrappers_good_arr[0]
		(graphDataAmp,graphDataPhase) = cav_wrapper.bpm_amp_phase_dict[base_bpm_wrapper]
		base_bpm_offset = base_bpm_wrapper.final_phase_offset.phaseOffset_avg	
		base_bpm_phase = makePhaseNear180(graphDataPhase.getY(ip) - base_bpm_offset,0.)
		gd.addPoint(base_bpm_wrapper.pos,base_bpm_phase)
		#print "debug ==== ip=",ip," cav_phase=",cav_phase," eKin_guess=",ekin_guess	
		for bpm_ind in range(1,len(bpm_wrappers_good_arr)):
			bpm_wrapper = bpm_wrappers_good_arr[bpm_ind]
			(graphDataAmp,graphDataPhase) = cav_wrapper.bpm_amp_phase_dict[bpm_wrapper]
			bpm_phase = graphDataPhase.getY(ip) - bpm_wrapper.final_phase_offset.phaseOffset_avg
			delta_pos = bpm_wrapper.pos - gd.getX(bpm_ind-1)
			bpm_phase_guess = gd.getY(bpm_ind-1) + coeff*delta_pos
			bpm_phase = makePhaseNear180(bpm_phase,bpm_phase_guess)
			gd.addPoint(bpm_wrapper.pos,bpm_phase)
			#print "debug bpm=",bpm_wrapper.alias," pos=",bpm_pos," phase=",bpm_phase
		res_arr = GraphDataOperations.polynomialFit(gd,-1.e+36,+1.e+36,1)		
		if(res_arr == null): return false
		slope = res_arr[0][1]
		init_phase = res_arr[0][0]
		bad_point_ind = 1
		bad_points_count = 0
		while(bad_point_ind >= 0):
			bad_point_ind = -1
			avg_err2 = 0.
			for index in range(gd.getNumbOfPoints()):
				avg_err2 += (gd.getY(index) - (init_phase + slope*gd.getX(index)))**2
			if(gd.getNumbOfPoints() > 1): avg_err2 /= gd.getNumbOfPoints()
			avg_err = math.sqrt(avg_err2)
			for index in range(gd.getNumbOfPoints()):
				diff = gd.getY(index) - (init_phase + slope*gd.getX(index))
				if(math.fabs(diff) > 3.0*avg_err):
					bad_point_ind = index
					break
			if(bad_point_ind >= 0):
				bad_points_count += 1
				gd.removePoint(bad_point_ind)
				res_arr = GraphDataOperations.polynomialFit(gd,-1.e+36,+1.e+36,1)		
				if(res_arr == null): return false	
				slope = res_arr[0][1]
				init_phase = res_arr[0][0]	
			if(bad_points_count > 4):
				return false
		slope_err = res_arr[1][1]	
		init_phase_err = res_arr[1][0]			
		beta = coeff_init/slope
		gamma = 1./math.sqrt(1.0-beta*beta)
		eKin = mass*(gamma-1.0)
		delta_eKin = mass*gamma**3*beta**3*slope_err/coeff_init
		cav_wrapper.eKinOutPlot.addPoint(cav_phase,eKin,delta_eKin)
		"""
		print "debug ==================== cav_phase=",cav_phase," eKin_out=",eKin," dE=",delta_eKin," ekin_guess=",ekin_guess
		for ip0 in range(gd.getNumbOfPoints()):
			print "debug bpm_ind=",ip0," pos=",gd.getX(ip0)," Y=",gd.getY(ip0)," delta=",(gd.getY(ip0)-(res_arr[0][1]*gd.getX(ip0)+init_phase))
		"""
	return true
def calculateOffsetsForNotDoneBPMsRight(cav_wrapper,scl_long_tuneup_controller,bpm_wrappers_good_arr,bpm_wrappers_not_done_arr):
	# this function will calculate offsets for "right" offsets of 
	# "Not yet Done" BPMs by using "good" (already done) BPMs
	eKin_in = cav_wrapper.eKin_in_guess
	mass = scl_long_tuneup_controller.mass/1.0e+6			
	c_light = scl_long_tuneup_controller.c_light	
	bpm_freq = scl_long_tuneup_controller.bpm_freq
	coeff_init = 360.0*bpm_freq/c_light
	for bpm_wrapper in bpm_wrappers_not_done_arr:
		bpm_wrapper.right_phase_offset.phaseOffset_arr = []
	(graphDataAmp,phaseDiffPlot) = cav_wrapper.bpm_amp_phase_dict[bpm_wrappers_good_arr[0]]
	cav_wrapper.eKinOutPlot.removeAllPoints()
	cav_wrapper.eKinOutPlotTh.removeAllPoints()
	for ip in range(phaseDiffPlot.getNumbOfPoints()):
		cav_phase = phaseDiffPlot.getX(ip)
		ekin_guess =  cav_wrapper.energy_guess_harm_funcion.getValue(cav_phase)
		beta_guess = math.sqrt(ekin_guess*(ekin_guess+2*mass))/(ekin_guess+mass)
		coeff = coeff_init/beta_guess
		# let's make bpm_phase(z) points for good BPMs
		gd = BasicGraphData()
		base_bpm_wrapper = bpm_wrappers_good_arr[0]
		(graphDataAmp,graphDataPhase) = cav_wrapper.bpm_amp_phase_dict[base_bpm_wrapper]
		base_bpm_offset = base_bpm_wrapper.right_phase_offset.phaseOffset_avg	
		base_bpm_phase = graphDataPhase.getY(ip) - base_bpm_offset
		#print "debug ==== ip=",ip," cav_phase=",cav_phase," eKin_guess=",ekin_guess	
		gd.addPoint(base_bpm_wrapper.pos,base_bpm_phase)
		for bpm_ind in range(1,len(bpm_wrappers_good_arr)):
			bpm_wrapper = bpm_wrappers_good_arr[bpm_ind]
			(graphDataAmp,graphDataPhase) = cav_wrapper.bpm_amp_phase_dict[bpm_wrapper]
			bpm_phase = graphDataPhase.getY(ip) - bpm_wrapper.right_phase_offset.phaseOffset_avg
			bpm_pos = bpm_wrapper.pos
			bpm_phase_guess = gd.getY(bpm_ind-1) + coeff*(bpm_pos-gd.getX(bpm_ind-1))
			bpm_phase = makePhaseNear(bpm_phase,bpm_phase_guess)
			gd.addPoint(bpm_pos,bpm_phase)
			#print "debug bpm=",bpm_wrapper.alias," pos=",bpm_pos," phase=",bpm_phase
		res_arr = GraphDataOperations.polynomialFit(gd,-1.e+36,+1.e+36,1)
		slope = res_arr[0][1]
		slope_err = res_arr[1][1]
		init_phase = res_arr[0][0]
		init_phase_err = res_arr[1][0]
		beta = coeff_init/slope
		gamma = 1./math.sqrt(1.0-beta*beta)
		eKin = mass*(gamma-1.0)	
		delta_eKin = mass*gamma**3*beta**3*slope_err/coeff_init
		cav_wrapper.eKinOutPlot.addPoint(cav_phase,eKin,delta_eKin)
		#print "debug cav_phase=",cav_phase,"eKin_guess=",ekin_guess," eKin=",eKin," delta_E=",delta_eKin
		# let's go over the bad BPMs and calculate offsets for particular cavity phase
		for bpm_wrapper in bpm_wrappers_not_done_arr:
			bpm_pos = bpm_wrapper.pos
			bpm_phase_th = init_phase + slope*bpm_pos
			(graphDataAmp,graphDataPhase) = cav_wrapper.bpm_amp_phase_dict[bpm_wrapper]
			bpm_phase = graphDataPhase.getY(ip)
			bpm_offset = makePhaseNear(bpm_phase - bpm_phase_th,0.)
			bpm_wrapper.right_phase_offset.phaseOffset_arr.append(bpm_offset)
	#----set up the output energy guess
	cav_wrapper.eKin_out_guess = cav_wrapper.eKinOutPlot.getValueY(cav_wrapper.livePhase)
	eKin_avg = 0.
	for i_ep in range(cav_wrapper.eKinOutPlot.getNumbOfPoints()):
		eKin_avg += cav_wrapper.eKinOutPlot.getY(i_ep)
	if(cav_wrapper.eKinOutPlot.getNumbOfPoints() != 0.):
		eKin_avg /= cav_wrapper.eKinOutPlot.getNumbOfPoints()
	cav_wrapper.eKin_in_guess = eKin_avg
	# let's calculate statistics for "not done" BPMs' offsets
	for bpm_wrapper in bpm_wrappers_not_done_arr:
		phase_arr = bpm_wrapper.right_phase_offset.phaseOffset_arr
		(phase0_avg, phase0_err) = calculateAvgErr(phase_arr)
		phase_arr = []
		for phase in bpm_wrapper.right_phase_offset.phaseOffset_arr:
			phase_arr.append(makePhaseNear(phase-180.,0.))
		(phase1_avg, phase1_err) = calculateAvgErr(phase_arr)
		phase1_avg = makePhaseNear(phase1_avg+180.,0.)
		if(phase1_err < phase0_err):
			bpm_wrapper.right_phase_offset.phaseOffset_avg = phase1_avg
			bpm_wrapper.right_phase_offset.phaseOffset_err = phase1_err
		else:
			bpm_wrapper.right_phase_offset.phaseOffset_avg = phase0_avg
			bpm_wrapper.right_phase_offset.phaseOffset_err = phase0_err
		bpm_wrapper.right_phase_offset.isReady = true
def calculateOffsetsForNotDoneBPMsLeft(cav_wrapper,scl_long_tuneup_controller,bpm_wrappers_good_arr,bpm_wrappers_not_done_arr):
	# this function will calculate offsets for "left" offsets of 
	# "Not yet Done" BPMs by using "good" (already done) BPMs
	eKin_in = cav_wrapper.eKin_in_guess
	mass = scl_long_tuneup_controller.mass/1.0e+6			
	c_light = scl_long_tuneup_controller.c_light	
	bpm_freq = scl_long_tuneup_controller.bpm_freq
	coeff_init = 360.0*bpm_freq/c_light
	for bpm_wrapper in bpm_wrappers_not_done_arr:
		bpm_wrapper.left_phase_offset.phaseOffset_arr = []
	phaseDiffPlot = cav_wrapper.phaseDiffPlot
	cav_wrapper.eKinOutPlot.removeAllPoints()
	cav_wrapper.eKinOutPlotTh.removeAllPoints()
	for ip in range(phaseDiffPlot.getNumbOfPoints()):
		cav_phase = phaseDiffPlot.getX(ip)
		ekin_guess =  cav_wrapper.energy_guess_harm_funcion.getValue(cav_phase)
		beta_guess = math.sqrt(ekin_guess*(ekin_guess+2*mass))/(ekin_guess+mass)
		coeff = coeff_init/beta_guess
		# let's make bpm_phase(z) points for good BPMs
		gd = BasicGraphData()
		base_bpm_wrapper = bpm_wrappers_good_arr[0]
		(graphDataAmp,graphDataPhase) = cav_wrapper.bpm_amp_phase_dict[base_bpm_wrapper]
		base_bpm_offset = base_bpm_wrapper.left_phase_offset.phaseOffset_avg	
		base_bpm_phase = graphDataPhase.getY(ip) - base_bpm_offset
		#print "debug ==== ip=",ip," cav_phase=",cav_phase," eKin_guess=",ekin_guess	
		gd.addPoint(base_bpm_wrapper.pos,base_bpm_phase)
		for bpm_ind in range(1,len(bpm_wrappers_good_arr)):
			bpm_wrapper = bpm_wrappers_good_arr[bpm_ind]
			(graphDataAmp,graphDataPhase) = cav_wrapper.bpm_amp_phase_dict[bpm_wrapper]
			bpm_phase = graphDataPhase.getY(ip) - bpm_wrapper.left_phase_offset.phaseOffset_avg
			bpm_pos = bpm_wrapper.pos
			bpm_phase_guess = gd.getY(bpm_ind-1) + coeff*(bpm_pos-gd.getX(bpm_ind-1))
			bpm_phase = makePhaseNear(bpm_phase,bpm_phase_guess)
			gd.addPoint(bpm_pos,bpm_phase)
			#print "debug bpm=",bpm_wrapper.alias," pos=",bpm_pos," phase=",bpm_phase
		res_arr = GraphDataOperations.polynomialFit(gd,-1.e+36,+1.e+36,1)
		slope = res_arr[0][1]
		slope_err = res_arr[1][1]
		init_phase = res_arr[0][0]
		init_phase_err = res_arr[1][0]
		beta = coeff_init/slope
		gamma = 1./math.sqrt(1.0-beta*beta)
		eKin = mass*(gamma-1.0)	
		delta_eKin = mass*gamma**3*beta**3*slope_err/coeff_init
		cav_wrapper.eKinOutPlot.addPoint(cav_phase,eKin,delta_eKin)
		#print "debug cav_phase=",cav_phase,"eKin_guess=",ekin_guess," eKin=",eKin," delta_E=",delta_eKin
		# let's go over the bad BPMs and calculate offsets for particular cavity phase
		for bpm_wrapper in bpm_wrappers_not_done_arr:
			bpm_pos = bpm_wrapper.pos
			bpm_phase_th = init_phase + slope*bpm_pos
			(graphDataAmp,graphDataPhase) = cav_wrapper.bpm_amp_phase_dict[bpm_wrapper]
			bpm_phase = graphDataPhase.getY(ip)
			bpm_offset = makePhaseNear(bpm_phase - bpm_phase_th,0.)
			bpm_wrapper.left_phase_offset.phaseOffset_arr.append(bpm_offset)
	#----set up the output energy guess
	cav_wrapper.eKin_out_guess = cav_wrapper.eKinOutPlot.getValueY(cav_wrapper.livePhase)
	# let's calculate statistics for "not done" BPMs' offsets
	for bpm_wrapper in bpm_wrappers_not_done_arr:
		phase_arr = bpm_wrapper.left_phase_offset.phaseOffset_arr
		(phase0_avg, phase0_err) = calculateAvgErr(phase_arr)
		phase_arr = []
		for phase in bpm_wrapper.left_phase_offset.phaseOffset_arr:
			phase_arr.append(makePhaseNear(phase-180.,0.))
		(phase1_avg, phase1_err) = calculateAvgErr(phase_arr)
		phase1_avg = makePhaseNear(phase1_avg+180.,0.)
		if(phase1_err < phase0_err):
			bpm_wrapper.left_phase_offset.phaseOffset_avg = phase1_avg
			bpm_wrapper.left_phase_offset.phaseOffset_err = phase1_err
		else:
			bpm_wrapper.left_phase_offset.phaseOffset_avg = phase0_avg
			bpm_wrapper.left_phase_offset.phaseOffset_err = phase0_err
		bpm_wrapper.left_phase_offset.isReady = true
예제 #5
0
    def calculateEnergy(self, eKin_in):
        mass = self.scl_long_tuneup_controller.mass / 1.0e+6
        c_light = self.scl_long_tuneup_controller.c_light
        bpm_freq = self.scl_long_tuneup_controller.bpm_freq
        coeff_init = 360.0 * bpm_freq / c_light
        beta = math.sqrt(eKin_in * (eKin_in + 2 * mass)) / (eKin_in + mass)
        coeff = coeff_init / beta
        #------ calculate avg bpm phases
        res_arr = []
        for bpm_ind in range(len(self.em_bpm_wrpprs)):
            bpm_wrapper = self.em_bpm_wrpprs[bpm_ind]
            if (not self.use_bpms[bpm_ind]): continue
            if (not self.bpm_amp_phase_data_dict.has_key(bpm_wrapper)):
                continue
            (amp_arr, phase_arr) = self.bpm_amp_phase_data_dict[bpm_wrapper]
            (phase_avg, phase_err) = calculateAvgErr(phase_arr)
            #print "debug bpm=",bpm_wrapper.alias," (phase_avg,phase_err) =",(phase_avg,phase_err)
            res_arr.append([bpm_ind, bpm_wrapper, phase_avg, phase_err])
        n_res = len(res_arr)
        scl_long_tuneup_energy_meter_controller = self.scl_long_tuneup_controller.scl_long_tuneup_energy_meter_controller
        table_and_plots_panel = scl_long_tuneup_energy_meter_controller.table_and_plots_panel
        init_start_stop_panel = scl_long_tuneup_energy_meter_controller.init_start_stop_panel
        fixInitEenergy_RadioButton = init_start_stop_panel.fixInitEenergy_RadioButton
        buffer_size = int(init_start_stop_panel.buffer_size_text.getValue())
        if (n_res < 2): return eKin_in
        [bpm_ind0, bpm_wrapper0, phase_avg0, phase_err0] = res_arr[0]
        phase_avg0 = phase_avg0 - self.bpm_phase_offsets[0]
        res_arr[0][2] = phase_avg0
        base_pos = bpm_wrapper0.pos
        #"""
        for ind in range(n_res - 1):
            [bpm_ind0, bpm_wrapper0, phase_avg0, phase_err0] = res_arr[ind]
            [bpm_ind1, bpm_wrapper1, phase_avg1, phase_err1] = res_arr[ind + 1]
            bpm1_phase = phase_avg1 - self.bpm_phase_offsets[bpm_ind1]
            bpm1_phase_guess = phase_avg0 + coeff * (bpm_wrapper1.pos -
                                                     bpm_wrapper0.pos)
            bpm1_phase = makePhaseNear(bpm1_phase, bpm1_phase_guess)
            res_arr[ind + 1][2] = bpm1_phase
        """
		for ind in range(1,n_res):
			[bpm_ind,bpm_wrapper,phase_avg,phase_err] = res_arr[ind]
			bpm_phase = phase_avg - self.bpm_phase_offsets[bpm_ind]
			delta_pos = bpm_wrapper.pos - base_pos
			bpm_phase_guess = phase_avg0 + coeff*delta_pos
			bpm_phase = makePhaseNear(bpm_phase,bpm_phase_guess)
			res_arr[ind][2] = bpm_phase	
		"""
        #----- make phase plot
        gd = BasicGraphData()
        for ind in range(n_res):
            [bpm_ind, bpm_wrapper, phase_avg, phase_err] = res_arr[ind]
            gd.addPoint(bpm_wrapper.pos - base_pos, phase_avg, phase_err)
        res_poly_arr = GraphDataOperations.polynomialFit(
            gd, -1.e+36, +1.e+36, 1)
        if (res_poly_arr == null): return eKin_in
        slope = res_poly_arr[0][1]
        init_phase = res_poly_arr[0][0]
        if (fixInitEenergy_RadioButton.isSelected()):
            slope = coeff
            init_phase = phase_avg0
        beta = coeff_init / slope
        gamma = 1. / math.sqrt(1.0 - beta * beta)
        eKin = mass * (gamma - 1.0)
        slope_err = res_poly_arr[1][1]
        delta_eKin = mass * gamma**3 * beta**3 * slope_err / coeff_init
        #make phase error plot
        x_arr = []
        y_arr = []
        err_arr = []
        for ind in range(n_res):
            [bpm_ind, bpm_wrapper, phase_avg, phase_err] = res_arr[ind]
            x_arr.append(bpm_wrapper.pos)
            phase_diff = makePhaseNear(
                phase_avg - (init_phase + slope *
                             (bpm_wrapper.pos - base_pos)), 0.)
            self.bpm_phase_diff_arr[bpm_ind].append(phase_diff)
            if (len(self.bpm_phase_diff_arr[bpm_ind]) > buffer_size):
                self.bpm_phase_diff_arr[bpm_ind] = self.bpm_phase_diff_arr[
                    bpm_ind][1:]
            (phase_diff,
             phase_err) = calculateAvgErr(self.bpm_phase_diff_arr[bpm_ind])
            y_arr.append(phase_diff)
            err_arr.append(phase_err)
        table_and_plots_panel.bpm_phase_err_gd.removeAllPoints()
        table_and_plots_panel.bpm_phase_err_gd.addPoint(x_arr, y_arr, err_arr)
        return (eKin, delta_eKin)