Getting Fill(i, v[i]) behavior from TTree::Draw()

Given a file with TTree “MyTree” containing branch “waves” which is a vector, where wave.size() == 1500, I can do this:

TFile *f = new TFile("myfile.root", "READ");
TTree *t;
f->GetObject("MyTree", t);
TH1I *hWaves = new TH1I("hWaves", "Waveforms", 1500, 0, 1499);
t->Draw("waves >> hWaves", "", "");

And this will fill a histogram the same as:

TFile *f = new TFile("myfile.root", "READ");
TTree *t;
vector<double> wave;
f->GetObject("MyTree", t);
t->SetBranchAddress("waves", &wave);
TH1I *hWaves = new TH1I("hWaves", "Waveforms", 1500, 0, 1499);
for(int i=0;i < t->GetEntries();i++)
{
  t->GetEntry(i);
  for(int k=0;k < wave.size();k++)
    hWaves->Fill(waves[k]);
}

However I’m interested in (quickly) plotting an average waveform. What I would like to do, using TTree::Draw(), is the analog to:

TFile *f = new TFile("myfile.root", "READ");
TTree *t;
vector<double> wave;
f->GetObject("MyTree", t);
t->SetBranchAddress("waves", &wave);
TH1I *hWaves = new TH1I("hWaves", "Waveforms", 1500, 0, 1499);
for(int i=0;i < t->GetEntries();i++)
{
  t->GetEntry(i);
  for(int k=0;k < wave.size();k++)
    hWaves->Fill(k, waves[k]);
}
hWaves->Divide(t->GetEntries());

Which would also allow me to easily make a selection based on other branches or friend trees. Is this possible? It seems like it should be and I’m overlooking something simple in the TTree::Draw() syntax. Thanks!

I’m not sure if I correctly understood your goal, but I think you can do it with

It’s difficult to find in the huge class documentation for TTree, but if you go here:
http://root.cern.ch/root/html/TTree.html#TTree:Draw@1
and search for the string “Entry$”, you will get a list of all the Special$ variables that can be used in a TTree::Draw command.

Jean-François

[quote=“jfcaron”]I’m not sure if I correctly understood your goal, but I think you can do it with

It’s difficult to find in the huge class documentation for TTree, but if you go here:
http://root.cern.ch/root/html/TTree.html#TTree:Draw@1
and search for the string “Entry$”, you will get a list of all the Special$ variables that can be used in a TTree::Draw command.

Jean-François[/quote]

Thanks Jean-Francois! That is very close - however I would like it to plot on a TH1 instead of a scatter plot or TH2 - so waves[Iteration$] is used as a weight, and the histogram is filled with Iteration$,ie:

TH1::Fill(Iteration$, waves[Iteration$])
//as opposed to
TH2::Fill(Iteration$, waves[Iteration$])

Does that make sense?

The selection formula can be used as a weight. When you do

the condition is normally a boolean true/false statement, but if the expression is a non-zero numerical value, it is used as a weight. So (again, if I am interpreting properly), you could do something like

Jean-François

That did it Jean-Francois, thanks.

Deleted.