ROOT  6.06/09
Reference Guide
TCut.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 14/04/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 TCut
13 A specialized string object used for TTree selections.
14 A TCut object has a name and a title. It does not add any data
15 members compared to a TNamed. It only add a set of operators to
16 facilitate logical string concatenation. For example, assume
17 ~~~ {.cpp}
18  cut1 = "x<1" and cut2 = "y>2"
19 ~~~
20 then
21 ~~~ {.cpp}
22  cut1 && cut2 will be the string "(x<1)&&(y>2)"
23 ~~~
24 Operators =, +=, +, *, !, &&, || overloaded.
25 
26 Examples of use:
27 ~~~ {.cpp}
28  Root > TCut c1 = "x<1"
29  Root > TCut c2 = "y<0"
30  Root > TCut c3 = c1&&c2
31  Root > ntuple.Draw("x", c1)
32  Root > ntuple.Draw("x", c1||"x>0")
33  Root > ntuple.Draw("x", c1&&c2)
34  Root > ntuple.Draw("x", "(x+y)"*(c1&&c2))
35 ~~~
36 */
37 
38 #include "TCut.h"
39 
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// Constructor.
44 
45 TCut::TCut() : TNamed()
46 {
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Constructor.
51 
52 TCut::TCut(const char *title) : TNamed("CUT",title)
53 {
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Constructor.
58 
59 TCut::TCut(const char *name, const char *title) : TNamed(name,title)
60 {
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Copy Constructor.
65 
66 TCut::TCut(const TCut &cut) : TNamed(cut)
67 {
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Typical destructor.
72 
74 {
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Comparison.
79 
80 Bool_t TCut::operator==(const char *rhs) const
81 {
82  return fTitle == rhs;
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Comparison.
87 
88 Bool_t TCut::operator==(const TCut &rhs) const
89 {
90  return fTitle == rhs.fTitle;
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Comparison.
95 
96 Bool_t TCut::operator!=(const char *rhs) const
97 {
98  return fTitle != rhs;
99 }
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Comparison.
103 
104 Bool_t TCut::operator!=(const TCut &rhs) const
105 {
106  return fTitle != rhs.fTitle;
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Assignment.
111 
112 TCut& TCut::operator=(const char *rhs)
113 {
114  fTitle = rhs;
115  return *this;
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Assignment.
120 
122 {
123  if (this != &rhs) TNamed::operator=(rhs);
124  return *this;
125 }
126 
127 ////////////////////////////////////////////////////////////////////////////////
128 /// Addition.
129 
130 TCut& TCut::operator+=(const char *rhs)
131 {
132  if (!rhs || !rhs[0]) return *this;
133  if (fTitle.Length() == 0)
134  fTitle = rhs;
135  else
136  fTitle = "(" + fTitle + ")&&(" + TString(rhs) + ")";
137  return *this;
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Addition.
142 
144 {
145  if (rhs.fTitle.Length() == 0) return *this;
146  if (fTitle.Length() == 0)
147  fTitle = rhs;
148  else
149  fTitle = "(" + fTitle + ")&&(" + rhs.fTitle + ")";
150  return *this;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Multiplication.
155 
156 TCut& TCut::operator*=(const char *rhs)
157 {
158 if (!rhs || !rhs[0]) return *this;
159  if (fTitle.Length() == 0)
160  fTitle = rhs;
161  else
162  fTitle = "(" + fTitle + ")*(" + TString(rhs) + ")";
163  return *this;
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Multiplication.
168 
170 {
171  if (rhs.fTitle.Length() == 0) return *this;
172  if (fTitle.Length() == 0)
173  fTitle = rhs;
174  else
175  fTitle = "(" + fTitle + ")*(" + rhs.fTitle + ")";
176  return *this;
177 }
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Addition.
181 
182 TCut operator+(const TCut& lhs, const char *rhs)
183 {
184  return TCut(lhs) += rhs;
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Addition.
189 
190 TCut operator+(const char *lhs, const TCut& rhs)
191 {
192  return TCut(lhs) += rhs;
193 }
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// Addition.
197 
198 TCut operator+(const TCut& lhs, const TCut& rhs)
199 {
200  return TCut(lhs) += rhs;
201 }
202 
203 ////////////////////////////////////////////////////////////////////////////////
204 /// Multiplication.
205 
206 TCut operator*(const TCut& lhs, const char *rhs)
207 {
208  return TCut(lhs) *= rhs;
209 }
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 /// Multiplication.
213 
214 TCut operator*(const char *lhs, const TCut& rhs)
215 {
216  return TCut(lhs) *= rhs;
217 }
218 
219 ////////////////////////////////////////////////////////////////////////////////
220 /// Multiplication.
221 
222 TCut operator*(const TCut& lhs, const TCut& rhs)
223 {
224  return TCut(lhs) *= rhs;
225 }
226 
227 ////////////////////////////////////////////////////////////////////////////////
228 /// Logical and.
229 
230 TCut operator&&(const TCut& lhs, const char *rhs)
231 {
232  return TCut(lhs) += rhs;
233 }
234 
235 ////////////////////////////////////////////////////////////////////////////////
236 /// Logical and.
237 
238 TCut operator&&(const char *lhs, const TCut& rhs)
239 {
240  return TCut(lhs) += rhs;
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// Logical and.
245 
246 TCut operator&&(const TCut& lhs, const TCut& rhs)
247 {
248  return TCut(lhs) += rhs;
249 }
250 
251 ////////////////////////////////////////////////////////////////////////////////
252 /// Logical or.
253 
254 TCut operator||(const TCut& lhs, const char *rhs)
255 {
256  if (lhs.fTitle.Length() == 0 && (!rhs || !rhs[0])) return TCut();
257  if (lhs.fTitle.Length() == 0) return TCut(rhs);
258  if (!rhs || !rhs[0]) return TCut(lhs);
259  TString s = "(" + lhs.fTitle + ")||(" + TString(rhs) + ")";
260  return TCut(s.Data());
261 }
262 
263 ////////////////////////////////////////////////////////////////////////////////
264 /// Logical or.
265 
266 TCut operator||(const char *lhs, const TCut& rhs)
267 {
268  if ((!lhs || !lhs[0]) && rhs.fTitle.Length() == 0) return TCut();
269  if (!lhs || !lhs[0]) return TCut(rhs);
270  if (rhs.fTitle.Length() == 0) return TCut(lhs);
271  TString s = "(" + TString(lhs) + ")||(" + rhs.fTitle + ")";
272  return TCut(s.Data());
273 }
274 
275 ////////////////////////////////////////////////////////////////////////////////
276 /// Logical or.
277 
278 TCut operator||(const TCut& lhs, const TCut& rhs)
279 {
280  if (lhs.fTitle.Length() == 0 && rhs.fTitle.Length() == 0) return TCut();
281  if (lhs.fTitle.Length() == 0) return TCut(rhs);
282  if (rhs.fTitle.Length() == 0) return TCut(lhs);
283  TString s = "(" + lhs.fTitle + ")||(" + rhs.fTitle + ")";
284  return TCut(s.Data());
285 }
286 
287 ////////////////////////////////////////////////////////////////////////////////
288 /// Logical negation.
289 
290 TCut operator!(const TCut &rhs)
291 {
292  if (rhs.fTitle.Length() == 0) return TCut();
293  TString s = "!(" + rhs.fTitle + ")";
294  return TCut(s.Data());
295 }
296 
TCut & operator+=(const char *rhs)
Addition.
Definition: TCut.cxx:130
TString fTitle
Definition: TNamed.h:37
TCut operator&&(const TCut &lhs, const char *rhs)
Logical and.
Definition: TCut.cxx:230
Ssiz_t Length() const
Definition: TString.h:390
Basic string class.
Definition: TString.h:137
bool Bool_t
Definition: RtypesCore.h:59
TCut operator+(const TCut &lhs, const char *rhs)
Addition.
Definition: TCut.cxx:182
TCut operator||(const TCut &lhs, const char *rhs)
Logical or.
Definition: TCut.cxx:254
const char * Data() const
Definition: TString.h:349
virtual ~TCut()
Typical destructor.
Definition: TCut.cxx:73
TCut & operator=(const char *rhs)
Assignment.
Definition: TCut.cxx:112
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
Bool_t operator==(const char *rhs) const
Comparison.
Definition: TCut.cxx:80
TCut & operator*=(const char *rhs)
Multiplication.
Definition: TCut.cxx:156
A specialized string object used for TTree selections.
Definition: TCut.h:27
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:40
Bool_t operator!=(const char *rhs) const
Comparison.
Definition: TCut.cxx:96
TCut operator*(const TCut &lhs, const char *rhs)
Multiplication.
Definition: TCut.cxx:206
TCut operator!(const TCut &rhs)
Logical negation.
Definition: TCut.cxx:290
#define name(a, b)
Definition: linkTestLib0.cpp:5
ClassImp(TCut) TCut
Constructor.
Definition: TCut.cxx:40