TTreePlayer::Scan (TTreeFormula) on non existent leaves

Dear rooters,

I have noticed that TTreePlayer::Scan
don’t show any error whenever you provide
something like:

TTreePlayer::Scan("branchname.leafname");

with ‘branchname’ an existing branch name but
‘leafname’ not existing.
The first column of the branch ‘branchame’ is returned,
as if you were called:

TTreePlayer::Scan("branchname");

If ‘branchname.’ is not included in the function call:

TTreePlayer::Scan("leafname");

TreePlayer report the non existing leaf as follows:
Error in TTreeFormula::Compile: Bad numerical expression : “leafname”

and don’t show any number in the column corresponding to ‘leafname’.

In branches containing several leaves these kind of input error
could be hard to catch.

To see one example run the attached files as follows:

$ROOTSYS/bin/root -l -q circular.C
$ROOTSYS/bin/root -l -q test_circular.C

I have installed ROOT 6.03/05

Thank you.
test_circular.C (432 Bytes)
circular.C (859 Bytes)

The problem arise how TTreeFormula parse and check the validity of the input expression.
The current implementation accept as valid expression something like: 'branchname.leafname’
with ‘branchname’ the name of an existing branch in the TTree, but ‘leafname’ not being
a leaf name belonging to such branch: in this cases the first leaf in the branch 'branchname’
is assumed.

This behaviour may provide the wrong results when applying selections on a TTree.
Consider the following example:

// $ROOTSYS/bin/root -l -q test_cuts.C
#include <iostream>
#include <iomanip>
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
//=============================================================================
void print(TTree *t,const char *cut, const char* comm)
{
  int pw = 28;
  TString ss("t->GetEntries(\"");
  ss += TString(cut) + TString("\"): ");
  
  std::cout << std::left <<  ss.Data()
            <<std::right
            << std::setw(pw - ss.Length())
            << t->GetEntries(cut)
            << " # " << comm
            << std::endl;
  return;
}
//=============================================================================
void make_tree()
{
  TFile f("test-TTreeFormula-cuts.root","RECREATE");
  TTree nt("nt","nt");
  struct data
  {
      float v1;
      float v2;
      float v3;
  }_Z,_H;
  nt.Branch("z",&_Z,"e/F:pt:mz");
  nt.Branch("h",&_H,"e/F:pt:mh");
  _Z.v1 = 80.1;
  _Z.v2 = 65.2;
  _Z.v3 = 91.2;
  _H.v1 = 110.2;
  _H.v2 = 98.43;
  _H.v3 = 124.35;
  nt.Fill();
  _Z.v1 = 100.1;
  _Z.v2 = 99.2;
  _Z.v3 = 90.6;
  _H.v1 = 85.0;
  _H.v2 = 130.0;
  _H.v3 = 125.75;
  nt.Fill();
  nt.Write("",TObject::kOverwrite);

  return;
}
//=============================================================================
void do_test()
{
  TFile *_file0 = TFile::Open("test-TTreeFormula-cuts.root");
  TTree *t = 0;
  _file0->GetObject("nt",t);
  t->Print();
  TString vars("z.e:z.pt:z.mz:h.e:h.pt:h.mh");
  t->Scan(vars.Data(),"","precision=4");
  float max_hpt   = 180;
  float min_he    = 25;
  float min_hmass = 90;
  print(t,TString::Format("h.e>%.0f",min_he).Data(),"ok");
  print(t,TString::Format("h.pt<%.0f",max_hpt).Data(),"ok");
  print(t,TString::Format("h.mh>%.0f",min_hmass).Data(),"ok");
  print(t,TString::Format("h.m>%.0f",min_hmass).Data(),
        "wrong leaf name! Used 'h.e' instead of 'h.mh'");
  return;
}
//=============================================================================
int test_cuts()
{
  make_tree();
  do_test();
  return 0;
}

There is intentionally a typo in one of the cuts:
TTreeFormula dont show any error and TTreeFormula::GetNdim return non-zero,
but the returned number of entries passing the selection is clearly wrong.

Cheers,
Tino