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.

138 lines
4.3 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
  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["yStraightEndT"] = array["ideal_state_770_y"] + array[
  30. "ideal_state_770_ty"
  31. ] * (9410.0 - array["ideal_state_770_z"])
  32. array["yDiffEndT"] = array["ideal_state_9410_y"] - array["yStraightEndT"]
  33. array["dSlope_xEndT"] = array["ideal_state_9410_tx"] - array["ideal_state_770_tx"]
  34. array["dSlope_yEndT"] = array["ideal_state_9410_ty"] - array["ideal_state_770_ty"]
  35. array["dSlope_xEndT_abs"] = abs(array["dSlope_xEndT"])
  36. array["dSlope_yEndT_abs"] = abs(array["dSlope_yEndT"])
  37. model_y_match, poly_features_y_match = fit_linear_regression_model(
  38. array,
  39. target_feat="yDiffOut",
  40. features=[
  41. "ideal_state_770_ty",
  42. "dSlope_xEndT",
  43. "dSlope_yEndT",
  44. ],
  45. keep=[
  46. "dSlope_yEndT",
  47. "dSlope_xEndT dSlope_yEndT",
  48. "ideal_state_770_ty dSlope_xEndT^2",
  49. "ideal_state_770_ty dSlope_yEndT^2",
  50. ],
  51. degree=3,
  52. fit_intercept=False,
  53. )
  54. keep_y_match_precise = [
  55. "dSlope_yEndT",
  56. "ideal_state_770_ty dSlope_xEndT_abs",
  57. "ideal_state_770_ty dSlope_yEndT_abs",
  58. "ideal_state_770_ty dSlope_yEndT^2",
  59. "ideal_state_770_ty dSlope_xEndT^2",
  60. "ideal_state_770_ty ideal_state_770_tx dSlope_xEndT",
  61. "ideal_state_770_tx^2 dSlope_yEndT",
  62. "ideal_state_770_ty ideal_state_770_tx^2 dSlope_xEndT_abs",
  63. "ideal_state_770_ty^3 ideal_state_770_tx dSlope_xEndT",
  64. ]
  65. model_y_match_precise, poly_features_y_match_precise = fit_linear_regression_model(
  66. array,
  67. "yDiffEndT",
  68. [
  69. "ideal_state_770_ty",
  70. "ideal_state_770_tx",
  71. "dSlope_xEndT",
  72. "dSlope_yEndT",
  73. "dSlope_xEndT_abs",
  74. "dSlope_yEndT_abs",
  75. ],
  76. keep=keep_y_match_precise,
  77. degree=5,
  78. )
  79. cpp_y_match = parse_regression_coef_to_array(
  80. model_y_match,
  81. poly_features_y_match,
  82. array_name="bendYParamsMatchElectron",
  83. )
  84. cpp_y_match_precise = parse_regression_coef_to_array(
  85. model_y_match_precise,
  86. poly_features_y_match_precise,
  87. array_name="bendYParamsElectron",
  88. )
  89. outpath = Path("parameterisations/result/track_model_params_electron.hpp")
  90. outpath.parent.mkdir(parents=True, exist_ok=True)
  91. with open(outpath, "w") as result:
  92. result.writelines(
  93. cpp_y_match + cpp_y_match_precise,
  94. )
  95. return outpath
  96. if __name__ == "__main__":
  97. parser = argparse.ArgumentParser()
  98. parser.add_argument(
  99. "--input-file",
  100. type=str,
  101. help="Path to the input file",
  102. required=False,
  103. )
  104. parser.add_argument(
  105. "--tree-name",
  106. type=str,
  107. help="Path to the input file",
  108. required=False,
  109. )
  110. args = parser.parse_args()
  111. args_dict = {arg: val for arg, val in vars(args).items() if val is not None}
  112. outfile = parameterise_track_model(**args_dict)
  113. try:
  114. import subprocess
  115. # run clang-format for nicer looking result
  116. subprocess.run(
  117. [
  118. "clang-format",
  119. "-i",
  120. f"{outfile}",
  121. ],
  122. check=True,
  123. )
  124. except:
  125. pass