/**
   * Tun ester2 diarization.
   *
   * @param parameter the parameter
   * @param clusterSet the cluster set
   * @return the tree map
   * @throws DiarizationException the diarization exception
   * @throws Exception the exception
   */
  public TreeMap<String, DiarizationResultList> tunEster2Diarization(
      Parameter parameter, ClusterSet clusterSet) throws DiarizationException, Exception {

    TreeMap<String, DiarizationResultList> result = new TreeMap<String, DiarizationResultList>();

    // double paramThr = parameter.getParameterClustering().getThreshold();
    lMin = parameter.getParameterDiarization().getThreshold("l");
    lMax = parameter.getParameterDiarization().getMaxThreshold("l");
    hMin = parameter.getParameterDiarization().getThreshold("h");
    hMax = parameter.getParameterDiarization().getMaxThreshold("h");
    dMin = parameter.getParameterDiarization().getThreshold("d");
    dMax = parameter.getParameterDiarization().getMaxThreshold("d");
    cMin = parameter.getParameterDiarization().getThreshold("c");
    cMax = parameter.getParameterDiarization().getMaxThreshold("c");

    String featureDesc = parameter.getParameterInputFeature().getFeaturesDescriptorAsString();

    AudioFeatureSet featureSet = null;
    ClusterSet clustersSegInit = null;

    if (parameter.getParameterDiarization().isLoadInputSegmentation() == false) {
      featureSet = loadFeature(parameter, clusterSet, featureDesc);
      featureSet.setCurrentShow(parameter.show);
      int nbFeatures = featureSet.getNumberOfFeatures();
      clusterSet.getFirstCluster().firstSegment().setLength(nbFeatures);
      clustersSegInit = sanityCheck(clusterSet, featureSet, parameter);
    } else {
      featureSet = loadFeature(parameter, clusterSet, featureDesc);
      featureSet.setCurrentShow(parameter.show);
      clustersSegInit = sanityCheck(clusterSet, featureSet, parameter);
      featureSet = loadFeature(parameter, clustersSegInit, featureDesc);
      featureSet.setCurrentShow(parameter.show);
    }

    // seg IRIT
    // ClusterSet clustersSegSave = clustersSegInit;
    // seg IRIT
    ClusterSet referenceClusterSet = MainTools.readTheSecondClusterSet(parameter);
    ClusterSet uemClusterSet = MainTools.readThe3rdClusterSet(parameter);

    if (parameter.getParameterDiarization().isLastStepOnly()) {
      String key = "l=" + lMin + " h=" + hMin + " d=" + dMin;
      DiarizationResultList values = null;
      if (parameter.getParameterDiarization().isCEClustering() == false) {
        logger.warning(" nothing to do isCEClustering == false");
      } else {
        values =
            tunEster2SpeakerCLRClustering(
                referenceClusterSet,
                uemClusterSet,
                key,
                "ce",
                clusterSet,
                clusterSet,
                featureSet,
                parameter);
      }
      result.put(key, values);
      return result;
    }

    ClusterSet clustersSegSave =
        segmentation("GLR", "FULL", clustersSegInit, featureSet, parameter);
    for (double l = lMin; l <= lMax; l += 0.5) {
      ClusterSet clustersSeg = clustersSegSave.clone();
      logger.finest("clustering l=" + l);
      ClusterSet clustersLClust = clusteringLinear(l, clustersSeg, featureSet, parameter);
      // ---- Begin NEW v 1.14 ---
      for (double h = hMin; h <= hMax; h += 0.5) {
        // for (double h = hMin; h <= hMax; h += 0.2) {
        // ---- end NEW v 1.14 ---
        // if (h > l) {
        ClusterSet clustersHClust = clustering(h, clustersLClust, featureSet, parameter);
        for (double d = dMin; d <= dMax; d += 50) {
          ClusterSet clustersDClust = decode(8, d, clustersHClust, featureSet, parameter);
          // double error = DiarizationError.scoreOfMatchedSpeakers(referenceClusterSet,
          // clustersDClust);
          ClusterSet clustersSplitClust =
              speech(
                  "10,10,50", clusterSet, clustersSegInit, clustersDClust, featureSet, parameter);
          ClusterSet clustersGender = gender(clusterSet, clustersSplitClust, featureSet, parameter);

          String key = "l=" + l + " h=" + h + " d=" + d;
          DiarizationResultList values = null;
          if (parameter.getParameterDiarization().isCEClustering() == false) {
            values = new DiarizationResultList(0, 0, 1);
            DiarizationError computeError =
                new DiarizationError(referenceClusterSet, uemClusterSet);
            DiarizationResult error = computeError.scoreOfMatchedSpeakers(clustersGender);
            values.setResult(0, 0, error);
            logger.finer(parameter.show + " key=" + key + " resultat du fichier");
            values.log("partial result: " + parameter.show + " " + key);
          } else {
            // V4.19 = CLUST_H_BIC_GMM_MAP
            // values = tunEster2SpeakerCLRClustering(referenceClusterSet, key, "bicgmmmap",
            // clustersGender, clustersGender, featureSet, parameter);
            // V5.16 = ce_d
            // values = tunEster2SpeakerCLRClustering(referenceClusterSet, key, "ce_d",
            // clustersGender, clustersGender, featureSet, parameter);
            values =
                tunEster2SpeakerCLRClustering(
                    referenceClusterSet,
                    uemClusterSet,
                    key,
                    "ce",
                    clustersGender,
                    clustersGender,
                    featureSet,
                    parameter);
          }
          if (result.containsKey(key)) {
            result.get(key).addResultArray(values);
          } else {
            result.put(key, values);
          }
        }
        // }
      }
    }
    return result;
  }
  /**
   * Ester2 version.
   *
   * @param parameter the parameter
   * @throws DiarizationException the diarization exception
   * @throws Exception the exception
   */
  public void ester2Version(Parameter parameter) throws DiarizationException, Exception {

    // ** Caution this system is developed using Sphinx MFCC computed with legacy mode
    ClusterSet referenceClusterSet = null;
    if (!parameter.getParameterSegmentationInputFile2().getMask().equals("")) {
      referenceClusterSet = MainTools.readTheSecondClusterSet(parameter);
    }
    ClusterSet uemClusterSet = null;
    if (!parameter.getParameterSegmentationInputFile3().getMask().equals("")) {
      referenceClusterSet = MainTools.readThe3rdClusterSet(parameter);
    }
    ParameterBNDiarization parameterDiarization = parameter.getParameterDiarization();
    // ** mask for the output of the segmentation file

    ClusterSet clusterSet = initialize(parameter);

    // ** load the features, sphinx format (13 MFCC with C0) or compute it form a wave file
    AudioFeatureSet featureSet =
        loadFeature(
            parameter,
            clusterSet,
            parameter.getParameterInputFeature().getFeaturesDescriptorAsString());
    featureSet.setCurrentShow(parameter.show);
    int nbFeatures = featureSet.getNumberOfFeatures();
    if (parameter.getParameterDiarization().isLoadInputSegmentation() == false) {
      clusterSet.getFirstCluster().firstSegment().setLength(nbFeatures);
    }
    // clusterSet.debug(3);
    ClusterSet clustersSegInit = sanityCheck(clusterSet, featureSet, parameter);
    ClusterSet clustersSeg = segmentation("GLR", "FULL", clustersSegInit, featureSet, parameter);
    // Seg IRIT
    // ClusterSet clustersSegInit = clusterSet;
    // ClusterSet clustersSeg = clusterSet;
    // Seg IRIT
    ClusterSet clustersLClust =
        clusteringLinear(
            parameterDiarization.getThreshold("l"), clustersSeg, featureSet, parameter);
    ClusterSet clustersHClust =
        clustering(parameterDiarization.getThreshold("h"), clustersLClust, featureSet, parameter);
    // MainTools.writeClusterSet(parameter, clustersHClust, false);

    ClusterSet clustersDClust =
        decode(8, parameterDiarization.getThreshold("d"), clustersHClust, featureSet, parameter);
    ClusterSet clustersSplitClust =
        speech("10,10,50", clusterSet, clustersSegInit, clustersDClust, featureSet, parameter);
    ClusterSet clustersGender = gender(clusterSet, clustersSplitClust, featureSet, parameter);

    if (parameter.getParameterDiarization().isCEClustering()) {
      ClusterSet clustersCLR =
          speakerClustering(
              parameterDiarization.getThreshold("c"),
              "ce",
              clustersSegInit,
              clustersGender,
              featureSet,
              parameter);
      MainTools.writeClusterSet(parameter, clustersCLR, false);
      if (referenceClusterSet != null) {
        DiarizationError computeError = new DiarizationError(referenceClusterSet, uemClusterSet);
        computeError.scoreOfMatchedSpeakers(clustersCLR);
      }
    } else {
      MainTools.writeClusterSet(parameter, clustersGender, false);
    }
  }