def plot_fields(self, fig=None):
        '''
        Plots the fields at 7 round trips distributed through
        all the round trips.
        '''
        if not self.fields_are_calculated():
            self.logger.error('Fields are not calculated')
            message = 'Calculate fields before ploting'
            raise NotReady(message)
        if fig is None:
            fig, (axI, axP) = EField.create_figure()

        step = round_to_odd(self.fields.shape[0] / 7)
        for i in range(0, self.fields.shape[0], step):
            efield = EField(self.input_efield.x, self.fields[i, :])
            fig, (axI, axP) = efield.plot(fig,
                                          label='# {}'.format(i),
                                          normalize=True)
        axI.legend()
        return fig, (axI, axP)
        for dL in tqdm(dLs, desc='Spectrum calculation')
    ]
    t_spectrum = time() - t

    E_cav_1 = EField(x, compute_total_field(fields, lda, res.x))
    E_cav_2 = EField(x, compute_total_field(fields, lda, res.x + lda / 4))

    # Timings
    print('Time for fields calculation : {:.1f} s'.format(t_fields))
    print('Time for maxima search : {:.1f} ms'.format(t_minimize * 1e3))
    print('Time for {} points spectrum : {:.1f} s'.format(n, t_spectrum))

    print('Maxima found at dL = {:.3f} lda'.format(res.x / lda))

    # Plot few round-trips
    fig, (axI, axP) = E_ref.plot(label='Input', normalize=True)
    for i in range(0, N_rt, round_to_odd(N_rt / 7)):
        EField(x, fields[i, :]).plot(fig,
                                     label='# {}'.format(i),
                                     normalize=False)
    axI.legend(loc='upper right')

    # Plot spectrum
    plt.figure()
    plt.semilogy(dLs / lda, power)
    plt.xlabel(r'$\Delta L$ ($\lambda$)')
    plt.semilogy([res.x / lda, res.x / lda], plt.ylim(), '--')
    plt.semilogy([res.x / lda + 0.25, res.x / lda + 0.25], plt.ylim(), '--')
    plt.ylabel('Power')

    # Plot modes
E = HG_efield(n=0, x=x, w=win,)
E.E = E.E / np.sqrt(E.I.max())

angle = 1e-2

print('Electric Field E : {}'.format(E))
print('Power : {:.5f}'.format(E.P))
print(len(E))

dist = 2.0
E_prop = E.tilt(angle).propagate(dist=dist)

zr = np.pi * win**2 / E.lda
w_th = win*np.sqrt(1+(dist/zr)**2)
E_theo = EField(x, gaussian(x, amplitude=np.sqrt(win/w_th),
                            center=angle * dist, waist=w_th, offset=0))
E_theo = HG_efield(n=0, x=x, amplitude=np.sqrt(win/w_th),
                   w=w_th, x0=angle*dist)


# plt.figure()
# plt.plot(x, abs(E_theo.I/E_theo.I.max()-E_prop.I/E_prop.I.max()))

s = 'Result agrees with theory : {}'
print(s.format(np.allclose(E_theo.I,  E_prop.I)))

fig, (axI, _) = E.plot(label='Reference')
E_prop.plot(fig, label='Propagated')
E_theo.plot(fig, linestyle='--', label='Theoretic')
axI.legend()
plt.show()
                         method='bounded',
                         options={'xatol': lda / 10000})

n = 200
DdL = lda / 2 / 20
dLs = np.linspace(0, 1.2 * lda / 2, n)
dLs = np.hstack([
    np.linspace(res.x - DdL, res.x + DdL, n),
    np.linspace(res.x + DdL, res.x + lda / 2 - DdL, 15)
])
power = [compute_power(x, fields, lda, dL) for dL in dLs]

E_cav = EField(x, compute_total_field(fields, lda, res.x))
print('Maxima found at dL = {:.3f} lda'.format(res.x / lda))

fig, (axI, axP) = E_ref.plot(label='Input', normalize=True)
for i in range(0, N_rt, round_to_odd(N_rt / 5)):
    EField(x, fields[i, :]).plot(fig, label='# {}'.format(i), normalize=True)
axI.legend(loc='upper right')

plt.figure()
plt.semilogy(dLs / lda, power)
plt.xlabel(r'$\Delta L$ ($\lambda$)')
plt.plot([res.x / lda, res.x / lda], plt.ylim(), '--')
plt.ylabel('Power')

fig, (axI, axP) = E_ref.plot(label='Input', normalize=True)
E_cav.plot(fig, label='Mode', normalize=True)

plt.show()