Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
HistoStyle.cxx
1/*
2 * HistoStyle.cxx
3 *
4 * Created on: 30 lip 2024
5 * Author: daniel
6 */
7
8#include "HistoStyle.h"
9
10#include <Rtypes.h>
11#include <TAttMarker.h>
12#include <TGraphErrors.h>
13#include <TH1.h>
14
15#include "Cout.h"
16#include "MultiGraph.h"
17#include "StdHist.h"
18#include "StdString.h"
19#include "Style.h"
20#include "XMLNode.h"
21
22#include <type_traits>
23
24namespace Hal {
25 const unsigned short int HistoStyle::kTitle = 0;
26 const unsigned short int HistoStyle::kMin = 1;
27 const unsigned short int HistoStyle::kMax = 2;
28 void HistoStyle::SetTitle(TString title) {
29 SetI(kTitle, 0);
30 fTitle = title;
31 }
32
33 AxisStyle& HistoStyle::GetAxisStyle(Char_t x) {
34 if (x == 'y' || x == 'Y') return fYAxis;
35 if (x == 'z' || x == 'Z') return fZAxis;
36 return fXAxis;
37 }
38
39 void HistoStyle::ExportToXML(XMLNode* node) const {
40 if (Find(kTitle)) node->AddAttrib(new Hal::XMLAttrib("Title", fTitle));
41 if (Find(kMin)) node->AddAttrib(new Hal::XMLAttrib("Min", Form("%4.4f", GetF(kMin))));
42 if (Find(kMax)) node->AddAttrib(new Hal::XMLAttrib("Max", Form("%4.4f", GetF(kMax))));
43 XMLNode* xAxis = new XMLNode("XAxis");
44 XMLNode* yAxis = new XMLNode("YAxis");
45 XMLNode* zAxis = new XMLNode("ZAxis");
46 XMLNode* fill = new XMLNode("Fill");
47 XMLNode* marker = new XMLNode("Marker");
48 XMLNode* line = new XMLNode("Line");
49 fXAxis.ExportToXML(xAxis);
50 fYAxis.ExportToXML(yAxis);
51 fZAxis.ExportToXML(zAxis);
52 fMarker.ExportToXML(marker);
53 fLine.ExportToXML(line);
54 fFill.ExportToXML(fill);
55 node->AddChild(xAxis);
56 node->AddChild(yAxis);
57 node->AddChild(zAxis);
58 node->AddChild(fill);
59 node->AddChild(marker);
60 node->AddChild(line);
61 }
62
64 HistoStyle res;
65 if (Hal::Std::FindParam(style, "05", kTRUE)) {
66 res.SetAxis(0.05, 0.8, 0.05, 0.005, 'x');
67 res.SetAxis(0.05, 0.8, 0.05, 0.005, 'y');
68 }
69 if (Hal::Std::FindParam(style, "color", kTRUE)) res.SetColor(kRed);
70 if (Hal::Std::FindParam(style, "circle", kTRUE)) res.GetMarkerStyle().SetStyle(kFullCircle);
71 if (Hal::Std::FindParam(style, "center", kTRUE)) {
72 res.CenterTitle(kTRUE, 'x');
73 res.CenterTitle(kTRUE, 'y');
74 }
75 return res;
76 }
77
78 void HistoStyle::SetMin(Double_t val) { SetF(kMin, val); }
79
80 void HistoStyle::SetMax(Double_t val) { SetF(kMax, val); }
81
82 void HistoStyle::SetMinMax(Double_t minVal, Double_t maxVal) {
83 SetMin(minVal);
84 SetMax(maxVal);
85 }
86
87 void HistoStyle::SetMarkerProperties(Color_t c, Int_t marker, Size_t size) {
88 fMarker.SetColor(c);
89 fMarker.SetStyle(marker);
90 fMarker.SetSize(size);
91 }
92
93 void HistoStyle::SetLineProperties(Color_t c, Style_t style, Double_t widgth) {
94 fLine.SetColor(c);
95 fLine.SetStyle(style);
96 fLine.SetWidth(widgth);
97 }
98
99 void HistoStyle::SetColor(Color_t c) {
100 fLine.SetColor(c);
101 fMarker.SetColor(c);
102 }
103
104 void HistoStyle::SetRangeUser(Double_t min, Double_t max, Char_t ax) {
105 if (ax == 'x') fXAxis.SetRangeUser(min, max);
106 if (ax == 'y') fYAxis.SetRangeUser(min, max);
107 if (ax == 'z') fZAxis.SetRangeUser(min, max);
108 }
109
110 void HistoStyle::SetTitles(TString histo, TString xAxis, TString yAxis, TString zAxis) {
111 SetTitle(histo);
112 fXAxis.SetTitle(xAxis);
113 fYAxis.SetTitle(yAxis);
114 fZAxis.SetTitle(zAxis);
115 }
116
117 void HistoStyle::SetAxis(Double_t titleSize, Double_t titleOffset, Double_t labelSize, Double_t labelOffset, Char_t x) {
118 auto setAx = [](AxisStyle& st, Double_t _titleSize, Double_t _titleOffset, Double_t _labelSize, Double_t _labelOffset) {
119 st.SetTitleSize(_titleSize);
120 st.SetTitleOffset(_titleOffset);
121 st.SetLabelOffset(_labelOffset);
122 st.SetLabelSize(_labelSize);
123 };
124 if (x == 'x') setAx(fXAxis, titleSize, titleOffset, labelSize, labelOffset);
125 if (x == 'y') setAx(fYAxis, titleSize, titleOffset, labelSize, labelOffset);
126 if (x == 'z') setAx(fZAxis, titleSize, titleOffset, labelSize, labelOffset);
127 }
128
129 void HistoStyle::CenterTitle(Bool_t center, Char_t x) {
130 if (x == 'x') fXAxis.SetCenterTitle(center);
131 if (x == 'y') fYAxis.SetCenterTitle(center);
132 if (x == 'z') fZAxis.SetCenterTitle(center);
133 }
134
135 void HistoStyle::Apply(TObject& h, Int_t no) const {
136 if (h.InheritsFrom("TH1")) {
137 ApplyInternal(*dynamic_cast<TH1*>(&h));
138 auto zax = static_cast<TH1&>(h).GetZaxis();
139 if (zax) fZAxis.Apply(*zax);
140 } else if (h.InheritsFrom("Hal::MultiGraph")) {
141 auto gr = dynamic_cast<Hal::MultiGraph*>(&h);
142 if (no < 0) {
143 for (int i = 0; i < gr->GetNGraphs(); i++) {
144 ApplyInternal(*gr->GetGraph(i));
145 }
146 fXAxis.Apply(*gr->GetXaxis());
147 fYAxis.Apply(*gr->GetYaxis());
148 } else {
149 ApplyInternal(*gr->GetGraph(no));
150 }
151 } else if (h.InheritsFrom("Hal::DividedHisto1D")) {
152 dynamic_cast<Hal::DividedHisto1D*>(&h)->ApplyStyle(*this);
153 }
154 if (h.InheritsFrom("TGraph")) { ApplyInternal(dynamic_cast<TGraph&>(h)); }
155 }
156
157 void HistoStyle::SetAntiColor(Bool_t safe) {
158 Int_t i;
159 auto GetCol = [&](Int_t col) {
160 col = Hal::Std::GetAntiColor(col);
161 return col;
162 };
163 fMarker.SetColor(GetCol(i));
164 fLine.SetColor(GetCol(i));
165 }
166
167 template<class T>
168 void Hal::HistoStyle::ApplyInternal(T& object) const {
169 if (Find(kTitle)) object.SetTitle(fTitle);
170 if (Find(kMin)) object.SetMinimum(GetF(kMin));
171 if (Find(kMax)) object.SetMaximum(GetF(kMax));
172
173 fMarker.Apply(object);
174 fLine.Apply(object);
175 fFill.Apply(object);
176 auto xax = object.GetXaxis();
177 if (xax) fXAxis.Apply(*xax);
178 auto yax = object.GetYaxis();
179 if (yax) fYAxis.Apply(*yax);
180 }
181
182 void HistoStyle::ImportFromXML(XMLNode* node) {
183 if (auto atr = node->GetAttrib("Title")) {
184 TString x = atr->GetValue();
185 SetTitle(x);
186 }
187 if (auto atr = node->GetAttrib("Min")) {
188 float x = atr->GetValue().Atof();
189 SetMin(x);
190 }
191 if (auto atr = node->GetAttrib("Max")) {
192 float x = atr->GetValue().Atof();
193 SetMax(x);
194 }
195 if (auto xAxis = node->GetChild("XAxis")) { fXAxis.ImportFromXML(xAxis); }
196 if (auto yAxis = node->GetChild("YAxis")) { fYAxis.ImportFromXML(yAxis); }
197 if (auto zAxis = node->GetChild("ZAxis")) { fZAxis.ImportFromXML(zAxis); }
198 if (auto xAxis = node->GetChild("Fill")) { fFill.ImportFromXML(xAxis); }
199 if (auto xAxis = node->GetChild("Marker")) { fMarker.ImportFromXML(xAxis); }
200 if (auto line = node->GetChild("Line")) { fLine.ImportFromXML(line); }
201 }
202
203} /* namespace Hal */
void SetAxis(Double_t titleSize, Double_t titleOffset, Double_t labelSize, Double_t labelOffset, Char_t x='x')
void SetColor(Color_t c)
static HistoStyle GetStyle(TString style)
void SetLineProperties(Color_t c, Style_t style, Double_t widgth)
void SetMarkerProperties(Color_t c, Int_t marker, Size_t size)
void CenterTitle(Bool_t center=kTRUE, Char_t='x')
void SetMax(Double_t val)
void SetMinMax(Double_t minVal, Double_t maxVal)
void SetRangeUser(Double_t min, Double_t max, Char_t ax)
void SetAntiColor(Bool_t safe=kFALSE)
void SetMin(Double_t val)
void SetTitles(TString histo, TString xAxis="", TString yAxis="", TString zAxis="")
Bool_t Find(Int_t bit) const
Definition Style.h:55