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
94
95////////////////////////////////////////////////////////////////////////////////
96/// TCutG default constructor.
97
99{
100 fObjectX = nullptr;
101 fObjectY = nullptr;
102}
103
104////////////////////////////////////////////////////////////////////////////////
105/// TCutG copy constructor
106
108 :TGraph(cutg)
109{
110 fVarX = cutg.fVarX;
111 fVarY = cutg.fVarY;
112 fObjectX = cutg.fObjectX ? cutg.fObjectX->Clone() : nullptr;
113 fObjectY = cutg.fObjectY ? cutg.fObjectY->Clone() : nullptr;
114}
115
116////////////////////////////////////////////////////////////////////////////////
117/// TCutG normal constructor.
118
120 :TGraph(n)
121{
122 fObjectX = nullptr;
123 fObjectY = nullptr;
124 SetName(name);
125 auto obj = gROOT->GetListOfSpecials()->FindObject(name);
126 if (obj) {
127 Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
128 obj->IsA()->GetName(),obj->GetName());
129 delete obj;
130 }
131 gROOT->GetListOfSpecials()->Add(this);
132
133 // Take name of cut variables from pad title if title contains ":"
134 if (gPad) {
135 TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
136 if (!ptitle) return;
137 TText *ttitle = ptitle->GetLineWith(":");
138 if (!ttitle) ttitle = ptitle->GetLineWith("{");
139 if (!ttitle) ttitle = ptitle->GetLine(0);
140 if (!ttitle) return;
141 const char *title = ttitle->GetTitle();
142 Int_t nch = strlen(title);
143 char *vars = new char[nch+1];
144 strlcpy(vars,title,nch+1);
145 char *col = strstr(vars,":");
146 if (col) {
147 *col = 0;
148 col++;
149 char *brak = strstr(col," {");
150 if (brak) *brak = 0;
151 fVarY = vars;
152 fVarX = col;
153 } else {
154 char *brak = strstr(vars," {");
155 if (brak) *brak = 0;
156 fVarX = vars;
157 }
158 delete [] vars;
159 }
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// TCutG normal constructor.
164
165TCutG::TCutG(const char *name, Int_t n, const Float_t *x, const Float_t *y)
166 :TGraph(n,x,y)
167{
168 fObjectX = nullptr;
169 fObjectY = nullptr;
170 SetName(name);
171 auto obj = gROOT->GetListOfSpecials()->FindObject(name);
172 if (obj) {
173 Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
174 obj->IsA()->GetName(),obj->GetName());
175 delete obj;
176 }
177 gROOT->GetListOfSpecials()->Add(this);
178
179 // Take name of cut variables from pad title if title contains ":"
180 if (gPad) {
181 TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
182 if (!ptitle) return;
183 TText *ttitle = ptitle->GetLineWith(":");
184 if (!ttitle) ttitle = ptitle->GetLineWith("{");
185 if (!ttitle) ttitle = ptitle->GetLine(0);
186 if (!ttitle) return;
187 const char *title = ttitle->GetTitle();
188 Int_t nch = strlen(title);
189 char *vars = new char[nch+1];
190 strlcpy(vars,title,nch+1);
191 char *col = strstr(vars,":");
192 if (col) {
193 *col = 0;
194 col++;
195 char *brak = strstr(col," {");
196 if (brak) *brak = 0;
197 fVarY = vars;
198 fVarX = col;
199 } else {
200 char *brak = strstr(vars," {");
201 if (brak) *brak = 0;
202 fVarX = vars;
203 }
204 delete [] vars;
205 }
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// TCutG normal constructor.
210
211TCutG::TCutG(const char *name, Int_t n, const Double_t *x, const Double_t *y)
212 :TGraph(n,x,y)
213{
214 fObjectX = nullptr;
215 fObjectY = nullptr;
216 SetName(name);
217 auto obj = gROOT->GetListOfSpecials()->FindObject(name);
218 if (obj) {
219 Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
220 obj->IsA()->GetName(),obj->GetName());
221 delete obj;
222 }
223 gROOT->GetListOfSpecials()->Add(this);
224
225 // Take name of cut variables from pad title if title contains ":"
226 if (gPad) {
227 TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
228 if (!ptitle) return;
229 TText *ttitle = ptitle->GetLineWith(":");
230 if (!ttitle) ttitle = ptitle->GetLineWith("{");
231 if (!ttitle) ttitle = ptitle->GetLine(0);
232 if (!ttitle) return;
233 const char *title = ttitle->GetTitle();
234 Int_t nch = strlen(title);
235 char *vars = new char[nch+1];
236 strlcpy(vars,title,nch+1);
237 char *col = strstr(vars,":");
238 if (col) {
239 *col = 0;
240 col++;
241 char *brak = strstr(col," {");
242 if (brak) *brak = 0;
243 fVarY = vars;
244 fVarX = col;
245 } else {
246 char *brak = strstr(vars," {");
247 if (brak) *brak = 0;
248 fVarX = vars;
249 }
250 delete [] vars;
251 }
252}
253
254////////////////////////////////////////////////////////////////////////////////
255/// TCutG destructor.
256
258{
259 delete fObjectX;
260 delete fObjectY;
261 if ( gROOT && !gROOT->TestBit( TObject::kInvalidObject ) )
262 gROOT->GetListOfSpecials()->Remove(this);
263}
264
265////////////////////////////////////////////////////////////////////////////////
266/// Assignment operator.
267
269{
270 if (this != &rhs) {
272 delete fObjectX;
273 delete fObjectY;
274 fObjectX = rhs.fObjectX ? rhs.fObjectX->Clone() : nullptr;
275 fObjectY = rhs.fObjectY ? rhs.fObjectY->Clone() : nullptr;
276 }
277 return *this;
278}
279
280////////////////////////////////////////////////////////////////////////////////
281/// Compute the area inside 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
287{
288 Double_t a = 0;
289 Int_t n = GetN();
290 for (Int_t i=0;i<n-1;i++) {
291 a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
292 }
293 a *= 0.5;
294 return a;
295}
296
297////////////////////////////////////////////////////////////////////////////////
298/// Compute the center x,y of this TCutG
299/// The algorithm uses Stoke's theorem over the border of the closed polygon.
300/// Just as a reminder: Stoke's theorem reduces a surface integral
301/// to a line integral over the border of the surface integral.
302
304{
305 Int_t n = GetN();
306 Double_t a = 0;
307 cx = cy = 0;
308 Double_t t;
309 for (Int_t i=0;i<n-1;i++) {
310 t = 2*fX[i]*fY[i] + fY[i]*fX[i+1] + fX[i]*fY[i+1] + 2*fX[i+1]*fY[i+1];
311 cx += (fX[i]-fX[i+1])*t;
312 cy += (-fY[i]+fY[i+1])*t;
313 a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
314 }
315 a *= 0.5;
316 cx *= 1./(6*a);
317 cy *= 1./(6*a);
318}
319
320////////////////////////////////////////////////////////////////////////////////
321/// Compute the integral of 2-d histogram h for all bins inside the cut
322/// if option "width" is specified, the integral is the sum of
323/// the bin contents multiplied by the bin width in x and in y.
324
326{
327 if (!h) return 0;
328 Int_t n = GetN();
330 Double_t xmax = -xmin;
333 for (Int_t i=0;i<n;i++) {
334 if (fX[i] < xmin) xmin = fX[i];
335 if (fX[i] > xmax) xmax = fX[i];
336 if (fY[i] < ymin) ymin = fY[i];
337 if (fY[i] > ymax) ymax = fY[i];
338 }
339 TAxis *xaxis = h->GetXaxis();
340 TAxis *yaxis = h->GetYaxis();
341 Int_t binx1 = xaxis->FindBin(xmin);
342 Int_t binx2 = xaxis->FindBin(xmax);
343 Int_t biny1 = yaxis->FindBin(ymin);
344 Int_t biny2 = yaxis->FindBin(ymax);
345 Int_t nbinsx = h->GetNbinsX();
346 Stat_t integral = 0;
347
348 // Loop on bins for which the bin center is in the cut
349 TString opt = option;
350 opt.ToLower();
352 if (opt.Contains("width")) width = kTRUE;
353 Int_t bin, binx, biny;
354 for (biny=biny1;biny<=biny2;biny++) {
355 Double_t y = yaxis->GetBinCenter(biny);
356 for (binx=binx1;binx<=binx2;binx++) {
357 Double_t x = xaxis->GetBinCenter(binx);
358 if (!IsInside(x,y)) continue;
359 bin = binx +(nbinsx+2)*biny;
360 if (width) integral += h->GetBinContent(bin)*xaxis->GetBinWidth(binx)*yaxis->GetBinWidth(biny);
361 else integral += h->GetBinContent(bin);
362 }
363 }
364 return integral;
365}
366
367////////////////////////////////////////////////////////////////////////////////
368/// Save primitive as a C++ statement(s) on output stream out.
369
370void TCutG::SavePrimitive(std::ostream &out, Option_t *option)
371{
373 TString arry = SavePrimitiveVector(out, "cutg", fNpoints, fY);
375 out, Class(), "cutg",
376 TString::Format("\"%s\", %d, %s.data(), %s.data()", GetName(), fNpoints, arrx.Data(), arry.Data()), kFALSE);
377 out << " cutg->SetVarX(\"" << GetVarX() << "\");\n";
378 out << " cutg->SetVarY(\"" << GetVarY() << "\");\n";
379 out << " cutg->SetTitle(\"" << TString(GetTitle()).ReplaceSpecialCppChars() << "\");\n";
380
381 SaveFillAttributes(out, "cutg", 0, 1001);
382 SaveLineAttributes(out, "cutg", 1, 1, 1);
383 SaveMarkerAttributes(out, "cutg", 1, 1, 1);
384
385 SavePrimitiveDraw(out, "cutg", option);
386}
387
388////////////////////////////////////////////////////////////////////////////////
389/// Set the X object (and delete the previous one if any).
390
392{
393 delete fObjectX;
394 fObjectX = obj;
395}
396
397////////////////////////////////////////////////////////////////////////////////
398/// Set the Y object (and delete the previous one if any).
399
401{
402 delete fObjectY;
403 fObjectY = obj;
404}
405
406////////////////////////////////////////////////////////////////////////////////
407/// Set X variable.
408
409void TCutG::SetVarX(const char *varx)
410{
411 fVarX = varx;
412 delete fObjectX;
413 fObjectX = nullptr;
414}
415
416////////////////////////////////////////////////////////////////////////////////
417/// Set Y variable.
418
419void TCutG::SetVarY(const char *vary)
420{
421 fVarY = vary;
422 delete fObjectY;
423 fObjectY = nullptr;
424}
425
426////////////////////////////////////////////////////////////////////////////////
427/// Stream an object of class TCutG.
428
430{
431 if (R__b.IsReading()) {
432 R__b.ReadClassBuffer(TCutG::Class(), this);
433 gROOT->GetListOfSpecials()->Add(this);
434 } else {
435 R__b.WriteClassBuffer(TCutG::Class(), this);
436 }
437}
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
Option_t Option_t width
char name[80]
Definition TGX11.cxx:110
float xmin
float ymin
float xmax
float ymax
#define gROOT
Definition TROOT.h:411
#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:238
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:274
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:32
Buffer base class used for serializing objects.
Definition TBuffer.h:43
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:325
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:303
~TCutG() override
TCutG destructor.
Definition TCutG.cxx:257
TCutG()
TCutG default constructor.
Definition TCutG.cxx:98
void Streamer(TBuffer &) override
Stream an object of class TCutG.
Definition TCutG.cxx:429
const char * GetVarX() const
Definition TCutG.h:41
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TCutG.cxx:370
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:419
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:286
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:400
virtual void SetVarX(const char *varx)
Set X variable.
Definition TCutG.cxx:409
TCutG & operator=(const TCutG &)
Assignment operator.
Definition TCutG.cxx:268
virtual void SetObjectX(TObject *obj)
Set the X object (and delete the previous one if any).
Definition TCutG.cxx:391
static TClass * Class()
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:1841
Int_t GetN() const
Definition TGraph.h:131
Double_t * fY
[fNpoints] array of Y points
Definition TGraph.h:48
void SetName(const char *name="") override
Set graph name.
Definition TGraph.cxx:2329
Double_t * fX
[fNpoints] array of X points
Definition TGraph.h:47
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition TGraph.cxx:233
Service class for 2-D histogram classes.
Definition TH2.h:30
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
Mother of all ROOT objects.
Definition TObject.h:41
static TString SavePrimitiveVector(std::ostream &out, const char *prefix, Int_t len, Double_t *arr, Bool_t empty_line=kFALSE)
Save array in the output stream "out" as vector.
Definition TObject.cxx:788
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:822
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:771
@ kInvalidObject
if object ctor succeeded but object should not be used
Definition TObject.h:78
A Pave (see TPave) with text, lines or/and boxes inside.
Definition TPaveText.h:21
Basic string class.
Definition TString.h:138
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1121
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
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