#include "TROOT.h" #include "TFile.h" #include "TTree.h" #include "TBrowser.h" #include "TH2.h" #include "TRandom.h" void tree1w( bool getndata ) { gRandom->SetSeed( 123456 ); //create the Tree and a few branches TTree t1("t1","a simple Tree with simple variables"); Float_t px, py, pz; t1.Branch("px",&px,"px/F"); t1.Branch("py",&py,"py/F"); t1.Branch("pz",&pz,"pz/F"); vector vecp; t1.Branch("vecp",&vecp); // create tree formulas TTreeFormula ttfFloat("ttfFloat","py",&t1); // evaluates a float == py TTreeFormula ttfVector("ttfVector","vecp[1]",&t1); // evaluates a vector element ==py // event loop for (Int_t i=0;i<10;i++) { // reset vecp.clear(); // generate event gRandom->Rannor(px,py); pz = px*px + py*py; vecp.push_back(px); vecp.push_back(py); vecp.push_back(pz); // evaluate formulas if( getndata ) ttfVector.GetNdata(); cout << " ttfFloat : py = " << py << " / " << ttfFloat.EvalInstance() << " ttfVector : py = " << vecp[1] << " / " << ttfVector.EvalInstance() << endl; // fill t1.Fill(); } } void tree1() { cout << endl; cout << "Printout : py variable (double) / same from formula; py as vector element / same from formula" << endl; cout << endl; cout.setf(ios::showpos); cout.setf(ios::fixed,ios::floatfield); cout.precision(5); cout << "No call to GetNdata : looks ok ?!" << endl; cout << endl; tree1w( false ); cout << endl; cout << "With call to GetNdata : vector gets stuck at first event" << endl; cout << endl; tree1w( true ); }