Validity of branch names for TTree

I imagine to sooner or later end up in trouble when I have +-/*()#[]<>% in a branch name.

I see that the following does not crash, raise exceptions, …

TTree* t = new TTree("mytree","treetitle")
float thefloat
t->Branch("foo+baz/bar",&thefloat)

Whether this behaviour is good or bad is not so much of what I’m concerned, I’m interested if there is a sanity checker existing already which validates if a TString (or std::string or const char*) is “safe for branch name usage”?

Or at least a list of things which I should check against (at programming time I don’t know what branch names I will encounter. In my program the name will come from argv)?

Spontaneously, I would check against the above list, a leading digit, and whitespaces.

Hi,

The name of branch can ‘technically’ contains any ascii characters … however, as you point out, many of those characters can lead to problem when using most of the tools based on TTree (for example TTree::Draw).

The easiest definition is that one should restrict the name of the branch to be a valid C++ variable name, hence including the characters you mention and at least “.:” (and of course quotes). However, some experiment manage to successfull use a single “:” or “.” in the name in some circumstances.

Cheers,
Philippe.

Hi,

for now I think I’ll allow only valid c++ variable names. We also have some “standard branches” with periods but I’ll not allow creating more of these in the app. So from cplusplus.com/reference/regex/regex_match/ I took as first check

  if (std::regex_match(argv[1],std::regex("([a-zA-Z_][a-zA-Z0-9_]*)")))
    std::cout << "fine" << std::endl;
  else
    std::cout << "bad" << std::endl;

On top of that I’ll black list a few keywords. I think I want to ensure that MakeClass creates more or less valid code and that Draw will not end up in a mess: TMath, cos, float, int, sin, abs, acos, atan, tan, asin, exp, log, double, class, static, inline, const, auto, Notify, Loop, max, min, new, delete, …
github.com/pseyfert/tmva-branch … lacklist.h

I restructured the code a bit, the new black list is now here:
https://github.com/pseyfert/tmva-branch-adder/blob/master/src/blacklist.cpp

and can be called like

    int                          SetTargetBranch(TString name) {
      // http://stackoverflow.com/questions/12993187/regular-expression-to-recognize-variable-declarations-in-c
      // https://root-forum.cern.ch/t/validity-of-branch-names-for-ttree/20936/3
      if (std::regex_match(name.Data(),std::regex("([a-zA-Z_][a-zA-Z0-9_]*)"))) {
        if (blacklisted(name)) {
          return 9;
        }
        m_targetbranchname = name;
        return 0;
      }
      return 8;
    }