Add TH2 with your settings and options for Add()

Hello,

I have 6 histograms and each has its own point settings. When I use the Add() function, all settings disappear. How to add them while saving your settings? I thought the solution is in the options of the function Add(), but did not find a list of options.

Code:

TFile *f0 = new TFile ("/home/slava/Data_Files/root/Var1/4009.root");
TFile *f1 = new TFile ("/home/slava/Data_Files/root/Var1/4210.root");
TFile *f2 = new TFile ("/home/slava/Data_Files/root/Var1/4230.root");
TFile *f3 = new TFile ("/home/slava/Data_Files/root/Var1/4260.root");
TFile *f4 = new TFile ("/home/slava/Data_Files/root/Var1/4360.root");
TFile *f5 = new TFile ("/home/slava/Data_Files/root/Var1/4470.root");
TFile *f6 = new TFile ("/home/slava/Data_Files/root/Var1/4600.root");

TH2D his4009 = (TH2D)f0->Get(“momentum_and_energy_p/Energy_point”);
TH2D his4210 = (TH2D)f1->Get(“momentum_and_energy_p/Energy_point”);
TH2D his4230 = (TH2D)f2->Get(“momentum_and_energy_p/Energy_point”);
TH2D his4260 = (TH2D)f3->Get(“momentum_and_energy_p/Energy_point”);
TH2D his4360 = (TH2D)f4->Get(“momentum_and_energy_p/Energy_point”);
TH2D his4470 = (TH2D)f5->Get(“momentum_and_energy_p/Energy_point”);
TH2D his4600 = (TH2D)f6->Get(“momentum_and_energy_p/Energy_point”);

his4009->SetMarkerStyle(21);
his4009->SetMarkerColor(2);

his4210->SetMarkerStyle(21);
his4210->SetMarkerColor(3);

his4230->SetMarkerStyle(21);
his4230->SetMarkerColor(4);

his4260->SetMarkerStyle(21);
his4260->SetMarkerColor(5);

his4360->SetMarkerStyle(21);
his4360->SetMarkerColor(6);

his4470->SetMarkerStyle(21);
his4470->SetMarkerColor(7);

his4600->SetMarkerStyle(21);
his4600->SetMarkerColor(8);

TH2D *Merger = new TH2D(“All_points”,“Momentum and energy;Momentum P_{p#bar{p}} - min P_{p#bar{p}}, GeV/c;Energy E_{p#bar{p}} - E_{point}, GeV/c^{2}”, 80, -0.04, 0.04, 80, -0.04 - 0.015, 0.04 - 0.015);

Merger->Add(his4009);
Merger->Add(his4210);
Merger->Add(his4230);
Merger->Add(his4260);
Merger->Add(his4360);
Merger->Add(his4470);
Merger->Add(his4600);


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


When you use TH2D::Add the data of each histogram is cumulated (added) to your resulting histogram “Merger”. You end up with one single histogram in which there is no memory about the histograms you added in. If you want to keep the memory of each histogram you should use THStack.

I added:

THStack *stack = new THStack(“stack”,“Momentum and energy”);

stack->Add(his4009);
stack->Add(his4210);
stack->Add(his4230);
stack->Add(his4260);
stack->Add(his4360);
stack->Add(his4470);
stack->Add(his4600);

stack->GetXaxis()->SetTitle(“Momentum P_{p#bar{p}} - min P_{p#bar{p}}, GeV/c”);
stack->GetYaxis()->SetTitle(“Energy E_{p#bar{p}} - E_{point}, GeV/c^{2}”);

, but getting:

warning: null passed to a callee that requires a non-null argument [-Wnonnull]
stack->GetXaxis()->SetTitle("Momentum P_{p#bar{p}} - min P_{p#bar{p}}…

I’m doing as in the example, I don’t understand what is wrong.

Try with:
THStack *stack = new THStack("stack", "Momentum and energy;Momentum P_{p#bar{p}} - min P_{p#bar{p}}, GeV/c;Energy E_{p#bar{p}} - E_{point}, GeV/c^{2}");

Yes, this solved the problem, although it is still interesting why it did not work after GetXaxis.

Another problem arose: for some reason, points began to appear that were not there. The first picture is what should be, the second is what is saved. The code is the same, just added

stack->Draw(“nostack”).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.