Hystograms root

Hi, two questions please;

  1. I wrote this code in ROOT:
	TFile f("H:/cors/cors_plot.root");
	f.ls();
	TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400);
	hngam->SetLineColor(kBlack);
	hngam->Draw();
	c1->Update();
	hnele->SetLineColor(kRed);
	hnele->Draw("same");
	c1->Update();
	hnmuo->SetLineColor(kGreen);
	hnmuo->Draw("same");
	c1->Update();
	hnhad->SetLineColor(kYellow);
	hnhad->Draw("same");
	c1->Update();
	TLegend* leg = new TLegend(0.2, 0.2, .8, .8);
	leg-> SetNColumns(1);
 	leg->AddEntry(hngam, "Fotoni", "l");
	leg->AddEntry(hnele, "Elettroni/Positroni", "l");
	leg->AddEntry(hnmuo, "Muoni", "l");
	leg->AddEntry(hnhad, "Adroni", "l");
	leg->Draw("same");
	c1->Update;

having this plot

as you can see, the statistical box plot just shows the information about the first hystogram (hngam), how can I change the code to have the informations about all the hystograms?

  1. I wrote this code
#include "TCanvas.h"
#include "TStyle.h"
#include "TH1.h"
#include "TGaxis.h"
#include "TRandom.h"

void macro_n_part()
{
	TFile f("H:/cors/cors_plot.root");
	f.ls();
	TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400);
	hngam->SetLineColor(kBlack);
	hngam->Draw();
	c1->Update();
	hnele->SetLineColor(kRed);
	hnele->Draw("same");
	c1->Update();
	hnmuo->SetLineColor(kGreen);
	hnmuo->Draw("same");
	c1->Update();
	hnhad->SetLineColor(kYellow);
	hnhad->Draw("same");
	c1->Update();
	TLegend* leg = new TLegend(0.2, 0.2, .8, .8);
	leg-> SetNColumns(1);
 	leg->AddEntry(hngam, "Fotoni", "l");
	leg->AddEntry(hnele, "Elettroni/Positroni", "l");
	leg->AddEntry(hnmuo, "Muoni", "l");
	leg->AddEntry(hnhad, "Adroni", "l");
	leg->Draw("same");
	c1->Update;
}

and I saved it in the macro_n_part.c file and I used it as a macro, but running it I have this plot

but as you can see, it is the same code that I wrote in ROOT (having the other plot), so why doesn’t it works saving it in a macro? Where am I wrong?

Thanks

Hi,

For the stats box issue, see for example the $(ROOTSYS)/tutorials/graphs/multigraph.C or $(ROOTSYS)/tutorials/hist/transpad.C tutorials

Cheers, Bertrand.

Hi Bellenot and thanks. I wrote a new macro using the transpad example (see the attachment) but unfortunatly running the macro I’ve this:

how can I resolve?
thanks

-------------------EDIT----------------------
I need to save the plots too in pdf files, and to make more plots running the same macro, so I tried to make this too, but running the macro I have the 2 plots

and

and it saves the 2 pdf files, but with the warnings

hnsecondari.c (2.53 KB)

Hi,

Please provide also cors_plot.root

Cheers, Bertrand.

[quote=“bellenot”]Hi,

Please provide also cors_plot.root

Cheers, Bertrand.[/quote]

Hi Bellenote, this is the root file.
cors_plot.root (1.16 MB)

Here is the solution for the macro_n_part macro:

[code]#include “TFile.h”
#include “TCanvas.h”
#include “TStyle.h”
#include “TH1.h”
#include “TGaxis.h”
#include “TRandom.h”
#include “TLegend.h”
#include “TPaveStats.h”

void macro_n_part()
{
TFile *f = TFile::Open(“cors_plot.root”);
if (f == 0) {
// if we cannot open the file, print an error message and return immediatly
printf(“Error: cannot open cors_plot.root!\n”);
return;
}
f->ls();
// get a pointer to the histogram
TH1F *hngam = (TH1F *)f->Get(“hngam”);
TH1F *hnele = (TH1F *)f->Get(“hnele”);
TH1F *hnmuo = (TH1F *)f->Get(“hnmuo”);
TH1F *hnhad = (TH1F *)f->Get(“hnhad”);
if ((hngam == 0) || (hnele == 0) || (hnmuo == 0) || (hnhad == 0)){
printf(“Error getting an histogram from the file!\n”);
return;
}
TCanvas c1 = new TCanvas(“c1”,“hists with different scales”,600,400);
c1->SetLogx();
c1->SetLogy();
hngam->SetLineColor(kBlack);
hngam->Draw();
c1->Update();
hnele->SetLineColor(kRed);
// use “sames” option to also draw the stats box
hnele->Draw(“sames”);
c1->Update();
hnmuo->SetLineColor(kGreen);
hnmuo->Draw(“sames”);
c1->Update();
hnhad->SetLineColor(kYellow);
hnhad->Draw(“sames”);
c1->Update();
TLegend
leg = new TLegend(0.2, 0.7, .4, .85);
leg->SetNColumns(1);
leg->AddEntry(hngam, “Fotoni”, “l”);
leg->AddEntry(hnele, “Elettroni/Positroni”, “l”);
leg->AddEntry(hnmuo, “Muoni”, “l”);
leg->AddEntry(hnhad, “Adroni”, “l”);
leg->Draw(“same”);
// now retrieve each stats box and reposition them
TPaveStats stats1 = (TPaveStats)hngam->GetListOfFunctions()->FindObject(“stats”);
TPaveStats stats2 = (TPaveStats)hnele->GetListOfFunctions()->FindObject(“stats”);
TPaveStats stats3 = (TPaveStats)hnmuo->GetListOfFunctions()->FindObject(“stats”);
TPaveStats stats4 = (TPaveStats)hnhad->GetListOfFunctions()->FindObject(“stats”);
stats1->SetTextColor(kBlack);
stats2->SetTextColor(kRed);
stats3->SetTextColor(kGreen);
stats4->SetTextColor(kYellow);
stats1->SetX1NDC(0.80); stats1->SetX2NDC(0.98);
stats1->SetY1NDC(0.77); stats1->SetY2NDC(0.92);
stats2->SetX1NDC(0.80); stats2->SetX2NDC(0.98);
stats2->SetY1NDC(0.60); stats2->SetY2NDC(0.75);
stats3->SetX1NDC(0.80); stats3->SetX2NDC(0.98);
stats3->SetY1NDC(0.43); stats3->SetY2NDC(0.58);
stats4->SetX1NDC(0.80); stats4->SetX2NDC(0.98);
stats4->SetY1NDC(0.26); stats4->SetY2NDC(0.41);
c1->Update();
}
[/code]
It gives:

Cheers, Bertrand.

Hi bellenot! Thanks! It works as I wanted! I’ve just two questions more about this macro (not very important),

  1. I added the code
gROOT->ProcessLine("gROOT->SetBatch()");
   c1.Print("File.pdf");
   gROOT->ProcessLine("gROOT->SetBatch(kFALSE)");

to have the .pdf files of the plot, the macro works very well and I’ve the pdf file, but wih the warnings

the lines 34,90,108,etc where there are the warnings are the line

   cn.Print("NameFile.pdf");

as I wrote it creates the pdf files, but why is there this warning?

  1. I copied your code more times (Changin the name of TCanvas, c1, c2, c3, etc) so I wrote a macro to have all the plots that I need just running this macro. Running the macro it opens all the TCanvas so when the run stops I’ve to close them; but as I wrote I added the code to print pdf files, so I don’t need to open all the pop ups of the canvas, is there a code to sto opening of the canvas so I’ve not close them everytime?

Thanks

cn->Print(“NameFile.pdf”); delete cn;

Thanks pepe, I added the code

and now it close alone the TCanvas so I’ve not to close them at the end of the run!