Parent Directory
|
Revision Log
Revision 3748 -
(view)
(download)
(as text)
Original Path: trunk/graf/src/TMultiGraph.cxx
| 1 : | rdm | 3748 | // @(#)root/graf:$Name: $:$Id: TMultiGraph.cxx,v 1.5 2002/01/23 17:52:49 rdm 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 : | // 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 : | rdm | 3742 | |
| 79 : | brun | 754 | 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 : | brun | 1205 | TH1F *TMultiGraph::GetHistogram() const |
| 129 : | brun | 754 | { |
| 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 : | brun | 1205 | TAxis *TMultiGraph::GetXaxis() const |
| 146 : | brun | 754 | { |
| 147 : | // Get x axis of the graph. | ||
| 148 : | |||
| 149 : | if (!gPad) return 0; | ||
| 150 : | return GetHistogram()->GetXaxis(); | ||
| 151 : | } | ||
| 152 : | |||
| 153 : | //______________________________________________________________________________ | ||
| 154 : | brun | 1205 | TAxis *TMultiGraph::GetYaxis() const |
| 155 : | brun | 754 | { |
| 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 : | brun | 1500 | Int_t i; |
| 171 : | for (i=0;i<nch;i++) chopt[i] = toupper(option[i]); | ||
| 172 : | brun | 754 | chopt[nch] = 0; |
| 173 : | brun | 1500 | Double_t *x, *y; |
| 174 : | brun | 3575 | TGraph *g; |
| 175 : | rdm | 3742 | |
| 176 : | brun | 754 | l = strstr(chopt,"A"); |
| 177 : | if (l) { | ||
| 178 : | *l = ' '; | ||
| 179 : | TIter next(fGraphs); | ||
| 180 : | Int_t npt = 100; | ||
| 181 : | Double_t maximum, minimum, rwxmin, rwxmax, rwymin, rwymax, uxmin, uxmax, dx, dy; | ||
| 182 : | brun | 3575 | rwxmin = gPad->GetUxmin(); |
| 183 : | rwxmax = gPad->GetUxmax(); | ||
| 184 : | rwymin = gPad->GetUymin(); | ||
| 185 : | rwymax = gPad->GetUymax(); | ||
| 186 : | brun | 754 | if (fHistogram) { |
| 187 : | brun | 3575 | //cleanup in case of a previous unzoom |
| 188 : | if (fHistogram->GetMinimum() >= fHistogram->GetMaximum()) { | ||
| 189 : | delete fHistogram; | ||
| 190 : | fHistogram = 0; | ||
| 191 : | } | ||
| 192 : | } | ||
| 193 : | if (fHistogram) { | ||
| 194 : | minimum = fHistogram->GetYaxis()->GetXmin(); | ||
| 195 : | maximum = fHistogram->GetYaxis()->GetXmax(); | ||
| 196 : | uxmin = gPad->PadtoX(rwxmin); | ||
| 197 : | uxmax = gPad->PadtoX(rwxmax); | ||
| 198 : | brun | 754 | } else { |
| 199 : | rwxmin = 1e100; | ||
| 200 : | rwxmax = -rwxmin; | ||
| 201 : | rwymin = rwxmin; | ||
| 202 : | rwymax = -rwymin; | ||
| 203 : | while ((g = (TGraph*) next())) { | ||
| 204 : | brun | 1500 | Int_t npoints = g->GetN(); |
| 205 : | x = g->GetX(); | ||
| 206 : | y = g->GetY(); | ||
| 207 : | for (i=0;i<npoints;i++) { | ||
| 208 : | if (x[i] < rwxmin) rwxmin = x[i]; | ||
| 209 : | if (x[i] > rwxmax) rwxmax = x[i]; | ||
| 210 : | if (y[i] < rwymin) rwymin = y[i]; | ||
| 211 : | if (y[i] > rwymax) rwymax = y[i]; | ||
| 212 : | } | ||
| 213 : | brun | 754 | g->ComputeRange(rwxmin, rwymin, rwxmax, rwymax); |
| 214 : | if (g->GetN() > npt) npt = g->GetN(); | ||
| 215 : | } | ||
| 216 : | if (rwxmin == rwxmax) rwxmax += 1.; | ||
| 217 : | if (rwymin == rwymax) rwymax += 1.; | ||
| 218 : | brun | 3575 | dx = 0.05*(rwxmax-rwxmin); |
| 219 : | dy = 0.05*(rwymax-rwymin); | ||
| 220 : | brun | 754 | uxmin = rwxmin - dx; |
| 221 : | uxmax = rwxmax + dx; | ||
| 222 : | minimum = rwymin - dy; | ||
| 223 : | maximum = rwymax + dy; | ||
| 224 : | } | ||
| 225 : | |||
| 226 : | if (fMinimum != -1111) rwymin = minimum = fMinimum; | ||
| 227 : | if (fMaximum != -1111) rwymax = maximum = fMaximum; | ||
| 228 : | if (uxmin < 0 && rwxmin >= 0) { | ||
| 229 : | if (gPad->GetLogx()) uxmin = 0.9*rwxmin; | ||
| 230 : | brun | 3575 | //else uxmin = 0; |
| 231 : | brun | 754 | } |
| 232 : | if (uxmax > 0 && rwxmax <= 0) { | ||
| 233 : | if (gPad->GetLogx()) uxmax = 1.1*rwxmax; | ||
| 234 : | brun | 3575 | //else uxmax = 0; |
| 235 : | brun | 754 | } |
| 236 : | if (minimum < 0 && rwymin >= 0) { | ||
| 237 : | if(gPad->GetLogy()) minimum = 0.9*rwymin; | ||
| 238 : | brun | 3575 | //else minimum = 0; |
| 239 : | brun | 754 | } |
| 240 : | if (maximum > 0 && rwymax <= 0) { | ||
| 241 : | if(gPad->GetLogy()) maximum = 1.1*rwymax; | ||
| 242 : | brun | 3575 | //else maximum = 0; |
| 243 : | brun | 754 | } |
| 244 : | if (minimum <= 0 && gPad->GetLogy()) minimum = 0.001*maximum; | ||
| 245 : | if (uxmin <= 0 && gPad->GetLogx()) { | ||
| 246 : | if (uxmax > 1000) uxmin = 1; | ||
| 247 : | else uxmin = 0.001*uxmax; | ||
| 248 : | } | ||
| 249 : | rwymin = minimum; | ||
| 250 : | rwymax = maximum; | ||
| 251 : | if (fHistogram) { | ||
| 252 : | brun | 3575 | fHistogram->GetYaxis()->SetLimits(rwymin,rwymax); |
| 253 : | } | ||
| 254 : | |||
| 255 : | //*-*- Create a temporary histogram to draw the axis | ||
| 256 : | if (!fHistogram) { | ||
| 257 : | // the graph is created with at least as many channels as there are points | ||
| 258 : | // to permit zooming on the full range | ||
| 259 : | rwxmin = uxmin; | ||
| 260 : | rwxmax = uxmax; | ||
| 261 : | fHistogram = new TH1F(GetName(),GetTitle(),npt,rwxmin,rwxmax); | ||
| 262 : | if (!fHistogram) return; | ||
| 263 : | brun | 754 | fHistogram->SetMinimum(rwymin); |
| 264 : | brun | 3575 | fHistogram->SetBit(TH1::kNoStats); |
| 265 : | brun | 754 | fHistogram->SetMaximum(rwymax); |
| 266 : | brun | 3575 | fHistogram->GetYaxis()->SetLimits(rwymin,rwymax); |
| 267 : | fHistogram->SetDirectory(0); | ||
| 268 : | brun | 754 | } |
| 269 : | brun | 3575 | fHistogram->Paint(); |
| 270 : | } | ||
| 271 : | rdm | 3742 | |
| 272 : | brun | 754 | if (fGraphs) { |
| 273 : | TIter next(fGraphs); | ||
| 274 : | while ((g = (TGraph*) next())) { | ||
| 275 : | g->Paint(chopt); | ||
| 276 : | } | ||
| 277 : | } | ||
| 278 : | } | ||
| 279 : | |||
| 280 : | //______________________________________________________________________________ | ||
| 281 : | brun | 1205 | void TMultiGraph::Print(Option_t *option) const |
| 282 : | brun | 754 | { |
| 283 : | // Print the list of graphs | ||
| 284 : | |||
| 285 : | TGraph *g; | ||
| 286 : | if (fGraphs) { | ||
| 287 : | TIter next(fGraphs); | ||
| 288 : | while ((g = (TGraph*) next())) { | ||
| 289 : | g->Print(option); | ||
| 290 : | } | ||
| 291 : | } | ||
| 292 : | } | ||
| 293 : | |||
| 294 : | //______________________________________________________________________________ | ||
| 295 : | void TMultiGraph::SavePrimitive(ofstream &out, Option_t *option) | ||
| 296 : | { | ||
| 297 : | // Save primitive as a C++ statement(s) on output stream out | ||
| 298 : | |||
| 299 : | char quote = '"'; | ||
| 300 : | out<<" "<<endl; | ||
| 301 : | if (gROOT->ClassSaved(TMultiGraph::Class())) { | ||
| 302 : | out<<" "; | ||
| 303 : | } else { | ||
| 304 : | out<<" TMultiGraph *"; | ||
| 305 : | } | ||
| 306 : | out<<"multigraph = new TMultiGraph();"<<endl; | ||
| 307 : | out<<" multigraph->SetName("<<quote<<GetName()<<quote<<");"<<endl; | ||
| 308 : | out<<" multigraph->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl; | ||
| 309 : | |||
| 310 : | TGraph *g; | ||
| 311 : | if (fGraphs) { | ||
| 312 : | TIter next(fGraphs); | ||
| 313 : | while ((g = (TGraph*) next())) { | ||
| 314 : | g->SavePrimitive(out,"multigraph"); | ||
| 315 : | } | ||
| 316 : | } | ||
| 317 : | out<<" multigraph->Draw(" | ||
| 318 : | <<quote<<option<<quote<<");"<<endl; | ||
| 319 : | } | ||
| 320 : | |||
| 321 : | //______________________________________________________________________________ | ||
| 322 : | void TMultiGraph::SetMaximum(Double_t maximum) | ||
| 323 : | { | ||
| 324 : | fMaximum = maximum; | ||
| 325 : | if (fHistogram) fHistogram->SetMaximum(maximum); | ||
| 326 : | } | ||
| 327 : | |||
| 328 : | //______________________________________________________________________________ | ||
| 329 : | void TMultiGraph::SetMinimum(Double_t minimum) | ||
| 330 : | { | ||
| 331 : | fMinimum = minimum; | ||
| 332 : | if (fHistogram) fHistogram->SetMinimum(minimum); | ||
| 333 : | } |
| Subversion Admin | ViewVC Help |
| Powered by ViewVC 1.0.9 |