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

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

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9