Re: [ROOT] Dividing histograms from a TChain

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Jan 14 2002 - 22:07:55 MET


Hi Emily,

A short example showing how to loop on all the files of a chain.
Note that a TChain was not designed as a collection of files,
rather as a virtual Tree combining the same tree name in
different files.
However, it would be simple to provide an iterator to loop
directly on all the files. I will add it to TChain.

void loopchain(TChain *ch) {
	TIter nextfile(ch->GetListOfFiles());
	TChainElement *elem;
	while ((elem = (TChainElement*)nextfile())) {
		TFile *f = new TFile(elem->GetTitle(),"update");
		TH2D *num = (TH2D*)f->Get("h810");
      TH2D *den = (TH2D*)f->Get("h811");
      num->Divide(den);
		num->Write(); // write result to the file or something
else
		delete f;
	}
}

Rene Brun


On Mon, 14 Jan 2002, Emily Nurse wrote:

> Hello,
> Sorry if this is a simple question.......
> I am trying to divide two histograms, each one comes from a TChain made up
> of .root files. I realse the histograms have to be renamed before they can
> be divided but the suggested way of doing this is using:
> 
> TFile *f = new TFile("myfile.root");
> 
> f->ls();
>    TH2D   h810;1  Hist title
>    TH2D   h811;1  Hist title
> 
> h810->Draw();  // this works
> h811->Draw();  // this works
> 
> TH2D *num = (TH2D*)f->Get("h810");
> TH2D *den = (TH2D*)f->Get("h811");
> 
> num->Divide(den);  //  this works!
> 
> how-ever I have more than one file so what would I need to put in place of
> f in this example?
> Thanks
> Emily.
> 



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:38 MET