-
Notifications
You must be signed in to change notification settings - Fork 17
Update to handle multiDim histograms #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aebid
wants to merge
14
commits into
cms-flaf:main
Choose a base branch
from
aebid:Add_MultiDim_Plots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3471e6c
Update to handle multiDim histograms
aebid 0dddc08
Merge
aebid 929d7cb
Update flaf env again after reset-merge
aebid eaa9bbf
Fixed some issues with flattened vars
aebid 711b98c
Formatting
aebid bda3535
Bug fix
aebid ffc7872
Remove debug prints
aebid 129d233
Remove debug
aebid c979734
Fix bug with 1D string binning
aebid 516c860
Merge with FLAF204
aebid a195bc4
Add this var_entry thing and remove import star because I hate it
aebid 71939ef
Formatting and fixed 1 more var_entry
aebid 0811e7d
Update flaf env to get latest black
aebid e53f55a
Remove hardcode black version
aebid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||
| sys.path.append(os.environ["ANALYSIS_PATH"]) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| from FLAF.Common.HistHelper import * | ||||||||||||||||||||||||||||||||||||
| import FLAF.Common.HistHelper as HistHelper | ||||||||||||||||||||||||||||||||||||
| import FLAF.Common.Utilities as Utilities | ||||||||||||||||||||||||||||||||||||
| from FLAF.Common.Setup import Setup | ||||||||||||||||||||||||||||||||||||
| from FLAF.RunKit.run_tools import ps_call | ||||||||||||||||||||||||||||||||||||
|
|
@@ -36,7 +36,19 @@ def SaveHist(key_tuple, outFile, hist_list, hist_name, unc, scale, verbose=0): | |||||||||||||||||||||||||||||||||||
| dir_ptr = Utilities.mkdir(outFile, dir_name) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| merged_hist = model.GetHistogram().Clone() | ||||||||||||||||||||||||||||||||||||
| for i in range(0, unit_hist.GetNbinsX() + 2): | ||||||||||||||||||||||||||||||||||||
| # Yes I know this is ugly | ||||||||||||||||||||||||||||||||||||
| # Axis needs +2 for under/overflow, but only if the return is not 1!!! | ||||||||||||||||||||||||||||||||||||
| # Was having issue with Z axis in 2D. We don't want to multiply by 3 if it's 2D | ||||||||||||||||||||||||||||||||||||
| N_xbins = unit_hist.GetNbinsX() + 2 | ||||||||||||||||||||||||||||||||||||
| N_ybins = unit_hist.GetNbinsY() if hasattr(unit_hist, "GetNbinsY") else 1 | ||||||||||||||||||||||||||||||||||||
| N_ybins = N_ybins + 2 if N_ybins > 1 else N_ybins | ||||||||||||||||||||||||||||||||||||
| N_zbins = unit_hist.GetNbinsZ() if hasattr(unit_hist, "GetNbinsZ") else 1 | ||||||||||||||||||||||||||||||||||||
| N_zbins = N_zbins + 2 if N_zbins > 1 else N_zbins | ||||||||||||||||||||||||||||||||||||
| N_bins = N_xbins * N_ybins * N_zbins | ||||||||||||||||||||||||||||||||||||
| # If we use the THnD then we have 'GetNbins' function instead | ||||||||||||||||||||||||||||||||||||
| N_bins = unit_hist.GetNbins() if hasattr(unit_hist, "GetNbins") else N_bins | ||||||||||||||||||||||||||||||||||||
| # This can be a loop over many bins, several times. Can be improved to be ran in c++ instead | ||||||||||||||||||||||||||||||||||||
| for i in range(0, N_bins): | ||||||||||||||||||||||||||||||||||||
| bin_content = unit_hist.GetBinContent(i) | ||||||||||||||||||||||||||||||||||||
| bin_error = unit_hist.GetBinError(i) | ||||||||||||||||||||||||||||||||||||
| merged_hist.SetBinContent(i, bin_content) | ||||||||||||||||||||||||||||||||||||
|
|
@@ -46,7 +58,7 @@ def SaveHist(key_tuple, outFile, hist_list, hist_name, unc, scale, verbose=0): | |||||||||||||||||||||||||||||||||||
| if len(hist_list) > 1: | ||||||||||||||||||||||||||||||||||||
| for model, unit_hist in hist_list[1:]: | ||||||||||||||||||||||||||||||||||||
| hist = model.GetHistogram() | ||||||||||||||||||||||||||||||||||||
| for i in range(0, unit_hist.GetNbinsX() + 2): | ||||||||||||||||||||||||||||||||||||
| for i in range(0, N_bins): | ||||||||||||||||||||||||||||||||||||
| bin_content = unit_hist.GetBinContent(i) | ||||||||||||||||||||||||||||||||||||
| bin_error = unit_hist.GetBinError(i) | ||||||||||||||||||||||||||||||||||||
| hist.SetBinContent(i, bin_content) | ||||||||||||||||||||||||||||||||||||
|
|
@@ -61,10 +73,35 @@ def SaveHist(key_tuple, outFile, hist_list, hist_name, unc, scale, verbose=0): | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def GetUnitBinHist(rdf, var, filter_to_apply, weight_name, unc, scale): | ||||||||||||||||||||||||||||||||||||
| model, unit_bin_model = GetModel(hist_cfg_dict, var, return_unit_bin_model=True) | ||||||||||||||||||||||||||||||||||||
| unit_hist = rdf.Filter(filter_to_apply).Histo1D( | ||||||||||||||||||||||||||||||||||||
| unit_bin_model, f"{var}_bin", weight_name | ||||||||||||||||||||||||||||||||||||
| var_entry = HistHelper.findBinEntry(hist_cfg_dict, args.var) | ||||||||||||||||||||||||||||||||||||
| dims = ( | ||||||||||||||||||||||||||||||||||||
| 1 | ||||||||||||||||||||||||||||||||||||
| if not hist_cfg_dict[var_entry].get("var_list", False) | ||||||||||||||||||||||||||||||||||||
| else len(hist_cfg_dict[var_entry]["var_list"]) | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| model, unit_bin_model = HistHelper.GetModel( | ||||||||||||||||||||||||||||||||||||
| hist_cfg_dict, var, dims, return_unit_bin_model=True | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| var_bin_list = ( | ||||||||||||||||||||||||||||||||||||
| [f"{var}_bin" for var in hist_cfg_dict[var_entry]["var_list"]] | ||||||||||||||||||||||||||||||||||||
| if dims > 1 | ||||||||||||||||||||||||||||||||||||
| else [f"{var}_bin"] | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| if dims == 1: | ||||||||||||||||||||||||||||||||||||
| unit_hist = rdf.Filter(filter_to_apply).Histo1D( | ||||||||||||||||||||||||||||||||||||
| unit_bin_model, *var_bin_list, weight_name | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| elif dims == 2: | ||||||||||||||||||||||||||||||||||||
| unit_hist = rdf.Filter(filter_to_apply).Histo2D( | ||||||||||||||||||||||||||||||||||||
| unit_bin_model, *var_bin_list, weight_name | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| elif dims == 3: | ||||||||||||||||||||||||||||||||||||
| unit_hist = rdf.Filter(filter_to_apply).Histo3D( | ||||||||||||||||||||||||||||||||||||
| unit_bin_model, *var_bin_list, weight_name | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+91
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||
| raise RuntimeError("Only 1D, 2D and 3D histograms are supported") | ||||||||||||||||||||||||||||||||||||
| return model, unit_hist | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -174,7 +211,7 @@ def CreateFakeStructure(outFile, setup, var, key_filter_dict, further_cuts): | |||||||||||||||||||||||||||||||||||
| for filter_key in key_filter_dict.keys(): | ||||||||||||||||||||||||||||||||||||
| print(filter_key) | ||||||||||||||||||||||||||||||||||||
| for further_cut_name in [None] + list(further_cuts.keys()): | ||||||||||||||||||||||||||||||||||||
| model, unit_bin_model = GetModel( | ||||||||||||||||||||||||||||||||||||
| model, unit_bin_model = HistHelper.GetModel( | ||||||||||||||||||||||||||||||||||||
| hist_cfg_dict, var, return_unit_bin_model=True | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| nbins = unit_bin_model.fNbinsX | ||||||||||||||||||||||||||||||||||||
|
|
@@ -276,7 +313,13 @@ def CreateFakeStructure(outFile, setup, var, key_filter_dict, further_cuts): | |||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| variables = setup.global_params["variables"] | ||||||||||||||||||||||||||||||||||||
| vars_needed = set(variables) | ||||||||||||||||||||||||||||||||||||
| vars_needed = set() | ||||||||||||||||||||||||||||||||||||
| for var in variables: | ||||||||||||||||||||||||||||||||||||
| if isinstance(var, dict) and "vars" in var: | ||||||||||||||||||||||||||||||||||||
| for v in var["vars"]: | ||||||||||||||||||||||||||||||||||||
| vars_needed.add(v) | ||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||
| vars_needed.add(var) | ||||||||||||||||||||||||||||||||||||
| for further_cut_name, (vars_for_cut, _) in further_cuts.items(): | ||||||||||||||||||||||||||||||||||||
| for var_for_cut in vars_for_cut: | ||||||||||||||||||||||||||||||||||||
| if var_for_cut: | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.