00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
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 #include <string.h>
00087 
00088 #include "Riostream.h"
00089 #include "TROOT.h"
00090 #include "TCutG.h"
00091 #include "TVirtualPad.h"
00092 #include "TPaveText.h"
00093 #include "TH2.h"
00094 #include "TClass.h"
00095 #include "TMath.h"
00096 
00097 ClassImp(TCutG)
00098 
00099 
00100 
00101 TCutG::TCutG() : TGraph()
00102 {
00103    
00104 
00105    fObjectX  = 0;
00106    fObjectY  = 0;
00107 }
00108 
00109 
00110 
00111 TCutG::TCutG(const TCutG &cutg)
00112       :TGraph(cutg)
00113 {
00114    
00115 
00116    fVarX    = cutg.fVarX;
00117    fVarY    = cutg.fVarY;
00118    fObjectX = cutg.fObjectX;
00119    fObjectY = cutg.fObjectY;
00120 }
00121 
00122 
00123 
00124 TCutG::TCutG(const char *name, Int_t n)
00125       :TGraph(n)
00126 {
00127    
00128 
00129    fObjectX  = 0;
00130    fObjectY  = 0;
00131    SetName(name);
00132    delete gROOT->GetListOfSpecials()->FindObject(name);
00133    gROOT->GetListOfSpecials()->Add(this);
00134 
00135    
00136    if (gPad) {
00137       TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
00138       if (!ptitle) return;
00139       TText *ttitle = ptitle->GetLineWith(":");
00140       if (!ttitle) ttitle = ptitle->GetLineWith("{");
00141       if (!ttitle) ttitle = ptitle->GetLine(0);
00142       if (!ttitle) return;
00143       const char *title = ttitle->GetTitle();
00144       Int_t nch = strlen(title);
00145       char *vars = new char[nch+1];
00146       strlcpy(vars,title,nch+1);
00147       char *col = strstr(vars,":");
00148       if (col) {
00149          *col = 0;
00150          col++;
00151          char *brak = strstr(col," {");
00152          if (brak) *brak = 0;
00153          fVarY = vars;
00154          fVarX = col;
00155       } else {
00156          char *brak = strstr(vars," {");
00157          if (brak) *brak = 0;
00158          fVarX = vars;
00159       }
00160       delete [] vars;
00161    }
00162 }
00163 
00164 
00165 
00166 TCutG::TCutG(const char *name, Int_t n, const Float_t *x, const Float_t *y)
00167       :TGraph(n,x,y)
00168 {
00169    
00170 
00171    fObjectX  = 0;
00172    fObjectY  = 0;
00173    SetName(name);
00174    delete gROOT->GetListOfSpecials()->FindObject(name);
00175    gROOT->GetListOfSpecials()->Add(this);
00176 
00177    
00178    if (gPad) {
00179       TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
00180       if (!ptitle) return;
00181       TText *ttitle = ptitle->GetLineWith(":");
00182       if (!ttitle) ttitle = ptitle->GetLineWith("{");
00183       if (!ttitle) ttitle = ptitle->GetLine(0);
00184       if (!ttitle) return;
00185       const char *title = ttitle->GetTitle();
00186       Int_t nch = strlen(title);
00187       char *vars = new char[nch+1];
00188       strlcpy(vars,title,nch+1);
00189       char *col = strstr(vars,":");
00190       if (col) {
00191          *col = 0;
00192          col++;
00193          char *brak = strstr(col," {");
00194          if (brak) *brak = 0;
00195          fVarY = vars;
00196          fVarX = col;
00197       } else {
00198          char *brak = strstr(vars," {");
00199          if (brak) *brak = 0;
00200          fVarX = vars;
00201       }
00202       delete [] vars;
00203    }
00204 }
00205 
00206 
00207 
00208 TCutG::TCutG(const char *name, Int_t n, const Double_t *x, const Double_t *y)
00209       :TGraph(n,x,y)
00210 {
00211    
00212 
00213    fObjectX  = 0;
00214    fObjectY  = 0;
00215    SetName(name);
00216    delete gROOT->GetListOfSpecials()->FindObject(name);
00217    gROOT->GetListOfSpecials()->Add(this);
00218 
00219    
00220    if (gPad) {
00221       TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
00222       if (!ptitle) return;
00223       TText *ttitle = ptitle->GetLineWith(":");
00224       if (!ttitle) ttitle = ptitle->GetLineWith("{");
00225       if (!ttitle) ttitle = ptitle->GetLine(0);
00226       if (!ttitle) return;
00227       const char *title = ttitle->GetTitle();
00228       Int_t nch = strlen(title);
00229       char *vars = new char[nch+1];
00230       strlcpy(vars,title,nch+1);
00231       char *col = strstr(vars,":");
00232       if (col) {
00233          *col = 0;
00234          col++;
00235          char *brak = strstr(col," {");
00236          if (brak) *brak = 0;
00237          fVarY = vars;
00238          fVarX = col;
00239       } else {
00240          char *brak = strstr(vars," {");
00241          if (brak) *brak = 0;
00242          fVarX = vars;
00243       }
00244       delete [] vars;
00245    }
00246 }
00247 
00248 
00249 
00250 TCutG::~TCutG()
00251 {
00252    
00253 
00254    delete fObjectX;
00255    delete fObjectY;
00256    gROOT->GetListOfSpecials()->Remove(this);
00257 }
00258 
00259 
00260 Double_t TCutG::Area() const
00261 {
00262    
00263    
00264    
00265    
00266    Double_t a = 0;
00267    Int_t n = GetN();
00268    for (Int_t i=0;i<n-1;i++) {
00269       a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
00270    }
00271    a *= 0.5;
00272    return a;   
00273 }
00274 
00275 
00276 void TCutG::Center(Double_t &cx, Double_t &cy) const
00277 {
00278    
00279    
00280    
00281    
00282    Int_t n = GetN();
00283    Double_t a  = 0;
00284    cx = cy = 0;
00285    Double_t t;
00286    for (Int_t i=0;i<n-1;i++) {
00287       t   = 2*fX[i]*fY[i] + fY[i]*fX[i+1] + fX[i]*fY[i+1] + 2*fX[i+1]*fY[i+1];
00288       cx += (fX[i]-fX[i+1])*t;
00289       cy += (-fY[i]+fY[i+1])*t;
00290       a  += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
00291    }
00292    a  *= 0.5;
00293    cx *= 1./(6*a);
00294    cy *= 1./(6*a);
00295 }
00296 
00297 
00298 Double_t TCutG::IntegralHist(TH2 *h, Option_t *option) const
00299 {
00300    
00301    
00302    
00303 
00304    if (!h) return 0;
00305    Int_t n = GetN();
00306    Double_t xmin= 1e200;
00307    Double_t xmax = -xmin;
00308    Double_t ymin = xmin;
00309    Double_t ymax = xmax;
00310    for (Int_t i=0;i<n;i++) {
00311       if (fX[i] < xmin) xmin = fX[i];
00312       if (fX[i] > xmax) xmax = fX[i];
00313       if (fY[i] < ymin) ymin = fY[i];
00314       if (fY[i] > ymax) ymax = fY[i];
00315    }
00316    TAxis *xaxis = h->GetXaxis();
00317    TAxis *yaxis = h->GetYaxis();
00318    Int_t binx1 = xaxis->FindBin(xmin);
00319    Int_t binx2 = xaxis->FindBin(xmax);
00320    Int_t biny1 = yaxis->FindBin(ymin);
00321    Int_t biny2 = yaxis->FindBin(ymax);
00322    Int_t nbinsx = h->GetNbinsX();
00323    Stat_t integral = 0;
00324 
00325    
00326    TString opt = option;
00327    opt.ToLower();
00328    Bool_t width = kFALSE;
00329    if (opt.Contains("width")) width = kTRUE;
00330    Int_t bin, binx, biny;
00331    for (biny=biny1;biny<=biny2;biny++) {
00332       Double_t y = yaxis->GetBinCenter(biny);
00333       for (binx=binx1;binx<=binx2;binx++) {
00334          Double_t x = xaxis->GetBinCenter(binx);
00335          if (!IsInside(x,y)) continue;
00336          bin = binx +(nbinsx+2)*biny;
00337          if (width) integral += h->GetBinContent(bin)*xaxis->GetBinWidth(binx)*yaxis->GetBinWidth(biny);
00338          else       integral += h->GetBinContent(bin);
00339       }
00340    }
00341    return integral;
00342 }
00343 
00344 
00345 
00346 void TCutG::SavePrimitive(ostream &out, Option_t *option )
00347 {
00348    
00349 
00350    char quote = '"';
00351    out<<"   "<<endl;
00352    if (gROOT->ClassSaved(TCutG::Class())) {
00353       out<<"   ";
00354    } else {
00355       out<<"   TCutG *";
00356    }
00357    out<<"cutg = new TCutG("<<quote<<GetName()<<quote<<","<<fNpoints<<");"<<endl;
00358    out<<"   cutg->SetVarX("<<quote<<GetVarX()<<quote<<");"<<endl;
00359    out<<"   cutg->SetVarY("<<quote<<GetVarY()<<quote<<");"<<endl;
00360    out<<"   cutg->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
00361 
00362    SaveFillAttributes(out,"cutg",0,1001);
00363    SaveLineAttributes(out,"cutg",1,1,1);
00364    SaveMarkerAttributes(out,"cutg",1,1,1);
00365 
00366    for (Int_t i=0;i<fNpoints;i++) {
00367       out<<"   cutg->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<endl;
00368    }
00369    out<<"   cutg->Draw("
00370       <<quote<<option<<quote<<");"<<endl;
00371 }
00372 
00373 
00374 void TCutG::SetObjectX(TObject *obj) 
00375 {
00376    
00377 
00378    delete fObjectX;
00379    fObjectX = obj;
00380 }
00381 
00382 
00383 void TCutG::SetObjectY(TObject *obj)
00384 {
00385    
00386    
00387    delete fObjectY;
00388    fObjectY = obj;
00389 }
00390 
00391 
00392 void TCutG::SetVarX(const char *varx)
00393 {
00394    
00395 
00396    fVarX = varx;
00397    delete fObjectX;
00398    fObjectX = 0;
00399 }
00400 
00401 
00402 
00403 void TCutG::SetVarY(const char *vary)
00404 {
00405    
00406 
00407    fVarY = vary;
00408    delete fObjectY;
00409    fObjectY = 0;
00410 }
00411 
00412 
00413 
00414 void TCutG::Streamer(TBuffer &R__b)
00415 {
00416    
00417 
00418    if (R__b.IsReading()) {
00419       R__b.ReadClassBuffer(TCutG::Class(), this);
00420       gROOT->GetListOfSpecials()->Add(this);
00421    } else {
00422       R__b.WriteClassBuffer(TCutG::Class(), this);
00423    }
00424 }