I have array of size 50 and want to draw 5 graphs on one canvas(10 points on each pad)

ROOT Version: ROOT 6.10/08
Platform: KATE
Compiler: LINUX


I have array of size 50, I want to draw 5 graphs on one canvas, with 10 points of array on each pad,
I am trying using Divide canvas but I am getting " *** Break *** segmentation violation"…There is a Crash.
Can somebody help in this???

Hi, would you please mind attaching your source file(s) to the post? “Segmentation fault” means your code is accessing a memory block that was not allocated by your program.

TCanvas *c15 = new TCanvas("c15","Higgs 4 point",200,10,600,400);
        c15->Divide(2,2);
        TMultiGraph *mg151 = new TMultiGraph();
        TMultiGraph *mg152 = new TMultiGraph();
        TMultiGraph *mg153 = new TMultiGraph();
        TMultiGraph *mg154 = new TMultiGraph();
        TMultiGraph *mg155 = new TMultiGraph();
        TLegend *legend151 = new TLegend(.75,.80,.95,.95);
        TLegend *legend152 = new TLegend(.75,.80,.95,.95);
        TLegend *legend153 = new TLegend(.75,.80,.95,.95);
        TLegend *legend154 = new TLegend(.75,.80,.95,.95);
        TLegend *legend155 = new TLegend(.75,.80,.95,.95);
double x[n];
double y1[n];
double ey1[n];
double y2[n];
double ey2[n];
double y3[n];
double ey3[n];
double y4[n];
double ey4[n];
double y5[n];
double ey5[n];
double totalarray[5*n];
double totalarrayer[5*n];
int clr,styl;
        clr=markercolor1[fl+1];
        styl=markerstyle1[fl];
        string hfl1;
        ifstream file1;
        hfl1=klink+kFile[fl]+slash+flhg4pfull;
        file1.open(hfl1.c_str());
        if(file1.is_open())
        {
            for(int i=0;i<(5*n);i++)
            {
                file1 >> totalarray[i];
            }
        }
        else if(!file1)
        {
            cout << "something is wrong with file " << endl;
        }
        file1.close();
        string hfl2;
        ifstream file2;
        hfl2=klink+kFile[fl]+slash+flhg4pfuller;
        file2.open(hfl2.c_str());
        if(file2.is_open())
        {
            for(int i=0;i<(5*n);i++)
            {
                file2 >> totalarrayer[i];
            }
        }
        else if(!file2)
        {
            cout << "something is wrong with file " << endl;
        }
        file2.close();
        for(int i=0;i<n;i++)
        {
            x[i]=static_cast<double>(i);
        }
        for(int i=0;i<(5*n);i++)
        {
                if(i<n)
                {
                    y1[i]=totalarray[i];
                    ey1[i]=totalarrayer[i];
                    
                }
                else if(n<=i&&i<(2*n))
                {
                    y2[i]=totalarray[i];
                    ey2[i]=totalarrayer[i];
                    
                }
                else if((2*n)<=i&&i<(3*n))
                {
                    y3[i]=totalarray[i];
                    ey3[i]=totalarrayer[i];
                    
                }
                else if((3*n)<=i&&i<(4*n))
                {
                    y4[i]=totalarray[i];
                    ey4[i]=totalarrayer[i];
                    
                }
                else if((4*n)<=i&&i<(5*n))
                {
                    y5[i]=totalarray[i];
                    ey5[i]=totalarrayer[i];
                    
                }
                
        }
        
        //**** TGraph1d with error bars ****
        TGraphErrors *gr1 = new TGraphErrors(n,x,y1,0,ey1);
        TGraphErrors *gr2 = new TGraphErrors(n,x,y2,0,ey2);
        TGraphErrors *gr3 = new TGraphErrors(n,x,y3,0,ey3);
        TGraphErrors *gr4 = new TGraphErrors(n,x,y4,0,ey4);
        TGraphErrors *gr5 = new TGraphErrors(n,x,y5,0,ey5);
         stringstream ss;
         ss << mh << coma << ms << coma << cpl1 << coma << cpl2 << coma << cpl3 << coma << cpl4 << coma << ncf;
         string str1 = ss.str();
        c15->cd(1);
        gr1->SetTitle("Higgs 4 point for smearing 0(non smear)");
        gr1->GetXaxis()->SetTitle("t-axis");
        gr1->GetYaxis()->SetTitle("higgs4pointsmear");
        gr1->SetMarkerColor(clr);
        gr1->SetMarkerStyle(styl);
        gr1->SetMarkerSize(1.1);
        mg151->Add(gr1);
        legend151->AddEntry(gr1,str1.c_str(),"p");
        mg151->Draw("AP");
        legend151->Draw();
        c15->cd(2);
        gr2->SetTitle("Higgs 4 pointfor smearing 1");
        gr2->GetXaxis()->SetTitle("t-axis");
        gr2->GetYaxis()->SetTitle("higgs4pointsmear");
        gr2->SetMarkerColor(clr);
        gr2->SetMarkerStyle(styl);
        gr2->SetMarkerSize(1.1);
        mg152->Add(gr2);
        legend152->AddEntry(gr2,str1.c_str(),"p");
        mg152->Draw("AP");
        legend152->Draw();
        c15->cd(3);
        gr3->SetTitle("Higgs 4 point for smearing 2");
        gr3->GetXaxis()->SetTitle("t-axis");
        gr3->GetYaxis()->SetTitle("higgs4pointsmear");
        gr3->SetMarkerColor(clr);
        gr3->SetMarkerStyle(styl);
        gr3->SetMarkerSize(1.1);
        mg153->Add(gr3);
        legend153->AddEntry(gr3,str1.c_str(),"p");
        mg153->Draw("AP");
        legend153->Draw();
        c15->cd(4);
        gr4->SetTitle("Higgs 4 point for smearing 3");
        gr4->GetXaxis()->SetTitle("t-axis");
        gr4->GetYaxis()->SetTitle("higgs4pointsmear");
        gr4->SetMarkerColor(clr);
        gr4->SetMarkerStyle(styl);
        gr4->SetMarkerSize(1.1);
        mg154->Add(gr4);
        legend154->AddEntry(gr4,str1.c_str(),"p");
        mg154->Draw("AP");
        legend154->Draw();
        c15->cd(5);
        gr5->SetTitle("Higgs 4 point for smearing 4");
        gr5->GetXaxis()->SetTitle("t-axis");
        gr5->GetYaxis()->SetTitle("higgs4pointsmear");
        gr5->SetMarkerColor(clr);
        gr5->SetMarkerStyle(styl);
        gr5->SetMarkerSize(1.1);
        mg155->Add(gr5);
        legend155->AddEntry(gr5,str1.c_str(),"p");
        mg155->Draw("AP");
        legend155->Draw();
}

I tried to run your code. It says error: use of undeclared identifier 'n'. Please provide a minimal complete example of your code with all the variables declared. Off the top of my head. You are dividing the canvas into 4 parts:

c15->Divide(2,2);

However later you are accessing pad number 5:

c15->cd(5);

This may be your issue. You have a total of 4 pads on the canvas. But accessing the pad number 5.

TCanvas *c15 = new TCanvas("c15","Higgs 4 point",200,10,600,400);
        c15->Divide(3,2);
        TMultiGraph *mg151 = new TMultiGraph();
        TMultiGraph *mg152 = new TMultiGraph();
        TMultiGraph *mg153 = new TMultiGraph();
        TMultiGraph *mg154 = new TMultiGraph();
        TMultiGraph *mg155 = new TMultiGraph();
        TLegend *legend151 = new TLegend(.75,.80,.95,.95);
        TLegend *legend152 = new TLegend(.75,.80,.95,.95);
        TLegend *legend153 = new TLegend(.75,.80,.95,.95);
        TLegend *legend154 = new TLegend(.75,.80,.95,.95);
        TLegend *legend155 = new TLegend(.75,.80,.95,.95);
int n;
n=10;
double x[n];
double y1[n];
double ey1[n];
double y2[n];
double ey2[n];
double y3[n];
double ey3[n];
double y4[n];
double ey4[n];
double y5[n];
double ey5[n];
double totalarray[5*n];
double totalarrayer[5*n];
            for(int i=0;i<(5*n);i++)
            {
                totalarray[i]=i;
                totalarrayer[i]=o.oo1*i;
            }
        for(int i=0;i<n;i++)
        {
            x[i]=static_cast<double>(i);
        }
        for(int i=0;i<(5*n);i++)
        {
                if(i<n)
                {
                    y1[i]=totalarray[i];
                    ey1[i]=totalarrayer[i];
                    
                }
                else if(n<=i&&i<(2*n))
                {
                    y2[i]=totalarray[i];
                    ey2[i]=totalarrayer[i];
                    
                }
                else if((2*n)<=i&&i<(3*n))
                {
                    y3[i]=totalarray[i];
                    ey3[i]=totalarrayer[i];
                    
                }
                else if((3*n)<=i&&i<(4*n))
                {
                    y4[i]=totalarray[i];
                    ey4[i]=totalarrayer[i];
                    
                }
                else if((4*n)<=i&&i<(5*n))
                {
                    y5[i]=totalarray[i];
                    ey5[i]=totalarrayer[i];
                    
                }
                
        }
        
        //**** TGraph1d with error bars ****
        TGraphErrors *gr1 = new TGraphErrors(n,x,y1,0,ey1);
        TGraphErrors *gr2 = new TGraphErrors(n,x,y2,0,ey2);
        TGraphErrors *gr3 = new TGraphErrors(n,x,y3,0,ey3);
        TGraphErrors *gr4 = new TGraphErrors(n,x,y4,0,ey4);
        TGraphErrors *gr5 = new TGraphErrors(n,x,y5,0,ey5);
        c15->cd(1);
        mg151->Add(gr1);
        mg151->Draw("AP");
        c15->cd(2);
        mg152->Add(gr2);
        mg152->Draw("AP");
        c15->cd(3);
        mg153->Add(gr3);
        mg153->Draw("AP");
        c15->cd(4);
        mg154->Add(gr4);
        mg154->Draw("AP");
        c15->cd(5);
        mg155->Add(gr5);
        mg155->Draw("AP");

I have updated my code.
Now you can run.

Still can’t run it. I get error: use of undeclared identifier 'o' totalarrayer[i]=o.oo1*i;. Please test your minimal code example before you post it on the thread.

It is a good idea to put your minimal example in a separate ROOT script. This way you can easily run and test it on your computer. You can then attach your ROOT script file with the “Upload” button on the toolbar instead of copy-pasting larger chunks of code in the comments.

Send us some example we can run please.

I have separated the minimal example of my code.

These are the files which i want to plot.flhg4pfuller.txt (600 Bytes)
flhg4pfull.txt (624 Bytes)

Following error i m receiving.

root [0]
Processing example.C…
Warning in TPad::ResizePad: c15_5 height changed from 64000 to 10

Error in TGaxis::PaintAxis: length of axis is 0
root [1] Warning in TPad::ResizePad: c15_5 height changed from 64000 to 10

Warning in TPad::ResizePad: c15_5 height changed from 64000 to 10

Warning in TPad::ResizePad: c15_5 height changed from 64000 to 10

Error in TGaxis::PaintAxis: length of axis is 0

In the “example.C”, add:

#include "TEnv.h"
#include "TCanvas.h"
#include "TMultiGraph.h"
#include "TLegend.h"
#include "TGraphErrors.h"
#include "TAxis.h"

Then fix all problems reported by:

[...]$ `root-config --cxx --cflags` -O2 -Wall -Wextra -c example.C
example.C: In function ‘void hg4pfplt1()’:
example.C:25:22: warning: unused variable ‘mg156’ [-Wunused-variable]
   25 |         TMultiGraph *mg156 = new TMultiGraph();
      |                      ^~~~~
example.C:31:18: warning: unused variable ‘legend156’ [-Wunused-variable]
   31 |         TLegend *legend156 = new TLegend(.75,.80,.95,.95);
      |                  ^~~~~~~~~
example.C:113:25: warning: array subscript 40 is above array bounds of ‘double [10]’ [-Warray-bounds]
  113 |                     y5[i]=totalarray[i];
      |                     ~~~~^
example.C:44:8: note: while referencing ‘y5’
   44 | double y5[n];
      |        ^~
example.C:114:26: warning: array subscript 40 is above array bounds of ‘double [10]’ [-Warray-bounds]
  114 |                     ey5[i]=totalarrayer[i];
      |                     ~~~~~^
example.C:45:8: note: while referencing ‘ey5’
   45 | double ey5[n];
      |        ^~~
example.C:108:25: warning: array subscript 30 is above array bounds of ‘double [10]’ [-Warray-bounds]
  108 |                     y4[i]=totalarray[i];
      |                     ~~~~^
example.C:42:8: note: while referencing ‘y4’
   42 | double y4[n];
      |        ^~
example.C:109:26: warning: array subscript 30 is above array bounds of ‘double [10]’ [-Warray-bounds]
  109 |                     ey4[i]=totalarrayer[i];
      |                     ~~~~~^
example.C:43:8: note: while referencing ‘ey4’
   43 | double ey4[n];
      |        ^~~
example.C:103:25: warning: array subscript 20 is above array bounds of ‘double [10]’ [-Warray-bounds]
  103 |                     y3[i]=totalarray[i];
      |                     ~~~~^
example.C:40:8: note: while referencing ‘y3’
   40 | double y3[n];
      |        ^~
example.C:104:26: warning: array subscript 20 is above array bounds of ‘double [10]’ [-Warray-bounds]
  104 |                     ey3[i]=totalarrayer[i];
      |                     ~~~~~^
example.C:41:8: note: while referencing ‘ey3’
   41 | double ey3[n];
      |        ^~~
example.C:98:25: warning: array subscript 10 is above array bounds of ‘double [10]’ [-Warray-bounds]
   98 |                     y2[i]=totalarray[i];
      |                     ~~~~^
example.C:38:8: note: while referencing ‘y2’
   38 | double y2[n];
      |        ^~
example.C:99:26: warning: array subscript 10 is above array bounds of ‘double [10]’ [-Warray-bounds]
   99 |                     ey2[i]=totalarrayer[i];
      |                     ~~~~~^
example.C:39:8: note: while referencing ‘ey2’
   39 | double ey2[n];
      |        ^~~

I have added all you have mentioned and removed the unused variable but still getting the same error.

[...]$ `root-config --cxx --cflags` -O2 -Wall -Wextra -c example.C
example.C: In function ‘void hg4pfplt1()’:
example.C:111:25: warning: array subscript 40 is above array bounds of ‘double [10]’ [-Warray-bounds]
  111 |                     y5[i]=totalarray[i];
      |                     ~~~~^
example.C:42:8: note: while referencing ‘y5’
   42 | double y5[n];
      |        ^~
example.C:112:26: warning: array subscript 40 is above array bounds of ‘double [10]’ [-Warray-bounds]
  112 |                     ey5[i]=totalarrayer[i];
      |                     ~~~~~^
example.C:43:8: note: while referencing ‘ey5’
   43 | double ey5[n];
      |        ^~~
example.C:106:25: warning: array subscript 30 is above array bounds of ‘double [10]’ [-Warray-bounds]
  106 |                     y4[i]=totalarray[i];
      |                     ~~~~^
example.C:40:8: note: while referencing ‘y4’
   40 | double y4[n];
      |        ^~
example.C:107:26: warning: array subscript 30 is above array bounds of ‘double [10]’ [-Warray-bounds]
  107 |                     ey4[i]=totalarrayer[i];
      |                     ~~~~~^
example.C:41:8: note: while referencing ‘ey4’
   41 | double ey4[n];
      |        ^~~
example.C:101:25: warning: array subscript 20 is above array bounds of ‘double [10]’ [-Warray-bounds]
  101 |                     y3[i]=totalarray[i];
      |                     ~~~~^
example.C:38:8: note: while referencing ‘y3’
   38 | double y3[n];
      |        ^~
example.C:102:26: warning: array subscript 20 is above array bounds of ‘double [10]’ [-Warray-bounds]
  102 |                     ey3[i]=totalarrayer[i];
      |                     ~~~~~^
example.C:39:8: note: while referencing ‘ey3’
   39 | double ey3[n];
      |        ^~~
example.C:96:25: warning: array subscript 10 is above array bounds of ‘double [10]’ [-Warray-bounds]
   96 |                     y2[i]=totalarray[i];
      |                     ~~~~^
example.C:36:8: note: while referencing ‘y2’
   36 | double y2[n];
      |        ^~
example.C:97:26: warning: array subscript 10 is above array bounds of ‘double [10]’ [-Warray-bounds]
   97 |                     ey2[i]=totalarrayer[i];
      |                     ~~~~~^
example.C:37:8: note: while referencing ‘ey2’
   37 | double ey2[n];
      |        ^~~

I am not getting these errors. May be you have other version of ROOT

or May be these errors are due to double type…which may be unable to store large numbers.

These are C++ bugs, independent of the ROOT version.
Newer compilers are better at “detecting” them (when compiling the source code).

So what you will suggest ?? How can I plot a file of 50 numbers on a canvas with 10 number on each pad of canvas??

Fix problems reported by the “clever” compiler.

Example (fixes reported problems in the lines 111 and 112):

                else if((n*4)<=i&&i<(n*5))
                {
                    y5[i-n*4]=totalarray[i];
                    ey5[i-n*4]=totalarrayer[i];
                }
1 Like