# Fill a th2 independently (x,y)

**URL:** <https://root-forum.cern.ch/t/fill-a-th2-independently-x-y/45136>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [May 28, 2021, 12:32pm UTC](https://root-forum.cern.ch/t/fill-a-th2-independently-x-y/45136 "2021-05-28T12:32:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![IKoro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/3e96dc/32.png) [@IKoro](https://root-forum.cern.ch/u/IKoro)\
**Post date:** [May 28, 2021, 12:32pm UTC](https://root-forum.cern.ch/t/fill-a-th2-independently-x-y/45136/1 "2021-05-28T12:32:39Z")

</div>

Hello,  
I would like to plot a TH2D with in x the load for a certain “om” and in y the load with another “om”.  
The problem is that I don’t know how to fill only the x part of TH2 and the y part independently.

```auto
void OMcompare()
{
	//gStyle->SetOptStat(0);
	//gStyle->SetOptFit(1111);

	TCanvas *c1 = new TCanvas();

	TFile *input = new TFile("/home/dupuy/Documents/code_dupuy/Exercices/fichier root/histo_Li_system_568.root","READ");

	TTree *tree = (TTree*)input->Get("Result_tree;3");

	int om_number;
	double amplitude_tree,charge_tree, time;

	tree->SetBranchAddress("amplitude_tree", &amplitude_tree);
	tree->SetBranchAddress("charge_tree", &charge_tree);
	tree->SetBranchAddress("om_number", &om_number);
	tree->SetBranchAddress("time", &time);

	int entries = tree->GetEntries();

  cout << entries << endl;
	double constante;
	constante = 100;

	TH2D *hist1 = new TH2D("hist", "amplitude1 en fonction du temps",1000, 0, 300, 1000, 0, 300);

  for(int i=0; i < entries; i++) // rentre les valeurs de la branche dans notre variable
      {
        tree->GetEntry(i);

				if (om_number == 301)
				{
        hist1->Fill(amplitude_tree);
			  }
				if (om_number == 302)
				{
        hist1->Fill(amplitude_tree);
			  }
      }

  hist1->Draw("");

	hist1->GetXaxis()->SetTitle("amplitude 301");
	hist1->GetYaxis()->SetTitle("amplitude 302");
	hist1->GetXaxis()->SetTitleSize(0.05);
	hist1->GetYaxis()->SetTitleSize(0.05);
	hist1->GetXaxis()->SetLabelSize(0.05);
	hist1->GetYaxis()->SetLabelSize(0.05);

  }

```

---

<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:** [May 28, 2021, 12:54pm UTC](https://root-forum.cern.ch/t/fill-a-th2-independently-x-y/45136/2 "2021-05-28T12:54:48Z")

</div>

`TH2D::Fill` has two values (at least) as input: The X and Y position of the bin you want to fill. The third parameter is the weight which can be omitted, in that case it will be equal to 1. Therefore something like `hist1->Fill(amplitude_tree);` is wrong if `hist1` is a `TH2D`. To plot a tree variable against and other, you can for instance do:  
`tree->Draw("y:x");` but may be that’s not what you are looking for ?

---

<div class="post-metadata">

**Author:** ![IKoro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/3e96dc/32.png) [@IKoro](https://root-forum.cern.ch/u/IKoro)\
**Post date:** [May 28, 2021, 12:57pm UTC](https://root-forum.cern.ch/t/fill-a-th2-independently-x-y/45136/3 "2021-05-28T12:57:49Z")

</div>

actually it was not my idea since the problem I am having is that charge\_tree has the same name for both so it will just draw a line for me, maybe this program will allow you to better understand what I am doing. try to do

```auto
for(int i=0; i < entries; i++) // rentre les valeurs de la branche dans notre variable
{
   tree->GetEntry(i);

   if (om_number == 301)
   {
      cout << amplitude_tree << endl;
      amplitude_tree = amplitude301;
      cout << amplitude301 << endl;
   }
   if (om_number == 302)
   {
      amplitude_tree = amplitude302;
   }

   hist1->Fill(amplitude301,amplitude302);
}

```

---

<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:** [May 28, 2021, 1:17pm UTC](https://root-forum.cern.ch/t/fill-a-th2-independently-x-y/45136/4 "2021-05-28T13:17:35Z")

</div>

The way you wrote it the two “if” are useless.

Did you mean:

```auto
for (int i=0; i < entries; i++) {
   tree->GetEntry(i);
   if (om_number == 301) amplitude301 = amplitude_tree;
   if (om_number == 302) amplitude302 = amplitude_tree;
   hist1->Fill(amplitude301,amplitude302);
}

```

If that’s the case you need a bit more logic before calling `Fill` because in one iteration of the loop you define either `amplitude301` or `amplitude302` but not both.

---

<div class="post-metadata">

**Author:** ![IKoro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/3e96dc/32.png) [@IKoro](https://root-forum.cern.ch/u/IKoro)\
**Post date:** [May 28, 2021, 1:54pm UTC](https://root-forum.cern.ch/t/fill-a-th2-independently-x-y/45136/5 "2021-05-28T13:54:01Z")

</div>

Thank you so much

---

<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:** [May 28, 2021, 2:23pm UTC](https://root-forum.cern.ch/t/fill-a-th2-independently-x-y/45136/6 "2021-05-28T14:23:51Z")

</div>

You’re welcome. Let me know if you need more help.
