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