CanvasHelper
CanvasHelper.h
1 #ifndef CanvasHelper_HH_
2 #define CanvasHelper_HH_
3 
4 #include <RtypesCore.h>
5 #include <Rtypes.h>
6 #include <TColor.h>
7 #include <TVirtualPad.h>
8 #include <THStack.h>
9 #include <TPaveStats.h>
10 #include <TLegend.h>
11 #include <TMap.h>
12 #include <TNamed.h>
13 #include <TLine.h>
14 #include <TQObject.h>
15 
16 #include <utility>
17 #include <map>
18 #include <string>
19 
24 namespace Round {
25  int getFirstDigit();
26  std::pair<double, double> valueError(const double value, const double error);
27  void paveTextValueErrors(TPaveText *pave);
28 }
29 
34 enum EPaveAlignBits {
35  kPaveAlignLeft = BIT(14),
36  kPaveAlignRight = BIT(15),
37  kPaveAlignTop = BIT(16),
38  kPaveAlignBottom = BIT(17)
39 };
40 
48 enum ECanvasFormatBits {
49  kFormatPng = BIT(14),
50  kFormatPs = BIT(15),
51  kFormatROOT = BIT(16),
52  kFormatC = BIT(17)
53 };
54 
59 class TNamedLine: public TLine {
60  protected:
61  TString fName;
62 
63  public:
64  TNamedLine(const char *name, Double_t x1, Double_t y1, Double_t x2, Double_t y2);
65  ~TNamedLine();
66 
67  const char *GetName() const override { return fName.Data(); }
68 
69  ClassDef(TNamedLine, 1)
70 };
71 
76 class CanvasHelper: public TObject {
77  public:
85  static CanvasHelper* getInstance();
86 
90  virtual ~CanvasHelper();
91 
101  void addCanvas(TCanvas *canvas);
102 
113  static void addSubtitle(TVirtualPad *pad, const char *text);
114 
127  static void setPaveAlignment(TPave *pave, UInt_t align);
128 
139  static TPaveStats* getDefaultPaveStats(TVirtualPad *pad);
140 
150  static TLegend* getDefaultLegend(TVirtualPad *pad);
151 
161  static void addTextToStats(const char *text, TVirtualPad *pad);
162 
173  static void addTextToStats(const char *text, TPaveStats *stats, TVirtualPad *pad);
174 
181  static void addMultiCanvasTitle(TCanvas *canvas, const char *title, const char *subtitle = "");
182 
190  static void saveCanvas(TCanvas *canvas, UInt_t format);
191 
192  protected:
193  CanvasHelper();
194  static CanvasHelper *fgInstance;
195 
196  struct Margin {
197  Double_t left;
198  Double_t right;
199  Double_t bottom;
200  Double_t top;
201  };
202 
203  enum EFontFace {
204  TimesItalic = 1,
205  TimesBold = 2,
206  TimesBoldItalic = 3,
207  Helvetica = 4,
208  HelveticaItalic = 5,
209  HelveticaBold = 6,
210  HelveticaBoldItalic = 7,
211  Courier = 8,
212  CourierItalic = 9,
213  CourierBold = 10,
214  CourierBoldItalic = 11,
215  Symbol = 12,
216  Times = 13,
217  Wingdings = 14,
218  SymbolItalic = 15
219  };
220 
221  // Font sizes in pixels
222  static const Int_t FONT_SIZE_NORMAL;
223  static const Int_t FONT_SIZE_SMALL;
224  static const Int_t FONT_SIZE_SMALLER;
225  static const Int_t FONT_SIZE_LARGE;
226  static const Int_t FONT_SIZE_LARGER;
227  static const Int_t FONT_SIZE_HUGE;
228 
229  static const Double_t LINE_HEIGHT;
230 
231 // static const Int_t AXIS_TITLE_OFFSET;
232  static const Int_t AXIS_LABEL_OFFSET;
233  static const Int_t AXIS_TICK_LENGTH;
234 
235  static const Int_t MARGIN_LEFT;
236  static const Int_t MARGIN_TOP;
237  static const Int_t MARGIN_RIGHT;
238  static const Int_t MARGIN_BOTTOM;
239 
240  static const Int_t TITLE_VSPACE;
241  static const Int_t SUBTITLE_VSPACE;
242  static const Int_t PAVELINE_VSPACE;
243  static const Int_t AXISTITLE_VSPACE;
244 
245  static Style_t getFont(EFontFace fontFace = EFontFace::Helvetica);
246  static UInt_t getPaveLines(TPave *pave);
247  static UInt_t getPaveTextWidthPx(TPaveText *paveText);
248  static UInt_t getLegendWidthPx(TLegend *paveText);
249 
250  // TMap *canvasesToBeExported;
251  static std::pair<Double_t, Double_t> getSubtitleYNDCCoordinates(TVirtualPad *pad);
252 
253  std::map<TCanvas*, std::pair<unsigned int, unsigned int>> registeredCanvases;
254 // std::map<std::string, double> defaultPadLeftMargins;
255 // static TGraph* findTGraphOnPad(TVirtualPad* pad);
256 
257  static TObject* findObjectOnPad(TClass *c, TVirtualPad *pad);
258  // static TObject* findObjectOnPad(const char* name, TVirtualPad* pad);
259 
260  static void alignChildPad(TVirtualPad *pad);
261 
262  static Int_t getFrameLeftMarginPx(TVirtualPad *pad);
263  static Int_t getFrameRightMarginPx(/*TVirtualPad* pad*/);
264  static Int_t getFrameTopMarginPx(TVirtualPad *pad);
265  static Int_t getFrameBottomMarginPx(TVirtualPad *pad);
266 
267  static Bool_t hasXAxisTitle(TVirtualPad *pad);
268  static Bool_t hasYAxisTitle(TVirtualPad *pad);
269 
270  static Bool_t isChildPad(TVirtualPad *pad);
271 
272  static void alignTitle(TVirtualPad *pad);
273  static void alignSubtitle(TVirtualPad *pad);
274  static void alignSubtitle(TPaveText *subtitle, TVirtualPad *pad);
275  static void alignAllPaves(TVirtualPad *pad);
276  // static void alignAxisTitles(TVirtualPad* pad);
277 
278  void processCanvas(TCanvas *canvas);
279  void processPad(TVirtualPad *pad);
280  static void setPadMargins(TVirtualPad *pad);
281 
282  static void setPadNDivisions(TVirtualPad *pad);
283  void convertAxisToPxSize(TAxis *axis, const char type, TVirtualPad *pad);
284 
285  static void setPadCustomFrameBorder(TVirtualPad *pad);
286 
287  static Double_t getPadWidthPx(TVirtualPad *pad);
288  static Double_t getPadHeightPx(TVirtualPad *pad);
289 
290  static std::pair<TAxis*, TAxis*> getPadXYAxis(TVirtualPad *pad);
291  static Double_t getYAxisMaxLabelWidthPx(TVirtualPad *pad);
292 // Double_t getLabelHeigthPx();
293 
294  static Double_t pxToNdcHorizontal(Int_t px, TVirtualPad *pad);
295  static Double_t pxToNdcVertical(Int_t py, TVirtualPad *pad);
296 
297  static constexpr char subtitleObjectName[] = "subtitle";
298 
299  static TFrame* getPadFrame(TVirtualPad *pad);
300 
301  public:
302  // Slot for canvas resizing (need to be public)
303  void onCanvasResized();
304 
305  ClassDef(CanvasHelper, 0)
306 };
307 
308 #endif
Definition: CanvasHelper.h:76
virtual ~CanvasHelper()
Class destructor.
Definition: CanvasHelper.cpp:126
static void addMultiCanvasTitle(TCanvas *canvas, const char *title, const char *subtitle="")
Adding a multi-pad canvas title. Optionally supprts subtitle too.
Definition: CanvasHelper.cpp:1119
static void addSubtitle(TVirtualPad *pad, const char *text)
Add subtitle to the canvas. lololo.
Definition: CanvasHelper.cpp:415
static void setPaveAlignment(TPave *pave, UInt_t align)
Used to specify TPave's alignment on the ROOT canvas. Emum variables can be combined:
Definition: CanvasHelper.cpp:789
static void saveCanvas(TCanvas *canvas, UInt_t format)
Definition: CanvasHelper.cpp:812
static CanvasHelper * getInstance()
Obtain an instance of the CanvasHelper class.
Definition: CanvasHelper.cpp:131
static void addTextToStats(const char *text, TVirtualPad *pad)
Used to add a line to the default canvas (pad) statistics box.
Definition: CanvasHelper.cpp:916
static TPaveStats * getDefaultPaveStats(TVirtualPad *pad)
Used to obtain default ROOT statistics box from a canvas with histogram, graph etc....
Definition: CanvasHelper.cpp:858
void addCanvas(TCanvas *canvas)
Register your canvas for processing. Canvas needs to be added after all the primitives are drawn on i...
Definition: CanvasHelper.cpp:295
static TLegend * getDefaultLegend(TVirtualPad *pad)
Used to obtain default ROOT legend from a canvas.
Definition: CanvasHelper.cpp:910
Definition: CanvasHelper.h:59
Definition: CanvasHelper.cpp:42