Home | News | Documentation | Download

Plotting values in a TTree multiplied by a variable without bad numerical expression error

Hi, I have what I hope is an easily solvable problem in that I want to draw some values from a TTree but multiplied by a scale factor. For example

TChain * tree = new TChain("ntp");
int nFile = tree->Add("some_root_file");
TH1F *hist=new TH1F("hist","",100,10000,500000);

tree->Draw("some_value_in_the_tree*10>>hist");

Works fine and multiplies all the some_value_in_the_tree by 10. However, if I instead define the scale factor as a variable rather than putting it in directly I get a bad numerical expression error. So the tree->Draw line becomes.

double scale_value = 10;
tree->Draw("some_value_in_the_tree*scale_value>>hist");

Any help on how to achieve this without the bad numerical expression error would be greatly appreciated.

I’m running ROOT 6.04/14 through a terminal on a Windows 10 machine

tree->Draw(TString::Format("some_value_in_the_tree * %g >> hist", scale_value));

Thanks for the reply! That solution works perfectly. I have one followup question though, if I want to add some more information to the drawing line as in the example below, how do I do that?

TString CoincidenceCut = "(another_variable==1)";
tree->Draw(TString::Format("some_value_in_the_tree * %g >> hist", scale_value,CoincidenceCut,"SAME"));

Where I want to introduce the cut CoincidenceCut and have the plot on the same canvas as some other histogram. The example above gives the errors error: cannot pass non-trivial object of type 'TString' to variadic function; expected type from format string was 'double' [-Wnon-pod-varargs],

warning: data argument not used by format string [-Wformat-extra-args],

and error: cannot pass object of non-trivial type 'TString' through variadic function; call will abort at runtime [-Wnon-pod-varargs]

For the last three bits of the draw line respectively.

tree->Draw(TString::Format("some_value_in_the_tree * %g >> hist", scale_value), CoincidenceCut.Data(), "SAME"); // note: "hist" will be reset

That’s worked perfectly! Thank you very much for your help

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