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.

151 lines
5.4 KiB

10 months ago
  1. # flake8: noqaq
  2. import os
  3. import subprocess
  4. import argparse
  5. from parameterisations.parameterise_magnet_kink import parameterise_magnet_kink
  6. from parameterisations.parameterise_track_model import parameterise_track_model
  7. from parameterisations.parameterise_search_window import parameterise_search_window
  8. from parameterisations.parameterise_field_integral import parameterise_field_integral
  9. from parameterisations.parameterise_hough_histogram import parameterise_hough_histogram
  10. from parameterisations.utils.preselection import preselection
  11. from parameterisations.train_forward_ghost_mlps import (
  12. train_default_forward_ghost_mlp,
  13. train_veloUT_forward_ghost_mlp,
  14. )
  15. from parameterisations.train_matching_ghost_mlps import train_matching_ghost_mlp
  16. from parameterisations.utils.parse_tmva_matrix_to_array import (
  17. parse_tmva_matrix_to_array,
  18. )
  19. parser = argparse.ArgumentParser()
  20. parser.add_argument(
  21. "--field-params",
  22. action="store_true",
  23. help="Enables determination of magnetic field parameterisations.",
  24. )
  25. parser.add_argument(
  26. "--forward-weights",
  27. action="store_true",
  28. help="Enables determination of weights used by neural networks.",
  29. )
  30. parser.add_argument(
  31. "--matching-weights",
  32. action="store_true",
  33. default=True,
  34. help="Enables determination of weights used by neural networks.",
  35. )
  36. parser.add_argument(
  37. "--prepare-params-data",
  38. action="store_true",
  39. help="Enables preparation of data for magnetic field parameterisations.",
  40. )
  41. parser.add_argument(
  42. "--prepare-weights-data",
  43. action="store_true",
  44. help="Enables preparation of data for NN weight determination.",
  45. )
  46. args = parser.parse_args()
  47. selected = "neural_net_training/data/param_data_selected.root"
  48. if args.prepare_params_data:
  49. selection = "chi2_comb < 5 && pt > 10 && p > 1500 && p < 100000 && pid != 11"
  50. print("Run selection cuts =", selection)
  51. selected_md = preselection(
  52. cuts=selection,
  53. input_file="data/param_data_MD.root",
  54. )
  55. selected_mu = preselection(
  56. cuts=selection,
  57. input_file="data/param_data_MU.root",
  58. )
  59. merge_cmd = ["hadd", "-fk", selected, selected_md, selected_mu]
  60. print("Concatenate polarities ...")
  61. subprocess.run(merge_cmd, check=True)
  62. cpp_files = []
  63. if args.field_params:
  64. print("Parameterise magnet kink position ...")
  65. cpp_files.append(parameterise_magnet_kink(input_file=selected))
  66. print("Parameterise track model ...")
  67. cpp_files.append(parameterise_track_model(input_file=selected))
  68. selected_all_p = "neural_net_training/data/param_data_selected_all_p.root"
  69. if args.prepare_params_data:
  70. selection_all_momenta = "chi2_comb < 5 && pid != 11"
  71. print()
  72. print("Run selection cuts =", selection_all_momenta)
  73. selected_md_all_p = preselection(
  74. cuts=selection_all_momenta,
  75. outfile_postfix="selected_all_p",
  76. input_file="data/param_data_MD.root",
  77. )
  78. selected_mu_all_p = preselection(
  79. cuts=selection_all_momenta,
  80. outfile_postfix="selected_all_p",
  81. input_file="data/param_data_MU.root",
  82. )
  83. merge_cmd = ["hadd", "-fk", selected_all_p, selected_md_all_p, selected_mu_all_p]
  84. print("Concatenate polarities ...")
  85. subprocess.run(merge_cmd, check=True)
  86. if args.field_params:
  87. print("Parameterise search window ...")
  88. cpp_files.append(parameterise_search_window(input_file=selected_all_p))
  89. print("Parameterise magnetic field integral ...")
  90. cpp_files.append(parameterise_field_integral(input_file=selected_all_p))
  91. print("Parameterise Hough histogram binning ...")
  92. cpp_files.append(parameterise_hough_histogram(input_file=selected_all_p))
  93. ###>>>
  94. ghost_data = "neural_net_training/data/ghost_data.root"
  95. if args.prepare_weights_data:
  96. merge_cmd = [
  97. "hadd",
  98. "-fk",
  99. ghost_data,
  100. "data/ghost_data_MD.root",
  101. "data/ghost_data_MU.root",
  102. ]
  103. print("Concatenate polarities for neural network training ...")
  104. subprocess.run(merge_cmd, check=True)
  105. ###<<<
  106. if args.forward_weights:
  107. train_default_forward_ghost_mlp(prepare_data=args.prepare_weights_data)
  108. # FIXME: use env variable instead
  109. os.chdir(os.path.dirname(os.path.realpath(__file__)))
  110. train_veloUT_forward_ghost_mlp(prepare_data=args.prepare_weights_data)
  111. # this ensures that the directory is correct
  112. os.chdir(os.path.dirname(os.path.realpath(__file__)))
  113. cpp_files += parse_tmva_matrix_to_array(
  114. [
  115. "neural_net_training/result/GhostNNDataSet/weights/TMVAClassification_default_forward_ghost_mlp.class.C",
  116. "neural_net_training/result/GhostNNDataSet/weights/TMVAClassification_veloUT_forward_ghost_mlp.class.C",
  117. ],
  118. )
  119. ###>>>
  120. if args.matching_weights:
  121. os.chdir(os.path.dirname(os.path.realpath(__file__)))
  122. train_matching_ghost_mlp(
  123. prepare_data=True, # args.prepare_weights_data,
  124. input_file="data/ghost_data_B.root",
  125. tree_name="PrMatchNN_e6feac0d.PrMCDebugMatchToolNN/MVAInputAndOutput",
  126. outdir="neural_net_training",
  127. )
  128. # this ensures that the directory is correct
  129. os.chdir(os.path.dirname(os.path.realpath(__file__)))
  130. cpp_files += parse_tmva_matrix_to_array(
  131. [
  132. "neural_net_training/result/MatchNNDataSet/weights/TMVAClassification_matching_mlp.class.C",
  133. ],
  134. simd_type=True,
  135. )
  136. ###<<<
  137. for file in cpp_files:
  138. subprocess.run(
  139. [
  140. "clang-format",
  141. "-i",
  142. f"{file}",
  143. ],
  144. )