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