# flake8: noqaq

"""
NOT IMPLEMENTED YET


Moore/run gaudirun.py /work/cetin/LHCb/reco_tuner/moore_options/residual_get_ghost_data.py
"""

from Moore import options, run_reconstruction
from Moore.config import Reconstruction
from PyConf.Algorithms import (
    PrForwardTrackingVelo,
    PrForwardTracking,
    PrTrackAssociator,
    PrMatchNN,
)
from PyConf.application import make_data_with_FetchDataFromFile
from PyConf.Tools import PrMCDebugForwardTool, PrMCDebugMatchToolNN
from RecoConf.data_from_file import mc_unpackers
from RecoConf.hlt1_tracking import make_hlt1_tracks, make_PrStoreSciFiHits_hits
from RecoConf.hlt2_tracking import get_global_ut_hits_tool, make_PrHybridSeeding_tracks
from RecoConf.mc_checking import make_links_lhcbids_mcparticles_tracking_system
import glob

options.evt_max = -1

decay = "B"  # D, B
options.ntuple_file = f"data/ghost_data_{decay}.root"
options.input_type = "ROOT"

if decay == "B":
    options.input_files = glob.glob("/auto/data/guenther/Bd_Kstee/*.xdigi")
elif decay == "D":
    options.input_files = glob.glob("/auto/data/guenther/Dst_D0ee/*.xdigi")
elif decay == "test":
    options.input_files = ["/auto/data/guenther/Bd_Kstee/00151673_00000002_1.xdigi"]
elif decay == "both":
    options.input_files = glob.glob("/auto/data/guenther/Bd_Kstee/*.xdigi") + glob.glob(
        "/auto/data/guenther/Dst_D0ee/*.xdigi"
    )

options.dddb_tag = "dddb-20210617"
options.conddb_tag = "sim-20210617-vc-md100"

options.simulation = True


def run_tracking_debug():
    links_to_hits = make_links_lhcbids_mcparticles_tracking_system()
    hlt1_tracks = make_hlt1_tracks()
    seed_tracks = make_PrHybridSeeding_tracks()

    # add MCLinking to the (fitted) V1 tracks
    links_to_velo_tracks = PrTrackAssociator(
        SingleContainer=hlt1_tracks["Velo"]["v1"],
        LinkerLocationID=links_to_hits,
        MCParticleLocation=mc_unpackers()["MCParticles"],
        MCVerticesInput=mc_unpackers()["MCVertices"],
    ).OutputLocation

    links_to_upstream_tracks = PrTrackAssociator(
        SingleContainer=hlt1_tracks["Upstream"]["v1"],
        LinkerLocationID=links_to_hits,
        MCParticleLocation=mc_unpackers()["MCParticles"],
        MCVerticesInput=mc_unpackers()["MCVertices"],
    ).OutputLocation

    links_to_seed_tracks = PrTrackAssociator(
        SingleContainer=seed_tracks["v1"],
        LinkerLocationID=links_to_hits,
        MCParticleLocation=mc_unpackers()["MCParticles"],
        MCVerticesInput=mc_unpackers()["MCVertices"],
    ).OutputLocation

    # be more robust against imperfect data
    loose_forward_params = dict(
        MaxChi2PerDoF=16,
        MaxChi2XProjection=30,
        MaxChi2PerDoFFinal=8,
        MaxChi2Stereo=8,
        MaxChi2StereoAdd=8,
    )

    forward_debug = PrForwardTrackingVelo(
        InputTracks=hlt1_tracks["Velo"]["Pr"],
        SciFiHits=make_PrStoreSciFiHits_hits(),
        AddUTHitsToolName=get_global_ut_hits_tool(enable=True),
        DebugTool=PrMCDebugForwardTool(
            InputTracks=hlt1_tracks["Velo"]["v1"],
            InputTrackLinks=links_to_velo_tracks,
            MCParticles=mc_unpackers()["MCParticles"],
            SciFiHitLinks=links_to_hits,
            SciFiHits=make_PrStoreSciFiHits_hits(),
            TrackInfo=make_data_with_FetchDataFromFile("/Event/MC/TrackInfo"),
        ),
        **loose_forward_params,
    )

    forward_ut_debug = PrForwardTracking(
        SciFiHits=make_PrStoreSciFiHits_hits(),
        InputTracks=hlt1_tracks["Upstream"]["Pr"],
        AddUTHitsToolName=get_global_ut_hits_tool(enable=True),
        DebugTool=PrMCDebugForwardTool(
            InputTracks=hlt1_tracks["Upstream"]["v1"],
            InputTrackLinks=links_to_upstream_tracks,
            MCParticles=mc_unpackers()["MCParticles"],
            SciFiHitLinks=links_to_hits,
            SciFiHits=make_PrStoreSciFiHits_hits(),
            TrackInfo=make_data_with_FetchDataFromFile("/Event/MC/TrackInfo"),
        ),
        **loose_forward_params,
    )

    loose_matching_params = dict(
        MaxMatchChi2=30.0,
        MaxDistX=500,
        MaxDistY=500,
        MaxDSlope=1.5,
        MinMatchNN=0.45,  # NN response cut value
    )

    match_debug = PrMatchNN(
        VeloInput=hlt1_tracks["Velo"]["Pr"],
        SeedInput=seed_tracks["Pr"],
        MatchDebugToolName=PrMCDebugMatchToolNN(
            VeloTracks=hlt1_tracks["Velo"]["v1"],
            SeedTracks=seed_tracks["v1"],
            VeloTrackLinks=links_to_velo_tracks,
            SeedTrackLinks=links_to_seed_tracks,
            TrackInfo=make_data_with_FetchDataFromFile("/Event/MC/TrackInfo"),
            MCParticles=mc_unpackers()["MCParticles"],
        ),
        AddUTHitsToolName=get_global_ut_hits_tool(enable=True),
        **loose_matching_params,
    ).MatchOutput

    data = [forward_debug, forward_ut_debug, match_debug]

    return Reconstruction("run_tracking_debug", data)


run_reconstruction(options, run_tracking_debug)
