You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.4 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. # flake8: noqa
  2. from parameterisations.utils.parse_regression_coef_to_array import (
  3. parse_regression_coef_to_array,
  4. )
  5. from parameterisations.utils.fit_linear_regression_model import (
  6. fit_linear_regression_model,
  7. )
  8. import uproot
  9. import argparse
  10. from pathlib import Path
  11. def parameterise_track_model(
  12. input_file: str = "data/tracking_losses_ntuple_B_default_selected.root",
  13. tree_name: str = "Selected",
  14. ) -> Path:
  15. """Function that calculates the parameterisations to estimate track model coefficients.
  16. Args:
  17. input_file (str, optional): Defaults to "data/param_data_selected.root".
  18. tree_name (str, optional): Defaults to "Selected".
  19. Returns:
  20. Path: Path to cpp code files containing the found parameters.
  21. """
  22. input_tree = uproot.open({input_file: tree_name})
  23. # this is an event list of dictionaries containing awkward arrays
  24. array = input_tree.arrays()
  25. array["yStraightOut"] = array["ideal_state_770_y"] + array["ideal_state_770_ty"] * (
  26. array["ideal_state_10000_z"] - array["ideal_state_770_z"]
  27. )
  28. array["yDiffOut"] = array["ideal_state_10000_y"] - array["yStraightOut"]
  29. array = array[(array["yDiffOut"] < 100) & (array["yDiffOut"] > -100)]
  30. array["yStraightEndT"] = array["ideal_state_770_y"] + array[
  31. "ideal_state_770_ty"
  32. ] * (9410.0 - array["ideal_state_770_z"])
  33. array["yDiffEndT"] = array["ideal_state_9410_y"] - array["yStraightEndT"]
  34. array["dSlope_xEndT"] = array["ideal_state_9410_tx"] - array["ideal_state_770_tx"]
  35. array["dSlope_yEndT"] = array["ideal_state_9410_ty"] - array["ideal_state_770_ty"]
  36. array["dSlope_xEndT_abs"] = abs(array["dSlope_xEndT"])
  37. array["dSlope_yEndT_abs"] = abs(array["dSlope_yEndT"])
  38. model_y_match, poly_features_y_match = fit_linear_regression_model(
  39. array,
  40. target_feat="yDiffOut",
  41. features=[
  42. "ideal_state_770_ty",
  43. "dSlope_xEndT",
  44. "dSlope_yEndT",
  45. ],
  46. keep=[
  47. "dSlope_yEndT",
  48. "dSlope_xEndT dSlope_yEndT",
  49. "ideal_state_770_ty dSlope_xEndT^2",
  50. "ideal_state_770_ty dSlope_yEndT^2",
  51. ],
  52. degree=3,
  53. fit_intercept=False,
  54. )
  55. keep_y_match_precise = [
  56. "dSlope_yEndT",
  57. "ideal_state_770_ty dSlope_xEndT_abs",
  58. "ideal_state_770_ty dSlope_yEndT_abs",
  59. "ideal_state_770_ty dSlope_yEndT^2",
  60. "ideal_state_770_ty dSlope_xEndT^2",
  61. "ideal_state_770_ty ideal_state_770_tx dSlope_xEndT",
  62. "ideal_state_770_tx^2 dSlope_yEndT",
  63. "ideal_state_770_ty ideal_state_770_tx^2 dSlope_xEndT_abs",
  64. "ideal_state_770_ty^3 ideal_state_770_tx dSlope_xEndT",
  65. ]
  66. model_y_match_precise, poly_features_y_match_precise = fit_linear_regression_model(
  67. array,
  68. "yDiffEndT",
  69. [
  70. "ideal_state_770_ty",
  71. "ideal_state_770_tx",
  72. "dSlope_xEndT",
  73. "dSlope_yEndT",
  74. "dSlope_xEndT_abs",
  75. "dSlope_yEndT_abs",
  76. ],
  77. keep=keep_y_match_precise,
  78. degree=5,
  79. )
  80. cpp_y_match = parse_regression_coef_to_array(
  81. model_y_match,
  82. poly_features_y_match,
  83. array_name="bendYParamsMatchElectron",
  84. )
  85. cpp_y_match_precise = parse_regression_coef_to_array(
  86. model_y_match_precise,
  87. poly_features_y_match_precise,
  88. array_name="bendYParamsElectron",
  89. )
  90. outpath = Path("parameterisations/result/track_model_params_electron.hpp")
  91. outpath.parent.mkdir(parents=True, exist_ok=True)
  92. with open(outpath, "w") as result:
  93. result.writelines(
  94. cpp_y_match + cpp_y_match_precise,
  95. )
  96. return outpath
  97. if __name__ == "__main__":
  98. parser = argparse.ArgumentParser()
  99. parser.add_argument(
  100. "--input-file",
  101. type=str,
  102. help="Path to the input file",
  103. required=False,
  104. )
  105. parser.add_argument(
  106. "--tree-name",
  107. type=str,
  108. help="Path to the input file",
  109. required=False,
  110. )
  111. args = parser.parse_args()
  112. args_dict = {arg: val for arg, val in vars(args).items() if val is not None}
  113. outfile = parameterise_track_model(**args_dict)
  114. try:
  115. import subprocess
  116. # run clang-format for nicer looking result
  117. subprocess.run(
  118. [
  119. "clang-format",
  120. "-i",
  121. f"{outfile}",
  122. ],
  123. check=True,
  124. )
  125. except:
  126. pass