[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 3799 - (view) (download) (as text)
Original Path: trunk/graf/src/TMultiGraph.cxx

1 : brun 3799 // @(#)root/graf:$Name: $:$Id: TMultiGraph.cxx,v 1.6 2002/01/24 11:39:28 rdm 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 : rdm 3748 #include "Riostream.h"
18 : brun 754
19 :     #include <ctype.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 : rdm 3742
79 : brun 754 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 : brun 3575 TGraph *g;
175 : rdm 3742
176 : brun 754 l = strstr(chopt,"A");
177 :     if (l) {
178 :     *l = ' ';
179 :     TIter next(fGraphs);
180 :     Int_t npt = 100;
181 :     Double_t maximum, minimum, rwxmin, rwxmax, rwymin, rwymax, uxmin, uxmax, dx, dy;
182 : brun 3575 rwxmin = gPad->GetUxmin();
183 :     rwxmax = gPad->GetUxmax();
184 :     rwymin = gPad->GetUymin();
185 :     rwymax = gPad->GetUymax();
186 : brun 3799 char *xtitle = 0;
187 :     char *ytitle = 0;
188 :     Int_t firstx = 0;
189 :     Int_t lastx = 0;
190 :    
191 : brun 754 if (fHistogram) {
192 : brun 3575 //cleanup in case of a previous unzoom
193 :     if (fHistogram->GetMinimum() >= fHistogram->GetMaximum()) {
194 : brun 3799 Int_t nch = strlen(fHistogram->GetXaxis()->GetTitle());
195 :     firstx = fHistogram->GetXaxis()->GetFirst();
196 :     lastx = fHistogram->GetXaxis()->GetLast();
197 :     if (nch) {
198 :     xtitle = new char[nch+1];
199 :     strcpy(xtitle,fHistogram->GetXaxis()->GetTitle());
200 :     }
201 :     nch = strlen(fHistogram->GetYaxis()->GetTitle());
202 :     if (nch) {
203 :     ytitle = new char[nch+1];
204 :     strcpy(ytitle,fHistogram->GetYaxis()->GetTitle());
205 :     }
206 : brun 3575 delete fHistogram;
207 :     fHistogram = 0;
208 :     }
209 :     }
210 :     if (fHistogram) {
211 :     minimum = fHistogram->GetYaxis()->GetXmin();
212 :     maximum = fHistogram->GetYaxis()->GetXmax();
213 :     uxmin = gPad->PadtoX(rwxmin);
214 :     uxmax = gPad->PadtoX(rwxmax);
215 : brun 754 } else {
216 :     rwxmin = 1e100;
217 :     rwxmax = -rwxmin;
218 :     rwymin = rwxmin;
219 :     rwymax = -rwymin;
220 :     while ((g = (TGraph*) next())) {
221 : brun 1500 Int_t npoints = g->GetN();
222 :     x = g->GetX();
223 :     y = g->GetY();
224 :     for (i=0;i<npoints;i++) {
225 :     if (x[i] < rwxmin) rwxmin = x[i];
226 :     if (x[i] > rwxmax) rwxmax = x[i];
227 :     if (y[i] < rwymin) rwymin = y[i];
228 :     if (y[i] > rwymax) rwymax = y[i];
229 :     }
230 : brun 754 g->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
231 :     if (g->GetN() > npt) npt = g->GetN();
232 :     }
233 :     if (rwxmin == rwxmax) rwxmax += 1.;
234 :     if (rwymin == rwymax) rwymax += 1.;
235 : brun 3575 dx = 0.05*(rwxmax-rwxmin);
236 :     dy = 0.05*(rwymax-rwymin);
237 : brun 754 uxmin = rwxmin - dx;
238 :     uxmax = rwxmax + dx;
239 :     minimum = rwymin - dy;
240 :     maximum = rwymax + dy;
241 :     }
242 :    
243 :     if (fMinimum != -1111) rwymin = minimum = fMinimum;
244 :     if (fMaximum != -1111) rwymax = maximum = fMaximum;
245 :     if (uxmin < 0 && rwxmin >= 0) {
246 :     if (gPad->GetLogx()) uxmin = 0.9*rwxmin;
247 : brun 3575 //else uxmin = 0;
248 : brun 754 }
249 :     if (uxmax > 0 && rwxmax <= 0) {
250 :     if (gPad->GetLogx()) uxmax = 1.1*rwxmax;
251 : brun 3575 //else uxmax = 0;
252 : brun 754 }
253 :     if (minimum < 0 && rwymin >= 0) {
254 :     if(gPad->GetLogy()) minimum = 0.9*rwymin;
255 : brun 3575 //else minimum = 0;
256 : brun 754 }
257 :     if (maximum > 0 && rwymax <= 0) {
258 :     if(gPad->GetLogy()) maximum = 1.1*rwymax;
259 : brun 3575 //else maximum = 0;
260 : brun 754 }
261 :     if (minimum <= 0 && gPad->GetLogy()) minimum = 0.001*maximum;
262 :     if (uxmin <= 0 && gPad->GetLogx()) {
263 :     if (uxmax > 1000) uxmin = 1;
264 :     else uxmin = 0.001*uxmax;
265 :     }
266 :     rwymin = minimum;
267 :     rwymax = maximum;
268 :     if (fHistogram) {
269 : brun 3575 fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
270 :     }
271 :    
272 :     //*-*- Create a temporary histogram to draw the axis
273 :     if (!fHistogram) {
274 :     // the graph is created with at least as many channels as there are points
275 :     // to permit zooming on the full range
276 :     rwxmin = uxmin;
277 :     rwxmax = uxmax;
278 :     fHistogram = new TH1F(GetName(),GetTitle(),npt,rwxmin,rwxmax);
279 :     if (!fHistogram) return;
280 : brun 754 fHistogram->SetMinimum(rwymin);
281 : brun 3575 fHistogram->SetBit(TH1::kNoStats);
282 : brun 754 fHistogram->SetMaximum(rwymax);
283 : brun 3575 fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
284 :     fHistogram->SetDirectory(0);
285 : brun 3799 if (xtitle) {fHistogram->GetXaxis()->SetTitle(xtitle); delete [] xtitle;}
286 :     if (ytitle) {fHistogram->GetYaxis()->SetTitle(ytitle); delete [] ytitle;}
287 :     if (firstx != lastx) fHistogram->GetXaxis()->SetRange(firstx,lastx);
288 : brun 754 }
289 : brun 3575 fHistogram->Paint();
290 :     }
291 : rdm 3742
292 : brun 754 if (fGraphs) {
293 :     TIter next(fGraphs);
294 :     while ((g = (TGraph*) next())) {
295 :     g->Paint(chopt);
296 :     }
297 :     }
298 :     }
299 :    
300 :     //______________________________________________________________________________
301 : brun 1205 void TMultiGraph::Print(Option_t *option) const
302 : brun 754 {
303 :     // Print the list of graphs
304 :    
305 :     TGraph *g;
306 :     if (fGraphs) {
307 :     TIter next(fGraphs);
308 :     while ((g = (TGraph*) next())) {
309 :     g->Print(option);
310 :     }
311 :     }
312 :     }
313 :    
314 :     //______________________________________________________________________________
315 :     void TMultiGraph::SavePrimitive(ofstream &out, Option_t *option)
316 :     {
317 :     // Save primitive as a C++ statement(s) on output stream out
318 :    
319 :     char quote = '"';
320 :     out<<" "<<endl;
321 :     if (gROOT->ClassSaved(TMultiGraph::Class())) {
322 :     out<<" ";
323 :     } else {
324 :     out<<" TMultiGraph *";
325 :     }
326 :     out<<"multigraph = new TMultiGraph();"<<endl;
327 :     out<<" multigraph->SetName("<<quote<<GetName()<<quote<<");"<<endl;
328 :     out<<" multigraph->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
329 :    
330 :     TGraph *g;
331 :     if (fGraphs) {
332 :     TIter next(fGraphs);
333 :     while ((g = (TGraph*) next())) {
334 :     g->SavePrimitive(out,"multigraph");
335 :     }
336 :     }
337 :     out<<" multigraph->Draw("
338 :     <<quote<<option<<quote<<");"<<endl;
339 :     }
340 :    
341 :     //______________________________________________________________________________
342 :     void TMultiGraph::SetMaximum(Double_t maximum)
343 :     {
344 :     fMaximum = maximum;
345 :     if (fHistogram) fHistogram->SetMaximum(maximum);
346 :     }
347 :    
348 :     //______________________________________________________________________________
349 :     void TMultiGraph::SetMinimum(Double_t minimum)
350 :     {
351 :     fMinimum = minimum;
352 :     if (fHistogram) fHistogram->SetMinimum(minimum);
353 :     }

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9