def sqe2dos(sqe, Ei):
    """Takes an instance `sqe` of class expSqe and returns `res`, an instance 
    of class densityOfStates, assuming that `sqe` is 1-phonon, single scattering 
    only. Sample shape and orientation are taken into account.  This is equivalent to first
    order multiple scattering correction.
    """ 
    dos = densityOfStates(sqe)
    g0 = gamma0( sqe, dos )
    DW2 = debyeWallerExp(sqe,dos)
    #res = nar( sqe.e*sqe.sqe*g0*(1 - np.exp(-sqe.e*sqe.beta)))[sqe.zeroInd:]
    geRaw = np.zeros(len(sqe.q))
    for enInd in range(len(sqe.e)):
        for qInd in range(len(sqe.q)):
            geRaw[enInd] += sqe.e[enInd]*sqe.sqe[qInd][enInd]*g0*(1 - \
                np.exp(-sqe.e[enInd]*sqe.beta))/(H1(sqe.q[qInd], sqe.e[enInd], Ei)*DW2*np.exp(-DW2)) 
    res = densityOfStates(dos.e, geRaw[sqe.zeroInd:], cutRange=sqe.cutRange)
    return res
E = numpy.arange(floor(-maxE/dE)*dE, (ceil(maxE/dE)+1)*dE, dE)
#if e[0]==0.0: #dos contains 0 point
#    E = numpy.concatenate((-1*e[::-1],e[1:]))
#else: #dos does not contain 0 point
#    E = numpy.concatenate((numpy.append(-1*e[::-1],0.0),e))
Q = numpy.arange(minQ, maxQ, dQ)
fakeSqe = numpy.outer(Q,E)
sqe = expSqe.expSqe(Q,E,fakeSqe,fakeSqe,T,M,cutRange=cutRange)
e = numpy.arange(0.0, maxE+dE, dE)
dos = numpy.zeros(len(e))
if len(g) <= len(dos):
  dos[:len(g)] += g
else:
  dos += g[:len(dos)]

dos = densityOfStates.densityOfStates(e,dos,cutRange=cutRange)
#------------------------------------------------------------------------------

#---- Get multiphonon scattering ----------------------------------------------
ANE = multiphonon.AthroughN(sqe,dos,N)
#for i in range(len(ANE)):
#    print 'ane',ANE[i]
SNQ = multiphonon.SthroughN(sqe,dos,N)
#for i in range(len(SNQ)):
#    print SNQ[i]
    
SN = []
for i in range(len(SNQ)):
  SN.append( numpy.outer(SNQ[i],ANE[i]) )

SN = numpy.array(SN)
R0 = D0.copy()

import random
for i in range(104,124):
  R0[i] += 0.02*(random.random() - 0.5)

import Gnuplot
g = Gnuplot.Gnuplot()
gd = Gnuplot.Data
g('set grid')
# g.plot(gd(e0,d0,with='l lw 5'))
# g.replot(gd(e0,D0,with='l lw 5'))
# g.replot(gd(e0,R0,with='l lw 5'))

import densityOfStates
d = densityOfStates.densityOfStates(e0,d0)
D = densityOfStates.densityOfStates(e0,D0)
R = densityOfStates.densityOfStates(e0,R0)

# g.replot(gd(d.e,d.g,with='l lw 3'))
# g.replot(gd(D.e,D.g,with='l lw 3'))
# g.replot(gd(R.e,R.g,with='l lw 3'))

d = densityOfStates.densityOfStates(e0,d0,cutRange=(30,40))
D = densityOfStates.densityOfStates(e0,D0,cutRange=(32,40))
R = densityOfStates.densityOfStates(e0,R0,cutRange=(32,40))
G = densityOfStates.densityOfStates(e0,R0,cutRange=(21,21))

g.plot(gd(d.e,d.gz,with='l lw 3'))
g.replot(gd(D.e,D.gz,with='l lw 3'))
g.replot(gd(R.e,R.gz,with='l lw 3'))