\section{Introduction}\label{sec: introduction} The weight
part of generalisations of Serre's conjecture has seen significant
progress in recent years, particularly for (forms of) $\operatorname{GL}_2$. Conjectural
descriptions of the set of Serre weights were made in increasing
generality by \cite{bdj}, \cite{MR2430440} and \cite{GHS}, and cases
of these conjectures were proved in \cite{geebdj} and
\cite{geesavitttotallyramified}. Most recently, significant progress
was made towards completely establishing the conjecture for rank two
unitary groups in \cite{blggU2}. We briefly recall this result. Let
$p>2$ be prime, let $F$ be a CM field, and let
$\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ be a modular representation (see
\cite{blggU2} for the precise definition of ``modular'', which is in
terms of automorphic forms on compact unitary groups). There is a
conjectural set $W^?(\bar{r})$ of Serre weights in which $\bar{r}$ is
predicted to be modular, which is defined in Section \ref{sec: serre
  weight definitions} below, following \cite{GHS}. Then the main
result of \cite{blggU2} is that under mild technical hypotheses,
$\bar{r}$ is modular of every weight in $W^?(\bar{r})$.

It remains to show that if $\bar{r}$ is modular of some weight, then
this weight is contained in $W^?(\bar{r})$. It had been previously
supposed that this was the easier direction; indeed, just as in the
classical case, the results of
\cite{blggU2} reduce the weight part of Serre's conjecture for these
unitary groups to a purely local problem in $p$-adic Hodge
theory. However, this problem has proved to be difficult,
and so far only fragmentary results are
known. In the present paper we resolve the problem in the totally
ramified case, so that in combination with \cite{blggU2} we resolve
the weight part of Serre's conjecture in this case, proving the
following Theorem (see Theorem \ref{thm: the main result, modular if
  and only if predicted}).
\begin{ithm}
  \label{thm: intro: the main result, modular if and only if predicted}Let
  $F$ be an imaginary CM field with maximal totally real subfield
 ~$F^+$, and suppose that $F/F^+$ is unramified at all finite places,
  that every place of $F^+$ dividing $p$ splits completely in $F$,
  that $\zeta_p\notin F$, and that $[F^+:{\mathbb{Q}}]$ is even. Suppose that
  $p>2$, and that $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is an irreducible
  modular representation with split ramification such that
  $\bar{r}(G_{F(\zeta_p)})$ is adequate. Assume that for each place $w|p$
  of $F$, $F_w/\Qp$ is totally ramified.

 Let $a\in({\mathbb{Z}}^2_+)_0^S$ be a Serre weight. Then
 $a_w\inW^?(\bar{r}|_{G_{F_w}})$ if and only if $\bar{r}$ is modular of
 weight $a$.
\end{ithm}(See the body of the paper, especially Section~\ref{ss:global}, for any unfamiliar notation and
terminology.) While \cite{blggU2} reduced this result to a purely
local problem, our methods are not purely local; in fact we use the
main result of \cite{blggU2}, together with potential automorphy
theorems, as part of our proof.

In the case that $\bar{r}|_{G_{F_w}}$ is semisimple for each place
$w|p$, the result was established (in a slightly different setting) in
\cite{geesavitttotallyramified}. The method of proof was in part
global, making use of certain potentially Barsotti-Tate lifts to
obtain conditions on $\bar{r}|_{G_{F_w}}$. We extend this analysis in
the present paper to the case that $\bar{r}|_{G_{F_w}}$ is reducible but
non-split,
obtaining conditions on the extension classes that can occur; we show
that (other than in one exceptional case) they lie in a certain set $L_{\operatorname{flat}}$, defined in terms of finite
flat models.

In the case that $\bar{r}|_{G_{F_w}}$ is reducible the definition of
$W^?$ also depends on the extension class; it is required to lie in
a set $L_{\operatorname{crys}}$, defined in terms of reducible crystalline lifts with
specified Hodge-Tate weights. To complete the proof, one must show
that $L_{\operatorname{crys}}=L_{\operatorname{flat}}$. An analogous result was proved in generic
unramified cases in section 3.4 of \cite{geebdj} by means of explicit
calculations with Breuil modules; our approach here is less direct,
but has the advantage of working in non-generic cases, and requires
far less calculation.

We use a global argument to show that
$L_{\operatorname{crys}}\subsetL_{\operatorname{flat}}$. Given a class in $L_{\operatorname{crys}}$, we use potential
automorphy theorems to realise the corresponding local representation
as part of a global modular representation, and then apply the main
result of \cite{blggU2} to show that this representation is modular of
the expected weight. Standard congruences between automorphic forms
then show that this class is also contained in $L_{\operatorname{flat}}$.

To prove the converse inclusion, we make a study of different finite
flat models to show that $L_{\operatorname{flat}}$ is contained in a vector space of
some dimension $d$. A standard calculation shows that $L_{\operatorname{crys}}$
contains a space of dimension $d$, so equality follows. As a
byproduct, we show that both $L_{\operatorname{flat}}$ and $L_{\operatorname{crys}}$ are vector
spaces. We also show that various spaces defined in terms of
crystalline lifts are independent of the choice of lift (see Corollary
\ref{cor: independence of lift for H^1_f}). The analogous property was
conjectured in the unramified case in \cite{bdj}.

It is natural to ask whether our methods could be extended to handle
the general case, where $F_w/\Qp$ is an arbitrary
extension. Unfortunately, this does not seem to be the case, because
in general the connection between being modular of some weight and
having a potentially Barsotti-Tate lift of some type is less direct. We expect that our methods could be used to reprove the results of
section 3.4 of \cite{geebdj}, but we do not see how to extend them to
cover the unramified case completely.

We now explain the structure of the paper. In Section \ref{sec: serre
  weight definitions} we recall the definition of~$W^?$, and the
global results from \cite{blggU2} that we will need. In Section \ref{sec:local to
  global} we recall a potential automorphy result from \cite{geekisin}, allowing us to
realise a local mod $p$ representation globally. Section \ref{sec:
  congruences to weight 0} contains the definitions of the spaces
$L_{\operatorname{crys}}$ and $L_{\operatorname{flat}}$ and the proof that $L_{\operatorname{crys}}\subsetL_{\operatorname{flat}}$, and in
Section \ref{sec: finite flat
  models} we carry out the necessary calculations with Breuil modules
to prove our main local results. Finally, in section \ref{sec: global
  consequences} we combine our local results with the techniques of
\cite{geesavitttotallyramified} and the main result of \cite{blggU2}
to prove Theorem \ref{thm: intro: the main result, modular if and only if predicted}.

\subsection{Notation}If $M$ is a field, we let $G_M$ denote its
absolute Galois group. Let~$\epsilon$ denote the $p$-adic cyclotomic
character, and $\bar{\epsilon}$ the mod $p$ cyclotomic character.
If~$M$ is a global field and $v$ is a place of $M$, let $M_v$ denote
the completion of $M$ at $v$.   If
~$M$ is a finite extension of $\mathbb{Q}_l$ for some $l$, we write $I_M$
for the inertia subgroup of~$G_M$. If $R$ is a local ring we write
$\mathfrak{m}_{R}$ for the maximal ideal of $R$.

 Let $K$ be a finite extension of $\Qp$, with ring of integers $\mathcal{O}_K$
 and residue field~$k$.   We write ${\operatorname{Art}}_K:K^\times\to W_K^{{\operatorname{ab}}}$ for
the isomorphism of local class field theory, normalised so that
uniformisers correspond to geometric Frobenius elements.  For each $\sigma\in {\operatorname{Hom}}(k,{\overline{\F}_p})$ we
define the fundamental character $\omega_{\sigma}$ corresponding
to~$\sigma$ to be the composite $$\xymatrix{I_K \ar[r] & W_K^{{\operatorname{ab}}} \ar[r]^{{\operatorname{Art}}_K^{-1}} &
  \mathcal{O}} \newcommand{\calO}{\mathcal{O}_{K}^{\times}\ar[r] & k^{\times}\ar[r]^{\sigma} &
  {\overline{\F}_p}^{\times}.}$$
In the case that $k\cong{\F_p}$, we will sometimes write $\omega$ for
$\omega_\sigma$. Note that in this case we have $\omega^{[K:\Qp]}=\epsilonbar$.

We fix an algebraic closure $\overline{{K}}$ of $K$.  If $W$ is a de Rham representation of $G_K$ over
$\overline{{\Q}}_p$ and $\tau$ is an embedding $K \hookrightarrow \overline{{\Q}}_p$ then the multiset
$\operatorname{HT}_\tau(W)$ of Hodge-Tate weights of $W$ with respect to $\tau$ is
defined to contain the integer $i$ with multiplicity $$\dim_{\overline{{\Q}}_p} (W
\otimes_{\tau,K} \widehat{\overline{{K}}}(-i))^{G_K},$$ with the usual notation
for Tate twists.  Thus for example
$\operatorname{HT}_\tau(\epsilon)=\{ 1\}$.

\section{Serre weight conjectures: definitions}\label{sec: serre
  weight definitions}\subsection{Local definitions}We begin by recalling some
generalisations of the weight part of Serre's conjecture. We begin
with some purely local definitions. Let $K$ be a finite totally ramified extension of
$\Qp$ with absolute ramification index $e$, and let $\rhobar:G_K\to\operatorname{GL}_2({\overline{\F}_p})$ be a continuous
representation.
\begin{defn}
  A \emph{Serre weight} is an irreducible ${\overline{\F}_p}$-representation of
  $\operatorname{GL}_2({\F_p})$. Up to isomorphism, any such representation is of the
  form \[F_a:=\det{}^{a_{2}}\otimes\operatorname{Sym}^{a_{1}-a_{2}}{\mathbb{F}}_p^2\otimes_{{\mathbb{F}}_p}{\overline{\F}_p}\]
  where $0\le a_{1}-a_{2}\le p-1$.  We also use the term Serre weight
  to refer to the pair $a = (a_1,a_2)$.
\end{defn}
We say that two Serre weights $a$ and $b$ are \emph{equivalent} if and only if
$F_a\cong F_b$ as representations of $\operatorname{GL}_2({\F_p})$. This is equivalent
to demanding that we
have $a_{1}-a_{2}=b_{1}-b_{2}$ and $a_2\equiv b_2\pmod{p-1}$.

We write ${\mathbb{Z}}^2_+$ for the set of pairs of integers $(n_1,n_2)$ with
$n_1\ge n_2$, so that a Serre weight $a$ is by definition an element
of ${\mathbb{Z}}^2_+$. We say that an element
$\lambda\in({\mathbb{Z}}^2_+)^{{\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})}$ is a \emph{lift} of a weight
$a\in{\mathbb{Z}}^2_+$ if there is an element $\tau\in{\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})$ such that
$\lambda_{\tau}=a$, and for all other $\tau'\in{\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})$ we have
$\lambda_{\tau'}=(0,0)$.

\begin{defn}
  \label{defn: Galois representation of Hodge type some weight}Let
  $K/\Qp$ be a finite extension, let
  $\lambda\in({\mathbb{Z}}^2_+)^{{\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})}$, and let
  $\rho:G_K\to\operatorname{GL}_2({\overline{\Q}_p})$ be a de Rham representation. Then we say
  that $\rho$ has \emph{Hodge type} $\lambda$ if for each
  $\tau\in{\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})$ we have $\operatorname{HT}_\tau(\rho)=\{\lambda_{\tau,1}+1,\lambda_{\tau,2}\}$.
\end{defn}

Following \cite{GHS} (which in turn follows \cite{bdj} and \cite{MR2430440}), we define an explicit
set of Serre weights $W^?(\rhobar)$.
\begin{defn}
  \label{defn: W? niveau 1}If $\rhobar$ is reducible, then a Serre
  weight $a\in{\mathbb{Z}}^2_+$ is in $W^?(\rhobar)$ if
  and only if $\rhobar$ has a crystalline lift of the
  form \[ \begin{pmatrix}\psi_1&*\\ 0& \psi_2
  \end{pmatrix}\] which has Hodge type $\lambda$ for some lift
  $\lambda\in({\mathbb{Z}}^2_+)^{{\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})}$ of $a$. In particular, if $a\in W^?(\rhobar)$ then by  Lemma 6.2 of \cite{geesavitttotallyramified} it is necessarily the case that  there is a decomposition
  ${\operatorname{Hom}}({\F_p},{\overline{\F}_p})=J\coprod J^c$ and  an integer
  $0\le \delta\le e-1$ such that \[\rhobar|_{I_K}\cong
  \begin{pmatrix} \omega^{\delta}
 \prod_{ \sigma\in
      J}\omega_{\sigma}^{a_{1}+1}\prod_{\sigma\in
      J^c}\omega_\sigma^{a_{2}}&*\\ 0& \omega^{e-1-\delta} \prod_{\sigma\in
      J^c}\omega_\sigma^{a_{1}+1}\prod_{\sigma\in
      J}\omega_\sigma^{a_{2}}.   \end{pmatrix}\]
\end{defn}
We remark that while it may seem strange to consider the single
element set ${\operatorname{Hom}}({\F_p},{\overline{\F}_p})$, this notation will be convenient for us.

\begin{defn}
  \label{defn: W? niveau 2}
 Let $K'$ denote the quadratic unramified
extension of $K$ inside~$\overline{{K}}$, with residue field
$k'$ of order $p^2$.

 If $\rhobar$ is irreducible, then a Serre
  weight $a\in{\mathbb{Z}}^2_+$ is in $W^?(\rhobar)$ if
  and only if there is a subset $J\subset{\operatorname{Hom}}(k',{\overline{\F}_p})$ of size $1$,
  and  an integer $0\le
  \delta\le e-1$ such that if we write
  ${\operatorname{Hom}}(k',{\overline{\F}_p})=J\coprod J^c$, then  \[\rhobar|_{I_K}\cong
  \begin{pmatrix}\prod_{\sigma\in
      J}\omega_{\sigma}^{a_{1}+1+\delta}\prod_{\sigma\in
      J^c}\omega_\sigma^{a_{2}+e-1-\delta}&0\\ 0& \prod_{\sigma\in
      J^c}\omega_\sigma^{a_{1}+1+\delta}\prod_{\sigma\in
      J}\omega_\sigma^{a_{2}+e-1-\delta}
  \end{pmatrix}.\]
\end{defn}
We remark that by Lemma 4.1.19 of \cite{blggU2}, if
$a\inW^?(\rhobar)$ and $\rhobar$ is irreducible then
$\rhobar$ necessarily has a crystalline lift of Hodge type $\lambda$ for any lift
  $\lambda\in({\mathbb{Z}}^2_+)^{{\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})}$ of $a$. Note also that if $a$
  and $b$ are equivalent and $a\inW^?(\rhobar)$ then $b\inW^?(\rhobar)$.
  \begin{remark}\label{rem: conjectured weights independent of
      unramified twist}
    Note that if $\thetabar: G_K\to{\overline{\F}_p}^\times$ is an unramified character, then
    $W^?(\bar{r})=W^?(\bar{r}\otimes\thetabar)$. \end{remark}
  \subsection{Global conjectures}\label{ss:global} The point of the local definitions
  above is to allow us to formulate global Serre weight
  conjectures. Following \cite{blggU2}, we work with rank two unitary
  groups which are compact at infinity. As we will not need to make
  any arguments that depend on the particular definitions made in
  \cite{blggU2}, and our main results are purely local, we simply
  recall some notation and basic properties of the definitions,
  referring the reader to \cite{blggU2} for precise formulations.

We emphasise that our conventions for Hodge-Tate weights are the
opposite of those of \cite{blggU2}; for this reason, we must introduce
a dual into the definitions.

Fix an imaginary CM field $F$, and let $F^+$ be its maximal totally
real subfield. We assume that each prime of $F^+$ over $p$ has residue
field ${\mathbb{F}}_p$ and splits in $F$. We define a global notion of Serre weight by taking a
product of local weights in the following way.

\begin{defn}
  \label{defn:global-serre-wts}
Let $S$ denote the set
of places of $F$ above $p$.  If $w \in S$ lies over a place $v$ of
$F^+$, write $v = w w^c$.  Let
$({\mathbb{Z}}^2_+)_0^{S}$ denote the subset of
$({\mathbb{Z}}^2_+)^{S}$ consisting of elements $a = (a_w)_{w \in S}$ such
that $a_{w,1}+a_{w^c,2}=0$ for all $w\in S$. We say that an
element  $a\in({\mathbb{Z}}^2_+)_0^{S}$ is a \emph{Serre
 weight} if for each $w|p$ we
have \[p-1\ge a_{w,1}-a_{w,2}.\]
\end{defn}

Let $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ be a continuous irreducible
representation. Definition 2.1.9 of \cite{blggU2} states what it
means for $\bar{r}$ to be modular, and more precisely for $\bar{r}$ to be
modular of some Serre weight $a$; roughly speaking, $\bar{r}$ is modular
of weight $a$ if there is a cohomology class on some unitary group
with coefficients in the local system corresponding to $a$ whose
Hecke eigenvalues are determined by the characteristic polynomials of
$\bar{r}$ at Frobenius elements. Since our conventions for Hodge-Tate
weights are the opposite of those of \cite{blggU2}, we make the
following definition.

\begin{defn}
   Suppose that $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is a continuous
  irreducible modular representation. Then we say that $\bar{r}$ \emph{is modular
  of weight} $a\in({\mathbb{Z}}^2_+)_0^S$ if
$\bar{r}^\vee$ is modular of weight $a$ in the sense of Definition 2.1.9
of \cite{blggU2}.
\end{defn} We  globalise the definition of the set
$W^?(\rhobar)$ in the following natural fashion.
\begin{defn}
  If $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is a continuous representation, then
  we define $W^?(\bar{r})$ to be the set of Serre weights
  $a\in({\mathbb{Z}}^2_+)_0^S$ such that for each
  place $w|p$ the corresponding Serre weight
  $a_w\in{\mathbb{Z}}^2_+$ is an element of
  $W^?(\bar{r}|_{G_{F_w}})$.
\end{defn}

One then has the following conjecture.
\begin{conj}\label{conj: global Serre weight explicit conjecture}
  Suppose that $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is a continuous irreducible
  modular representation, and that
  $a\in({\mathbb{Z}}^2_+)_0^S$ is a Serre
  weight. Then $\bar{r}$ is modular of weight $a$ if and only if
  $a\inW^?(\bar{r})$.
\end{conj}
If $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is a continuous representation,
  then we say that $\bar{r}$ has \emph{split ramification} if any finite
  place of $F$ at which $\bar{r}$ is ramified is split over $F^+$.   The
  following result is
Theorem 5.1.3 of \cite{blggU2}, one of the
main theorems of that paper, in the special case where $F_w/\Qp$ is
totally ramified for all $w|p$.  (Note that in \cite{blggU2}, the set
of weights $W^?(\bar{r})$ is referred to as
$W^{\operatorname{explicit}}(\bar{r})$.)
\begin{thm}
  \label{thm: explicit local lifts implies Serre
    weight}Let $F$ be an imaginary CM field with maximal totally real subfield~$F^+$. Assume that $\zeta_p\notin F$, that $F/F^+$ is unramified at all finite places,
  that every place of $F^+$ dividing $p$ has residue field ${\mathbb{F}}_p$ and splits completely in $F$,
  and that $[F^+:{\mathbb{Q}}]$ is even. Suppose that $p>2$, and that
  $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is an irreducible modular
  representation with split ramification. Assume that $\bar{r}(G_{F(\zeta_p)})$ is adequate.

 Let $a\in({\mathbb{Z}}^2_+)_0^S$ be a
  Serre weight. Assume that $a\in W^?(\bar{r})$. Then $\bar{r}$ is
  modular of weight $a$.
\end{thm}
Here \emph{adequacy} is a group-theoretic condition, introduced in
\cite{jack}, that for subgroups of $\operatorname{GL}_2({\overline{\F}_p})$ with $p > 5$ is
equivalent to the usual condition that $\bar{r}|_{G_{F(\zeta_p)}}$ is irreducible.  For a precise
definition we refer the reader to Definition A.1.1 of \cite{blggU2}.
We  also remark that the hypotheses that  $F/F^+$ is unramified at all finite places,
  that every place of $F^+$ dividing $p$ splits completely in $F$,
  and that $[F^+:{\mathbb{Q}}]$ is even, are in fact part of the definition of
  ``modular'' made in \cite{blggU2}.)
  Theorem~\ref{thm: explicit local lifts implies Serre
    weight}  establishes one direction of Conjecture \ref{conj: global Serre
    weight explicit conjecture}, and we are left with the problem of
  ``elimination,'' i.e., the problem of proving that if $\bar{r}$ is
  modular of weight $a$, then $a\in W^?(\bar{r})$.
  We believe that this problem should have a purely local resolution,
  as we now explain.

The key point is the relationship between being
  modular of weight $a$, and the existence of certain de Rham lifts of
  the local Galois representations $\bar{r}|_{G_{F_w}}$, $w|p$. The link
  between these properties is provided by local-global compatibility
  for the Galois representations associated to the automorphic
  representations under consideration; rather than give a detailed
  development of this connection, for which see \cite{blggU2}, we
  simply summarise the key results from \cite{blggU2} that we will
  use. The
  following is Corollary 4.1.8 of \cite{blggU2}.

\begin{prop}
  \label{prop: modular of some weight implies crystalline lifts
    exist}Let $F$ be an imaginary CM field with maximal totally real
  subfield $F^+$, and suppose that $F/F^+$ is unramified at all finite
  places, that every place of $F^+$ dividing $p$ has residue field
  ${\mathbb{F}}_p$ and splits completely in
  $F$, and that $[F^+:{\mathbb{Q}}]$ is even. Suppose that $p>2$, and that
  $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is an irreducible modular representation
  with split ramification. Let
  $a\in({\mathbb{Z}}^2_+)_0^S$ be a Serre
  weight. If $\bar{r}$ is modular of weight $a$, then for each place
  $w|p$ of $F$, there is a crystalline representation
  $\rho_w:G_{F_w}\to\operatorname{GL}_2({\overline{\Q}_p})$ lifting $\bar{r}|_{G_{F_w}}$, such
  that $\rho_w$ has Hodge type $\lambda_w$ for some lift
  $\lambda_w\in({\mathbb{Z}}^2_+)^{{\operatorname{Hom}}_{\Qp}(F_w,{\overline{\Q}_p})}$ of $a$.
\end{prop}

We stress that Proposition~\ref{prop: modular of some weight implies crystalline lifts
    exist} does not already complete the proof of Conjecture \ref{conj: global Serre
    weight explicit conjecture}, because the representation $\rho_w$
  may be irreducible (compare with Definition~\ref{defn: W? niveau 1}).
However, in light of this result, it is natural to make the following
purely local conjecture, which together with Theorem \ref{thm:
  explicit local lifts implies Serre weight} would essentially resolve
Conjecture \ref{conj: global Serre weight explicit conjecture}.

  \begin{conj}
    \label{conj: crystalline lift implies explicit crystalline lift}
    Let $K/\Qp$ be a finite totally ramified extension, and let
    $\rhobar:G_K\to\operatorname{GL}_2({\overline{\F}_p})$ be a continuous representation. Let
    $a\in{\mathbb{Z}}^2_+$ be a Serre weight, and suppose
    that for some lift $\lambda\in({\mathbb{Z}}^2_+)^{{\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})}$, there is
    a continuous crystalline representation
    $\rho:G_{K}\to\operatorname{GL}_2({\overline{\Q}_p})$ lifting $\rhobar$, such
    that $\rho$ has Hodge type $\lambda$.

Then $a\inW^?(\bar{r})$.
  \end{conj}
  We do not know how to prove this conjecture, and we do not directly
  address the conjecture in the rest of this paper. Instead, we
  proceed more indirectly. Proposition \ref{prop: modular of some
    weight implies crystalline lifts exist} is a simple consequence of
  lifting automorphic forms of weight $a$ to forms of weight
  $\lambda$; we may also obtain non-trivial information by lifting to
  forms of weight $0$ and non-trivial type. In this paper, we will
  always consider principal series types. Recall that if $K/\Qp$ is a finite extension the
  \emph{inertial type} of a potentially semistable Galois
  representation $\rho:G_K\to\operatorname{GL}_n({\overline{\Q}_p})$ is the restriction to
  $I_K$ of the corresponding Weil-Deligne representation. In this
  paper we normalise this definition as in the appendix to
  \cite{MR1639612}, so that for example the inertial type of a finite
  order character is just the restriction to inertia of that
  character.

\begin{prop}
  \label{prop: modular of some weight implies potentially BT lifts
    exist}Let $F$ be an imaginary CM field with maximal totally real
  subfield $F^+$, and suppose that $F/F^+$ is unramified at all finite
  places, that every place of $F^+$ dividing $p$ has residue field
  ${\F_p}$ and splits completely in
  $F$, and that $[F^+:{\mathbb{Q}}]$ is even. Suppose that $p>2$, and that
  $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is an irreducible modular representation
 with split ramification. Let $a\in({\mathbb{Z}}^2_+)_0^S$ be a
  Serre weight. If $\bar{r}$ is modular of weight $a$, then for each
  place $w|p$ of $F$, there is a continuous potentially semistable
  representation $\rho_w:G_{F_w}\to\operatorname{GL}_2({\overline{\Q}_p})$ lifting
  $\bar{r}|_{G_{F_w}}$, such that $\rho_w$ has Hodge type $0$ and
  inertial type $\omegat^{a_1}\oplus\omegat^{a_2}$. (Here $\omegat$ is
  the Teichm\"uller lift of $\omega$.)  Furthermore, $\rho_w$ is
  potentially crystalline unless $a_{1}-a_{2}=p-1$ and  $\bar{r}|_{G_{F_w}}\cong
  \begin{pmatrix}
    \chibar\epsilonbar&*\\0&\chibar
  \end{pmatrix}
$ for some character $\chibar$.
\end{prop}
\begin{proof}
  This may be proved in exactly the same way as Lemma 3.4 of
  \cite{geesavitttotallyramified}, working in the setting of
  \cite{blggU2} (cf. the proof of Lemma 3.1.1 of \cite{blggU2}). Note
  that if $\rho_w$ is not potentially crystalline, then it is
  necessarily a twist of an extension of the trivial character by the
  cyclotomic character.
\end{proof}

\section{Realising local representations globally}\label{sec:local to
  global}\subsection{}We now recall a result from the forthcoming paper \cite{geekisin}
which allows us to realise local representations globally, in order to
apply the results of Section~\ref{ss:global} in a purely local
setting.

\begin{thm}
  \label{thm: the final local-to-global result} Suppose that $p>2$,
  that $K/\Qp$ is a finite extension, and let
  $\bar{r}_K:G_K\to\operatorname{GL}_2({\overline{\F}_p})$ be a continuous representation. Then
  there is an imaginary CM field $F$ and a continuous irreducible
  representation $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ such that, if $F^+$ denotes the maximal totally real subfield of $F$,
  \begin{itemize}
  \item each place $v|p$ of $F^+$ splits in $F$ and has $F^+_v\cong
    K$,
  \item for each place $v|p$ of $F^+$, there is a place ${\widetilde{{v}}}$ of $F$
    lying over $F^+$ with $\bar{r}|_{G_{F_{\widetilde{{v}}}}}$ isomorphic to an
    unramified twist of $\bar{r}_K$,
  \item $\zeta_p\notin F$,
  \item $\bar{r}$ is unramified outside of $p$,
  \item $\bar{r}$ is modular in the sense of \cite{blggU2}, and
  \item $\bar{r}(G_{F(\zeta_p)})$ is adequate.
  \end{itemize}
\end{thm}

\begin{proof}We sketch the proof; the full details will appear in
  \cite{geekisin}. The argument is a straightforward application of
  potential modularity techniques. First, an application of
  Proposition 3.2 of \cite{frankII} supplies a totally real field $L^+$ and a continuous irreducible
  representation $\bar{r}:G_{L^+}\to\operatorname{GL}_2({\overline{\F}_p})$ such that
  \begin{itemize}
  \item for each place $v|p$ of $L^+$, $L^+_v\cong K$ and
    $\bar{r}|_{L^+_v}\cong\bar{r}_K$,
 \item for each place $v|\infty$ of $L^+$, $\det\bar{r}(c_v)=-1$, where
    $c_v$ is a complex conjugation at $v$, and
  \item there is a non-trivial finite extension ${\mathbb{F}}/{\mathbb{F}}_p$ such that
    $\bar{r}(G_{L^+})=\operatorname{GL}_2({\mathbb{F}})$.
\end{itemize}
By a further base change one can also arrange that  $\bar{r}|_{G_{L^+_v}}$ is unramified
at each finite place $v\nmid p$ of $L^+$.

By  Lemma 6.1.6 of \cite{blggord} and the proof of
Proposition 7.8.1 of \cite{0905.4266}, $\bar{r}_K$ admits a potentially
Barsotti-Tate lift, and one may then apply  Proposition 8.2.1 of
\cite{0905.4266} to deduce that there is a finite totally real Galois
  extension $F^+/L^+$ in which all primes of $L^+$ above $p$ split
  completely, such that $\bar{r}|_{G_{F^+}}$ is modular in the sense
  that it is congruent to the Galois representation associated to some
  Hilbert modular form of parallel weight $2$.

By the theory of base change between $\operatorname{GL}_2$ and unitary groups
(\textit{cf.} section 2 of \cite{blggU2}), it now suffices to show that
there is a totally imaginary quadratic extension $F/F^+$ and a
character $\thetabar:G_F\to{\overline{\F}_p}^\times$ such that
$\bar{r}|_{G_F}\otimes\thetabar$ has multiplier~$\epsilonbar^{-1}$ and
such that for each place $v|p$ of $F^+$, there is a place ${\widetilde{{v}}}$ of $F$
lying over $v$ with $\thetabar|_{G_{F_{{\widetilde{{v}}}}}}$ unramified. The
existence of such a character is a straightforward exercise in class
field theory, and follows for example from Lemma 4.1.5 of \cite{cht}.
\end{proof}

\section{Congruences}\label{sec: congruences to weight 0}\subsection{} Having realised a local mod $p$
representation globally, we can now use the results explained in
Section \ref{sec: serre
  weight definitions} to deduce non-trivial local consequences.
\begin{thm}
  \label{thm: explicit weight implies pot BT lift}Let $p>2$ be prime,
  let $K/\Qp$ be a finite totally ramified extension, and let
  $\rhobar:G_K\to\operatorname{GL}_2({\overline{\F}_p})$ be a continuous representation. Let
  $a\inW^?(\rhobar)$ be a Serre weight. Then there is a continuous
  potentially semistable representation $\rho:G_K\to\operatorname{GL}_2({\overline{\Q}_p})$
  lifting $\rhobar$, such that $\rho$ has Hodge type $0$ and inertial
  type $\omegat^{a_1}\oplus\omegat^{a_2}$. Furthermore, $\rho$ is
  potentially crystalline unless $a_{1}-a_{2}=p-1$ and $\rhobar\cong
  \begin{pmatrix}
    \chibar\epsilonbar&*\\0&\chibar
  \end{pmatrix}$ for some character $\chibar$.

\end{thm}
\begin{proof}  By Theorem \ref{thm: the final local-to-global result}, there is
  an imaginary CM field $F$ and a modular representation
  $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ such that
  \begin{itemize}
  \item for each place $v|p$ of $F^+$, $v$ splits in $F$ as
    ${\widetilde{{v}}}\tv^c$, and we have $F_{\widetilde{{v}}}\cong K$, and $\bar{r}|_{G_{F_{\widetilde{{v}}}}}$ is
    isomorphic to an unramified twist of $\rhobar$,
\item $\bar{r}$ is unramified outside of $p$,
\item $\zeta_p\notin F$, and
\item $\bar{r}(G_{F(\zeta_p)})$ is adequate.
\end{itemize}Now, since the truth of the result to be proved is
obviously unaffected by making an unramified twist (if $\rhobar$ is
replaced by a twist by an unramified character $\overline{\theta}$, one may
replace $\rho$ by a twist by an unramified
lift of $\overline{\theta}$), we may without loss of
generality suppose that $\bar{r}|_{G_{F_w}}\cong\rhobar$. Let
$b\in({\mathbb{Z}}^2_+)_0^{S}$ be the Serre weight such that
 $b_{\widetilde{{v}}}=a$ for each place $v|p$ of $F^+$, where $S$ denotes the set of
 places of $F$ above $p$. By Remark \ref{rem: conjectured weights independent of
      unramified twist}, $b\inW^?(\bar{r})$. Then by Theorem \ref{thm: explicit local lifts implies Serre
    weight}, $\bar{r}$ is modular of weight $b$. The result now follows
  from Proposition \ref{prop: modular of some weight implies potentially BT lifts
    exist}.
\end{proof}

\subsection{Spaces of crystalline extensions}\label{subsec: H^1_f}We
now specialise to the setting of Definition \ref{defn: W? niveau
  1}. As usual, we let $K/\Qp$ be a finite totally ramified extension with residue
field $k={\F_p}$, ramification index $e$, and uniformiser $\pi$. We fix a Serre weight $a\in{\mathbb{Z}}^2_+$. We fix a
continuous representation $\rhobar:G_K\to\operatorname{GL}_2({\overline{\F}_p})$, and we assume
that there is:
\begin{itemize}
\item  a decomposition
  ${\operatorname{Hom}}({\F_p},{\overline{\F}_p})=J\coprod J^c$, and
\item an integer
  $0\le \delta\le e-1$ such that \[\rhobar|_{I_K}\cong
  \begin{pmatrix}
 \omega^\delta\prod_{\sigma\in
      J}\omega_{\sigma}^{a_{1}+1}\prod_{\sigma\in
      J^c}\omega_\sigma^{a_{2}}&*\\ 0& \omega^{e-1-\delta}\prod_{\sigma\in
      J^c}\omega_\sigma^{a_{1}+1}\prod_{\sigma\in
      J}\omega_\sigma^{a_{2}}.   \end{pmatrix}\]
\end{itemize}
Note that in general there might be several choices
of $J$, $\delta$. Fix such a choice for the
moment. Consider pairs of characters $\chi_1$,
$\chi_2:G_K\to{\overline{\Q}_p}^\times$ with the properties that:
\begin{enumerate}
\item $\rhobar\cong
  \begin{pmatrix}
    \chibar_1&*\\0&\chibar_2
  \end{pmatrix}$,
\item $\chi_1$ and $\chi_2$ are crystalline, and
\item if we let $S$ denote the set of
  ${\operatorname{Hom}}_{\Qp}(K,{\overline{\Q}_p})$, then either
  \begin{enumerate}[(i)]
  \item $J$ is non-empty, and there is one embedding $\tau\in
    S$ with $\operatorname{HT}_\tau(\chi_1)=a_{1}+1$ and
    $\operatorname{HT}_\tau(\chi_2)=a_{2}$, there are $\delta$ embeddings
    $\tau\in S$ with  $\operatorname{HT}_\tau(\chi_1)=1$ and
    $\operatorname{HT}_\tau(\chi_2)=0$, and for the remaining $e-1-\delta$
    embeddings $\tau\in S$ we have  $\operatorname{HT}_\tau(\chi_1)=0$ and
    $\operatorname{HT}_\tau(\chi_2)=1$, or
   \item $J=\emptyset$, and there is one embedding $\tau\in
    S$ with $\operatorname{HT}_\tau(\chi_1)=a_{2}$ and
    $\operatorname{HT}_\tau(\chi_2)=a_{1}+1$, there are $\delta$ embeddings
    $\tau\in S$ with  $\operatorname{HT}_\tau(\chi_1)=1$ and
    $\operatorname{HT}_\tau(\chi_2)=0$, and for the remaining $e-1-\delta$
    embeddings $\tau\in S$ we have  $\operatorname{HT}_\tau(\chi_1)=0$ and
    $\operatorname{HT}_\tau(\chi_2)=1$.
  \end{enumerate}
\end{enumerate}
Note that these properties do not specify the characters $\chi_1$ and
$\chi_2$ uniquely, even in the unramified case, as one is always free
to twist either character by an unramified character which is trivial
mod $p$.  We point out that the Hodge type of any de Rham extension of
$\chi_2$ by $\chi_1$
will be a lift of $a$.  Conversely, by Lemma~6.2 of \cite{geesavitttotallyramified} any $\chi_1,\chi_2$ satisfying
(1) and (2) such that the Hodge type of $\chi_1 \oplus \chi_2$ is a
lift of $a$ will satisfy (3) for a valid choice of $J$ and $\delta$
(unique unless $a=0$).

Suppose now that we have fixed two such characters $\chi_1$ and
$\chi_2$, and we now allow the (line corresponding to the) extension
class of $\rhobar$ in ${\operatorname{Ext}}_{G_K}(\chibar_2,\chibar_1)$ to vary. We
naturally identify ${\operatorname{Ext}}_{G_K}(\chibar_2,\chibar_1)$ with
$H^1(G_K,\chibar_1 \chibar_2^{-1})$ from now on.
\begin{defn}
  Let $L_{\chi_1,\chi_2}$ be the subset of
  $H^1(G_K,\chibar_1\chibar_2^{-1})$ such that the corresponding
  representation $\rhobar$ has a crystalline lift $\rho$ of the
  form \[
  \begin{pmatrix}
    \chi_1&*\\0&\chi_2
  \end{pmatrix}.\]
\end{defn}
We have the following variant of Lemma 3.12 of \cite{bdj}.
\begin{lem}
  \label{lem: dimension of H^1_f spaces} $L_{\chi_1,\chi_2}$ is an
  ${\overline{\F}_p}$-vector subspace of $ H^1(G_K,\chibar_1\chibar_2^{-1})$ of
  dimension $|J|+\delta$, unless
  $\chibar_1=\chibar_2$, in which case it has dimension
  $|J|+\delta+1$.
\end{lem}
\begin{proof} Let $\chi=\chi_1\chi_2^{-1}$.
  Recall that
  $H^1_f(G_K,\overline{\Z}_p(\chi))$ is the preimage of
  $H^1_f(G_K,{\overline{\Q}_p}(\chi))$ under the natural map
  $\eta : H^1(G_K,\overline{\Z}_p(\chi))\to H^1(G_K,{\overline{\Q}_p}(\chi))$, so that
  $L_{\chi_1,\chi_2}$ is the image of $H^1_f(G_K,\overline{\Z}_p(\chi))$ in
  $H^1(G_K,\chibar)$.  The kernel of $\eta$ is precisely the torsion
  part of $H^1_f(G_K,\overline{\Z}_p(\chi))$, which (since $\chi\neq 1$,
  e.g. by examining Hodge-Tate weights) is non-zero  if and only if
  $\chibar=1$, in which case it has the form $\kappa^{-1} \overline{\Z}_p/\overline{\Z}_p$
  for some  $\kappa \in \mathfrak{m}_{\overline{\Z}_p}$.

By
Proposition 1.24(2) of \cite{nekovar} we see that $\dim_{\overline{\Q}_p}
H^1_f(G_K,{\overline{\Q}_p}(\chi))=|J|+\delta$, again using $\chi \neq 1$. Since
$H^1(G_K,\overline{\Z}_p(\chi))$ is a finitely generated $\overline{\Z}_p$-module,
the result follows.
  \end{proof}
  \begin{defn}
    \label{defn: union of H^1_f subspaces}If $\chibar_1$ and
      $\chibar_2$ are fixed, we define $L_{\operatorname{crys}}$ to be the subset of
      $H^1(G_K,\chibar_1 \chibar_2^{-1})$ given by the union of the $L_{\chi_1,\chi_2}$
      over all $\chi_1$ and $\chi_2$ as above.
  \end{defn}
Note that $L_{\operatorname{crys}}$ is a union of subspaces of possibly varying
dimensions, and as such it is not clear that $L_{\operatorname{crys}}$ is itself a
subspace. Note also that the representations $\rhobar$ corresponding
to elements of $L_{\operatorname{crys}}$ are by definition precisely those for which
$F_a\inW^?(\rhobar)$.

\begin{defn}
  \label{defn: H^1_flat subspace}Let $L_{\operatorname{flat}}$ be the subset
  of $H^1(G_K,\chibar_1\chibar_2^{-1})$ consisting of classes with the property that
  if $\rhobar\cong
  \begin{pmatrix}
    \chibar_1&*\\0&\chibar_2
  \end{pmatrix}$ is the corresponding representation, then there is a
  finite field $k_E \subset {\overline{\F}_p}$  and a
  finite flat $k_E$-vector space scheme over $\mathcal{O}_{K(\pi^{1/(p-1)})}$ with
  generic fibre
  descent data to $K$ of the
  form $ \omega^{a_{1}}\oplus\omega^{a_{2}}$
(see Definition~\ref{defn:dd-of-the-form}) whose generic fibre is $\rhobar$.
\end{defn}
\begin{thm}
  \label{thm: crystalline extension implies flat}Provided that
  $a_{1}-a_{2}\ne p-1$ or that $\chibar_1\chibar_2^{-1}\ne \epsilonbar$,
  $L_{\operatorname{crys}}\subsetL_{\operatorname{flat}}$.
\end{thm}
\begin{proof}
   Take a class in $L_{\operatorname{crys}}$, and consider the corresponding
  representation  $\rhobar\cong
  \begin{pmatrix}
    \chibar_1&*\\0&\chibar_2
  \end{pmatrix}$. As remarked above, $F_a\inW^?(\rhobar)$, so by
  Theorem \ref{thm: explicit weight implies pot BT lift}, $\rhobar$
  has a crystalline lift of Hodge type $0$ and inertial
  type \[\omegat^{a_{1}}\oplus\omegat^{a_{2}},\] and this
  representation can be taken to have coefficients in the ring of
  integers $\mathcal{O}_E$ of a finite
  extension $E/\Qp$.  Let $\varpi$ be a uniformiser of $\mathcal{O}_E$, and $k_E$
  the residue field.  Such a representation
  corresponds to a $p$-divisible $\mathcal{O}_E$-module with generic fibre descent data, and
  taking the $\varpi$-torsion
  gives a finite flat $k_E$-vector space scheme with generic fibre descent
  data whose generic fibre is $\rhobar$.  By  Corollary 5.2 of \cite{geesavittquaternionalgebras} this
descent data has the form $\omega^{a_1} \oplus \omega^{a_2}$.
\end{proof}
In the next section we will make calculations with finite flat group
schemes in order to relate $L_{\operatorname{flat}}$ and $L_{\operatorname{crys}}$.

\section{Finite flat models}\label{sec: finite flat
  models}\subsection{}We work throughout this section in the following setting:
\begin{itemize}
\item $K/\Qp$ is a finite extension with ramification index $e$,
  inertial degree $1$, ring
  of integers $\mathcal{O}_K$, uniformiser $\pi$ and residue field ${\F_p}$.
\item $\chibar_1$, $\chibar_2$ are characters
  $G_K\to{\overline{\F}_p}^\times$.
\item $a\in{\mathbb{Z}}^2_+$ is a Serre weight.
\item There is a decomposition ${\operatorname{Hom}}({\F_p},{\overline{\F}_p})=J\coprod J^c$, and an integer $0\le
  \delta\le e-1$ such that \[\chibar_1|_{I_K}=\omega^\delta\prod_{\sigma\in
    J}\omega^{a_{1}+1}\prod_{\sigma\in
    J^c}\omega^{a_{2}},\] \[\chibar_2|_{I_K}=\omega^{e-1-\delta}\prod_{\sigma\in
    J^c}\omega^{a_{1}+1}\prod_{\sigma\in
    J}\omega^{a_{2}}.\]
\end{itemize}
Note in particular that $(\chibar_1\chibar_2)|_{I_K}=\omega^{a_1+a_2+e}$.

Let $K_1:=K(\pi^{1/(p-1)})$. Let $k_E$ be a finite extension of ${\F_p}$
such that $\chibar_1,\chibar_2$ are defined over $k_E$; for the moment
$k_E$ will be fixed, but eventually it will be allowed to vary.
We wish to consider the representations  $\rhobar\cong
  \begin{pmatrix}
    \chibar_1&*\\0&\chibar_2
  \end{pmatrix}$ such that there is a finite flat $k_E$-vector space
  scheme $\mathcal{G}$ over $\mathcal{O}_{K_1}$ with generic fibre descent data to $K$ of the form
  $\omega^{a_1}\oplus\omega^{a_2}$ (see Definition~\ref{defn:dd-of-the-form}), whose generic fibre is
  $\rhobar$.

In order to do, we will work with Breuil modules with descent data
from $K_1$ to~$K$. We
recall the necessary definitions from
\cite{geesavittquaternionalgebras}.

Fix $\pi_1$, a $(p-1)$-st root of $\pi$ in $K_1$. Write
$e'=e(p-1)$. The category $\operatorname{BrMod}_{\operatorname{dd}}$
consists of quadruples $(\mathcal{M},{\operatorname{Fil}}^1
\mathcal{M},\phi_{1},\{\widehat{g}\})$ where:

\begin{itemize}\item $\mathcal{M}$ is a finitely generated free
  $k_E[u]/u^{e'p}$-module,
\item ${\operatorname{Fil}}^1 {\mathcal{M}}$ is a $k_E[u]/u^{e'p}$-submodule of ${\mathcal{M}}$ containing $u^{e'}{\mathcal{M}}$,
\item $\phi_{1}:{\operatorname{Fil}}^1{\mathcal{M}}\to{\mathcal{M}}$ is $k_E$-linear and $\phi$-semilinear
  (where $\phi:{\F_p}[u]/u^{e'p}\to {\F_p}[u]/u^{e'p}$ is the $p$-th power map)
  with image generating ${\mathcal{M}}$ as a $k_E[u]/u^{e'p}$-module, and
\item $\widehat{g}:{\mathcal{M}}\to{\mathcal{M}}$ for each $g\in{\operatorname{Gal}}(K_1/K)$ are additive
  bijections that preserve ${\operatorname{Fil}}^1 {\mathcal{M}}$, commute with the $\phi_1$-,
  and $k_E$-actions, and satisfy $\widehat{g}_1\circ
  \widehat{g}_2=\widehat{g_1\circ g}_2$ for all
  $g_1,g_2\in{\operatorname{Gal}}(K_1/K)$; furthermore $\widehat{1}$ is the identity,
  and if $a\in k_E$, $m\in{\mathcal{M}}$ then
  $\widehat{g}(au^{i}m)=a((g(\pi)/\pi)^{i})u^{i}\widehat{g}(m)$.\end{itemize}

The category $\operatorname{BrMod}_{\operatorname{dd}}$ is equivalent to the category of finite
flat $k_E$-vector space schemes over $\mathcal{O}_{K_1}$ together with
descent data on the generic fibre from $K_1$ to~$K$
(this equivalence depends on $\pi_1$); see \cite{sav06}, for instance. We obtain the associated
$G_{K}$-representation (which we will refer to as the generic fibre)
of an object of $\operatorname{BrMod}_{\operatorname{dd},K_1}$ via the covariant functor
$T_{{\operatorname{st}},2}^{K}$ (which is defined immediately before Lemma 4.9 of
\cite{MR2137952}).

\begin{defn}
  \label{defn:dd-of-the-form}
  Let ${\mathcal{M}}$ be an object of $\operatorname{BrMod}_{\operatorname{dd}}$ such that the underlying
  $k_E$-module has rank two. We say that the finite flat $k_E$-vector
  space scheme corresponding to ${\mathcal{M}}$ \emph{has descent data
  of the form} $\omega^{a_1} \oplus \omega^{a_2}$ if ${\mathcal{M}}$ has a basis
 $e_1,e_2$ such that $\widehat{g}(e_i) = \omega^{a_i}(g) e_i$. (Here
 we abuse notation by identifying an element of $G_K$ with its image
 in ${\operatorname{Gal}}(K_1/K)$.)
\end{defn}

We now consider a finite flat group scheme with generic fibre descent data $\mathcal{G}$ as above. By a standard scheme-theoretic
closure argument, $\chibar_1$ corresponds to a finite flat subgroup
scheme with generic fibre descent data
$\mathcal{H}$ of $\mathcal{G}$, so we begin by analysing the possible
finite flat group schemes corresponding to characters.

Suppose now that ${\mathcal{M}}$ is an object of $\operatorname{BrMod}_{\operatorname{dd}}$. The rank
one objects of $\operatorname{BrMod}_{\operatorname{dd}}$ are classified as follows.

\begin{prop} \label{prop:rank one breuil modules} With our fixed choice of uniformiser
 $\pi$, every rank one object of $\operatorname{BrMod}_{\operatorname{dd}}$ has the form:
\begin{itemize}
\item ${\mathcal{M}} = (k_E[u]/u^{e'p}) \cdot v $,
\item ${\operatorname{Fil}}^1 {\mathcal{M}} = u^{x(p-1)} {\mathcal{M}}$,
\item $\phi_1( u^{x(p-1)} v) = cv$ for some $c \in k_E^{\times}$, and
\item $\widehat{g}(v) = \omega(g)^kv$ for all $g \in {\operatorname{Gal}}(K_1/K)$,
\end{itemize}
where $0 \le x \le e$ and $0 \le k< p-1$ are
integers.

Then $T_{{\operatorname{st}},2}^{K}({\mathcal{M}}) =
\omega^{k + x} \cdot \mathrm{ur}_{c^{-1}}$, where $\mathrm{ur}_{c^{-1}}$ is the
unramified character taking an arithmetic Frobenius element to
$c^{-1}$.\end{prop}
\begin{proof}
  This is a special case of Proposition 4.2 and Corollary 4.3 of
  \cite{geesavittquaternionalgebras}.
\end{proof}

Let ${\mathcal{M}}$ (or ${\mathcal{M}}(x)$) be the rank one Breuil
module with $k_E$-coefficients and
descent data from $K_1$ to $K$ corresponding to $\mathcal{H}$, and
write ${\mathcal{M}}$ in the form given by Proposition \ref{prop:rank one breuil
  modules}. Since $\mathcal{G}$ has descent data of the form
$\omega^{a_1}\oplus\omega^{a_2}$,
we must have  $\omega^k \in \{\omega^{a_1},\omega^{a_2}\}$.

\subsection{Extensions} Having determined the rank one characters, we
now go further and compute the possible extension
classes. By a scheme-theoretic closure argument, the Breuil module
$\mathcal{P}$ corresponding to $\mathcal{G}$ is an extension of $\mathcal{N}$ by
$\mathcal{M}$, where $\mathcal{M}$ is as in the previous section, and $\mathcal{N}$ (or
$\mathcal{N}(y)$)  is defined
by \begin{itemize}
\item ${\mathcal{N}} = (k_E[u]/u^{e'p}) \cdot w $,
\item ${\operatorname{Fil}}^1 {\mathcal{N}} = u^{y(p-1)} {\mathcal{N}}$,
\item $\phi_1( u^{y(p-1)} v) = dw$ for some $d \in k_E^{\times}$, and
\item $\widehat{g}(v) = \omega(g)^lv$ for all $g \in {\operatorname{Gal}}(K_1/K)$,
\end{itemize}
where $0 \le y \le e$ and $0 \le l< p-1$ are
integers. Now, as noted above, the descent data for $\mathcal{G}$ is of the form
$\omega^{a_1}\oplus\omega^{a_2}$, so we must have that either $\omega^k=\omega^{a_1}$
and $\omega^l=\omega^{a_2}$, or $\omega^{k}=\omega^{a_2}$ and $\omega^l=\omega^{a_1}$. Since by definition we have
$(\chibar_1\chibar_2)|_{I_K}=\omega^{a_1+a_2+e}$, we see from
Proposition \ref{prop:rank one breuil modules} that \[x+y\equiv e\pmod{p-1}.\]

  We have the following classification of extensions of $\mathcal{N}$ by $\mathcal{M}$.
\begin{prop}\label{prop: possible extensions of Breuil modules} Every extension of $\mathcal{N}$ by
  $\mathcal{M}$ is isomorphic to exactly one of the form
  \begin{itemize}
\item $\mathcal{P} = (k_E[u]/u^{e'p}) \cdot v + (k_E[u]/u^{e'p}) \cdot w $,
\item ${\operatorname{Fil}}^1 \mathcal{P} =(k_E[u]/u^{e'p}) \cdot u^{x(p-1)} v +
  (k_E[u]/u^{e'p}) \cdot (u^{y(p-1)}w+\lambda v) $,
\item $\phi_1(u^{x(p-1)} v) = cv$, $\phi_1(u^{y(p-1)}w+\lambda v)=dw$,
\item $\widehat{g}(v) =\omega^k(g)v$ and  $\widehat{g}(w) =\omega^l(g)w$ for all $g \in {\operatorname{Gal}}(K_1/K)$,
\end{itemize}where $\lambda\in u^{\max\{0,(x+y-e)(p-1)\}}k_E[u]/u^{e'p}$
has all nonzero terms of degree congruent to $l-k$ modulo $p-1$, and has all terms
of degree less than $x(p-1)$, unless $\chibar_1=\chibar_2$ and $x\ge y$,
in which case it may additionally have a term of degree $px-y$.
\end{prop}
\begin{proof}
  This is a special case of Theorem 7.5 of \cite{MR2004122}, with the
  addition of $k_E$-coefficients in place of ${\F_p}$-coefficients. When
  $K$ (in the notation of \emph{loc.~cit.}) is totally ramified over $\Qp$, the
  proof of \emph{loc.~cit.} is argued in precisely the same manner when
  coefficients are added, taking care to note the following changes:
\begin{itemize}
\item Replace Lemma 7.1 of \emph{loc.~cit.} (i.e., Lemma 5.2.2 of
  \cite{MR1839918}) with Lemma 5.2.4 of \cite{MR1839918}  (with
  $k'=k_E$ and $k={\F_p}$ in the notation of that Lemma).  In particular
  replace $t^l$ with $\phi(t)$ wherever it appears in the proof, where~$\phi$
is the $k_E$-linear endomorphism of $k_E[u]/u^{e'p}$ sending
  $u^i$ to $u^{pi}$.

\item Instead of applying Lemma 4.1 of \cite{MR2004122}, note that the
 cohomology group
 $H^1({\operatorname{Gal}}(K_1/K),k_E[u]/u^{e'p})$ vanishes because ${\operatorname{Gal}}(K_1/K)$ has prime-to-$p$
 order while $k_E[u]/u^{e'p}$ has $p$-power order.

\item Every occurrence of $T_{i}^l$ in the proof (for any subscript $i$) should be replaced with
  $T_{i}$. In the notation of \cite{MR2004122} the element $\eta$ is
  defined when the map $\alpha \mapsto  (1-b/a)\alpha$ on $k_E$ is not
  surjective, i.e., when $a=b$; we may then take $\eta=1$.

\item The coefficients of $h,t$ are permitted to lie in $k_E$
  (i.e., they are not constrained to lie in any particular proper subfield).
\end{itemize}
\end{proof}

Note that the recipe for $\mathcal{P}$ in the statement of
Proposition~\ref{prop: possible extensions of Breuil modules} defines
an extension of $\mathcal{N}$ by $\mathcal{M}$ provided that $\lambda$ lies in  $u^{\max\{0,(x+y-e)(p-1)\}}k_E[u]/u^{e'p}$
and has all nonzero terms of degree congruent to $l-k$ modulo $p-1$
(\emph{cf.} the discussion in Section 7 of \cite{MR2004122}).  Denote
this Breuil module by $\mathcal{P}(x,y,\lambda)$.    Note that $c$ is fixed
while $x$ determines
$k$, since we require $\omega^{k+x} \cdot \mathrm{ur}_{c^{-1}} =
\chibar_1$; similarly $d$ is fixed and $y$ determines $l$.  So this notation
is reasonable.

We would like to compare the generic fibres of extensions of different
choices of $\mathcal{M}$ and $\mathcal{N}$. To this end, we have the following
result.  Write
$\chibar_1|_{I_K}=\omega^\alpha$, $\chibar_2|_{I_K}=\omega^\beta$.
\begin{prop}
  \label{prop: comparing extensions}The Breuil module $\mathcal{P}(x,y,\lambda)$ has the same generic fibre as the Breuil module $\mathcal{P}'$,
  where  \begin{itemize}
\item $\mathcal{P}' = (k_E[u]/u^{e'p}) \cdot v' + (k_E[u]/u^{e'p}) \cdot w' $,
\item ${\operatorname{Fil}}^1 \mathcal{P}' =(k_E[u]/u^{e'p}) \cdot u^{e(p-1)} v' +
  (k_E[u]/u^{e'p}) \cdot (w'+u^{p(e-x)+y}\lambda v') $,
\item $\phi_1(u^{e(p-1)} v') = cv'$, $\phi_1(w'+u^{p(e-x)+y}\lambda v')=dw'$,
\item $\widehat{g}(v') =\omega^{\alpha-e}(g)v'$ and  $\widehat{g}(w') =\omega^{\beta}(g)w'$ for all $g \in {\operatorname{Gal}}(K_1/K)$.
\end{itemize}
\end{prop}
\begin{proof}
  Consider the Breuil module $\mathcal{P}''$ defined by  \begin{itemize}
\item $\mathcal{P}'' = (k_E[u]/u^{e'p}) \cdot v'' + (k_E[u]/u^{e'p}) \cdot w'' $,
\item ${\operatorname{Fil}}^1 \mathcal{P}'' =(k_E[u]/u^{e'p}) \cdot u^{e(p-1)} v'' +
  (k_E[u]/u^{e'p}) \cdot (u^{y(p-1)}w''+u^{p(e-x)}\lambda v'') $,
\item $\phi_1(u^{e(p-1)} v'') = cv''$, $\phi_1(u^{y(p-1)}w''+u^{p(e-x)+y}\lambda v'')=dw''$,
\item $\widehat{g}(v'') =\omega^{k+x-e}(g)v''$ and  $\widehat{g}(w'') =\omega^{l}(g)w''$ for all $g \in {\operatorname{Gal}}(K_1/K)$.
\end{itemize}
(One checks without difficulty that this \emph{is} a Breuil module.  For instance the condition
on the minimum degree of terms appearing in $\lambda$ guarantees that
${\operatorname{Fil}}^1 \mathcal{P}''$ contains $u ^{e'}\mathcal{P}''$.)  Note that $k+x\equiv \alpha\pmod{p-1}$,
  $l+y\equiv\beta\pmod{p-1}$.   We claim that $\mathcal{P}$, $\mathcal{P}'$ and $\mathcal{P}''$ all have the
same generic fibre. To see this, one can check directly that there is a morphism
$\mathcal{P}\to\mathcal{P}''$ given by \[v\mapsto u^{p(e-x)}v'',\ w\mapsto w'',\]and a
morphism $\mathcal{P}'\to\mathcal{P}''$ given by \[v'\mapsto v'',\ w'\mapsto
u^{py}w''.\] By Proposition 8.3 of \cite{MR2004122}, it is enough to
check that the kernels of these maps do not contain any free
$k_E[u]/(u^{e'p})$-submodules, which is an immediate consequence of
the inequalities $p(e-x),py<e'p$.
\end{proof}

\begin{rem}
  \label{rem:extension-classes}
  We note for future reference that while the classes in
  $H^1(G_K,\chibar_1 \chibar_2^{-1})$ realised by $\mathcal{P}(x,y,\lambda)$ and
  $\mathcal{P}'$ may not coincide, they differ at most by multiplication
  by a $k_E$-scalar.  To see this, observe that the maps $\mathcal{P} \to
  \mathcal{P}''$ and $\mathcal{P}' \to \mathcal{P}''$ induce $k_E$-isomorphisms on the
  rank one sub- and quotient Breuil modules.
\end{rem}

We review the constraints on the integers $x,y$: they must lie
between $0$ and~$e$, and if we let $k,l$ be the residues of
$\alpha-x,\beta-y \pmod{p-1}$ in the interval $[0,p-1)$ then we must
have $\{\omega^k,\omega^l\} = \{\omega^{a_1},\omega^{a_2}\}$. Call such a pair $x,y$ \emph{valid}.
Note that $l-k \equiv \beta-\alpha + x - y \pmod{p-1}$ for any valid pair.

\begin{cor}
  \label{cor:comparison-of-good-models}
Let $x',y'$ be another valid pair.
  Suppose that $x' + y' \le e$ and $p(x'-x)+(y -y') \ge 0$.   Then $\mathcal{P}(x,y,\lambda)$ has
  the same generic fibre as
  $\mathcal{P}(x',y',\lambda')$, where $\lambda' = u^{p(x'-x)+(y-y')} \lambda$.
\end{cor}

\begin{proof}
 The Breuil module $\mathcal{P}(x',y',\lambda')$ is well-defined: one checks
 from the definition that the congruence
 condition on the degrees of the nonzero terms in $\lambda'$ is
 satisfied, while since $x'+y' \le e$
 there is no condition on the lowest degrees appearing in $\lambda'$.
 Now the result is immediate from Proposition~\ref{prop: comparing extensions},
  since $u^{p(e-x)+y}\lambda = u^{p(e-x')+y'}\lambda'$.
\end{proof}

Recall that $x+y \equiv e\pmod{p-1}$, so that $x$ and $e-y$ have the
same residue modulo $p-1$.   It follows that if $x,y$ is a valid pair
of parameters, then so is $e-y,y$.   Let $X$ be the largest
value of $x$ over all valid pairs $x,y$, and similarly $Y$ the smallest value of $y$;
then $Y=e-X$, since if if we had $Y > e-X$ then $e-Y$ would be a
smaller  possible value for $x$.

\begin{cor}
  \label{cor:generic-fibres-all-occur-extremally}
  The module $\mathcal{P}(x,y,\lambda)$ has the same generic fibre as
  $\mathcal{P}(X,Y,\mu)$ where $\mu \in k_E[u]/u^{e'p}$ has all nonzero terms of degree congruent to $\beta-\alpha+X-Y$ modulo $p-1$, and has all terms
of degree less than $X(p-1)$, unless $\chibar_1=\chibar_2$,
in which case it may additionally have a term of degree
$pX-Y$.
\end{cor}

\begin{proof}
 Since $X+Y=e$ and $p(X-x)+(y-Y) \ge 0$ from the choice of $X,Y$, the
 previous Corollary shows that $\mathcal{P}(x,y,\lambda)$ has the same generic
 fibre as some $\mathcal{P}(X,Y,\lambda')$; by Proposition~\ref{prop:
   possible extensions of Breuil modules}   this has the same generic
 fibre as $\mathcal{P}(X,Y,\mu)$  for $\mu$ as in the statement.  (Note that
 if $\chibar_1=\chibar_2$ then automatically $X \ge Y$, because in this
 case if $x,y$ is a valid pair then so is $y,x$.)
\end{proof}

\begin{prop}
  \label{prop:computation of the dimension of Lflat}Let $X$ be as
  above, i.e., $X$ is the maximal integer
  such
  that
  \begin{itemize}
  \item $0\le X\le e$, and
  \item either $\chibar_1|_{I_K}=\omega^{a_1+X}$ or
    $\chibar_1|_{I_K}=\omega^{a_2+X}$.
  \end{itemize}
Then $L_{\operatorname{flat}}$is an ${\overline{\F}_p}$-vector space of dimension at most
$X$, unless $\chibar_1=\chibar_2$, in which case it has
dimension at most $X+1$.
\end{prop}
\begin{proof}
  Let $L_{\mathrm{flat},k_E} \subset L_{\operatorname{flat}}$ consist of the  classes $\eta$ such that the containment $\eta \in L_{\operatorname{flat}}$ is
  witnessed by a $k_E$-vector space scheme with generic fibre descent
  data.  By
  Corollary~\ref{cor:generic-fibres-all-occur-extremally} and Remark~\ref{rem:extension-classes} these are exactly
  the classes arising from the Breuil modules $\mathcal{P}(X,Y,\mu)$ with
  $k_E$-coefficients as in
  Corollary~\ref{cor:generic-fibres-all-occur-extremally}.  These
  classes form a $k_E$-vector space (since they are \emph{all} the
  extension classes arising from extensions of $\mathcal{N}(Y)$ by $\mathcal{M}(X)$),
  and by counting the (finite) number of possibilities for $\mu$ we see
  that $\dim_{k_E} L_{\mathrm{flat},k_E}$ is at most $X$ (resp.
  $X+1$ when $\chibar_1=\chibar_2$).

  Since $L_{\mathrm{flat},k_E}  \subset L_{\mathrm{flat},k'_E}$ if
  $k_E \subset k'_E$ it follows easily that $L_{\operatorname{flat}} = \cup_{k_E}
  L_{\mathrm{flat},k_E}$ is an ${\overline{\F}_p}$-vector space of dimension at
  most $X$ (resp. $X+1$).
\end{proof}
We can now prove our main local result, the promised relation between $L_{\operatorname{flat}}$ and
$L_{\operatorname{crys}}$. \begin{thm}
  \label{thm: crystalline equals flat}Provided that either $a_1-a_2\ne
  p-1$ or $\chibar_1\chibar_2^{-1}\ne\epsilonbar$, we
  have $L_{\operatorname{flat}}=L_{\operatorname{crys}}$.
\end{thm}
\begin{proof}By Theorem \ref{thm: crystalline extension implies flat},
  we know that $L_{\operatorname{crys}}\subsetL_{\operatorname{flat}}$, so by Proposition~\ref{prop:computation of the dimension of Lflat} it suffices to show that
  $L_{\operatorname{crys}}$ contains an ${\overline{\F}_p}$-subspace of dimension
  $X$ (respectively $X+1$ if $\chibar_1 = \chibar_2$). Since $L_{\operatorname{crys}}$ is the union of the spaces
  $L_{\chi_1,\chi_2}$, it suffices to show that one of these spaces
  has the required dimension. Let $X$ be as in the statement of
  Proposition \ref{prop:computation of the dimension of Lflat}, so
  that $X$ is maximal in $[0,e]$ with the property that either  $\chibar_1|_{I_K}=\omega^{a_1+X}$ or
    $\chibar_1|_{I_K}=\omega^{a_2+X}$. Note that by the assumption
    that there is a decomposition
  ${\operatorname{Hom}}({\F_p},{\overline{\F}_p})=J\coprod J^c$, and an integer
  $0\le \delta\le e-1$ such that \[\rhobar|_{I_K}\cong
  \begin{pmatrix}
\omega^\delta \prod_{\sigma\in
      J}\omega_{\sigma}^{a_{1}+1}\prod_{\sigma\in
      J^c}\omega_\sigma^{a_{2}}&*\\ 0& \omega^{e-1-\delta}\prod_{\sigma\in
      J^c}\omega_\sigma^{a_{1}+1}\prod_{\sigma\in
      J}\omega_\sigma^{a_{2}}  \end{pmatrix},\]we see that
  if $X=0$ then  $\chibar_1|_{I_K}=\omega^{a_2}$ (and $J$ must be empty).

  If $\chibar_1|_{I_K}=\omega^{a_2+X}$ then we take $J$ to be empty
  and we take $\delta=X$; otherwise $X > 0$ and $\chibar_1|_{I_K} =
  \omega^{a_1+X}$, and we can take $J^c$ to be empty and
  $\delta=X-1$. In either case, we may define characters $\chi_1$ and
  $\chi_2$ as in Section \ref{subsec: H^1_f}, and we see from Lemma
  \ref{lem: dimension of H^1_f spaces} that
  $\dim_{{\overline{\F}_p}}L_{\chi_1,\chi_2}=X$ unless $\chibar_1=\chibar_2$, in
  which case it is $X+1$. The result follows.\end{proof}
As a consequence of this result, we can also address the question of
the relationship between the different spaces $L_{\chi_1,\chi_2}$ for
a fixed Serre weight $a\inW^?(\rhobar)$. If $e$ is large, then
these spaces do not necessarily have the same dimension, so they
cannot always be equal. However, it is usually the case that the
spaces of maximal dimension coincide, as we can now see.
\begin{cor}
  \label{cor: independence of lift for H^1_f}If either $a_1-a_2\ne
  p-1$ or $\chibar_1\chibar_2^{-1}\ne\epsilonbar$, then
  the spaces $L_{\chi_1,\chi_2}$ of maximal dimension are all equal.
\end{cor}
\begin{proof}
  In this case $\dim_{{\overline{\F}_p}} L_{\chi_1,\chi_2}=\dim_{{\overline{\F}_p}}L_{\operatorname{crys}}$
  by the proof of Theorem \ref{thm: crystalline equals flat}, so we
  must have $L_{\chi_1,\chi_2}=L_{\operatorname{crys}}$.
\end{proof}
Finally, we determine $L_{\operatorname{crys}}$ in the one remaining case, where the
spaces $L_{\chi_1,\chi_2}$ of maximal dimension no longer coincide.
\begin{prop}
  \label{prop: Lcrys in the exceptional case}Suppose that
  $a_1-a_2=p-1$ and that $\chibar_1\chibar_2^{-1}=\epsilonbar$. Then $L_{\operatorname{crys}}=H^1(G_K,\epsilonbar)$.
\end{prop}
\begin{proof}We prove this in a similar fashion to the proof of Lemma
  6.1.6 of \cite{blggord}.  By twisting we can reduce to the case
  $(a_1,a_2)=(p-1,0)$.  Let $L$ be a given line in
  $H^1(G_K,\epsilonbar)$, and choose an unramified character $\psi$
  with trivial reduction.  Let
  $\chi$ be some fixed crystalline character of $G_K$ with Hodge-Tate weights
  $p,1,\dots,1$ such that $\chibar=\epsilonbar$. Let $E/\Qp$ be a finite extension with ring
  of integers $\mathcal{O}$, uniformiser $\varpi$ and residue field ${\mathbb{F}}$, such
  that $\psi$ and $\chi$ are defined over $E$ and $L$ is defined over ${\mathbb{F}}$. Since any extension of $1$ by $\chi\psi$ is
  automatically crystalline, it suffices to show that we can choose
  $\psi$ so that $L$ lifts to $H^1(G_K,\mathcal{O}(\psi\chi))$.

Let $H$ be the
  hyperplane in $H^1(G_K,\mathbb{F})$ which annihilates $L$ under the Tate
  pairing. Let $\delta_1 : H^1(G_K,\mathbb F(\overline{\epsilon})) \to
  H^2(G_K,\mathcal{O}(\psi\chi))$ be the map coming from
  the exact sequence $0\to \mathcal{O}(\psi\chi)\stackrel{\varpi}{\to}\mathcal
  O(\psi\chi)\to \mathbb F(\overline{\epsilon})\to 0$ of
  $G_K$-modules. We need to show that $\delta_1(L)=0$ for some choice
  of $\psi$.

  Let $\delta_0$ be the map
  $H^0(G_K,(E/\mathcal{O})(\psi^{-1}\chi^{-1}\epsilon)) \to
  H^{1}(G_K,\mathbb{F})$ coming from the exact sequence $0 \to \mathbb{F} \to
  (E/\mathcal{O})(\psi^{-1}\chi^{-1}\epsilon) \stackrel{\varpi}{\to}
  (E/\mathcal{O})(\psi^{-1}\chi^{-1}\epsilon) \to 0$ of $G_K$-modules.  By
  Tate local duality, the condition that $L$ vanishes under the map
  $\delta_1$ is equivalent to the condition that the image of the map
  $\delta_0$ is contained in $H$.  Let $n \geq 1$ be the largest
  integer with the property that $\psi^{-1}\chi^{-1}\epsilon \equiv 1
  \pmod{\varpi^n}$. Then we can write $\psi^{-1}\chi^{-1}\epsilon(x)=
  1+\varpi^n \alpha(x)$ for some function $\alpha : G_K \to
  \mathcal{O}$. Let $\overline{\alpha}$ denote $\alpha \pmod{\varpi} : G_K
  \to \mathbb{F}$. Then $\overline{\alpha}$ is additive and the choice of
  $n$ ensures that it is non-trivial. It is straightforward to check
  that the image of the map $\delta_0$ is the line spanned by
  $\overline{\alpha}$. If $\overline{\alpha}$ is in $H$, we are
  done. Suppose this is not the case. We break the rest of the proof
  into two cases.

  \medskip{\sl Case 1: $L$ is
    tr\`es ramifi\'e:}  To begin, we observe that it is
  possible to have chosen
  $\psi$ so that
  $\overline{\alpha}$ is ramified. To see this, let $m$ be the largest integer with the property that
$(\psi^{-1} \chi^{-1} \epsilon)|_{I_K} \equiv 1 \pmod{\varpi^m}$.   Note that $m$ exists since the
Hodge-Tate weights of $\psi^{-1}\chi^{-1}\epsilon$ are not all $0$.
If $m = n$ then we are done, so assume instead that $m >n$. Let $g\in
G_K$ be a lift of ${\operatorname{Frob}}_K$. We claim that
$\psi^{-1}\chi^{-1}\epsilon(g)= 1 +\varpi^{n} \alpha(g)$ such that
$\alpha (g) \not \equiv 0 \pmod{\varpi}$. In fact, if $\alpha
(g)\equiv 0 \pmod{\varpi}$ then $\psi^{-1}\chi^{-1}\epsilon(g) \in  1
+ \varpi^{n+1} \mathcal{O}_K$. Since $m > n$ we see that
$\psi^{-1}\chi^{-1}\epsilon(G_K) \subset 1 + \varpi^{n+1} \mathcal{O}_K$
and this contradicts the selection of $n$. Now define a unramifed
character $\psi'$ with trivial reduction by setting $\psi' (g) =
1 - \varpi^n \alpha (g)$. After replacing $\psi$ by $\psi \psi'$ we
see that $n$ has increased but $m$ has not changed.  After finitely
many iterations of this procedure we have  $m=n$, completing the
claim.

Suppose, then, that $\overline{\alpha}$ is ramified.    The fact that $L$ is tr\`es
  ramifi\'e implies that $H$ does not contain the unramified line in
  $H^1(G_K,\mathbb{F})$. Thus there is a unique $\overline{x} \in
  \mathbb{F}^\times$ such that $\overline{\alpha}+u_{\overline{x}} \in H$
  where $u_{\overline{x}}: G_K\to \mathbb{F}$ is the unramified
  homomorphism sending ${\operatorname{Frob}}_K$ to $\overline{x}$. Replacing $\psi$ with $\psi$ times
  the unramified character sending ${\operatorname{Frob}}_K$ to $(1+\varpi^n x)^{-1}$,
  for $x$ a lift of $\overline{x}$, we are done.

  \medskip{\sl Case 2: $L$ is peu ramifi\'e:} Making a ramified
  extension of $\mathcal{O}$ if necessary, we can and do assume that $n\geq
  2$. The fact that $L$ is peu ramifi\'e implies that $H$ contains the
  unramified line. It follows that if we replace $\psi$ with $\psi$
  times the unramified character sending ${\operatorname{Frob}}_K$ to $1+\varpi$, then
  we are done (as the new $\overline{\alpha}$ will be unramified).
\end{proof}

\section{Global consequences}\label{sec: global
  consequences}\subsection{}We now deduce our main global results,
using the main theorems of \cite{blggU2} together with our local
results to precisely determine the set of Serre weights for a global
representation in  the totally ramified case.

\begin{prop}
  \label{prop: semisimple elimination if totally ramified}Let $F$ be an imaginary CM field with maximal totally real
  subfield $F^+$, and suppose that $F/F^+$ is unramified at all finite
  places, that every place of $F^+$ dividing $p$ splits completely in
  $F$, and that $[F^+:{\mathbb{Q}}]$ is even. Suppose that $p>2$, and that
  $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is an irreducible modular representation
  with split ramification. Let
  $a\in({\mathbb{Z}}^2_+)_0^S$ be a Serre
  weight such that $\bar{r}$ is modular of weight $a$. Let $w$ be a
  place of $F$ such that $F_w/\Qp$ is totally ramified of degree $e$. Write
  $a_w=(a_1,a_2)$, and write $\omega$ for the unique fundamental
  character of $I_{F_w}$ of niveau one.

Then $a_w\inW^?(\bar{r}|_{G_{F_w}})$.
\end{prop}
\begin{proof}

  Suppose first that $\bar{r}|_{G_{F_w}}$ is irreducible. Then the
  proof of Lemma 5.5 of \cite{geesavitttotallyramified} goes through
  unchanged, and gives the required result. So we may suppose that
  $\bar{r}|_{G_{F_w}}$ is reducible. In this case the proof of Lemma 5.4 of
  \cite{geesavitttotallyramified} goes through unchanged, and shows
  that we have \[\bar{r}|_{G_{F_w}}\cong
  \begin{pmatrix}
    \chibar_1&*\\0&\chibar_2
  \end{pmatrix}\]where
  $(\chibar_1\chibar_2)|_{I_K}=\omega^{a_1+a_2+e}$, and either
  $\chibar_1|_{I_K}=\omega^{a_1+z}$ or
  $\chibar_1|_{I_K}=\omega^{a_2+e-z}$ for some $1\le z\le e$, so we
  are in the situation of Section \ref{subsec: H^1_f}. Consider the
  extension class in $H^1(G_{F_w},\chibar_1\chibar_2^{-1})$
  corresponding to $\bar{r}|_{G_{F_w}}$. By Proposition \ref{prop:
    modular of some weight implies potentially BT lifts exist}, either
  $a_1-a_2=p-1$ and $\chibar_1\chibar_2^{-1}=\epsilonbar$, or this extension class is in $L_{\operatorname{flat}}$. In either case,
  by Theorem \ref{thm: crystalline equals flat} and Proposition
  \ref{prop: Lcrys in the exceptional case}, the extension class is in
  $L_{\operatorname{crys}}$, so that  $a_w\inW^?(\bar{r}|_{G_{F_w}})$, as required.
\end{proof}
Combining this with Theorem 5.1.3 of \cite{blggU2}, we obtain our
final result.
\begin{thm}
  \label{thm: the main result, modular if and only if predicted}Let
  $F$ be an imaginary CM field with maximal totally real subfield
  $F^+$, and suppose that $F/F^+$ is unramified at all finite places,
  that every place of $F^+$ dividing $p$ splits completely in $F$,
  that $\zeta_p\notin F$, and that $[F^+:{\mathbb{Q}}]$ is even. Suppose that
  $p>2$, and that $\bar{r}:G_F\to\operatorname{GL}_2({\overline{\F}_p})$ is an irreducible
  modular representation with split ramification such that
  $\bar{r}(G_{F(\zeta_p)})$ is adequate. Assume that for each place $w|p$
  of $F$, $F_w/\Qp$ is totally ramified.

 Let $a\in({\mathbb{Z}}^2_+)_0^S$ be a Serre weight. Then
 $a_w\inW^?(\bar{r}|_{G_{F_w}})$ for all $w$ if and only if $\bar{r}$ is modular of
 weight $a$.
\end{thm}

\bibliographystyle{amsalpha}


\section{Overview}
ALICE (A Large Ion Collider Experiment)\cite{ALICEref} at the LHC\cite{LHCref} is a general purpose experiment designed to study the phase transition between ordinary nuclear matter and the quark-gluon plasma, which occurs in high energy nucleus-nucleus collisions.
To enhance its capabilities for measuring jet properties, the ALICE detector has been upgraded in 2010 with a large acceptance ($\Delta \eta \times \Delta \phi = 1.4 \times 1.86$ (107\textdegree)) ElectroMagnetic Calorimeter (EMCal)\cite{EMCALTDR} providing a measurement of the neutral fraction of the jet energy and an unbiased jet trigger, thanks to a centrality dependent energy threshold.

The sampling calorimeter consists of 12288 towers of layered Pb-scintillator arranged in modules of $2 \times 2$ towers, with each tower containing 77 layers for a total of 20.1 radiation lengths.
A tower is read out with an avalanche photodiode (APD) which collects, via a bundle of optical fibers, the light created by particle interactions.
A charge sensitive preamplifier (CSP) is used to instrument each APD.
A supermodule (SM) is made of 24 strips of 12 modules (1152 towers). In 2010, the EMCal comprised four SM, in 2011 there were ten SM and in 2012 the full EMCal contains ten complete SM plus two thirds of a SM.

\begin{figure}
  \begin{center}
   \includegraphics[angle=0,width=0.5\textwidth]{./figs/trigger_overview}
  \caption{Flat view of the EMCal detector with its surrounding trigger electronics.}
  \label{elec_overview}
  \end{center}
\end{figure}
A schematic view of the EMCal detector with its front-end and trigger electronics is sketched in fig.~\ref{elec_overview}.
Each SM is divided into three regions, and each region is instrumented by 12 FEE cards, so each SM has 36 FEE cards\cite{FEEpaper}.
Each FEE takes 32 analog inputs and generates eight fastOR signals. These are fast shaped (100\,ns) analog sums over one module, i.e. four tower signals.
The individual tower signals are used for energy measurement while the 3072 module analog sums (fastOR), are used to build the trigger.
The Trigger Region Unit (TRU) \cite{TRUpaper} are used to digitize, at the machine bunch crossing rate (40.08\,MHz), the fastOR signals provided by the FEE and to compute and generate the local Level 0 (L0) triggers. Finally, the Summary Trigger Unit (STU), computes the global L0 trigger by ORing the local L0 triggers.
The STU also collects and aggregates the TRU data used to compute the the two  Level 1 (L1) triggers, the photon trigger and the jet trigger.
The L1 thresholds are computed event-by-event using the ALICE beam-beam counter detector\cite{V0paper} (V0) according to a 2\textsuperscript{nd} order fit function $A \cdot V0_{count}^2 + B \cdot V0_{count}+C$, where $V0_{count}$ is the total charge information provided by the V0 and A, B, C the threshold parameters.

The communication between the TRUs and the STU is performed through 12\,m point to point cat7 Ethernet cables.
Additionally, the STU is included in the EMCal readout via a Detector Data Link\cite{DDLpaper} (DDL) to the ALICE DAQ\cite{ALICEDAQ}. The readout, which is primarily used to return the triggering indexes and thresholds used on a event-by-event basis, can also be used to provide the primitive triggering data in order to recheck off-line the on-line trigger quality.
Additionally, an Ethernet interface to the Detector Control System (DCS) interface is used for fast FPGA firmware upload and run configuration (thresholds parameters, trigger delays, etc).

\section{Trigger algorithms}
\subsection{TRU L0 algorithm}
After digitization, each fastOR is digitally integrated over a sliding time window of four samples. Then, the results of these operations are continuously fed to $2 \times 2$ spatial sum processors that compute the energy deposit in patches of $4\times4$ towers (or $2 \times 2$ fastOR) for the region managed. Each patch energy is constantly compared to a minimum bias threshold; whenever it is crossed and the maximum of the peak has been found, a local L0 trigger is fired. In preparation for the L1 algorithm, the time integrated sums are also stored in a circular buffer for later retrieval and transmission to STU.
Note that Level 0 trigger suffers from some spatial trigger inefficiencies due to the fact that TRUs cannot compute the spatial sum for patches sitting on region boundaries

\subsection{Global EMCal triggers computed in STU}
The STU is the access point to the Central trigger Processor CTP\cite{CTPpaper} for EMCal.
Consequently, it is used to provide the global L0, which is an OR of the 32 L0 locally calculated by the TRUs and two L1 triggers: the L1-gamma trigger and the L1-jet trigger.
The L1-gamma trigger uses the same patch size as L0, but without the inefficiencies displayed by the local L0 (i.e. $2\times2$ patch across several TRU regions can be computed).
The L1-jet trigger is built by summing energy over a sliding window of $4\times4$ subregions, where a subregion is defined as a $4 \times 4$ fastOR (or $8 \times 8$ towers) area, see fig.~\ref{SM_map}.
 \begin{figure}[b]
  \begin{center}
   \includegraphics[angle=0,width=0.8\textwidth]{./figs/SM_map3}
  \caption{Cartoon of different possible L0, L1-gamma and L1-jet trigger patches.}
  \label{SM_map}
  \end{center}
\end{figure}
With the given EMCal geometry and due to the various trigger patches sizes, there are a total of 2208 L0, 2961 L1-gamma and 117 L1-jet trigger patches that can be fired.

\subsection{L1 trigger processing}
A block diagram of the L1 trigger processing is shown in fig.~\ref{L1_trig_proc}.
The L1-processing is not continuously running, i.e. pipelined, it is instead initiated on the confirmed L0 reception provided by the CTP via the TTC\cite{TTCpaper} links (TRUs and STU).
At this moment, 1.2\,\textmu s after interaction, the TRUs send to the STU the appropriate time integrated data from their circular buffers to the STU via the custom serial links.
The serialization, propagation delay and deserialization takes 3075\,ns.
Meanwhile, the V0 detector transfers its charge information to the STU via a direct optical link. The thresholds for photon and jet patches are immediately processed and made available before the actual trigger processing starts.
Once the TRU data reception is achieved, the L1-photon trigger processing and also the subregion energy calculation are done in parallel for each TRU region.
Then when the previous processing is over, the L1-jet trigger starts and uses the previously generated subregion accumulation. Finally, both triggers are adequately delayed to accommodate the L1-trigger latency expected by the CTP.
More technical details about the trigger implementation may be found in \cite{STU_twepp2010}.
\begin{figure}
  \begin{center}
  \includegraphics[angle=-90,width=0.9\textwidth]{./figs/L1_trig_proc}
  \caption{Block diagram of the L1 trigger processing annotated with the time required to go through each step. }
  \label{L1_trig_proc}
  \end{center}
\end{figure}

\section{Custom serial protocol}
\subsection{Original solution}
The main motivation for the development of this custom serial link was the desire to reuse the TRU design made for the \textbf{PHO}ton \textbf{S}pectrometer (PHOS) which was equipped with a spare RJ45 connector directly linked to its FPGA.
The original trigger timing constraints drove the design in the same direction.
This solution minimizes transmission latency and meets some functional requirements, allowing the STU to be used as a low jitter reference clock distributor for TRUs.
Additionally, the fact that the local L0s had to be forwarded to STU for feeding its global OR required a custom solution.
Thus, the choice was made to use a four-pair LVDS link transported over CAT7 Ethernet cables because they have the appropriate impedance and feature low signal attenuation and low skew between pairs (see fig.~\ref{original_serial_link}).
Pair usage is as follows: one pair is dedicated for the LHC reference clock transfer to the TRU, another is used by the TRUs to forward their local L0 candidates and the two remaining are used for synchronous serial data transfer without any encoding.
Each data pair was running at 400\,Mb/s and the clock used for transfer is the LHC clock multiplied by 10.
With this very light protocol, the latency is only the sum of the cable delay and bit transmission time.
Each TRU sends simultaneously its 96 values of 12 bit coded time integrated fastOR data to the STU at 800\,Mb/s; in this case the serialization latency is thus 1.44\,\textmu s. The communication protocol was simple, outside of the data payload transmission a known inter-packet word was continuously transfered. Then at the transmission time, right after the confirmed L0 reception, a header packet was sent followed by the time-integrated data.

The link synchronization is done before each start of run by a Finite State Machine (FSM) implemented in the FPGA.
This is done in two steps. In the first step, the data phase alignment takes place, it relies on the individual data path fine granularity delaying feature available in the Virtex 5 FPGA (up to 64 steps of 78\,ps).
A scanning of all delay values is made in order to obtain the zone where data reception is stable and then the central value is applied.
In the second step, character framing is performed for associating individually each incoming bit to the good deserialized word.
This whole process is performed with the inter-packet data word used as the synchronization/training pattern.

For the link quality monitoring, error counter monitors were implemented.
These were incremented for each bad inter-packet received outside of the expected payload transmission.
The counters were checked every minute via DCS, and an alarm was raised in case of  transmission errors.
 \begin{figure}
  \begin{center}
   \includegraphics[angle=-90,width=0.85\textwidth]{./figs/original_serial_link}
  \caption{Sketch of the original custom serial link.}
  \label{original_serial_link}
  \end{center}
\end{figure}

\subsection{Problem encountered and diagnosis tool}
The original custom serial link solution was successfully validated in the laboratory and also in 2010 with four installed SM by regularly performing TRU/STU data correlation checks. Unfortunately, in 2011, when the EMCal was fully installed, several random TRU-STU links were displaying communication errors during some runs, while for all links the start of run synchronization went through correctly.
As expected from the missing links, off-line validation showed missing L1-photon triggers for the missing regions.
But, from time to time, the on-line L1 jet trigger rate (relative to accepted L0 triggers) jumped from a nominal value of 2\% to 100\% (no rejection).

In order to understand where the problem lay, a frame reception monitor was inserted in the deployed firmware.
It is able to check, for each TRU-STU link and for each event, the good or bad reception of the packet header.
The resulting reception bit mask is inserted in the data stream along with the corresponding event.
A run diagnosis example is shown in fig.~\ref{frame_errors_vs_rate} for  run 163532. It can be seen that while the error counter monitoring tool is indicating that TRU 1, 21 and 30 are badly communicating with the STU, the frame reception monitor shows that in fact TRU~1 is not communicating at all with the STU and that TRU~21 is transmitting data most of the time. Remarkably, TRU~30 has only three successful data transfers toward STU and the first one is actually causing the trigger rate increase.
This observation not only confirmed the suspected communication problem, but also revealed that there was a flaw in the L1-jet trigger algorithm implementation.
 \begin{figure}
  \begin{center}
   \includegraphics[angle=-90,width=0.95\textwidth]{./figs/frame_errors_vs_rate}
  \caption{Communication failure and trigger rate diagnosis  of run 163532.
  Top figure shown the L1 trigger rate relative to accepted L0 trigger (L1-jet in red and L1-photon in blue). This plot was obtained by dividing the error count (maximum of 65535) by 100000 and by adding the corresponding TRU number.
  Bottom left plot shows the error count recorded for every minute and for each TRU. Bottom right plot shows the frame bit received for each accepted trigger and for each TRU. This latter plot was obtained by dividinng the frame error (maximum of 1) by 2 and by adding the corresponding TRU number.
  The correlation between the first successful communication of TRU-STU link 30 and the L1-jet trigger rate increase is shown.}
  \label{frame_errors_vs_rate}
  \end{center}
\end{figure}
For fixing the communication problem, the first cure attempt was to decrease the transmission rate to $2 \times 240$\,Mb/s, thus relaxing the serial link timing constraints. This was possible in 2011, thanks to the increased timing budget for providing the candidate L1 trigger at the CTP input (from 6.2 to 7.3\,\textmu s).
Unfortunately, this did not solve the problem.
By performing a data recording at fixed latency after confirmed L0 reception, instead of recording the payload after a packet header reception, the issue was found to be due to the serialization/deserialization.
While the synchronization seemed good, sometimes a cycle delay between the LSB part and MSB part of the transmitted data word appeared. Obviously, this problem could not be observed at the synchronization time with a single word training pattern.
Therefore, the second, and successful, cure applied was to use a three word training pattern, in conjunction with the possibility to delay the MSB or the LSB part of a word during the synchronization phase.

\section{Correcting fake and missing triggers fixing, from simulation to on-line debugging}
From the early development stage of the hardware and firmware, gateway tools were developed to exchange data between ``physics and ''firmware`` simulations, as shown schematically in fig.~\ref{vhdl_aliroot}.
This allowed for the validation of the core STU algorithms (jet and photon) before deployment and, as a side benefit, for the quick adaptation of gateway tools --- such as the trigger index decoding routine --- to the off-line software.

While these tools were useful in the early stage of development, they were limited.
For instance, the firmware simulation is slow. Moreover, it is not easy to validate all possible external effects which could cause false and/or missing triggers.
Examples of such possible effects include communication breakdown, clock jitter, and other, not necessarily predictable, issues.
Consequently, an ``event player'' feature was added in the STU firmware.
As shown in fig.~\ref{L1_trig_proc_pattern}, this on-line tool allows to select the data to be used by the trigger processors between the TRU received data and DCS preloaded data.
The ``event player'' can play up to eight different patterns.
It offers the possibility of validating the entire L1 algorithms in-situ and to check the compliance with the ALICE DAQ after each data packet modification.
Additionally, it may be used to accumulate statistics to check for eventual timing issues or other such as radiations effect.
Thanks to this debugging tool, the L1-jet trigger rate issue was pinpointed to the missing ``sub-region'' buffer clearing when serial communication links failed.
Indeed, for flaky links, the ``sub-region'' computed from the last correctly received data were constantly used by the L1-jet processor.
Hence, when the last received information contained a high energy event, the subsequent L0 confirmed event were mistakenly accepted at L1.
After correcting this problem, both L1 triggers performed as expected, as detailed in the next section.
 \begin{figure}
  \begin{center}
   \includegraphics[angle=-90,width=0.6\textwidth]{./figs/vhdl_aliroot}
  \caption{Overview of the ``physics'' and ``firmware'' co-simulation.}
  \label{vhdl_aliroot}
  \end{center}
\end{figure}

 \begin{figure}
  \begin{center}
   \includegraphics[angle=-90,width=0.8\textwidth]{./figs/L1_trig_proc_pattern}
  \caption{Modified STU firmware featuring the ``event-player'' allowing to select the data source to be used by the trigger processor between the TRU received data and DCS preloaded data. The buffer causing fake L1-jet trigger when not reseted between event and with flaky communication is colored in orange. }
  \label{L1_trig_proc_pattern}
  \end{center}
\end{figure}

\section{Trigger performance}
As an illustration of the trigger performance during the 2011 lead beam period (Pb-Pb collisions at $\sqrt{s_{NN}}$ = 2.76\,TeV), the event selection as a function of the centrality of the collision for Minimum Bias (MB) and EMCal L1-jet trigger classes is shown in fig.~\ref{plot_efficacite}.
The upper plot shows the minimum bias\footnote{The minimum bias trigger class is composed of the coincidence of the V0 detector L0 trigger signal and the ZDC L1 trigger signal (Zero Degree Calorimeter).} and L1-jet samples\footnote{The L1-jet sample is a subsample of the MB, obtained with the coincidence with EMCal L1 jet trigger signal.} for a linear energy threshold with two threshold parameter sets.
The lower plot shows the MB to L1-jet ratios for the different threshold parameters.
The set of parameters giving the magenta distribution rejects too many central events, while the set of of parameters giving the red one is more uniform for V0A + V0C signal above 5000 ADC.
The L1 trigger could provide a uniform background rejection, in a large centrality region, while disfavoring the most peripheral events.
This behavior, inherent to the order of the threshold computation, will be improved by using a second order centrality dependent energy threshold.
 \begin{figure}
  \begin{center}
   \includegraphics[angle=0,width=0.4\textwidth]{./figs_ext/plot_efficacite}
  \caption{Plot of the event selection for Pb/Pb in 2011. The event yields versus the centrality for minimum bias and EMCal L1-jet trigger classes is shown.
  The lower plot shows the MB to L1-jet ratios distribution for the different threshold parameters. Horizontal scale is the total amount of V0 charge expressed in ADC counts.}
  \label{plot_efficacite}
  \end{center}
\end{figure}

As shown on the left of fig.~\ref{spatial_uniformity}, a spatial non uniformity in jet triggers was observed.
While the APD inter-calibration was done in the laboratory using cosmics, an in-situ calibration was performed using $\pi^0$ data at the end of 2011.
The calibration constants obtained roughly reproduce the trigger non uniformity.
This modified APD inter-calibration correction was used in 2012, further detailed analysis are required to assess the correction benefit.
 \begin{figure}
  \begin{center}
  \includegraphics[angle=0,width=0.48\textwidth]{./figs_ext/L1PatchPosition}
  \includegraphics[angle=0,width=0.45\textwidth]{./figs_ext/coefCalib}
  \caption{Left plot shows the occurrence of a jet trigger patch, a factor of six can be observed between the most active and the least active patch. Right plot shows in-situ calibration constants using $\pi^0$  for each jet trigger patch.}
  \label{spatial_uniformity}
  \end{center}
\end{figure}

\section{Perspectives}
Thanks to STU flexibility (available FPGA resources and spare trigger outputs), a 2\textsuperscript{nd} set of threshold parameters has been implemented to improve the L1 data sample overlap with the MB data sample. The resource usage increased from 69\% to 92\%.
In mid-2013, ALICE is forecasted to be upgraded with the Di-jet CALorimeter, that will increase the coverage by $\Delta \eta \times \Delta \phi = 1.4 \times 100$\textdegree (PHOS included).
It is composed of six shorter SM that will be installed on either side of the PHOS. For this operation one or two STUs will be used, depending whether PHOS will be included in the DCAL trigger or not (one STU for DCAL, one STU for PHOS).

\section{Summary}
The STU has been installed for two years, and all the system interfaces have been validated.
The custom serial protocol, which has been modified, has been demonstrated to operate in realistic conditions with intensive readout.
The fast FPGA remote configuration proved to be an asset for regular upgrades and problem solving. Moreover, it has been noted that from early on, it is an advantage to implement monitoring tools and to develop diagnosis tools. For instance, the ``event player'' demonstrated to be good tool for in situ validation of trigger algorithm without beam.