Newer
Older
STPerformancePaper / drawing / CreateITHist.py
@Andrea Mauri Andrea Mauri on 25 Oct 2017 8 KB added more files
  1. import pickle
  2. from ROOT import gStyle
  3. from ROOT import TColor
  4. from ROOT import gROOT
  5. import ROOT as R
  6. #import statistics
  7. from ITMapping import ITMapping
  8. from ITMapping import PlotITBoxes
  9. from ITMapping import PlotITLabels
  10. from Create_Maps import IT_Map as IT_Map_func
  11. from config import ITMeanRange
  12. from config import ITWidthRange
  13. from config import ITEffRange
  14. from config import UsePredefinedRanges
  15. from config import UsePredefinedTitles
  16. from config import ITMeanTitle
  17. from config import ITWidthTitle
  18. from config import ITEffTitle
  19. from config import IncudeMissingSectorsToSummary
  20. from array import array
  21.  
  22.  
  23. def CreateITHist(data,variable, mode, suffix, address="Plots/", test_mode=False):
  24. """
  25. This finction creates map of the IT from given dictionary.
  26. Dictionary whould have a form:
  27. data = {<st_id1>:{
  28. <variable>:<number or TH1>
  29. },
  30. <st_id2>:{},
  31. ...}
  32. depending on mode, the function will create a map according to the:
  33. - Number ("Value" mode)
  34. - Mean of the histogram ("Mean" mode)
  35. - R.M.S. of the histogram ("Sigma" mode)
  36.  
  37. st_id is a 3-digit ID of a sector, which is defined in STTrackTuple algorithm.
  38. The map between st_id and sector name can be found it Create_Maps.py file (or be obtained with IT_Map_func())
  39. """
  40.  
  41.  
  42. global ITMeanRange
  43. global ITWidthRange
  44. global ITEffRange
  45. global UsePredefinedRanges
  46. global UsePredefinedTitles
  47. global ITMeanTitle
  48. global ITWidthTitle
  49. global ITEffTitle
  50. global IncudeMissingSectorsToSummary
  51. #Check if requested variable is in collection.
  52. variable_in_collection = False
  53. for st_id in data:
  54. if variable in data[st_id]:
  55. variable_in_collection = True
  56. break
  57. if not variable_in_collection:
  58. return False
  59. first_lower = lambda s: s[:1].lower() + s[1:] if s else ''
  60. stats = ["1","2","3"]
  61. boxes = ["ASide","CSide","Top","Bottom"]
  62. layers = ["X1", "U", "V", "X2"]
  63. sectors = ["1","2","3","4","5","6","7"]
  64.  
  65. IT_Map = IT_Map_func()
  66. gROOT.SetStyle("Modern")
  67. gROOT.ForceStyle()
  68. gStyle.SetOptStat(0)
  69. gStyle.SetOptFit(0)
  70. gStyle.SetPadRightMargin(0.2)
  71. gStyle.SetTitleX(0.5)
  72. gStyle.SetTitleAlign(23)
  73. gStyle.SetTitleBorderSize(0)
  74. gStyle.SetPaintTextFormat("5.1f")
  75. gStyle.SetStatFormat("5.5f")
  76. gStyle.SetTitleFontSize(0.07)
  77. gStyle.SetPadTickY(1)
  78. gStyle.SetPadTickX(1)
  79. nColors=52
  80. MyPalette = [0]*nColors
  81. stops = [0.00, 0.50, 1.00]
  82. red = [0.80, 1.00, 0.00]
  83. green = [0.00, 1.00, 0.00]
  84. blue = [0.00, 1.00, 0.80]
  85.  
  86. s = array('d', stops)
  87. r = array('d', red)
  88. g = array('d', green)
  89. b = array('d', blue)
  90.  
  91. FI = TColor.CreateGradientColorTable(3, s, r, g, b, nColors);
  92. for k in range(0, nColors):
  93. MyPalette[k] = FI+k
  94.  
  95. gStyle.SetNumberContours(nColors)
  96. gStyle.SetPalette(nColors, array('i',MyPalette))
  97. gROOT.ForceStyle()
  98. m_mapping={}
  99. m_nSensors={}
  100. for st_id in IT_Map:
  101. m_mapping[st_id]=ITMapping(IT_Map[st_id])
  102. if (mode =="Mean") or (variable == "mean"):
  103. maximum = ITMeanRange[1]
  104. minimum = ITMeanRange[0]
  105. if UsePredefinedTitles:
  106. title = ITMeanTitle
  107. else:
  108. title = "Bias distribution, [mm]"
  109. elif (mode =="Sigma") or (variable == "width"):
  110. maximum = ITWidthRange[1]
  111. minimum = ITWidthRange[0]
  112. if UsePredefinedTitles:
  113. title = ITWidthTitle
  114. else:
  115. title = "Resolution, [mm]"
  116. elif variable == "efficiency":
  117. maximum = ITEffRange[1]
  118. minimum = ITEffRange[0]
  119. if UsePredefinedTitles:
  120. title = ITEffTitle
  121. else:
  122. title = "Hit efficiency"
  123. nBinsX = 25
  124. nBinsY = 52
  125. lowX = -12.5
  126. upX = 12.5
  127. lowY = -13
  128. upY = 13
  129. hist = R.TH2D("hist", title, nBinsX, lowX, upX, nBinsY, lowY, upY)
  130. masked_sectors = []
  131. vals = []
  132. if not test_mode:
  133. for st_id in data:
  134. if mode =="Mean":
  135. hist.Fill(m_mapping[st_id][0], m_mapping[st_id][1], data[st_id][variable].GetMean())
  136. if IncudeMissingSectorsToSummary:
  137. vals.append(data[st_id][variable].GetMean())
  138. else:
  139. if (data[st_id][variable].GetMean()<maximum) and (data[st_id][variable].GetMean()>minimum):
  140. vals.append(data[st_id][variable].GetMean())
  141. if (maximum<data[st_id][variable].GetMean()) or (minimum>data[st_id][variable].GetMean()):
  142. masked_sectors.append(IT_Map[st_id])
  143. print "Atention, hit bias of sector "+IT_Map[st_id]+" is out of hist range. The value is "+str(data[st_id][variable].GetMean())
  144. elif mode =="Sigma":
  145. hist.Fill(m_mapping[st_id][0], m_mapping[st_id][1], data[st_id][variable].GetRMS())
  146. if IncudeMissingSectorsToSummary:
  147. vals.append(data[st_id][variable].GetRMS())
  148. else:
  149. if (data[st_id][variable].GetRMS()<maximum) and (data[st_id][variable].GetRMS()>minimum):
  150. vals.append(data[st_id][variable].GetRMS())
  151. if (maximum<data[st_id][variable].GetRMS()) or (minimum>data[st_id][variable].GetRMS()):
  152. masked_sectors.append(IT_Map[st_id])
  153. print "Atention, resolution of sector "+IT_Map[st_id]+" is out of hist range. The value is "+str(data[st_id][variable].GetRMS())
  154. elif mode =="Value":
  155. hist.Fill(m_mapping[st_id][0], m_mapping[st_id][1], data[st_id][variable])
  156. if IncudeMissingSectorsToSummary:
  157. vals.append(data[st_id][variable])
  158. else:
  159. if (data[st_id][variable]<maximum) and (data[st_id][variable]>minimum):
  160. vals.append(data[st_id][variable])
  161. if (maximum<data[st_id][variable]) or (minimum>data[st_id][variable]):
  162. masked_sectors.append(IT_Map[st_id])
  163. if variable == "efficiency":
  164. try:
  165. print "Hit efficiency of sector "+IT_Map[st_id]+" is not shown since it is out of range ($\epsilon = "+str(data[st_id]["efficiency"]) + " \pm "+str(data[st_id]["err_efficiency"])+"$)."
  166. except:
  167. print "Atention, "+variable+" of sector "+IT_Map[st_id]+" is out of hist range. The value is "+str(data[st_id][variable])
  168. else:
  169. print "Atention, "+variable+" of sector "+IT_Map[st_id]+" is out of hist range. The value is "+str(data[st_id][variable])
  170. else:
  171. print "Please use one of the following modes: Mean, Sigma, Value"
  172. c = R.TCanvas("c","c",600,600)
  173.  
  174. if UsePredefinedRanges:
  175. hist.SetMaximum( maximum)
  176. hist.SetMinimum( minimum)
  177. hist.Draw("COLZ")
  178.  
  179. #if test_mode:
  180. PlotITBoxes(hist, nBinsX, lowX, upX, nBinsY, lowY, upY, masked_sectors)
  181. PlotITLabels(hist)
  182.  
  183. gStyle.SetOptStat(1111110)
  184. gStyle.SetOptFit(1111110)
  185. gROOT.ForceStyle()
  186. if not test_mode:
  187. c.SaveAs(address+variable+"_"+mode+"_IT_"+suffix+".pdf")
  188. c.SaveAs(address+variable+"_"+mode+"_IT_"+suffix+".C")
  189.  
  190.  
  191. gROOT.ProcessLine(".x lhcbStyle.C")
  192. gStyle.SetOptStat('erm')
  193. gROOT.ForceStyle()
  194.  
  195. try:
  196. from config import nBins_in_summary
  197. nBins = nBins_in_summary
  198. except:
  199. nBins = 50
  200. if (mode =="Mean") or (variable == "mean"):
  201. hist_summary = R.TH1D("hist_summary", "IT "+first_lower(title)+"; Bias [mm];Number of sectors", nBins, min(vals), max(vals))
  202. elif (mode =="Sigma") or (variable == "width"):
  203. hist_summary = R.TH1D("hist_summary", "IT "+first_lower(title)+"; Resolution [mm];Number of sectors", nBins, min(vals), max(vals))
  204. elif variable == "efficiency":
  205. hist_summary = R.TH1D("hist_summary", "IT "+first_lower(title)+";Hit detection efficiency;Number of sectors", nBins, min(vals), max(vals))
  206. else:
  207. hist_summary = R.TH1D("hist_summary", title, nBins, min(vals), max(vals))
  208.  
  209. #hist_summary.GetYaxis().SetTitleOffset(1.2)
  210. #hist_summary.GetYaxis().SetLabelSize(0.03)
  211. #hist_summary.GetXaxis().SetLabelSize(0.03)
  212. for v in vals:
  213. hist_summary.Fill(v)
  214.  
  215. c_s = R.TCanvas("c_s","c_s",800,800)
  216.  
  217. hist_summary.Draw()
  218.  
  219. R.gPad.Update()
  220.  
  221. if variable == "efficiency":
  222. st = hist_summary.FindObject("stats")
  223. st.SetX1NDC(0.15)
  224. st.SetX2NDC(0.35)
  225. st.SetY1NDC(0.65)
  226. st.SetY2NDC(0.85)
  227. elif variable == "mean":
  228. st = hist_summary.FindObject("stats")
  229. st.SetX1NDC(0.65)
  230. st.SetX2NDC(0.85)
  231. st.SetY1NDC(0.65)
  232. st.SetY2NDC(0.85)
  233. elif variable == "width":
  234. st = hist_summary.FindObject("stats")
  235. st.SetX1NDC(0.78)
  236. st.SetX2NDC(0.98)
  237. st.SetY1NDC(0.65)
  238. st.SetY2NDC(0.85)
  239.  
  240.  
  241. R.gPad.Update()
  242.  
  243. if not test_mode:
  244. c_s.SaveAs(address+"Summary_"+variable+"_"+mode+"_IT_"+suffix+".pdf")
  245. c_s.SaveAs(address+"Summary_"+variable+"_"+mode+"_IT_"+suffix+".C")
  246. # print "Mean : "+str(statistics.mean(vals))+" +/- "+str(statistics.stdev(vals))
  247. # print "Median : "+str(statistics.median(vals))
  248. gROOT.SetStyle("Modern")
  249. gROOT.ForceStyle()
  250. return c
  251. if __name__ == "__main__":
  252. c = CreateITHist(True, "Mean", "suffix","Plots/",True)
  253.