###############################################################################
# (c) Copyright 2024 CERN for the benefit of the LHCb Collaboration           #
#                                                                             #
# This software is distributed under the terms of the GNU General Public      #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING".   #
#                                                                             #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization  #
# or submit itself to any jurisdiction.                                       #
###############################################################################
"""
Run Moore with the RD lines.

This options file will produce three files

- 'hlt2_bandwidth_{stream_name}_{sample}.mdf': main output file with the HLT2
  information
- 'hlt2_bandwidth_{stream_name}_{sample}_histos.root': ROOT file with the monitoring
  histograms
- 'hlt2_bandwidth_{stream_name}_{sample}.tck.json': the manifest file of the TCK

where "stream_name" and "sample" are placholders and can be modified (see below).

Run it simply with

Moore/build.x86_64_v2-el9-gcc13-opt/run gaudirun.py rd-lines.py

for data and

Moore/build.x86_64_v2-el9-gcc13+detdesc-opt/run gaudirun.py rd-lines.py

for simulation. Therefore you need two builds of the stack to be able to
test both data and simulation.
"""
#
# Configuration options:
#
# These lines define the different parameters that
# can be changed and that differ from the standard
# Moore test.
#
stream_name = 'rd'
evt_max = 100000 # can be increased if necessary
sample = 'data' # or 'mc'
threads = 10 # modify according to your machine specs
#
from Moore import options, run_moore
from Moore.streams import DETECTORS, Stream, Streams
from Hlt2Conf.settings.hlt2_binds import config_pp_2024
from RecoConf.global_tools import stateProvider_with_simplified_geom, trackMasterExtrapolator_with_simplified_geom
from Hlt2Conf.lines.rd import all_lines
from RecoConf.reconstruction_objects import reconstruction

def make_streams() -> Streams:
    streams = [
        Stream(
            stream_name,
            lines=[builder()
                   for builder in all_lines.values()],  # whole module
            routing_bit=85,  # some dummy value != 94 or 95
            # lines=[line_to_run()] # single line
            detectors=[])  # Turbo and Full case - no detector raw banks
        # detectors=DETECTORS) # if persisting detector raw banks i.e. special cases or TurCal lines
    ]
    return Streams(streams=streams)

from DDDB.CheckDD4Hep import UseDD4Hep

if UseDD4Hep:
    from Configurables import LHCb__Det__LbDD4hep__DD4hepSvc as DD4hepSvc
    dd4hepSvc = DD4hepSvc()
    dd4hepSvc.ConditionsLocation = 'git:/cvmfs/lhcb.cern.ch/lib/lhcb/git-conddb/lhcb-conditions-database.git'
    # This needs to happen before the public tools are instantiated,
    # which means we cannot put it inside make_streams().
    from PyConf.Tools import TrackMasterExtrapolator, TrackMasterFitter
    TrackMasterExtrapolator.global_bind(
        ApplyMultScattCorr=False,
        ApplyEnergyLossCorr=False,
        ApplyElectronEnergyLossCorr=False)
    TrackMasterFitter.global_bind(ApplyMaterialCorrections=False)

if sample == 'mc':
    options.set_input_and_conds_from_testfiledb(
        'expected_2024_min_bias_hlt1_filtered_v2')
elif sample == 'data':
    # average HLT input rate: 1289692.0647585
    # beta star: 2
    # average mu: 5.3159537813416
    # average luminosity: 2011.1238479377
    #
    # No downstream lines present in HLT1
    #
    options.input_files = [
        f"mdf:root://eoslhcb.cern.ch//eos/lhcb/wg/rta/WP3/mdfs_hlt1filtered_307586/Run_0000307586_20241003-{s}.mdf"
        for s in [ 
            '184935-211_MAEB04_1490', '184940-296_UCEB01_1491',
            '184941-498_UAEB16_1489', '184941-807_R1EB16_1492',
            '184943-556_ECEB09_1492', '184946-863_ECEB07_1492',
            '184952-419_R1EB19_1497', '184955-753_R1EB06_1497',
            '185001-203_MAEB02_1500', '185002-425_R1EB19_1500',
            '185003-695_UCEB18_1494', '185005-001_UCEB07_1495',
            '185008-273_SCEB07_1497', '185015-847_R1EB19_1504',
            '185016-688_UAEB03_1502', '185032-064_UCEB01_1507',
            '185032-604_UCEB05_1503', '185033-321_UCEB11_1505',
            '185036-996_SCEB09_1507', '185042-714_R1EB19_1512',
            '185053-279_SAEB08_1511', '185053-706_UCEB06_1509',
            # ... there are a lot more that can be checked on EOS
        ]
    ]
    options.geometry_version = 'run3/2024.Q1.2-v00.00'
    options.conditions_version = 'master'
    options.conddb_tag = 'run3/2024.Q1.2-v00.00'
    options.dddb_tag = 'master'
    options.simulation = False
    options.input_type = 'MDF'
    options.data_type = 'Upgrade'
else:
    raise NotImplementedError(f'Unable to process sample "{sample}"')

options.input_raw_format = 0.5
options.evt_max = evt_max
options.n_threads = threads
options.scheduler_legacy_mode = False
options.output_file = f'hlt2_bandwidth_{stream_name}_{sample}.mdf'
options.output_type = 'MDF'
options.output_manifest_file = f'hlt2_bandwidth_{stream_name}_{sample}.tck.json'
options.histo_file = options.output_file.replace('.mdf', '_histos.root')

with reconstruction.bind(from_file=False), config_pp_2024():
    public_tools = [
        trackMasterExtrapolator_with_simplified_geom(),
        stateProvider_with_simplified_geom(),
    ]
    config = run_moore(options, make_streams, public_tools=public_tools)