Home | News | Documentation | Download

Get Histogram from root files

Hi

I am trying to read histogram from a root file. Here is my attempt:

	const Char_t *directory="/home/haha/data.root";
	TFile *f1=TFile::Open(directory1,"READ");
	//TH1D *num11=(TH1D*)f1->Get(TString::Format("NumQinV;%d",i1));num11->Sumw2();
    //int =13;
    //char temp[20];
    //sprintf(temp,"Num;%d",13);
    //string a="Num;13";
	TString b;
	b.Format("Num;%d",13);
	TH1D *num11=(TH1D*)f1->Get(b);
	num11->Sumw2();

But this gives a weird graph. I have also tried using the sprintf method to format string then put in as the argument of Get. But that does not work neither. Could anyone explain what happen?

Thanks.

In your example you do not produce a graph (no drawing). Can you provide an example we can run ?

Hi

I must have done something wrong before but now it works if I use

TString b;
b.Form("Num%d",13);
TH1D *num11=(TH1D*)f1->Get(b);

But I still don’t understand why the sprintf and TString::Format do not work (See the commented code). For these two method I can’t get the histogram and it gives a null pointer error at the line num11->Sumw2(). Thank you very much.

std::cout << "b = " << b << std::endl; // try it before and then afterwards

See methods’ descriptions: TString

The variable "directory " is wrong,you can change it in the code.

Hi
My program works now. But I forgor what I have changed. I guess I just made some silly mistake.
Thanks.

In ROOT,function “sprintf” is always used as another function TString::Form().For example,it always used as this:

Form("%s",location); //location is an object of TString instanced before.