Why is TTree mangling cut strings for me?

I am running root 5.34/18 and I have a piece of code that is inside a loop that is part of a GUI analysis package I am writing. In this code I generate the draw string and the cut string and pass them to the tree. After I do this I get some error messages printed out to the terminal about bad numerical expressions and the strings it outputs as a bad expression have nothing to do with what I put in. I have put a code excerpt and the errors below.

//first retrive the PID cut from the auxilliary file
TCutG* pidCut = retrievePIDCut(runs[i].runNumber);
//get the tree from the main file
TTree* tree = retrieveTree(runs[i].runNumber);
//build the draw command
string histName = makeBGSpecName(runs[i].runNumber);
ostringstream cmdBuilder;
cmdBuilder<<"Yfp>>"<<histName<<"(200,-50,50)";
string drawCommand = cmdBuilder.str();
//build the cut string
ostringstream yCut;
yCut<<RayCut<<" && "<<pidCut->GetName();
string cut = yCut.str();
//dump some debugging info to the terminal
cout<<"\nDebugging information\n"<<endl;
cout<<"Draw command is: \" "<<drawCommand.c_str()<<" \""<<endl;
cout<<"Cut string is:   \" "<<cut.c_str()<<" \"\n"<<endl;
tree->Print();
cout<<"\nEnd of debugging information\n"<<endl;
//draw the spectrum
tree->Draw(drawCommand.c_str(), cut.c_str());
whiteBoard->SetLogy(1);
whiteBoard->Update();

And this is the output on the terminal:

Debugging information

Draw command is: " Yfp>>h1yfpRun1156(200,-50,50) "
Cut string is:   " Rayid==0 && pidCut1156 "

******************************************************************************
*Tree    :run1156_tree: run1156_tree                                           *
*Entries :   734761 : Total =        26537364 bytes  File  Size =   15341916 *
*        :          : Tree compression factor =   1.73                       *
******************************************************************************
*Br    0 :Rayid     : Rayid/F                                                *
*Entries :   734761 : Total  Size=    2948638 bytes  File Size  =     101220 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=  29.11     *
*............................................................................*
*Br    1 :Tpos1     : Tpos1/F                                                *
*Entries :   734761 : Total  Size=    2948638 bytes  File Size  =     651776 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=   4.52     *
*............................................................................*
*Br    2 :Tpos2     : Tpos2/F                                                *
*Entries :   734761 : Total  Size=    2948638 bytes  File Size  =     740546 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=   3.98     *
*............................................................................*
*Br    3 :Pi1       : Pi1/F                                                  *
*Entries :   734761 : Total  Size=    2948444 bytes  File Size  =    1672944 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=   1.76     *
*............................................................................*
*Br    4 :Pi2       : Pi2/F                                                  *
*Entries :   734761 : Total  Size=    2948444 bytes  File Size  =    1782901 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=   1.65     *
*............................................................................*
*Br    5 :Xfp       : Xfp/F                                                  *
*Entries :   734761 : Total  Size=    2948444 bytes  File Size  =    2358722 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=   1.25     *
*............................................................................*
*Br    6 :Yfp       : Yfp/F                                                  *
*Entries :   734761 : Total  Size=    2948444 bytes  File Size  =    2674089 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=   1.10     *
*............................................................................*
*Br    7 :Thfp      : Thfp/F                                                 *
*Entries :   734761 : Total  Size=    2948541 bytes  File Size  =    2669742 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=   1.10     *
*............................................................................*
*Br    8 :Thscat    : Thscat/F                                               *
*Entries :   734761 : Total  Size=    2948735 bytes  File Size  =    2682855 *
*Baskets :       93 : Basket Size=      32000 bytes  Compression=   1.10     *
*............................................................................*

End of debugging information

Error in <TTreeFormula::Compile>:  Bad numerical expression : "Pid2(Rayid==0)"
Error in <TTreeFormula::Compile>:  Bad numerical expression : "Run1156Pid1"

Oddly enough it does manage to draw an empty histogram to the canvas and the title displayed for it is “Yfp {Rayid==0 && pidCut1156}”

Anyways, does anyone have any idea why this might be happening?

And now I have the answer. The X and Y variables of the TCutG were somehow being parsed from the title of the TH2F that I was generating from the tree. Unfortunately, I was changing the title and this was leading to the issues. By forcibly setting the X and Y variables of the TCutG to the correct values before saving it to a file I fixed the problem.

Thanks for posting your solution. I ran into what seems like a related problems and setting the variables explicitly via TCutG::SetVarX(“xname”) and TCutG::SetVarY(“yname”) did the trick for me.