# flake8: noqa


"""
This set of options is used for reconstruction development purposes,
and assumes that the input contains MCHits (i.e. is of `Exended`
DST/digi type).
author:     Furkan Cetin
date:       10/2023
"""

from Moore import options, run_reconstruction

from Moore.config import Reconstruction
from PyConf.Algorithms import (
    PrMatchNN,
    fromPrMatchTracksV1Tracks,
)
from PyConf.Tools import TrackMasterExtrapolator
from RecoConf.hlt1_tracking import make_all_pvs
from RecoConf.event_filters import require_gec
from RecoConf.mc_checking import (
    check_track_resolution,
    check_tracking_efficiency,
    get_mc_categories,
    get_hit_type_mask,
    make_links_lhcbids_mcparticles_tracking_system,
    make_links_tracks_mcparticles,
    get_track_checkers,
    get_fitted_tracks_checkers,
)
from RecoConf.core_algorithms import make_unique_id_generator
from RecoConf.hlt2_tracking import (
    make_hlt2_tracks,
    get_default_out_track_types_for_light_reco,
    get_global_ut_hits_tool,
)
from RecoConf.hlt1_tracking import (
    make_VeloClusterTrackingSIMD_hits,
    make_PrStorePrUTHits_hits,
    make_PrStoreSciFiHits_hits,
    get_global_materiallocator,
)
import glob

decay = "BJpsi"

options.evt_max = -1

# options.ntuple_file = f"/work/cetin/LHCb/reco_tuner/data_matching/sample4_data/match_effs_{decay}_EFilter.root"
options.ntuple_file = f"/work/cetin/LHCb/reco_tuner/data_matching/sample4_data/match_effs_{decay}_Default.root"

options.input_type = "ROOT"


if decay == "B":
    options.input_files = glob.glob("/auto/data/guenther/Bd_Kstee/*.xdigi")
elif decay == "BJpsi":
    options.input_files = glob.glob("/auto/data/guenther/Bd_JpsiKst_ee/*.xdigi")
elif decay == "D":
    options.input_files = glob.glob("/auto/data/guenther/Dst_D0ee/*.xdigi")
elif decay == "testJpsi":
    options.input_files = [
        "/auto/data/guenther/Bd_JpsiKst_ee/00143565_00000009_1.xdigi",
        "/auto/data/guenther/Bd_JpsiKst_ee/00143565_00000059_1.xdigi",
        "/auto/data/guenther/Bd_JpsiKst_ee/00143565_00000020_1.xdigi",
    ]
elif decay == "test":
    options.input_files = ["/auto/data/guenther/Bd_Kstee/00151673_00000002_1.xdigi"]

options.dddb_tag = "dddb-20210617"
options.conddb_tag = "sim-20210617-vc-md100"
options.simulation = True
options.output_level = 3


def run_tracking_debug():
    tracks = make_hlt2_tracks(light_reco=True, fast_reco=False, use_pr_kf=True)

    matching_params = dict(
        MinMatchNN=0.215,  # NN response cut value
    )

    match_long = PrMatchNN(
        VeloInput=tracks["Velo"]["Pr"],
        SeedInput=tracks["Seed"]["Pr"],
        AddUTHitsToolName=get_global_ut_hits_tool(),
        **matching_params,
    ).MatchOutput

    match_tracks = {}
    match_tracks["Pr"] = match_long
    match_tracks["v1"] = fromPrMatchTracksV1Tracks(
        InputTracksLocation=match_tracks["Pr"],
        VeloTracksLocation=tracks["Velo"]["v1"],
        SeedTracksLocation=tracks["Seed"]["v1"],
    ).OutputTracksLocation

    types_and_locations_for_checkers = {
        "Forward": tracks["Forward"],
        "Seed": tracks["Seed"],
        "Match": match_tracks,
    }

    best_tracks = {
        "BestLong": tracks["BestLong"],
    }

    data = [match_long]
    data += get_track_checkers(types_and_locations_for_checkers)
    # data += get_fitted_tracks_checkers(best_tracks, fitted_track_types=["BestLong"])

    return Reconstruction("hlt2_matching_reco", data, [require_gec()])


run_reconstruction(options, run_tracking_debug)
