Using information from TTree for histogram name in TTree::Draw

Hi everyone,

I’m wondering if this is possible: I have a few different histograms for different backgrounds and would like to plot something, say dE/dx, for each of the separate backgrounds.

I would like to do something like this (just an example with two backgrounds but obviously becomes more useful with more):

TH1F* dEdx_bg[2];
dEdx_bg[0] = new TH1F("dEdx_2212","",100,0,10);
dEdx_bg[1] = new TH1F("dEdx_321","",100,0,10);


However, I can’t seem to make this work. I’m wondering if root just can’t evaluate elements of the tree after the >>. Or, if this should work, what the correct syntax should be?


The parameter passed to TTree:Draw is a string and Abs will not be evaluated this way. The way to do it is:

tree->Project("dEdx_2212", "TrackdEdx", "abs(TrackPDG) == 2212");
tree->Project("dEdx_321", "TrackdEdx", "abs(TrackPDG) == 321");

Just what I needed, thank you!

FYI for anyone else needing this, the Form needs to be wrapped in quotes to for Draw to evaluate (and obviously the inner quotes escaped):


That’s not true. Form already return a text string. See the following:

root [0] ntuple->Draw("px")
root [1] float f=2.
(float) 2.00000f
root [2] ntuple->Draw(Form("%g*px",f));

He wants this “Form” to be evaluated on an event-by-event basis (when “Draw” is being executed).

Ok but all I get with a such construct is errors … I tried to mimic it with:

root [0] ntuple->Draw( "Form(\"px>>h%d\",1)" );
Error in <TTreeFormula::Compile>:  ')' is expected
Info in <TSelectorDraw::AbortProcess>: Variable compilation failed: {Form("px,}

May be I did an obvious mistake … but I do not see it right now.

That’s why I split it into two separate “Project” (“Draw”) commands.

I got that. I was just pointing the fact that, seems to me, the command posted by @mike1 (with Form between quotes) cannot work.

Yes, sorry you’re right. But the way you posted didn’t work either actually, I thought I’d fixed it but obviously didn’t!

If the number is in the tree, then root can’t evaluate it like that.

plot.C:231:48: error: use of undeclared identifier 'MuonPDG'
    treeBg->Draw(Form("MuondEdx>>dEdxBg%d",MuonPDG), cutIt->cut);

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