Fill a th1 using Draw() and store it in a branch

Hi,
I need to fill a branch of histograms, using TTree->Draw(), but the histogram is stored empty.

TFile *ifile = new TFile("test1.root","READ");
TTree *treein = (TTree*)ifile->Get("mytree");
TFile *ofile = new TFile("test2.root","RECREATE");
TH1F *H = new TH1F("h","h",100,0,100);
TTree *treeout = new TTree("t","t");
treeout->Branch("hist","TH1F",H);
for (int i=0;i<treein->GetEntries();i++)
{
  treein->GetEntry(i);
  H = new TH1F(Form("h%i",i),"h",100,0,100);
  treein->Draw(Form("var>>h%i",i));
  treeout->Fill();
}

If I fill the histogram doing the usual Fill(), the TH1 is stored correctly… But I need to use Draw() in my macro.
I think the problem is when I do H = new TH1F , but if I don’t do it still doesn’t work.

Any help is welcome!

I solved it. This is the right code:

TFile *ifile = new TFile("test1.root","READ");
TTree *treein = (TTree*)ifile->Get("mytree");
TFile *ofile = new TFile("test2.root","RECREATE");
TH1F *H = new TH1F("h","h",100,0,100);
TTree *treeout = new TTree("t","t");
treeout->Branch("hist","TH1F",H);
for (int i=0;i<treein->GetEntries();i++)
{
  treein->GetEntry(i);
  TH1F Haux(Form("h%i",i),"h",100,0,100);
  treein->Draw(Form("var>>h%i",i));
  *H=Haux;
  treeout->Fill();
}

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