Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 = nullptr;
102 fObjectY = nullptr;
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// TCutG copy constructor
107
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 = nullptr;
124 fObjectY = nullptr;
125 SetName(name);
126 auto obj = gROOT->GetListOfSpecials()->FindObject(name);
127 if (obj) {
128 Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
129 obj->IsA()->GetName(),obj->GetName());
130 delete obj;
131 }
132 gROOT->GetListOfSpecials()->Add(this);
133
134 // Take name of cut variables from pad title if title contains ":"
135 if (gPad) {
136 TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
137 if (!ptitle) return;
138 TText *ttitle = ptitle->GetLineWith(":");
139 if (!ttitle) ttitle = ptitle->GetLineWith("{");
140 if (!ttitle) ttitle = ptitle->GetLine(0);
141 if (!ttitle) return;
142 const char *title = ttitle->GetTitle();
143 Int_t nch = strlen(title);
144 char *vars = new char[nch+1];
145 strlcpy(vars,title,nch+1);
146 char *col = strstr(vars,":");
147 if (col) {
148 *col = 0;
149 col++;
150 char *brak = strstr(col," {");
151 if (brak) *brak = 0;
152 fVarY = vars;
153 fVarX = col;
154 } else {
155 char *brak = strstr(vars," {");
156 if (brak) *brak = 0;
157 fVarX = vars;
158 }
159 delete [] vars;
160 }
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// TCutG normal constructor.
165
166TCutG::TCutG(const char *name, Int_t n, const Float_t *x, const Float_t *y)
167 :TGraph(n,x,y)
168{
169 fObjectX = nullptr;
170 fObjectY = nullptr;
171 SetName(name);
172 auto obj = gROOT->GetListOfSpecials()->FindObject(name);
173 if (obj) {
174 Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
175 obj->IsA()->GetName(),obj->GetName());
176 delete obj;
177 }
178 gROOT->GetListOfSpecials()->Add(this);
179
180 // Take name of cut variables from pad title if title contains ":"
181 if (gPad) {
182 TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
183 if (!ptitle) return;
184 TText *ttitle = ptitle->GetLineWith(":");
185 if (!ttitle) ttitle = ptitle->GetLineWith("{");
186 if (!ttitle) ttitle = ptitle->GetLine(0);
187 if (!ttitle) return;
188 const char *title = ttitle->GetTitle();
189 Int_t nch = strlen(title);
190 char *vars = new char[nch+1];
191 strlcpy(vars,title,nch+1);
192 char *col = strstr(vars,":");
193 if (col) {
194 *col = 0;
195 col++;
196 char *brak = strstr(col," {");
197 if (brak) *brak = 0;
198 fVarY = vars;
199 fVarX = col;
200 } else {
201 char *brak = strstr(vars," {");
202 if (brak) *brak = 0;
203 fVarX = vars;
204 }
205 delete [] vars;
206 }
207}
208
209////////////////////////////////////////////////////////////////////////////////
210/// TCutG normal constructor.
211
212TCutG::TCutG(const char *name, Int_t n, const Double_t *x, const Double_t *y)
213 :TGraph(n,x,y)
214{
215 fObjectX = nullptr;
216 fObjectY = nullptr;
217 SetName(name);
218 auto obj = gROOT->GetListOfSpecials()->FindObject(name);
219 if (obj) {
220 Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
221 obj->IsA()->GetName(),obj->GetName());
222 delete obj;
223 }
224 gROOT->GetListOfSpecials()->Add(this);
225
226 // Take name of cut variables from pad title if title contains ":"
227 if (gPad) {
228 TPaveText *ptitle = (TPaveText*)gPad->FindObject("title");
229 if (!ptitle) return;
230 TText *ttitle = ptitle->GetLineWith(":");
231 if (!ttitle) ttitle = ptitle->GetLineWith("{");
232 if (!ttitle) ttitle = ptitle->GetLine(0);
233 if (!ttitle) return;
234 const char *title = ttitle->GetTitle();
235 Int_t nch = strlen(title);
236 char *vars = new char[nch+1];
237 strlcpy(vars,title,nch+1);
238 char *col = strstr(vars,":");
239 if (col) {
240 *col = 0;
241 col++;
242 char *brak = strstr(col," {");
243 if (brak) *brak = 0;
244 fVarY = vars;
245 fVarX = col;
246 } else {
247 char *brak = strstr(vars," {");
248 if (brak) *brak = 0;
249 fVarX = vars;
250 }
251 delete [] vars;
252 }
253}
254
255////////////////////////////////////////////////////////////////////////////////
256/// TCutG destructor.
257
259{
260 delete fObjectX;
261 delete fObjectY;
262 if ( gROOT && !gROOT->TestBit( TObject::kInvalidObject ) )
263 gROOT->GetListOfSpecials()->Remove(this);
264}
265
266////////////////////////////////////////////////////////////////////////////////
267/// Assignment operator.
268
270{
271 if (this != &rhs) {
273 delete fObjectX;
274 delete fObjectY;
275 fObjectX = rhs.fObjectX ? rhs.fObjectX->Clone() : nullptr;
276 fObjectY = rhs.fObjectY ? rhs.fObjectY->Clone() : nullptr;
277 }
278 return *this;
279}
280
281////////////////////////////////////////////////////////////////////////////////
282/// Compute the area inside 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
288{
289 Double_t a = 0;
290 Int_t n = GetN();
291 for (Int_t i=0;i<n-1;i++) {
292 a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
293 }
294 a *= 0.5;
295 return a;
296}
297
298////////////////////////////////////////////////////////////////////////////////
299/// Compute the center x,y of this TCutG
300/// The algorithm uses Stoke's theorem over the border of the closed polygon.
301/// Just as a reminder: Stoke's theorem reduces a surface integral
302/// to a line integral over the border of the surface integral.
303
305{
306 Int_t n = GetN();
307 Double_t a = 0;
308 cx = cy = 0;
309 Double_t t;
310 for (Int_t i=0;i<n-1;i++) {
311 t = 2*fX[i]*fY[i] + fY[i]*fX[i+1] + fX[i]*fY[i+1] + 2*fX[i+1]*fY[i+1];
312 cx += (fX[i]-fX[i+1])*t;
313 cy += (-fY[i]+fY[i+1])*t;
314 a += (fX[i]-fX[i+1])*(fY[i]+fY[i+1]);
315 }
316 a *= 0.5;
317 cx *= 1./(6*a);
318 cy *= 1./(6*a);
319}
320
321////////////////////////////////////////////////////////////////////////////////
322/// Compute the integral of 2-d histogram h for all bins inside the cut
323/// if option "width" is specified, the integral is the sum of
324/// the bin contents multiplied by the bin width in x and in y.
325
327{
328 if (!h) return 0;
329 Int_t n = GetN();
331 Double_t xmax = -xmin;
334 for (Int_t i=0;i<n;i++) {
335 if (fX[i] < xmin) xmin = fX[i];
336 if (fX[i] > xmax) xmax = fX[i];
337 if (fY[i] < ymin) ymin = fY[i];
338 if (fY[i] > ymax) ymax = fY[i];
339 }
340 TAxis *xaxis = h->GetXaxis();
341 TAxis *yaxis = h->GetYaxis();
342 Int_t binx1 = xaxis->FindBin(xmin);
343 Int_t binx2 = xaxis->FindBin(xmax);
344 Int_t biny1 = yaxis->FindBin(ymin);
345 Int_t biny2 = yaxis->FindBin(ymax);
346 Int_t nbinsx = h->GetNbinsX();
347 Stat_t integral = 0;
348
349 // Loop on bins for which the bin center is in the cut
350 TString opt = option;
351 opt.ToLower();
353 if (opt.Contains("width")) width = kTRUE;
354 Int_t bin, binx, biny;
355 for (biny=biny1;biny<=biny2;biny++) {
356 Double_t y = yaxis->GetBinCenter(biny);
357 for (binx=binx1;binx<=binx2;binx++) {
358 Double_t x = xaxis->GetBinCenter(binx);
359 if (!IsInside(x,y)) continue;
360 bin = binx +(nbinsx+2)*biny;
361 if (width) integral += h->GetBinContent(bin)*xaxis->GetBinWidth(binx)*yaxis->GetBinWidth(biny);
362 else integral += h->GetBinContent(bin);
363 }
364 }
365 return integral;
366}
367
368////////////////////////////////////////////////////////////////////////////////
369/// Save primitive as a C++ statement(s) on output stream out.
370
371void TCutG::SavePrimitive(std::ostream &out, Option_t *option)
372{
374 TString arry = SavePrimitiveVector(out, "cutg", fNpoints, fY);
376 out, Class(), "cutg",
377 TString::Format("\"%s\", %d, %s.data(), %s.data()", GetName(), fNpoints, arrx.Data(), arry.Data()), kFALSE);
378 out << " cutg->SetVarX(\"" << GetVarX() << "\");\n";
379 out << " cutg->SetVarY(\"" << GetVarY() << "\");\n";
380 out << " cutg->SetTitle(\"" << TString(GetTitle()).ReplaceSpecialCppChars() << "\");\n";
381
382 SaveFillAttributes(out, "cutg", 0, 1001);
383 SaveLineAttributes(out, "cutg", 1, 1, 1);
384 SaveMarkerAttributes(out, "cutg", 1, 1, 1);
385
386 SavePrimitiveDraw(out, "cutg", option);
387}
388
389////////////////////////////////////////////////////////////////////////////////
390/// Set the X object (and delete the previous one if any).
391
393{
394 delete fObjectX;
395 fObjectX = obj;
396}
397
398////////////////////////////////////////////////////////////////////////////////
399/// Set the Y object (and delete the previous one if any).
400
402{
403 delete fObjectY;
404 fObjectY = obj;
405}
406
407////////////////////////////////////////////////////////////////////////////////
408/// Set X variable.
409
410void TCutG::SetVarX(const char *varx)
411{
412 fVarX = varx;
413 delete fObjectX;
414 fObjectX = nullptr;
415}
416
417////////////////////////////////////////////////////////////////////////////////
418/// Set Y variable.
419
420void TCutG::SetVarY(const char *vary)
421{
422 fVarY = vary;
423 delete fObjectY;
424 fObjectY = nullptr;
425}
426
427////////////////////////////////////////////////////////////////////////////////
428/// Stream an object of class TCutG.
429
431{
432 if (R__b.IsReading()) {
433 R__b.ReadClassBuffer(TCutG::Class(), this);
434 gROOT->GetListOfSpecials()->Add(this);
435 } else {
436 R__b.WriteClassBuffer(TCutG::Class(), this);
437 }
438}
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:374
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:406
#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:239
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:275
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:326
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:304
~TCutG() override
TCutG destructor.
Definition TCutG.cxx:258
TCutG()
TCutG default constructor.
Definition TCutG.cxx:99
void Streamer(TBuffer &) override
Stream an object of class TCutG.
Definition TCutG.cxx:430
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:371
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:420
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:287
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:401
virtual void SetVarX(const char *varx)
Set X variable.
Definition TCutG.cxx:410
TCutG & operator=(const TCutG &)
Assignment operator.
Definition TCutG.cxx:269
virtual void SetObjectX(TObject *obj)
Set the X object (and delete the previous one if any).
Definition TCutG.cxx:392
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:1842
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:2330
Double_t * fX
[fNpoints] array of X points
Definition TGraph.h:47
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition TGraph.cxx:234
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:139
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1114
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:2378
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
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