#include <string.h>
#include "Riostream.h"
#include "TROOT.h"
#include "TCutG.h"
#include "TVirtualPad.h"
#include "TPaveText.h"
#include "TH2.h"
#include "TClass.h"
#include "TMath.h"
ClassImp(TCutG)
TCutG::TCutG() : TGraph()
{
fObjectX = 0;
fObjectY = 0;
}
TCutG::TCutG(const TCutG &cutg)
:TGraph(cutg)
{
fVarX = cutg.fVarX;
fVarY = cutg.fVarY;
fObjectX = cutg.fObjectX;
fObjectY = cutg.fObjectY;
}
TCutG::TCutG(const char *name, Int_t n)
:TGraph(n)
{
fObjectX = 0;
fObjectY = 0;
SetName(name);
delete gROOT->GetListOfSpecials()->FindObject(name);
gROOT->GetListOfSpecials()->Add(this);
if (gPad) {
TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
if (!ptitle) return;
TText *ttitle = ptitle->GetLineWith(":");
if (!ttitle) ttitle = ptitle->GetLineWith("{");
if (!ttitle) ttitle = ptitle->GetLine(0);
if (!ttitle) return;
const char *title = ttitle->GetTitle();
Int_t nch = strlen(title);
char *vars = new char[nch+1];
strlcpy(vars,title,nch+1);
char *col = strstr(vars,":");
if (col) {
*col = 0;
col++;
char *brak = strstr(col," {");
if (brak) *brak = 0;
fVarY = vars;
fVarX = col;
} else {
char *brak = strstr(vars," {");
if (brak) *brak = 0;
fVarX = vars;
}
delete [] vars;
}
}
TCutG::TCutG(const char *name, Int_t n, const Float_t *x, const Float_t *y)
:TGraph(n,x,y)
{
fObjectX = 0;
fObjectY = 0;
SetName(name);
delete gROOT->GetListOfSpecials()->FindObject(name);
gROOT->GetListOfSpecials()->Add(this);
if (gPad) {
TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
if (!ptitle) return;
TText *ttitle = ptitle->GetLineWith(":");
if (!ttitle) ttitle = ptitle->GetLineWith("{");
if (!ttitle) ttitle = ptitle->GetLine(0);
if (!ttitle) return;
const char *title = ttitle->GetTitle();
Int_t nch = strlen(title);
char *vars = new char[nch+1];
strlcpy(vars,title,nch+1);
char *col = strstr(vars,":");
if (col) {
*col = 0;
col++;
char *brak = strstr(col," {");
if (brak) *brak = 0;
fVarY = vars;
fVarX = col;
} else {
char *brak = strstr(vars," {");
if (brak) *brak = 0;
fVarX = vars;
}
delete [] vars;
}
}
TCutG::TCutG(const char *name, Int_t n, const Double_t *x, const Double_t *y)
:TGraph(n,x,y)
{
fObjectX = 0;
fObjectY = 0;
SetName(name);
delete gROOT->GetListOfSpecials()->FindObject(name);
gROOT->GetListOfSpecials()->Add(this);
if (gPad) {
TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
if (!ptitle) return;
TText *ttitle = ptitle->GetLineWith(":");
if (!ttitle) ttitle = ptitle->GetLineWith("{");
if (!ttitle) ttitle = ptitle->GetLine(0);
if (!ttitle) return;
const char *title = ttitle->GetTitle();
Int_t nch = strlen(title);
char *vars = new char[nch+1];
strlcpy(vars,title,nch+1);
char *col = strstr(vars,":");
if (col) {
*col = 0;
col++;
char *brak = strstr(col," {");
if (brak) *brak = 0;
fVarY = vars;
fVarX = col;
} else {
char *brak = strstr(vars," {");
if (brak) *brak = 0;
fVarX = vars;
}
delete [] vars;
}
}
TCutG::~TCutG()
{
delete fObjectX;
delete fObjectY;
gROOT->GetListOfSpecials()->Remove(this);
}
TCutG &TCutG::operator=(const TCutG &rhs)
{
if (this != &rhs) {
TGraph::operator=(rhs);
delete fObjectX;
delete fObjectY;
fObjectX = rhs.fObjectX->Clone();
fObjectY = rhs.fObjectY->Clone();
}
return *this;
}
Double_t TCutG::Area() const
{
Double_t a = 0;
Int_t n = GetN();
for (Int_t i=0;i<n-1;i++) {
a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
}
a *= 0.5;
return a;
}
void TCutG::Center(Double_t &cx, Double_t &cy) const
{
Int_t n = GetN();
Double_t a = 0;
cx = cy = 0;
Double_t t;
for (Int_t i=0;i<n-1;i++) {
t = 2*fX[i]*fY[i] + fY[i]*fX[i+1] + fX[i]*fY[i+1] + 2*fX[i+1]*fY[i+1];
cx += (fX[i]-fX[i+1])*t;
cy += (-fY[i]+fY[i+1])*t;
a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
}
a *= 0.5;
cx *= 1./(6*a);
cy *= 1./(6*a);
}
Double_t TCutG::IntegralHist(TH2 *h, Option_t *option) const
{
if (!h) return 0;
Int_t n = GetN();
Double_t xmin= 1e200;
Double_t xmax = -xmin;
Double_t ymin = xmin;
Double_t ymax = xmax;
for (Int_t i=0;i<n;i++) {
if (fX[i] < xmin) xmin = fX[i];
if (fX[i] > xmax) xmax = fX[i];
if (fY[i] < ymin) ymin = fY[i];
if (fY[i] > ymax) ymax = fY[i];
}
TAxis *xaxis = h->GetXaxis();
TAxis *yaxis = h->GetYaxis();
Int_t binx1 = xaxis->FindBin(xmin);
Int_t binx2 = xaxis->FindBin(xmax);
Int_t biny1 = yaxis->FindBin(ymin);
Int_t biny2 = yaxis->FindBin(ymax);
Int_t nbinsx = h->GetNbinsX();
Stat_t integral = 0;
TString opt = option;
opt.ToLower();
Bool_t width = kFALSE;
if (opt.Contains("width")) width = kTRUE;
Int_t bin, binx, biny;
for (biny=biny1;biny<=biny2;biny++) {
Double_t y = yaxis->GetBinCenter(biny);
for (binx=binx1;binx<=binx2;binx++) {
Double_t x = xaxis->GetBinCenter(binx);
if (!IsInside(x,y)) continue;
bin = binx +(nbinsx+2)*biny;
if (width) integral += h->GetBinContent(bin)*xaxis->GetBinWidth(binx)*yaxis->GetBinWidth(biny);
else integral += h->GetBinContent(bin);
}
}
return integral;
}
void TCutG::SavePrimitive(std::ostream &out, Option_t *option )
{
char quote = '"';
out<<" "<<std::endl;
if (gROOT->ClassSaved(TCutG::Class())) {
out<<" ";
} else {
out<<" TCutG *";
}
out<<"cutg = new TCutG("<<quote<<GetName()<<quote<<","<<fNpoints<<");"<<std::endl;
out<<" cutg->SetVarX("<<quote<<GetVarX()<<quote<<");"<<std::endl;
out<<" cutg->SetVarY("<<quote<<GetVarY()<<quote<<");"<<std::endl;
out<<" cutg->SetTitle("<<quote<<GetTitle()<<quote<<");"<<std::endl;
SaveFillAttributes(out,"cutg",0,1001);
SaveLineAttributes(out,"cutg",1,1,1);
SaveMarkerAttributes(out,"cutg",1,1,1);
for (Int_t i=0;i<fNpoints;i++) {
out<<" cutg->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<std::endl;
}
out<<" cutg->Draw("
<<quote<<option<<quote<<");"<<std::endl;
}
void TCutG::SetObjectX(TObject *obj)
{
delete fObjectX;
fObjectX = obj;
}
void TCutG::SetObjectY(TObject *obj)
{
delete fObjectY;
fObjectY = obj;
}
void TCutG::SetVarX(const char *varx)
{
fVarX = varx;
delete fObjectX;
fObjectX = 0;
}
void TCutG::SetVarY(const char *vary)
{
fVarY = vary;
delete fObjectY;
fObjectY = 0;
}
void TCutG::Streamer(TBuffer &R__b)
{
if (R__b.IsReading()) {
R__b.ReadClassBuffer(TCutG::Class(), this);
gROOT->GetListOfSpecials()->Add(this);
} else {
R__b.WriteClassBuffer(TCutG::Class(), this);
}
}