diff --git a/image_analysis/lattice_search/LatticeSearch.cpp b/image_analysis/lattice_search/LatticeSearch.cpp index ff58afb1e..dd22b9fc8 100644 --- a/image_analysis/lattice_search/LatticeSearch.cpp +++ b/image_analysis/lattice_search/LatticeSearch.cpp @@ -569,13 +569,14 @@ std::optional LatticeSearchForGroup(const CrystalLattice &L std::optional LatticeSearchForSystem(const CrystalLattice &L, gemmi::CrystalSystem system) { const bool hexagonal_family = system == gemmi::CrystalSystem::Hexagonal || system == gemmi::CrystalSystem::Trigonal; + std::optional best; for (const char c : {'P', 'C', 'A', 'B', 'I', 'F', 'R'}) { const auto s = !hexagonal_family ? system : c == 'R' ? gemmi::CrystalSystem::Trigonal : gemmi::CrystalSystem::Hexagonal; - if (auto r = LatticeSearchForClass(L, s, c)) - return r; + if (auto r = LatticeSearchForClass(L, s, c); r && (!best || MetricDeviation(*r) < MetricDeviation(*best))) + best = r; } - return std::nullopt; + return best; } int LatticePointGroupOrder(gemmi::CrystalSystem system) { diff --git a/image_analysis/lattice_search/LatticeSearch.h b/image_analysis/lattice_search/LatticeSearch.h index 2c7b85910..28419d6fe 100644 --- a/image_analysis/lattice_search/LatticeSearch.h +++ b/image_analysis/lattice_search/LatticeSearch.h @@ -57,8 +57,10 @@ std::optional LatticeSearchForClass(const CrystalLattice& L // metric cannot carry the group's system at all. std::optional LatticeSearchForGroup(const CrystalLattice &L, const gemmi::SpaceGroup &sg); -// The lattice of a crystal system the user fixed (the cell given with -C): the system's centrings are -// tried in turn, primitive first, as the cell typed cannot say which the crystal has and the data can. +// The lattice of a crystal system the user fixed (the cell given with -C): of the system's centrings +// the metric carries - the cell typed cannot say which the crystal has, the data can - the one it fits +// best. Primitive first on a tie: a C-centred monoclinic lattice with beta near 90 also passes, 2 deg +// off, as a primitive one, and that cell is not the crystal's. // The hexagonal family takes a rhombohedral lattice as well. Empty where the metric carries none. std::optional LatticeSearchForSystem(const CrystalLattice &L, gemmi::CrystalSystem system);