Logo ROOT   6.08/07
Reference Guide
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",6);
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 
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  if ( gROOT && !gROOT->TestBit( TObject::kInvalidObject ) )
246  gROOT->GetListOfSpecials()->Remove(this);
247 }
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 /// Assignment operator.
251 
253 {
254  if (this != &rhs) {
255  TGraph::operator=(rhs);
256  delete fObjectX;
257  delete fObjectY;
258  fObjectX = rhs.fObjectX->Clone();
259  fObjectY = rhs.fObjectY->Clone();
260  }
261  return *this;
262 }
263 
264 ////////////////////////////////////////////////////////////////////////////////
265 /// Compute the area inside this TCutG
266 /// The algorithm uses Stoke's theorem over the border of the closed polygon.
267 /// Just as a reminder: Stoke's theorem reduces a surface integral
268 /// to a line integral over the border of the surface integral.
269 
271 {
272  Double_t a = 0;
273  Int_t n = GetN();
274  for (Int_t i=0;i<n-1;i++) {
275  a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
276  }
277  a *= 0.5;
278  return a;
279 }
280 
281 ////////////////////////////////////////////////////////////////////////////////
282 /// Compute the center x,y of this TCutG
283 /// The algorithm uses Stoke's theorem over the border of the closed polygon.
284 /// Just as a reminder: Stoke's theorem reduces a surface integral
285 /// to a line integral over the border of the surface integral.
286 
287 void TCutG::Center(Double_t &cx, Double_t &cy) const
288 {
289  Int_t n = GetN();
290  Double_t a = 0;
291  cx = cy = 0;
292  Double_t t;
293  for (Int_t i=0;i<n-1;i++) {
294  t = 2*fX[i]*fY[i] + fY[i]*fX[i+1] + fX[i]*fY[i+1] + 2*fX[i+1]*fY[i+1];
295  cx += (fX[i]-fX[i+1])*t;
296  cy += (-fY[i]+fY[i+1])*t;
297  a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
298  }
299  a *= 0.5;
300  cx *= 1./(6*a);
301  cy *= 1./(6*a);
302 }
303 
304 ////////////////////////////////////////////////////////////////////////////////
305 /// Compute the integral of 2-d histogram h for all bins inside the cut
306 /// if option "width" is specified, the integral is the sum of
307 /// the bin contents multiplied by the bin width in x and in y.
308 
310 {
311  if (!h) return 0;
312  Int_t n = GetN();
313  Double_t xmin= 1e200;
314  Double_t xmax = -xmin;
315  Double_t ymin = xmin;
316  Double_t ymax = xmax;
317  for (Int_t i=0;i<n;i++) {
318  if (fX[i] < xmin) xmin = fX[i];
319  if (fX[i] > xmax) xmax = fX[i];
320  if (fY[i] < ymin) ymin = fY[i];
321  if (fY[i] > ymax) ymax = fY[i];
322  }
323  TAxis *xaxis = h->GetXaxis();
324  TAxis *yaxis = h->GetYaxis();
325  Int_t binx1 = xaxis->FindBin(xmin);
326  Int_t binx2 = xaxis->FindBin(xmax);
327  Int_t biny1 = yaxis->FindBin(ymin);
328  Int_t biny2 = yaxis->FindBin(ymax);
329  Int_t nbinsx = h->GetNbinsX();
330  Stat_t integral = 0;
331 
332  // Loop on bins for which the bin center is in the cut
333  TString opt = option;
334  opt.ToLower();
335  Bool_t width = kFALSE;
336  if (opt.Contains("width")) width = kTRUE;
337  Int_t bin, binx, biny;
338  for (biny=biny1;biny<=biny2;biny++) {
339  Double_t y = yaxis->GetBinCenter(biny);
340  for (binx=binx1;binx<=binx2;binx++) {
341  Double_t x = xaxis->GetBinCenter(binx);
342  if (!IsInside(x,y)) continue;
343  bin = binx +(nbinsx+2)*biny;
344  if (width) integral += h->GetBinContent(bin)*xaxis->GetBinWidth(binx)*yaxis->GetBinWidth(biny);
345  else integral += h->GetBinContent(bin);
346  }
347  }
348  return integral;
349 }
350 
351 ////////////////////////////////////////////////////////////////////////////////
352 /// Save primitive as a C++ statement(s) on output stream out.
353 
354 void TCutG::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
355 {
356  char quote = '"';
357  out<<" "<<std::endl;
358  if (gROOT->ClassSaved(TCutG::Class())) {
359  out<<" ";
360  } else {
361  out<<" TCutG *";
362  }
363  out<<"cutg = new TCutG("<<quote<<GetName()<<quote<<","<<fNpoints<<");"<<std::endl;
364  out<<" cutg->SetVarX("<<quote<<GetVarX()<<quote<<");"<<std::endl;
365  out<<" cutg->SetVarY("<<quote<<GetVarY()<<quote<<");"<<std::endl;
366  out<<" cutg->SetTitle("<<quote<<GetTitle()<<quote<<");"<<std::endl;
367 
368  SaveFillAttributes(out,"cutg",0,1001);
369  SaveLineAttributes(out,"cutg",1,1,1);
370  SaveMarkerAttributes(out,"cutg",1,1,1);
371 
372  for (Int_t i=0;i<fNpoints;i++) {
373  out<<" cutg->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<std::endl;
374  }
375  out<<" cutg->Draw("
376  <<quote<<option<<quote<<");"<<std::endl;
377 }
378 
379 ////////////////////////////////////////////////////////////////////////////////
380 /// Set the X object (and delete the previous one if any).
381 
383 {
384  delete fObjectX;
385  fObjectX = obj;
386 }
387 
388 ////////////////////////////////////////////////////////////////////////////////
389 /// Set the Y object (and delete the previous one if any).
390 
392 {
393  delete fObjectY;
394  fObjectY = obj;
395 }
396 
397 ////////////////////////////////////////////////////////////////////////////////
398 /// Set X variable.
399 
400 void TCutG::SetVarX(const char *varx)
401 {
402  fVarX = varx;
403  delete fObjectX;
404  fObjectX = 0;
405 }
406 
407 ////////////////////////////////////////////////////////////////////////////////
408 /// Set Y variable.
409 
410 void TCutG::SetVarY(const char *vary)
411 {
412  fVarY = vary;
413  delete fObjectY;
414  fObjectY = 0;
415 }
416 
417 ////////////////////////////////////////////////////////////////////////////////
418 /// Stream an object of class TCutG.
419 
420 void TCutG::Streamer(TBuffer &R__b)
421 {
422  if (R__b.IsReading()) {
423  R__b.ReadClassBuffer(TCutG::Class(), this);
424  gROOT->GetListOfSpecials()->Add(this);
425  } else {
426  R__b.WriteClassBuffer(TCutG::Class(), this);
427  }
428 }
TCutG()
TCutG default constructor.
Definition: TCutG.cxx:97
Int_t fNpoints
Current dimension of arrays fX and fY.
Definition: TGraph.h:58
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
Bool_t IsReading() const
Definition: TBuffer.h:83
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:382
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
float ymin
Definition: THbookFile.cxx:93
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
TH1 * h
Definition: legend2.C:5
TObject * fObjectX
! pointer to an object corresponding to X
Definition: TCutG.h:27
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
#define gROOT
Definition: TROOT.h:364
Basic string class.
Definition: TString.h:137
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1089
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 ~TCutG()
TCutG destructor.
Definition: TCutG.cxx:241
const char * Class
Definition: TXMLSetup.cxx:64
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TCutG.cxx:354
TString fVarY
Y variable.
Definition: TCutG.h:26
TCutG & operator=(const TCutG &)
Assignment operator.
Definition: TCutG.cxx:252
const char * GetVarX() const
Definition: TCutG.h:43
Graphical cut class.
Definition: TCutG.h:22
Double_t x[n]
Definition: legend1.C:17
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition: TGraph.cxx:187
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:260
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition: TAxis.cxx:464
virtual TText * GetLine(Int_t number) const
Get Pointer to line number in this pavetext.
Definition: TPaveText.cxx:252
virtual void SetVarY(const char *vary)
Set Y variable.
Definition: TCutG.cxx:410
Base class for several text objects.
Definition: TText.h:33
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:229
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:309
float ymax
Definition: THbookFile.cxx:93
const char * GetVarY() const
Definition: TCutG.h:44
virtual TText * GetLineWith(const char *text) const
Get Pointer to first containing string text in this pavetext.
Definition: TPaveText.cxx:267
Service class for 2-Dim histogram classes.
Definition: TH2.h:36
Class to manage histogram axis.
Definition: TAxis.h:36
if object ctor succeeded but object should not be used
Definition: TObject.h:63
virtual Double_t Area() const
Compute the area inside this TCutG The algorithm uses Stoke&#39;s theorem over the border of the closed p...
Definition: TCutG.cxx:270
virtual void SetObjectY(TObject *obj)
Set the Y object (and delete the previous one if any).
Definition: TCutG.cxx:391
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:232
virtual void SetVarX(const char *varx)
Set X variable.
Definition: TCutG.cxx:400
Int_t GetN() const
Definition: TGraph.h:133
TAxis * GetYaxis()
Definition: TH1.h:325
float xmax
Definition: THbookFile.cxx:93
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:279
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:27
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
TString fVarX
X variable.
Definition: TCutG.h:25
double Stat_t
Definition: RtypesCore.h:73
Mother of all ROOT objects.
Definition: TObject.h:37
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TObject.cxx:204
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
Definition: TAxis.cxx:526
Double_t * fY
Definition: TGraph.h:60
TObject * fObjectY
! pointer to an object corresponding to Y
Definition: TCutG.h:28
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
#define gPad
Definition: TVirtualPad.h:289
virtual Int_t GetNbinsX() const
Definition: TH1.h:301
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:1794
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void Center(Double_t &cx, Double_t &cy) const
Compute the center x,y of this TCutG The algorithm uses Stoke&#39;s theorem over the border of the closed...
Definition: TCutG.cxx:287
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109
TAxis * GetXaxis()
Definition: TH1.h:324
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52