2 from optparse
import OptionParser
13 from plotUtil
import Draw_2Dhist
14 from sigmaEff
import minimum_size_range
16 ROOT.gROOT.LoadMacro(
'./sPHENIXStyle/sPhenixStyle.C')
17 ROOT.gROOT.ProcessLine(
'SetsPhenixStyle()')
18 ROOT.gROOT.SetBatch(
True)
28 global nbinsdphi, nbinsdca
29 global dphiinterval, dcainterval
31 def Draw_1Dhist_wTF1(hist, histdata, norm1, logy, ymaxscale, XaxisName, Ytitle_unit, outname):
33 effsigma = xmax - xmin
36 binwidth = hist.GetXaxis().GetBinWidth(1)
37 histmax = hist.GetMaximum()
38 c = ROOT.TCanvas(
'c',
'c', 800, 700)
40 hist.Scale(1. / hist.Integral(-1, -1))
44 ROOT.gPad.SetRightMargin(RightMargin)
45 ROOT.gPad.SetTopMargin(TopMargin)
46 ROOT.gPad.SetLeftMargin(LeftMargin)
47 ROOT.gPad.SetBottomMargin(BottomMargin)
51 'Normalized entries / ({:g})'.
format(binwidth))
54 'Normalized entries / ({:g} {unit})'.
format(binwidth, unit=Ytitle_unit))
60 'Entries / ({:g} {unit})'.
format(binwidth, unit=Ytitle_unit))
64 hist.GetYaxis().SetRangeUser(hist.GetMinimum(0)*0.5, histmax * ymaxscale)
66 hist.GetYaxis().SetRangeUser(0., histmax * ymaxscale)
68 hist.GetXaxis().SetTickSize(TickSize)
69 hist.GetXaxis().SetTitleSize(AxisTitleSize)
70 hist.GetXaxis().SetLabelSize(AxisLabelSize)
71 hist.GetYaxis().SetTickSize(TickSize)
72 hist.GetYaxis().SetTitleSize(AxisTitleSize)
73 hist.GetYaxis().SetLabelSize(AxisLabelSize)
74 hist.GetXaxis().SetTitleOffset(1.1)
75 hist.GetYaxis().SetTitleOffset(1.35)
90 l1 = ROOT.TLine(xmin, 0, xmin, histmax)
91 l1.SetLineColor(ROOT.kRed)
92 l1.SetLineStyle(ROOT.kDashed)
95 l2 = ROOT.TLine(xmax, 0, xmax, histmax)
96 l2.SetLineColor(ROOT.kRed)
97 l2.SetLineStyle(ROOT.kDashed)
100 leg = ROOT.TLegend((1-RightMargin)-0.45, (1-TopMargin)-0.1,
101 (1-RightMargin)-0.1, (1-TopMargin)-0.03)
102 leg.SetTextSize(0.045)
104 leg.AddEntry(
'',
'#it{#bf{sPHENIX}} Simulation',
'')
107 leg2 = ROOT.TLegend(0.45, 0.67, 0.88, 0.8)
108 leg2.SetTextSize(0.033)
113 leg2.AddEntry(
'',
'#LT\Deltaz#GT={0:.2e}#pm{1:.2e} {2}'.
format(hist.GetMean(), hist.GetMeanError(), Ytitle_unit),
'')
115 leg2.AddEntry(
'',
'#sigma_{{eff}} (68.5%) ={0:.4g} {1}'.
format(effsigma, Ytitle_unit),
'')
119 c.SaveAs(outname+
'.pdf')
120 c.SaveAs(outname+
'.png')
123 ROOT.gSystem.ProcessEvents()
128 return hist.GetMean(), hist.GetMeanError(), effsigma, 0.0
132 c = ROOT.TCanvas(
'c',
'c', 4000, 3700)
134 ROOT.gPad.SetRightMargin(0.185)
135 ROOT.gPad.SetTopMargin(TopMargin)
136 ROOT.gPad.SetLeftMargin(LeftMargin)
137 ROOT.gPad.SetBottomMargin(BottomMargin)
138 ROOT.gStyle.SetPaintTextFormat(
'1.5f')
139 hist.SetMarkerSize(0.4)
143 hist.GetXaxis().SetTickSize(TickSize)
144 hist.GetYaxis().SetTickSize(TickSize)
145 hist.GetXaxis().SetTitleSize(AxisTitleSize)
146 hist.GetYaxis().SetTitleSize(AxisTitleSize)
147 hist.GetZaxis().SetTitleSize(AxisTitleSize)
148 hist.GetXaxis().SetLabelSize(AxisLabelSize)
149 hist.GetYaxis().SetLabelSize(AxisLabelSize)
150 hist.GetXaxis().SetTitleOffset(1.1)
151 hist.GetYaxis().SetTitleOffset(1.3)
152 hist.GetZaxis().SetTitleOffset(1.3)
153 hist.GetZaxis().SetLabelSize(AxisLabelSize)
154 hist.GetZaxis().SetRangeUser(hist.GetMinimum(), hist.GetMaximum())
155 hist.LabelsOption(
"v")
156 hist.SetContour(10000)
167 c.SaveAs(outname+
'.pdf')
168 c.SaveAs(outname+
'.png')
171 ROOT.gSystem.ProcessEvents()
176 print (
'In doFitSaveFitresult()')
177 out = ROOT.TFile(fitresname,
'recreate')
178 tree = ROOT.TTree(
'tree',
'tree')
179 arrmean =
array(
'f', [0])
180 arrmeanerr =
array(
'f', [0])
181 arrsigma =
array(
'f', [0])
182 arrsigmaerr =
array(
'f', [0])
183 tree.Branch(
'mean', arrmean,
'mean/F')
184 tree.Branch(
'mean_err', arrmeanerr,
'mean_err/F')
185 tree.Branch(
'sigma', arrsigma,
'sigma/F')
186 tree.Branch(
'sigma_err', arrsigmaerr,
'sigma_err/F')
187 arrmean[0], arrmeanerr[0], arrsigma[0], arrsigmaerr[0] =
Draw_1Dhist_wTF1(reshist, resdata,
False,
True, 50,
'#Deltaz(PV_{Truth}, PV_{Reco}) [cm]',
'cm', histwTF1name)
189 tree.Write(
'', ROOT.TObject.kOverwrite)
191 del reshist, out, tree, arrmean, arrmeanerr, arrsigma, arrsigmaerr
193 def main(dphitxt, dcatxt, infiledir, outfiledir):
196 hM_DiffVtxZ = ROOT.TH1F(
'hM_DiffVtxZ',
'hM_DiffVtxZ_altrange', 200, -5, 5)
197 hM_DiffVtxZ_NClusLayer1 = ROOT.TH2F(
'hM_DiffVtxZ_NClusLayer1',
'hM_DiffVtxZ_NClusLayer1', 200, -5, 5, 200, 0, 4000)
199 df = ROOT.RDataFrame(
'minitree',
'{}/dphi{}deg_dca{}cm/INTTVtxZ.root'.
format(infiledir, dphitxt, dcatxt))
200 data = df.AsNumpy(columns=[
'NTruthVtx',
'PV_z',
'TruthPV_z',
'NClusLayer1'])
202 for i, ntruthvtx
in enumerate(data[
'NTruthVtx']):
204 hM_DiffVtxZ.Fill((data[
'PV_z'][i] - data[
'TruthPV_z'][i]))
205 hM_DiffVtxZ_NClusLayer1.Fill((data[
'PV_z'][i] - data[
'TruthPV_z'][i]), data[
'NClusLayer1'][i])
207 diffarray = data[
'PV_z'] - data[
'TruthPV_z']
208 doFitSaveFitresult(hM_DiffVtxZ, diffarray, outfiledir+
'fitresult_dPhi{}deg_dca{}cm.root'.
format(dphitxt, dcatxt), outfiledir+
'DiffVtxZ_wTF1')
209 Draw_2Dhist(hM_DiffVtxZ_NClusLayer1,
False,
False,
False, 0.13,
'#Deltaz(PV_{Truth}, PV_{Reco}) [cm]',
'Number of clusters (inner)',
'colz', outfiledir+
'DiffVtxZ_NClusLayer1_dphi{}deg_dca{}cm'.
format(dphitxt, dcatxt))
211 del df, data, hM_DiffVtxZ, hM_DiffVtxZ_NClusLayer1
214 print(
'In get2DTable')
215 hM_mean_dPhidcaCut = ROOT.TH2F(
'hM_mean_dPhidcaCut',
'hM_mean_dPhidcaCut', nbinsdphi, 0, nbinsdphi, nbinsdca, 0, nbinsdca)
216 hM_sigma_dPhidcaCut = ROOT.TH2F(
'hM_sigma_dPhidcaCut',
'hM_sigma_dPhidcaCut', nbinsdphi, 0, nbinsdphi, nbinsdca, 0, nbinsdca)
222 for i
in range(0, nbinsdphi):
223 for j
in range(0, nbinsdca):
224 dphicut_deg = dphiinterval * (i+1)
225 dcacut = dcainterval * (j+1)
226 dphitext =
'{:.3f}'.
format(dphicut_deg).replace(
'.',
'p')
227 dcadtext =
'{:.3f}'.
format(dcacut).replace(
'.',
'p')
229 fitrs = ROOT.RDataFrame(
'tree',
'{}/dPhi{}deg_dca{}cm/fitresult_dPhi{}deg_dca{}cm.root'.
format(fitresdir, dphitext, dcadtext, dphitext, dcadtext))
230 res = fitrs.AsNumpy(columns=[
'mean',
'sigma'])
231 l_dPhi.append(dphicut_deg)
233 l_mean.append(res[
'mean'][0])
234 l_sigma.append(res[
'sigma'][0])
235 hM_mean_dPhidcaCut.SetBinContent(i + 1, j + 1, res[
'mean'][0])
236 hM_sigma_dPhidcaCut.SetBinContent(i + 1, j + 1, res[
'sigma'][0])
237 hM_mean_dPhidcaCut.GetXaxis().SetBinLabel(i+1,
'{:.2f}'.
format((i+1)*dphiinterval))
238 hM_mean_dPhidcaCut.GetYaxis().SetBinLabel(j+1,
'{:.3f}'.
format((j+1)*dcainterval))
239 hM_sigma_dPhidcaCut.GetXaxis().SetBinLabel(i+1,
'{:.2f}'.
format((i+1)*dphiinterval))
240 hM_sigma_dPhidcaCut.GetYaxis().SetBinLabel(j+1,
'{:.3f}'.
format((j+1)*dcainterval))
243 except Exception
as e:
246 df_opt = pd.DataFrame(list(
zip(l_dPhi, l_dca, l_mean, l_sigma)), columns =[
'dPhiBin',
'dZBin',
'mean',
'sigma'])
247 df_opt = df_opt.sort_values(by=
'sigma', ascending=
True, ignore_index=
True)
249 del l_dPhi, l_dca, l_mean, l_sigma
250 return hM_mean_dPhidcaCut, hM_sigma_dPhidcaCut, df_opt
252 if __name__ ==
'__main__':
253 parser = OptionParser(usage=
"usage: %prog ver [options -h]")
254 parser.add_option(
"-i",
"--infiledir", dest=
"infiledir", default=
'/sphenix/user/hjheng/TrackletAna/minitree/INTT/VtxEvtMap_ana382_zvtx-20cm_dummyAlignParams', help=
"Input file directory")
255 parser.add_option(
"-o",
"--outdirprefix", dest=
"outdirprefix", default=
'ana382_zvtx-20cm_dummyAlignParams', help=
"Output file directory")
256 parser.add_option(
"-f",
"--dofit", action=
"store_true", dest=
"dofit", default=
False, help=
"Do fit")
257 parser.add_option(
"-p",
"--nbinsdphi", dest=
"nbinsdphi", default=20, help=
"Number of bins in dPhi")
258 parser.add_option(
"-d",
"--nbinsdca", dest=
"nbinsdca", default=20, help=
"Number of bins in dca")
260 (opt, args) = parser.parse_args()
263 infiledir = opt.infiledir
264 outdirprefix = opt.outdirprefix
266 nbinsdphi = int(opt.nbinsdphi)
267 nbinsdca = int(opt.nbinsdca)
269 plotpath =
'./RecoPV_optimization/'
270 os.makedirs(
'{}/{}'.
format(plotpath,outdirprefix), exist_ok=
True)
275 for i
in range(0, nbinsdphi):
276 for j
in range(0, nbinsdca):
277 dphicut_deg = dphiinterval * (i+1)
278 dphicut_rad = dphicut_deg * (np.pi / 180.)
279 dcacut = dcainterval * (j+1)
280 dphitext =
'{:.3f}'.
format(dphicut_deg).replace(
'.',
'p')
281 dcadtext =
'{:.3f}'.
format(dcacut).replace(
'.',
'p')
283 outpath =
'./RecoPV_optimization/{}/dPhi{}deg_dca{}cm/'.
format(outdirprefix,dphitext,dcadtext)
284 os.makedirs(outpath, exist_ok=
True)
287 main(dphitext,dcadtext,infiledir,outpath)
288 except Exception
as e:
291 hM_mean_dPhidcaCut, hM_sigma_dPhidcaCut, df_opt =
get2DTable(
'./RecoPV_optimization/{}'.
format(outdirprefix))
293 ROOT.gStyle.SetPalette(ROOT.kThermometer)
294 ROOT.TGaxis.SetMaxDigits(3)
301 Draw2Dhisttable(hM_mean_dPhidcaCut,
'#Delta#phi [degree]',
'DCA [cm]',
'#LT\Deltaz#GT [cm]',
'colztext45',
'{}/{}/Optimization_GaussianMean_Table'.
format(plotpath,outdirprefix))
302 Draw2Dhisttable(hM_sigma_dPhidcaCut,
'#Delta#phi [degree]',
'DCA [cm]',
'#sigma_{eff} (68.5%) [cm]',
'colztext45',
'{}/{}/Optimization_GaussianSigma_Table'.
format(plotpath,outdirprefix))
305 sigmainfo = df_opt.iloc[i]