CbmRoot
Loading...
Searching...
No Matches
CbmFlesCanvasTools.cxx
Go to the documentation of this file.
1/* Copyright (C) 2019 Facility for Antiproton and Ion Research in Europe, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Pierre-Alain Loizeau [committer] */
4
6
7#include "TCanvas.h"
8#include "TH1.h"
9
10#include <iostream>
11
12/**********************************************************************/
14 : fsName("")
15 , fsTitle("")
16 , fuNbPads(0)
17 , fuNbPadsX(0)
18 , fuNbPadsY(0)
19 , fvbGridx()
20 , fvbGridy()
21 , fvbLogx()
22 , fvbLogy()
23 , fvbLogz()
24 , fvvsObjName()
25 , fvvsOptions()
26{
27}
28CanvasConfig::CanvasConfig(std::string sName, std::string sTitle, uint32_t uNbPadsX, uint32_t uNbPadsY)
29 : fsName(sName)
30 , fsTitle(sTitle)
31 , fuNbPads(uNbPadsX * uNbPadsY)
32 , fuNbPadsX(uNbPadsX)
33 , fuNbPadsY(uNbPadsY)
34 , fvbGridx(fuNbPads, false)
35 , fvbGridy(fuNbPads, false)
36 , fvbLogx(fuNbPads, false)
37 , fvbLogy(fuNbPads, false)
38 , fvbLogz(fuNbPads, false)
39 , fvvsObjName(fuNbPads)
40 , fvvsOptions(fuNbPads)
41{
42}
43
45{
47 fvbGridx.clear();
48 fvbGridy.clear();
49 fvbLogx.clear();
50 fvbLogy.clear();
51 fvbLogz.clear();
52 for (uint32_t uObj = 0; uObj < fvvsObjName.size(); ++uObj) {
53 fvvsObjName[uObj].clear();
54 fvvsOptions[uObj].clear();
55 } // for( uint32_t uObj = 0; uObj < fvvsObjName.size(); ++uObj )
56 fvvsObjName.clear();
57 fvvsOptions.clear();
58}
59
61bool CanvasConfig::GetGridx(uint32_t uPadIdx) const
62{
64 if (uPadIdx < fuNbPads) { return fvbGridx[uPadIdx]; } // if( uPadIdx < fuNbPads )
65 else {
66 std::cerr << "CanvasConfig::GetGridx => Pad index out of bounds, returning false! " << uPadIdx << " VS " << fuNbPads
67 << std::endl;
68 return false;
69 } // else of if( uPadIdx < fuNbPads )
70}
71bool CanvasConfig::GetGridy(uint32_t uPadIdx) const
72{
74 if (uPadIdx < fuNbPads) { return fvbGridy[uPadIdx]; } // if( uPadIdx < fuNbPads )
75 else {
76 std::cerr << "CanvasConfig::GetGridy => Pad index out of bounds, returning false! " << uPadIdx << " VS " << fuNbPads
77 << std::endl;
78 return false;
79 } // else of if( uPadIdx < fuNbPads )
80}
81bool CanvasConfig::GetLogx(uint32_t uPadIdx) const
82{
84 if (uPadIdx < fuNbPads) { return fvbLogx[uPadIdx]; } // if( uPadIdx < fuNbPads )
85 else {
86 std::cerr << "CanvasConfig::GetLogx => Pad index out of bounds, returning false! " << uPadIdx << " VS " << fuNbPads
87 << std::endl;
88 return false;
89 } // else of if( uPadIdx < fuNbPads )
90}
91bool CanvasConfig::GetLogy(uint32_t uPadIdx) const
92{
94 if (uPadIdx < fuNbPads) { return fvbLogy[uPadIdx]; } // if( uPadIdx < fuNbPads )
95 else {
96 std::cerr << "CanvasConfig::GetLogy => Pad index out of bounds, returning false! " << uPadIdx << " VS " << fuNbPads
97 << std::endl;
98 return false;
99 } // else of if( uPadIdx < fuNbPads )
100}
101bool CanvasConfig::GetLogz(uint32_t uPadIdx) const
102{
104 if (uPadIdx < fuNbPads) { return fvbLogz[uPadIdx]; } // if( uPadIdx < fuNbPads )
105 else {
106 std::cerr << "CanvasConfig::GetLogz => Pad index out of bounds, returning false! " << uPadIdx << " VS " << fuNbPads
107 << std::endl;
108 return false;
109 } // else of if( uPadIdx < fuNbPads )
110}
111uint32_t CanvasConfig::GetNbObjsInPad(uint32_t uPadIdx) const
112{
114 if (uPadIdx < fuNbPads) { return fvvsObjName[uPadIdx].size(); } // if( uPadIdx < fuNbPads )
115 else {
116 std::cerr << "CanvasConfig::GetLogz => Pad index out of bounds, returning 0! " << uPadIdx << " VS " << fuNbPads
117 << std::endl;
118 return 0;
119 } // else of if( uPadIdx < fuNbPads )
120}
121std::string CanvasConfig::GetObjName(uint32_t uPadIdx, uint32_t uObjIdx) const
122{
124 if (uPadIdx >= fuNbPads) {
125 std::cerr << "CanvasConfig::GetObjName => Pad index out of bounds, "
126 "returning nullptr! "
127 << uPadIdx << " VS " << fuNbPads << std::endl;
128 return std::string("nullptr");
129 } // if( uPadIdx >= fuNbPads )
130
132 if (uObjIdx < GetNbObjsInPad(uPadIdx)) {
133 return fvvsObjName[uPadIdx][uObjIdx];
134 } // if( uObjIdx < GetNbObjsInPad( uPadIdx ) )
135 {
136 std::cerr << "CanvasConfig::GetObjName => Object index out of bounds, "
137 "returning nullptr! "
138 << "Pad " << uPadIdx << " " << uObjIdx << " VS " << GetNbObjsInPad(uPadIdx) << std::endl;
139 return std::string("nullptr");
140 } // else of if( uObjIdx < GetNbObjsInPad( uPadIdx ) )
141}
142std::string CanvasConfig::GetOption(uint32_t uPadIdx, uint32_t uObjIdx) const
143{
145 if (uPadIdx >= fuNbPads) {
146 std::cerr << "CanvasConfig::GetObjName => Pad index out of bounds, "
147 "returning nullptr! "
148 << uPadIdx << " VS " << fuNbPads << std::endl;
149 return std::string("nullptr");
150 } // if( uPadIdx >= fuNbPads )
151
153 if (uObjIdx < GetNbObjsInPad(uPadIdx)) {
154 return fvvsOptions[uPadIdx][uObjIdx];
155 } // if( uObjIdx < GetNbObjsInPad( uPadIdx ) )
156 {
157 std::cerr << "CanvasConfig::GetObjName => Object index out of bounds, "
158 "returning nullptr! "
159 << "Pad " << uPadIdx << " " << uObjIdx << " VS " << GetNbObjsInPad(uPadIdx) << std::endl;
160 return std::string("nullptr");
161 } // else of if( uObjIdx < GetNbObjsInPad( uPadIdx ) )
162}
163
165void CanvasConfig::SetNbPadsX(uint32_t uNbColumns)
166{
167 if (fuNbPadsX != uNbColumns) {
169 if (0 < fuNbPads)
170 std::cout << "CanvasConfig::SetNbPadsX => Warning: Number of pads "
171 "changed, stored configuration cleared!"
172 << std::endl;
173
174 fuNbPadsX = uNbColumns;
175
178
180 ResizeFields();
181 } // if( fuNbPadsX != uNbColumns )
182}
183void CanvasConfig::SetNbPadsY(uint32_t uNbRows)
184{
185 if (fuNbPadsY != uNbRows) {
187 if (0 < fuNbPads)
188 std::cout << "CanvasConfig::SetNbPadsY => Warning: Number of pads "
189 "changed, stored configuration cleared!"
190 << std::endl;
191
192 fuNbPadsY = uNbRows;
193
196
198 ResizeFields();
199 } // if( fuNbPadsY != uNbRows )
200}
201
202bool CanvasConfig::SetConfig(uint32_t uPadIdx, bool bGridx, bool bGridy, bool bLogx, bool bLogy, bool bLogz,
203 std::vector<std::string> vsObjName, std::vector<std::string> vsOptions)
204{
205 if (uPadIdx < fuNbPads) {
207 fvbGridx[uPadIdx] = bGridx;
208 fvbGridy[uPadIdx] = bGridy;
209 fvbLogx[uPadIdx] = bLogx;
210 fvbLogy[uPadIdx] = bLogy;
211 fvbLogz[uPadIdx] = bLogz;
212 fvvsObjName[uPadIdx] = vsObjName;
213 fvvsOptions[uPadIdx] = vsOptions;
214 } // if( uPadIdx < fuNbPads )
215 else {
216 std::cerr << "CanvasConfig::SetConfig => Pad index out of bounds! " << uPadIdx << " VS " << fuNbPads << std::endl;
217 return false;
218 } // else of if( uPadIdx < fuNbPads )
219
220 return true;
221}
222
224{
226 fvbGridx.clear();
227 fvbGridy.clear();
228 fvbLogx.clear();
229 fvbLogy.clear();
230 fvbLogz.clear();
231 for (uint32_t uObj = 0; uObj < fvvsObjName.size(); ++uObj) {
232 fvvsObjName[uObj].clear();
233 fvvsOptions[uObj].clear();
234 } // for( uint32_t uObj = 0; uObj < fvvsObjName.size(); ++uObj )
235 fvvsObjName.clear();
236 fvvsOptions.clear();
237
239 fvbGridx.resize(fuNbPads, false);
240 fvbGridy.resize(fuNbPads, false);
241 fvbLogx.resize(fuNbPads, false);
242 fvbLogy.resize(fuNbPads, false);
243 fvbLogz.resize(fuNbPads, false);
244 fvvsObjName.resize(fuNbPads);
245 fvvsOptions.resize(fuNbPads);
246}
247/**********************************************************************/
248
249void GetNbPadsXY(TPad* pPad, uint32_t& uNbPadsX, uint32_t& uNbPadsY)
250{
251 uint32_t uNbPads = 1;
252 uNbPadsX = 0;
253 uNbPadsY = 0;
254 pPad->cd(); // set current (mother) pad
255
257 double_t dPrevX = -1.0; // relative scale from 0 to 1, go over to avoid problem if not margin
258 double_t dPrevY = 2.0; // relative scale from 0 to 1, go over to avoid problem if not margin
259
261 while (pPad->GetPad(uNbPads)) {
263 pPad->cd(uNbPads);
264
266 if (dPrevX < gPad->GetXlowNDC()) {
267 uNbPadsX++;
268 dPrevX = gPad->GetXlowNDC();
269 } // if( dPrevX < gPad->GetXlowNDC() )
270
272 if (dPrevY > gPad->GetYlowNDC()) {
273 uNbPadsY++;
274 dPrevY = gPad->GetYlowNDC();
275 } // if( dPrevY > gPad->GetYlowNDC() )
276
278 uNbPads++;
279 } // while( pPad->GetPad( uNbPads ) );
280
281 return;
282}
283
286std::string GenerateCanvasConfigString(TCanvas* pCanv)
287{
288 if (nullptr == pCanv) return std::string("");
289
291 std::string sConfig = pCanv->GetName();
292 sConfig += ";";
293
295 sConfig += pCanv->GetTitle();
296 sConfig += ";";
297
299 std::string sPadsConfig = "";
300 uint32_t uNbPads = 1;
301 uint32_t uNbPadsX = 0;
302 uint32_t uNbPadsY = 0;
303 pCanv->cd(); // set current (mother) pad
304
306 double_t dPrevX = -1.0; // relative scale from 0 to 1, go over to avoid problem if not margin
307 double_t dPrevY = 2.0; // relative scale from 0 to 1, go over to avoid problem if not margin
308
310 while (pCanv->GetPad(uNbPads)) {
312 pCanv->cd(uNbPads);
313
315 if (dPrevX < gPad->GetXlowNDC()) {
316 uNbPadsX++;
317 dPrevX = gPad->GetXlowNDC();
318 } // if( dPrevX < gPad->GetXlowNDC() )
319
321 if (dPrevY > gPad->GetYlowNDC()) {
322 uNbPadsY++;
323 dPrevY = gPad->GetYlowNDC();
324 } // if( dPrevY > gPad->GetYlowNDC() )
325
328 sPadsConfig +=
329 Form("%d,%d,%d,%d,%d", gPad->GetGridx(), gPad->GetGridy(), gPad->GetLogx(), gPad->GetLogy(), gPad->GetLogz());
331 TObjLink* lnkHist = gPad->GetListOfPrimitives()->FirstLink();
332
334 if (!lnkHist) sPadsConfig += ",(nullptr,nullptr)";
335
336 while (lnkHist) {
337 if (nullptr != dynamic_cast<TH1*>(lnkHist->GetObject()))
338 sPadsConfig += Form(",(%s,%s)", lnkHist->GetObject()->GetName(), lnkHist->GetOption());
339 lnkHist = lnkHist->Next();
340 } // else while( lnkHist ) of if( nullptr == lnkHist )
341
343 sPadsConfig += ";";
344
346 uNbPads++;
347 } // while( pCanv->GetPad( uNbPads ) )
348
349 sConfig += Form("%u;%u;", uNbPadsX, uNbPadsY);
350 sConfig += sPadsConfig;
351
352 return sConfig;
353};
354
358{
360 std::string sName = "";
361 std::string sTitle = "";
362 uint32_t uNbPads = 0;
363 uint32_t uNbPadsX = 0;
364 uint32_t uNbPadsY = 0;
365 bool bGridx = false;
366 bool bGridy = false;
367 bool bLogx = false;
368 bool bLogy = false;
369 bool bLogz = false;
370 std::vector<std::string> vsObjName;
371 std::vector<std::string> vsOptions;
372
374 size_t charPosDel;
375
377 charPosDel = sFullConfig.find(';');
378 sName = sFullConfig.substr(0, charPosDel);
379 charPosDel++;
380 std::string sNext = sFullConfig.substr(charPosDel);
381
383 charPosDel = sNext.find(';');
384 sTitle = sNext.substr(0, charPosDel);
385 charPosDel++;
386 sNext = sNext.substr(charPosDel);
387
389 uNbPadsX = std::stoul(sNext, &charPosDel);
390 charPosDel++;
391 sNext = sNext.substr(charPosDel);
392
394 uNbPadsY = std::stoul(sNext, &charPosDel);
395 charPosDel++;
396 sNext = sNext.substr(charPosDel);
397
399 uNbPads = uNbPadsX * uNbPadsY;
400
402 CanvasConfig conf(sName, sTitle, uNbPadsX, uNbPadsY);
403
405 for (UInt_t uPadIdx = 0; uPadIdx < uNbPads; ++uPadIdx) {
406 if (0 == sNext.size()) {
407 std::cerr << "ExtractCanvasConfigFromString => Empty configuration string while " << uPadIdx << " over "
408 << uNbPads << " pads remain! "
409 << "last ones will have default config!" << std::endl;
410 continue;
411 } // if( 0 == sNext.size() )
412
415 bGridx = std::stoul(sNext, &charPosDel);
416 charPosDel++;
417 sNext = sNext.substr(charPosDel);
418
420 bGridy = std::stoul(sNext, &charPosDel);
421 charPosDel++;
422 sNext = sNext.substr(charPosDel);
423
425 bLogx = std::stoul(sNext, &charPosDel);
426 charPosDel++;
427 sNext = sNext.substr(charPosDel);
428
430 bLogy = std::stoul(sNext, &charPosDel);
431 charPosDel++;
432 sNext = sNext.substr(charPosDel);
433
435 bLogz = std::stoul(sNext, &charPosDel);
436 charPosDel++;
437 sNext = sNext.substr(charPosDel);
438
440 charPosDel = sNext.find(';');
441 std::string sObjs = sNext.substr(0, charPosDel);
442
444 size_t charPosOpBrkt = sObjs.find('(');
445 while (charPosOpBrkt != std::string::npos) {
447 sObjs = sObjs.substr(charPosOpBrkt);
448
450 charPosOpBrkt = sObjs.find('(');
451 size_t charPosComma = sObjs.find(',');
452 size_t charPosClBrkt = sObjs.find(')');
453
454 charPosOpBrkt++;
455 charPosComma++;
456 charPosClBrkt++;
457
459 std::string sObjName = sObjs.substr(charPosOpBrkt, charPosComma - charPosOpBrkt - 1);
460 std::string sObjOpt = sObjs.substr(charPosComma, charPosClBrkt - charPosComma - 1);
461
462 vsObjName.push_back(sObjName);
463 vsOptions.push_back(sObjOpt);
464
466 sObjs = sObjs.substr(charPosClBrkt);
467 charPosOpBrkt = sObjs.find('(');
468 } // while( charPosOpBrkt < charPosDel )
469
471 charPosDel = sNext.find(';');
472 charPosDel++;
473 sNext = sNext.substr(charPosDel);
474
476 conf.SetConfig(uPadIdx, bGridx, bGridy, bLogx, bLogy, bLogz, vsObjName, vsOptions);
477
479 vsObjName.clear();
480 vsOptions.clear();
481 } // for( UInt_t uPadIdx = 0; uPadIdx < uNbPads; ++uPadIdx )
482
484 return conf;
485}
std::string GenerateCanvasConfigString(TCanvas *pCanv)
void GetNbPadsXY(TPad *pPad, uint32_t &uNbPadsX, uint32_t &uNbPadsY)
CanvasConfig ExtractCanvasConfigFromString(std::string sFullConfig)
Extraction.
bool GetLogz(uint32_t uPadIdx) const
std::vector< bool > fvbLogx
std::vector< bool > fvbLogz
void SetNbPadsX(uint32_t uNbColumns)
setters
std::vector< std::vector< std::string > > fvvsOptions
bool GetGridy(uint32_t uPadIdx) const
bool GetLogy(uint32_t uPadIdx) const
std::string GetOption(uint32_t uPadIdx, uint32_t uObjIdx) const
void SetNbPadsY(uint32_t uNbRows)
std::vector< bool > fvbGridx
std::vector< bool > fvbLogy
std::vector< std::vector< std::string > > fvvsObjName
bool GetLogx(uint32_t uPadIdx) const
uint32_t GetNbObjsInPad(uint32_t uPadIdx) const
bool SetConfig(uint32_t uPadIdx, bool bGridx, bool bGridy, bool bLogx, bool bLogy, bool bLogz, std::vector< std::string > vsObjName, std::vector< std::string > vsOptions)
bool GetGridx(uint32_t uPadIdx) const
accessors
std::vector< bool > fvbGridy
std::string GetObjName(uint32_t uPadIdx, uint32_t uObjIdx) const