Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TTreeDrawArgsParser.cxx
Go to the documentation of this file.
1// @(#)root/treeplayer:$Id$
2// Author: Marek Biskup 24/01/2005
3
4/*************************************************************************
5 * Copyright (C) 1995-2005, 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 TTreeDrawArgsParser
13A class that parses all parameters for TTree::Draw().
14See TTree::Draw() for the format description.
15*/
16
17#include "TTreeDrawArgsParser.h"
18#include "TDirectory.h"
19
20
23
24
25
26////////////////////////////////////////////////////////////////////////////////
27/// Constructor - cleans all the class variables.
28
33
34////////////////////////////////////////////////////////////////////////////////
35/// Destructor.
36
40
41////////////////////////////////////////////////////////////////////////////////
42/// return fgMaxDimension (cannot be inline)
43
48
49////////////////////////////////////////////////////////////////////////////////
50/// Resets all the variables of the class.
51
53{
54 fExp = "";
55 fSelection = "";
56 fOption = "";
57 fDimension = -1;
58 int i;
59 for (i = 0; i < fgMaxDimension; i++) {
60 fVarExp[i] = "";
61 }
62 fAdd = false;
63 fName = "";
64 fNoParameters = 0;
65 for (i = 0; i < fgMaxParameters; i++) {
66 fParameterGiven[i] = false;
67 fParameters[i] = 0;
68 }
69 fShouldDraw = true;
70 fOriginal = nullptr;
71 fDrawProfile = false;
72 fOptionSame = false;
73 fEntryList = false;
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Parse expression [var1 [:var2 [:var3] ...]],
79/// number of variables cannot be greater than fgMaxDimension.
80///
81/// A colon which is followed by (or that follows) another semicolon
82/// is not regarded as a separator.
83///
84/// If there are more separating : than fgMaxDimension - 1 then
85/// all characters after (fgMaxDimension - 1)th colon is put into
86/// the last variable.
87///
88/// - `fDimension := <number of variables>`
89/// - `fVarExp[0] := <first variable string>`
90/// - `fVarExp[1] := <second variable string>`
91/// ..
92/// Returns false in case of an error.
93
95{
96 fDimension = 0;
97 if (variables.Length() == 0)
98 return true;
99
100 int prev = 0;
101 int i;
102 for (i = 0; i < variables.Length() && fDimension < fgMaxDimension; i++) {
103 if (variables[i] == ':'
104 && !( (i > 0 && variables[i - 1] == ':')
105 || (i + 1 < variables.Length() && variables[i + 1] == ':') ) ) {
106 fVarExp[fDimension] = variables(prev, i - prev);
107 prev = i+1;
108 fDimension++;
109 }
110 }
111 if (fDimension < fgMaxDimension && i != prev)
112 fVarExp[fDimension++] = variables(prev, i - prev);
113 else
114 return false;
115
116 return true;
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Syntax:
121///
122/// [' '*][[\+][' '*]name[(num1 [, [num2] ] [, [num3] ] ...)]]
123///
124/// num's are floating point numbers
125/// sets the fileds fNoParameters, fParameterGiven, fParameters, fAdd, fName
126/// to appropriate values.
127///
128/// Returns false in case of an error.
129
131{
132 name.ReplaceAll(" ", "");
133
134 if (name.Length() != 0 && name[0] == '+') {
135 fAdd = true;
136 name = name (1, name.Length() - 1);
137 }
138 else
139 fAdd = false;
140 bool result = true;
141
142 fNoParameters = 0;
143 for (int i = 0; i < fgMaxParameters; i++)
144 fParameterGiven[i] = false;
145
146 if (char *p = (char*)strstr(name.Data(), "(")) {
147 fName = name(0, p - name.Data());
148 p++;
149 char* end = p + strlen(p);
150
151 for (int i = 0; i < fgMaxParameters; i++) {
152 char* q = p;
153 while (p < end && *p != ',' && *p != ')')
154 p++;
155 TString s(q, p - q);
156 if (sscanf(s.Data(), "%lf", &fParameters[i]) == 1) {
157 fParameterGiven[i] = true;
159 }
160 if (p == end) {
161 Error("ParseName", "expected \')\'");
162 result = false;
163 break;
164 }
165 else if (*p == ')')
166 break;
167 else if (*p == ',')
168 p++;
169 else {
170 Error("ParseName", "impossible value for *q!");
171 result = false;
172 break;
173 }
174 }
175 }
176 else { // if (char *p = strstr(name.Data(), "("))
177 fName = name;
178 }
179 return result;
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Split variables and parse name and parameters in brackets.
184
186{
187 char* gg = (char*)strstr(fExp.Data(), ">>");
188 TString variables;
190
191 if (gg) {
192 variables = fExp(0, gg - fExp.Data());
193 name = fExp(gg+2 - fExp.Data(), fExp.Length() - (gg + 2 - fExp.Data()));
194 }
195 else {
196 variables = fExp;
197 name = "";
198 }
199 bool result = SplitVariables(variables) && ParseName(name);
200 if (!result) {
201 Error("ParseVarExp", "error parsing variable expression");
202 return false;
203 }
204 return result;
205}
206
207////////////////////////////////////////////////////////////////////////////////
208/// Check if options contain some data important for choosing the type of the
209/// drawn object.
210
212{
214
215 if (fOption.Contains("goff")) {
216 fShouldDraw = false;
217 }
218 if (fOption.Contains("prof")) {
219 fDrawProfile = true;
220 }
221 if (fOption.Contains("same")) {
222 fOptionSame = true;
223 }
224 if (fOption.Contains("entrylist")){
225 fEntryList = true;
226 }
227 return true;
228}
229
230////////////////////////////////////////////////////////////////////////////////
231/// Parses parameters from TTree::Draw().
232/// - varexp - Variable expression; see TTree::Draw()
233/// - selection - selection expression; see TTree::Draw()
234/// - option - Drawing option; see TTree::Draw
235
237{
239
240 // read the data provided and fill class fields
242 fExp = varexp;
243 fOption = option;
244 bool success = ParseVarExp();
245 success &= ParseOption();
246
247 if (!success)
248 return false;
249
250 // if the name was specified find the existing histogram
251 if (fName != "") {
252 fOriginal = gDirectory->Get(fName);
253 }
254 else
255 fOriginal = nullptr;
256
257 DefineType();
258
259 return true;
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// Put the type of the draw result into fOutputType and return it.
264
266{
267 if (fDimension == 0){
268 if (fEntryList)
269 return fOutputType = kENTRYLIST;
270 else
271 return fOutputType = kEVENTLIST;
272 }
273 if (fDimension == 2 && fDrawProfile)
274 return fOutputType = kPROFILE;
275 if (fDimension == 3 && fDrawProfile)
276 return fOutputType = kPROFILE2D;
277
278 if (fDimension == 2) {
279 bool graph = false;
280// GG 9Mar2014: fixing ROOT-5337; should understand why it was like this, but we move to TSelectorDraw
281// and this will disappear
282// Int_t l = fOption.Length();
283// if (l == 0 || fOption.Contains("same")) graph = true;
284 if (fOption.Contains("same")) graph = true;
285 if (fOption.Contains("p") || fOption.Contains("*") || fOption.Contains("l")) graph = true;
286 if (fOption.Contains("surf") || fOption.Contains("lego") || fOption.Contains("cont")) graph = false;
287 if (fOption.Contains("col") || fOption.Contains("hist") || fOption.Contains("scat")) graph = false;
288 if (fOption.Contains("box")) graph = false;
289 if (graph)
290 return fOutputType = kGRAPH;
291 else
292 return fOutputType = kHISTOGRAM2D;
293 }
294 if (fDimension == 3) {
295 if (fOption.Contains("col"))
296 return fOutputType = kLISTOFGRAPHS;
297 else
298 return fOutputType = kHISTOGRAM3D;
299// GG 9Mar2014: fixing ROOT-5337; should understand why it was like this, but we move to TSelectorDraw
300// and this will disappear
301// return fOutputType = kPOLYMARKER3D;
302 }
303 if (fDimension == 1)
304 return fOutputType = kHISTOGRAM1D;
305 if (fDimension == 4)
307 return kUNKNOWN;
308}
309
310////////////////////////////////////////////////////////////////////////////////
311/// returns *num*-th parameter from brackets in the expression
312/// in case of an error (wrong number) returns 0.0
313/// num - number of parameter (counted from 0)
314
316{
317 if (num >= 0 && num <= fgMaxParameters && fParameterGiven[num])
318 return fParameters[num];
319 else {
320 Error("GetParameter","wrong arguments");
321 return 0.0;
322 }
323}
324
325////////////////////////////////////////////////////////////////////////////////
326/// - num - parameter number
327/// - def - default value of the parameter
328/// returns the value of *num*-th parameter from the brackets in the variable expression
329/// if the parameter of that number wasn't specified returns *def*.
330
332{
333 if (num >= 0 && num <= fgMaxParameters && fParameterGiven[num])
334 return fParameters[num];
335 else
336 return def;
337}
338
339////////////////////////////////////////////////////////////////////////////////
340/// returns true if the *num*-th parameter was specified
341/// otherwise returns fFALSE
342/// in case of an error (wrong num) prints an error message and
343/// returns false.
344
346{
347 if (num >= 0 && num <= fgMaxParameters)
348 return fParameterGiven[num];
349 else
350 Error("Specified", "wrong parameter %d; fgMaxParameters: %d", num, fgMaxParameters);
351 return false;
352}
353
354////////////////////////////////////////////////////////////////////////////////
355/// Returns the *num*-th variable string
356/// in case of an error prints an error message and returns an empty string.
357
359{
360 if (num >= 0 && num < fDimension)
361 return fVarExp[num];
362 else
363 Error("GetVarExp", "wrong Parameters %d; fDimension = %d", num, fDimension);
364 return "";
365}
366
367////////////////////////////////////////////////////////////////////////////////
368/// Returns the variable string, i.e. [var1[:var2[:var2[:var4]]]].
369
371{
372 if (fDimension <= 0)
373 return "";
374 TString exp = fVarExp[0];
375 for (int i = 1; i < fDimension; i++) {
376 exp += ":";
377 exp += fVarExp[i];
378 }
379 return exp;
380}
381
382
383////////////////////////////////////////////////////////////////////////////////
384/// Returns the desired plot title.
385
387{
388 if (fSelection != "")
389 return Form("%s {%s}", GetVarExp().Data(), fSelection.Data());
390 else
391 return GetVarExp();
392}
393
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
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.
#define gDirectory
Definition TDirectory.h:385
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition TGX11.cxx:110
float * q
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2495
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189
const char * Data() const
Definition TString.h:384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
TString GetObjectTitle() const
Returns the desired plot title.
Double_t GetParameter(int num) const
returns num-th parameter from brackets in the expression in case of an error (wrong number) returns 0...
bool SplitVariables(TString variables)
Parse expression [var1 [:var2 [:var3] ...]], number of variables cannot be greater than fgMaxDimensio...
static Int_t GetMaxDimension()
return fgMaxDimension (cannot be inline)
Double_t GetIfSpecified(Int_t num, Double_t def) const
bool IsSpecified(int num) const
returns true if the num-th parameter was specified otherwise returns fFALSE in case of an error (wron...
TString fOption
Draw options.
bool fShouldDraw
If to draw the plot.
bool ParseName(TString name)
Syntax:
EOutputType fOutputType
Type of the output.
~TTreeDrawArgsParser() override
Destructor.
Int_t fNoParameters
If dimensions of the plot was specified.
bool fOptionSame
If option contained "same".
bool fDrawProfile
True if the options contain :"prof".
Int_t fDimension
Dimension of the histogram/plot.
TTreeDrawArgsParser::EOutputType DefineType()
Put the type of the draw result into fOutputType and return it.
TObject * fOriginal
Original plot (if it is to be reused)
TString fSelection
Selection expression.
static Int_t fgMaxDimension
= 4
bool fAdd
Values should be added to an existing object.
TString fVarExp[4]
Variable expression 0 - X, 1 - Y, 2 - Z, 3 - W If dimension < fgMaxDimension then some Expressions ar...
TTreeDrawArgsParser()
Constructor - cleans all the class variables.
bool fEntryList
If fill a TEntryList.
Double_t fParameters[9]
Parameters in brackets.
bool ParseVarExp()
Split variables and parse name and parameters in brackets.
void ClearPrevious()
Resets all the variables of the class.
static Int_t fgMaxParameters
= 9
TString fExp
Complete variable expression.
TString fName
Histogram's/plot's name.
bool fParameterGiven[9]
True if the parameter was given, otherwise false.
bool ParseOption()
Check if options contain some data important for choosing the type of the drawn object.
bool Parse(const char *varexp, const char *selection, Option_t *option)
Parses parameters from TTree::Draw().
TString GetVarExp() const
Returns the variable string, i.e. [var1[:var2[:var2[:var4]]]].