Get Histogram from root files


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];
    //string a="Num;13";
	TString b;
	TH1D *num11=(TH1D*)f1->Get(b);

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?


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


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

TString b;
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.

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

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.