CbmRoot
Loading...
Searching...
No Matches
CaToolsWFExpression.cxx
Go to the documentation of this file.
1/* Copyright (C) 2022 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergey Gorbunov, Sergei Zharko [committer] */
4
5
7
8#include "Logger.h"
9#include "TBox.h"
10#include "TF1.h"
11#include "TGraph.h"
12#include "TH1F.h"
13#include "TH2F.h"
14#include "TPad.h"
15#include "TTree.h"
16
17#include <algorithm>
18
20
21// ---------------------------------------------------------------------------------------------------------------------
22//
23WFExpression::WFExpression(TTree* pTree, const char* exprDist, const char* exprParam)
24 : fpTree(pTree)
25 , fsExprDist(exprDist)
26 , fsExprParam(exprParam)
27{
28 // Assertions
29 // TODO: SZh 11.11.2022: REPLACE ALL THE ASSERTIONS WITH EXCEPTIONS!!!!
30 assert(fsExprDist.Length());
31 assert(fsExprParam.Length());
32 assert(fpTree);
33}
34
35// ---------------------------------------------------------------------------------------------------------------------
36//
37std::tuple<std::vector<float>, std::vector<float>> WFExpression::CalculateParameters()
38{
39 // Check, if data were initialized
40 assert(fpPadBase);
41 assert(fpPadSlices);
42
43 // Unique name, used to access a histogram
44 fsName = Form("%s:%s_%s", fsExprDist.Data(), fsExprParam.Data(), fCut.GetTitle());
45 fsName.ReplaceAll(" ", "");
46
47 // Expression to be drawn from the tree
48 TString sExpr = Form("%s:%s>>htemp(%d,,,%d)", fsExprDist.Data(), fsExprParam.Data(), fNbinsParam, fNbinsDist);
49
50 // Get base distribution
51 fpPadBase->cd();
52 fpTree->Draw(sExpr.Data(), fCut, "colz");
53 fpHistBase = (TH2F*) gPad->GetPrimitive("htemp");
54 fpHistBase->Sumw2();
55 fpHistBase->SetStats(kFALSE);
56 fpHistBase->SetName(fsName);
57 fpHistBase->SetTitle(fsTitle);
58
59 // Create slices
60 int sliceSize = fNbinsParam / fNslices;
61 fpPadSlices->cd();
62 fpPadSlices->Divide(fNslices == 1 ? 1 : 2, (fNslices + 1) / 2);
63
65 for (int iS = 0; iS < fNslices; ++iS) {
66 fpPadSlices->cd(iS + 1);
67 int iBinMin = sliceSize * iS;
68 int iBinMax = sliceSize * (iS + 1) - 1;
69 std::tie(fvLoSBoundaries[iS], fvUpSBoundaries[iS], fvSCenters[iS]) = ProcessSlice(iBinMin, iBinMax);
70 assert(fvLoSBoundaries[iS] <= fvUpSBoundaries[iS]);
71 }
72
73 // Draw edges, obtained for different slices
74 fpPadBase->cd();
75 TGraph* pGrUpper = new TGraph(fNslices, fvSCenters.data(), fvUpSBoundaries.data());
76 pGrUpper->SetMarkerColor(kOrange + 2);
77 pGrUpper->SetMarkerStyle(20);
78 pGrUpper->Draw("PSAME");
79 TGraph* pGrLower = new TGraph(fNslices, fvSCenters.data(), fvLoSBoundaries.data());
80 pGrLower->SetMarkerColor(kOrange + 2);
81 pGrLower->SetMarkerStyle(20);
82 pGrLower->Draw("PSAME");
83
84 // Get window parameters
86
87 return std::tie(fvLoParams, fvUpParams);
88}
89
90// ---------------------------------------------------------------------------------------------------------------------
91//
92std::tuple<float, float, float> WFExpression::ProcessSlice(int iBinMin, int iBinMax)
93{
94 // ----- Create a slice
95 assert(iBinMin <= iBinMax);
96 float lEdge = fpHistBase->GetXaxis()->GetBinLowEdge(iBinMin + 1);
97 float uEdge = fpHistBase->GetXaxis()->GetBinUpEdge(iBinMax + 1);
98 float sliceCenter = (lEdge + uEdge) * 0.5;
99 const char* parTitle = fpHistBase->GetXaxis()->GetTitle();
100 TString sSliceTitle = Form("%.2f < %s < %.2f (bins %d-%d)", lEdge, parTitle, uEdge, iBinMin, iBinMax);
101 TString sSliceName = Form("%s_slice%d-%d", fpHistBase->GetName(), iBinMin, iBinMax);
102 TH1F* pHistSlice = (TH1F*) fpHistBase->ProjectionY(sSliceName, iBinMin, iBinMax);
103 pHistSlice->SetTitle(sSliceTitle);
104
105 // Slice drawing attributes
106 constexpr float maximumScale = 1.1;
107 pHistSlice->SetMaximum(pHistSlice->GetMaximum() * maximumScale);
108 pHistSlice->Draw("");
109
110 // ----- Find selection boundaries
111 // A simple algorithm, which finds boundaries containing eps/2 entries to the left and right from the central region.
112 int contentL = 0;
113 int contentR = 0;
114 float windowMin = 0.f;
115 float windowMax = 0.f;
116 float nEntriesTot = pHistSlice->GetEntries();
117
118 // left side
119 for (int iBin = 1; contentL < nEntriesTot * fEps * 0.5; ++iBin) {
120 windowMin = pHistSlice->GetXaxis()->GetBinLowEdge(iBin);
121 contentL += pHistSlice->GetBinContent(iBin);
122 }
123
124 // right side
125 for (int iBin = pHistSlice->GetXaxis()->GetNbins(); contentR < nEntriesTot * fEps * 0.5; --iBin) {
126 windowMax = pHistSlice->GetXaxis()->GetBinUpEdge(iBin);
127 contentR += pHistSlice->GetBinContent(iBin);
128 }
129
130 // ----- Draw selection box on the histogram
131 TBox* pWindowBox = new TBox(windowMin, pHistSlice->GetMinimum(), windowMax, pHistSlice->GetMaximum());
132 pWindowBox->SetFillColor(kGreen);
133 pWindowBox->Draw("SAME");
134 pHistSlice->Draw("SAME");
135
136 return std::tie(windowMin, windowMax, sliceCenter);
137}
138
139// ---------------------------------------------------------------------------------------------------------------------
140//
142{
143 assert(kNpars == 1); // Cannot be used for a different number of parameters
144 assert(fvUpParams.size() == 1);
145 assert(fvLoParams.size() == 1);
146 // In case of several slices we select the most outlying point as a boundary of the window
147 fvUpParams[0] = *(std::max_element(fvUpSBoundaries.begin(), fvUpSBoundaries.end()));
148 fvLoParams[0] = *(std::min_element(fvLoSBoundaries.begin(), fvLoSBoundaries.end()));
149
150 // ----- Draw the estimated parameterisations
151 fpPadBase->cd();
152 float xMin = fpHistBase->GetXaxis()->GetBinLowEdge(1);
153 float xMax = fpHistBase->GetXaxis()->GetBinUpEdge(fpHistBase->GetXaxis()->GetNbins());
154 TF1* fMin = new TF1("fMin", "[0]", xMin, xMax);
155 fMin->SetParameter(0, fvLoParams[0]);
156 fMin->SetLineColor(kOrange + 2);
157 fMin->Draw("LSAME");
158 TF1* fMax = new TF1("fMax", "[0]", xMin, xMax);
159 fMax->SetParameter(0, fvUpParams[0]);
160 fMax->SetLineColor(kOrange + 2);
161 fMax->Draw("LSAME");
162}
163
164// ---------------------------------------------------------------------------------------------------------------------
165//
166void WFExpression::SetEps(float eps)
167{
168 assert(eps > 0 && eps < 1.0);
169 fEps = eps;
170}
171
172// ---------------------------------------------------------------------------------------------------------------------
173//
175{
176 assert(nSlices > 0);
177 fNslices = nSlices;
178 fvUpSBoundaries.clear();
179 fvLoSBoundaries.clear();
180 fvSCenters.clear();
183 fvSCenters.resize(fNslices);
184}
185
186// ---------------------------------------------------------------------------------------------------------------------
187//
188void WFExpression::SetNbins(int nBinsDist, int nBinsParam)
189{
190 assert(nBinsDist > 0);
191 assert(nBinsParam > 0);
192 fNbinsDist = nBinsDist;
193 fNbinsParam = nBinsParam;
194}
195
196// ---------------------------------------------------------------------------------------------------------------------
197//
199{
200 assert(pad);
201 fpPadBase = pad;
202}
203
204// ---------------------------------------------------------------------------------------------------------------------
205//
207{
208 assert(pad);
209 fpPadSlices = pad;
210}
WFExpression()=delete
TMP: number of parameters.
TString fsExprDist
Expression along the distance axis.
void SetNslices(int nSlices)
Sets number of slices.
int fNbinsParam
Number of bins along the parameter axis.
void GetConstWindowParams()
Gets window parameterisations assuming there is no dependence from parameter.
int fNbinsDist
Number of bins along the distance axis.
void SetPadBase(TPad *pad)
Sets base pad pointer.
TH2F * fpHistBase
Base histogram (distance vs. parameter (x0 or y0))
std::vector< float > fvLoSBoundaries
Lower boundaries for diff. slices.
TTree * fpTree
Tree to be analyzed.
float fEps
A fraction of triplets, which can be lost.
std::tuple< float, float, float > ProcessSlice(int iBinMin, int iBinMax)
void SetEps(float eps)
Sets fraction of the events, which can be left out of boundaries.
TCut fCut
Cut used to draw and expression.
std::vector< float > fvUpSBoundaries
Upper boundaries for diff. slices.
std::vector< float > fvUpParams
Parameters for max.
int fNslices
Number of slices along the parameter axis.
TString fsName
Name of the expression (expr + cut, must be unique!!, TODO: make a check)
std::tuple< std::vector< float >, std::vector< float > > CalculateParameters()
TPad * fpPadSlices
Pointer to a pad for slices.
void SetNbins(int nBinsDist, int nBinsParam)
TString fsTitle
Title of expression.
void SetPadSlices(TPad *pad)
Sets slices pad pointer.
std::vector< float > fvLoParams
Parameters for min.
TString fsExprParam
Expression along the parameter axis.
TPad * fpPadBase
Pointer to a pad for base histogram.
std::vector< float > fvSCenters
Slice centers.