Belle II Software development
HadronPrep.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <cdc/calibration/CDCdEdx/HadronPrep.h>
10using namespace Belle2;
11
13{
14 m_bgBins = 10;
15 m_bgMin = 2.85;
16 m_bgMax = 40;
17 m_cosBins = 8;
18 m_cosMin = 0.0;
19 m_cosMax = 0.95;
20 m_cut = 0.5;
21}
22
23HadronPrep::HadronPrep(int bgbins, double lowerbg, double upperbg, int cosbins, double lowercos, double uppercos, double cut)
24{
25 m_bgBins = bgbins;
26 m_bgMin = lowerbg;
27 m_bgMax = upperbg;
28 m_cosBins = cosbins;
29 m_cosMin = lowercos;
30 m_cosMax = uppercos;
31 m_cut = cut;
32}
33
34void HadronPrep::prepareSample(std::shared_ptr<TTree> hadron, TFile*& outfile, const std::string& suffix,
35 const std::string& pdg, bool ismakePlots, bool correct)
36{
37
38 std::map<int, std::vector<TH1F*>> hdedx_bgcosth;
39 std::vector<TH2F*> hdedxvscosth_bg;
40
41 //define histograms
42 for (int i = 0; i < m_bgBins; ++i) {
43
44 std::string title = Form("%s_dedxvscos_bg_%d", pdg.data(), i);
45 hdedxvscosth_bg.push_back(new TH2F(title.data(), Form("%s;costh;dEdx", title.data()),
46 440, -1.1, 1.1, 2600, -1.0, 25));
47
48 defineHisto(hdedx_bgcosth[i], Form("dedx_bg_%d_cos", i), pdg.data());
49 }
50
51
52 // Create some containers to calculate averages
53 for (int i = 0; i < m_bgBins; ++i) {
54 for (int j = 0; j < m_cosBins; ++j) {
55 m_sumcos[i].push_back(0.);
56 m_sumbg[i].push_back(0.);
57 m_sumsize[i].push_back(0.);
58 m_means[i].push_back(0.);
59 m_errors[i].push_back(0.);
60
61 }
62 }
63
64 // // --------------------------------------------------
65 // // LOOP OVER EVENTS AND FILL CONTAINERS
66 // // --------------------------------------------------
67 // // Fill the histograms to be fitted
68
69 double dedxnosat; // dE/dx w/o HS correction (use to get new parameters)
70 double p; // track momentum
71 double costh; // cosine of track polar angle
72 double timereso;
73 int nhit; // number of hits on this track
74
75 hadron->SetBranchAddress("dedxnosat", &dedxnosat); //must be without any HS correction
76 hadron->SetBranchAddress("p", &p);
77 hadron->SetBranchAddress("costh", &costh);
78 hadron->SetBranchAddress("timereso", &timereso);
79 hadron->SetBranchAddress("nhits", &nhit);
80
81 HadronBgPrep prep;
82 double mass = prep.getParticleMass(pdg);
83 if (mass == 0.0) B2FATAL("Mass of particle " << pdg.data() << " is zero");
84
85 CDCDedxHadSat had;
86
87 if (correct) {
88 std::string infile = "sat-pars.fit.txt";
89 std::ifstream parfile(infile);
90
91 if (!parfile.fail()) {
92 B2INFO("new parameters are using");
93 had.setParameters(infile);
94 } else {
95 B2INFO("default parameters are using");
96 had.setParameters();
97 }
98 }
99
100 for (unsigned int index = 0; index < hadron->GetEntries(); ++index) {
101
102 hadron->GetEvent(index);
103
104 double bg; // track beta-gamma
105 bg = fabs(p) / mass;
106
107 double uncosth = costh;
108 costh = fabs(costh);
109
110 // clean up bad events and restrict the momentum range
111 if (nhit < 0 || nhit > 100)continue;
112 if (dedxnosat <= 0)continue;
113 if (costh != costh)continue;
114 if (bg <= m_bgMin || bg >= m_bgMax)continue;
115 if (costh <= m_cosMin || costh >= m_cosMax)continue;
116
117 if (pdg == "proton") if ((dedxnosat - 0.45) * abs(p) * abs(p) < m_cut)continue;
118
119 int bgBin = (int)((bg - m_bgMin) / (m_bgMax - m_bgMin) * m_bgBins);
120 int cosBin = (int)((costh - m_cosMin) / (m_cosMax - m_cosMin) * m_cosBins);
121
122 double dedx_new = dedxnosat;
123
124 if (correct) dedx_new = had.D2I(costh, had.I2D(costh, 1.0) * dedxnosat);
125
126 hdedx_bgcosth[bgBin][cosBin]->Fill(dedx_new);
127 hdedxvscosth_bg[bgBin]->Fill(uncosth, dedx_new);
128
129 m_sumcos[bgBin][cosBin] += costh;
130 m_sumbg[bgBin][cosBin] += bg;
131 m_sumsize[bgBin][cosBin] += 1;
132
133 }// end of event loop
134
135 // // --------------------------------------------------
136 // // FIT IN BINS OF BETA-GAMMA AND COS(THETA)
137 // // --------------------------------------------------
138 // // fit the histograms with Gaussian functions
139 // // and extract the means and errors
140
141 setPars(outfile, hdedx_bgcosth, pdg.data());
142
143 if (ismakePlots) {
144
145 plotDist(hdedx_bgcosth, Form("fits_dedx_inbg_%s", suffix.data()), pdg.data());
146 plotDist(hdedxvscosth_bg, Form("dist_dedx_vscos_scat_%s", suffix.data()), pdg.data());
147 plotGraph(suffix.data(), pdg.data());
148 }
149
150 hdedxvscosth_bg.clear();
151 hdedx_bgcosth.clear();
152 clear();
153
154}
155
156//------------------------------------
157void HadronPrep::defineHisto(std::vector<TH1F*>& htemp, const std::string& svar, const std::string& pdg)
158{
159 for (int j = 0; j < m_cosBins; ++j) {
160 // initialize the histograms
161 std::string title = Form("%s_%s_%d", pdg.data(), svar.data(), j);
162 double dedxMax = 4.0;
163 int cosbins = int(50 * dedxMax);
164 if (pdg == "pion") {
165 dedxMax = 2.0; cosbins = int(100 * dedxMax);
166 }
167 if (pdg == "proton") dedxMax = 20.0;
168 if (svar.substr(0, svar.find("_")) == "dedx")
169 htemp.push_back(new TH1F(title.data(), title.data(), cosbins, 0, dedxMax));
170 else
171 htemp.push_back(new TH1F(title.data(), title.data(), cosbins, -30, 30));
172
173 }
174}
175
176//------------------------------------
177void HadronPrep::plotDist(std::map<int, std::vector<TH1F*>>& hist, const std::string& sname, const std::string& pdg)
178{
179
180 TCanvas ctmp(Form("cdcdedx_%s_%s", sname.data(), pdg.data()), "", 1200, 1200);
181 ctmp.Divide(2, 2);
182 ctmp.SetBatch(kTRUE);
183
184 std::stringstream psname;
185 psname << Form("plots/HadronSat/%s_%s.pdf[", sname.data(), pdg.data());
186 ctmp.Print(psname.str().c_str());
187 psname.str("");
188 psname << Form("plots/HadronSat/%s_%s.pdf", sname.data(), pdg.data());
189
190 for (int i = 0 ; i < m_bgBins; ++i) {
191 for (int j = 0; j < m_cosBins; ++j) {
192 hist[i][j]->SetFillColor(kYellow - 9);
193
194 ctmp.cd(j % 4 + 1);
195 hist[i][j]->Draw();
196
197 if (m_cosBins <= 4) {
198 if (j + 1 == m_cosBins) ctmp.Print(psname.str().c_str());
199 } else if ((j + 1) % 4 == 0 || (j + 1 == m_cosBins)) {
200 ctmp.Print(psname.str().c_str());
201 ctmp.Clear("D");
202 }
203 }
204 }
205 psname.str("");
206 psname << Form("plots/HadronSat/%s_%s.pdf]", sname.data(), pdg.data());
207 ctmp.Print(psname.str().c_str());
208}
209
210//------------------------------------
211void HadronPrep::plotDist(std::vector<TH2F*>& hist, const std::string& sname, const std::string& pdg)
212{
213
214 TCanvas ctmp(Form("cdcdedx_%s_%s", sname.data(), pdg.data()), "", 1200, 1200);
215 ctmp.Divide(2, 2);
216 ctmp.SetBatch(kTRUE);
217
218 std::stringstream psname;
219 psname << Form("plots/HadronSat/%s_%s.pdf[", sname.data(), pdg.data());
220 ctmp.Print(psname.str().c_str());
221 psname.str("");
222 psname << Form("plots/HadronSat/%s_%s.pdf", sname.data(), pdg.data());
223 double bgstep = (m_bgMax - m_bgMin) / m_bgBins;
224 double dmax = 2.5;
225 if (pdg == "kaon")dmax = 3.5;
226 else if (pdg == "proton")dmax = 15.5;
227
228 for (int i = 0 ; i < m_bgBins; ++i) {
229 ctmp.cd(i % 4 + 1);
230 double frombg = m_bgMin + i * bgstep;
231 double tobg = frombg + bgstep;
232
233 hist[i]->SetTitle(Form("%s, pF=(%0.02f,%0.02f)", hist[i]->GetTitle(), frombg, tobg));
234 hist[i]->GetYaxis()->SetRangeUser(-0.5, dmax);
235 hist[i]->DrawCopy("colz");
236 if ((i + 1) % 4 == 0 || (i + 1) == m_bgBins) {
237 ctmp.Print(psname.str().c_str());
238 ctmp.Clear("D");
239 }
240 }
241 psname.str("");
242 psname << Form("plots/HadronSat/%s_%s.pdf]", sname.data(), pdg.data());
243 ctmp.Print(psname.str().c_str());
244}
245
246//----------------------------------------
247void HadronPrep::setPars(TFile*& outfile, std::map<int, std::vector<TH1F*>>& hdedx_bgcosth, const std::string& pdg)
248{
249 outfile->cd();
250 TTree* satTree = new TTree(Form("%s", pdg.data()), "dE/dx means and errors");
251 double satbg; // beta-gamma value for this bin
252 double satcosth; // cos(theta) value for this bin
253 double satdedx; // mean dE/dx value for this bin
254 double satdedxerr; // error on ^
255 double satdedxwidth;// width of ^ distribution
256 double satbg_avg; // average beta-gamma value for this sample
257 double satcosth_avg; // average cos(theta) value for this sample
258
259 satTree->Branch("bg", &satbg, "bg/D");
260 satTree->Branch("costh", &satcosth, "costh/D");
261
262 //dEdx related variables
263 satTree->Branch("dedx", &satdedx, "dedx/D");
264 satTree->Branch("dedxerr", &satdedxerr, "dedxerr/D");
265 satTree->Branch("dedxwidth", &satdedxwidth, "dedxwidth/D");
266
267 // Other variables
268 satTree->Branch("bg_avg", &satbg_avg, "bg_avg/D");
269 satTree->Branch("costh_avg", &satcosth_avg, "costh_avg/D");
270
271 double bgstep = (m_bgMax - m_bgMin) / m_bgBins;
272 double cosstep = (m_cosMax - m_cosMin) / m_cosBins;
273
274 for (int i = 0; i < m_bgBins; ++i) {
275 for (int j = 0; j < m_cosBins; ++j) {
276
277 // fill some details for this bin
278 satbg = m_bgMin + 0.5 * bgstep + i * bgstep;
279 satcosth = m_cosMin + 0.5 * cosstep + j * cosstep;
280
281 satbg_avg = m_sumbg[i][j] / m_sumsize[i][j];
282 satcosth_avg = m_sumcos[i][j] / m_sumsize[i][j];
283
284 //1. -------------------------
285 // fit the dE/dx distribution in bins of beta-gamma and cosine
286 HadronBgPrep prep;
287 prep.fit(hdedx_bgcosth[i][j], pdg.data());
288 satdedx = m_means[i][j] = hdedx_bgcosth[i][j]->GetFunction("gaus")->GetParameter(1);
289 satdedxerr = m_errors[i][j] = hdedx_bgcosth[i][j]->GetFunction("gaus")->GetParError(1);
290 satdedxwidth = hdedx_bgcosth[i][j]->GetFunction("gaus")->GetParameter(2);
291
292 //Be careful to not set for every particle
293 if (satdedx > m_dedxmax) m_dedxmax = satdedx;
294 if (satdedx < m_dedxmin) m_dedxmin = satdedx;
295
296 // fill the tree for this bin
297 satTree->Fill();
298 }
299 }
300 outfile->Write();
301}
302
303//------------------------------------
304void HadronPrep::plotGraph(const std::string& sname, const std::string& pdg)
305{
306 std::vector<double> cbcenters(m_cosBins), cberrors(m_cosBins);
307 double cosstep = (m_cosMax - m_cosMin) / m_cosBins;
308 double bgstep = (m_bgMax - m_bgMin) / m_bgBins;
309
310 for (int j = 0; j < m_cosBins; ++j) {
311 cbcenters[j] = m_cosMin + 0.5 * cosstep + j * cosstep;
312 cberrors[j] = 0;
313 }
314
315 // Plot the dE/dx means vs. cos(theta) for validation
316 TCanvas ctmp6(Form("cdcdedx_costh_%s_%s", sname.data(), pdg.data()), "", 500, 500);
317 ctmp6.SetGridy(1);
318
319 TH1F base("base", "bla-bla", 25, 0, 1.0);
320 base.SetTitle(Form("%s: dE/dx fit means vs. cos(#theta);cos(#theta);dE/dx-fit mean", pdg.data()));
321 base.GetXaxis()->SetTitleOffset(1.2);
322
323 double margin = 0.1;
324 if (pdg == "proton") margin = 0.95;
325
326 base.SetMaximum(m_dedxmax + margin);
327 base.SetMinimum(m_dedxmin - margin);
328 base.SetStats(0);
329 base.DrawCopy("");
330
331 TLegend legend(0.75, 0.75, 0.95, 0.9);
332
333 std::vector<TGraphErrors*> gdedx_costh(m_bgBins);
334
335 legend.SetBorderSize(0);
336
337 for (int i = 0; i < m_bgBins; ++i) {
338 std::vector<double> mean_d(m_cosBins);
339 std::vector<double> error_d(m_cosBins);
340
341 for (int j = 0; j < m_cosBins; ++j) {
342 mean_d [j] = m_means[i][j];
343 error_d[j] = m_errors[i][j];
344 }
345 gdedx_costh[i] = new TGraphErrors(m_cosBins, cbcenters.data(), mean_d.data(), cberrors.data(), error_d.data());
346
347 gdedx_costh[i]->SetMarkerSize(0.9);
348 gdedx_costh[i]->SetMarkerColor(50 + i * 3);
349 gdedx_costh[i]->SetLineColor(i + 1);
350 gdedx_costh[i]->SetLineWidth(1);
351 gdedx_costh[i]->Draw("same");
352 legend.AddEntry(gdedx_costh[i], Form("bg = (%0.03f - %0.03f)", m_bgMin + i * bgstep, m_bgMin + (i + 1) * bgstep), "lep");
353 }
354
355 legend.Draw("same");
356
357 std::stringstream psname;
358 psname.str("");
359 psname << Form("plots/HadronSat/gr_dedx_vscosth_%s_%s.pdf", sname.data(), pdg.data());
360 ctmp6.SaveAs(psname.str().c_str());
361
362 for (int i = 0; i < m_bgBins; ++i)
363 delete gdedx_costh[i];
364}
365
366
368{
369 m_sumcos.clear();
370 m_sumbg.clear();
371 m_sumsize.clear();
372 m_means.clear();
373 m_errors.clear();
374}
Class to hold the hadron saturation functions.
Definition: CDCDedxHadSat.h:31
double I2D(double cosTheta, double I) const
hadron saturation parameterization part 2
double D2I(double cosTheta, double D) const
hadron saturation parameterization part 1
void setParameters()
set the parameters
Class to prepare sample for fitting in beta gamma bins.
Definition: HadronBgPrep.h:43
double getParticleMass(const std::string &particle)
function to get the particle mass
Definition: HadronBgPrep.h:160
void fit(TH1F *&hist, const std::string &pdg)
function to fit the histograms
void prepareSample(std::shared_ptr< TTree > hadron, TFile *&outfile, const std::string &suffix, const std::string &pdg, bool ismakePlots, bool correct)
function to prepare sample for monitoring plots, bg curve fitting and sigma vs ionz fitting
Definition: HadronPrep.cc:34
double m_dedxmax
variables to set maximum dedx mean
Definition: HadronPrep.h:104
int m_cosBins
bins for dedx histogram
Definition: HadronPrep.h:111
std::map< int, std::vector< double > > m_errors
error variable
Definition: HadronPrep.h:100
double m_cosMax
max range of dedx
Definition: HadronPrep.h:113
std::map< int, std::vector< int > > m_sumsize
variables for size
Definition: HadronPrep.h:102
HadronPrep()
Constructor: Sets the description, the properties and the parameters of the algorithm.
Definition: HadronPrep.cc:12
double m_dedxmin
variables to set minimum dedx mean
Definition: HadronPrep.h:105
void plotDist(std::map< int, std::vector< TH1F * > > &hist, const std::string &sname, const std::string &pdg)
function to plot the map of histograms
Definition: HadronPrep.cc:177
int m_bgBins
bins for dedx histogram
Definition: HadronPrep.h:107
void setPars(TFile *&outfile, std::map< int, std::vector< TH1F * > > &hdedx_bgcosth, const std::string &pdg)
function to fill the parameters like mean and reso in the tree
Definition: HadronPrep.cc:247
double m_bgMax
max range of dedx
Definition: HadronPrep.h:109
void plotGraph(const std::string &sname, const std::string &pdg)
function to make graph dedx vs cos in different bg bins
Definition: HadronPrep.cc:304
void defineHisto(std::vector< TH1F * > &htemp, const std::string &title, const std::string &pdg)
function to define histograms
Definition: HadronPrep.cc:157
double m_bgMin
min range of dedx
Definition: HadronPrep.h:108
double m_cosMin
min range of dedx
Definition: HadronPrep.h:112
double m_cut
cut to clean protons
Definition: HadronPrep.h:115
void clear()
function to clear the variables
Definition: HadronPrep.cc:367
std::map< int, std::vector< double > > m_means
mean variable
Definition: HadronPrep.h:99
std::map< int, std::vector< double > > m_sumbg
variables to add bg values
Definition: HadronPrep.h:98
std::map< int, std::vector< double > > m_sumcos
variables to add cos values
Definition: HadronPrep.h:97
Abstract base class for different kinds of events.