TTreeFormula branch detection

Hello guys,

I have a tree with a branch called “reference_frame.lab.p0” and a second one called “a”. This first variable is a lorentzvector and the second a double.

When I draw an histogram using this branch I am calling the following command (this is an illustration of my problem):

t->Draw("TMath::Sqrt(reference_frame.lab.p0.P()*reference_frame.lab.p0.P()-reference_frame.lab.p0.E()*reference_frame.lab.p0.E())")

This formula seems to be processed using TTreeFormula.
Basically I am looking for a way to properly catch “reference_frame.lab.p0” and replace it by an other one and do not replace wrong part of the string like “a”… which occurs everywhere

Do you have any suggestion ?
For the moment I wrote this :

TList *l = (TList*) tree->GetListOfBranches();
    std::cout << varexp_expanded << std::endl;
    for(int i = 0, N = l->GetSize(); i < N; i++) {

        TString branchname = l->At(i)->GetName();

        int j = 0;
        int index = 0;
        //std::cout << "branch:" << branchname << std::endl;
        while((index = varexp_expanded.Index(branchname, (Ssiz_t) index)) != -1) {

            //if(index == 5) std::cout << branchname << std::endl;
            TString substr1 = varexp_expanded(index-1,branchname.Length()+1).String();
            TString substr2 = varexp_expanded(index-2,branchname.Length()+2).String();
            std::cout << "input: " << varexp_expanded << std::endl;
            std::cout << "index: " << index<< std::endl;
            std::cout << "substring: " << substr1 << std::endl;

            if(!substr1.BeginsWith(".") && !substr2.BeginsWith("::")) {

                TString replacement = (TString) tree->GetName() + ".";
                varexp_expanded.Replace(index, 0, replacement);
                index += replacement.Length();
            }

            index += branchname.Length();

            j++;
            if(j>10) gSystem->Abort();
        }
    }


    std::cout << varexp_expanded << std::endl;
    std::cout << std::endl;

It’s not clear to me what you actually want to do. Do you want to plot a different TTreeFormula? Or replace the name of the branch in the file? Can you try to explain a bit more what exactly you want to do?

Hi Amadio,

I would like to be able to properly catch the tree leaves in the string. Some of my leaves are too short and catched some simple char like “s” in my TTreeFormula. So either if TTreeFormula is returning something it would be useful or I just try to catch words.

I finally found what I needed by doing the following :

TList *l = (TList*) t->GetListOfBranches();
for(int i = 0, N = l->GetSize(); i < N; i++) {

    TString branchname = l->At(i)->GetName();
    Ssiz_t length;

    int index = 0;
    TPRegexp r1("[*\\/\\-+() ]+"+branchname+"[*\\/\\-+() ]?");
    while((index = varexp_expanded.Index(r1, &length, (Ssiz_t) index)) != -1) {

        TString substr1 = varexp_expanded(index,length);
        TPRegexp r3("[^*\\/\\-+() ]");
        int offset = substr1.Index(r3);

        TString substr2 = varexp_expanded(index+offset-1,length-offset+1);
        if(!substr2.BeginsWith(".")) {

            TString replacement = (TString) t->GetName() + ".";
            varexp_expanded.Replace(index+offset, 0, replacement);
            index += replacement.Length();
        }

        index += branchname.Length();
    }

    index = 0;
    TPRegexp r2("[*\\/\\-+() ]?"+branchname+"[*\\/\\-+() ]+");
    while((index = varexp_expanded.Index(r2, &length, (Ssiz_t) index)) != -1) {

        TString substr1 = varexp_expanded(index,length);
        TPRegexp r3("[^*\\/\\-+() ]");
        int offset = substr1.Index(r3);

        TString substr2 = varexp_expanded(index+offset-1,length-offset+1);
        if(!substr2.BeginsWith(".")) {

            TString replacement = (TString) t->GetName() + ".";
            varexp_expanded.Replace(index+offset, 0, replacement);
            index += replacement.Length();
        }

        index += branchname.Length();
    }
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.