CbmRoot
CbmFlesCanvasTools.cxx
Go to the documentation of this file.
1 #include "CbmFlesCanvasTools.h"
2 
3 #include <iostream>
4 
5 #include "TCanvas.h"
6 #include "TH1.h"
7 
8 /**********************************************************************/
10  : fsName("")
11  , fsTitle("")
12  , fuNbPads(0)
13  , fuNbPadsX(0)
14  , fuNbPadsY(0)
15  , fvbGridx()
16  , fvbGridy()
17  , fvbLogx()
18  , fvbLogy()
19  , fvbLogz()
20  , fvvsObjName()
21  , fvvsOptions() {}
22 CanvasConfig::CanvasConfig(std::string sName,
23  std::string sTitle,
24  uint32_t uNbPadsX,
25  uint32_t uNbPadsY)
26  : fsName(sName)
27  , fsTitle(sTitle)
28  , fuNbPads(uNbPadsX * uNbPadsY)
29  , fuNbPadsX(uNbPadsX)
30  , fuNbPadsY(uNbPadsY)
31  , fvbGridx(fuNbPads, false)
32  , fvbGridy(fuNbPads, false)
33  , fvbLogx(fuNbPads, false)
34  , fvbLogy(fuNbPads, false)
35  , fvbLogz(fuNbPads, false)
36  , fvvsObjName(fuNbPads)
37  , fvvsOptions(fuNbPads) {}
38 
41  fvbGridx.clear();
42  fvbGridy.clear();
43  fvbLogx.clear();
44  fvbLogy.clear();
45  fvbLogz.clear();
46  for (uint32_t uObj = 0; uObj < fvvsObjName.size(); ++uObj) {
47  fvvsObjName[uObj].clear();
48  fvvsOptions[uObj].clear();
49  } // for( uint32_t uObj = 0; uObj < fvvsObjName.size(); ++uObj )
50  fvvsObjName.clear();
51  fvvsOptions.clear();
52 }
53 
55 bool CanvasConfig::GetGridx(uint32_t uPadIdx) const {
57  if (uPadIdx < fuNbPads) {
58  return fvbGridx[uPadIdx];
59  } // if( uPadIdx < fuNbPads )
60  else {
61  std::cerr
62  << "CanvasConfig::GetGridx => Pad index out of bounds, returning false! "
63  << uPadIdx << " VS " << fuNbPads << std::endl;
64  return false;
65  } // else of if( uPadIdx < fuNbPads )
66 }
67 bool CanvasConfig::GetGridy(uint32_t uPadIdx) const {
69  if (uPadIdx < fuNbPads) {
70  return fvbGridy[uPadIdx];
71  } // if( uPadIdx < fuNbPads )
72  else {
73  std::cerr
74  << "CanvasConfig::GetGridy => Pad index out of bounds, returning false! "
75  << uPadIdx << " VS " << fuNbPads << std::endl;
76  return false;
77  } // else of if( uPadIdx < fuNbPads )
78 }
79 bool CanvasConfig::GetLogx(uint32_t uPadIdx) const {
81  if (uPadIdx < fuNbPads) {
82  return fvbLogx[uPadIdx];
83  } // if( uPadIdx < fuNbPads )
84  else {
85  std::cerr
86  << "CanvasConfig::GetLogx => Pad index out of bounds, returning false! "
87  << uPadIdx << " VS " << fuNbPads << std::endl;
88  return false;
89  } // else of if( uPadIdx < fuNbPads )
90 }
91 bool CanvasConfig::GetLogy(uint32_t uPadIdx) const {
93  if (uPadIdx < fuNbPads) {
94  return fvbLogy[uPadIdx];
95  } // if( uPadIdx < fuNbPads )
96  else {
97  std::cerr
98  << "CanvasConfig::GetLogy => Pad index out of bounds, returning false! "
99  << uPadIdx << " VS " << fuNbPads << std::endl;
100  return false;
101  } // else of if( uPadIdx < fuNbPads )
102 }
103 bool CanvasConfig::GetLogz(uint32_t uPadIdx) const {
105  if (uPadIdx < fuNbPads) {
106  return fvbLogz[uPadIdx];
107  } // if( uPadIdx < fuNbPads )
108  else {
109  std::cerr
110  << "CanvasConfig::GetLogz => Pad index out of bounds, returning false! "
111  << uPadIdx << " VS " << fuNbPads << std::endl;
112  return false;
113  } // else of if( uPadIdx < fuNbPads )
114 }
115 uint32_t CanvasConfig::GetNbObjsInPad(uint32_t uPadIdx) const {
117  if (uPadIdx < fuNbPads) {
118  return fvvsObjName[uPadIdx].size();
119  } // if( uPadIdx < fuNbPads )
120  else {
121  std::cerr
122  << "CanvasConfig::GetLogz => Pad index out of bounds, returning 0! "
123  << uPadIdx << " VS " << fuNbPads << std::endl;
124  return 0;
125  } // else of if( uPadIdx < fuNbPads )
126 }
127 std::string CanvasConfig::GetObjName(uint32_t uPadIdx, uint32_t uObjIdx) const {
129  if (uPadIdx >= fuNbPads) {
130  std::cerr << "CanvasConfig::GetObjName => Pad index out of bounds, "
131  "returning nullptr! "
132  << uPadIdx << " VS " << fuNbPads << std::endl;
133  return std::string("nullptr");
134  } // if( uPadIdx >= fuNbPads )
135 
137  if (uObjIdx < GetNbObjsInPad(uPadIdx)) {
138  return fvvsObjName[uPadIdx][uObjIdx];
139  } // if( uObjIdx < GetNbObjsInPad( uPadIdx ) )
140  {
141  std::cerr << "CanvasConfig::GetObjName => Object index out of bounds, "
142  "returning nullptr! "
143  << "Pad " << uPadIdx << " " << uObjIdx << " VS "
144  << GetNbObjsInPad(uPadIdx) << std::endl;
145  return std::string("nullptr");
146  } // else of if( uObjIdx < GetNbObjsInPad( uPadIdx ) )
147 }
148 std::string CanvasConfig::GetOption(uint32_t uPadIdx, uint32_t uObjIdx) const {
150  if (uPadIdx >= fuNbPads) {
151  std::cerr << "CanvasConfig::GetObjName => Pad index out of bounds, "
152  "returning nullptr! "
153  << uPadIdx << " VS " << fuNbPads << std::endl;
154  return std::string("nullptr");
155  } // if( uPadIdx >= fuNbPads )
156 
158  if (uObjIdx < GetNbObjsInPad(uPadIdx)) {
159  return fvvsOptions[uPadIdx][uObjIdx];
160  } // if( uObjIdx < GetNbObjsInPad( uPadIdx ) )
161  {
162  std::cerr << "CanvasConfig::GetObjName => Object index out of bounds, "
163  "returning nullptr! "
164  << "Pad " << uPadIdx << " " << uObjIdx << " VS "
165  << GetNbObjsInPad(uPadIdx) << std::endl;
166  return std::string("nullptr");
167  } // else of if( uObjIdx < GetNbObjsInPad( uPadIdx ) )
168 }
169 
171 void CanvasConfig::SetNbPadsX(uint32_t uNbColumns) {
172  if (fuNbPadsX != uNbColumns) {
174  if (0 < fuNbPads)
175  std::cout << "CanvasConfig::SetNbPadsX => Warning: Number of pads "
176  "changed, stored configuration cleared!"
177  << std::endl;
178 
179  fuNbPadsX = uNbColumns;
180 
183 
185  ResizeFields();
186  } // if( fuNbPadsX != uNbColumns )
187 }
188 void CanvasConfig::SetNbPadsY(uint32_t uNbRows) {
189  if (fuNbPadsY != uNbRows) {
191  if (0 < fuNbPads)
192  std::cout << "CanvasConfig::SetNbPadsY => Warning: Number of pads "
193  "changed, stored configuration cleared!"
194  << std::endl;
195 
196  fuNbPadsY = uNbRows;
197 
200 
202  ResizeFields();
203  } // if( fuNbPadsY != uNbRows )
204 }
205 
206 bool CanvasConfig::SetConfig(uint32_t uPadIdx,
207  bool bGridx,
208  bool bGridy,
209  bool bLogx,
210  bool bLogy,
211  bool bLogz,
212  std::vector<std::string> vsObjName,
213  std::vector<std::string> vsOptions) {
214  if (uPadIdx < fuNbPads) {
216  fvbGridx[uPadIdx] = bGridx;
217  fvbGridy[uPadIdx] = bGridy;
218  fvbLogx[uPadIdx] = bLogx;
219  fvbLogy[uPadIdx] = bLogy;
220  fvbLogz[uPadIdx] = bLogz;
221  fvvsObjName[uPadIdx] = vsObjName;
222  fvvsOptions[uPadIdx] = vsOptions;
223  } // if( uPadIdx < fuNbPads )
224  else {
225  std::cerr << "CanvasConfig::SetConfig => Pad index out of bounds! "
226  << uPadIdx << " VS " << fuNbPads << std::endl;
227  return false;
228  } // else of if( uPadIdx < fuNbPads )
229 
230  return true;
231 }
232 
235  fvbGridx.clear();
236  fvbGridy.clear();
237  fvbLogx.clear();
238  fvbLogy.clear();
239  fvbLogz.clear();
240  for (uint32_t uObj = 0; uObj < fvvsObjName.size(); ++uObj) {
241  fvvsObjName[uObj].clear();
242  fvvsOptions[uObj].clear();
243  } // for( uint32_t uObj = 0; uObj < fvvsObjName.size(); ++uObj )
244  fvvsObjName.clear();
245  fvvsOptions.clear();
246 
248  fvbGridx.resize(fuNbPads, false);
249  fvbGridy.resize(fuNbPads, false);
250  fvbLogx.resize(fuNbPads, false);
251  fvbLogy.resize(fuNbPads, false);
252  fvbLogz.resize(fuNbPads, false);
253  fvvsObjName.resize(fuNbPads);
254  fvvsOptions.resize(fuNbPads);
255 }
256 /**********************************************************************/
257 
258 void GetNbPadsXY(TPad* pPad, uint32_t& uNbPadsX, uint32_t& uNbPadsY) {
259  uint32_t uNbPads = 1;
260  uNbPadsX = 0;
261  uNbPadsY = 0;
262  pPad->cd(); // set current (mother) pad
263 
265  double_t dPrevX =
266  -1.0; // relative scale from 0 to 1, go over to avoid problem if not margin
267  double_t dPrevY =
268  2.0; // relative scale from 0 to 1, go over to avoid problem if not margin
269 
271  while (pPad->GetPad(uNbPads)) {
273  pPad->cd(uNbPads);
274 
276  if (dPrevX < gPad->GetXlowNDC()) {
277  uNbPadsX++;
278  dPrevX = gPad->GetXlowNDC();
279  } // if( dPrevX < gPad->GetXlowNDC() )
280 
282  if (dPrevY > gPad->GetYlowNDC()) {
283  uNbPadsY++;
284  dPrevY = gPad->GetYlowNDC();
285  } // if( dPrevY > gPad->GetYlowNDC() )
286 
288  uNbPads++;
289  } // while( pPad->GetPad( uNbPads ) );
290 
291  return;
292 }
293 
296 std::string GenerateCanvasConfigString(TCanvas* pCanv) {
297  if (nullptr == pCanv) return std::string("");
298 
300  std::string sConfig = pCanv->GetName();
301  sConfig += ";";
302 
304  sConfig += pCanv->GetTitle();
305  sConfig += ";";
306 
308  std::string sPadsConfig = "";
309  uint32_t uNbPads = 1;
310  uint32_t uNbPadsX = 0;
311  uint32_t uNbPadsY = 0;
312  pCanv->cd(); // set current (mother) pad
313 
315  double_t dPrevX =
316  -1.0; // relative scale from 0 to 1, go over to avoid problem if not margin
317  double_t dPrevY =
318  2.0; // relative scale from 0 to 1, go over to avoid problem if not margin
319 
321  while (pCanv->GetPad(uNbPads)) {
323  pCanv->cd(uNbPads);
324 
326  if (dPrevX < gPad->GetXlowNDC()) {
327  uNbPadsX++;
328  dPrevX = gPad->GetXlowNDC();
329  } // if( dPrevX < gPad->GetXlowNDC() )
330 
332  if (dPrevY > gPad->GetYlowNDC()) {
333  uNbPadsY++;
334  dPrevY = gPad->GetYlowNDC();
335  } // if( dPrevY > gPad->GetYlowNDC() )
336 
339  sPadsConfig += Form("%d,%d,%d,%d,%d",
340  gPad->GetGridx(),
341  gPad->GetGridy(),
342  gPad->GetLogx(),
343  gPad->GetLogy(),
344  gPad->GetLogz());
346  TObjLink* lnkHist = gPad->GetListOfPrimitives()->FirstLink();
347 
349  if (!lnkHist) sPadsConfig += ",(nullptr,nullptr)";
350 
351  while (lnkHist) {
352  if (nullptr != dynamic_cast<TH1*>(lnkHist->GetObject()))
353  sPadsConfig += Form(
354  ",(%s,%s)", lnkHist->GetObject()->GetName(), lnkHist->GetOption());
355  lnkHist = lnkHist->Next();
356  } // else while( lnkHist ) of if( nullptr == lnkHist )
357 
359  sPadsConfig += ";";
360 
362  uNbPads++;
363  } // while( pCanv->GetPad( uNbPads ) )
364 
365  sConfig += Form("%u;%u;", uNbPadsX, uNbPadsY);
366  sConfig += sPadsConfig;
367 
368  return sConfig;
369 };
370 
373 CanvasConfig ExtractCanvasConfigFromString(std::string sFullConfig) {
375  std::string sName = "";
376  std::string sTitle = "";
377  uint32_t uNbPads = 0;
378  uint32_t uNbPadsX = 0;
379  uint32_t uNbPadsY = 0;
380  bool bGridx = false;
381  bool bGridy = false;
382  bool bLogx = false;
383  bool bLogy = false;
384  bool bLogz = false;
385  std::vector<std::string> vsObjName;
386  std::vector<std::string> vsOptions;
387 
389  size_t charPosDel;
390 
392  charPosDel = sFullConfig.find(';');
393  sName = sFullConfig.substr(0, charPosDel);
394  charPosDel++;
395  std::string sNext = sFullConfig.substr(charPosDel);
396 
398  charPosDel = sNext.find(';');
399  sTitle = sNext.substr(0, charPosDel);
400  charPosDel++;
401  sNext = sNext.substr(charPosDel);
402 
404  uNbPadsX = std::stoul(sNext, &charPosDel);
405  charPosDel++;
406  sNext = sNext.substr(charPosDel);
407 
409  uNbPadsY = std::stoul(sNext, &charPosDel);
410  charPosDel++;
411  sNext = sNext.substr(charPosDel);
412 
414  uNbPads = uNbPadsX * uNbPadsY;
415 
417  CanvasConfig conf(sName, sTitle, uNbPadsX, uNbPadsY);
418 
420  for (UInt_t uPadIdx = 0; uPadIdx < uNbPads; ++uPadIdx) {
421  if (0 == sNext.size()) {
422  std::cerr
423  << "ExtractCanvasConfigFromString => Empty configuration string while "
424  << uPadIdx << " over " << uNbPads << " pads remain! "
425  << "last ones will have default config!" << std::endl;
426  } // if( 0 == sNext.size() )
427 
430  bGridx = std::stoul(sNext, &charPosDel);
431  charPosDel++;
432  sNext = sNext.substr(charPosDel);
433 
435  bGridy = std::stoul(sNext, &charPosDel);
436  charPosDel++;
437  sNext = sNext.substr(charPosDel);
438 
440  bLogx = std::stoul(sNext, &charPosDel);
441  charPosDel++;
442  sNext = sNext.substr(charPosDel);
443 
445  bLogy = std::stoul(sNext, &charPosDel);
446  charPosDel++;
447  sNext = sNext.substr(charPosDel);
448 
450  bLogz = std::stoul(sNext, &charPosDel);
451  charPosDel++;
452  sNext = sNext.substr(charPosDel);
453 
455  charPosDel = sNext.find(';');
456  std::string sObjs = sNext.substr(0, charPosDel);
457 
459  size_t charPosOpBrkt = sObjs.find('(');
460  while (charPosOpBrkt != std::string::npos) {
462  sObjs = sObjs.substr(charPosOpBrkt);
463 
465  charPosOpBrkt = sObjs.find('(');
466  size_t charPosComma = sObjs.find(',');
467  size_t charPosClBrkt = sObjs.find(')');
468 
469  charPosOpBrkt++;
470  charPosComma++;
471  charPosClBrkt++;
472 
474  std::string sObjName =
475  sObjs.substr(charPosOpBrkt, charPosComma - charPosOpBrkt - 1);
476  std::string sObjOpt =
477  sObjs.substr(charPosComma, charPosClBrkt - charPosComma - 1);
478 
479  vsObjName.push_back(sObjName);
480  vsOptions.push_back(sObjOpt);
481 
483  sObjs = sObjs.substr(charPosClBrkt);
484  charPosOpBrkt = sObjs.find('(');
485  } // while( charPosOpBrkt < charPosDel )
486 
488  charPosDel = sNext.find(';');
489  charPosDel++;
490  sNext = sNext.substr(charPosDel);
491 
493  conf.SetConfig(
494  uPadIdx, bGridx, bGridy, bLogx, bLogy, bLogz, vsObjName, vsOptions);
495 
497  vsObjName.clear();
498  vsOptions.clear();
499  } // for( UInt_t uPadIdx = 0; uPadIdx < uNbPads; ++uPadIdx )
500 
502  return conf;
503 }
CanvasConfig::GetObjName
std::string GetObjName(uint32_t uPadIdx, uint32_t uObjIdx) const
Definition: CbmFlesCanvasTools.cxx:127
CanvasConfig::SetNbPadsX
void SetNbPadsX(uint32_t uNbColumns)
setters
Definition: CbmFlesCanvasTools.cxx:171
ExtractCanvasConfigFromString
CanvasConfig ExtractCanvasConfigFromString(std::string sFullConfig)
Extraction.
Definition: CbmFlesCanvasTools.cxx:373
CanvasConfig::GetLogy
bool GetLogy(uint32_t uPadIdx) const
Definition: CbmFlesCanvasTools.cxx:91
CanvasConfig::fvbLogx
std::vector< bool > fvbLogx
Definition: CbmFlesCanvasTools.h:64
CbmFlesCanvasTools.h
CanvasConfig::SetConfig
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)
Definition: CbmFlesCanvasTools.cxx:206
CanvasConfig::GetLogz
bool GetLogz(uint32_t uPadIdx) const
Definition: CbmFlesCanvasTools.cxx:103
CanvasConfig::fvbLogz
std::vector< bool > fvbLogz
Definition: CbmFlesCanvasTools.h:66
CanvasConfig
Definition: CbmFlesCanvasTools.h:17
CanvasConfig::GetNbObjsInPad
uint32_t GetNbObjsInPad(uint32_t uPadIdx) const
Definition: CbmFlesCanvasTools.cxx:115
CanvasConfig::SetNbPadsY
void SetNbPadsY(uint32_t uNbRows)
Definition: CbmFlesCanvasTools.cxx:188
CanvasConfig::fuNbPadsX
uint32_t fuNbPadsX
Definition: CbmFlesCanvasTools.h:59
CanvasConfig::fvbLogy
std::vector< bool > fvbLogy
Definition: CbmFlesCanvasTools.h:65
CanvasConfig::fvvsOptions
std::vector< std::vector< std::string > > fvvsOptions
Definition: CbmFlesCanvasTools.h:68
CanvasConfig::fvbGridx
std::vector< bool > fvbGridx
Definition: CbmFlesCanvasTools.h:62
CanvasConfig::GetOption
std::string GetOption(uint32_t uPadIdx, uint32_t uObjIdx) const
Definition: CbmFlesCanvasTools.cxx:148
CanvasConfig::fvbGridy
std::vector< bool > fvbGridy
Definition: CbmFlesCanvasTools.h:63
CanvasConfig::GetLogx
bool GetLogx(uint32_t uPadIdx) const
Definition: CbmFlesCanvasTools.cxx:79
GenerateCanvasConfigString
std::string GenerateCanvasConfigString(TCanvas *pCanv)
Definition: CbmFlesCanvasTools.cxx:296
CanvasConfig::~CanvasConfig
~CanvasConfig()
Definition: CbmFlesCanvasTools.cxx:39
CanvasConfig::GetGridy
bool GetGridy(uint32_t uPadIdx) const
Definition: CbmFlesCanvasTools.cxx:67
CanvasConfig::fuNbPads
uint32_t fuNbPads
Definition: CbmFlesCanvasTools.h:58
CanvasConfig::CanvasConfig
CanvasConfig()
Definition: CbmFlesCanvasTools.cxx:9
CanvasConfig::fvvsObjName
std::vector< std::vector< std::string > > fvvsObjName
Definition: CbmFlesCanvasTools.h:67
GetNbPadsXY
void GetNbPadsXY(TPad *pPad, uint32_t &uNbPadsX, uint32_t &uNbPadsY)
Definition: CbmFlesCanvasTools.cxx:258
CanvasConfig::fuNbPadsY
uint32_t fuNbPadsY
Definition: CbmFlesCanvasTools.h:60
CanvasConfig::ResizeFields
void ResizeFields()
Definition: CbmFlesCanvasTools.cxx:233
CanvasConfig::GetGridx
bool GetGridx(uint32_t uPadIdx) const
accessors
Definition: CbmFlesCanvasTools.cxx:55