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

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

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9