TTree::Draw() creating several histograms per search cycle

Hello again,

This question is somehow related to my last one. I have a really big tree and I realized that all my histograms that I have always created in a very nice looking way via TTree::Draw() have to loop over all the data to be created. So creating several histograms produces several loops.

I suppose there is no way, to “load” a command and to run the loop only once? Somthing like this:

TTree::LoadDraw(“myvalue_x >> myhist1(100,0,1)”, “mycut”); // set one draw command
TTree::LoadDraw(“myvalue_y >> myhist2(100,0,1)”, “mycut”); // set the next one
TTree::LoadDraw(“myvalue_z >> myhist3(100,0,1)”, “mycut”);
TTree:DrawLoaded(); // perform all the operations before in only one loop
TH1* myhist1 = (TH1*)gDirectory->Get(“myhist1”);
TH1* myhist2 = (TH1*)gDirectory->Get(“myhist2”);
TH1* myhist3 = (TH1*)gDirectory->Get(“myhist3”);

I guess this would lead to consistency problems with simultanious drawing to Pads as well as the program structure itself -> I have to change my code to a loop of my own

for (int i = 0; i < tree->GetEntries(); i++){
tree->GetEntry(i);
if (mycut){
hist1->Fill(myvalue_x);
}
if (mycut){
hist2->Fill(myvalue_y);
}
//etc.
}

?

Hi,

use a TChain; see e.g. tutorials/tree/h1chain.C and the users guide.

Cheers, Axel.

Hi,

One solution is to use the TTree::Draw syntax that uses a script:[code]// multihist.C
TH1F *myhist1;
TH1F *myhist2;
TH1F *myhist3;

void h1analysisProxy_SlaveBegin(TTree *tree) {
myhist1 = new TH1F(“myhist1”,“myhist1”,100,0,1);
myhist2 = new TH1F(“myhist2”,“myhist2”,100,0,1);
myhist3 = new TH1F(“myhist3”,“myhist3”,100,0,1);
}

Double_t multihist() {
if (mycut) {
myhist1->Fill(myvalue_x);
myhist2->Fill(myvalue_y);
myhist3->Fill(myvalue_z);
}
return 0;
}

void multihist_Terminate() {
// Display the histograms, in this example, all on the same pad.
myhist1->Draw();
myhist2->Draw(“SAME”);
myhist3->Draw(“SAME”);
}[/code]and use:tree->Draw("multihist.C","","nohist");
The ‘nohist’ is to suppress the creation of the default histogram which would contains the return value of the function multihist().

Alternatively you could use a TEntryList:mytree->Draw(">>cutlist","mycut","entrylist"); TEntryList *cutlist = (TEntryList*)gDirectory->Get("mycut"); mytree->SetEntryList(cutlist); mytree->Draw("myvalue_x >> myhist1(100,0,1)", ""); mytree->Draw("myvalue_y >> myhist2(100,0,1)", ""); mytree->Draw("myvalue_z >> myhist3(100,0,1)", "");

Cheers,
Philippe.

I really appreciate that you condense the example to what I need! I will try it.