def do_dijet_gen_distributions(root_dir):
    """Do plots comparing different different inputs in dijet region"""
    root_files = [
        qgc.QCD_FILENAME, qgc.QCD_PYTHIA_ONLY_FILENAME, qgc.QCD_HERWIG_FILENAME
    ][:]
    root_files = [
        cu.open_root_file(os.path.join(root_dir, r)) for r in root_files
    ]

    directories = [
        cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files[:]
    ]
    mc_col = qgc.QCD_COLOUR
    mc_col2 = qgc.QCD_COLOURS[2]
    mc_col3 = qgc.QCD_COLOURS[3]
    msize = 1
    lw = 2
    csd = [
        {
            "label": "QCD MC [MG+PY8]",
            "line_color": mc_col,
            "fill_color": mc_col,
            "marker_color": mc_col,
            "marker_style": 22,
            "fill_style": 0,
            "marker_size": msize,
            'line_width': lw
        },
        {
            "label": "QCD MC [PY8]",
            "line_color": mc_col2,
            "fill_color": mc_col2,
            "marker_color": mc_col2,
            "marker_style": 21,
            "fill_style": 0,
            "marker_size": msize,
            'line_width': lw
        },
        {
            "label": "QCD MC [H++]",
            "line_color": mc_col3,
            "fill_color": mc_col3,
            "marker_color": mc_col3,
            "marker_style": 23,
            "fill_style": 0,
            "marker_size": msize,
            'line_width': lw
        },
    ]
    jet_config_str = qgc.extract_jet_config(root_dir)

    # Compare shapes
    do_all_1D_plots_in_dir(directories=directories,
                           output_dir=os.path.join(
                               root_dir,
                               "Dijet_gen_kin_comparison_normalised"),
                           components_styles_dicts=csd,
                           jet_config_str=jet_config_str,
                           normalise_hists=True)
def do_comparison_plots(workdir_label_pairs, output_dir):
    dirnames = [w[0] for w in workdir_label_pairs]

    # templates, we'll change the filename/dir as per instance
    total_len = len(workdir_label_pairs)
    mark = cu.Marker()
    sources = [
       {
           # "root_dir": wd,
           'label': label,
           "style": {
               'line_style': 1,
               'line_color': cu.get_colour_seq(ind, total_len),
               'marker_color': cu.get_colour_seq(ind, total_len),
               'marker_style': m,
               'marker_size': 0.75,
           }

       }
       for ind, ((wd, label), m) in enumerate(zip(workdir_label_pairs, mark.cycle(cycle_filling=True)))
    ]

    jet_config_str = qgc.extract_jet_config(dirnames[0])
    if len(dirnames) >1 and qgc.extract_jet_config(dirnames[1]) != jet_config_str:
        print("Conflicting jet config str, not adding")
        jet_config_str = None

    # COMPARE NOMINAL QCD
    if exists_in_all(qgc.QCD_FILENAME, dirnames):
        print("Found", qgc.QCD_FILENAME, "in all dirs")
        root_files = [cu.open_root_file(os.path.join(d, qgc.QCD_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "QCD [MG+PY8] [%s]" % s['label']

        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_qcd_compare_dijet_tighter_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='ave')

        directories = [cu.get_from_tfile(rf, "Dijet_eta_ordered") for rf in root_files]
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_qcd_compare_dijet_eta_ordered_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='ave')


    # COMPARE NOMINAL DY
    if exists_in_all(qgc.DY_FILENAME, dirnames):
        root_files = [cu.open_root_file(os.path.join(d, qgc.DY_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "ZPlusJets") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "Z+Jet [MG+PY8] [%s]" % s['label']

        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_dy_compare_kinematics_absolute"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='Z')
        
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_dy_compare_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='Z')

    # COMPARE JETHT+ZEROBIAS
    if exists_in_all(qgc.JETHT_ZB_FILENAME, dirnames):
        print("Found", qgc.JETHT_ZB_FILENAME, "in all dirs")
        root_files = [cu.open_root_file(os.path.join(d, qgc.JETHT_ZB_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "Data [%s]" % s['label']

        directories = [cu.get_from_tfile(rf, "Dijet_Presel") for rf in root_files]
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_jetht_zb_compare_dijet_presel_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='ave')

        directories = [cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files]
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_jetht_zb_compare_dijet_tighter_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='ave')

        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_jetht_zb_compare_dijet_tighter_kinematics_abs"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='ave')


    # COMPARE SINGLEMU

    # COMPARE HERWIG++ QCD
    if exists_in_all(qgc.QCD_HERWIG_FILENAME, dirnames):
        root_files = [cu.open_root_file(os.path.join(d, qgc.QCD_HERWIG_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "QCD [H++] [%s]" % s['label']

        # do_all_1D_projection_plots_in_dir(directories=directories,
        #                                   components_styles_dicts=this_sources,
        #                                   output_dir=os.path.join(output_dir, "plots_qcd_hpp_compare_dijet_tighter_kinematics_normalised"),
        #                                   jet_config_str=jet_config_str,
        #                                   normalise_hists=True,
        #                                   bin_by='ave')

        # directories = [cu.get_from_tfile(rf, "Dijet_eta_ordered") for rf in root_files]
        # do_all_1D_projection_plots_in_dir(directories=directories,
        #                                   components_styles_dicts=this_sources,
        #                                   output_dir=os.path.join(output_dir, "plots_qcd_hpp_compare_dijet_eta_ordered_kinematics_normalised"),
        #                                   jet_config_str=jet_config_str,
        #                                   normalise_hists=True,
        #                                   bin_by='ave')
        
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_qcd_hpp_compare_dijet_tighter_kinematics_absolute"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='ave')

        directories = [cu.get_from_tfile(rf, "Dijet_eta_ordered") for rf in root_files]
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_qcd_hpp_compare_dijet_eta_ordered_kinematics_absolute"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='ave')

    # COMPARE HERWIG++ DY
    if exists_in_all(qgc.DY_HERWIG_FILENAME, dirnames):
        root_files = [cu.open_root_file(os.path.join(d, qgc.DY_HERWIG_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "ZPlusJets") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "Z+Jet [H++] [%s]" % s['label']

        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_dy_hpp_compare_kinematics_absolute"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='Z')
        
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_dy_hpp_compare_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='Z')
def do_zpj_distributions(root_dir, title):
    """Do plots comparing different different inputs in Z+jet region"""
    # root_files = [qgc.SINGLE_MU_FILENAME, qgc.DY_FILENAME, qgc.DY_HERWIG_FILENAME, qgc.DY_MG_HERWIG_FILENAME]
    root_files = [
        qgc.SINGLE_MU_FILENAME, qgc.DY_FILENAME, qgc.DY_HERWIG_FILENAME
    ]

    if not all([os.path.isfile(os.path.join(root_dir, r))
                for r in root_files]):
        print("Cannot find ROOT files, skipping Z+J kinematic plots")
        return

    root_files = [
        cu.TFileCacher(os.path.join(root_dir, r)) for r in root_files
    ]

    directories = [rf.get("ZPlusJets") for rf in root_files]
    data_col = qgc.SINGLE_MU_COLOUR
    mc_col = qgc.DY_COLOUR
    mc_col2 = qgc.HERWIGPP_DY_COLOUR
    mc_col3 = qgc.DY_COLOURS[3]
    msize = 0.75
    csd = [
        {
            "label": "Data",
            "line_color": data_col,
            "fill_color": data_col,
            "marker_color": data_col,
            "marker_style": 20,
            "fill_style": 0,
            "marker_size": msize
        },
        {
            "label": "DY+Jets MC [MG+PY8]",
            "line_color": mc_col,
            "fill_color": mc_col,
            "marker_color": mc_col,
            "marker_style": 21,
            "fill_style": 0,
            "marker_size": msize
        },
        {
            "label": "DY+Jets MC [H++]",
            "line_color": mc_col2,
            "fill_color": mc_col2,
            "marker_color": mc_col2,
            "marker_style": 23,
            "fill_style": 0,
            "marker_size": msize
        },
        # {"label": "DY+Jets MC [MG+H++]", "line_color": mc_col3, "fill_color": mc_col3, "marker_color": mc_col3, "marker_style": 22, "fill_style": 0, "marker_size": msize},
    ]
    jet_config_str = qgc.extract_jet_config(root_dir)

    # Compare yields
    do_all_1D_projection_plots_in_dir(
        directories=directories,
        output_dir=os.path.join(root_dir,
                                "ZPlusJets_data_mc_kin_comparison_absolute2"),
        # output_dir=os.path.join(root_dir, "ZPlusJets_data_mc_kin_comparison_absolute_compareKFactor"),
        # output_dir=os.path.join(root_dir, "ZPlusJets_data_mc_kin_comparison_absolute_all"),
        components_styles_dicts=csd,
        jet_config_str=jet_config_str,
        normalise_hists=False,
        region_str=qgc.ZpJ_LABEL,
        bin_by='Z')

    # Compare shapes
    do_all_1D_projection_plots_in_dir(
        directories=directories,
        output_dir=os.path.join(
            root_dir, "ZPlusJets_data_mc_kin_comparison_normalised_compare2"),
        # output_dir=os.path.join(root_dir, "ZPlusJets_data_mc_kin_comparison_normalised_compare_KFactor"),
        # output_dir=os.path.join(root_dir, "ZPlusJets_data_mc_kin_comparison_normalised_all"),
        jet_config_str=jet_config_str,
        title=title,
        components_styles_dicts=csd,
        normalise_hists=True,
        region_str=qgc.ZpJ_LABEL,
        bin_by='Z')
        # },
        # {"root_dir": pythia_dir,
        #  "label": "MG+PYTHIA8 MC (groomed)",
        #  "style": {
        #      "line_color": ROOT.kBlue,
        #      "marker_color": ROOT.kBlue
        #  },
        #  "dy_filename": qgc.DY_FILENAME,
        #  "zpj_dirname": "ZPlusJets_QG_groomed",
        #  "qcd_filename": qgc.QCD_FILENAME,
        #  "dj_cen_dirname": "Dijet_QG_central_tighter_groomed",
        #  "dj_fwd_dirname": "Dijet_QG_forward_tighter_groomed"
        # },
    ]

    title = qgc.extract_jet_config(pythia_dir)
    # Do Z+jets region only
    # print("Doing Z+J ungroomed")
    # qgp.do_all_exclusive_plots_comparison(sources=all_sources[:2],
    #                                       var_list=qgc.COMMON_VARS[:],
    #                                       plot_dir=os.path.join(pythia_dir, "ungroomed_jet_zpj"),
    #                                       dy_filename=qgc.DY_FILENAME,
    #                                       qcd_filename=None,
    #                                       dj_cen_dirname=None,
    #                                       dj_fwd_dirname=None,
    #                                       show_region_labels=False,
    #                                       subplot_type="ratio",
    #                                       # subplot_title="#splitline{Groomed/}{ungroomed}",
    #                                       do_flav_tagged=False,
    #                                       pt_bins=qgc.PT_BINS,
    #                                       title=title+"\n"+qgc.ZpJ_LABEL,
def do_dijet_distributions(root_dir, title):
    """Do plots comparing different different inputs in dijet region"""
    # root_files = [qgc.JETHT_ZB_FILENAME, qgc.QCD_FILENAME, qgc.QCD_PYTHIA_ONLY_FILENAME, qgc.QCD_HERWIG_FILENAME]
    # root_files = [qgc.JETHT_ZB_FILENAME, qgc.QCD_FILENAME, qgc.QCD_PYTHIA_ONLY_FILENAME]
    root_files = [
        qgc.JETHT_ZB_FILENAME, qgc.QCD_FILENAME, qgc.QCD_HERWIG_FILENAME
    ][:]
    # root_files = [qgc.JETHT_ZB_FILENAME, qgc.QCD_FILENAME]
    # root_files = [qgc.JETHT_ZB_FILENAME, qgc.QCD_PYTHIA_ONLY_FILENAME]
    # root_files = [qgc.JETHT_ZB_FILENAME, qgc.QCD_HERWIG_FILENAME]

    if not all(os.path.isfile(os.path.join(root_dir, r)) for r in root_files):
        print("Cannot find ROOT files, skipping dijet kinematic plots")
        return

    root_files = [
        cu.TFileCacher(os.path.join(root_dir, r)) for r in root_files
    ]

    # herwig_dir = "workdir_ak4chs_herwig_newFlav_withPUreweight_withMuSF"
    # herwig_dir = "workdir_ak4chs_herwig_newFlav_withPUreweight_withMuSF_noExtraJetCuts"
    # root_files.append(cu.TFileCacher(os.path.join(herwig_dir, qgc.QCD_FILENAME)))

    # directories = [rf.get("Dijet") for rf in root_files]
    directories = [rf.get("Dijet_tighter") for rf in root_files[:]]
    # directories.extend([rf.get("Dijet_tighter") for rf in root_files[1:]])
    mc_col = qgc.QCD_COLOUR
    mc_col2 = qgc.QCD_COLOURS[2]
    # mc_col3 = qgc.QCD_COLOURS[3]
    data_col = qgc.JETHT_COLOUR
    zb_col = ROOT.kGreen + 2
    msize = 0.75
    csd = [
        {
            "label": "Data",
            "line_color": data_col,
            "fill_color": data_col,
            "marker_color": data_col,
            "marker_style": 20,
            "fill_style": 0,
            "marker_size": msize,
            'line_width': 2
        },
        {
            "label": "QCD MC [MG+PY8]",
            "line_color": mc_col,
            "fill_color": mc_col,
            "marker_color": mc_col,
            "marker_style": 22,
            "fill_style": 0,
            "marker_size": msize
        },
        # {"label": "QCD MC [PY8]", "line_color": mc_col2, "fill_color": mc_col2, "marker_color": mc_col2, "marker_style": 21, "fill_style": 0, "marker_size": msize},
        {
            "label": "QCD MC [H++]",
            "line_color": qgc.HERWIGPP_QCD_COLOUR,
            "fill_color": qgc.HERWIGPP_QCD_COLOUR,
            "marker_color": qgc.HERWIGPP_QCD_COLOUR,
            "marker_style": 23,
            "fill_style": 0,
            "marker_size": msize
        },
    ]
    jet_config_str = qgc.extract_jet_config(root_dir)

    # Compare yields
    do_all_1D_projection_plots_in_dir(
        directories=directories,
        output_dir=os.path.join(root_dir,
                                "Dijet_data_mc_kin_comparison_absolute2"),
        #                                   # output_dir=os.path.join(root_dir, "Dijet_data_mc_kin_comparison_absolute_pythiaOnly"),
        # output_dir=os.path.join(root_dir, "Dijet_data_mc_kin_comparison_absolute_both"),
        # output_dir=os.path.join(root_dir, "Dijet_data_mc_kin_comparison_absolute_everything"),
        #                                   # output_dir=os.path.join(root_dir, "Dijet_data_mc_kin_comparison_absolute_all"),
        components_styles_dicts=csd,
        region_str=qgc.Dijet_LABEL,
        jet_config_str=jet_config_str,
        normalise_hists=False,
        bin_by='ave')

    # Compare shapes
    do_all_1D_projection_plots_in_dir(
        directories=directories,
        output_dir=os.path.join(root_dir,
                                "Dijet_data_mc_kin_comparison_normalised2"),
        # output_dir=os.path.join(root_dir, "Dijet_data_mc_kin_comparison_normalised_pythiaOnly"),
        # output_dir=os.path.join(root_dir, "Dijet_data_mc_kin_comparison_normalised_both"),
        # output_dir=os.path.join(root_dir, "Dijet_data_mc_kin_comparison_normalised_all"),
        components_styles_dicts=csd,
        region_str=qgc.Dijet_LABEL,
        jet_config_str=jet_config_str,
        title=title,
        bin_by='ave')

    # Do eta-ordered
    directories = [rf.get("Dijet_eta_ordered") for rf in root_files[:]]
    do_all_1D_projection_plots_in_dir(
        directories=directories,
        output_dir=os.path.join(
            root_dir, "Dijet_data_mc_kin_comparison_eta_ordered_normalised2"),
        components_styles_dicts=csd,
        region_str=qgc.Dijet_LABEL,
        jet_config_str=jet_config_str,
        title=title,
        bin_by='ave')