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.

309 lines
9.6 KiB

10 months ago
  1. import argparse
  2. from ROOT import TLatex, TCanvas, TFile, TGaxis
  3. from ROOT import kOrange, kGray, kMagenta, kCyan, kGreen, kBlue, kBlack, gPad, TF1
  4. from ROOT import gROOT
  5. from ROOT import TObjArray
  6. gROOT.SetBatch(True)
  7. def get_colors():
  8. return [kBlack, kBlue, kGreen + 3, kMagenta + 2, kOrange, kCyan + 2]
  9. def get_markers():
  10. return [20, 24, 21, 22, 23, 25]
  11. def get_fillstyles():
  12. return [3004, 3003, 3325, 3144, 3244, 3444]
  13. def get_files(tf, filename, label):
  14. for i, f in enumerate(filename):
  15. tf[label[i]] = TFile(f, "read")
  16. return tf
  17. def argument_parser():
  18. parser = argparse.ArgumentParser(description="location of the histogram file")
  19. parser.add_argument("--filename", nargs="+", default=[], help="name of input files")
  20. parser.add_argument(
  21. "--label",
  22. nargs="+",
  23. default=["TrackRes"],
  24. help="name of input tuple files",
  25. )
  26. parser.add_argument(
  27. "--trackers",
  28. type=str,
  29. nargs="+",
  30. default=["Forward", "BestLong", "BestForward"],
  31. help="Trackers to plot.",
  32. )
  33. parser.add_argument(
  34. "--outfile",
  35. type=str,
  36. default="checks/TrackResolution_plots.root",
  37. help="name of output files",
  38. )
  39. parser.add_argument(
  40. "--savepdf",
  41. default=False,
  42. action="store_true",
  43. help="save plots in pdf format",
  44. )
  45. return parser
  46. def PrCheckerTrackResolution(filename, label, trackers, outfile, savepdf):
  47. from utils.LHCbStyle import setLHCbStyle, set_style
  48. from utils.Legend import place_legend
  49. setLHCbStyle()
  50. latex = TLatex()
  51. latex.SetNDC()
  52. latex.SetTextSize(0.05)
  53. markers = get_markers()
  54. colors = get_colors()
  55. styles = get_fillstyles()
  56. tf = {}
  57. tf = get_files(tf, filename, label)
  58. outputfile = TFile(outfile, "recreate")
  59. outputfile.cd()
  60. for tracker in trackers:
  61. hres_p = {}
  62. hres_eta = {}
  63. canvas1 = TCanvas("res_p", "res v.s. p")
  64. canvas1.cd()
  65. arrp = TObjArray()
  66. gaus = TF1("gaus", "gaus", -1, 1)
  67. for idx, lab in enumerate(label):
  68. hdp_p = tf[lab].Get(
  69. f"Track/TrackResChecker{tracker}/ALL/vertex/dpoverp_vs_p",
  70. )
  71. hdp_p.SetName("dpoverp_p_" + lab)
  72. hmom = hdp_p.ProjectionX()
  73. hmom.SetTitle("p distribution Long Tracks")
  74. hdp_p.FitSlicesY(gaus, 0, -1, 0, "Q", arrp)
  75. hres_p[lab] = arrp[2]
  76. hres_p[lab].GetYaxis().SetTitle("dp/p [%]")
  77. hres_p[lab].GetXaxis().SetTitle("p [GeV]")
  78. hres_p[lab].GetYaxis().SetRangeUser(0, 1.2)
  79. hres_p[lab].SetTitle(lab)
  80. set_style(hres_p[lab], colors[idx], markers[idx], 0)
  81. if idx == 0:
  82. hres_p[lab].Draw("E1 p1")
  83. set_style(hmom, kGray + 1, markers[idx], styles[idx])
  84. hmom.Scale(gPad.GetUymax() / hmom.GetMaximum())
  85. hmom.Draw("hist same")
  86. else:
  87. hres_p[lab].Draw("E1 p1 same")
  88. set_style(hmom, colors[idx] - 10, markers[idx], styles[idx])
  89. # hmom.Scale(gPad.GetUymax() / hmom.GetMaximum())
  90. # hmom.Draw("hist same")
  91. for i in range(1, hres_p[lab].GetNbinsX() + 1):
  92. hres_p[lab].SetBinContent(i, hres_p[lab].GetBinContent(i) * 100)
  93. hres_p[lab].SetBinError(i, hres_p[lab].GetBinError(i) * 100)
  94. print(
  95. lab
  96. + ": Track resolution (dp/p) in p region: ("
  97. + format(hres_p[lab].GetBinLowEdge(i), ".2f")
  98. + ", "
  99. + format(
  100. hres_p[lab].GetBinLowEdge(i) + hres_p[lab].GetBinWidth(i),
  101. ".2f",
  102. )
  103. + ") [GeV/c]"
  104. + " --- ("
  105. + format(hres_p[lab].GetBinContent(i), ".2f")
  106. + "+-"
  107. + format(hres_p[lab].GetBinError(i), ".2f")
  108. + ")%",
  109. )
  110. print("-----------------------------------------------------")
  111. if "Best" not in tracker:
  112. legend = place_legend(
  113. canvas1,
  114. 0.45,
  115. 0.33,
  116. 0.93,
  117. 0.63,
  118. header="LHCb Simulation",
  119. option="LPE",
  120. )
  121. else:
  122. legend = place_legend(
  123. canvas1,
  124. 0.45,
  125. 0.53,
  126. 0.93,
  127. 0.83,
  128. header="LHCb Simulation",
  129. option="LPE",
  130. )
  131. for le in legend.GetListOfPrimitives():
  132. if "distribution" in le.GetLabel():
  133. le.SetOption("LF")
  134. legend.Draw()
  135. for lab in label:
  136. hres_p[lab].Draw("E1 p1 same")
  137. canvas1.SetRightMargin(0.1)
  138. if "Best" not in tracker:
  139. latex.DrawLatex(
  140. legend.GetX1() + 0.01,
  141. legend.GetY1() - 0.05,
  142. "without Kalman Filter",
  143. )
  144. low = 0
  145. high = 1.2
  146. gPad.Update()
  147. axis = TGaxis(
  148. gPad.GetUxmax(),
  149. gPad.GetUymin(),
  150. gPad.GetUxmax(),
  151. gPad.GetUymax(),
  152. low,
  153. high,
  154. 510,
  155. "+U",
  156. )
  157. axis.SetTitleFont(132)
  158. axis.SetTitleSize(0.06)
  159. axis.SetTitleOffset(0.55)
  160. axis.SetTitle("# Tracks p distribution [a.u.]")
  161. axis.SetLabelSize(0)
  162. axis.Draw()
  163. canvas1.Write()
  164. if savepdf:
  165. filestypes = ["pdf"] # , "png", "eps", "C", "ps", "tex"]
  166. for ftype in filestypes:
  167. canvas1.SaveAs(f"checks/{tracker}_trackres_p." + ftype)
  168. gaus.Delete()
  169. arrp.Delete()
  170. canvas2 = TCanvas("res_eta", "res v.s. eta")
  171. canvas2.cd()
  172. arreta = TObjArray()
  173. gaus = TF1("gaus", "gaus", -1, 1)
  174. for idx, lab in enumerate(label):
  175. hdp_eta = tf[lab].Get(
  176. f"Track/TrackResChecker{tracker}/ALL/vertex/dpoverp_vs_eta",
  177. )
  178. hdp_eta.SetName("dpoverp_eta_" + lab)
  179. hdp_eta.FitSlicesY(gaus, 0, -1, 0, "Q", arreta)
  180. heta = hdp_eta.ProjectionX()
  181. heta.SetTitle("#eta distribution Long Tracks")
  182. hres_eta[lab] = arreta[2]
  183. hres_eta[lab].GetYaxis().SetTitle("dp/p [%]")
  184. hres_eta[lab].GetXaxis().SetTitle("#eta")
  185. hres_eta[lab].GetYaxis().SetRangeUser(0, 1.2)
  186. hres_eta[lab].SetTitle(lab)
  187. set_style(hres_eta[lab], colors[idx], markers[idx], 0)
  188. if idx == 0:
  189. hres_eta[lab].Draw("E1 p1")
  190. set_style(heta, kGray + 1, markers[idx], styles[idx])
  191. heta.Scale(gPad.GetUymax() / heta.GetMaximum())
  192. heta.Draw("hist same")
  193. else:
  194. hres_eta[lab].Draw("E1 p1 same")
  195. set_style(heta, colors[idx] - 10, markers[idx], styles[idx])
  196. # heta.Scale(gPad.GetUymax() / heta.GetMaximum())
  197. # heta.Draw("hist same")
  198. for i in range(1, hres_eta[lab].GetNbinsX() + 1):
  199. hres_eta[lab].SetBinContent(i, hres_eta[lab].GetBinContent(i) * 100)
  200. hres_eta[lab].SetBinError(i, hres_eta[lab].GetBinError(i) * 100)
  201. print(
  202. lab
  203. + ": Track resolution (dp/p) in eta region: ("
  204. + format(hres_eta[lab].GetBinLowEdge(i), ".2f")
  205. + ", "
  206. + format(
  207. hres_eta[lab].GetBinLowEdge(i) + hres_eta[lab].GetBinWidth(i),
  208. ".2f",
  209. )
  210. + ")"
  211. + " --- ("
  212. + format(hres_eta[lab].GetBinContent(i), ".2f")
  213. + "+-"
  214. + format(hres_eta[lab].GetBinError(i), ".2f")
  215. + ")%",
  216. )
  217. print("-----------------------------------------------------")
  218. legend = place_legend(
  219. canvas2,
  220. 0.41,
  221. 0.27,
  222. 0.89,
  223. 0.57,
  224. header="LHCb Simulation",
  225. option="LPE",
  226. )
  227. for le in legend.GetListOfPrimitives():
  228. if "distribution" in le.GetLabel():
  229. le.SetOption("LF")
  230. legend.SetTextFont(132)
  231. legend.SetTextSize(0.045)
  232. legend.Draw()
  233. for lab in label:
  234. hres_eta[lab].Draw("E1 p1 same")
  235. canvas2.SetRightMargin(0.1)
  236. if "Best" not in tracker:
  237. latex.DrawLatex(
  238. legend.GetX1() + 0.01,
  239. legend.GetY1() - 0.05,
  240. "without Kalman Filter",
  241. )
  242. low = 0
  243. high = 1.2
  244. gPad.Update()
  245. axis = TGaxis(
  246. gPad.GetUxmax(),
  247. gPad.GetUymin(),
  248. gPad.GetUxmax(),
  249. gPad.GetUymax(),
  250. low,
  251. high,
  252. 510,
  253. "+U",
  254. )
  255. axis.SetTitleFont(132)
  256. axis.SetTitleSize(0.06)
  257. axis.SetTitleOffset(0.55)
  258. axis.SetTitle("# Tracks #eta distribution [a.u.]")
  259. axis.SetLabelSize(0)
  260. axis.Draw()
  261. canvas2.Write()
  262. if savepdf:
  263. filestypes = ["pdf"] # , "png", "eps", "C", "ps", "tex"]
  264. for ftype in filestypes:
  265. canvas2.SaveAs(f"checks/{tracker}_trackres_eta." + ftype)
  266. gaus.Delete()
  267. arreta.Delete()
  268. outputfile.Write()
  269. outputfile.Close()
  270. if __name__ == "__main__":
  271. parser = argument_parser()
  272. args = parser.parse_args()
  273. PrCheckerTrackResolution(**vars(args))