Belle II Software  light-2303-iriomote
2_analysis_pi0.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """
13 <header>
14  <input>../GenericB_GENSIMRECtoDST.dst.root</input>
15  <output>Pi0_Validation.root</output>
16  <contact>Mario Merola (mario.merola@na.infn.it), Andrea Selce (selce@infn.it)</contact>
17  <description>
18  Check the calibration of the ECL in the MC by determining the measured pi0 invariant mass.
19  </description>
20 </header>
21 """
22 
23 import basf2
24 import ROOT
25 from modularAnalysis import cutAndCopyList, inputMdst
26 from validation_tools.metadata import create_validation_histograms
27 from validation_tools.metadata import validation_metadata_update
28 from variables import variables as vm
29 
30 INPUT_FILENAME = "../GenericB_GENSIMRECtoDST.dst.root"
31 OUTPUT_FILENAME = "Pi0_Validation.root"
32 
33 main = basf2.Path()
34 inputMdst(INPUT_FILENAME, path=main)
35 
36 cutAndCopyList('pi0:rec', 'pi0:all', 'daughter(0, E)>0.05 and daughter(1, E)>0.05', path=main)
37 cutAndCopyList('pi0:mc', 'pi0:all', 'mcErrors<1', path=main)
38 
39 vm.addAlias('Mreco', 'M')
40 
41 
42 create_validation_histograms(
43  main, OUTPUT_FILENAME, "pi0:rec",
44  variables_1d=[
45  (
46  "Mreco", 40, 0.08, 0.18,
47  "#pi^{0} reconstructed candidates, invariant mass",
48  "Andrea Selce <selce@infn.it>",
49  r"The $\pi^0$ invariant mass distribution with $E_{\gamma}>0.05\, \text{GeV}$",
50  r"Distribution should be peaking at the nominal $\pi^0$ mass.",
51  "M(#pi^{0}) [GeV/c^{2}]", "Candidates", "shifter"
52  ),
53  ],
54  description=r"$\pi^0$ reconstructed mass distribution",
55 )
56 
57 
58 vm.addAlias('Mmc', 'M')
59 
60 create_validation_histograms(
61  main, OUTPUT_FILENAME, "pi0:mc",
62  variables_1d=[
63  (
64  "Mmc", 40, 0.08, 0.18,
65  "#pi^{0} MC candidates, invariant mass",
66  "Andrea Selce <selce@infn.it>",
67  r"The $\pi^0$ invariant mass distribution for truth matched candidates",
68  r"Distribution should be peaking at the nominal $\pi^0$ mass.",
69  "M(#pi^{0}) [GeV/c^{2}]", "Candidates", "shifter"
70  ),
71  ],
72  description=r"$\pi^0$ MC mass distribution",
73 )
74 
75 main.add_module('Progress')
76 basf2.process(main)
77 print(basf2.statistics)
78 
79 
80 f = ROOT.TFile(OUTPUT_FILENAME)
81 Mrecohist = f.Get('Mreco')
82 Mmchist = f.Get('Mmc')
83 
84 
85 mass = ROOT.RooRealVar("recomass", "m_{#gamma#gamma} [GeV/c^{2}]", 0.11, 0.15)
86 
87 h_pi0_reco = ROOT.RooDataHist("h_pi0_reco", "h_pi0_reco", ROOT.RooArgList(mass), Mrecohist)
88 h_pi0_mc = ROOT.RooDataHist("h_pi0_mc", "h_pi0_mc", ROOT.RooArgList(mass), Mmchist)
89 
90 
91 # pi0 signal PDF is a Gaussian (Crystal Ball also listed in case we want to switch)
92 mean = ROOT.RooRealVar("mean", "mean", 0.125, 0.11, 0.15)
93 sig1 = ROOT.RooRealVar("#sigma", "sig", 0.007, 0.002, 0.1)
94 gau1 = ROOT.RooGaussian("gau1", "gau1", mass, mean, sig1)
95 
96 alphacb = ROOT.RooRealVar("alphacb", "alpha", 1.5, 0.1, 1.9)
97 ncb = ROOT.RooRealVar("ncb", "n", 8) # ,2.,15)
98 sigcb = ROOT.RooCBShape("sigcb", "sigcb", mass, mean, sig1, alphacb, ncb)
99 
100 # pi0 background PDF is a 2nd order Chebyshev
101 b1 = ROOT.RooRealVar("b1", "b1", 0.1, -1, 1)
102 a1 = ROOT.RooRealVar("a1", "a1", 0.1, -1, 1)
103 bList = ROOT.RooArgList(a1, b1)
104 bkg = ROOT.RooChebychev("bkg", "bkg", mass, bList)
105 
106 
107 nsig = ROOT.RooRealVar("nsig", "nsig", 3000, 0, 1000000)
108 nbkg = ROOT.RooRealVar("nbkg", "nbkg", 12000, 0, 1000000)
109 
110 
111 totalPdf = ROOT.RooAddPdf("totalpdf", "", ROOT.RooArgList(gau1, bkg), ROOT.RooArgList(nsig, nbkg))
112 
113 
114 output = ROOT.TFile("Pi0_Validation_ntuple.root", "recreate")
115 
116 # Store pi0 mass fit results to a tuple for comparison of mean and width among releases.
117 outputNtuple = ROOT.TNtuple(
118  "pi0_mass",
119  "Pi0 mass fit results",
120  "mean:meanerror:width:widtherror:mean_MC:meanerror_MC:width_MC:widtherror_MC")
121 
122 
123 ROOT.gROOT.SetBatch(True)
124 canvas = ROOT.TCanvas("canvas", "pi0 mass fit", 1000, 600)
125 canvas.Divide(2, 1)
126 canvas.cd(1)
127 
128 # Fit to the reco mass
129 totalPdf.fitTo(h_pi0_reco, ROOT.RooFit.Extended(True), ROOT.RooFit.Minimizer("Minuit2", "Migrad"))
130 frame1 = mass.frame()
131 h_pi0_reco.plotOn(frame1, ROOT.RooFit.Name("Hist"))
132 frame1.SetMaximum(frame1.GetMaximum())
133 totalPdf.plotOn(frame1, ROOT.RooFit.Name("curve"))
134 totalPdf.plotOn(frame1, ROOT.RooFit.Components("gau1"), ROOT.RooFit.LineStyle(ROOT.kDashed), ROOT.RooFit.LineColor(ROOT.kRed))
135 totalPdf.plotOn(frame1, ROOT.RooFit.Components("bkg"), ROOT.RooFit.LineStyle(3), ROOT.RooFit.LineColor(ROOT.kBlue))
136 frame1.SetMaximum(Mrecohist.GetMaximum() * 1.5)
137 frame1.GetXaxis().SetTitleOffset(1.4)
138 frame1.GetYaxis().SetTitleOffset(1.5)
139 meanval = mean.getVal()
140 meanerror = mean.getError()
141 width = sig1.getVal()
142 widtherror = sig1.getError()
143 frame1.Draw("")
144 
145 canvas.cd(2)
146 
147 # ---------------------------
148 # Fit to the truth matched mass.
149 # The same signal parametrisation as for the reco mass but only a 1st order Chebyshev polynomial are used.
150 # Re-initialize the fit parameters.
151 mean.setVal(0.125)
152 sig1.setVal(0.007)
153 nsig.setVal(2200)
154 nbkg.setVal(700)
155 a1.setVal(-0.5)
156 
157 bkg = ROOT.RooChebychev("bkg1", "bkg", mass, a1)
158 totalPdf = ROOT.RooAddPdf("totalpdfMC", "", ROOT.RooArgList(gau1, bkg), ROOT.RooArgList(nsig, nbkg))
159 
160 # Fit to the truth matched mass
161 totalPdf.fitTo(h_pi0_mc, ROOT.RooFit.Extended(True), ROOT.RooFit.Minimizer("Minuit2", "Migrad"))
162 frame2 = mass.frame()
163 h_pi0_mc.plotOn(frame2, ROOT.RooFit.Name("Hist"))
164 frame2.SetMaximum(frame2.GetMaximum())
165 totalPdf.plotOn(frame2, ROOT.RooFit.Name("curve"))
166 totalPdf.plotOn(frame2, ROOT.RooFit.Components("gau1"), ROOT.RooFit.LineStyle(ROOT.kDashed), ROOT.RooFit.LineColor(ROOT.kRed))
167 totalPdf.plotOn(frame2, ROOT.RooFit.Components("bkg1"), ROOT.RooFit.LineStyle(3), ROOT.RooFit.LineColor(ROOT.kBlue))
168 frame2.SetMaximum(Mmchist.GetMaximum() * 1.5)
169 frame2.GetXaxis().SetTitleOffset(1.4)
170 frame2.GetYaxis().SetTitleOffset(1.5)
171 meanval_mc = mean.getVal()
172 meanerror_mc = mean.getError()
173 width_mc = sig1.getVal()
174 widtherror_mc = sig1.getError()
175 frame2.Draw("")
176 
177 canvas.Write()
178 
179 outputNtuple.Fill(meanval, meanerror, width, widtherror, meanval_mc, meanerror_mc, width_mc, widtherror_mc)
180 
181 outputNtuple.Write()
182 
183 validation_metadata_update(
184  output,
185  "pi0_mass",
186  title="Pi0 mass fit results",
187  contact="selce@infn.it",
188  description="Fit to the invariant mass of the reconstructed and truth matched pi0s",
189  check="Consistent numerical fit results. Stable mean and width.",
190  metaoptions="shifter")
191 
192 
193 output.Close()