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

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

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9