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

1 : brun 4083 // @(#)root/graf:$Name: $:$Id: TMultiGraph.cxx,v 1.8 2002/02/19 17:43:41 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 : 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 : brun 4037 // mg->Add(gr1,"lp");
35 :     // mg->Add(gr2,"cp");
36 :     // mg->Draw("a");
37 :     //
38 :     // The drawing option for each TGraph may be specified as an optional
39 :     // second argument of the Add function.
40 :     // If a draw option is specified, it will be used to draw the graph,
41 :     // otherwise the graph will be drawn with the option specified in
42 :     // TMultiGraph::Draw
43 : brun 754
44 :     //______________________________________________________________________________
45 :     TMultiGraph::TMultiGraph(): TNamed()
46 :     {
47 :     // TMultiGraph default constructor
48 :    
49 :     fGraphs = 0;
50 :     fHistogram = 0;
51 :     fMaximum = -1111;
52 :     fMinimum = -1111;
53 :     }
54 :    
55 :     //______________________________________________________________________________
56 :     TMultiGraph::TMultiGraph(const char *name, const char *title)
57 :     : TNamed(name,title)
58 :     {
59 :     // constructor with name and title
60 :     fGraphs = 0;
61 :     fHistogram = 0;
62 :     fMaximum = -1111;
63 :     fMinimum = -1111;
64 :     }
65 :    
66 :     //______________________________________________________________________________
67 :     TMultiGraph::~TMultiGraph()
68 :     {
69 :     // TMultiGraph destructor
70 :    
71 :    
72 :     if (!fGraphs) return;
73 :     fGraphs->Delete();
74 :     delete fGraphs;
75 :     fGraphs = 0;
76 :     delete fHistogram;
77 :     fHistogram = 0;
78 :     }
79 :    
80 :     //______________________________________________________________________________
81 : brun 4037 void TMultiGraph::Add(TGraph *graph, Option_t *chopt)
82 : brun 754 {
83 :     // add a new graph to the list of graphs
84 : rdm 3742
85 : brun 754 if (!fGraphs) fGraphs = new TList();
86 : brun 4037 fGraphs->Add(graph,chopt);
87 : brun 754 }
88 :    
89 :     //______________________________________________________________________________
90 :     void TMultiGraph::Browse(TBrowser *)
91 :     {
92 :     Draw("alp");
93 :     gPad->Update();
94 :     }
95 :    
96 :     //______________________________________________________________________________
97 :     Int_t TMultiGraph::DistancetoPrimitive(Int_t px, Int_t py)
98 :     {
99 :     // Compute distance from point px,py to each graph
100 :     //
101 :    
102 :     //*-*- Are we on the axis?
103 :     const Int_t kMaxDiff = 10;
104 :     Int_t distance = 9999;
105 :     if (fHistogram) {
106 :     distance = fHistogram->DistancetoPrimitive(px,py);
107 :     if (distance <= 0) return distance;
108 :     }
109 :    
110 :    
111 :     //*-*- Loop on the list of graphs
112 :     if (!fGraphs) return distance;
113 :     TGraph *g;
114 :     TIter next(fGraphs);
115 :     while ((g = (TGraph*) next())) {
116 :     Int_t dist = g->DistancetoPrimitive(px,py);
117 :     if (dist < kMaxDiff) {gPad->SetSelected(g); return dist;}
118 :     }
119 :     return distance;
120 :     }
121 :    
122 :     //______________________________________________________________________________
123 :     void TMultiGraph::Draw(Option_t *option)
124 :     {
125 :     //*-*-*-*-*-*-*-*-*-*-*Draw this multigraph with its current attributes*-*-*-*-*-*-*
126 :     //*-* ==========================================
127 :     //
128 :     // Options to draw a graph are described in TGraph::PainGraph
129 : brun 4037 //
130 :     // The drawing option for each TGraph may be specified as an optional
131 :     // second argument of the Add function.
132 :     // If a draw option is specified, it will be used to draw the graph,
133 :     // otherwise the graph will be drawn with the option specified in
134 :     // TMultiGraph::Draw
135 : brun 754
136 :     AppendPad(option);
137 :     }
138 :    
139 :     //______________________________________________________________________________
140 : brun 1205 TH1F *TMultiGraph::GetHistogram() const
141 : brun 754 {
142 :     // Returns a pointer to the histogram used to draw the axis
143 :     // Takes into account the two following cases.
144 :     // 1- option 'A' was specified in TMultiGraph::Draw. Return fHistogram
145 :     // 2- user had called TPad::DrawFrame. return pointer to hframe histogram
146 :    
147 :     if (fHistogram) return fHistogram;
148 :     if (!gPad) return 0;
149 :     gPad->Modified();
150 :     gPad->Update();
151 :     if (fHistogram) return fHistogram;
152 :     TH1F *h1 = (TH1F*)gPad->FindObject("hframe");
153 :     return h1;
154 :     }
155 :    
156 :     //______________________________________________________________________________
157 : brun 1205 TAxis *TMultiGraph::GetXaxis() const
158 : brun 754 {
159 :     // Get x axis of the graph.
160 :    
161 :     if (!gPad) return 0;
162 : brun 4083 TH1 *h = GetHistogram();
163 :     if (!h) return 0;
164 :     return h->GetXaxis();
165 : brun 754 }
166 :    
167 :     //______________________________________________________________________________
168 : brun 1205 TAxis *TMultiGraph::GetYaxis() const
169 : brun 754 {
170 :     // Get y axis of the graph.
171 :    
172 :     if (!gPad) return 0;
173 : brun 4083 TH1 *h = GetHistogram();
174 :     if (!h) return 0;
175 :     return h->GetYaxis();
176 : brun 754 }
177 :    
178 :     //______________________________________________________________________________
179 :     void TMultiGraph::Paint(Option_t *option)
180 :     {
181 :     // paint all the graphs of this multigraph
182 :    
183 :     char *l;
184 :     static char chopt[33];
185 :     Int_t nch = strlen(option);
186 : brun 1500 Int_t i;
187 :     for (i=0;i<nch;i++) chopt[i] = toupper(option[i]);
188 : brun 754 chopt[nch] = 0;
189 : brun 1500 Double_t *x, *y;
190 : brun 3575 TGraph *g;
191 : rdm 3742
192 : brun 754 l = strstr(chopt,"A");
193 :     if (l) {
194 :     *l = ' ';
195 :     TIter next(fGraphs);
196 :     Int_t npt = 100;
197 :     Double_t maximum, minimum, rwxmin, rwxmax, rwymin, rwymax, uxmin, uxmax, dx, dy;
198 : brun 3575 rwxmin = gPad->GetUxmin();
199 :     rwxmax = gPad->GetUxmax();
200 :     rwymin = gPad->GetUymin();
201 :     rwymax = gPad->GetUymax();
202 : brun 3799 char *xtitle = 0;
203 :     char *ytitle = 0;
204 :     Int_t firstx = 0;
205 :     Int_t lastx = 0;
206 :    
207 : brun 754 if (fHistogram) {
208 : brun 3575 //cleanup in case of a previous unzoom
209 :     if (fHistogram->GetMinimum() >= fHistogram->GetMaximum()) {
210 : brun 3799 Int_t nch = strlen(fHistogram->GetXaxis()->GetTitle());
211 :     firstx = fHistogram->GetXaxis()->GetFirst();
212 :     lastx = fHistogram->GetXaxis()->GetLast();
213 :     if (nch) {
214 :     xtitle = new char[nch+1];
215 :     strcpy(xtitle,fHistogram->GetXaxis()->GetTitle());
216 :     }
217 :     nch = strlen(fHistogram->GetYaxis()->GetTitle());
218 :     if (nch) {
219 :     ytitle = new char[nch+1];
220 :     strcpy(ytitle,fHistogram->GetYaxis()->GetTitle());
221 :     }
222 : brun 3575 delete fHistogram;
223 :     fHistogram = 0;
224 :     }
225 :     }
226 :     if (fHistogram) {
227 :     minimum = fHistogram->GetYaxis()->GetXmin();
228 :     maximum = fHistogram->GetYaxis()->GetXmax();
229 :     uxmin = gPad->PadtoX(rwxmin);
230 :     uxmax = gPad->PadtoX(rwxmax);
231 : brun 754 } else {
232 :     rwxmin = 1e100;
233 :     rwxmax = -rwxmin;
234 :     rwymin = rwxmin;
235 :     rwymax = -rwymin;
236 :     while ((g = (TGraph*) next())) {
237 : brun 1500 Int_t npoints = g->GetN();
238 :     x = g->GetX();
239 :     y = g->GetY();
240 :     for (i=0;i<npoints;i++) {
241 :     if (x[i] < rwxmin) rwxmin = x[i];
242 :     if (x[i] > rwxmax) rwxmax = x[i];
243 :     if (y[i] < rwymin) rwymin = y[i];
244 :     if (y[i] > rwymax) rwymax = y[i];
245 :     }
246 : brun 754 g->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
247 :     if (g->GetN() > npt) npt = g->GetN();
248 :     }
249 :     if (rwxmin == rwxmax) rwxmax += 1.;
250 :     if (rwymin == rwymax) rwymax += 1.;
251 : brun 3575 dx = 0.05*(rwxmax-rwxmin);
252 :     dy = 0.05*(rwymax-rwymin);
253 : brun 754 uxmin = rwxmin - dx;
254 :     uxmax = rwxmax + dx;
255 :     minimum = rwymin - dy;
256 :     maximum = rwymax + dy;
257 :     }
258 :    
259 :     if (fMinimum != -1111) rwymin = minimum = fMinimum;
260 :     if (fMaximum != -1111) rwymax = maximum = fMaximum;
261 :     if (uxmin < 0 && rwxmin >= 0) {
262 :     if (gPad->GetLogx()) uxmin = 0.9*rwxmin;
263 : brun 3575 //else uxmin = 0;
264 : brun 754 }
265 :     if (uxmax > 0 && rwxmax <= 0) {
266 :     if (gPad->GetLogx()) uxmax = 1.1*rwxmax;
267 : brun 3575 //else uxmax = 0;
268 : brun 754 }
269 :     if (minimum < 0 && rwymin >= 0) {
270 :     if(gPad->GetLogy()) minimum = 0.9*rwymin;
271 : brun 3575 //else minimum = 0;
272 : brun 754 }
273 :     if (maximum > 0 && rwymax <= 0) {
274 :     if(gPad->GetLogy()) maximum = 1.1*rwymax;
275 : brun 3575 //else maximum = 0;
276 : brun 754 }
277 :     if (minimum <= 0 && gPad->GetLogy()) minimum = 0.001*maximum;
278 :     if (uxmin <= 0 && gPad->GetLogx()) {
279 :     if (uxmax > 1000) uxmin = 1;
280 :     else uxmin = 0.001*uxmax;
281 :     }
282 :     rwymin = minimum;
283 :     rwymax = maximum;
284 :     if (fHistogram) {
285 : brun 3575 fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
286 :     }
287 :    
288 :     //*-*- Create a temporary histogram to draw the axis
289 :     if (!fHistogram) {
290 :     // the graph is created with at least as many channels as there are points
291 :     // to permit zooming on the full range
292 :     rwxmin = uxmin;
293 :     rwxmax = uxmax;
294 :     fHistogram = new TH1F(GetName(),GetTitle(),npt,rwxmin,rwxmax);
295 :     if (!fHistogram) return;
296 : brun 754 fHistogram->SetMinimum(rwymin);
297 : brun 3575 fHistogram->SetBit(TH1::kNoStats);
298 : brun 754 fHistogram->SetMaximum(rwymax);
299 : brun 3575 fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
300 :     fHistogram->SetDirectory(0);
301 : brun 3799 if (xtitle) {fHistogram->GetXaxis()->SetTitle(xtitle); delete [] xtitle;}
302 :     if (ytitle) {fHistogram->GetYaxis()->SetTitle(ytitle); delete [] ytitle;}
303 :     if (firstx != lastx) fHistogram->GetXaxis()->SetRange(firstx,lastx);
304 : brun 754 }
305 : brun 4037 fHistogram->Paint("0");
306 : brun 3575 }
307 : rdm 3742
308 : brun 754 if (fGraphs) {
309 : brun 4037 TObjOptLink *lnk = (TObjOptLink*)fGraphs->FirstLink();
310 :     TObject *obj;
311 :    
312 :     while (lnk) {
313 :     obj = lnk->GetObject();
314 :     if (strlen(lnk->GetOption())) obj->Paint(lnk->GetOption());
315 :     else obj->Paint(chopt);
316 :     lnk = (TObjOptLink*)lnk->Next();
317 :     }
318 : brun 754 }
319 :     }
320 :    
321 :     //______________________________________________________________________________
322 : brun 1205 void TMultiGraph::Print(Option_t *option) const
323 : brun 754 {
324 :     // Print the list of graphs
325 :    
326 :     TGraph *g;
327 :     if (fGraphs) {
328 :     TIter next(fGraphs);
329 :     while ((g = (TGraph*) next())) {
330 :     g->Print(option);
331 :     }
332 :     }
333 :     }
334 :    
335 :     //______________________________________________________________________________
336 :     void TMultiGraph::SavePrimitive(ofstream &out, Option_t *option)
337 :     {
338 :     // Save primitive as a C++ statement(s) on output stream out
339 :    
340 :     char quote = '"';
341 :     out<<" "<<endl;
342 :     if (gROOT->ClassSaved(TMultiGraph::Class())) {
343 :     out<<" ";
344 :     } else {
345 :     out<<" TMultiGraph *";
346 :     }
347 :     out<<"multigraph = new TMultiGraph();"<<endl;
348 :     out<<" multigraph->SetName("<<quote<<GetName()<<quote<<");"<<endl;
349 :     out<<" multigraph->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
350 :    
351 :     TGraph *g;
352 :     if (fGraphs) {
353 :     TIter next(fGraphs);
354 :     while ((g = (TGraph*) next())) {
355 :     g->SavePrimitive(out,"multigraph");
356 :     }
357 :     }
358 :     out<<" multigraph->Draw("
359 :     <<quote<<option<<quote<<");"<<endl;
360 :     }
361 :    
362 :     //______________________________________________________________________________
363 :     void TMultiGraph::SetMaximum(Double_t maximum)
364 :     {
365 :     fMaximum = maximum;
366 :     if (fHistogram) fHistogram->SetMaximum(maximum);
367 :     }
368 :    
369 :     //______________________________________________________________________________
370 :     void TMultiGraph::SetMinimum(Double_t minimum)
371 :     {
372 :     fMinimum = minimum;
373 :     if (fHistogram) fHistogram->SetMinimum(minimum);
374 :     }

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9