[root] / trunk / hist / hist / src / TMultiGraph.cxx Repository:
ViewVC logotype

Annotation of /trunk/hist/hist/src/TMultiGraph.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1500 - (view) (download) (as text)
Original Path: trunk/graf/src/TMultiGraph.cxx

1 : brun 1500 // @(#)root/graf:$Name: $:$Id: TMultiGraph.cxx,v 1.2 2000/12/13 15:13:50 brun Exp $
2 : brun 754 // Author: Rene Brun 12/10/2000
3 :    
4 :     /*************************************************************************
5 :     * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 :     * All rights reserved. *
7 :     * *
8 :     * For the licensing terms see $ROOTSYS/LICENSE. *
9 :     * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 :     *************************************************************************/
11 :    
12 :     #include "TROOT.h"
13 :     #include "TMultiGraph.h"
14 :     #include "TGraph.h"
15 :     #include "TH1.h"
16 :     #include "TVirtualPad.h"
17 :    
18 :     #include <ctype.h>
19 :     #include <fstream.h>
20 :    
21 :    
22 :     ClassImp(TMultiGraph)
23 :    
24 :     //______________________________________________________________________________
25 :     //
26 :     // A TMultiGraph is a collection of TGraph (or derived) objects
27 :     // Use TMultiGraph::Add to add a new graph to the list.
28 :     // The TMultiGraph owns the objects in the list.
29 :     // Drawing options are the same as for TGraph
30 :     // Example;
31 :     // TGraph *gr1 = new TGraph(...
32 :     // TGraphErrors *gr2 = new TGraphErrors(...
33 :     // TMultiGraph *mg = new TMultiGraph();
34 :     // mg->Add(gr1);
35 :     // mg->Add(gr2);
36 :     // mg->Draw("alp");
37 :    
38 :     //______________________________________________________________________________
39 :     TMultiGraph::TMultiGraph(): TNamed()
40 :     {
41 :     // TMultiGraph default constructor
42 :    
43 :     fGraphs = 0;
44 :     fHistogram = 0;
45 :     fMaximum = -1111;
46 :     fMinimum = -1111;
47 :     }
48 :    
49 :     //______________________________________________________________________________
50 :     TMultiGraph::TMultiGraph(const char *name, const char *title)
51 :     : TNamed(name,title)
52 :     {
53 :     // constructor with name and title
54 :     fGraphs = 0;
55 :     fHistogram = 0;
56 :     fMaximum = -1111;
57 :     fMinimum = -1111;
58 :     }
59 :    
60 :     //______________________________________________________________________________
61 :     TMultiGraph::~TMultiGraph()
62 :     {
63 :     // TMultiGraph destructor
64 :    
65 :    
66 :     if (!fGraphs) return;
67 :     fGraphs->Delete();
68 :     delete fGraphs;
69 :     fGraphs = 0;
70 :     delete fHistogram;
71 :     fHistogram = 0;
72 :     }
73 :    
74 :     //______________________________________________________________________________
75 :     void TMultiGraph::Add(TGraph *graph)
76 :     {
77 :     // add a new graph to the list of graphs
78 :    
79 :     if (!fGraphs) fGraphs = new TList();
80 :     fGraphs->Add(graph);
81 :     }
82 :    
83 :     //______________________________________________________________________________
84 :     void TMultiGraph::Browse(TBrowser *)
85 :     {
86 :     Draw("alp");
87 :     gPad->Update();
88 :     }
89 :    
90 :     //______________________________________________________________________________
91 :     Int_t TMultiGraph::DistancetoPrimitive(Int_t px, Int_t py)
92 :     {
93 :     // Compute distance from point px,py to each graph
94 :     //
95 :    
96 :     //*-*- Are we on the axis?
97 :     const Int_t kMaxDiff = 10;
98 :     Int_t distance = 9999;
99 :     if (fHistogram) {
100 :     distance = fHistogram->DistancetoPrimitive(px,py);
101 :     if (distance <= 0) return distance;
102 :     }
103 :    
104 :    
105 :     //*-*- Loop on the list of graphs
106 :     if (!fGraphs) return distance;
107 :     TGraph *g;
108 :     TIter next(fGraphs);
109 :     while ((g = (TGraph*) next())) {
110 :     Int_t dist = g->DistancetoPrimitive(px,py);
111 :     if (dist < kMaxDiff) {gPad->SetSelected(g); return dist;}
112 :     }
113 :     return distance;
114 :     }
115 :    
116 :     //______________________________________________________________________________
117 :     void TMultiGraph::Draw(Option_t *option)
118 :     {
119 :     //*-*-*-*-*-*-*-*-*-*-*Draw this multigraph with its current attributes*-*-*-*-*-*-*
120 :     //*-* ==========================================
121 :     //
122 :     // Options to draw a graph are described in TGraph::PainGraph
123 :    
124 :     AppendPad(option);
125 :     }
126 :    
127 :     //______________________________________________________________________________
128 : brun 1205 TH1F *TMultiGraph::GetHistogram() const
129 : brun 754 {
130 :     // Returns a pointer to the histogram used to draw the axis
131 :     // Takes into account the two following cases.
132 :     // 1- option 'A' was specified in TMultiGraph::Draw. Return fHistogram
133 :     // 2- user had called TPad::DrawFrame. return pointer to hframe histogram
134 :    
135 :     if (fHistogram) return fHistogram;
136 :     if (!gPad) return 0;
137 :     gPad->Modified();
138 :     gPad->Update();
139 :     if (fHistogram) return fHistogram;
140 :     TH1F *h1 = (TH1F*)gPad->FindObject("hframe");
141 :     return h1;
142 :     }
143 :    
144 :     //______________________________________________________________________________
145 : brun 1205 TAxis *TMultiGraph::GetXaxis() const
146 : brun 754 {
147 :     // Get x axis of the graph.
148 :    
149 :     if (!gPad) return 0;
150 :     return GetHistogram()->GetXaxis();
151 :     }
152 :    
153 :     //______________________________________________________________________________
154 : brun 1205 TAxis *TMultiGraph::GetYaxis() const
155 : brun 754 {
156 :     // Get y axis of the graph.
157 :    
158 :     if (!gPad) return 0;
159 :     return GetHistogram()->GetYaxis();
160 :     }
161 :    
162 :     //______________________________________________________________________________
163 :     void TMultiGraph::Paint(Option_t *option)
164 :     {
165 :     // paint all the graphs of this multigraph
166 :    
167 :     char *l;
168 :     static char chopt[33];
169 :     Int_t nch = strlen(option);
170 : brun 1500 Int_t i;
171 :     for (i=0;i<nch;i++) chopt[i] = toupper(option[i]);
172 : brun 754 chopt[nch] = 0;
173 : brun 1500 Double_t *x, *y;
174 :    
175 : brun 754 l = strstr(chopt,"A");
176 :     if (l) {
177 :     *l = ' ';
178 :     TGraph *g;
179 :     TIter next(fGraphs);
180 :     Int_t npt = 100;
181 :     Double_t maximum, minimum, rwxmin, rwxmax, rwymin, rwymax, uxmin, uxmax, dx, dy;
182 :     if (fHistogram) {
183 :     rwxmin = gPad->GetUxmin();
184 :     rwxmax = gPad->GetUxmax();
185 :     rwymin = gPad->GetUymin();
186 :     rwymax = gPad->GetUymax();
187 :     minimum = fHistogram->GetMinimumStored();
188 :     maximum = fHistogram->GetMaximumStored();
189 :     if (minimum == -1111) minimum = fHistogram->GetYaxis()->GetXmin();
190 :     if (maximum == -1111) maximum = fHistogram->GetYaxis()->GetXmax();
191 :     uxmin = gPad->PadtoX(rwxmin);
192 :     uxmax = gPad->PadtoX(rwxmax);
193 :     } else {
194 :     rwxmin = 1e100;
195 :     rwxmax = -rwxmin;
196 :     rwymin = rwxmin;
197 :     rwymax = -rwymin;
198 :     while ((g = (TGraph*) next())) {
199 : brun 1500 Int_t npoints = g->GetN();
200 :     x = g->GetX();
201 :     y = g->GetY();
202 :     for (i=0;i<npoints;i++) {
203 :     if (x[i] < rwxmin) rwxmin = x[i];
204 :     if (x[i] > rwxmax) rwxmax = x[i];
205 :     if (y[i] < rwymin) rwymin = y[i];
206 :     if (y[i] > rwymax) rwymax = y[i];
207 :     }
208 : brun 754 g->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
209 :     if (g->GetN() > npt) npt = g->GetN();
210 :     }
211 :     if (rwxmin == rwxmax) rwxmax += 1.;
212 :     if (rwymin == rwymax) rwymax += 1.;
213 :     dx = 0.1*(rwxmax-rwxmin);
214 :     dy = 0.1*(rwymax-rwymin);
215 :     uxmin = rwxmin - dx;
216 :     uxmax = rwxmax + dx;
217 :     minimum = rwymin - dy;
218 :     maximum = rwymax + dy;
219 :     }
220 :    
221 :     if (fMinimum != -1111) rwymin = minimum = fMinimum;
222 :     if (fMaximum != -1111) rwymax = maximum = fMaximum;
223 :     if (uxmin < 0 && rwxmin >= 0) {
224 :     if (gPad->GetLogx()) uxmin = 0.9*rwxmin;
225 :     else uxmin = 0;
226 :     }
227 :     if (uxmax > 0 && rwxmax <= 0) {
228 :     if (gPad->GetLogx()) uxmax = 1.1*rwxmax;
229 :     else uxmax = 0;
230 :     }
231 :     if (minimum < 0 && rwymin >= 0) {
232 :     if(gPad->GetLogy()) minimum = 0.9*rwymin;
233 :     else minimum = 0;
234 :     }
235 :     if (maximum > 0 && rwymax <= 0) {
236 :     if(gPad->GetLogy()) maximum = 1.1*rwymax;
237 :     else maximum = 0;
238 :     }
239 :     if (minimum <= 0 && gPad->GetLogy()) minimum = 0.001*maximum;
240 :     if (uxmin <= 0 && gPad->GetLogx()) {
241 :     if (uxmax > 1000) uxmin = 1;
242 :     else uxmin = 0.001*uxmax;
243 :     }
244 :     rwymin = minimum;
245 :     rwymax = maximum;
246 :     if (fHistogram) {
247 :     fHistogram->SetMinimum(rwymin);
248 :     fHistogram->SetMaximum(rwymax);
249 :     }
250 :    
251 :     //*-*- Create a temporary histogram to draw the axis
252 :     if (!fHistogram) {
253 :     // the graph is created with at least as many channels as there are points
254 :     // to permit zooming on the full range
255 :     rwxmin = uxmin;
256 :     rwxmax = uxmax;
257 :     fHistogram = new TH1F(GetName(),GetTitle(),npt,rwxmin,rwxmax);
258 :     if (!fHistogram) return;
259 :     fHistogram->SetMinimum(rwymin);
260 :     fHistogram->SetBit(TH1::kNoStats);
261 :     fHistogram->SetMaximum(rwymax);
262 :     fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
263 :     fHistogram->SetDirectory(0);
264 :     }
265 :     fHistogram->Paint();
266 :     }
267 :    
268 :     TGraph *g;
269 :     if (fGraphs) {
270 :     TIter next(fGraphs);
271 :     while ((g = (TGraph*) next())) {
272 :     g->Paint(chopt);
273 :     }
274 :     }
275 :     }
276 :    
277 :     //______________________________________________________________________________
278 : brun 1205 void TMultiGraph::Print(Option_t *option) const
279 : brun 754 {
280 :     // Print the list of graphs
281 :    
282 :     TGraph *g;
283 :     if (fGraphs) {
284 :     TIter next(fGraphs);
285 :     while ((g = (TGraph*) next())) {
286 :     g->Print(option);
287 :     }
288 :     }
289 :     }
290 :    
291 :     //______________________________________________________________________________
292 :     void TMultiGraph::SavePrimitive(ofstream &out, Option_t *option)
293 :     {
294 :     // Save primitive as a C++ statement(s) on output stream out
295 :    
296 :     char quote = '"';
297 :     out<<" "<<endl;
298 :     if (gROOT->ClassSaved(TMultiGraph::Class())) {
299 :     out<<" ";
300 :     } else {
301 :     out<<" TMultiGraph *";
302 :     }
303 :     out<<"multigraph = new TMultiGraph();"<<endl;
304 :     out<<" multigraph->SetName("<<quote<<GetName()<<quote<<");"<<endl;
305 :     out<<" multigraph->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
306 :    
307 :     TGraph *g;
308 :     if (fGraphs) {
309 :     TIter next(fGraphs);
310 :     while ((g = (TGraph*) next())) {
311 :     g->SavePrimitive(out,"multigraph");
312 :     }
313 :     }
314 :     out<<" multigraph->Draw("
315 :     <<quote<<option<<quote<<");"<<endl;
316 :     }
317 :    
318 :     //______________________________________________________________________________
319 :     void TMultiGraph::SetMaximum(Double_t maximum)
320 :     {
321 :     fMaximum = maximum;
322 :     if (fHistogram) fHistogram->SetMaximum(maximum);
323 :     }
324 :    
325 :     //______________________________________________________________________________
326 :     void TMultiGraph::SetMinimum(Double_t minimum)
327 :     {
328 :     fMinimum = minimum;
329 :     if (fHistogram) fHistogram->SetMinimum(minimum);
330 :     }

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9