Fill a th2 independently (x,y)

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.

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);


  }

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 ?

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

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);
}

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

Did you mean:

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.

Thank you so much

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