Exemple #1
0
    def get_hist(self, typ, wave1, wave2=None):
        """
		Creates a ROOT histogram for the gibten waves and type
		@param typ: Type of the histogram:
			intensity: Intensity of wave1 (No wave2 needed)
			real: Real part of the intereference of wave1 and wave2
			imag: Imaginary part of the intereference of wave1 and wave2
			phase: Complex phase of the intereference of wave1 and wave2
		@type typ: str
		@param wave1: Number of the first wave
		@type wave1: int
		@param wave2: Number of the second wave
		@type wave2: int
		@return: Chosen ROOT histogram		
		@rtype: Hist
		"""
        mmin, mmax, nbin = self.get_mmin_mmax_nbin()
        hist = Hist(nbin, mmin, mmax)
        for bin in self.bins:
            mass = bin.center()
            n_bin = hist.FindBin(mass)
            if typ == "intensity":
                hist.SetBinContent(n_bin, bin.intens[wave1])
                hist.SetBinError(n_bin, bin.errors_intens[wave1])
            elif typ == "real":
                hist.SetBinContent(n_bin, bin.re[wave1][wave2])
                hist.SetBinError(n_bin, bin.errors_re[wave1][wave2])
            elif typ == "imag":
                hist.SetBinContent(n_bin, bin.im[wave1][wave2])
                hist.SetBinError(n_bin, bin.errors_im[wave1][wave2])
            elif typ == "phase":
                hist.SetBinContent(n_bin, bin.phases[wave1][wave2] * 180. / pi)
                hist.SetBinError(n_bin,
                                 bin.errors_phase[wave1][wave2] * 180. / pi)
            else:
                raise KeyError  # Type not defined
        hist.SetTitle("mass-independent")
        hist.SetName("mass-independent")
        return hist
'''
Created on 11 Mar 2015

@author: kreczko
'''

from rootpy.io import File
from rootpy.plotting import Hist
from rootpy import asrootpy

rootpy_hist = Hist(100, 0, 100, type='F')

rootpy_hist.SetName('hist')
test_file = File('test.root', 'RECREATE')
test_file.mkdir("test")
test_file.cd('test')
rootpy_hist.Write()
test_file.Write()
test_file.Close()

read_file = File('test.root')
folder = read_file.Get('test')
hist = folder.hist
print hist.TYPE
read_file.Close()

hist = None

read_file = File('test.root')
hist = read_file.Get('test/hist')
hist1 = hist.empty_clone(type='D')
Exemple #3
0
def get_fit_histogram(chi2,typ,tbin,wave1, wave2 = None, mmin = .5, mmax = 2.5, nbin = 500,component = -1, parameters = None):
	"""
	Gets a single histogram from a chi2 object
	@param chi2: Chi2 from where the histogrm is built
	@type chi2: chi2
	@param typ: type of the histogram:
		intensity: Intensity for wave1
		amp_re: Real part of wave1
		amp_im: Imag part of wave1
		amp_phase: Phase of wave1
		real: Real part of the interference of wave1 and wave2
		imag: Imag part of the interference of wave1 and wave2
		phase: Phase of the interference of wave1 and wave2
	@type param: str
	@param wave1: Number of the first wave
	@type wave1: int
	@param wave2: Number of the first wave (only for interferences, ignored otherwise)
	@type wave2: int
	@param mmin: Minimum mass for the histogram
	@type mmin: float
	@param mmax: Maximum mass for the histogram
	@type mmax: float
	@param nbin: Number of bins for the histogram
	@type nbin: int
	@param component: Number of active component, all active if component == -1 (default)
	@type component: int
	@return Specified histogram
	@rtyp Hist
	"""
	nWaves = chi2.nWaves()
	nTbin= chi2.nTbin()
	perT = chi2.nBrCpl()
	nCpl = perT*nTbin


	hist = Hist(nbin,mmin,mmax)
	if typ.startswith("all_waves"):
		if not component == -1:
			raise ValueError # Not possible with specified component
		hist = []
		for i in range(nWaves):
			histLine = []
			for j in range(nWaves):
				histLine.append(Hist(nbin,mmin,mmax))
			hist.append(histLine)

	step = (mmax - mmin)/nbin
	if not parameters:
		parameters = chi2.fullParameters()
	print '-----------------------------------------------------'
	print '-----------------------------------------------------'
	print tbin,wave1,component
	print '-----------------------------------------------------'
	print parameters[:2*nTbin*perT]
	if not component == -1:
		params_to_keep = chi2.getFuncParameters(component)
		for i in range(2*nCpl):
			if not i in params_to_keep:
				parameters[i] = 0.
	print parameters[:2*nTbin*perT]
	for i in range(nTbin):
		if not i == tbin:
			for j in range(perT):
				parameters[2*(j+i*perT)  ] = 0. # Set eveything in the wrong tbin to zero, just to be shure
				parameters[2*(j+i*perT)+1] = 0.
	print '-----------------------------------------------------'
	print parameters[:2*nTbin*perT]

	for i in range(1,nbin+1):
		m = mmin+(i-.5)*step
		amps = chi2.Amplitudes(m,tbin, parameters)

		if typ == "intensity":
			hist.SetBinContent(i,abs(amps[wave1])**2)
		elif typ == "ampl_real":
			hist.SetBinContent(i,amps[wave1].real)
		elif typ == "ampl_imag":
			hist.SetBinContent(i,amps[wave1].imag)
		elif typ == "ampl_phase":
			hist.SetBinContent(i,math.atan2(amps[wave1].imag,amps[wave1].real)*180./math.pi)
		elif typ.startswith("all_waves"):
			for k in range(nWaves):
				for l in range(nWaves):
					interference = amps[k]*amps[l].conjugate()
					if typ.endswith("real"):
						hist[k][l].SetBinContent(i,interference.real)
					if typ.endswith("imag"):
						hist[k][l].SetBinContent(i,interference.imag)
					if typ.endswith("phase"):
						hist[k][l].SetBinConetnt(i,math.atan2(interference.imag,interference.real)*180./math.pi)
		else:
			interference = amps[wave1]*amps[wave2].conjugate()
			if typ =="real":
				hist.SetBinContent(i,interference.real)
			elif typ == "imag":
				hist.SetBinContent(i,interference.imag)
			elif typ == "phase":
				phase=math.atan2(interference.imag,interference.real)*180./math.pi
				hist.SetBinContent(i,phase)
	if component == -1:
		name = "mass-dependent"
	else:
		name = chi2.get_component_name(component)
	if not typ.startswith("all_waves"):
		hist.SetTitle(name)
		hist.SetName(name)	
	else:
		for i in range(nWaves):
			for j in range(nWaves):
				hist[i][j].SetTitle(name)
				hist[i][j].SetName(name)	
	return hist