BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
filtrator.C
Go to the documentation of this file.
1#include "TCanvas.h"
2#include "TF1.h"
3#include "TH1.h"
4#include "TLegend.h"
5#include "TMath.h"
6
7#include <TFile.h>
8#include <TKey.h>
9#include <TStyle.h>
10
11using namespace std;
12using namespace TMath;
13
14// signal region
15// Float_t leftSig = 2.980;
16// Float_t rightSig = 3.002;
17
18const Float_t massTheor = 2.992;
19
20// Float_t range = 0.012;
21// Float_t leftSig = massTheor - range;
22// Float_t rightSig = massTheor + range;
23// full region
24const Float_t minBound = 2.96;
25const Float_t maxBound = 3.02;
26
28{
29 Double_t fA = minBound;
30 Double_t fB = maxBound;
31
32 vector<Double_t> fT;
33 fT.resize(6); // polynomial
34
35 Double_t x = (2.0 * xx[0] - fA - fB) / (fB - fA);
36 int order = fT.size();
37 if (order == 1)
38 return p[0];
39 if (order == 2)
40 return p[0] + x * p[1];
41 // build the polynomials
42 fT[0] = 1;
43 fT[1] = x;
44 for (Int_t i = 1; i < order; ++i) {
45 fT[i + 1] = 2 * x * fT[i] - fT[i - 1];
46 }
47 Double_t sum = p[0] * fT[0];
48 for (int i = 1; i <= order; ++i) {
49 sum += p[i] * fT[i];
50 }
51 return sum;
52}
53
55{
56
57 // convert from (A,B) to (-1,1), Chebyshev defined on (-1, 1)
58 Double_t x = (2.0 * xx[0] - minBound - maxBound) / (maxBound - minBound);
59
60 Double_t T0 = 1.0;
61 Double_t T1 = 1.0 * x;
62 Double_t T2 = 2.0 * x * x - 1.0;
63 Double_t T3 = 4.0 * x * x * x - 3.0 * x;
64 Double_t result = p[0] * T0 + p[1] * T1 + p[2] * T2 + p[3] * T3;
65
66 return result;
67}
68
70{
71
72 // convert from (A,B) to (-1,1), Chebyshev defined on (-1, 1)
73 Double_t x = (2.0 * xx[0] - minBound - maxBound) / (maxBound - minBound);
74
75 Double_t T0 = 1.0;
76 Double_t T1 = 1.0 * x;
77 Double_t T2 = 2.0 * x * x - 1.0;
78 Double_t T3 = 4.0 * x * x * x - 3.0 * x;
79 Double_t T4 = 8.0 * x * x * x * x - 8.0 * x * x + 1.0;
80 Double_t result = p[0] * T0 + p[1] * T1 + p[2] * T2 + p[3] * T3 + p[4] * T4;
81
82 return result;
83}
84
86{
87
88 // convert from (A,B) to (-1,1), Chebyshev defined on (-1, 1)
89 Double_t x = (2.0 * xx[0] - minBound - maxBound) / (maxBound - minBound);
90
91 Double_t T0 = 1.0;
92 Double_t T1 = 1.0 * x;
93 Double_t T2 = 2.0 * x * x - 1.0;
94 Double_t T3 = 4.0 * x * x * x - 3.0 * x;
95 Double_t T4 = 8.0 * x * x * x * x - 8.0 * x * x + 1.0;
96 Double_t T5 = 16.0 * x * x * x * x * x - 20.0 * x * x * x + 5.0 * x;
97 Double_t T6 = 32.0 * x * x * x * x * x * x - 48.0 * x * x * x * x + 18.0 * x * x - 1.0;
98 Double_t result = p[0] * T0 + p[1] * T1 + p[2] * T2 + p[3] * T3 + p[4] * T4 + p[5] * T5 + p[6] * T6;
99
100 return result;
101}
102
103// Gausian Peak function
105{
106 Double_t arg = (x[0] - par[1]) / par[2];
107 Double_t fitval = par[0] * TMath::Exp(-0.5 * arg * arg);
108 return fitval;
109}
110
111// Sum of background and peak function
113{
114 return gausPeak(x, &par[0]) + backgroundChe4(x, &par[3]);
115}
116
118{
119 gStyle->SetOptStat(kFALSE);
120 auto c = new TCanvas(h->GetName(), h->GetName(), 800, 500);
121 h->SetTitle("");
122 h->GetXaxis()->SetTitle("M_{^{3}He + #pi^{-}}, GeV/c^{2}");
123 h->GetXaxis()->SetTitleSize(0.045);
124 h->GetXaxis()->SetTitleOffset(0.9);
125 h->GetXaxis()->SetTitleFont(62);
126 h->GetXaxis()->SetLabelFont(62);
127
128 h->GetYaxis()->SetTitle(
129 Form("Entries / (%d MeV/c^{2})", (int)(1000 * (3.05 - 2.95) / h->GetXaxis()->GetNbins() + 0.5)));
130 h->GetYaxis()->SetTitleSize(0.045);
131 h->GetYaxis()->SetTitleOffset(1.1);
132 h->GetYaxis()->SetTitleFont(62);
133 h->GetYaxis()->SetLabelFont(62);
134
135 h->SetMarkerStyle(20);
136 h->SetMarkerSize(1.0);
139 h->SetMinimum(0);
140
141 TF1* fitS = h->GetFunction("fitFcn");
142 if (!fitS)
143 return;
144 fitS->SetNpx(500);
145 fitS->SetLineColor(kBlack);
146 TF1* fitB = h->GetFunction("fitBack");
147 if (!fitB)
148 return;
149 fitB->SetLineColor(kBlack);
150 fitB->SetLineStyle(7);
151 auto leg = new TLegend(0.2, 0.2, 0.6, 0.5);
152 // auto leg = new TLegend(0.5, 0.5, 0.9, 0.9);
153
154 Float_t T = S + B;
155
156 Float_t dS = TMath::Sqrt(T + B);
157 Float_t dSB = TMath::Sqrt((T * B + 1) / B / B / B);
158 Float_t dSIGN = 0.5 * TMath::Sqrt(1 + 12 * T * B / (T + B) / (T + B));
159
160 leg->SetHeader("Parameters:", "L");
161 leg->AddEntry((TObject*)0, Form("S = %d #pm %d", S, Int_t(dS)), "");
162 leg->AddEntry((TObject*)0, Form("S / B = %4.3f #pm %4.3f", S / B, dSB), "");
163 leg->AddEntry((TObject*)0, Form("S / #sqrt{S + 2B} = %4.1f #pm %4.1f", S / dS, dSIGN), "");
164 leg->AddEntry((TObject*)0, Form("#mu = %4.4f #pm %4.4f", fitS->GetParameter(1), fitS->GetParError(1)), "");
165 leg->AddEntry((TObject*)0,
166 Form("#sigma = %4.4f #pm %4.4f", TMath::Abs(fitS->GetParameter(2)), fitS->GetParError(2)), "");
167 h->Draw("pe1");
168 leg->Draw();
169
170 // TH1F* hSignal = new TH1F("hSignal", "hSignal", h->GetNbinsX(), h->GetXaxis()->GetXmin(),
171 // h->GetXaxis()->GetXmax()); hSignal->SetMarkerStyle(29); hSignal->SetMarkerSize(1.5);
172 // hSignal->SetMarkerColor(kBlack);
173 // hSignal->SetLineColor(kBlack);
174 // for (Int_t i = 0; i < h->GetNbinsX(); ++i) {
175 // hSignal->SetBinContent(i, h->GetBinContent(i) - fitB->Eval(h->GetBinCenter(i)));
176 // }
177 // hSignal->Draw("pe1 same");
178 // c->SaveAs(Form("%s.png", h->GetName()));
179}
180
181void fitator(TH1F* h, Double_t& Tout, Double_t& Bout, Double_t& sigmaOut)
182{
183
184 if (h->GetEntries() == 0)
185 return;
186
187 Int_t leftBin = 9; // h->FindBin(leftSig);
188 Int_t rightBin = 13; // h->FindBin(rightSig);
189
190 vector<Float_t> values;
191 vector<Float_t> errors;
192 for (Int_t i = leftBin; i < rightBin; i++) {
193 values.push_back(h->GetBinContent(i));
194 errors.push_back(h->GetBinError(i));
195 h->SetBinContent(i, 0);
196 h->SetBinError(i, 1e6);
197 }
198 TF1* fitBack = new TF1("fitBack", backgroundChe4, minBound, maxBound, 5);
199 // TF1* fitBack = new TF1("fitBack", backgroundChe4, minBound, maxBound, 5);
200 // TF1* fitBack = new TF1("fitBack", backgroundChe3, minBound, maxBound, 4);
201 h->Fit(fitBack, "QIER", "", minBound, maxBound);
202
203 Double_t B = 0;
204 Double_t T = 0;
205
206 for (Int_t i = leftBin; i < rightBin; i++) {
207 h->SetBinContent(i, values[i - leftBin]);
208 h->SetBinError(i, errors[i - leftBin]);
209 T += values[i - leftBin];
210 B += fitBack->Eval(h->GetBinCenter(i));
211 }
212
213 // if (T - B < 1000) return;
214 // cout << T << " " << B << endl;
215
216 TF1* fitFcn = new TF1("fitFcn", fitFunction, minBound, maxBound, 8);
217 fitFcn->SetLineColor(kBlue);
218
219 // Устанавливаем параметры гаусса
220 fitFcn->SetParameter(0, (T - B) / 2);
221 fitFcn->SetParameter(1, massTheor);
222 fitFcn->SetParameter(2, 0.004); // Ожидаемая ширина
223 // Ограничения для стабильности
224 fitFcn->SetParLimits(0, 0.1 * (T - B), 1.5 * (T - B)); // Амплитуда не может быть отрицательной
225 fitFcn->SetParLimits(1, massTheor - 0.010, massTheor + 0.010);
226 fitFcn->SetParLimits(2, 0.003, 0.010);
227 fitFcn->FixParameter(3, fitBack->GetParameter(0));
228 fitFcn->FixParameter(4, fitBack->GetParameter(1));
229 fitFcn->FixParameter(5, fitBack->GetParameter(2));
230 fitFcn->FixParameter(6, fitBack->GetParameter(3));
231 fitFcn->FixParameter(7, fitBack->GetParameter(4));
232 // fitFcn->FixParameter(7, fitBack->GetParameter(4));
233 // fitFcn->FixParameter(8, fitBack->GetParameter(5));
234 // fitFcn->FixParameter(9, fitBack->GetParameter(6));
235
236 h->Fit(fitFcn, "QIER+", "e1p", minBound, maxBound);
237 // cout << h->GetName() << endl;
238 Tout = T;
239 Bout = B;
240 sigmaOut = fitFcn->GetParameter(2);
241}
242
243void filtrator(TString inFileName = "/home/merz/data/h3l/h3l_plots_25090_A_withKplus/h3l_plots.root")
244{
245
246 TFile* f = TFile::Open(inFileName);
247 TIter keyList(f->GetListOfKeys());
248 TKey* key;
249
250 Int_t nFiltered = 0;
251
252 cout << "Number of total hist = " << f->GetListOfKeys()->GetSize() << endl;
253
254 Float_t maxS = 0.0;
255 Float_t maxS_B = 0.0;
256 Float_t maxS_S = 0.0;
257 Float_t maxSB = 0.0;
258 Float_t maxSB_B = 0.0;
259 Float_t maxSB_S = 0.0;
260 Float_t maxSig = 0.0;
261 Float_t maxSig_B = 0.0;
262 Float_t maxSig_S = 0.0;
263 TH1F* hMaxS = nullptr;
264 TH1F* hMaxSB = nullptr;
265 TH1F* hMaxSig = nullptr;
266
267 TFile* fOut = new TFile("/home/merz/data/h3l/h3l_plots_25090_A_withKplus/h3l_plots_filtered.root", "recreate");
268
269 vector<Float_t> dSignArray;
270 vector<Float_t> dSArray;
271 vector<Float_t> dBArray;
272 vector<Float_t> signArray;
273 vector<Float_t> sArray;
274 vector<Float_t> bArray;
275 while ((key = (TKey*)keyList())) {
276 TH1F* h = (TH1F*)key->ReadObj();
277 TString name = h->GetName();
278
279 h->Rebin(2);
280 h->Rebin(2);
281
282 Double_t T = 0;
283 Double_t B = 0;
284 Double_t sigma = 0;
285 fitator(h, T, B, sigma);
286
287 if (T == 0 || B == 0 || sigma == 0)
288 continue;
289
290 Float_t S = T - B;
291 Float_t SB = S / B;
292 Float_t SIGN = S / TMath::Sqrt(T + B);
293
294 h->SetTitle(Form("S:%4.2f_B:%4.2f_SB:%4.2f_SIGN:%4.2f", S, B, SB, SIGN));
295
296 if (S > maxS) {
297 maxS = S;
298 maxS_B = B;
299 maxS_S = S;
300 hMaxS = h;
301 hMaxS->SetTitle(Form("maxS___S:%4.2f_B:%4.2f_SB:%4.2f_SIGN:%4.2f", S, B, SB, SIGN));
302 }
303
304 if (SB > maxSB) {
305 maxSB = SB;
306 maxSB_B = B;
307 maxSB_S = S;
308 hMaxSB = h;
309 hMaxSB->SetTitle(Form("maxSB___S:%4.2f_B:%4.2f_SB:%4.2f_SIGN:%4.2f", S, B, SB, SIGN));
310 }
311
312 if (SIGN > maxSig) {
313 maxSig = SIGN;
314 maxSig_B = B;
315 maxSig_S = S;
316 hMaxSig = h;
317 hMaxSig->SetTitle(Form("maxSig___S:%4.2f_B:%4.2f_SB:%4.2f_SIGN:%4.2f", S, B, SB, SIGN));
318 }
319
320 // if (S < 2000)
321 // continue;
322 // if (SB < 0.035)
323 // continue;
324 if (SIGN < 3.9)
325 continue;
326 // if (sigma > 0.0050)
327 // continue;
328 Float_t dS = TMath::Sqrt(T + B);
329 Float_t dB = TMath::Sqrt(B);
330 Float_t dSIGN = 0.5 * TMath::Sqrt(1 + 12 * T * B / (T + B) / (T + B));
331 // cout << h->GetName() << ":\t" << sigma << "\t" << S << "\t" << dS << "\t" << SIGN << "\t" << dSIGN << "\t" <<
332 // B << "\t" << dB << endl;
333 cout << h->GetName() << ":\t" << sigma << "\t" << S << "\t" << SIGN << "\t" << SB << endl;
334 signArray.push_back(SIGN);
335 sArray.push_back(S);
336 bArray.push_back(B);
337 dSignArray.push_back(dSIGN);
338 dBArray.push_back(dB);
339 dSArray.push_back(dS);
340 h->Write();
341 nFiltered++;
342 }
343
344 cout << endl;
345 for (auto s : signArray) {
346 printf("%4.1f, ", s);
347 }
348 cout << endl;
349 for (auto s : dSignArray) {
350 printf("%4.1f, ", s);
351 }
352 cout << endl;
353 for (auto s : sArray) {
354 printf("%4.0f, ", s);
355 }
356 cout << endl;
357 for (auto s : dSArray) {
358 printf("%4.0f, ", s);
359 }
360 cout << endl;
361 for (auto s : bArray) {
362 printf("%4.0f, ", s);
363 }
364 cout << endl;
365 for (auto s : dBArray) {
366 printf("%4.0f, ", s);
367 }
368 cout << endl;
369 cout << endl;
370
371 DrawNicePlot(hMaxS, maxS_S, maxS_B);
372 DrawNicePlot(hMaxSB, maxSB_S, maxSB_B);
373 DrawNicePlot(hMaxSig, maxSig_S, maxSig_B);
374
375 cout << hMaxS->GetName() << "\t" << hMaxSB->GetName() << "\t" << hMaxSig->GetName() << endl;
376
377 cout << "Number of filtered histo = " << nFiltered << endl;
378
379 fOut->Close();
380}
int i
Definition P4_F32vec4.h:22
int Int_t
float Float_t
constexpr Bool_t kFALSE
double Double_t
kBlack
kBlue
char * Form(const char *fmt,...)
R__EXTERN TStyle * gStyle
virtual void SetTitleOffset(Float_t offset=1)
virtual void SetTitleFont(Style_t font=62)
virtual void SetLabelFont(Style_t font=62)
virtual void SetTitleSize(Float_t size=0.04)
virtual void SetLineStyle(Style_t lstyle)
virtual void SetLineColor(Color_t lcolor)
virtual void SetMarkerColor(Color_t mcolor=1)
virtual void SetMarkerStyle(Style_t mstyle=1)
virtual void SetMarkerSize(Size_t msize=1)
Int_t GetNbins() const
virtual Double_t GetParameter(const TString &name) const
virtual void SetNpx(Int_t npx=100)
virtual void SetParLimits(Int_t ipar, Double_t parmin, Double_t parmax)
virtual Double_t GetParError(const char *name) const
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
virtual void SetParameter(const TString &name, Double_t value)
virtual void FixParameter(Int_t ipar, Double_t value)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
virtual Double_t GetBinCenter(Int_t bin) const
void SetTitle(const char *title) override
virtual Double_t GetBinError(Int_t bin) const
TAxis * GetXaxis()
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
virtual void SetBinError(Int_t bin, Double_t error)
TAxis * GetYaxis()
void Draw(Option_t *option="") override
virtual void SetMinimum(Double_t minimum=-1111)
virtual void SetBinContent(Int_t bin, Double_t content)
virtual Double_t GetEntries() const
virtual TF1 * GetFunction(const char *name) const
virtual TH1 * Rebin(Int_t ngroup=2, const char *newname="", const Double_t *xbins=nullptr)
virtual Double_t GetBinContent(Int_t bin) const
virtual void SetTitle(const char *title="")
const char * GetName() const override
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
const Float_t minBound
Definition filtrator.C:24
const Float_t maxBound
Definition filtrator.C:25
const Float_t massTheor
Definition filtrator.C:18
Double_t backgroundChe(Double_t *xx, Double_t *p)
Definition filtrator.C:27
void fitator(TH1F *h, Double_t &Tout, Double_t &Bout, Double_t &sigmaOut)
Definition filtrator.C:181
Double_t backgroundChe4(Double_t *xx, Double_t *p)
Definition filtrator.C:69
Double_t gausPeak(Double_t *x, Double_t *par)
Definition filtrator.C:104
Double_t backgroundChe6(Double_t *xx, Double_t *p)
Definition filtrator.C:85
void DrawNicePlot(TH1F *h, Int_t S, Float_t B)
Definition filtrator.C:117
Double_t backgroundChe3(Double_t *xx, Double_t *p)
Definition filtrator.C:54
Double_t fitFunction(Double_t *x, Double_t *par)
Definition filtrator.C:112
void filtrator(TString inFileName="/home/merz/data/h3l/h3l_plots_25090_A_withKplus/h3l_plots.root")
Definition filtrator.C:243
Double_t Exp(Double_t x)
Double_t Sqrt(Double_t x)
Double_t Abs(Double_t d)
Definition json.hpp:4446