TTreeDrawArgsParser.cxx

Go to the documentation of this file.
00001 // @(#)root/treeplayer:$Id: TTreeDrawArgsParser.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Marek Biskup   24/01/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TTreeDrawArgsParser                                                  //
00015 //                                                                      //
00016 // A class that parses all parameters for TTree::Draw().                //
00017 // See TTree::Draw() for the format description.                        //
00018 //                                                                      //
00019 //////////////////////////////////////////////////////////////////////////
00020 
00021 #include "TTreeDrawArgsParser.h"
00022 #include "TDirectory.h"
00023 
00024 
00025 Int_t TTreeDrawArgsParser::fgMaxDimension = 4;
00026 Int_t TTreeDrawArgsParser::fgMaxParameters = 9;
00027 
00028 
00029 ClassImp(TTreeDrawArgsParser)
00030 
00031 //______________________________________________________________________________
00032 TTreeDrawArgsParser::TTreeDrawArgsParser()
00033 {
00034    // Constructor - cleans all the class variables.
00035 
00036    ClearPrevious();
00037 }
00038 
00039 
00040 //______________________________________________________________________________
00041 TTreeDrawArgsParser::~TTreeDrawArgsParser()
00042 {
00043    // Destructor.
00044 }
00045 
00046 //______________________________________________________________________________
00047 Int_t TTreeDrawArgsParser::GetMaxDimension()
00048 {
00049    // return fgMaxDimension (cannot be inline)
00050    return fgMaxDimension;
00051 }
00052 
00053 //______________________________________________________________________________
00054 void TTreeDrawArgsParser::ClearPrevious()
00055 {
00056    // Resets all the variables of the class.
00057 
00058    fExp = "";
00059    fSelection = "";
00060    fOption = "";
00061    fDimension = -1;
00062    int i;
00063    for (i = 0; i < fgMaxDimension; i++) {
00064       fVarExp[i] = "";
00065    }
00066    fAdd = kFALSE;
00067    fName = "";
00068    fNoParameters = 0;
00069    for (i = 0; i < fgMaxParameters; i++) {
00070       fParameterGiven[i] = kFALSE;
00071       fParameters[i] = 0;
00072    }
00073    fShouldDraw = kTRUE;
00074    fOriginal = 0;
00075    fDrawProfile = kFALSE;
00076    fOptionSame = kFALSE;
00077    fEntryList = kFALSE;
00078    fOutputType = kUNKNOWN;
00079 }
00080 
00081 //______________________________________________________________________________
00082 Bool_t TTreeDrawArgsParser::SplitVariables(TString variables)
00083 {
00084    // Parse expression [var1 [:var2 [:var3] ...]],
00085    // number of variables cannot be greater than fgMaxDimension.
00086    // A colon which is followed by (or that follows) another semicolon
00087    // is not regarded as a separator.
00088    // If there are more separating : than fgMaxDimension - 1 then
00089    // all characters after (fgMaxDimension - 1)th colon is put into
00090    // the last variable.
00091    // fDimension := <number of variables>
00092    // fVarExp[0] := <first variable string>
00093    // fVarExp[1] := <second variable string>
00094    // ..
00095    // Returns kFALSE in case of an error.
00096 
00097    fDimension = 0;
00098    if (variables.Length() == 0)
00099       return kTRUE;
00100 
00101    int prev = 0;
00102    int i;
00103    for (i = 0; i < variables.Length() && fDimension < fgMaxDimension; i++) {
00104       if (variables[i] == ':'
00105           && !( (i > 0 && variables[i - 1] == ':')
00106                 || (i + 1 < variables.Length() && variables[i + 1] == ':') ) ) {
00107          fVarExp[fDimension] = variables(prev, i - prev);
00108          prev = i+1;
00109          fDimension++;
00110       }
00111    }
00112    if (fDimension < fgMaxDimension && i != prev)
00113       fVarExp[fDimension++] = variables(prev, i - prev);
00114    else
00115       return kFALSE;
00116 
00117    return kTRUE;
00118 }
00119 
00120 //______________________________________________________________________________
00121 Bool_t TTreeDrawArgsParser::ParseName(TString name)
00122 {
00123    // Syntax:
00124    // [' '*][[\+][' '*]name[(num1 [, [num2] ] [, [num3] ] ...)]]
00125    // num's are floating point numbers
00126    // sets the fileds fNoParameters, fParameterGiven, fParameters, fAdd, fName
00127    // to apropriate values.
00128    // Returns kFALSE in case of an error.
00129 
00130    name.ReplaceAll(" ", "");
00131 
00132    if (name.Length() != 0 && name[0] == '+') {
00133       fAdd = kTRUE;
00134       name = name (1, name.Length() - 1);
00135    }
00136    else
00137       fAdd = kFALSE;
00138    Bool_t result = kTRUE;
00139 
00140    fNoParameters = 0;
00141    for (int i = 0; i < fgMaxParameters; i++)
00142       fParameterGiven[i] = kFALSE;
00143 
00144    if (char *p = (char*)strstr(name.Data(), "(")) {
00145       fName = name(0, p - name.Data());
00146       p++;
00147       char* end = p + strlen(p);
00148 
00149       for (int i = 0; i < fgMaxParameters; i++) {
00150          char* q = p;
00151          while (p < end && *p != ',' && *p != ')')
00152             p++;
00153          TString s(q, p - q);
00154          if (sscanf(s.Data(), "%lf", &fParameters[i]) == 1) {
00155             fParameterGiven[i] = kTRUE;
00156             fNoParameters++;
00157          }
00158          if (p == end) {
00159             Error("ParseName", "expected \')\'");
00160             result = kFALSE;
00161             break;
00162          }
00163          else if (*p == ')')
00164             break;
00165          else if (*p == ',')
00166             p++;
00167          else {
00168             Error("ParseName", "impossible value for *q!");
00169             result = kFALSE;
00170             break;
00171          }
00172       }
00173    }
00174    else { // if (char *p = strstr(name.Data(), "("))
00175       fName = name;
00176    }
00177    return result;
00178 }
00179 
00180 //______________________________________________________________________________
00181 Bool_t TTreeDrawArgsParser::ParseVarExp()
00182 {
00183    // Split variables and parse name and parameters in brackets.
00184 
00185    char* gg = (char*)strstr(fExp.Data(), ">>");
00186    TString variables;
00187    TString name;
00188 
00189    if (gg) {
00190       variables = fExp(0, gg - fExp.Data());
00191       name = fExp(gg+2 - fExp.Data(), fExp.Length() - (gg + 2 - fExp.Data()));
00192    }
00193    else {
00194       variables = fExp;
00195       name = "";
00196    }
00197    Bool_t result = SplitVariables(variables) && ParseName(name);
00198    if (!result) {
00199       Error("ParseVarExp", "error parsing variable expression");
00200       return kFALSE;
00201    }
00202    return result;
00203 }
00204 
00205 //______________________________________________________________________________
00206 Bool_t TTreeDrawArgsParser::ParseOption()
00207 {
00208    // Check if options contain some data important for choosing the type of the
00209    // drawn object.
00210 
00211    fOption.ToLower();
00212 
00213    if (fOption.Contains("goff")) {
00214       fShouldDraw = kFALSE;
00215    }
00216    if (fOption.Contains("prof")) {
00217       fDrawProfile = kTRUE;
00218    }
00219    if (fOption.Contains("same")) {
00220       fOptionSame = kTRUE;
00221    }
00222    if (fOption.Contains("entrylist")){
00223       fEntryList = kTRUE;
00224    }
00225    return true;
00226 }
00227 
00228 //______________________________________________________________________________
00229 Bool_t TTreeDrawArgsParser::Parse(const char *varexp, const char *selection, Option_t *option)
00230 {
00231    // Parses parameters from TTree::Draw().
00232    // varexp - Variable expression; see TTree::Draw()
00233    // selection - selection expression; see TTree::Draw()
00234    // option - Drawnig option; see TTree::Draw
00235 
00236    ClearPrevious();
00237 
00238    // read the data provided and fill class fields
00239    fSelection = selection;
00240    fExp = varexp;
00241    fOption = option;
00242    Bool_t success = ParseVarExp();
00243    success &= ParseOption();
00244 
00245    if (!success)
00246       return kFALSE;
00247 
00248    // if the name was specified find the existing histogram
00249    if (fName != "") {
00250       fOriginal = gDirectory->Get(fName);
00251    }
00252    else
00253       fOriginal = 0;
00254 
00255    DefineType();
00256 
00257    return kTRUE;
00258 }
00259 
00260 //______________________________________________________________________________
00261 TTreeDrawArgsParser::EOutputType TTreeDrawArgsParser::DefineType()
00262 {
00263    // Put the type of the draw result into fOutputType and return it.
00264 
00265    if (fDimension == 0){
00266       if (fEntryList)
00267          return fOutputType = kENTRYLIST;
00268       else
00269          return fOutputType = kEVENTLIST;
00270    }
00271    if (fDimension == 2 && fDrawProfile)
00272       return fOutputType = kPROFILE;
00273    if (fDimension == 3 && fDrawProfile)
00274       return fOutputType = kPROFILE2D;
00275 
00276    if (fDimension == 2) {
00277       Bool_t graph = kFALSE;
00278       Int_t l = fOption.Length();
00279       if (l == 0 || fOption.Contains("same")) graph = kTRUE;
00280       if (fOption.Contains("p")     || fOption.Contains("*")    || fOption.Contains("l"))    graph = kTRUE;
00281       if (fOption.Contains("surf")  || fOption.Contains("lego") || fOption.Contains("cont")) graph = kFALSE;
00282       if (fOption.Contains("col")   || fOption.Contains("hist") || fOption.Contains("scat")) graph = kFALSE;
00283       if (fOption.Contains("box"))                                                   graph = kFALSE;
00284       if (graph)
00285          return fOutputType = kGRAPH;
00286       else
00287          return fOutputType = kHISTOGRAM2D;
00288    }
00289    if (fDimension == 3) {
00290       if (fOption.Contains("col"))
00291          return fOutputType = kLISTOFGRAPHS;
00292       else
00293          return fOutputType = kPOLYMARKER3D;
00294    }
00295    if (fDimension == 1)
00296       return fOutputType = kHISTOGRAM1D;
00297    if (fDimension == 4)
00298       return fOutputType = kLISTOFPOLYMARKERS3D;
00299    return kUNKNOWN;
00300 }
00301 
00302 //______________________________________________________________________________
00303 TString TTreeDrawArgsParser::GetProofSelectorName() const
00304 {
00305    // Returns apropriate TSelector class name for proof for the object that is to be drawn
00306    // assumes that Parse() method has been called before.
00307 
00308    switch (fOutputType) {
00309       case kUNKNOWN:
00310          return "";
00311       case kEVENTLIST:
00312          return "TProofDrawEventList";
00313       case kENTRYLIST:
00314          return "TProofDrawEntryList";
00315       case kPROFILE:
00316          return "TProofDrawProfile";
00317       case kPROFILE2D:
00318          return "TProofDrawProfile2D";
00319       case kGRAPH:
00320          return "TProofDrawGraph";
00321       case kPOLYMARKER3D:
00322          return "TProofDrawPolyMarker3D";
00323       case kLISTOFGRAPHS:
00324          return "TProofDrawListOfGraphs";
00325       case kHISTOGRAM1D:
00326       case kHISTOGRAM2D:
00327          return "TProofDrawHist";
00328       case kLISTOFPOLYMARKERS3D:
00329          return "TProofDrawListOfPolyMarkers3D";
00330       default:
00331          return "";
00332    }
00333 }
00334 
00335 //______________________________________________________________________________
00336 Double_t TTreeDrawArgsParser::GetParameter(Int_t num) const
00337 {
00338    // returns *num*-th parameter from brackets in the expression
00339    // in case of an error (wrong number) returns 0.0
00340    // num - number of parameter (counted from 0)
00341 
00342    if (num >= 0 && num <= fgMaxParameters && fParameterGiven[num])
00343       return fParameters[num];
00344    else {
00345       Error("GetParameter","wrong arguments");
00346       return 0.0;
00347    }
00348 }
00349 
00350 //______________________________________________________________________________
00351 Double_t TTreeDrawArgsParser::GetIfSpecified(Int_t num, Double_t def) const
00352 {
00353    // num - parameter number
00354    // def - default value of the parameter
00355    // returns the value of *num*-th parameter from the brackets in the variable expression
00356    // if the parameter of that number wasn't specified returns *def*.
00357 
00358    if (num >= 0 && num <= fgMaxParameters && fParameterGiven[num])
00359       return fParameters[num];
00360    else
00361       return def;
00362 }
00363 
00364 //______________________________________________________________________________
00365 Bool_t TTreeDrawArgsParser::IsSpecified(int num) const
00366 {
00367    // returns kTRUE if the *num*-th parameter was specified
00368    // otherwise returns fFALSE
00369    // in case of an error (wrong num) prints an error message and
00370    // returns kFALSE.
00371 
00372    if (num >= 0 && num <= fgMaxParameters)
00373       return fParameterGiven[num];
00374    else
00375       Error("Specified", "wrong parameter %d; fgMaxParameters: %d", num, fgMaxParameters);
00376    return kFALSE;
00377 }
00378 
00379 //______________________________________________________________________________
00380 TString TTreeDrawArgsParser::GetVarExp(Int_t num) const
00381 {
00382    // Returns the *num*-th variable string
00383    // in case of an error prints an error message and returns an empty string.
00384 
00385    if (num >= 0 && num < fDimension)
00386       return fVarExp[num];
00387    else
00388       Error("GetVarExp", "wrong Parameters %d; fDimension = %d", num, fDimension);
00389    return "";
00390 }
00391 
00392 //______________________________________________________________________________
00393 TString TTreeDrawArgsParser::GetVarExp() const
00394 {
00395    // Returns the variable string, i.e. [var1[:var2[:var2[:var4]]]].
00396 
00397    if (fDimension <= 0)
00398       return "";
00399    TString exp = fVarExp[0];
00400    for (int i = 1; i < fDimension; i++) {
00401       exp += ":";
00402       exp += fVarExp[i];
00403    }
00404    return exp;
00405 }
00406 
00407 
00408 //______________________________________________________________________________
00409 TString TTreeDrawArgsParser::GetObjectTitle() const
00410 {
00411    // Returns the desired plot title.
00412    if (fSelection != "")
00413       return Form("%s {%s}", GetVarExp().Data(), fSelection.Data());
00414    else
00415       return GetVarExp();
00416 }
00417 

Generated on Tue Jul 5 15:43:01 2011 for ROOT_528-00b_version by  doxygen 1.5.1