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