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.

115 lines
3.5 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_magnet_kink(
  12. input_file: str = "data/tracking_losses_ntuple_B_default_selected.root",
  13. tree_name: str = "Selected",
  14. ) -> Path:
  15. """Function that calculates parameters for estimating the magnet kink z position.
  16. Args:
  17. input_file (str, optional): Defaults to "data/param_data_selected.root".
  18. tree_name (str, optional): Defaults to "Selected".
  19. per_layer (bool, optional): If true also calculates parameters per SciFi layer. Defaults to False.
  20. Returns:
  21. Path: Path to cpp code file.
  22. """
  23. input_tree = uproot.open({input_file: tree_name})
  24. # this is an event list of dictionaries containing awkward arrays
  25. array = input_tree.arrays()
  26. array["dSlope_xEndT"] = array["ideal_state_9410_tx"] - array["ideal_state_770_tx"]
  27. array["dSlope_xEndT_abs"] = abs(array["dSlope_xEndT"])
  28. array["x_EndT_abs"] = abs(array["ideal_state_9410_x"])
  29. # the magnet kink position is the point of intersection of the track tagents
  30. array["z_mag_xEndT"] = (
  31. array["ideal_state_770_x"]
  32. - array["ideal_state_9410_x"]
  33. - array["ideal_state_770_tx"] * array["ideal_state_770_z"]
  34. + array["ideal_state_9410_tx"] * array["ideal_state_9410_z"]
  35. ) / array["dSlope_xEndT"]
  36. array = array[(array["z_mag_xEndT"] < 5800) & (array["z_mag_xEndT"] > 5000)]
  37. model_endt, poly_features_endt = fit_linear_regression_model(
  38. array,
  39. target_feat="z_mag_xEndT",
  40. features=[
  41. "ideal_state_770_tx",
  42. "dSlope_xEndT",
  43. "dSlope_xEndT_abs",
  44. "x_EndT_abs",
  45. "ideal_state_770_ty",
  46. ],
  47. keep=[
  48. "dSlope_xEndT_abs",
  49. "x_EndT_abs",
  50. "ideal_state_770_tx^2",
  51. "ideal_state_770_tx dSlope_xEndT",
  52. "dSlope_xEndT^2",
  53. "x_EndT_abs^2",
  54. "ideal_state_770_ty^2",
  55. "ideal_state_770_tx^2 x_EndT_abs",
  56. "ideal_state_770_tx dSlope_xEndT x_EndT_abs",
  57. "dSlope_xEndT^2 x_EndT_abs",
  58. "dSlope_xEndT_abs ideal_state_770_ty^2",
  59. "x_EndT_abs^3",
  60. ],
  61. degree=3,
  62. fit_intercept=True,
  63. )
  64. cpp_ref = parse_regression_coef_to_array(
  65. model_endt,
  66. poly_features_endt,
  67. array_name="zMagnetParamsEndTElectron",
  68. )
  69. outpath = Path("parameterisations/result/z_mag_kink_params_electron.hpp")
  70. outpath.parent.mkdir(parents=True, exist_ok=True)
  71. with open(outpath, "w") as result:
  72. result.writelines(cpp_ref)
  73. return outpath
  74. if __name__ == "__main__":
  75. parser = argparse.ArgumentParser()
  76. parser.add_argument(
  77. "--input-file",
  78. type=str,
  79. help="Path to the input file",
  80. required=False,
  81. )
  82. parser.add_argument(
  83. "--tree-name",
  84. type=str,
  85. help="Path to the input file",
  86. required=False,
  87. )
  88. args = parser.parse_args()
  89. args_dict = {arg: val for arg, val in vars(args).items() if val is not None}
  90. outfile = parameterise_magnet_kink(**args_dict)
  91. try:
  92. import subprocess
  93. # run clang-format for nicer looking result
  94. subprocess.run(
  95. [
  96. "clang-format",
  97. "-i",
  98. f"{outfile}",
  99. ],
  100. check=True,
  101. )
  102. except:
  103. pass