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

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

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9