Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
MultiGraph.cxx
1/*
2 * HalMultiGraph.cxx
3 *
4 * Created on: 5 mar 2016
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9
10#include "MultiGraph.h"
11
12#include "Cout.h"
13#include "StdHist.h"
14
15#include <TAttAxis.h>
16#include <TAttLine.h>
17#include <TAttMarker.h>
18#include <TAxis.h>
19#include <TGraphErrors.h>
20#include <TH1D.h>
21#include <TMultiGraph.h>
22#include <TString.h>
23#include <TVirtualPad.h>
24#include <iostream>
25
26namespace Hal {
27
28 MultiGraph::MultiGraph() : fN(0), fGraphErrors(nullptr), fMultiGraph(new TMultiGraph()), fXaxis(nullptr), fYaxis(nullptr) {
29 fMin[0] = fMin[1] = 0;
30 fMax[0] = fMax[1] = 0;
31 }
32 MultiGraph::MultiGraph(Int_t graphs, Option_t* opt) : MultiGraph() { MakeGraphs(graphs, opt); }
33
34 void MultiGraph::MakeGraph(Option_t* opt) {
35 TGraphErrors** temp = fGraphErrors;
36 fGraphErrors = new TGraphErrors*[fN + 1];
37 for (int i = 0; i < fN; i++) {
38 fGraphErrors[i] = temp[i];
39 }
40 delete[] temp;
41 fGraphErrors[fN] = new TGraphErrors();
42 fMultiGraph->Add(fGraphErrors[fN], opt);
43 fN++;
44 }
45
46 void MultiGraph::SetXRange(Double_t min, Double_t max) {
47 fMin[0] = min;
48 fMax[0] = max;
49 }
50
51 void MultiGraph::SetYRange(Double_t min, Double_t max) {
52 fMin[1] = min;
53 fMax[1] = max;
54 }
55
56 void MultiGraph::SetPoint(Int_t graph, Int_t point, Double_t x, Double_t y) { fGraphErrors[graph]->SetPoint(point, x, y); }
57
58 void MultiGraph::SetPoint(Int_t graph, Int_t point, Double_t x, Double_t ex, Double_t y, Double_t ey) {
59 fGraphErrors[graph]->SetPoint(point, x, y);
60 fGraphErrors[graph]->SetPointError(point, ex, ey);
61 }
62
63 TGraphErrors* MultiGraph::GetGraph(Int_t no) const { return fGraphErrors[no]; }
64
65 void MultiGraph::Draw(Option_t* opt) {
66 fDrawn = kTRUE;
67 TString option = opt;
68 if (option.Contains("A") == kFALSE) option = "A" + option;
69 fMultiGraph->Draw(option);
70 if (fXaxis) Hal::Std::CopyAxisProp(fXaxis, fMultiGraph->GetXaxis());
71 if (fYaxis) Hal::Std::CopyAxisProp(fYaxis, fMultiGraph->GetYaxis());
72 if (fMin[0] != fMax[0]) {
73 fMultiGraph->GetXaxis()->SetRangeUser(fMin[0], fMax[0]);
74 fMultiGraph->GetXaxis()->SetLimits(fMin[0], fMax[0]); // why?
75 }
76 if (fMin[1] != fMax[1]) fMultiGraph->GetYaxis()->SetRangeUser(fMin[1], fMax[1]);
77 gPad->Update();
78 gPad->RedrawAxis();
79 }
80
81 void MultiGraph::SetMarkerStyle(Style_t marker, Int_t no) {
82 if (no < 0) {
83 for (int i = 0; i < fN; i++)
84 fGraphErrors[i]->SetMarkerStyle(marker);
85 } else if (no < fN) {
86 fGraphErrors[no]->SetMarkerStyle(marker);
87 } else {
88 std::cout << __FILE__ << " " << __LINE__ << "Cannot SetMarkerStyle to graph " << no << std::endl;
89 }
90 }
91
92 void MultiGraph::SetMarkerSize(Size_t size, Int_t no) {
93 if (no < 0) {
94 for (int i = 0; i < fN; i++)
95 fGraphErrors[i]->SetMarkerSize(size);
96 } else if (no < fN) {
97 fGraphErrors[no]->SetMarkerSize(size);
98 } else {
99 std::cout << __FILE__ << " " << __LINE__ << "Cannot SetMarkerSize to graph " << no << std::endl;
100 }
101 }
102
103 void MultiGraph::SetMarkerColor(Color_t color, Int_t no) {
104 if (no < 0) {
105 for (int i = 0; i < fN; i++)
106 fGraphErrors[i]->SetMarkerColor(color);
107 } else if (no < fN) {
108 fGraphErrors[no]->SetMarkerColor(color);
109 } else {
110 std::cout << __FILE__ << " " << __LINE__ << "Cannot SetMarkerColor to graph " << no << std::endl;
111 }
112 }
113
114 void MultiGraph::SetLineWidth(Width_t width, Int_t no) {
115 if (no < 0) {
116 for (int i = 0; i < fN; i++)
117 fGraphErrors[i]->SetLineWidth(width);
118 } else if (no < fN) {
119 fGraphErrors[no]->SetLineWidth(width);
120 } else {
121 std::cout << __FILE__ << " " << __LINE__ << "Cannot SetLineWidth to graph " << no << std::endl;
122 }
123 }
124
125 void MultiGraph::SetLineColor(Color_t color, Int_t no) {
126 if (no < 0) {
127 for (int i = 0; i < fN; i++)
128 fGraphErrors[i]->SetLineColor(color);
129 } else if (no < fN) {
130 fGraphErrors[no]->SetLineColor(color);
131 } else {
132 std::cout << __FILE__ << " " << __LINE__ << "Cannot SetLineColor to graph " << no << std::endl;
133 }
134 }
135
136 void MultiGraph::SetLineStyle(Style_t style, Int_t no) {
137 if (no < 0) {
138 for (int i = 0; i < fN; i++)
139 fGraphErrors[i]->SetLineStyle(style);
140 } else if (no < fN) {
141 fGraphErrors[no]->SetLineStyle(style);
142 }
143 }
144
145 void MultiGraph::SetLineAttributes(Style_t style, Width_t width, Color_t color, Int_t no) {
146 SetLineStyle(style, no);
147 SetLineWidth(width, no);
148 SetLineColor(color, no);
149 }
150
151 void MultiGraph::SetMarkerAttributes(Style_t style, Size_t size, Color_t color, Int_t no) {
152 SetMarkerStyle(style, no);
153 SetMarkerSize(size, no);
154 SetMarkerColor(color, no);
155 }
156
157 void MultiGraph::MakeGraphs(Int_t no, Option_t* opt) {
158 for (int i = 0; i < no; i++) {
159 MakeGraph(opt);
160 }
161 }
162
163 TMultiGraph* MultiGraph::GetMutliGraph() const { return fMultiGraph; }
164
165 MultiGraph::~MultiGraph() {
166 delete fMultiGraph;
167 if (fGraphErrors) {
168 for (int i = 0; i < fN; i++) {
169 delete fGraphErrors[i];
170 }
171 }
172 delete[] fGraphErrors;
173 if (fXaxis) delete fXaxis;
174 if (fYaxis) delete fYaxis;
175 }
176
178 if (fXaxis) return fXaxis;
179 fXaxis = new TAxis();
180 return fXaxis;
181 }
182
184 if (fYaxis) return fYaxis;
185 fYaxis = new TAxis();
186 return fYaxis;
187 }
188
189 void MultiGraph::AddHistogram(const TH1& h, Int_t no) {
190 if (no == -1) {
191 MakeGraph();
192 no = GetNGraphs() - 1;
193 }
194 auto axis = h.GetXaxis();
195 for (int i = 0; i <= axis->GetNbins(); i++) {
196 SetPoint(no, i, axis->GetBinCenter(i), 0, h.GetBinContent(i), h.GetBinError(i));
197 }
198 }
199
200} // namespace Hal
void MakeGraphs(Int_t no, Option_t *opt="P")
void MakeGraph(Option_t *opt="P")
void SetYRange(Double_t min, Double_t max)
void SetLineColor(Color_t color, Int_t no=-1)
TGraphErrors * GetGraph(Int_t no) const
TMultiGraph * GetMutliGraph() const
TAxis * GetYaxis()
void SetLineAttributes(Style_t style, Width_t width, Color_t color, Int_t no=-1)
void SetXRange(Double_t min, Double_t max)
TAxis * GetXaxis()
void SetMarkerColor(Color_t color, Int_t no=-1)
void Draw(Option_t *opt="")
void SetMarkerStyle(Style_t marker, Int_t no=-1)
void SetLineStyle(Style_t style, Int_t no=-1)
Int_t GetNGraphs() const
Definition MultiGraph.h:168
void SetMarkerSize(Size_t size, Int_t no=-1)
void SetMarkerAttributes(Style_t style, Size_t size, Color_t color, Int_t no=-1)
void SetLineWidth(Width_t width, Int_t no=-1)
void AddHistogram(const TH1 &h, Int_t no=-1)
void SetPoint(Int_t graph, Int_t point, Double_t x, Double_t y)