Logo ROOT   master
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. Being a TGraph the drawing options and behavior relatives to graphs apply.
77 They are listed in the TGraphPainter description.
78 See in particular "Graphs in logarithmic scale".
79 
80 A Graphical cut may be saved to a file via TCutG::Write.
81 */
82 
83 #include <string.h>
84 
85 #include "Riostream.h"
86 #include "TROOT.h"
87 #include "TCutG.h"
88 #include "TBuffer.h"
89 #include "TVirtualPad.h"
90 #include "TPaveText.h"
91 #include "TH2.h"
92 #include "TClass.h"
93 #include "TMath.h"
94 
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// TCutG default constructor.
99 
101 {
102  fObjectX = 0;
103  fObjectY = 0;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// TCutG copy constructor
108 
109 TCutG::TCutG(const TCutG &cutg)
110  :TGraph(cutg)
111 {
112  fVarX = cutg.fVarX;
113  fVarY = cutg.fVarY;
114  fObjectX = cutg.fObjectX ? cutg.fObjectX->Clone() : nullptr;
115  fObjectY = cutg.fObjectY ? cutg.fObjectY->Clone() : nullptr;
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// TCutG normal constructor.
120 
121 TCutG::TCutG(const char *name, Int_t n)
122  :TGraph(n)
123 {
124  fObjectX = 0;
125  fObjectY = 0;
126  SetName(name);
127  delete gROOT->GetListOfSpecials()->FindObject(name);
128  gROOT->GetListOfSpecials()->Add(this);
129 
130  // Take name of cut variables from pad title if title contains ":"
131  if (gPad) {
132  TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
133  if (!ptitle) return;
134  TText *ttitle = ptitle->GetLineWith(":");
135  if (!ttitle) ttitle = ptitle->GetLineWith("{");
136  if (!ttitle) ttitle = ptitle->GetLine(0);
137  if (!ttitle) return;
138  const char *title = ttitle->GetTitle();
139  Int_t nch = strlen(title);
140  char *vars = new char[nch+1];
141  strlcpy(vars,title,nch+1);
142  char *col = strstr(vars,":");
143  if (col) {
144  *col = 0;
145  col++;
146  char *brak = strstr(col," {");
147  if (brak) *brak = 0;
148  fVarY = vars;
149  fVarX = col;
150  } else {
151  char *brak = strstr(vars," {");
152  if (brak) *brak = 0;
153  fVarX = vars;
154  }
155  delete [] vars;
156  }
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// TCutG normal constructor.
161 
162 TCutG::TCutG(const char *name, Int_t n, const Float_t *x, const Float_t *y)
163  :TGraph(n,x,y)
164 {
165  fObjectX = 0;
166  fObjectY = 0;
167  SetName(name);
168  delete gROOT->GetListOfSpecials()->FindObject(name);
169  gROOT->GetListOfSpecials()->Add(this);
170 
171  // Take name of cut variables from pad title if title contains ":"
172  if (gPad) {
173  TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
174  if (!ptitle) return;
175  TText *ttitle = ptitle->GetLineWith(":");
176  if (!ttitle) ttitle = ptitle->GetLineWith("{");
177  if (!ttitle) ttitle = ptitle->GetLine(0);
178  if (!ttitle) return;
179  const char *title = ttitle->GetTitle();
180  Int_t nch = strlen(title);
181  char *vars = new char[nch+1];
182  strlcpy(vars,title,nch+1);
183  char *col = strstr(vars,":");
184  if (col) {
185  *col = 0;
186  col++;
187  char *brak = strstr(col," {");
188  if (brak) *brak = 0;
189  fVarY = vars;
190  fVarX = col;
191  } else {
192  char *brak = strstr(vars," {");
193  if (brak) *brak = 0;
194  fVarX = vars;
195  }
196  delete [] vars;
197  }
198 }
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// TCutG normal constructor.
202 
203 TCutG::TCutG(const char *name, Int_t n, const Double_t *x, const Double_t *y)
204  :TGraph(n,x,y)
205 {
206  fObjectX = 0;
207  fObjectY = 0;
208  SetName(name);
209  delete gROOT->GetListOfSpecials()->FindObject(name);
210  gROOT->GetListOfSpecials()->Add(this);
211 
212  // Take name of cut variables from pad title if title contains ":"
213  if (gPad) {
214  TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
215  if (!ptitle) return;
216  TText *ttitle = ptitle->GetLineWith(":");
217  if (!ttitle) ttitle = ptitle->GetLineWith("{");
218  if (!ttitle) ttitle = ptitle->GetLine(0);
219  if (!ttitle) return;
220  const char *title = ttitle->GetTitle();
221  Int_t nch = strlen(title);
222  char *vars = new char[nch+1];
223  strlcpy(vars,title,nch+1);
224  char *col = strstr(vars,":");
225  if (col) {
226  *col = 0;
227  col++;
228  char *brak = strstr(col," {");
229  if (brak) *brak = 0;
230  fVarY = vars;
231  fVarX = col;
232  } else {
233  char *brak = strstr(vars," {");
234  if (brak) *brak = 0;
235  fVarX = vars;
236  }
237  delete [] vars;
238  }
239 }
240 
241 ////////////////////////////////////////////////////////////////////////////////
242 /// TCutG destructor.
243 
245 {
246  delete fObjectX;
247  delete fObjectY;
248  if ( gROOT && !gROOT->TestBit( TObject::kInvalidObject ) )
249  gROOT->GetListOfSpecials()->Remove(this);
250 }
251 
252 ////////////////////////////////////////////////////////////////////////////////
253 /// Assignment operator.
254 
256 {
257  if (this != &rhs) {
258  TGraph::operator=(rhs);
259  delete fObjectX;
260  delete fObjectY;
261  fObjectX = rhs.fObjectX ? rhs.fObjectX->Clone() : nullptr;
262  fObjectY = rhs.fObjectY ? rhs.fObjectY->Clone() : nullptr;
263  }
264  return *this;
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Compute the area inside this TCutG
269 /// The algorithm uses Stoke's theorem over the border of the closed polygon.
270 /// Just as a reminder: Stoke's theorem reduces a surface integral
271 /// to a line integral over the border of the surface integral.
272 
274 {
275  Double_t a = 0;
276  Int_t n = GetN();
277  for (Int_t i=0;i<n-1;i++) {
278  a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
279  }
280  a *= 0.5;
281  return a;
282 }
283 
284 ////////////////////////////////////////////////////////////////////////////////
285 /// Compute the center x,y of this TCutG
286 /// The algorithm uses Stoke's theorem over the border of the closed polygon.
287 /// Just as a reminder: Stoke's theorem reduces a surface integral
288 /// to a line integral over the border of the surface integral.
289 
290 void TCutG::Center(Double_t &cx, Double_t &cy) const
291 {
292  Int_t n = GetN();
293  Double_t a = 0;
294  cx = cy = 0;
295  Double_t t;
296  for (Int_t i=0;i<n-1;i++) {
297  t = 2*fX[i]*fY[i] + fY[i]*fX[i+1] + fX[i]*fY[i+1] + 2*fX[i+1]*fY[i+1];
298  cx += (fX[i]-fX[i+1])*t;
299  cy += (-fY[i]+fY[i+1])*t;
300  a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
301  }
302  a *= 0.5;
303  cx *= 1./(6*a);
304  cy *= 1./(6*a);
305 }
306 
307 ////////////////////////////////////////////////////////////////////////////////
308 /// Compute the integral of 2-d histogram h for all bins inside the cut
309 /// if option "width" is specified, the integral is the sum of
310 /// the bin contents multiplied by the bin width in x and in y.
311 
313 {
314  if (!h) return 0;
315  Int_t n = GetN();
316  Double_t xmin= 1e200;
317  Double_t xmax = -xmin;
318  Double_t ymin = xmin;
319  Double_t ymax = xmax;
320  for (Int_t i=0;i<n;i++) {
321  if (fX[i] < xmin) xmin = fX[i];
322  if (fX[i] > xmax) xmax = fX[i];
323  if (fY[i] < ymin) ymin = fY[i];
324  if (fY[i] > ymax) ymax = fY[i];
325  }
326  TAxis *xaxis = h->GetXaxis();
327  TAxis *yaxis = h->GetYaxis();
328  Int_t binx1 = xaxis->FindBin(xmin);
329  Int_t binx2 = xaxis->FindBin(xmax);
330  Int_t biny1 = yaxis->FindBin(ymin);
331  Int_t biny2 = yaxis->FindBin(ymax);
332  Int_t nbinsx = h->GetNbinsX();
333  Stat_t integral = 0;
334 
335  // Loop on bins for which the bin center is in the cut
336  TString opt = option;
337  opt.ToLower();
338  Bool_t width = kFALSE;
339  if (opt.Contains("width")) width = kTRUE;
340  Int_t bin, binx, biny;
341  for (biny=biny1;biny<=biny2;biny++) {
342  Double_t y = yaxis->GetBinCenter(biny);
343  for (binx=binx1;binx<=binx2;binx++) {
344  Double_t x = xaxis->GetBinCenter(binx);
345  if (!IsInside(x,y)) continue;
346  bin = binx +(nbinsx+2)*biny;
347  if (width) integral += h->GetBinContent(bin)*xaxis->GetBinWidth(binx)*yaxis->GetBinWidth(biny);
348  else integral += h->GetBinContent(bin);
349  }
350  }
351  return integral;
352 }
353 
354 ////////////////////////////////////////////////////////////////////////////////
355 /// Save primitive as a C++ statement(s) on output stream out.
356 
357 void TCutG::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
358 {
359  char quote = '"';
360  out<<" "<<std::endl;
361  if (gROOT->ClassSaved(TCutG::Class())) {
362  out<<" ";
363  } else {
364  out<<" TCutG *";
365  }
366  out<<"cutg = new TCutG("<<quote<<GetName()<<quote<<","<<fNpoints<<");"<<std::endl;
367  out<<" cutg->SetVarX("<<quote<<GetVarX()<<quote<<");"<<std::endl;
368  out<<" cutg->SetVarY("<<quote<<GetVarY()<<quote<<");"<<std::endl;
369  out<<" cutg->SetTitle("<<quote<<GetTitle()<<quote<<");"<<std::endl;
370 
371  SaveFillAttributes(out,"cutg",0,1001);
372  SaveLineAttributes(out,"cutg",1,1,1);
373  SaveMarkerAttributes(out,"cutg",1,1,1);
374 
375  for (Int_t i=0;i<fNpoints;i++) {
376  out<<" cutg->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<std::endl;
377  }
378  out<<" cutg->Draw("
379  <<quote<<option<<quote<<");"<<std::endl;
380 }
381 
382 ////////////////////////////////////////////////////////////////////////////////
383 /// Set the X object (and delete the previous one if any).
384 
386 {
387  delete fObjectX;
388  fObjectX = obj;
389 }
390 
391 ////////////////////////////////////////////////////////////////////////////////
392 /// Set the Y object (and delete the previous one if any).
393 
395 {
396  delete fObjectY;
397  fObjectY = obj;
398 }
399 
400 ////////////////////////////////////////////////////////////////////////////////
401 /// Set X variable.
402 
403 void TCutG::SetVarX(const char *varx)
404 {
405  fVarX = varx;
406  delete fObjectX;
407  fObjectX = 0;
408 }
409 
410 ////////////////////////////////////////////////////////////////////////////////
411 /// Set Y variable.
412 
413 void TCutG::SetVarY(const char *vary)
414 {
415  fVarY = vary;
416  delete fObjectY;
417  fObjectY = 0;
418 }
419 
420 ////////////////////////////////////////////////////////////////////////////////
421 /// Stream an object of class TCutG.
422 
423 void TCutG::Streamer(TBuffer &R__b)
424 {
425  if (R__b.IsReading()) {
426  R__b.ReadClassBuffer(TCutG::Class(), this);
427  gROOT->GetListOfSpecials()->Add(this);
428  } else {
429  R__b.WriteClassBuffer(TCutG::Class(), this);
430  }
431 }
TCutG()
TCutG default constructor.
Definition: TCutG.cxx:100
Int_t fNpoints
Number of points <= fMaxSize.
Definition: TGraph.h:46
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Bool_t IsReading() const
Definition: TBuffer.h:85
float xmin
Definition: THbookFile.cxx:93
Double_t * fX
[fNpoints] array of X points
Definition: TGraph.h:47
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:385
float Float_t
Definition: RtypesCore.h:55
const char Option_t
Definition: RtypesCore.h:64
float ymin
Definition: THbookFile.cxx:93
TObject * fObjectX
! pointer to an object corresponding to X
Definition: TCutG.h:25
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
#define gROOT
Definition: TROOT.h:404
Basic string class.
Definition: TString.h:131
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1125
virtual ~TCutG()
TCutG destructor.
Definition: TCutG.cxx:244
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TCutG.cxx:357
TString fVarY
Y variable.
Definition: TCutG.h:24
TCutG & operator=(const TCutG &)
Assignment operator.
Definition: TCutG.cxx:255
const char * GetVarX() const
Definition: TCutG.h:41
Graphical cut class.
Definition: TCutG.h:20
Double_t x[n]
Definition: legend1.C:17
void Class()
Definition: Class.C:29
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition: TGraph.cxx:185
virtual void SetName(const char *name="")
Set graph name.
Definition: TGraph.cxx:2300
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:270
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition: TAxis.cxx:475
virtual TText * GetLine(Int_t number) const
Get Pointer to line number in this pavetext.
Definition: TPaveText.cxx:274
virtual void SetVarY(const char *vary)
Set Y variable.
Definition: TCutG.cxx:413
Base class for several text objects.
Definition: TText.h:23
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:339
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:312
float ymax
Definition: THbookFile.cxx:93
const char * GetVarY() const
Definition: TCutG.h:42
virtual TText * GetLineWith(const char *text) const
Get Pointer to first containing string text in this pavetext.
Definition: TPaveText.cxx:289
Service class for 2-Dim histogram classes.
Definition: TH2.h:30
Class to manage histogram axis.
Definition: TAxis.h:30
if object ctor succeeded but object should not be used
Definition: TObject.h:68
auto * a
Definition: textangle.C:12
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:273
virtual void SetObjectY(TObject *obj)
Set the Y object (and delete the previous one if any).
Definition: TCutG.cxx:394
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:234
virtual void SetVarX(const char *varx)
Set X variable.
Definition: TCutG.cxx:403
Int_t GetN() const
Definition: TGraph.h:123
float xmax
Definition: THbookFile.cxx:93
#define h(i)
Definition: RSha256.hxx:106
const Bool_t kFALSE
Definition: RtypesCore.h:90
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:290
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:21
#define ClassImp(name)
Definition: Rtypes.h:361
Double_t y[n]
Definition: legend1.C:17
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
TString fVarX
X variable.
Definition: TCutG.h:23
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:144
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
Definition: TAxis.cxx:537
Double_t * fY
[fNpoints] array of Y points
Definition: TGraph.h:48
TObject * fObjectY
! pointer to an object corresponding to Y
Definition: TCutG.h:26
A TGraph is an object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
#define gPad
Definition: TVirtualPad.h:287
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:1886
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:290
const Bool_t kTRUE
Definition: RtypesCore.h:89
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48