CbmRoot
Loading...
Searching...
No Matches
CaToolsWindowFinder.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
9
10#include "CaToolsWindowFinder.h"
11
12#include "CaConfigReader.h"
13#include "CaDefs.h"
14#include "CaSearchWindow.h"
15#include "CaToolsWFExpression.h"
16#include "Logger.h"
17#include "TCanvas.h"
18#include "TChain.h"
19#include "TPad.h"
20#include "TPaveText.h"
21#include "TString.h"
22
23#include <boost/archive/text_oarchive.hpp>
24
25#include <fstream>
26#include <sstream>
27
28#include <yaml-cpp/yaml.h>
29
30using namespace cbm::ca::tools;
31using namespace cbm::algo::ca::constants; // for colored logs
32
36
38
39// ---------------------------------------------------------------------------------------------------------------------
40//
41WindowFinder::WindowFinder() : fpMcTripletsTree(new TChain(kTreeName))
42{
43 LOG(info) << clrs::GNb << "Call" << clrs::CLb << "WindowFinder constructor\n" << clrs::CL;
44}
45
46// ---------------------------------------------------------------------------------------------------------------------
47//
48void WindowFinder::AddInputFile(const char* filename)
49{
50 assert(fpMcTripletsTree);
51 auto addingRes = fpMcTripletsTree->AddFile(filename);
52 if (addingRes != 1) {
53 LOG(error) << "WindowFinder::AddInputFile: File \"" << filename << "\" cannot be added to the MC triplets chain, "
54 << "some errors occurred\n";
55 }
56}
57
58// ---------------------------------------------------------------------------------------------------------------------
59//
61{
62
63 // Get a cut
64 TCut cut = GetTrackSelectionCut(iStation, caIter);
65
66 if (TString(fExtraCut.GetTitle()).Length()) {
67 cut = cut && fExtraCut;
68 }
69
70 // Set a unique to a canvas using the cut expression
71 TString sUniqueName = cut.GetTitle();
72 sUniqueName.ReplaceAll(" ", "");
73
74 // Prepare a canvas and pads to draw the histograms
75 TString canvName = TString("canv_") + sUniqueName;
76 TCanvas* canv = new TCanvas(canvName, canvName, 3000, 2000);
77 fvpCanvases.push_back(canv); // Register canvas
78
79 // Pads for base distributions
80 TPad* padBase[EExpr::kEND];
81 padBase[EExpr::kDxVsX0] = new TPad("padBase_dx_x0", "padBase_dx_x0", 0.00, 0.50, 0.25, 0.80);
82 padBase[EExpr::kDxVsY0] = new TPad("padBase_dx_y0", "padBase_dx_y0", 0.25, 0.50, 0.50, 0.80);
83 padBase[EExpr::kDyVsX0] = new TPad("padBase_dy_x0", "padBase_dy_x0", 0.50, 0.50, 0.75, 0.80);
84 padBase[EExpr::kDyVsY0] = new TPad("padBase_dy_y0", "padBase_dy_y0", 0.75, 0.50, 1.00, 0.80);
85
86 // Pads for slices
87 TPad* padSlices[EExpr::kEND];
88 padSlices[EExpr::kDxVsX0] = new TPad("padSlices_dx_x0", "padSlices_dx_x0", 0.00, 0.00, 0.25, 0.50);
89 padSlices[EExpr::kDxVsY0] = new TPad("padSlices_dx_y0", "padSlices_dx_y0", 0.25, 0.00, 0.50, 0.50);
90 padSlices[EExpr::kDyVsX0] = new TPad("padSlices_dy_x0", "padSlices_dy_x0", 0.50, 0.00, 0.75, 0.50);
91 padSlices[EExpr::kDyVsY0] = new TPad("padSlices_dy_y0", "padSlices_dy_y0", 0.75, 0.00, 1.00, 0.50);
92
93 for (int iExpr = 0; iExpr < EExpr::kEND; ++iExpr) {
94 padSlices[iExpr]->Draw();
95 padBase[iExpr]->Draw();
96 }
97
98 // A pad for text
99 TPad* padDescr = new TPad("padDescr", "padDescr", 0.00, 0.80, 1.00, 1.00);
100 padDescr->Draw();
101
102 PrintCaseInformation(padDescr, iStation, caIter);
103
104
105 // Process expressions
107 exprDxVsX0.SetEps(fEps);
108 exprDxVsX0.SetNbins(fNbinsY, fNbinsX);
109 exprDxVsX0.SetNslices(fNslices);
110 exprDxVsX0.SetCut(cut);
111 exprDxVsX0.SetTitle("dx vs. x0;x0 [cm];dx [cm]");
112 exprDxVsX0.SetPadBase(padBase[EExpr::kDxVsX0]);
113 exprDxVsX0.SetPadSlices(padSlices[EExpr::kDxVsX0]);
114
116 exprDxVsY0.SetEps(fEps);
117 exprDxVsY0.SetNbins(fNbinsY, fNbinsX);
118 exprDxVsY0.SetNslices(fNslices);
119 exprDxVsY0.SetCut(cut);
120 exprDxVsY0.SetTitle("dx vs. y0;y0 [cm];dx [cm]");
121 exprDxVsY0.SetPadBase(padBase[EExpr::kDxVsY0]);
122 exprDxVsY0.SetPadSlices(padSlices[EExpr::kDxVsY0]);
123
125 exprDyVsX0.SetEps(fEps);
126 exprDyVsX0.SetNbins(fNbinsY, fNbinsX);
127 exprDyVsX0.SetNslices(fNslices);
128 exprDyVsX0.SetCut(cut);
129 exprDyVsX0.SetTitle("dy vs. x0;x0 [cm];dy [cm]");
130 exprDyVsX0.SetPadBase(padBase[EExpr::kDyVsX0]);
131 exprDyVsX0.SetPadSlices(padSlices[EExpr::kDyVsX0]);
132
134 exprDyVsY0.SetEps(fEps);
135 exprDyVsY0.SetNbins(fNbinsY, fNbinsX);
136 exprDyVsY0.SetNslices(fNslices);
137 exprDyVsY0.SetCut(cut);
138 exprDyVsY0.SetTitle("dy vs. y0;y0 [cm];dy [cm]");
139 exprDyVsY0.SetPadBase(padBase[EExpr::kDyVsY0]);
140 exprDyVsY0.SetPadSlices(padSlices[EExpr::kDyVsY0]);
141
142 auto [parsUpperDxVsX0, parsLowerDxVsX0] = exprDxVsX0.CalculateParameters();
143 auto [parsUpperDxVsY0, parsLowerDxVsY0] = exprDxVsY0.CalculateParameters();
144 auto [parsUpperDyVsX0, parsLowerDyVsX0] = exprDyVsX0.CalculateParameters();
145 auto [parsUpperDyVsY0, parsLowerDyVsY0] = exprDyVsY0.CalculateParameters();
146
147 // Prepare a searching window
148 int iCaIter = &caIter - &fvCaIters[0];
149 ca::SearchWindow sw(iStation, iCaIter);
150 sw.SetTag(caIter.GetName().c_str());
151 assert(fNparams == 1); // TMP: At the moment only constant windows available
152 for (int iP = 0; iP < fNparams; ++iP) {
153 sw.SetParamDxMaxVsX0(iP, parsUpperDxVsX0[iP]);
154 sw.SetParamDxMinVsX0(iP, parsLowerDxVsX0[iP]);
155 sw.SetParamDxMaxVsY0(iP, parsUpperDxVsY0[iP]);
156 sw.SetParamDxMinVsY0(iP, parsLowerDxVsY0[iP]);
157 sw.SetParamDyMaxVsX0(iP, parsUpperDyVsX0[iP]);
158 sw.SetParamDyMinVsX0(iP, parsLowerDyVsX0[iP]);
159 sw.SetParamDyMaxVsY0(iP, parsUpperDyVsY0[iP]);
160 sw.SetParamDyMinVsY0(iP, parsLowerDyVsY0[iP]);
161 }
162 return sw;
163}
164
165// ---------------------------------------------------------------------------------------------------------------------
166//
167void WindowFinder::DumpCanvasesToPdf(const char* filename) const
168{
169 for (int iC = 0; iC < (int) fvpCanvases.size(); ++iC) {
170 TString sCanvName = Form("%s_%d.pdf", filename, iC);
171 fvpCanvases[iC]->SaveAs(sCanvName);
172 }
173}
174
175// ---------------------------------------------------------------------------------------------------------------------
176//
177const char* WindowFinder::GetDistExpr(EExpr expr) const
178{
179 assert(fTargetPos[0] == 0);
180 assert(fTargetPos[1] == 0);
181 switch (expr) {
182 case EExpr::kDxVsX0:
183 case EExpr::kDxVsY0: return Form("x1 - x0 * (z1 - (%f)) / (z0 - (%f))", fTargetPos[2], fTargetPos[2]);
184 case EExpr::kDyVsX0:
185 case EExpr::kDyVsY0: return Form("y1 - y0 * (z1 - (%f)) / (z0 - (%f))", fTargetPos[2], fTargetPos[2]);
186 default: return ""; // TODO: throw an exception
187 }
188}
189
190// ---------------------------------------------------------------------------------------------------------------------
191// TODO: SZh 10.11.2022: Should it be a static function??
192TCut WindowFinder::GetTrackSelectionCut(int iStation, const ca::Iteration& caIter) const
193{
194 TCut cut;
195
196 // Select station
197 // NOTE: iStation stands for global index of an active tracking station for which a searching window is estimated.
198 // In the MC triplets tree the station index ("s") is stored for the left station of the triplet. Thus, for
199 // doublets the station is to be selected as "iStation - 1" (search window is estimated on the middle station),
200 // and as "iStation - 2" for the triplets (search window is estimated on the right station).
201 cut = cut && Form("s==%d", iStation - 1);
202
203 // Select q/p
204 cut = cut && Form("abs(q/p)<%f", caIter.GetMaxQp());
205
206 // Select origin (primary/secondary)
207 cut = cut && (caIter.GetPrimaryFlag() ? "processId==0" : "processId!=0");
208
209 // TODO: SZh 10.11.2022: Try to apply other cuts (electron, slope etc.)
210 return cut;
211}
212
213// ---------------------------------------------------------------------------------------------------------------------
214//
215void WindowFinder::PrintCaseInformation(TPad* pPad, int iStation, const ca::Iteration& caIter) const
216{
217 pPad->cd();
218
219 TPaveText* textL = new TPaveText(0.01, 0.01, 0.49, 0.99);
220 textL->SetBorderSize(1);
221 textL->SetTextFont(42);
222 textL->SetFillColor(kWhite);
223 textL->SetTextAlign(11);
224 textL->SetTextSize(0.1);
225 textL->AddText(Form("Global index of active station: %d", iStation));
226 textL->AddText(Form("Track finder iteration: %s", caIter.GetName().c_str()));
227 textL->AddText(Form(" - |q/p| < %.2f e(Gev/c)^{-1}", caIter.GetMaxQp()));
228 textL->AddText(Form(" - primary: %s", caIter.GetPrimaryFlag() ? "yes" : "no"));
229 if (TString(fExtraCut.GetTitle()).Length()) {
230 textL->AddText(Form("Optional cut: %s", fExtraCut.GetTitle()));
231 }
232 textL->Draw();
233
234 TPaveText* textR = new TPaveText(0.51, 0.01, 0.99, 0.99);
235 textR->SetBorderSize(1);
236 textR->SetTextFont(42);
237 textR->SetTextAlign(11);
238 textR->SetTextSize(0.1);
239 textR->SetFillColor(kWhite);
240 textR->AddText("Distance expressions:");
241 textR->AddText(Form(" dx = %s", GetDistExpr(EExpr::kDxVsY0)));
242 textR->AddText(Form(" dy = %s", GetDistExpr(EExpr::kDyVsY0)));
243
244 textR->Draw();
245 //pPad->Draw();
246}
247
248// ---------------------------------------------------------------------------------------------------------------------
249//
250void WindowFinder::Process(Option_t* /*opt*/)
251{
252 std::ofstream ofs(fsOutputName);
253 if (!ofs) {
254 LOG(error) << "WindowFinder: failed opening file \"" << fsOutputName << " for writing search windows\"";
255 return;
256 }
257
258 boost::archive::text_oarchive oa(ofs);
259 oa << fNparams;
260 oa << int(fvStationIndexes.size() * fvCaIters.size());
261 for (auto iStation : fvStationIndexes) {
262 for (const auto& iter : fvCaIters) {
263 LOG(info) << "WindowFinder: processing global active station index " << clrs::CLb << iStation << clrs::CL
264 << " and track finder iteration " << clrs::CLb << iter.GetName() << clrs::CL;
265 auto sw = CreateSW(iStation, iter);
266 oa << sw;
267 }
268 }
269}
270
271// ---------------------------------------------------------------------------------------------------------------------
272//
274{
275 ca::ConfigReader reader(/*pInitManager = */ nullptr, 0);
276 reader.SetMainConfigPath(filename);
277
278 // Get iterations
280
281 // Print iterations:
282 for (const auto& iter : fvCaIters) {
283 LOG(info) << iter.ToString() << '\n';
284 }
285}
286
287
288// ****************************
289// ** Setters implementation **
290// ****************************
291
292// ---------------------------------------------------------------------------------------------------------------------
293//
294void WindowFinder::SetBinning(int nBinsX, int nBinsY)
295{
296 assert(nBinsX > 0);
297 assert(nBinsY > 0);
298 fNbinsX = nBinsX;
299 fNbinsY = nBinsY;
300}
301
302// ---------------------------------------------------------------------------------------------------------------------
303//
305{
306 assert(eps > 0. && eps <= 1.);
307 fEps = eps;
308}
309
310// ---------------------------------------------------------------------------------------------------------------------
311//
313{
314 assert(nSlices > 0);
315 fNslices = nSlices;
316}
317
318// ---------------------------------------------------------------------------------------------------------------------
319//
320void WindowFinder::SetStationIndexes(const std::vector<int>& vStationIndexes)
321{
322 fvStationIndexes.clear();
323 for (auto iSt : vStationIndexes) {
324 if (iSt < 1) {
325 LOG(warn) << "WindowFinder::SetStationIndexes: attempt to estimate searching window for station with index "
326 << iSt << " < 1. This index will be omitted";
327 continue;
328 }
329 fvStationIndexes.push_back(iSt);
330 }
331}
332
333// ---------------------------------------------------------------------------------------------------------------------
334//
335void WindowFinder::SetTarget(double x, double y, double z)
336{
337 fTargetPos[0] = x;
338 fTargetPos[1] = y;
339 fTargetPos[2] = z;
340}
Configuration parameter file reader for the CA tracking algorithm (header)
Compile-time constants definition for the CA tracking algorithm.
ClassImp(cbm::ca::tools::WindowFinder)
Framework for CA tracking hit-finder window estimation from MC (header)
A reader for the CA parameters from the YAML configuration files.
std::vector< Iteration > ReadCAIterationVector()
Reads CA track finder iterations from YAML node.
void SetMainConfigPath(const std::string &path)
Sets main config file.
A set of parameters for the CA Track finder iteration.
float GetMaxQp() const
Gets max considered q/p for tracks.
Definition CaIteration.h:79
bool GetPrimaryFlag() const
Checks flag: true - only primary tracks are searched, false - [all or only secondary?...
const std::string & GetName() const
Gets the name of the iteration.
Definition CaIteration.h:94
Class L1SearchWindow defines a parameterisation of hits search window for CA tracking algorithm TODO:...
void SetParamDxMinVsX0(int id, float val)
Sets parameters for dx_min(x0)
void SetParamDyMaxVsX0(int id, float val)
Sets parameters for dy_max(x0)
void SetParamDxMaxVsY0(int id, float val)
Sets parameters for dx_max(y0)
void SetParamDyMinVsX0(int id, float val)
Sets parameters for dy_min(x0)
void SetParamDyMaxVsY0(int id, float val)
Sets parameters for dy_max(y0)
void SetParamDxMinVsY0(int id, float val)
Sets parameters for dx_min(y0)
void SetParamDyMinVsY0(int id, float val)
Sets parameters for dy_min(y0)
void SetParamDxMaxVsX0(int id, float val)
Sets parameters for dx_max(x0)
void SetTag(const char *name)
Sets tag.
void SetNslices(int nSlices)
Sets number of slices.
void SetCut(const TCut &cut)
Sets cut, including information on the station and track finder iteration.
void SetPadBase(TPad *pad)
Sets base pad pointer.
void SetEps(float eps)
Sets fraction of the events, which can be left out of boundaries.
std::tuple< std::vector< float >, std::vector< float > > CalculateParameters()
void SetNbins(int nBinsDist, int nBinsParam)
void SetPadSlices(TPad *pad)
Sets slices pad pointer.
void SetTitle(const char *title)
Sets title of the histograms.
TODO: ... write an instruction ...
void AddInputFile(const char *filename)
int fNbinsX
Number of bins for the X axis of the distribution.
int fNslices
Number of slices along the X axis.
int fNbinsY
Number of bins for the Y axis of the distribution.
float fEps
Fraction of triplets (doublets), which can be omitted.
void SetTarget(double x, double y, double z)
void PrintCaseInformation(TPad *pPad, int iStation, const ca::Iteration &caIter) const
void DumpCanvasesToPdf(const char *filename="WFLog") const
Saves canvases to a set of canvases to pdf.
void ReadTrackingIterationsFromYAML(const char *filename)
Reads the iterations from YAML config.
std::vector< int > fvStationIndexes
Global indexes of active stations to find the windows.
ca::SearchWindow CreateSW(int iStation, const ca::Iteration &caIter)
Creates a search window for a selected station and iteration.
std::array< double, 3 > fTargetPos
Target position {x, y, z} [cm].
void SetBinning(int nBinsX, int nBinsY)
std::string fsOutputName
Name for output file with estimated search windows.
const char * GetDistExpr(EExpr expr) const
Returns expression for dx or dy to be drawn in a tree.
TChain * fpMcTripletsTree
Chain of trees containing MC triplets, generated in CbmL1 Performance.
void SetStationIndexes(const std::vector< int > &vStationIndexes)
TCut fExtraCut
Optional cut on the triplets/doublets.
TCut GetTrackSelectionCut(int iStation, const ca::Iteration &caIter) const
int fNparams
number of parameters of the searching window
std::vector< ca::Iteration > fvCaIters
Tracking iterations.
std::vector< TCanvas * > fvpCanvases
void SetEpsilon(float eps)
Sets a fraction of triplets (doublets), which can be omitted by the window.
constexpr char GNb[]
bold green
Definition CaDefs.h:155
constexpr char CLb[]
clear bold
Definition CaDefs.h:133
constexpr char CL[]
clear
Definition CaDefs.h:132
EExpr
Enumeration to handle processed expressions.