In the example code below, I try to plot f(x) = |1/(1+ix)|^2
in four different ways. The first three work, but the fourth doesn’t.
I guess the reason is that 1 and 2 use the “new” TFormula
, while 3 and 4 use TTreeFormula
which relies on the older version ROOT::v5::TFormula
. Is that right? If so, are there any plans to update TTreeFormula
? It’s not urgent, but it would be convenient if method 4 also worked.
I’m using v6.22.06.
Thanks!
TTree* xTree;
TH1F* xHist;
TTree* makeXTree(){
if (xTree) delete xTree;
xTree = new TTree("xTree","xTree");
double xVar = 0.0;
xTree->Branch("xVar",&xVar,"xVar/D");
for (int i = 0; i < 100000; i++){
xVar = gRandom->Uniform(0.0,10.0);
xTree->Fill();
}
return xTree;
}
complex<double> oneOverOnePlusIX(double x){
complex<double> onePlusIX(1.0,x);
return complex<double>(1.0,0.0)/onePlusIX;
}
double normOneOverOnePlusIX(double x){
return norm(oneOverOnePlusIX(x));
}
void plotNormOneOverOnePlusIX_works1(){
if (xHist) delete xHist;
xHist = new TH1F("xHist","xHist",100,0.0,10.0);
TFormula rootFormula("tempFormula","normOneOverOnePlusIX(x)");
for (int iBin = 1; iBin <= 100; iBin++){
double x = xHist->GetBinCenter(iBin);
xHist->SetBinContent(iBin,rootFormula.Eval(x));
}
xHist->Draw("hist");
}
void plotNormOneOverOnePlusIX_works2(){
if (xHist) delete xHist;
xHist = new TH1F("xHist","xHist",100,0.0,10.0);
TFormula rootFormula("tempFormula","norm(oneOverOnePlusIX(x))");
for (int iBin = 1; iBin <= 100; iBin++){
double x = xHist->GetBinCenter(iBin);
xHist->SetBinContent(iBin,rootFormula.Eval(x));
}
xHist->Draw("hist");
}
void plotNormOneOverOnePlusIX_works3(){
if (xHist) delete xHist;
xTree = makeXTree();
xTree->Project("xHistFromTree(100,0.0,10.0)","xVar","normOneOverOnePlusIX(xVar)");
xHist = (TH1F*) gDirectory->FindObject("xHistFromTree"); xHist->SetDirectory(0);
xHist->Draw("hist");
}
void plotNormOneOverOnePlusIX_fails4(){
if (xHist) delete xHist;
xTree = makeXTree();
xTree->Project("xHistFromTree(100,0.0,10.0)","xVar","norm(oneOverOnePlusIX(xVar))");
xHist = (TH1F*) gDirectory->FindObject("xHistFromTree"); xHist->SetDirectory(0);
xHist->Draw("hist");
}