TTreeFormula() analysis

Hello,

I’m implementing a TTree analysis, and while doing some tests with TTreeFormula I found the following seg.fault which I cannot understad:

I define:

TCut cut = "trigNames.find(\"HLT_Iso\")==0";
TChain *ProcFile= new TChain("AnaTree");
ProcFile->Add("/lustre/data3/cmslocal/bargassa/Leg2/5fb/SingleMu_rereco_May10/*.root");
TH1D * hist = new TH1D("hist","hist",5,0,5);

If I then execute, everything works perfectly:

ProcFile->Draw("MCweight>>hist",cut);

If instead, I do something like:

vector<string> *trigNames = 0;
ProcFile->SetBranchAddress("trigNames",&trigNames);
Double_t MCweight = 0;
ProcFile->SetBranchAddress("MCweight", &MCweight);
TTreeFormula *formula=new TTreeFormula("formula",cut,ProcFile);

for (Int_t entry = 0; entry<10000; entry++) {
   ProcFile->GetEntry(entry);
   if (formula->EvalInstance()>0)
     hist->Fill(MCweight);
 }

Things are ok also.

The problem appears when I try to execute both sets of coded sequentially. Independently of which one comes first, I get to execute correctly the first set of instructions while things crash on the second.

Apart from this, I have another question regarding which method is more efficient. Inside the loop, should retrieve entries by calling the GetEntry(entry) or the LoadTree(entry) methods?

Thanks in advance,

Hi Miguel,

Inside the loop, you should call LoadTree (the TTreeFormula will do the GetEntry on the branch it needs), but you also need to call UpdateLeaves every time LoadTree change the value of GetTreeNumber.

Cheers,
Philippe.

Hi,

Thanks for the answer. Still related to the TTreeFormula’s I would like to know if the following:

On a TTree branch which is a vector, e.g. vector<double> vjtPt. I would like to know if it is possible to define formulas from conditions on its elements, without using the GetNdata() to cycle over the vector, such as:

TCut cut1 = "(vjtPt_pf[0]>30)&&(vjtPt_pf[1]>30)"
TCut cut2 = "(vjtPt_pf[0]>30)&&(vjtPt_pf[1]>30)&&(vjtPt_pf[2]>30)"

With the TTree’s I’m working, this is strangely working fine when I use “cut1”, but not “cut2”. Is this just a coincidence?

This kind of TCut’s can be applied to TTree::Draw() analysis method. And I’m trying to migrate an analysis code using the Draw() method, while trying to keep the selections cut definitions unchanged.

Cheers,

[quote]This kind of TCut’s can be applied to TTree::Draw() analysis method. [/quote]Anything that works with TTree::Draw should work with TTreeFormula.

TCut cut2 = "(vjtPt_pf[0]>30)&&(vjtPt_pf[1]>30)&&(vjtPt_pf[2]>30)"Note that this formula will return GetNdata()==0 for entries where vjtPt_pf.size() < 2.

Cheers,
Philippe.

Hi Philippe,

Would it be possible to read a single element in a branch which is a vector

into a variable, by calling GetEntry, using something like:

double vjtPt_pf1;
ttree->SetBranchAddress("vjtPt_pf[1]",&vjtPt_pf1);

If not, an alternative would be to define a TTreeFormula with:

TTreeFormula *vjtPt_pf1=new TTreeFormula("vjtPt_pf1","vjtPt_pf[1]",ttree);

Would this return a default value, e.g. 0, when the vector is smaller than the entry we are trying to get? Or would it always be necessary to check for GetNdata().

In terms of performance, which method is faster?

Thanks a lot for your support,
Miguel

[quote]into a variable, by calling GetEntry, using something like:[/quote]No this is not possible (In part because this operation is undefined if the size of the vector is less than 2).

[quote]Would this return a default value, e.g. 0, when the vector is smaller than the entry we are trying to get? Or would it always be necessary to check for GetNdata().[/quote]I think it would return 0, however it is better and more efficient to check the value of GetNdata.

In term of performance the best is:std::vector<double> * vjtPt_pf = 0; TBranch *branch = 0; ttree->SetBranchAddress("vjtPt_pf",&vjtPt_pf1,&branch); ... branch->GetEntry( entry );

Cheers,
Philippe.