# Get Histogram from root files

**URL:** <https://root-forum.cern.ch/t/get-histogram-from-root-files/44065>\
**Category:** Newbie\
**Created:** [March 19, 2021, 4:55am UTC](https://root-forum.cern.ch/t/get-histogram-from-root-files/44065 "2021-03-19T04:55:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![haha123](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@haha123](https://root-forum.cern.ch/u/haha123)\
**Post date:** [March 19, 2021, 4:55am UTC](https://root-forum.cern.ch/t/get-histogram-from-root-files/44065/1 "2021-03-19T04:55:10Z")

</div>

Hi

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

```auto
	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.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 19, 2021, 7:50am UTC](https://root-forum.cern.ch/t/get-histogram-from-root-files/44065/2 "2021-03-19T07:50:41Z")

</div>

> [@haha123](#):
>
> But this gives a weird graph

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

---

<div class="post-metadata">

**Author:** ![haha123](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@haha123](https://root-forum.cern.ch/u/haha123)\
**Post date:** [March 19, 2021, 4:24pm UTC](https://root-forum.cern.ch/t/get-histogram-from-root-files/44065/3 "2021-03-19T16:24:09Z")

</div>

Hi

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 19, 2021, 4:35pm UTC](https://root-forum.cern.ch/t/get-histogram-from-root-files/44065/4 "2021-03-19T16:35:49Z")

</div>

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

See methods’ descriptions: [TString](https://root.cern/doc/master/classTString.html)

---

<div class="post-metadata">

**Author:** ![ZhuKun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/58f4c7/32.png) [@ZhuKun](https://root-forum.cern.ch/u/ZhuKun)\
**Post date:** [March 24, 2021, 1:02am UTC](https://root-forum.cern.ch/t/get-histogram-from-root-files/44065/5 "2021-03-24T01:02:06Z")

</div>

> [@haha123](#):
>
> 试过使用sprintf方法格式化字符串，然后将其作为Get的参数放入。但这也不起作用。谁能解释会发生什么？

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

---

<div class="post-metadata">

**Author:** ![haha123](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@haha123](https://root-forum.cern.ch/u/haha123)\
**Post date:** [March 24, 2021, 1:14am UTC](https://root-forum.cern.ch/t/get-histogram-from-root-files/44065/6 "2021-03-24T01:14:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ZhuKun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/58f4c7/32.png) [@ZhuKun](https://root-forum.cern.ch/u/ZhuKun)\
**Post date:** [March 24, 2021, 1:31am UTC](https://root-forum.cern.ch/t/get-histogram-from-root-files/44065/7 "2021-03-24T01:31:15Z")

</div>

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.
