2 histograms in one canvas

hay
I have two histograms, which I obtain by reading data from the file output_file1.txt which is attached. Then I would like to plot both histograms in one canvas (but with different color). Unfortunately the histograms have a different scale. This can be seen if you read all the data points in one histogram (then you will see that the disks are in the middle of the halos). So one of the histograms does not follow the scale on the axis. Where is my mistake?
I run the program with

.x pos_plot.C

A second point which occurs in some of my root files is, that I can execute the program only once. If I try it again I get a segmentation fault. I have to leave root and start it again. Probably I wrote bad code and forgot anything, but what?
thanks for any help
best regards
florian



output_file1.txt (1.8 MB)
pos_plot.C (947 Bytes)

here is a small example showing how to draw 2 TGraph2D with the same scale on the same plot:

void graph2dsame2()
{
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(1111);

   TCanvas *c = new TCanvas("c","Graph2D example",0,0,800,800);

   TH2F *h1 = new TH2F("h1","h1",10,0,10,10,0,10);
   h1->SetDirectory(0);
   h1->SetMinimum(0);
   h1->SetMaximum(10);

   Double_t x1[4] = {1,1,2,2};
   Double_t y1[4] = {1,2,1,2};
   Double_t z1[4] = {1,2,3,4};

   TGraph2D *g2d1 = new TGraph2D(4,x1,y1,z1);
   g2d1->SetHistogram(h1);
   g2d1->Draw("P0 TRI1");

   TH2F *h2 = new TH2F("h2","h2",10,0,10,10,0,10);
   h2->SetDirectory(0);
   h2->SetMinimum(0);
   h2->SetMaximum(10);

   Double_t x2[4] = {5,5,6,6};
   Double_t y2[4] = {5,6,5,6};
   Double_t z2[4] = {5,6,7,8};

   TGraph2D *g2d2 = new TGraph2D(4,x2,y2,z2);
   g2d2->SetHistogram(h2);
   g2d2->Draw("P0 TRI1 SAME");
}

hay, thanks for the answer…
Is this the easiest method? Because I would like to define the axis automatically by the data. A scale from 0 to 10 is not very helpful. Of cause I can read in the maximum value of the data and use this for the histograms, but I fear the histogram max and min values have to be integer? I would like to be independent of histograms and stay with TGrapg2D
I implemented your solution, and it is working, but only with some constrains and I fear it is not portable to similar problems.
Is there a solution which just tells the second Graph to follow the scale of the first? (Actually I thought is is default, so maybe I made a mistake somewhere???)
best regards
florian

in the line:

   TH2F *h1 = new TH2F("h1","h1",10,0,10,10,0,10); 

only the number of bin is integer. the other parameter are double and you can pout the value you want. see:http://root.cern.ch/root/html/TH2.html

hay
I would like to have only points “P” in the plot like show in the picture above. In your example you use TRI1 which gives a surface. If I delete TRI1 and use only “P” or “P0” as draw option the axis of the histogram are not plotted???
my modified program looks at the moment like this

#include <math.h>

void pos_plot(){
   gROOT->SetStyle("Plain");
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(1111);

   const int num = 1000000;
   double x[num], y[num], z[num], type[num], dummy;
   double xmax, ymax, zmax;
   int n = 0;
   int n2 = 0;

   xmax = 0;
   ymax = 0;
   zmax = 0;

   ifstream widthfile("output_file1.txt");
   while((widthfile >> x[n] >> y[n] >> z[n] >> type[n])){
      if(xmax < x[n]) xmax = x[n];
      if(ymax < y[n]) ymax = y[n];
      if(zmax < z[n]) zmax = z[n];
      n++;
   }  
   widthfile.close();

   cout<<"xmax: "<<xmax<< endl;
   cout<<"ymax: "<<ymax<< endl;
   cout<<"zmax: "<<zmax<< endl;

   TCanvas *c1 = new TCanvas("c1", "pos_plot", 0, 0, 750, 600);    

   TH2F *h1 = new TH2F("h1","h1",10,-ymax,ymax,10,-zmax,zmax);
   h1->SetDirectory(0);
   h1->SetMinimum(-xmax);
   h1->SetMaximum(xmax);

   h1->SetTitle("");

   TGraph2D *g2d1 = new TGraph2D(100,x,y,z);
   g2d1->SetHistogram(h1);
   g2d1->Draw("P");

   TH2F *h2 = new TH2F("h2","h2",10,-ymax,ymax,10,-zmax,zmax);
   h2->SetDirectory(0);
   h2->SetMinimum(-xmax);
   h2->SetMaximum(xmax);

   TGraph2D *g2d2 = new TGraph2D(2,x,y,z);
   g2d2->SetHistogram(h2);
   g2d2->Draw("P SAME");
}

thank you so much for helping
best regards
florian

Yes I see the problem I have no answer yet. Meanwhile I can propose you a other solution. As it seems you need a could of points it does not seems really necessary to use TGraph2D . 3D polymarkers are enough. The following example does what you want:

{
   gStyle->SetOptStat(0);

   TH3F *frame3d = new TH3F("frame3d","frame3d",10,-1,1,10,-1,1,10,-1,1);
   frame3d->Draw();

   TPolyMarker3D *pm3d1 = new TPolyMarker3D(3);
   pm3d1->SetPoint(0,1,0,0);
   pm3d1->SetPoint(1,0,1,0);
   pm3d1->SetPoint(2,0,0,1);

   pm3d1->SetMarkerSize(2);
   pm3d1->SetMarkerColor(kBlue);
   pm3d1->SetMarkerStyle(20);

   pm3d1->Draw();

   TPolyMarker3D *pm3d2 = new TPolyMarker3D(3);
   pm3d2->SetPoint(0,-1,0,0);
   pm3d2->SetPoint(1,0,-1,0);
   pm3d2->SetPoint(2,0,0,-1);

   pm3d2->SetMarkerSize(2);
   pm3d2->SetMarkerColor(kRed);
   pm3d2->SetMarkerStyle(20);

   pm3d2->Draw();
}

thanks a lot… thats exactly what I needed…
best regards
florian

Greetings,

I am sorry for posting this question here. I am a newbie in both c++ and root and I have limited time unfortunately.

I read your peoblem, it’s the same that I want to do. But I already have my .root histograms taken from G4 and AIDA. The output of AIDA is .root.

Nevertheless I have open these two histo in one canvas to compare them. Just want to ask if I should write a c++ code for that or if I can do it easily from the browser?

many thanks in advance

If you want to do what is describe here use the c++ program I posted.
You sent too few details to help you further.

I have three (.root) files including histograms.

I want to put all these three histogtams in one canvas in order to comapre these three histograms.

Thank you aagin

Hi again,

I felt better to include my files as well.
any of them includes 3 histograms. In this case I want to compare all number 2s with each other.

Thank you again
essai3.root (9.13 KB)
essai2.root (9.74 KB)
essai1.root (9.78 KB)

see my answer here: