Skip to content

Commit

Permalink
fix file variations issue & workspace.json maker
Browse files Browse the repository at this point in the history
  • Loading branch information
gojakuch committed Aug 28, 2023
1 parent c7c9f4d commit a5dc7f2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
59 changes: 42 additions & 17 deletions src/LHC_AGC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,43 +64,57 @@ Generates a workspace dictionary, writes it to a JSON file and returns
`real_data` should contain real data that we are going to put into `:observations` (essentially vectors of numbers). we put `real_data[1]` for `"4j1b CR"` and `real_data[2]` for `"4j2b SR"`.
"""
function generate_workspace_file(all_hists::DictT, filename, real_data; rebin2=true, systematics=false)::Dict where DictT
# copy to a modified variable
all_hists::DictT = (rebin2 ? Dict(evt_type => Dict(var => all_hists[evt_type][var] |> restrict(120, Inf) |> rebin(2) for var in (systematics ? keys(all_hists[evt_type]) : [:HT_4j1b_nominal, :mbjj_4j2b_nominal])) for evt_type in keys(all_hists)) : copy(all_hists))

region_names = Dict(
:HT_4j1b => "4j1b CR",
:mbjj_4j2b => "4j2b SR",
)
evt_type_names = Dict(
:ttbar => "ttbar",
:wjets => "W+jets",
:single_top_s_chan => "single top, s-channel",
:single_top_t_chan => "single top, t-channel",
:single_top_tW => "tW"
)

norm_factors = Dict{Symbol, Dict{Symbol, Float64}}() # norm factors for hi

if systematics
modifiers_dict = Dict(
#Symbol("Luminosity") => (:_lumi_up, :_lumi_down),
Symbol("ME variation") => (:_ME_var, :_ME_var_sym),
Symbol("PS variation") => (:_PS_var, :_PS_var_sym),
# construct modifiers_dict
tmp_dict = Dict(
Symbol("Jet energy resolution") => (:_pt_res, :_pt_res_sym),
Symbol("Jet energy scale") => (:_pt_scale_up, :_pt_scale_down),
Symbol("Scale variations") => (:_scale_var_up, :_scale_var_down),
Symbol("b-tag NP 1") => (:_btag_var_0_up, :_btag_var_0_down),
Symbol("b-tag NP 2") => (:_btag_var_1_up, :_btag_var_1_down),
Symbol("b-tag NP 3") => (:_btag_var_2_up, :_btag_var_2_down),
Symbol("b-tag NP 4") => (:_btag_var_3_up, :_btag_var_3_down),
)
modifiers_dict = Dict(evt_type => tmp_dict for evt_type in keys(all_hists) if evt_type != :nominal)
modifiers_dict[:nominal] = merge(tmp_dict, Dict(Symbol("ME variation") => (:_ME_var, :_ME_var_sym), Symbol("PS variation") => (:_PS_var, :_PS_var_sym), Symbol("Scale variations") => (:_scale_var_up, :_scale_var_down)))

for evt_type in keys(all_hists)
norm_factors[evt_type] = Dict{Symbol, Float64}()
for x in keys(region_names)
# symmetric reconstruction of the _down
for (var, var_down) in values(modifiers_dict)
for (var, var_down) in (values(modifiers_dict[evt_type]))
norm = norm_factors[evt_type][Symbol(x, var)] = integral(all_hists[evt_type][Symbol(x, var)])/integral(all_hists[evt_type][Symbol(x, :_nominal)])
all_hists[evt_type][Symbol(x, var)] = all_hists[evt_type][Symbol(x, var)]*(1. / norm) # normalise the up variation
if var in [:_ME_var, :_PS_var, :_pt_res] # either reconstruction or normalisation of the down variation
if var == :_pt_res # either reconstruction or normalisation of the down variation
all_hists[evt_type][Symbol(x, var_down)] = all_hists[evt_type][Symbol(x, :_nominal)]*2 - all_hists[evt_type][Symbol(x, var)]
else
all_hists[evt_type][Symbol(x, var_down)] = all_hists[evt_type][Symbol(x, var_down)] * (integral(all_hists[evt_type][Symbol(x, :_nominal)]) / integral(all_hists[evt_type][Symbol(x, var_down)]))
end
end
end
end
# reconstruct these separately for ttbar only
for x in keys(region_names)
for (var, var_down) in [(:_ME_var, :_ME_var_sym), (:_PS_var, :_PS_var_sym)]
all_hists[:ttbar][Symbol(x, var_down)] = all_hists[:ttbar][Symbol(x, :_nominal)]*2 - all_hists[:ttbar][Symbol(x, var)]
end
end
end

workspace = Dict(
Expand All @@ -114,35 +128,44 @@ function generate_workspace_file(all_hists::DictT, filename, real_data; rebin2=t
Dict{Symbol, Any}[
Dict(
:data => binerrors(all_hists[evt_type][Symbol(region, :_nominal)]),
:name => "staterrors_"*replace(region_names[region], " " => "-"),
:name => "staterror_"*replace(region_names[region], " " => "-"),
:type => "staterror"
),
Dict(
:data => nothing,
:name => "ttbar_norm",
:type => "normfactor"
:data => Dict(
:hi => 1.03,
:lo => 0.97
),
:name => "Luminocity",
:type => "normsys"
)
],
((evt_type == :ttbar) ?
Dict{Symbol, Any}[Dict(
:data => nothing,
:name => "ttbar_norm",
:type => "normfactor"
)] : Dict{Symbol, Any}[]),
reduce(append!, (systematics ? [
Dict{Symbol, Any}[Dict(
:data => Dict(
:hi => norm_factors[evt_type][Symbol(region, modifiers_dict[name][1])],
:lo => 2. - norm_factors[evt_type][Symbol(region, modifiers_dict[name][1])]
:hi => norm_factors[evt_type][Symbol(region, modifiers_dict[evt_type][name][1])],
:lo => 2. - norm_factors[evt_type][Symbol(region, modifiers_dict[evt_type][name][1])]
),
:name => name,
:type => "normsys"
),
Dict(
:data => Dict(
:hi_data => bincounts(all_hists[evt_type][Symbol(region, modifiers_dict[name][1])]),
:lo_data => bincounts(all_hists[evt_type][Symbol(region, modifiers_dict[name][2])])
:hi_data => bincounts(all_hists[evt_type][Symbol(region, modifiers_dict[evt_type][name][1])]),
:lo_data => bincounts(all_hists[evt_type][Symbol(region, modifiers_dict[evt_type][name][2])])
),
:name => name,
:type => "histosys"
)] for name in keys(modifiers_dict)
)] for name in keys(modifiers_dict[evt_type])
] : []), init=Dict{Symbol, Any}[]),
dims=1),
:name => String(evt_type)
:name => evt_type_names[evt_type]
) for evt_type in keys(all_hists)
]
) for region in keys(region_names)
Expand Down Expand Up @@ -179,10 +202,12 @@ function generate_workspace_file(all_hists::DictT, filename, real_data; rebin2=t
:version => "1.0.0"
)

# write to JSON
open(filename, "w") do f
JSON3.pretty(f, workspace)
end

# return it
workspace
end

Expand Down
13 changes: 9 additions & 4 deletions src/main_loop.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""
get_histo(process_tag::Symbol; file_variation_tags=[:nominal], wgt = 0.0, n_files_max_per_sample = MAX_N_FILES_PER_SAMPLE[])
get_histo(process_tag::Symbol; do_file_variations::Bool=true, wgt = 0.0, n_files_max_per_sample = MAX_N_FILES_PER_SAMPLE[])
"""
function get_histo(process_tag::Symbol; file_variation_tags=[:nominal], wgt = 0.0, n_files_max_per_sample = MAX_N_FILES_PER_SAMPLE[])
function get_histo(process_tag::Symbol; do_file_variations::Bool=true, wgt = 0.0, n_files_max_per_sample = MAX_N_FILES_PER_SAMPLE[])
N = n_files_max_per_sample
if iszero(wgt)
wgt = LUMI * xsec_info[process_tag] / nevts_total(process_tag)
end

file_variation_tags = (do_file_variations ? keys(TAG_PATH_DICT[process_tag]) : [:nominal])

all_hists = reduce(merge, [
mapreduce(mergewith(+), @view TAG_PATH_DICT[process_tag][variation_tag][begin:N]) do path
get_histo(LazyTree(path, "Events"), wgt, file_variation=variation_tag)
Expand All @@ -15,14 +18,16 @@ function get_histo(process_tag::Symbol; file_variation_tags=[:nominal], wgt = 0.
end

"""
get_histo_distributed(process_tag::Symbol, pmap::Function; file_variation_tags=[:nominal], wgt = 0.0, n_files_max_per_sample = MAX_N_FILES_PER_SAMPLE[])
get_histo_distributed(process_tag::Symbol; do_file_variations::Bool=true, wgt = 0.0, n_files_max_per_sample = MAX_N_FILES_PER_SAMPLE[])
"""
function get_histo_distributed(process_tag::Symbol, pmap::Function; file_variation_tags=[:nominal], wgt = 0.0, n_files_max_per_sample = MAX_N_FILES_PER_SAMPLE[])
function get_histo_distributed(process_tag::Symbol; do_file_variations::Bool=true, wgt = 0.0, n_files_max_per_sample = MAX_N_FILES_PER_SAMPLE[])
N = n_files_max_per_sample
if iszero(wgt)
wgt = LUMI * xsec_info[process_tag] / nevts_total(process_tag)
end

file_variation_tags = (do_file_variations ? keys(TAG_PATH_DICT[process_tag]) : [:nominal])

files = Tuple{String, Symbol}[]
for variation_tag in file_variation_tags
append!(files, Tuple{String, Symbol}[(path, variation_tag) for path in @view TAG_PATH_DICT[process_tag][variation_tag][begin:N]])
Expand Down

0 comments on commit a5dc7f2

Please sign in to comment.