ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TCutG.cxx
Go to the documentation of this file.
1 // @(#)root/graf:$Id$
2 // Author: Rene Brun 16/05/97
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 /** \class TCutG
13 \ingroup BasicGraphics
14 
15 Graphical cut class.
16 
17 A TCutG object is a closed polygon defining a closed region in a x,y plot.
18 It can be created via the graphics editor option "CutG" or directly by
19 invoking its constructor. The first and last points should be the same.
20 
21 To create a TCutG via the graphics editor, use the left button to select the
22 points building the contour of the cut. Click on the right button to close the
23 TCutG. When it is created via the graphics editor, the TCutG object is named
24 "CUTG". It is recommended to immediately change the name by using the context
25 menu item "SetName". When the graphics editor is used, the names of the
26 variables X,Y are automatically taken from the current pad title.
27 
28 Example:
29 
30 Assume a TTree object T and:
31 ~~~ {.cpp}
32  Root > T.Draw("abs(fMomemtum)%fEtot")
33 ~~~
34 the TCutG members fVarX, fVary will be set to:
35 ~~~ {.cpp}
36  fVarx = fEtot
37  fVary = abs(fMomemtum)
38 ~~~
39 A graphical cut can be used in a TTree selection expression:
40 ~~~ {.cpp}
41  Root > T.Draw("fEtot","cutg1")
42 ~~~
43 where "cutg1" is the name of an existing graphical cut.
44 
45 Note that, as shown in the example above, a graphical cut may be used in a
46 selection expression when drawing TTrees expressions of 1-d, 2-d or
47 3-dimensions. The expressions used in TTree::Draw can reference the variables in
48 the fVarX, fVarY of the graphical cut plus other variables.
49 
50 When the TCutG object is created by TTree::Draw, it is added to the list of special objects in
51 the main TROOT object pointed by gROOT. To retrieve a pointer to this object
52 from the code or command line, do:
53 ~~~ {.cpp}
54  TCutG *mycutg;
55  mycutg = (TCutG*)gROOT->GetListOfSpecials()->FindObject("CUTG")
56  mycutg->SetName("mycutg");
57 ~~~
58 When the TCutG is not created via TTree::Draw, one must set the variable names
59 corresponding to x,y if one wants to use the cut as input to TTree::Draw,eg
60 ~~~ {.cpp}
61  TCutG *cutg = new TCutG("mycut",5);
62  cutg->SetVarX("y");
63  cutg->SetVarY("x");
64  cutg->SetPoint(0,-0.3586207,1.509534);
65  cutg->SetPoint(1,-1.894181,-0.529661);
66  cutg->SetPoint(2,0.07780173,-1.21822);
67  cutg->SetPoint(3,-1.0375,-0.07944915);
68  cutg->SetPoint(4,0.756681,0.1853814);
69  cutg->SetPoint(5,-0.3586207,1.509534);
70 ~~~
71 Example of use of a TCutG in TTree::Draw:
72 ~~~ {.cpp}
73  tree.Draw("x:y","mycutg && z>0 %% sqrt(x)>1")
74 ~~~
75 A Graphical cut may be drawn via TGraph::Draw. It can be edited like a normal
76 TGraph.
77 
78 A Graphical cut may be saved to a file via TCutG::Write.
79 */
80 
81 #include <string.h>
82 
83 #include "Riostream.h"
84 #include "TROOT.h"
85 #include "TCutG.h"
86 #include "TVirtualPad.h"
87 #include "TPaveText.h"
88 #include "TH2.h"
89 #include "TClass.h"
90 #include "TMath.h"
91 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// TCutG default constructor.
96 
97 TCutG::TCutG() : TGraph()
98 {
99  fObjectX = 0;
100  fObjectY = 0;
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// TCutG copy constructor
105 
106 TCutG::TCutG(const TCutG &cutg)
107  :TGraph(cutg)
108 {
109  fVarX = cutg.fVarX;
110  fVarY = cutg.fVarY;
111  fObjectX = cutg.fObjectX;
112  fObjectY = cutg.fObjectY;
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// TCutG normal constructor.
117 
118 TCutG::TCutG(const char *name, Int_t n)
119  :TGraph(n)
120 {
121  fObjectX = 0;
122  fObjectY = 0;
123  SetName(name);
124  delete gROOT->GetListOfSpecials()->FindObject(name);
125  gROOT->GetListOfSpecials()->Add(this);
126 
127  // Take name of cut variables from pad title if title contains ":"
128  if (gPad) {
129  TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
130  if (!ptitle) return;
131  TText *ttitle = ptitle->GetLineWith(":");
132  if (!ttitle) ttitle = ptitle->GetLineWith("{");
133  if (!ttitle) ttitle = ptitle->GetLine(0);
134  if (!ttitle) return;
135  const char *title = ttitle->GetTitle();
136  Int_t nch = strlen(title);
137  char *vars = new char[nch+1];
138  strlcpy(vars,title,nch+1);
139  char *col = strstr(vars,":");
140  if (col) {
141  *col = 0;
142  col++;
143  char *brak = strstr(col," {");
144  if (brak) *brak = 0;
145  fVarY = vars;
146  fVarX = col;
147  } else {
148  char *brak = strstr(vars," {");
149  if (brak) *brak = 0;
150  fVarX = vars;
151  }
152  delete [] vars;
153  }
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// TCutG normal constructor.
158 
159 TCutG::TCutG(const char *name, Int_t n, const Float_t *x, const Float_t *y)
160  :TGraph(n,x,y)
161 {
162  fObjectX = 0;
163  fObjectY = 0;
164  SetName(name);
165  delete gROOT->GetListOfSpecials()->FindObject(name);
166  gROOT->GetListOfSpecials()->Add(this);
167 
168  // Take name of cut variables from pad title if title contains ":"
169  if (gPad) {
170  TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
171  if (!ptitle) return;
172  TText *ttitle = ptitle->GetLineWith(":");
173  if (!ttitle) ttitle = ptitle->GetLineWith("{");
174  if (!ttitle) ttitle = ptitle->GetLine(0);
175  if (!ttitle) return;
176  const char *title = ttitle->GetTitle();
177  Int_t nch = strlen(title);
178  char *vars = new char[nch+1];
179  strlcpy(vars,title,nch+1);
180  char *col = strstr(vars,":");
181  if (col) {
182  *col = 0;
183  col++;
184  char *brak = strstr(col," {");
185  if (brak) *brak = 0;
186  fVarY = vars;
187  fVarX = col;
188  } else {
189  char *brak = strstr(vars," {");
190  if (brak) *brak = 0;
191  fVarX = vars;
192  }
193  delete [] vars;
194  }
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// TCutG normal constructor.
199 
200 TCutG::TCutG(const char *name, Int_t n, const Double_t *x, const Double_t *y)
201  :TGraph(n,x,y)
202 {
203  fObjectX = 0;
204  fObjectY = 0;
205  SetName(name);
206  delete gROOT->GetListOfSpecials()->FindObject(name);
207  gROOT->GetListOfSpecials()->Add(this);
208 
209  // Take name of cut variables from pad title if title contains ":"
210  if (gPad) {
211  TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
212  if (!ptitle) return;
213  TText *ttitle = ptitle->GetLineWith(":");
214  if (!ttitle) ttitle = ptitle->GetLineWith("{");
215  if (!ttitle) ttitle = ptitle->GetLine(0);
216  if (!ttitle) return;
217  const char *title = ttitle->GetTitle();
218  Int_t nch = strlen(title);
219  char *vars = new char[nch+1];
220  strlcpy(vars,title,nch+1);
221  char *col = strstr(vars,":");
222  if (col) {
223  *col = 0;
224  col++;
225  char *brak = strstr(col," {");
226  if (brak) *brak = 0;
227  fVarY = vars;
228  fVarX = col;
229  } else {
230  char *brak = strstr(vars," {");
231  if (brak) *brak = 0;
232  fVarX = vars;
233  }
234  delete [] vars;
235  }
236 }
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 /// TCutG destructor.
240 
242 {
243  delete fObjectX;
244  delete fObjectY;
245  gROOT->GetListOfSpecials()->Remove(this);
246 }
247 
248 ////////////////////////////////////////////////////////////////////////////////
249 /// Assignment operator.
250 
252 {
253  if (this != &rhs) {
254  TGraph::operator=(rhs);
255  delete fObjectX;
256  delete fObjectY;
257  fObjectX = rhs.fObjectX->Clone();
258  fObjectY = rhs.fObjectY->Clone();
259  }
260  return *this;
261 }
262 
263 ////////////////////////////////////////////////////////////////////////////////
264 /// Compute the area inside this TCutG
265 /// The algorithm uses Stoke's theorem over the border of the closed polygon.
266 /// Just as a reminder: Stoke's theorem reduces a surface integral
267 /// to a line integral over the border of the surface integral.
268 
270 {
271  Double_t a = 0;
272  Int_t n = GetN();
273  for (Int_t i=0;i<n-1;i++) {
274  a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
275  }
276  a *= 0.5;
277  return a;
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 /// Compute the center x,y of this TCutG
282 /// The algorithm uses Stoke's theorem over the border of the closed polygon.
283 /// Just as a reminder: Stoke's theorem reduces a surface integral
284 /// to a line integral over the border of the surface integral.
285 
286 void TCutG::Center(Double_t &cx, Double_t &cy) const
287 {
288  Int_t n = GetN();
289  Double_t a = 0;
290  cx = cy = 0;
291  Double_t t;
292  for (Int_t i=0;i<n-1;i++) {
293  t = 2*fX[i]*fY[i] + fY[i]*fX[i+1] + fX[i]*fY[i+1] + 2*fX[i+1]*fY[i+1];
294  cx += (fX[i]-fX[i+1])*t;
295  cy += (-fY[i]+fY[i+1])*t;
296  a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
297  }
298  a *= 0.5;
299  cx *= 1./(6*a);
300  cy *= 1./(6*a);
301 }
302 
303 ////////////////////////////////////////////////////////////////////////////////
304 /// Compute the integral of 2-d histogram h for all bins inside the cut
305 /// if option "width" is specified, the integral is the sum of
306 /// the bin contents multiplied by the bin width in x and in y.
307 
309 {
310  if (!h) return 0;
311  Int_t n = GetN();
312  Double_t xmin= 1e200;
313  Double_t xmax = -xmin;
314  Double_t ymin = xmin;
315  Double_t ymax = xmax;
316  for (Int_t i=0;i<n;i++) {
317  if (fX[i] < xmin) xmin = fX[i];
318  if (fX[i] > xmax) xmax = fX[i];
319  if (fY[i] < ymin) ymin = fY[i];
320  if (fY[i] > ymax) ymax = fY[i];
321  }
322  TAxis *xaxis = h->GetXaxis();
323  TAxis *yaxis = h->GetYaxis();
324  Int_t binx1 = xaxis->FindBin(xmin);
325  Int_t binx2 = xaxis->FindBin(xmax);
326  Int_t biny1 = yaxis->FindBin(ymin);
327  Int_t biny2 = yaxis->FindBin(ymax);
328  Int_t nbinsx = h->GetNbinsX();
329  Stat_t integral = 0;
330 
331  // Loop on bins for which the bin center is in the cut
332  TString opt = option;
333  opt.ToLower();
334  Bool_t width = kFALSE;
335  if (opt.Contains("width")) width = kTRUE;
336  Int_t bin, binx, biny;
337  for (biny=biny1;biny<=biny2;biny++) {
338  Double_t y = yaxis->GetBinCenter(biny);
339  for (binx=binx1;binx<=binx2;binx++) {
340  Double_t x = xaxis->GetBinCenter(binx);
341  if (!IsInside(x,y)) continue;
342  bin = binx +(nbinsx+2)*biny;
343  if (width) integral += h->GetBinContent(bin)*xaxis->GetBinWidth(binx)*yaxis->GetBinWidth(biny);
344  else integral += h->GetBinContent(bin);
345  }
346  }
347  return integral;
348 }
349 
350 ////////////////////////////////////////////////////////////////////////////////
351 /// Save primitive as a C++ statement(s) on output stream out.
352 
353 void TCutG::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
354 {
355  char quote = '"';
356  out<<" "<<std::endl;
357  if (gROOT->ClassSaved(TCutG::Class())) {
358  out<<" ";
359  } else {
360  out<<" TCutG *";
361  }
362  out<<"cutg = new TCutG("<<quote<<GetName()<<quote<<","<<fNpoints<<");"<<std::endl;
363  out<<" cutg->SetVarX("<<quote<<GetVarX()<<quote<<");"<<std::endl;
364  out<<" cutg->SetVarY("<<quote<<GetVarY()<<quote<<");"<<std::endl;
365  out<<" cutg->SetTitle("<<quote<<GetTitle()<<quote<<");"<<std::endl;
366 
367  SaveFillAttributes(out,"cutg",0,1001);
368  SaveLineAttributes(out,"cutg",1,1,1);
369  SaveMarkerAttributes(out,"cutg",1,1,1);
370 
371  for (Int_t i=0;i<fNpoints;i++) {
372  out<<" cutg->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<std::endl;
373  }
374  out<<" cutg->Draw("
375  <<quote<<option<<quote<<");"<<std::endl;
376 }
377 
378 ////////////////////////////////////////////////////////////////////////////////
379 /// Set the X object (and delete the previous one if any).
380 
382 {
383  delete fObjectX;
384  fObjectX = obj;
385 }
386 
387 ////////////////////////////////////////////////////////////////////////////////
388 /// Set the Y object (and delete the previous one if any).
389 
391 {
392  delete fObjectY;
393  fObjectY = obj;
394 }
395 
396 ////////////////////////////////////////////////////////////////////////////////
397 /// Set X variable.
398 
399 void TCutG::SetVarX(const char *varx)
400 {
401  fVarX = varx;
402  delete fObjectX;
403  fObjectX = 0;
404 }
405 
406 ////////////////////////////////////////////////////////////////////////////////
407 /// Set Y variable.
408 
409 void TCutG::SetVarY(const char *vary)
410 {
411  fVarY = vary;
412  delete fObjectY;
413  fObjectY = 0;
414 }
415 
416 ////////////////////////////////////////////////////////////////////////////////
417 /// Stream an object of class TCutG.
418 
419 void TCutG::Streamer(TBuffer &R__b)
420 {
421  if (R__b.IsReading()) {
422  R__b.ReadClassBuffer(TCutG::Class(), this);
423  gROOT->GetListOfSpecials()->Add(this);
424  } else {
425  R__b.WriteClassBuffer(TCutG::Class(), this);
426  }
427 }
TCutG()
pointer to an object corresponding to Y
Int_t fNpoints
Current dimension of arrays fX and fY.
Definition: TGraph.h:58
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
float xmin
Definition: THbookFile.cxx:93
Double_t * fX
Definition: TGraph.h:59
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual void SetObjectX(TObject *obj)
Set the X object (and delete the previous one if any).
Definition: TCutG.cxx:381
ClassImp(TCutG) TCutG
TCutG default constructor.
Definition: TCutG.cxx:92
Bool_t IsReading() const
Definition: TBuffer.h:83
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
float ymin
Definition: THbookFile.cxx:93
virtual Int_t IsInside(Double_t x, Double_t y) const
Return 1 if the point (x,y) is inside the polygon defined by the graph vertices 0 otherwise...
Definition: TGraph.cxx:1771
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
TH1 * h
Definition: legend2.C:5
TObject * fObjectX
Definition: TCutG.h:34
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
#define gROOT
Definition: TROOT.h:344
Basic string class.
Definition: TString.h:137
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1075
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
Definition: TAxis.cxx:511
virtual ~TCutG()
TCutG destructor.
Definition: TCutG.cxx:241
virtual Int_t GetNbinsX() const
Definition: TH1.h:296
Int_t GetN() const
Definition: TGraph.h:132
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TCutG.cxx:353
TString fVarY
Definition: TCutG.h:33
TCutG & operator=(const TCutG &)
Assignment operator.
Definition: TCutG.cxx:251
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TObject.cxx:203
Graphical cut class.
Definition: TCutG.h:29
Double_t x[n]
Definition: legend1.C:17
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition: TGraph.cxx:180
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition: TAttLine.cxx:257
void Class()
Definition: Class.C:29
virtual void SetVarY(const char *vary)
Set Y variable.
Definition: TCutG.cxx:409
Base class for several text objects.
Definition: TText.h:42
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition: TAttMarker.cxx:226
char * out
Definition: TBase64.cxx:29
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
virtual Double_t Area() const
Compute the area inside this TCutG The algorithm uses Stoke's theorem over the border of the closed p...
Definition: TCutG.cxx:269
virtual TText * GetLine(Int_t number) const
Get Pointer to line number in this pavetext.
Definition: TPaveText.cxx:252
TThread * t[5]
Definition: threadsh1.C:13
float ymax
Definition: THbookFile.cxx:93
Service class for 2-Dim histogram classes.
Definition: TH2.h:36
Class to manage histogram axis.
Definition: TAxis.h:36
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
virtual void SetObjectY(TObject *obj)
Set the Y object (and delete the previous one if any).
Definition: TCutG.cxx:390
virtual void Center(Double_t &cx, Double_t &cy) const
Compute the center x,y of this TCutG The algorithm uses Stoke's theorem over the border of the closed...
Definition: TCutG.cxx:286
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition: TAttFill.cxx:229
virtual void SetVarX(const char *varx)
Set X variable.
Definition: TCutG.cxx:399
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
TAxis * GetYaxis()
Definition: TH1.h:320
float xmax
Definition: THbookFile.cxx:93
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:264
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:35
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
TString fVarX
Definition: TCutG.h:32
double Stat_t
Definition: RtypesCore.h:73
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition: TAxis.cxx:449
Mother of all ROOT objects.
Definition: TObject.h:58
Double_t * fY
Definition: TGraph.h:60
TObject * fObjectY
pointer to an object corresponding to X
Definition: TCutG.h:35
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
const char * GetVarY() const
Definition: TCutG.h:51
#define gPad
Definition: TVirtualPad.h:288
virtual Double_t IntegralHist(TH2 *h, Option_t *option="") const
Compute the integral of 2-d histogram h for all bins inside the cut if option "width" is specified...
Definition: TCutG.cxx:308
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
const Int_t n
Definition: legend1.C:16
TAxis * GetXaxis()
Definition: TH1.h:319
const char * GetVarX() const
Definition: TCutG.h:50
virtual TText * GetLineWith(const char *text) const
Get Pointer to first containing string text in this pavetext.
Definition: TPaveText.cxx:267