Drawing Multipad in ROOT Canvas with different graphs contained in each TPad and plotting different legends in each TPad

here i am getting error while plotting 3 TPad in a single canvas and each TPad should contain two different graphs and legends … Is that the code is correct mentiones here?

void MultipdTest2( )
{ 

  gROOT->SetStyle("Plain");
  gStyle->SetOptStat(0);
  TCanvas *c1 = new TCanvas("c1","multipads", 800,2000);
  c1->GetFrame()->SetBorderSize(12);
  gStyle->SetOptTitle(0);

   //TGraph *g[10];
   //Double_t x[10] = {0,1,2,3,4,5,6,7,8,9};
   //Double_t y[10] = {1,2,3,4,5,6,4,3,2,1};
   //TMultiGraph *mg = new TMultiGraph();
   //for (int i=1; i<=3; i++)
   //{
     switch (int l=i) 
     { 
       case 1: TPad *p1 = new TPad("p1", "p1", 0.2, 0.1, 0.8, 0.5, 0, 0, 0);
               p1->SetTopMargin(0);
               p1->Draw();
               //mg->Add(g[i]);

               auto leg = new TLegend(0.88,.14,.6,0.3);
               leg->SetHeader("Graph1","C"); // option "C" allows to center the header
               leg->AddEntry(c1,"x1","p");
               leg->AddEntry(hr,"y1","l");
               hr->GetListOfFunctions()->Add(leg);
               hr->Draw();
               //First graph
               const int N1 = 7;
               double x1[N1]  = {1,2,3,4,5,6,7};
               double y1[N1]  = {1,2,4,8,16,32};
               TGraph  *gr1=new TGraph(N1,x1,y1);
               gr1->SetLineWidth(3);
               gr1->SetLineColor(4);
               gr1->Draw("C");
               //const  int j=i+1;
               //Second graph
               const int N2 = 13;
               float x2[N2]  = {10,11,12,13,14,15,16,17,18,19,20,21,22};
               float y2[N2]  = {10,20,40,80,160,320,350,380,410,440,470,500,530};
               float yer[N2] = {3,4.5,5.8,6.6,7.1,8.8,9.5,10.1,9.6,12.5,13,15.9,12.3};
               TGraphErrors *gr2 = new TGraphErrors(N2,x2,y2,nullptr,yer);
               gr2->Draw("P");
               gr2->SetMarkerColor(1);
               gr2->SetMarkerStyle(20);
               gr2->SetMarkerSize(1.5); 
               break; 
       case 2: TPad *p2 = new TPad("p2", "p2", 0.2, 0.1, 0.8, 0.5, 0, 0, 0);
               p2->SetTopMargin(0);
               p2->SetBottomMargin(0);
               p2->Draw();
               //mg->Add(g[i]);
               auto leg = new TLegend(0.88,.14,.6,0.3);
               leg->SetHeader("Graph2","C"); // option "C" allows to center the header
               leg->AddEntry(c1,"x2","p");
               leg->AddEntry(hr,"y2","l");
               hr->GetListOfFunctions()->Add(leg);
               hr->Draw();

               //First graph
               const int N1 = 7;
               double x1[N1]  = {13,15,20,40,45,48,50};
               double y1[N1]  = {15,113,120,125,135,145,150};
               TGraph  *gr1=new TGraph(N1,x1,y1);
               gr1->SetLineWidth(3);
               gr1->SetLineColor(4);
               gr1->Draw("C");
               const  int j=i+1;
               //Second graph
               const int N2 = 14;
               float x2[N2]  = {32,33,35,36,37,40,42,43,44,45,47,48,49,50};
               float y2[N2]  = {38,70,71,98,125,130,135,140,145,147,150,152,155,157,165};
               float yer[N2] = {10.0,19.2,5.3,5.4,7.5,8.5,8.9,10.0,9.1,11.7,19.1,9.4,10.2,8.0,11.5};
               TGraphErrors *gr2 = new TGraphErrors(N2,x2,y2,nullptr,yer);
               gr2->Draw("P");
               gr2->SetMarkerColor(1);
               gr2->SetMarkerStyle(20);
               gr2->SetMarkerSize(1.5);
               break; 
       case 3: TPad *p3 = new TPad("p3", "p3", 0.2, 0.1, 0.8, 0.5, 0, 0, 0);
               p3->SetTopMargin(0);
               p3->SetBottomMargin(0);
               p3->Draw(); 

              
               //mg->Add(g[i]);
               auto leg = new TLegend(0.88,.14,.6,0.3);
               leg->SetHeader("Graph2","C"); // option "C" allows to center the header
               leg->AddEntry(c1,"x3","p");
               leg->AddEntry(hr,"y3","l");
               hr->GetListOfFunctions()->Add(leg);
               hr->Draw();
               //First graph
               const int N1 = 8;
               double x1[N1] = {30,33,36,39,42,45,48,51};
               double y1[N1] ={10,36,90,130,150,161,165,169};
               TGraph  *gr1=new TGraph(N1,x1,y1);
               gr1->SetLineWidth(3);
               gr1->SetLineColor(4);
               gr1->Draw("C");
               const  int j=i+1; 
               //Second graph
               const int N2 = 14;
               float x2[N2] = {3,4,5.5,6.8,8,9,9.2,10.3,12.7,14,15,16.2,17.2,18.1,19.1};
               float y2[N2] = {34,51,75,95,111,122,133,144,155,166,177,188,199,210};
               float yer[N2] = {2.1,3.2,4.5,6.1,7.6,9.5,9.1,9.2,8.8,9.9,9.2,10.9,10.4,11.3};
               TGraphErrors *gr2 = new TGraphErrors(N2,x2,y2,nullptr,yer);
               gr2->Draw("P");
               gr2->SetMarkerColor(1);
               gr2->SetMarkerStyle(20);
               gr2->SetMarkerSize(1.5)
               break; 
       }  
     
  // }
   //mg->Draw("AL");
   c1->Update();
   c1->Print("image.jpeg");

Can you first fix the basic C++ mistakes in your code please ?

Processing MultipdTest2.C...
In file included from input_line_15:1:
/Users/couet/roottest/MultipdTest2.C:16:20: error: use of undeclared identifier 'i'
     switch (int l=i)
                   ^
/Users/couet/roottest/MultipdTest2.C:26:30: error: use of undeclared identifier 'hr'
               leg->AddEntry(hr,"y1","l");
                             ^
/Users/couet/roottest/MultipdTest2.C:27:16: error: use of undeclared identifier 'hr'
               hr->GetListOfFunctions()->Add(leg);
               ^
/Users/couet/roottest/MultipdTest2.C:28:16: error: use of undeclared identifier 'hr'
               hr->Draw();
               ^
/Users/couet/roottest/MultipdTest2.C:54:21: error: redefinition of 'leg'
               auto leg = new TLegend(0.88,.14,.6,0.3);
                    ^
/Users/couet/roottest/MultipdTest2.C:23:21: note: previous definition is here
               auto leg = new TLegend(0.88,.14,.6,0.3);
                    ^
/Users/couet/roottest/MultipdTest2.C:57:30: error: use of undeclared identifier 'hr'
               leg->AddEntry(hr,"y2","l");
                             ^
/Users/couet/roottest/MultipdTest2.C:58:16: error: use of undeclared identifier 'hr'
               hr->GetListOfFunctions()->Add(leg);
               ^
/Users/couet/roottest/MultipdTest2.C:59:16: error: use of undeclared identifier 'hr'
               hr->Draw();
               ^
/Users/couet/roottest/MultipdTest2.C:62:26: error: redefinition of 'N1'
               const int N1 = 7;
                         ^
/Users/couet/roottest/MultipdTest2.C:30:26: note: previous definition is here
               const int N1 = 7;
                         ^
/Users/couet/roottest/MultipdTest2.C:63:23: error: redefinition of 'x1'
               double x1[N1]  = {13,15,20,40,45,48,50};
                      ^
/Users/couet/roottest/MultipdTest2.C:31:23: note: previous definition is here
               double x1[N1]  = {1,2,3,4,5,6,7};
                      ^
/Users/couet/roottest/MultipdTest2.C:64:23: error: redefinition of 'y1'
               double y1[N1]  = {15,113,120,125,135,145,150};
                      ^
/Users/couet/roottest/MultipdTest2.C:32:23: note: previous definition is here
               double y1[N1]  = {1,2,4,8,16,32};
                      ^
/Users/couet/roottest/MultipdTest2.C:65:25: error: redefinition of 'gr1'
               TGraph  *gr1=new TGraph(N1,x1,y1);
                        ^
/Users/couet/roottest/MultipdTest2.C:33:25: note: previous definition is here
               TGraph  *gr1=new TGraph(N1,x1,y1);
                        ^
/Users/couet/roottest/MultipdTest2.C:69:29: error: use of undeclared identifier 'i'
               const  int j=i+1;
                            ^
/Users/couet/roottest/MultipdTest2.C:71:26: error: redefinition of 'N2'
               const int N2 = 14;
                         ^
/Users/couet/roottest/MultipdTest2.C:39:26: note: previous definition is here
               const int N2 = 13;
                         ^
/Users/couet/roottest/MultipdTest2.C:72:22: error: redefinition of 'x2'
               float x2[N2]  = {32,33,35,36,37,40,42,43,44,45,47,48,49,50};
                     ^
/Users/couet/roottest/MultipdTest2.C:40:22: note: previous definition is here
               float x2[N2]  = {10,11,12,13,14,15,16,17,18,19,20,21,22};
                     ^
/Users/couet/roottest/MultipdTest2.C:73:22: error: redefinition of 'y2'
               float y2[N2]  = {38,70,71,98,125,130,135,140,145,147,150,152,155,157,165};
                     ^
/Users/couet/roottest/MultipdTest2.C:41:22: note: previous definition is here
               float y2[N2]  = {10,20,40,80,160,320,350,380,410,440,470,500,530};
                     ^
/Users/couet/roottest/MultipdTest2.C:74:22: error: redefinition of 'yer'
               float yer[N2] = {10.0,19.2,5.3,5.4,7.5,8.5,8.9,10.0,9.1,11.7,19.1,9.4,10.2,8.0,11.5};
                     ^
/Users/couet/roottest/MultipdTest2.C:42:22: note: previous definition is here
               float yer[N2] = {3,4.5,5.8,6.6,7.1,8.8,9.5,10.1,9.6,12.5,13,15.9,12.3};
                     ^
/Users/couet/roottest/MultipdTest2.C:75:30: error: redefinition of 'gr2'
               TGraphErrors *gr2 = new TGraphErrors(N2,x2,y2,nullptr,yer);
                             ^
/Users/couet/roottest/MultipdTest2.C:43:30: note: previous definition is here
               TGraphErrors *gr2 = new TGraphErrors(N2,x2,y2,nullptr,yer);
                             ^
/Users/couet/roottest/MultipdTest2.C:88:21: error: redefinition of 'leg'
               auto leg = new TLegend(0.88,.14,.6,0.3);
                    ^
/Users/couet/roottest/MultipdTest2.C:23:21: note: previous definition is here
               auto leg = new TLegend(0.88,.14,.6,0.3);
                    ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]

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