BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
compareA.C
Go to the documentation of this file.
1/*
2 BM@N alignment routine
3 BM@N experiment at NICA complex, JINR, 2025
4
5 Department: Math & Soft Group of HEP lab
6 Author: Igor Polev, polev@jinr.int
7
8 Aligmnment quality evaluation macro
9*/
10
11// headers below are for IDE syntax to work and they will be
12// turned off during execution in ROOT macro mode
13#ifndef __CLING__
14#include "BmnDataReader.h"
15#include "CbmStsTrack.h"
16#include "CbmVertex.h"
17
18#include <TBox.h>
19#include <TCanvas.h>
20#include <TGaxis.h>
21#include <TH1D.h>
22#include <TH2D.h>
23#include <TPad.h>
24#include <TPaveText.h>
25#include <TString.h>
26#include <TStyle.h>
27#include <TTreeReaderArray.h>
28#include <TTreeReaderValue.h>
29#include <iomanip>
30#include <iostream>
31#endif
32
33#define BMN_FILELIST_NA "/home/igor/bmnroot/align_new/cfg/run8/7466ev0p3.noa_rlerr.list"
34#define BMN_FILELIST_A "/home/igor/bmnroot/align_new/cfg/run8/7466ev0p3.manual.list"
35constexpr static const char*
36 // BMN_RESULT_PDF {nullptr};
37 BMN_RESULT_PDF{"/home/igor/bmnroot/align_new/reports/MMCP-2026/Proceedings/Polev_MMCP2026/QualityExample.pdf"};
38
39// canvas and pads configuration
40#define RES_X 1920 // 3440
41#define RES_Y 1440
42#define PADS_X 2 // 4
43#define PADS_Y 2
44
46{ // aligned or not
47 kNA = 0,
48 kAL = 1
49};
50constexpr Int_t kDEF{kAL}, kSEC{kNA};
51
64
65// historgam positioning on pads
66constexpr const Int_t histPos[eCmpHists::kCount][2]{
67 {1, 0}, // kTracksCount
68 {1, 1}, // kVertexCount
69 {0, 0}, // kTracksChi2perNDF
70 {0, 1}, // kVertexZ
71 {-1, -1}, // kVertexX - not drawn
72 {-1, -1}, // kVertexY - not drawn
73 {-1, -1}, // kVertexXYbefore - not drawn
74 {-1, -1} // kVertexXYafter - not drawn
75};
76
77/**
78 * @brief Judge alignment quality by comparing reconstruction before and after.
79 *
80 * Reads two datasets - the same events reconstructed with and without the
81 * alignment applied - and overlays what should improve: track count, chi2 per
82 * NDF, vertex position and its spread. The alignment is good when the aligned
83 * sample yields more tracks with a tighter chi2 and a sharper vertex.
84 *
85 * Configuration is compile-time, through the defines at the top of the file
86 * (BMN_FILELIST_NA and BMN_FILELIST_A name the two datasets).
87 *
88 * @param argc unused, kept so the macro can be invoked with ROOT's argument
89 * passing convention
90 * @param argv unused, see argc
91 *
92 * @return 0 on success, non-zero if either dataset could not be read
93 */
94int compareA(int argc = 0, char** argv = nullptr)
95{
96 // force x10^3 scale on histogram vertical axes
98
99 // text sizes for the paper figure: axes x2, titles x1.5 of ROOT defaults;
100 // must be set before the histograms are created, they copy gStyle then
101 gStyle->SetLabelSize(0.055, "XY");
102 gStyle->SetTitleSize(0.055, "XY");
103 gStyle->SetTitleH(0.07);
104 gStyle->SetTitleW(0.98); // default width is capped at 0.7, too narrow for long titles
105 gStyle->SetPadLeftMargin(0.19);
106 gStyle->SetPadBottomMargin(0.16);
107 gStyle->SetPadTopMargin(0.14); // room for the axis exponent below the title
108
109 // prepare canvas and pads
110 TCanvas* pCnv = new TCanvas( // RAM is not released in macro mode
111 "canvas", "BM@N alignment", RES_X, RES_Y);
112 pCnv->SetCanvasSize(RES_X - 20, RES_Y - 96);
113 constexpr const Double_t padW{1.0 / PADS_X}, padH{1.0 / PADS_Y};
114 TPad* pPads[PADS_Y][PADS_X];
115 for (UInt_t i = 0; i < PADS_Y; i++) {
116 for (UInt_t j = 0; j < PADS_X; j++) {
117 pPads[i][j] = new TPad(Form("pad%d%d", i + 1, j + 1), Form("pad%d%d", i + 1, j + 1), padW * j, padH * i,
118 padW * (j + 1), padH * (i + 1));
119 pPads[i][j]->Draw();
120 }
121 }
122
123 // prepare ROOT file readers and histograms
124 TH1* pHist[2][eCmpHists::kCount];
125 BmnDataReader* pData[2];
127 TTreeReaderValue<CbmVertex>* pVertex[2];
128 for (Int_t al = kNA; al <= kAL; al++) {
129 pData[al] = new BmnDataReader(al ? BMN_FILELIST_A : BMN_FILELIST_NA);
130 pTracks[al] = new TTreeReaderArray<CbmStsTrack>(pData[al]->Reader(), "StsVector");
131 pVertex[al] = new TTreeReaderValue<CbmVertex>(pData[al]->Reader(), "MpdVertex.");
132 pHist[al][kVertexCount] = new TH1D(
133 al ? "VC-AL" : "VC-NA", "Event count with reconstructed primary vertex (tracks > 1);;Events", 4, -1.0, 2.0);
134 pHist[al][kTracksChi2perNDF] =
135 new TH1D(al ? "TChi-AL" : "TChi-NA",
136 "Track distribution by #chi^{2} / NDF (cut = 5.0, red: aligned);#chi^{2} / NDF value;Tracks", 100,
137 0.0, 5.0);
138 pHist[al][kVertexX] = new TH1D(al ? "VX-AL" : "VX-NA", "Vertex X distribution;X, cm;Vertices", 100, -6.0, 6.0);
139 pHist[al][kVertexY] = new TH1D(al ? "VY-AL" : "VY-NA", "Vertex Y distribution;Y, cm;Vertices", 100, -6.0, 6.0);
140 pHist[al][kVertexZ] =
141 new TH1D(al ? "VZ-AL" : "VZ-NA",
142 "Primary vertex distribution along beam axis (red: aligned);Z, cm;Vertices", 100, -2.0, 2.0);
143 }
144 pHist[kDEF][kVertexXYbefore] =
145 new TH2D("VXY-NA", "Non-aligned vertex XY;X, cm;Y, cm", 100, -2.0, 2.0, 100, -2.0, 2.0);
146 pHist[kDEF][kVertexXYafter] = new TH2D("VXY-AL", "Aligned vertex XY;X, cm;Y, cm", 100, -2.0, 2.0, 100, -2.0, 2.0);
147 for (Int_t al = kNA; al <= kAL; al++) {
148 pHist[al][kTracksCount] =
149 new TH1D(al ? "TC-AL" : "TC-NA", "Tracks reconstructed (hits > 1);;Tracks", 4, -1.0, 2.0);
150 }
151 pHist[kSEC][kVertexXYbefore] = nullptr;
152 pHist[kSEC][kVertexXYafter] = nullptr;
153
154 // fill histograms
155 TH1D::StatOverflows(kFALSE); // need to ignore overflows, otherwise
156 // vertex Z histogram may show increase of StdDev
157 Int_t vertexTotal[2]{0, 0}, tracksTotal[2]{0, 0};
158 for (Int_t al = kNA; al <= kAL; al++) {
159 Double_t dCounter{static_cast<Double_t>(al)};
160 TH2D* pHistVertexXY = (TH2D*)(al == kAL ? pHist[kDEF][kVertexXYafter] : pHist[kDEF][kVertexXYbefore]);
161 while (pData[al]->Reader().Next()) {
162 // vertex data
163 CbmVertex& vertex = **pVertex[al];
164 if (vertex.GetNTracks() < 2) // ignore vertices with less than 2 tracks
165 continue;
166 vertexTotal[al]++;
167 pHist[al][kVertexCount]->Fill(dCounter);
168 pHist[al][kVertexX]->Fill(vertex.GetX());
169 pHist[al][kVertexY]->Fill(vertex.GetY());
170 pHist[al][kVertexZ]->Fill(vertex.GetZ());
171 pHistVertexXY->Fill(vertex.GetX(), vertex.GetY());
172 // tracks data
173 for (CbmStsTrack& track : *pTracks[al]) {
174 if (track.GetNStsHits() < 2) // ignore tracks with less than 2 hits
175 continue;
176 tracksTotal[al]++;
177 pHist[al][kTracksCount]->Fill(dCounter);
178 pHist[al][kTracksChi2perNDF]->Fill(track.GetChi2() / track.GetNDF());
179 }
180 }
181 }
182
183 // rescale chi2/hit and vertex profiles histograms
184 Double_t tracksIncrease{static_cast<Double_t>(tracksTotal[kAL]) / tracksTotal[kNA]},
185 vertexIncrease{static_cast<Double_t>(vertexTotal[kAL]) / vertexTotal[kNA]};
186 pHist[kNA][kVertexX]->Scale(vertexIncrease);
187 pHist[kNA][kVertexY]->Scale(vertexIncrease);
188 pHist[kNA][kVertexZ]->Scale(vertexIncrease);
189 pHist[kNA][kTracksChi2perNDF]->Scale(tracksIncrease);
190
191 // summarize results
192 Double_t rTracksIncrease{100.0 * tracksIncrease - 100.0}, rVertexIncrease{100.0 * vertexIncrease - 100.0},
193 rTracksChi2perNDFMeanDecrease{
194 100.0 * pHist[kAL][kTracksChi2perNDF]->GetMean() / pHist[kNA][kTracksChi2perNDF]->GetMean() - 100.0},
195 rVertexXStdDevDecrease{100.0 * pHist[kAL][kVertexX]->GetStdDev() / pHist[kNA][kVertexY]->GetStdDev() - 100.0},
196 rVertexYStdDevDecrease{100.0 * pHist[kAL][kVertexY]->GetStdDev() / pHist[kNA][kVertexY]->GetStdDev() - 100.0},
197 rVertexZStdDevDecrease{100.0 * pHist[kAL][kVertexZ]->GetStdDev() / pHist[kNA][kVertexZ]->GetStdDev() - 100.0};
198
199 // report to stdout
200 using namespace std;
201 cout << fixed << setprecision(1);
202 cout << "Tracks count increase ......................... " << rTracksIncrease << "%\n";
203 cout << "Vertices count increase........................ " << rVertexIncrease << "%\n";
204 cout << "Mean tracks Chi2 per NDF decrease ............. " << rTracksChi2perNDFMeanDecrease << "%\n";
205 cout << "Vertex X distribution StdDev decrease ......... " << rVertexXStdDevDecrease << "%\n";
206 cout << "Vertex Y distribution StdDev decrease ......... " << rVertexYStdDevDecrease << "%\n";
207 cout << "Vertex Z distribution StdDev decrease ......... " << rVertexZStdDevDecrease << "%\n";
208 cout << "Vertex Z distribution StdDev before / after ... " << fixed << setprecision(3)
209 << pHist[kNA][kVertexZ]->GetStdDev() << " / " << pHist[kAL][kVertexZ]->GetStdDev() << "\n";
210
211 // report to GUI
212 TH1* hst[2];
213 TPaveText* pave;
214 TString hstOpt[2];
215 EColor colorNA{kGray};
216 Double_t value;
217 for (Int_t hstEN = 0; hstEN < eCmpHists::kCount; hstEN++) {
218 // skip deactivated histograms
219 if (histPos[hstEN][0] == -1 || histPos[hstEN][1] == -1)
220 continue;
221
222 pPads[histPos[hstEN][0]][histPos[hstEN][1]]->cd();
223 hst[kNA] = pHist[kNA][hstEN];
224 hst[kAL] = pHist[kAL][hstEN];
225 // default histogram configuration
226 hst[kDEF]->SetLineColor(kRed);
227 hst[kDEF]->SetLineWidth(2);
228 hst[kDEF]->SetStats(0);
229 if (hst[kSEC])
230 hst[kSEC]->SetStats(0);
231 hstOpt[kNA] = "HIST";
232 hstOpt[kAL] = "C SAME";
233 // every TPaveText object is left in heap due to ROOT macro drawing specifics
234 pave = new TPaveText(0.6, 0.85, 0.95, 0.92, "BR NDC");
235 switch (hstEN) {
236 case kTracksCount:
237 case kVertexCount:
238 // fancy colors rejected by teamlead as non-scientific ))
239 // colorNA = hstEN == kTracksCount ? kCyan : kTeal;
240 for (Int_t al = kNA; al <= kAL; al++) {
241 hst[al]->GetXaxis()->SetBinLabel(1, "");
242 hst[al]->GetXaxis()->SetBinLabel(kNA + 2, "Not aligned");
243 hst[al]->GetXaxis()->SetBinLabel(kAL + 2, "Aligned");
244 hst[al]->GetXaxis()->SetBinLabel(4, "");
245 hst[al]->GetXaxis()->SetLabelSize(0.06); // largest size that fits the bin width
246 hst[al]->SetBarWidth(0.8);
247 hst[al]->SetBarOffset(0.1);
248 }
249 // "Aligned" bar: white fill, red outline drawn below; kWhite (0) comes
250 // out black in PDF, and an empty fill style outlines the empty bins too
251 hst[kAL]->SetFillColor(10);
252 hstOpt[kNA] = "BAR";
253 hstOpt[kAL] = "BAR SAME";
254 pave->AddText(hstEN == kTracksCount ? Form("Tracks increase: %+5.1f%%", rTracksIncrease)
255 : Form("Events increase: %+5.1f%%", rVertexIncrease));
256 break;
258 // colorNA = kCyan;
259 pave->AddText(Form("Mean decrease: %+5.1f%%", rTracksChi2perNDFMeanDecrease));
260 break;
261 case kVertexX:
262 case kVertexY:
263 case kVertexZ:
264 // colorNA = kTeal;
265 if (hstEN == kVertexX)
266 value = rVertexXStdDevDecrease;
267 else if (hstEN == kVertexY)
268 value = rVertexYStdDevDecrease;
269 else if (hstEN == kVertexZ)
270 value = rVertexZStdDevDecrease;
271 pave->AddText(Form("StdDev decrease: %+5.1f%%", value));
272 break;
273 case kVertexXYbefore:
274 case kVertexXYafter:
275 hstOpt[kDEF] = "CONTZ";
276 delete pave, pave = nullptr;
277 break;
278 default:
279 hst[kNA] = nullptr;
280 hst[kAL] = nullptr;
281 delete pave, pave = nullptr;
282 }
283 if (hst[kNA]) {
284 hst[kNA]->SetFillColor(colorNA);
285 hst[kNA]->SetLineColor(colorNA);
286 if (hstOpt[kNA] != "CONTZ")
287 hst[kNA]->GetYaxis()->SetRangeUser(0.0, hst[kAL]->GetMaximum() * 1.1);
288 hst[kNA]->Draw(hstOpt[kNA]);
289 }
290 if (hst[kAL])
291 hst[kAL]->Draw(hstOpt[kAL]);
292 if (pave)
293 pave->Draw();
294 // Overlay red outline on the "Aligned" bar: BAR option ignores line color
295 if (hstEN == kTracksCount || hstEN == kVertexCount) {
296 constexpr Int_t alBin{kAL + 2};
297 Double_t binLow = hst[kAL]->GetXaxis()->GetBinLowEdge(alBin);
298 Double_t binWidth = hst[kAL]->GetXaxis()->GetBinWidth(alBin);
299 TBox* outline = new TBox( // left in heap due to ROOT macro drawing specifics
300 binLow + 0.1 * binWidth, 0.0, binLow + 0.9 * binWidth, hst[kAL]->GetBinContent(alBin));
301 outline->SetFillStyle(0);
302 outline->SetLineColor(kRed);
303 outline->SetLineWidth(2);
304 outline->Draw();
305 gPad->RedrawAxis(); // the white bar fill hides the frame lines
306 }
307 }
308
309 // report to PDF
310 if (BMN_RESULT_PDF)
311 pCnv->SaveAs(BMN_RESULT_PDF);
312
313 // partial cleanup
314 for (UInt_t i = 0; i < 2; i++) {
315 delete pData[i];
316 delete pTracks[i];
317 delete pVertex[i];
318 }
319
320 return 0;
321}
int i
Definition P4_F32vec4.h:22
int Int_t
unsigned int UInt_t
constexpr Bool_t kFALSE
double Double_t
EColor
kGray
kRed
char * Form(const char *fmt,...)
R__EXTERN TStyle * gStyle
#define gPad
CbmVertex.h.
Definition CbmVertex.h:21
virtual void SetLabelSize(Float_t size=0.04)
virtual void SetFillColor(Color_t fcolor)
virtual void SetFillStyle(Style_t fstyle)
virtual void SetLineWidth(Width_t lwidth)
virtual void SetLineColor(Color_t lcolor)
virtual void SetBinLabel(Int_t bin, const char *label)
virtual Double_t GetBinLowEdge(Int_t bin) const
virtual void SetRangeUser(Double_t ufirst, Double_t ulast)
virtual Double_t GetBinWidth(Int_t bin) const
void Draw(Option_t *option="") override
void SetCanvasSize(UInt_t ww, UInt_t wh) override
static void SetMaxDigits(Int_t maxd=5)
virtual void SetBarOffset(Float_t offset=0.25)
static void StatOverflows(Bool_t flag=kTRUE)
virtual Double_t GetStdDev(Int_t axis=1) const
virtual Double_t GetMean(Int_t axis=1) const
TAxis * GetXaxis()
TAxis * GetYaxis()
void Draw(Option_t *option="") override
virtual Int_t Fill(const char *name, Double_t w)
virtual void Scale(Double_t c1=1, Option_t *option="")
virtual void SetBarWidth(Float_t width=0.5)
virtual void SetStats(Bool_t stats=kTRUE)
virtual Int_t Fill(const char *namex, const char *namey, Double_t w)
void SaveAs(const char *filename="", Option_t *option="") const override
TVirtualPad * cd(Int_t subpadnumber=0) override
void Draw(Option_t *option="") override
virtual TText * AddText(const char *label)
void Draw(Option_t *option="") override
#define PADS_Y
Definition compareA.C:43
#define RES_Y
Definition compareA.C:41
#define PADS_X
Definition compareA.C:42
eAligned
Definition compareA.C:46
@ kNA
Definition compareA.C:47
@ kAL
Definition compareA.C:48
constexpr Int_t kDEF
Definition compareA.C:50
int compareA(int argc=0, char **argv=nullptr)
Judge alignment quality by comparing reconstruction before and after.
Definition compareA.C:94
eCmpHists
Definition compareA.C:53
@ kVertexX
Definition compareA.C:58
@ kVertexXYafter
Definition compareA.C:61
@ kVertexY
Definition compareA.C:59
@ kVertexZ
Definition compareA.C:57
@ kCount
Definition compareA.C:62
@ kTracksCount
Definition compareA.C:54
@ kVertexCount
Definition compareA.C:55
@ kTracksChi2perNDF
Definition compareA.C:56
@ kVertexXYbefore
Definition compareA.C:60
static constexpr const char * BMN_RESULT_PDF
Definition compareA.C:37
constexpr Int_t kSEC
Definition compareA.C:50
#define RES_X
Definition compareA.C:40
#define BMN_FILELIST_A
Definition compareA.C:34
#define BMN_FILELIST_NA
Definition compareA.C:33
constexpr const Int_t histPos[eCmpHists::kCount][2]
Definition compareA.C:66