00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "Riostream.h"
00013 #include "TROOT.h"
00014 #include "TMath.h"
00015 #include "TH2.h"
00016 #include "TF2.h"
00017 #include "TList.h"
00018 #include "TGraph2D.h"
00019 #include "TGraphDelaunay.h"
00020 #include "TVirtualPad.h"
00021 #include "TVirtualFitter.h"
00022 #include "TPluginManager.h"
00023 #include "TClass.h"
00024 #include "TSystem.h"
00025 #include <stdlib.h>
00026
00027 #include "HFitInterface.h"
00028 #include "Fit/DataRange.h"
00029 #include "Math/MinimizerOptions.h"
00030
00031 ClassImp(TGraph2D)
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 TGraph2D::TGraph2D()
00226 : TNamed("Graph2D","Graph2D"), TAttLine(1,1,1), TAttFill(0,1001),
00227 TAttMarker(), fNpoints(0)
00228 {
00229
00230
00231 fSize = 0;
00232 fMargin = 0.;
00233 fNpx = 40;
00234 fNpy = 40;
00235 fDirectory = 0;
00236 fHistogram = 0;
00237 fMaximum = -1111;
00238 fMinimum = -1111;
00239 fX = 0;
00240 fY = 0;
00241 fZ = 0;
00242 fZout = 0;
00243 fMaxIter = 100000;
00244 fPainter = 0;
00245 fFunctions = new TList;
00246 fUserHisto = kFALSE;
00247 }
00248
00249
00250
00251 TGraph2D::TGraph2D(Int_t n, Int_t *x, Int_t *y, Int_t *z)
00252 : TNamed("Graph2D","Graph2D"), TAttLine(1,1,1), TAttFill(0,1001),
00253 TAttMarker(), fNpoints(n)
00254 {
00255
00256
00257 Build(n);
00258
00259
00260 for (Int_t i=0; i<fNpoints; ++i) {
00261 fX[i] = (Double_t)x[i];
00262 fY[i] = (Double_t)y[i];
00263 fZ[i] = (Double_t)z[i];
00264 }
00265 }
00266
00267
00268
00269 TGraph2D::TGraph2D(Int_t n, Float_t *x, Float_t *y, Float_t *z)
00270 : TNamed("Graph2D","Graph2D"), TAttLine(1,1,1), TAttFill(0,1001),
00271 TAttMarker(), fNpoints(n)
00272 {
00273
00274
00275 Build(n);
00276
00277
00278 for (Int_t i=0; i<fNpoints; ++i) {
00279 fX[i] = x[i];
00280 fY[i] = y[i];
00281 fZ[i] = z[i];
00282 }
00283 }
00284
00285
00286
00287 TGraph2D::TGraph2D(Int_t n, Double_t *x, Double_t *y, Double_t *z)
00288 : TNamed("Graph2D","Graph2D"), TAttLine(1,1,1), TAttFill(0,1001),
00289 TAttMarker(), fNpoints(n)
00290 {
00291
00292
00293 Build(n);
00294
00295
00296 for (Int_t i=0; i<fNpoints; ++i) {
00297 fX[i] = x[i];
00298 fY[i] = y[i];
00299 fZ[i] = z[i];
00300 }
00301 }
00302
00303
00304
00305 TGraph2D::TGraph2D(TH2 *h2)
00306 : TNamed("Graph2D","Graph2D"), TAttLine(1,1,1), TAttFill(0,1001),
00307 TAttMarker(), fNpoints(0)
00308 {
00309
00310
00311
00312 Build(h2->GetNbinsX()*h2->GetNbinsY());
00313
00314 TString gname = "Graph2D_from_" + TString(h2->GetName() );
00315 SetName(gname);
00316
00317 SetTitle(h2->GetTitle());
00318
00319
00320
00321 TAxis *xaxis = h2->GetXaxis();
00322 TAxis *yaxis = h2->GetYaxis();
00323 Int_t xfirst = xaxis->GetFirst();
00324 Int_t xlast = xaxis->GetLast();
00325 Int_t yfirst = yaxis->GetFirst();
00326 Int_t ylast = yaxis->GetLast();
00327
00328
00329 Double_t x, y, z;
00330 Int_t k=0;
00331
00332 for (Int_t i=xfirst; i<= xlast; i++) {
00333 for (Int_t j=yfirst; j<= ylast; j++) {
00334 x = xaxis->GetBinCenter(i);
00335 y = yaxis->GetBinCenter(j);
00336 z = h2->GetBinContent(i,j);
00337 Double_t ez = h2->GetBinError(i,j);
00338 if (z != 0. || ez != 0) {
00339 SetPoint(k, x, y, z);
00340 k++;
00341 }
00342 }
00343 }
00344 }
00345
00346
00347
00348 TGraph2D::TGraph2D(const char *name,const char *title,
00349 Int_t n, Double_t *x, Double_t *y, Double_t *z)
00350 : TNamed(name,title), TAttLine(1,1,1), TAttFill(0,1001),
00351 TAttMarker(), fNpoints(n)
00352 {
00353
00354
00355
00356
00357
00358
00359
00360 Build(n);
00361
00362
00363 for (Int_t i=0; i<fNpoints; ++i) {
00364 fX[i] = x[i];
00365 fY[i] = y[i];
00366 fZ[i] = z[i];
00367 }
00368 }
00369
00370
00371
00372 TGraph2D::TGraph2D(Int_t n)
00373 : TNamed("Graph2D","Graph2D"), TAttLine(1,1,1), TAttFill(0,1001),
00374 TAttMarker(), fNpoints(0)
00375 {
00376
00377
00378
00379 Build(n);
00380 }
00381
00382
00383
00384 TGraph2D::TGraph2D(const char *filename, const char *format, Option_t *)
00385 : TNamed("Graph2D",filename), TAttLine(1,1,1), TAttFill(0,1001),
00386 TAttMarker(), fNpoints(0)
00387 {
00388
00389
00390
00391 Build(100);
00392
00393 Double_t x,y,z;
00394 FILE *fp = fopen(filename,"r");
00395 if (!fp) {
00396 MakeZombie();
00397 Error("TGraph2D", "Cannot open file: %s, TGraph2D is Zombie",filename);
00398 return;
00399 }
00400 char line[80];
00401 Int_t np = 0;
00402 while (fgets(line,80,fp)) {
00403 sscanf(&line[0],format,&x, &y, &z);
00404 SetPoint(np,x,y,z);
00405 np++;
00406 }
00407
00408 fclose(fp);
00409 }
00410
00411
00412
00413 TGraph2D::TGraph2D(const TGraph2D &g)
00414 : TNamed(g), TAttLine(g), TAttFill(g), TAttMarker(g)
00415 {
00416
00417
00418 fNpoints = g.fNpoints;
00419 Build(fNpoints);
00420
00421 for (Int_t n=0; n<fNpoints; n++) {
00422 fX[n] = g.fX[n];
00423 fY[n] = g.fY[n];
00424 fZ[n] = g.fZ[n];
00425 }
00426 }
00427
00428
00429
00430 TGraph2D::~TGraph2D()
00431 {
00432
00433
00434 Clear();
00435 }
00436
00437
00438
00439 TGraph2D& TGraph2D::operator=(const TGraph2D &g)
00440 {
00441
00442
00443 if (this == &g) return *this;
00444
00445 Clear();
00446
00447 fNpoints = g.fNpoints;
00448 Build(fNpoints);
00449
00450 for (Int_t n=0; n<fNpoints; n++) {
00451 fX[n] = g.fX[n];
00452 fY[n] = g.fY[n];
00453 fZ[n] = g.fZ[n];
00454 }
00455 return *this;
00456 }
00457
00458
00459 void TGraph2D::Build(Int_t n)
00460 {
00461
00462
00463 if (n <= 0) {
00464 Error("TGraph2D", "Invalid number of points (%d)", n);
00465 return;
00466 }
00467
00468 fSize = n;
00469 fMargin = 0.;
00470 fNpx = 40;
00471 fNpy = 40;
00472 fDirectory = 0;
00473 fHistogram = 0;
00474 fMaximum = -1111;
00475 fMinimum = -1111;
00476 fX = new Double_t[fSize];
00477 fY = new Double_t[fSize];
00478 fZ = new Double_t[fSize];
00479 fZout = 0;
00480 fMaxIter = 100000;
00481 fFunctions = new TList;
00482 fPainter = 0;
00483 fUserHisto = kFALSE;
00484
00485 if (TH1::AddDirectoryStatus()) {
00486 fDirectory = gDirectory;
00487 if (fDirectory) {
00488 fDirectory->Append(this,kTRUE);
00489 }
00490 }
00491 }
00492
00493
00494
00495 void TGraph2D::Clear(Option_t * )
00496 {
00497
00498
00499 delete [] fX; fX = 0;
00500 delete [] fY; fY = 0;
00501 delete [] fZ; fZ = 0;
00502 delete fHistogram; fHistogram = 0;
00503 if (fFunctions) {
00504 fFunctions->SetBit(kInvalidObject);
00505 fFunctions->Delete();
00506 delete fFunctions;
00507 fFunctions = 0;
00508 }
00509 if (fDirectory) {
00510 fDirectory->Remove(this);
00511 fDirectory = 0;
00512 }
00513 delete fPainter;
00514 fPainter = 0;
00515 }
00516
00517
00518
00519 void TGraph2D::DirectoryAutoAdd(TDirectory *dir)
00520 {
00521
00522
00523
00524
00525
00526
00527 Bool_t addStatus = TH1::AddDirectoryStatus();
00528 if (addStatus) {
00529 SetDirectory(dir);
00530 if (dir) {
00531 ResetBit(kCanDelete);
00532 }
00533 }
00534 }
00535
00536
00537
00538 Int_t TGraph2D::DistancetoPrimitive(Int_t px, Int_t py)
00539 {
00540
00541
00542 Int_t distance = 9999;
00543 if (fHistogram) distance = fHistogram->DistancetoPrimitive(px,py);
00544 return distance;
00545 }
00546
00547
00548
00549 void TGraph2D::Draw(Option_t *option)
00550 {
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 TString opt = option;
00575 opt.ToLower();
00576 if (gPad) {
00577 if (!gPad->IsEditable()) gROOT->MakeDefCanvas();
00578 if (!opt.Contains("same")) {
00579
00580
00581 if (TestBit(kCanDelete)) gPad->GetListOfPrimitives()->Remove(this);
00582 gPad->Clear();
00583 }
00584 }
00585 AppendPad(opt.Data());
00586 }
00587
00588
00589
00590 void TGraph2D::ExecuteEvent(Int_t event, Int_t px, Int_t py)
00591 {
00592
00593
00594 if (fHistogram) fHistogram->ExecuteEvent(event, px, py);
00595 }
00596
00597
00598
00599 TObject *TGraph2D::FindObject(const char *name) const
00600 {
00601
00602
00603 if (fFunctions) return fFunctions->FindObject(name);
00604 return 0;
00605 }
00606
00607
00608
00609 TObject *TGraph2D::FindObject(const TObject *obj) const
00610 {
00611
00612
00613 if (fFunctions) return fFunctions->FindObject(obj);
00614 return 0;
00615 }
00616
00617
00618
00619 TFitResultPtr TGraph2D::Fit(const char *fname, Option_t *option, Option_t *)
00620 {
00621
00622
00623
00624
00625
00626
00627
00628 char *linear;
00629 linear= (char*)strstr(fname, "++");
00630 TF2 *f2=0;
00631 if (linear)
00632 f2=new TF2(fname, fname);
00633 else{
00634 f2 = (TF2*)gROOT->GetFunction(fname);
00635 if (!f2) { Printf("Unknown function: %s",fname); return -1; }
00636 }
00637 return Fit(f2,option,"");
00638
00639 }
00640
00641
00642
00643 TFitResultPtr TGraph2D::Fit(TF2 *f2, Option_t *option, Option_t *)
00644 {
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770 Foption_t fitOption;
00771 Option_t *goption = "";
00772 ROOT::Fit::FitOptionsMake(option,fitOption);
00773
00774
00775 ROOT::Fit::DataRange range(2);
00776 ROOT::Math::MinimizerOptions minOption;
00777 return ROOT::Fit::FitObject(this, f2 , fitOption , minOption, goption, range);
00778 }
00779
00780
00781
00782 void TGraph2D::FitPanel()
00783 {
00784
00785
00786
00787 if (!gPad)
00788 gROOT->MakeDefCanvas();
00789
00790 if (!gPad) {
00791 Error("FitPanel", "Unable to create a default canvas");
00792 return;
00793 }
00794
00795
00796 TPluginHandler *handler = gROOT->GetPluginManager()->FindHandler("TFitEditor");
00797 if (handler && handler->LoadPlugin() != -1) {
00798 if (handler->ExecPlugin(2, gPad, this) == 0)
00799 Error("FitPanel", "Unable to crate the FitPanel");
00800 }
00801 else
00802 Error("FitPanel", "Unable to find the FitPanel plug-in");
00803
00804 }
00805
00806
00807
00808 TAxis *TGraph2D::GetXaxis() const
00809 {
00810
00811
00812
00813 TH1 *h = ((TGraph2D*)this)->GetHistogram();
00814 if (!h) return 0;
00815 return h->GetXaxis();
00816 }
00817
00818
00819
00820 TAxis *TGraph2D::GetYaxis() const
00821 {
00822
00823
00824
00825 TH1 *h = ((TGraph2D*)this)->GetHistogram();
00826 if (!h) return 0;
00827 return h->GetYaxis();
00828 }
00829
00830
00831
00832 TAxis *TGraph2D::GetZaxis() const
00833 {
00834
00835
00836
00837 TH1 *h = ((TGraph2D*)this)->GetHistogram();
00838 if (!h) return 0;
00839 return h->GetZaxis();
00840 }
00841
00842
00843
00844 TList *TGraph2D::GetContourList(Double_t contour)
00845 {
00846
00847
00848
00849
00850 if (fNpoints <= 0) {
00851 Error("GetContourList", "Empty TGraph2D");
00852 return 0;
00853 }
00854
00855 if(!fHistogram) GetHistogram("empty");
00856
00857 if (!fPainter) fPainter = fHistogram->GetPainter();
00858
00859 return fPainter->GetContourList(contour);
00860 }
00861
00862
00863
00864 Double_t TGraph2D::GetErrorX(Int_t) const
00865 {
00866
00867
00868
00869 return -1;
00870 }
00871
00872
00873
00874 Double_t TGraph2D::GetErrorY(Int_t) const
00875 {
00876
00877
00878
00879 return -1;
00880 }
00881
00882
00883
00884 Double_t TGraph2D::GetErrorZ(Int_t) const
00885 {
00886
00887
00888
00889 return -1;
00890 }
00891
00892
00893
00894 TH2D *TGraph2D::GetHistogram(Option_t *option)
00895 {
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905 if (fNpoints <= 0) {
00906 Error("GetHistogram", "Empty TGraph2D");
00907 return 0;
00908 }
00909
00910 TString opt = option;
00911 opt.ToLower();
00912 Bool_t empty = opt.Contains("empty");
00913
00914 if (fHistogram) {
00915 if (!empty && fHistogram->GetEntries()==0) {
00916 if (!fUserHisto) {
00917 delete fHistogram;
00918 fHistogram = 0;
00919 }
00920 } else {
00921 return fHistogram;
00922 }
00923 }
00924
00925 Double_t hxmax, hymax, hxmin, hymin;
00926
00927
00928 if (!fUserHisto) {
00929 Bool_t add = TH1::AddDirectoryStatus();
00930 TH1::AddDirectory(kFALSE);
00931 Double_t xmax = GetXmaxE();
00932 Double_t ymax = GetYmaxE();
00933 Double_t xmin = GetXminE();
00934 Double_t ymin = GetYminE();
00935 hxmin = xmin-fMargin*(xmax-xmin);
00936 hymin = ymin-fMargin*(ymax-ymin);
00937 hxmax = xmax+fMargin*(xmax-xmin);
00938 hymax = ymax+fMargin*(ymax-ymin);
00939 fHistogram = new TH2D(GetName(),GetTitle(),
00940 fNpx ,hxmin, hxmax,
00941 fNpy, hymin, hymax);
00942 TH1::AddDirectory(add);
00943 fHistogram->SetBit(TH1::kNoStats);
00944 } else {
00945 hxmin = fHistogram->GetXaxis()->GetXmin();
00946 hymin = fHistogram->GetYaxis()->GetXmin();
00947 hxmax = fHistogram->GetXaxis()->GetXmax();
00948 hymax = fHistogram->GetYaxis()->GetXmax();
00949 }
00950
00951
00952 TGraphDelaunay *dt = new TGraphDelaunay(this);
00953 dt->SetMaxIter(fMaxIter);
00954 dt->SetMarginBinsContent(fZout);
00955 TList *hl = fHistogram->GetListOfFunctions();
00956 hl->Add(dt);
00957
00958
00959 if (empty) {
00960 Double_t hzmax, hzmin;
00961 if (fMinimum != -1111) {
00962 hzmin = fMinimum;
00963 } else {
00964 hzmin = GetZmin();
00965
00966 }
00967 if (fMaximum != -1111) {
00968 hzmax = fMaximum;
00969 } else {
00970 hzmax = GetZmax();
00971
00972 }
00973 if (hzmin==hzmax) {
00974 hzmin = hzmin-0.01*hzmin;
00975 hzmax = hzmax+0.01*hzmax;
00976 }
00977 fHistogram->SetMinimum(hzmin);
00978 fHistogram->SetMaximum(hzmax);
00979 return fHistogram;
00980 }
00981
00982 Double_t dx = (hxmax-hxmin)/fNpx;
00983 Double_t dy = (hymax-hymin)/fNpy;
00984
00985 Double_t x, y, z;
00986 for (Int_t ix=1; ix<=fNpx; ix++) {
00987 x = hxmin+(ix-0.5)*dx;
00988 for (Int_t iy=1; iy<=fNpy; iy++) {
00989 y = hymin+(iy-0.5)*dy;
00990 z = dt->ComputeZ(x, y);
00991 fHistogram->Fill(x, y, z);
00992 }
00993 }
00994
00995 if (fMinimum != -1111) fHistogram->SetMinimum(fMinimum);
00996 if (fMaximum != -1111) fHistogram->SetMaximum(fMaximum);
00997
00998 return fHistogram;
00999 }
01000
01001
01002
01003 Double_t TGraph2D::GetXmax() const
01004 {
01005
01006
01007 Double_t v = fX[0];
01008 for (Int_t i=1; i<fNpoints; i++) if (fX[i]>v) v=fX[i];
01009 return v;
01010 }
01011
01012
01013
01014 Double_t TGraph2D::GetXmin() const
01015 {
01016
01017
01018 Double_t v = fX[0];
01019 for (Int_t i=1; i<fNpoints; i++) if (fX[i]<v) v=fX[i];
01020 return v;
01021 }
01022
01023
01024
01025 Double_t TGraph2D::GetYmax() const
01026 {
01027
01028
01029 Double_t v = fY[0];
01030 for (Int_t i=1; i<fNpoints; i++) if (fY[i]>v) v=fY[i];
01031 return v;
01032 }
01033
01034
01035
01036 Double_t TGraph2D::GetYmin() const
01037 {
01038
01039
01040 Double_t v = fY[0];
01041 for (Int_t i=1; i<fNpoints; i++) if (fY[i]<v) v=fY[i];
01042 return v;
01043 }
01044
01045
01046
01047 Double_t TGraph2D::GetZmax() const
01048 {
01049
01050
01051 Double_t v = fZ[0];
01052 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]>v) v=fZ[i];
01053 return v;
01054 }
01055
01056
01057
01058 Double_t TGraph2D::GetZmin() const
01059 {
01060
01061
01062 Double_t v = fZ[0];
01063 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]<v) v=fZ[i];
01064 return v;
01065 }
01066
01067
01068
01069 Double_t TGraph2D::Interpolate(Double_t x, Double_t y)
01070 {
01071
01072
01073
01074 if (fNpoints <= 0) {
01075 Error("Interpolate", "Empty TGraph2D");
01076 return 0;
01077 }
01078
01079 TGraphDelaunay *dt;
01080
01081 if(!fHistogram) GetHistogram("empty");
01082
01083 TList *hl = fHistogram->GetListOfFunctions();
01084 dt = (TGraphDelaunay*)hl->FindObject("TGraphDelaunay");
01085
01086 return dt->ComputeZ(x, y);
01087 }
01088
01089
01090
01091 void TGraph2D::Paint(Option_t *option)
01092 {
01093
01094
01095 if (fNpoints <= 0) {
01096 Error("Paint", "Empty TGraph2D");
01097 return;
01098 }
01099
01100 TString opt = option;
01101 opt.ToLower();
01102 if (opt.Contains("p") && !opt.Contains("tri")) {
01103 if (!opt.Contains("pol") &&
01104 !opt.Contains("sph") &&
01105 !opt.Contains("psr")) opt.Append("tri0");
01106 }
01107
01108 if (opt.Contains("line") && !opt.Contains("tri")) opt.Append("tri0");
01109
01110 if (opt.Contains("err") && !opt.Contains("tri")) opt.Append("tri0");
01111
01112 if (opt.Contains("tri0")) {
01113 GetHistogram("empty");
01114 } else {
01115 GetHistogram();
01116 }
01117
01118 fHistogram->SetLineColor(GetLineColor());
01119 fHistogram->SetLineStyle(GetLineStyle());
01120 fHistogram->SetLineWidth(GetLineWidth());
01121 fHistogram->SetFillColor(GetFillColor());
01122 fHistogram->SetFillStyle(GetFillStyle());
01123 fHistogram->SetMarkerColor(GetMarkerColor());
01124 fHistogram->SetMarkerStyle(GetMarkerStyle());
01125 fHistogram->SetMarkerSize(GetMarkerSize());
01126 fHistogram->Paint(opt.Data());
01127 }
01128
01129
01130
01131 TH1 *TGraph2D::Project(Option_t *option) const
01132 {
01133
01134
01135
01136
01137
01138
01139
01140
01141 if (fNpoints <= 0) {
01142 Error("Project", "Empty TGraph2D");
01143 return 0;
01144 }
01145
01146 TString opt = option; opt.ToLower();
01147
01148 Int_t pcase = 0;
01149 if (opt.Contains("x")) pcase = 1;
01150 if (opt.Contains("y")) pcase = 2;
01151 if (opt.Contains("xy")) pcase = 3;
01152 if (opt.Contains("yx")) pcase = 4;
01153
01154
01155 TH1D *h1 = 0;
01156 TH2D *h2 = 0;
01157 Int_t nch = strlen(GetName()) +opt.Length() +2;
01158 char *name = new char[nch];
01159 snprintf(name,nch,"%s_%s",GetName(),option);
01160 nch = strlen(GetTitle()) +opt.Length() +2;
01161 char *title = new char[nch];
01162 snprintf(title,nch,"%s_%s",GetTitle(),option);
01163
01164 Double_t hxmin = GetXmin();
01165 Double_t hxmax = GetXmax();
01166 Double_t hymin = GetYmin();
01167 Double_t hymax = GetYmax();
01168
01169 switch (pcase) {
01170 case 1:
01171
01172 h1 = new TH1D(name,title,fNpx,hxmin,hxmax);
01173 break;
01174 case 2:
01175
01176 h1 = new TH1D(name,title,fNpy,hymin,hymax);
01177 break;
01178 case 3:
01179
01180 h2 = new TH2D(name,title,fNpx,hxmin,hxmax,fNpy,hymin,hymax);
01181 break;
01182 case 4:
01183
01184 h2 = new TH2D(name,title,fNpy,hymin,hymax,fNpx,hxmin,hxmax);
01185 break;
01186 }
01187
01188 delete [] name;
01189 delete [] title;
01190 TH1 *h = h1;
01191 if (h2) h = h2;
01192 if (h == 0) return 0;
01193
01194
01195 Double_t entries = 0;
01196 for (Int_t n=0; n<fNpoints; n++) {
01197 switch (pcase) {
01198 case 1:
01199
01200 h1->Fill(fX[n], fZ[n]);
01201 break;
01202 case 2:
01203
01204 h1->Fill(fY[n], fZ[n]);
01205 break;
01206 case 3:
01207
01208 h2->Fill(fX[n], fY[n], fZ[n]);
01209 break;
01210 case 4:
01211
01212 h2->Fill(fY[n], fX[n], fZ[n]);
01213 break;
01214 }
01215 entries += fZ[n];
01216 }
01217 h->SetEntries(entries);
01218 return h;
01219 }
01220
01221
01222
01223 Int_t TGraph2D::RemovePoint(Int_t ipoint)
01224 {
01225
01226
01227 if (ipoint < 0) return -1;
01228 if (ipoint >= fNpoints) return -1;
01229
01230 fNpoints--;
01231 Double_t *newX = new Double_t[fNpoints];
01232 Double_t *newY = new Double_t[fNpoints];
01233 Double_t *newZ = new Double_t[fNpoints];
01234 Int_t j = -1;
01235 for (Int_t i=0;i<fNpoints+1;i++) {
01236 if (i == ipoint) continue;
01237 j++;
01238 newX[j] = fX[i];
01239 newY[j] = fY[i];
01240 newZ[j] = fZ[i];
01241 }
01242 delete [] fX;
01243 delete [] fY;
01244 delete [] fZ;
01245 fX = newX;
01246 fY = newY;
01247 fZ = newZ;
01248 fSize = fNpoints;
01249 if (fHistogram) {delete fHistogram; fHistogram = 0;}
01250 return ipoint;
01251 }
01252
01253
01254
01255 void TGraph2D::SavePrimitive(ostream &out, Option_t *option )
01256 {
01257
01258
01259 char quote = '"';
01260 out<<" "<<endl;
01261 if (gROOT->ClassSaved(TGraph2D::Class())) {
01262 out<<" ";
01263 } else {
01264 out<<" TGraph2D *";
01265 }
01266
01267 out<<"graph2d = new TGraph2D("<<fNpoints<<");"<<endl;
01268 out<<" graph2d->SetName("<<quote<<GetName()<<quote<<");"<<endl;
01269 out<<" graph2d->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
01270
01271 if (fDirectory == 0) {
01272 out<<" "<<GetName()<<"->SetDirectory(0);"<<endl;
01273 }
01274
01275 SaveFillAttributes(out,"graph2d",0,1001);
01276 SaveLineAttributes(out,"graph2d",1,1,1);
01277 SaveMarkerAttributes(out,"graph2d",1,1,1);
01278
01279 for (Int_t i=0;i<fNpoints;i++) {
01280 out<<" graph2d->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<","<<fZ[i]<<");"<<endl;
01281 }
01282
01283
01284 TIter next(fFunctions);
01285 TObject *obj;
01286 while ((obj=next())) {
01287 obj->SavePrimitive(out,"nodraw");
01288 out<<" graph2d->GetListOfFunctions()->Add("<<obj->GetName()<<");"<<endl;
01289 if (obj->InheritsFrom("TPaveStats")) {
01290 out<<" ptstats->SetParent(graph2d->GetListOfFunctions());"<<endl;
01291 }
01292 }
01293
01294 out<<" graph2d->Draw("<<quote<<option<<quote<<");"<<endl;
01295 }
01296
01297
01298
01299 void TGraph2D::Set(Int_t n)
01300 {
01301
01302
01303
01304
01305 if (n < 0) n = 0;
01306 if (n == fNpoints) return;
01307 if (n > fNpoints) SetPoint(n,0,0,0);
01308 fNpoints = n;
01309 }
01310
01311
01312
01313 void TGraph2D::SetDirectory(TDirectory *dir)
01314 {
01315
01316
01317
01318
01319
01320
01321 if (fDirectory == dir) return;
01322 if (fDirectory) fDirectory->Remove(this);
01323 fDirectory = dir;
01324 if (fDirectory) fDirectory->Append(this);
01325 }
01326
01327
01328
01329 void TGraph2D::SetHistogram(TH2 *h)
01330 {
01331
01332
01333 fUserHisto = kTRUE;
01334 fHistogram = (TH2D*)h;
01335 fNpx = h->GetNbinsX();
01336 fNpy = h->GetNbinsY();
01337 }
01338
01339
01340
01341 void TGraph2D::SetMargin(Double_t m)
01342 {
01343
01344
01345 if (m<0 || m>1) {
01346 Warning("SetMargin","The margin must be >= 0 && <= 1, fMargin set to 0.1");
01347 fMargin = 0.1;
01348 } else {
01349 fMargin = m;
01350 }
01351 if (fHistogram) {delete fHistogram; fHistogram = 0;}
01352 }
01353
01354
01355
01356 void TGraph2D::SetMarginBinsContent(Double_t z)
01357 {
01358
01359
01360
01361 fZout = z;
01362 if (fHistogram) {delete fHistogram; fHistogram = 0;}
01363 }
01364
01365
01366
01367 void TGraph2D::SetMaximum(Double_t maximum)
01368 {
01369
01370
01371 fMaximum = maximum;
01372 GetHistogram()->SetMaximum(maximum);
01373 }
01374
01375
01376
01377 void TGraph2D::SetMinimum(Double_t minimum)
01378 {
01379
01380
01381 fMinimum = minimum;
01382 GetHistogram()->SetMinimum(minimum);
01383 }
01384
01385
01386
01387 void TGraph2D::SetName(const char *name)
01388 {
01389
01390
01391
01392
01393 if (fDirectory) fDirectory->Remove(this);
01394 fName = name;
01395 if (fDirectory) fDirectory->Append(this);
01396 }
01397
01398
01399
01400 void TGraph2D::SetNameTitle(const char *name, const char *title)
01401 {
01402
01403
01404
01405
01406
01407 if (fDirectory) fDirectory->Remove(this);
01408 fName = name;
01409 SetTitle(title);
01410 if (fDirectory) fDirectory->Append(this);
01411 }
01412
01413
01414
01415 void TGraph2D::SetNpx(Int_t npx)
01416 {
01417
01418
01419 if (npx < 4) {
01420 Warning("SetNpx","Number of points must be >4 && < 500, fNpx set to 4");
01421 fNpx = 4;
01422 } else if(npx > 500) {
01423 Warning("SetNpx","Number of points must be >4 && < 500, fNpx set to 500");
01424 fNpx = 500;
01425 } else {
01426 fNpx = npx;
01427 }
01428 if (fHistogram) {delete fHistogram; fHistogram = 0;}
01429 }
01430
01431
01432
01433 void TGraph2D::SetNpy(Int_t npy)
01434 {
01435
01436
01437 if (npy < 4) {
01438 Warning("SetNpy","Number of points must be >4 && < 500, fNpy set to 4");
01439 fNpy = 4;
01440 } else if(npy > 500) {
01441 Warning("SetNpy","Number of points must be >4 && < 500, fNpy set to 500");
01442 fNpy = 500;
01443 } else {
01444 fNpy = npy;
01445 }
01446 if (fHistogram) {delete fHistogram; fHistogram = 0;}
01447 }
01448
01449
01450
01451 void TGraph2D::SetPoint(Int_t n, Double_t x, Double_t y, Double_t z)
01452 {
01453
01454
01455
01456
01457 if (n < 0) return;
01458
01459 if (!fX || !fY || !fZ || n >= fSize) {
01460
01461 Int_t newN = TMath::Max(2*fSize,n+1);
01462 Double_t *savex = new Double_t [newN];
01463 Double_t *savey = new Double_t [newN];
01464 Double_t *savez = new Double_t [newN];
01465 if (fX && fSize) {
01466 memcpy(savex,fX,fSize*sizeof(Double_t));
01467 memset(&savex[fSize],0,(newN-fSize)*sizeof(Double_t));
01468 delete [] fX;
01469 }
01470 if (fY && fSize) {
01471 memcpy(savey,fY,fSize*sizeof(Double_t));
01472 memset(&savey[fSize],0,(newN-fSize)*sizeof(Double_t));
01473 delete [] fY;
01474 }
01475 if (fZ && fSize) {
01476 memcpy(savez,fZ,fSize*sizeof(Double_t));
01477 memset(&savez[fSize],0,(newN-fSize)*sizeof(Double_t));
01478 delete [] fZ;
01479 }
01480 fX = savex;
01481 fY = savey;
01482 fZ = savez;
01483 fSize = newN;
01484 }
01485 fX[n] = x;
01486 fY[n] = y;
01487 fZ[n] = z;
01488 fNpoints = TMath::Max(fNpoints,n+1);
01489 }
01490
01491
01492
01493 void TGraph2D::SetTitle(const char* title)
01494 {
01495
01496
01497 fTitle = title;
01498 if (fHistogram) fHistogram->SetTitle(title);
01499 }
01500
01501
01502
01503 void TGraph2D::Streamer(TBuffer &b)
01504 {
01505
01506
01507 if (b.IsReading()) {
01508 UInt_t R__s, R__c;
01509 Version_t R__v = b.ReadVersion(&R__s, &R__c);
01510 b.ReadClassBuffer(TGraph2D::Class(), this, R__v, R__s, R__c);
01511
01512 ResetBit(kMustCleanup);
01513 } else {
01514 b.WriteClassBuffer(TGraph2D::Class(),this);
01515 }
01516 }