# Reading a Histogram from a Canvas stored in a Root file

**URL:** <https://root-forum.cern.ch/t/reading-a-histogram-from-a-canvas-stored-in-a-root-file/55109>\
**Category:** Newbie\
**Created:** [May 31, 2023, 10:34am UTC](https://root-forum.cern.ch/t/reading-a-histogram-from-a-canvas-stored-in-a-root-file/55109 "2023-05-31T10:34:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Natilus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@Natilus](https://root-forum.cern.ch/u/Natilus)\
**Post date:** [May 31, 2023, 10:34am UTC](https://root-forum.cern.ch/t/reading-a-histogram-from-a-canvas-stored-in-a-root-file/55109/1 "2023-05-31T10:34:16Z")

</div>

Dear Root experts,  
I have a simple script that reads histograms from several root files (one histogram called JPsiMass from a Canvase with the same name stored in each file) and stores in a vector of TH1D\*.  
After reading and storing them, I can Draw them but that’s all I can do.  
I would like, for example, find the bin with maximum content in one of them (please see the code attached) but I get the following error:

```auto
In file included from input_line_9:6:
././HistogramAnalyzer.cc:51:77: error: no member named 'GetMaximun' in 'TH1D'
        std::cout << "#########################################" << tempHistogram->GetMaximun() << endl;

```

My code is:

```auto
void HistogramAnalyzer(std::vector<std::string> FileNames) {

	std::vector<TH1D*> InputHistograms;

	for (auto FN=FileNames.begin(); FN!=FileNames.end(); FN++) {
		TFile* InputRootFile = new TFile(FN->c_str(), "READ");
		InputRootFile->ls();
		TCanvas* InputCanvas = (TCanvas*)InputRootFile->Get("JPsiMass");
		TH1D* InputHistogram = (TH1D*)InputCanvas->GetPrimitive("JPsiMass");
		InputHistograms.push_back(InputHistogram);
	}

	TCanvas* temp_canv = new TCanvas("temp","temp");
	InputHistograms[0]->Draw();
	std::cout << "Maximum Bin = " << InputHistograms[0]->GetMaximumBin() << endl;
}

```

Thank you for your help in advance

---

<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:** [May 31, 2023, 10:36am UTC](https://root-forum.cern.ch/t/reading-a-histogram-from-a-canvas-stored-in-a-root-file/55109/2 "2023-05-31T10:36:07Z")

</div>

_GetMaximu_ **n**?

---

<div class="post-metadata">

**Author:** ![Natilus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@Natilus](https://root-forum.cern.ch/u/Natilus)\
**Post date:** [May 31, 2023, 10:40am UTC](https://root-forum.cern.ch/t/reading-a-histogram-from-a-canvas-stored-in-a-root-file/55109/3 "2023-05-31T10:40:49Z")

</div>

Hi @Wile_E_Coyote  
Sorry that was a typo, I meant GetMaximumBin().  
Other methods don’t work either like GetMaximum(), or GetBinContent()

To make it more clear, when I loop over the vector of histograms this problem happens. If I work with one, lets say “InputHistograms[0]” everything is good, but if I use “InputHistograms[i]” in a loop, I get the error message.

---

<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:** [May 31, 2023, 4:55pm UTC](https://root-forum.cern.ch/t/reading-a-histogram-from-a-canvas-stored-in-a-root-file/55109/4 "2023-05-31T16:55:56Z")

</div>

What “error message”?

BTW. Make sure all canvases contain what you expect: `InputCanvas->ls();`

---

<div class="post-metadata">

**Author:** ![Natilus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@Natilus](https://root-forum.cern.ch/u/Natilus)\
**Post date:** [June 1, 2023, 2:49pm UTC](https://root-forum.cern.ch/t/reading-a-histogram-from-a-canvas-stored-in-a-root-file/55109/5 "2023-06-01T14:49:22Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> `InputCanvas->ls()`

Hi @Wile_E_Coyote  
Thank you very much for your help.  
Indeed, part of the problem was the input canvases being totally empty.  
Also when looping over histograms, if I use  
for (int h=0; h\<InputHistograms.size(); i++)  
instead of  
for (auto h=InputHistograms.begin(); h!=InputHistograms.end(); h++)  
my code works fine.

Anyway, the problem is solved and the code works.  
Thank you again
