def getDefaultBeamdata(self):
     E = 2e9*constants.e # 2GeV to joule from ref F. # Why show this in Joules?
     freq = 704.42e6 # (Hz) from ref. F
     
     rf_lambda = float(constants.c)/freq  # beam data needed
     m = float(constants.m_p)
     beta = betaFromE(m, E)
     q = float(constants.e)
     nbrOfParticles = 1000
     I = 0.0625
     beamdata = [beta, rf_lambda, m, q, E, nbrOfParticles, I]
     return beamdata
def loadMultipartFormat_dst(filename):
    with open(filename, "rb") as f: # b is for binary
        fileContent = f.read()
        twoChars = struct.unpack("cc", fileContent[:2]) # ???
        #print "twoChars: " + str(twoChars)
        Np = struct.unpack("i", fileContent[2:6]) # Number of particles
        Np = Np[0]
        #print "Np: " + str(Np)
        Ib = struct.unpack("d",fileContent[6:14]) # Beam current
        Ib = Ib[0]
        #print "Ib: " + str(Ib)
        freq = struct.unpack("d",fileContent[14:22]) # Frequency in MHz
        freq = freq[0]
        #print "freq: " + str(freq)
        thirdChar = struct.unpack("c", fileContent[22:23]) # ???
        #print "thirdChar: " + str(thirdChar)

        # for loop the particles
        x = np.linspace(0,0,Np)
        xp = np.linspace(0,0,Np)
        y = np.linspace(0,0,Np)
        yp = np.linspace(0,0,Np)
        phi = np.linspace(0,0,Np)
        energie = np.linspace(0,0,Np)
        s = np.linspace(0,0,Np)
        nextStart = 23
        nextEnd = 23+8*6
        for i in range(0,Np):
            sixDoubles = struct.unpack("dddddd",fileContent[nextStart:nextEnd]) #[28+i*8*6:28+(i+1)*8*6])
            #print "sixDoubles: " + str(sixDoubles)

            x[i] = sixDoubles[0]/100 # cm to m
            xp[i] = sixDoubles[1] # rad should be the same as unitless
            y[i] = sixDoubles[2]/100 # cm to m
            yp[i] = sixDoubles[3] # rad should be the same as unitless
            phi[i] = sixDoubles[4] # rad
            energie[i] = sixDoubles[5] # MeV and its the actual energy not a diff

            nextStart = nextEnd
            nextEnd = nextStart+8*6
            #print "bla"


        mc2 = struct.unpack("d", fileContent[-8:]) # particle rest mass in MeV ( the usual /c**2 is dropped since c2 is in the name)
        mc2 = mc2[0]
        #print "mc2: " + str(mc2) # Yields the correct value for a proton, wohoo!

        # convert phi and energie to z and zp
        rf_lambda = constants.c/(freq*1000000) # *1000000 is freq converted to Hz, fixed phi to z problems
        m_0 = mc2*1.672621777e-27/938.272046 # conversion from MeV/c**2 to kg
        E = float(input('Enter E [J]: '))
        beta = betaFromE(m_0, E)
        gamma = gammaFromBeta(beta)
        #z, zp = zzpFromPhiEnergie(phi, energie, beta, rf_lambda, gamma, mc2, E)
        z, zp = zdeltaFromPhiEnergie(phi, energie, beta, rf_lambda, gamma, mc2, E)


        # Make the multipart array
        bigMatrix = np.array([x, xp, y, yp, z, zp])
        multipart = [[bigMatrix[:,i], s[i]] for i in xrange(Np)]

        #print "multipart: " + str(multipart)

        #print "tjena" 
        # Should the other beam data be returned as well, such as rf_lambda, beam current and m_0?
        return multipart
    return 0
# sets z as x from old format
multipartfromold, twissfromold = loadSummer2015Formatzasx(datafilepart, datafiletwiss)


#nbrOfParticles = 1000
#multipartfromold = gaussianTwiss3D(nbrOfParticles, twissfromold)
#print "multipartfromoldcopy: \n" + str(multipartfromoldcopy)

spaceChargeOnInComp = 0

E = 2e9*constants.e # 2GeV to joule from ref F.
freq = 704.42e6 # (Hz) from ref. F

rf_lambda = constants.c/freq  # beam data needed
m = constants.m_p
beta = betaFromE(m, E)
q = constants.e
beamdata = [beta, rf_lambda, m, q, E]

#envelopeInComp = np.array([1, 0, 0, 1, 0, 0, 1, 0, 0])
envelopeInComp = envelopeFromMultipart(multipartfromold)
#print "envelopeInComp: " + str(envelopeInComp)

nbrOfSplits = 1

## the lattice will be a FODSO cell (Focusing Quad, Drift, Defocusing Quad, Sextupole, Drift)
compLattice = Lattice('compLattice', beamdata, twissfromold, multipartfromold)

cavityName = "cavity"
cavityLength = 2.0
cavityOscillations = 2