-
Notifications
You must be signed in to change notification settings - Fork 0
Kfactors io #13
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
Kfactors io #13
Changes from all commits
542430b
4efcbe6
0e4e59c
a1b1eee
15f1c15
64c8a26
caa372f
429d19b
4a45b86
00e05cf
f0685a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import pandas as pd | ||
| import yadism | ||
| import yaml | ||
| from datetime import date | ||
|
|
||
| from .runner import Runner | ||
| from . import configs, parameters | ||
|
|
@@ -39,6 +40,8 @@ def dis_tp_like(self, hid): | |
| new_t_card["mass"] = parameters.default_masses(hid) | ||
| new_t_card["fns"] = "fonll" | ||
| new_t_card["order"] = "N" * self.t_card["PTO"] + "LO" | ||
| if self.t_card["PTO"] == 3: | ||
| new_t_card["order"] = "N3LO" | ||
| return new_t_card | ||
|
|
||
|
|
||
|
|
@@ -60,6 +63,7 @@ def __init__(self, configs, name): | |
| print("Warning, setting TargetDIS = proton") | ||
| obs["TargetDIS"] = "proton" | ||
| self.o_card = obs | ||
| self.dataset_name = name | ||
|
|
||
| def yadism_like(self): | ||
| return self.o_card | ||
|
|
@@ -82,52 +86,154 @@ def dis_tp_like(self, pdf_name, restype): | |
|
|
||
|
|
||
| class KfactorRunner: | ||
| def __init__(self, t_card_name, o_card_name, pdf_name): | ||
| def __init__(self, t_card_name, dataset_name, pdf_name, use_yadism): | ||
|
Owner
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. Is the use_yadism flag needed for the NNLO part?
Collaborator
Author
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. yes this is a nice feature, you can do kfact both DISTP/DISTP ((pto+1)/pto) or DISTB/YADISM (same order)
Owner
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. And can you also do YADISM/YADISM in principle?
Collaborator
Author
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. no you can't... do you want to support this feature?
Owner
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. Maybe in the future this can be handy but for the moment let's just open an issue (I am doing that)
Collaborator
Author
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. nice |
||
| cfg = configs.load() | ||
| cfg = configs.defaults(cfg) | ||
|
|
||
| # Load the ymldb file | ||
| with open( | ||
| cfg["paths"]["ymldb"] / f"{dataset_name}.yaml", encoding="utf-8" | ||
| ) as f: | ||
| ymldb = yaml.safe_load(f) | ||
|
|
||
| self.theory = TheoryCard(cfg, t_card_name) | ||
| self.observables = Observable_card(cfg, o_card_name) | ||
|
|
||
| o_card_names = ymldb["operands"] | ||
| self.observables = [] | ||
| for ocard_list in o_card_names: | ||
| for o_card_name in ocard_list: | ||
| self.observables.append(Observable_card(cfg, o_card_name)) | ||
|
|
||
| self.pdf_name = pdf_name | ||
| self.use_yadism = use_yadism | ||
| self.result_path = cfg["paths"]["results"] | ||
| self.dataset_name = ymldb["target_dataset"] | ||
| self.operation = ymldb["operation"] | ||
| # self.conversion_factor = ymldb["conversion_factor"] | ||
| self._results = None | ||
|
|
||
| def run_yadism(self): | ||
| output = yadism.run_yadism( | ||
| self.theory.yadism_like(), self.observables.yadism_like() | ||
| ) | ||
| yad_pred = output.apply_pdf(lhapdf.mkPDF(self.pdf_name)) | ||
| yad_pred = {} | ||
| for observable in self.observables: | ||
| output = yadism.run_yadism( | ||
| self.theory.yadism_like(), observable.yadism_like() | ||
| ) | ||
| yad_pred[observable.dataset_name] = output.apply_pdf( | ||
| lhapdf.mkPDF(self.pdf_name) | ||
| ) | ||
| return yad_pred | ||
|
|
||
| def run_dis_tp(self, hid, n_cores): | ||
| # TODO: here we nned to run FO and M type | ||
| restype = "FO" | ||
| runner = Runner( | ||
| self.observables.dis_tp_like(self.pdf_name, restype), | ||
| self.theory.dis_tp_like(hid), | ||
| ) | ||
| runner.compute(n_cores) | ||
| return runner.results | ||
| # TODO: how do we treat bottom mass effects in this code? | ||
| # TODO: here we need to run FO and M type | ||
| restype = "M" | ||
| distp_pred = {} | ||
| for observable in self.observables: | ||
| runner = Runner( | ||
| observable.dis_tp_like(self.pdf_name, restype), | ||
| self.theory.dis_tp_like(hid), | ||
| ) | ||
| runner.compute(n_cores) | ||
| distp_pred[observable.dataset_name] = runner.results | ||
| return distp_pred | ||
|
|
||
| def compute(self, hid, n_cores): | ||
| # TODO: cache results somewahere | ||
| yad_log = self.run_yadism() | ||
| dis_tp_log = self.run_dis_tp(hid, n_cores) | ||
| self._results = self._log(dis_tp_log, yad_log) | ||
| # TODO: cache results somewhere | ||
| mumerator_log = self.run_dis_tp(hid, n_cores) | ||
| if self.use_yadism: | ||
| denominator_log = self.run_yadism() | ||
| else: | ||
| self.theory.t_card["PTO"] -= 1 | ||
| denominator_log = self.run_dis_tp(hid, n_cores) | ||
|
|
||
| @staticmethod | ||
| def _log(dis_tp_log, yad_log): | ||
| for obs in yad_log: | ||
| my_obs = obs.split("_")[0] | ||
| yad_df = pd.DataFrame(yad_log[obs]).rename(columns={"result": "yadism"}) | ||
| dis_tp_df = dis_tp_log[my_obs].rename(columns={"result": "dis_tp"}) | ||
| log_df = pd.concat([yad_df, dis_tp_df], axis=1).T.drop_duplicates().T | ||
|
|
||
| # construct some nice log table | ||
| log_df.drop("q", axis=1, inplace=True) | ||
| log_df.drop("y", axis=1, inplace=True) | ||
| log_df.drop("error", axis=1, inplace=True) | ||
| log_df["k-factor"] = np.abs(log_df.dis_tp - log_df.yadism) | ||
| return log_df | ||
|
|
||
| # TODO: add a save mathod | ||
| def save_results(self): | ||
| logs_df = self._log(mumerator_log, denominator_log, self.use_yadism) | ||
| self._results = self.build_kfactor(logs_df) | ||
| print(self._results) | ||
|
|
||
| @staticmethod | ||
| def _log(mumerator_log, denominator_log, use_yadism): | ||
| logs_df = {} | ||
| # loop on operands | ||
| for (num_name, num), (den_name, den) in zip( | ||
| mumerator_log.items(), denominator_log.items() | ||
| ): | ||
|
|
||
| if num_name != den_name: | ||
| raise ValueError( | ||
| "Numerator dataset name do not coincide with denominator." | ||
| ) | ||
|
|
||
| # loop on SF | ||
| for obs in den: | ||
| my_obs = obs.split("_")[0] | ||
| if use_yadism: | ||
| den_df = pd.DataFrame(den[obs]).rename(columns={"result": "yadism"}) | ||
| num_df = num[my_obs].rename(columns={"result": "dis_tp"}) | ||
| else: | ||
| den_df = den[my_obs].rename(columns={"result": "NNLO"}) | ||
| num_df = num[my_obs].rename(columns={"result": "N3LO"}) | ||
| log_df = pd.concat([den_df, num_df], axis=1).T.drop_duplicates().T | ||
|
|
||
| # construct some nice log table | ||
| log_df.drop("y", axis=1, inplace=True) | ||
| if use_yadism: | ||
| log_df.drop("q", axis=1, inplace=True) | ||
| log_df.drop("error", axis=1, inplace=True) | ||
| # log_df["k-factor"] = log_df.dis_tp / log_df.yadism | ||
| else: | ||
| log_df["Q2"] = log_df.q**2 | ||
| log_df.drop("q", axis=1, inplace=True) | ||
| # log_df["k-factor"] = log_df.N3LO / log_df.NNLO | ||
| logs_df[num_name] = log_df | ||
|
|
||
| return logs_df | ||
|
|
||
| def build_kfactor(self, logs_df): | ||
|
|
||
| if self.operation == "null": | ||
| for log_df in logs_df.values(): | ||
| if self.use_yadism: | ||
| log_df["k-factor"] = log_df.dis_tp / log_df.yadism | ||
| else: | ||
| log_df["k-factor"] = log_df.N3LO / log_df.NNLO | ||
| return log_df | ||
|
|
||
| elif self.operation == "RATIO": | ||
| data1, data2 = (*logs_df,) | ||
| k_fact_log = logs_df[data1] | ||
| if self.use_yadism: | ||
| k_fact_log["dis_tp"] = logs_df[data1].dis_tp / logs_df[data2].dis_tp | ||
| k_fact_log["yadism"] = logs_df[data1].yadism / logs_df[data2].yadism | ||
| k_fact_log["k-factor"] = k_fact_log.dis_tp / k_fact_log.yadism | ||
| else: | ||
| k_fact_log["N3LO"] = logs_df[data1].N3LO / logs_df[data2].N3LO | ||
| k_fact_log["NNLO"] = logs_df[data1].NNLO / logs_df[data2].NNLO | ||
| k_fact_log["k-factor"] = k_fact_log.N3LO / k_fact_log.NNLO | ||
| return k_fact_log | ||
|
|
||
| else: | ||
| raise ValueError(f"Operation {self.operation} no implemented") | ||
|
|
||
|
|
||
| def save_results(self, author, th_input): | ||
|
|
||
| if self.use_yadism: | ||
| k_fatctor_type = "N3LO FONLL DIS_TP / N3LO ZM-VFNS Yadism" | ||
| else: | ||
| k_fatctor_type = "N3LO FONLL DIS_TP / NNLO FONLL DIS_TP" | ||
| intro = [ | ||
| "********************************************************************************\n", | ||
| f"SetName: {self.dataset_name}\n", | ||
| f"Author: {author}\n", | ||
| f"Date: {date.today()}\n", | ||
| "CodesUsed: https://github.com/andreab1997/DIS_TP\n", | ||
| f"TheoryInput: {th_input}\n", | ||
| f"PDFset: {self.pdf_name}\n", | ||
| f"Warinings: {k_fatctor_type}\n" | ||
| "********************************************************************************\n", | ||
| ] | ||
| res_path = self.result_path / f"CF_QCD_{self.dataset_name}.dat" | ||
| print(f"Saving the k-factors in: {res_path}") | ||
| with open(res_path, "w", encoding="utf-8") as f: | ||
| f.writelines(intro) | ||
| f.writelines([f"{k:4f} 0.0000\n" for k in self._results["k-factor"]]) | ||
Uh oh!
There was an error while loading. Please reload this page.