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

1 : brun 9010 // @(#)root/graf:$Name: $:$Id: TMultiGraph.cxx,v 1.12 2003/05/16 13:12:07 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 : brun 9010 if (minimum < 0 && rwymin >= 0) minimum = 0;
270 :     if (maximum > 0 && rwymax <= 0) maximum = 0;
271 : brun 754 }
272 :    
273 :     if (fMinimum != -1111) rwymin = minimum = fMinimum;
274 :     if (fMaximum != -1111) rwymax = maximum = fMaximum;
275 :     if (uxmin < 0 && rwxmin >= 0) {
276 :     if (gPad->GetLogx()) uxmin = 0.9*rwxmin;
277 : brun 3575 //else uxmin = 0;
278 : brun 754 }
279 :     if (uxmax > 0 && rwxmax <= 0) {
280 :     if (gPad->GetLogx()) uxmax = 1.1*rwxmax;
281 : brun 3575 //else uxmax = 0;
282 : brun 754 }
283 :     if (minimum < 0 && rwymin >= 0) {
284 :     if(gPad->GetLogy()) minimum = 0.9*rwymin;
285 : brun 3575 //else minimum = 0;
286 : brun 754 }
287 :     if (maximum > 0 && rwymax <= 0) {
288 :     if(gPad->GetLogy()) maximum = 1.1*rwymax;
289 : brun 3575 //else maximum = 0;
290 : brun 754 }
291 :     if (minimum <= 0 && gPad->GetLogy()) minimum = 0.001*maximum;
292 :     if (uxmin <= 0 && gPad->GetLogx()) {
293 :     if (uxmax > 1000) uxmin = 1;
294 :     else uxmin = 0.001*uxmax;
295 :     }
296 :     rwymin = minimum;
297 :     rwymax = maximum;
298 :     if (fHistogram) {
299 : brun 3575 fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
300 :     }
301 :    
302 :     //*-*- Create a temporary histogram to draw the axis
303 :     if (!fHistogram) {
304 :     // the graph is created with at least as many channels as there are points
305 :     // to permit zooming on the full range
306 :     rwxmin = uxmin;
307 :     rwxmax = uxmax;
308 :     fHistogram = new TH1F(GetName(),GetTitle(),npt,rwxmin,rwxmax);
309 :     if (!fHistogram) return;
310 : brun 754 fHistogram->SetMinimum(rwymin);
311 : brun 3575 fHistogram->SetBit(TH1::kNoStats);
312 : brun 754 fHistogram->SetMaximum(rwymax);
313 : brun 3575 fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
314 :     fHistogram->SetDirectory(0);
315 : brun 3799 if (xtitle) {fHistogram->GetXaxis()->SetTitle(xtitle); delete [] xtitle;}
316 :     if (ytitle) {fHistogram->GetYaxis()->SetTitle(ytitle); delete [] ytitle;}
317 :     if (firstx != lastx) fHistogram->GetXaxis()->SetRange(firstx,lastx);
318 : brun 754 }
319 : brun 4037 fHistogram->Paint("0");
320 : brun 3575 }
321 : rdm 3742
322 : brun 754 if (fGraphs) {
323 : brun 4037 TObjOptLink *lnk = (TObjOptLink*)fGraphs->FirstLink();
324 :     TObject *obj;
325 :    
326 :     while (lnk) {
327 :     obj = lnk->GetObject();
328 :     if (strlen(lnk->GetOption())) obj->Paint(lnk->GetOption());
329 :     else obj->Paint(chopt);
330 :     lnk = (TObjOptLink*)lnk->Next();
331 :     }
332 : brun 754 }
333 :     }
334 :    
335 :     //______________________________________________________________________________
336 : brun 1205 void TMultiGraph::Print(Option_t *option) const
337 : brun 754 {
338 :     // Print the list of graphs
339 :    
340 :     TGraph *g;
341 :     if (fGraphs) {
342 :     TIter next(fGraphs);
343 :     while ((g = (TGraph*) next())) {
344 :     g->Print(option);
345 :     }
346 :     }
347 :     }
348 :    
349 :     //______________________________________________________________________________
350 : brun 5552 void TMultiGraph::RecursiveRemove(TObject *obj)
351 :     {
352 :     // Recursively remove this object from a list. Typically implemented
353 :     // by classes that can contain mulitple references to a same object.
354 :    
355 :     if (!fGraphs) return;
356 :     TObject *objr = fGraphs->Remove(obj);
357 :     if (!objr) return;
358 :     delete fHistogram; fHistogram = 0;
359 :     if (gPad) gPad->Modified();
360 :     }
361 :    
362 :     //______________________________________________________________________________
363 : brun 754 void TMultiGraph::SavePrimitive(ofstream &out, Option_t *option)
364 :     {
365 :     // Save primitive as a C++ statement(s) on output stream out
366 :    
367 :     char quote = '"';
368 :     out<<" "<<endl;
369 :     if (gROOT->ClassSaved(TMultiGraph::Class())) {
370 :     out<<" ";
371 :     } else {
372 :     out<<" TMultiGraph *";
373 :     }
374 :     out<<"multigraph = new TMultiGraph();"<<endl;
375 :     out<<" multigraph->SetName("<<quote<<GetName()<<quote<<");"<<endl;
376 :     out<<" multigraph->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
377 :    
378 :     TGraph *g;
379 :     if (fGraphs) {
380 :     TIter next(fGraphs);
381 :     while ((g = (TGraph*) next())) {
382 :     g->SavePrimitive(out,"multigraph");
383 :     }
384 :     }
385 :     out<<" multigraph->Draw("
386 :     <<quote<<option<<quote<<");"<<endl;
387 :     }
388 :    
389 :     //______________________________________________________________________________
390 :     void TMultiGraph::SetMaximum(Double_t maximum)
391 :     {
392 :     fMaximum = maximum;
393 :     if (fHistogram) fHistogram->SetMaximum(maximum);
394 :     }
395 :    
396 :     //______________________________________________________________________________
397 :     void TMultiGraph::SetMinimum(Double_t minimum)
398 :     {
399 :     fMinimum = minimum;
400 :     if (fHistogram) fHistogram->SetMinimum(minimum);
401 :     }

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9