# Plotting several graphs on divided canvas using compiled macro

**URL:** <https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902>\
**Category:** Newbie\
**Tags:** graphics\
**Created:** [June 28, 2019, 11:45am UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902 "2019-06-28T11:45:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![angad](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/angad/32/24245_2.png) [@angad](https://root-forum.cern.ch/u/angad)\
**Post date:** [June 28, 2019, 11:45am UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/1 "2019-06-28T11:45:13Z")

</div>

* * *

\_ROOT Version: v6-18-00  
\_Platform: Ubuntu 18.04  
\_Compiler: g++

Hi everyone,

I am trying to plot four graphs on a divided TCanvas in a compiled macro.

The first case is a simple example showing what I would like to plot, while the plot from the second case shows my problem. I attached two figures showing the output.  
For some reason the histograms in mainPad(1) and resPad(1) are plotted on top of eachother, while the histogram in resPad(2) is in the space of resPad(1).

Could you please help me find out what the problem is?

Cheers,  
Andreas

[case2.pdf](https://root-forum.cern.ch/uploads/default/original/3X/c/e/cef9bd93ce54e301e2add74dfdf7186add43b0d1.pdf) (15.8 KB)  
[case1.pdf](https://root-forum.cern.ch/uploads/default/original/3X/2/1/21de8e9fa861d55c6cff1410731c79281414b72e.pdf) (16.2 KB)

```auto
#include <iostream>
#include <fstream>
#include <string>
#include <TMath.h>
#include <TH1.h>
#include <TCanvas.h>
#include <mutex>
#include <memory>
#include <TGraph.h>
#include <TApplication.h>
#include <TSystem.h>
#include <TROOT.h>
#include <thread>
#include <unistd.h>
#include <iostream>
#include <readline/readline.h>
#include <readline/history.h>
#include <functional>

using namespace std;
using namespace ROOT;

class RootWrapper {

public:
    RootWrapper() : app("-", &_dummy, nullptr), shouldRun(false)
    {
        canvas.Divide(1,2);
        mainPanel = canvas.cd(1);
        mainPanel->SetLogy(true);
        residualPanel = canvas.cd(2);
    }

    void run() {
        shouldRun = true;
        while (shouldRun) {
            auto l = lock();

            usleep(100);

            gSystem->ProcessEvents();
        }
    }

    void requestStop() {
        shouldRun = false;
    }

    void update() {
        canvas.Update();
    }

    std::unique_lock<std::recursive_mutex> lock() {
        return std::unique_lock<std::recursive_mutex>{mutex};
    }

    void switchToMainPad(int n = 0) {
        auto _ = lock();
        mainPanel->cd(n);
    }

    void switchToResPad(int n = 0) {
        auto _ = lock();
        residualPanel->cd(n);
    }

    void split(int n) {
        auto _ = lock();
        residualPanel->Divide(n, 1);
        mainPanel->Divide(n, 1);
        for (int i = 1; i <= n; i++) mainPanel->cd(i)->SetLogy(true);
    }

    void manipulateGui(std::function<void()> f) {
        auto _ = lock();
        f();
        update();
    }

private:
    atomic_bool shouldRun;

    int _dummy = 0;
    std::recursive_mutex mutex;
    TApplication app;
    TCanvas canvas;
    TVirtualPad *mainPanel, *residualPanel;
};

int main(int argc, char *argv[]) {
    int n = 2;
    auto hist1 = new TH1F("h1", "h1", 10, 0, 100);
    hist1->Fill(5);
    auto hist2 = new TH1F("h2", "h2", 10, 0, 10);
    hist2->Fill(5);

    if(stoi(argv[1]) == 1) {

        // Test 1 - Working.

        int _dummy = 0;
        auto app = new TApplication("-", &_dummy, nullptr);
        auto canvas = new TCanvas("test1", "test1");
        canvas->Divide(1, 2);

        auto mainPad = canvas->cd(1);
        auto resPad = canvas->cd(2);

        mainPad->Divide(n, 1);
        resPad->Divide(n, 1);

        for (int i = 1; i <= n; ++i) {
            hist1->SetLineColor(i);
            hist2->SetLineColor(i);
            mainPad->cd(i);
            hist1->DrawClone();

            resPad->cd(i);
            hist2->DrawClone();
        }

        canvas->Modified();
        canvas->Update();
        app->Run();

    } else {

        // Test 2 - Is not working.
        RootWrapper gui;

        thread rootTread([&]() {
            gui.run();
        });

        gui.split(n);
        for (int i = 1; i <= n; ++i) {
            hist1->SetLineColor(i);
            hist2->SetLineColor(i);

            gui.manipulateGui([&]() {
                gui.switchToMainPad(i);
                hist1->DrawClone();
            });

            gui.manipulateGui([&]() {
                gui.switchToResPad(i);
                hist2->DrawClone();
            });
        }

        while (true) {

            string cmd;
            cin >> cmd;
            if (cmd == "x") break;
            else cout << "Unknown command " << cmd << endl;
        }
        gui.requestStop();
        rootTread.join();
    }

    return 0;
 }

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 28, 2019, 12:11pm UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/2 "2019-06-28T12:11:45Z")

</div>

could it be there is a `cd` missing ?  
Can you manipulate interactively the plot to see where the missing plot went ?

---

<div class="post-metadata">

**Author:** ![angad](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/angad/32/24245_2.png) [@angad](https://root-forum.cern.ch/u/angad)\
**Post date:** [June 28, 2019, 12:15pm UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/3 "2019-06-28T12:15:33Z")

</div>

The plots on mainPad(1) and resPad(1) are on top of eachother in the pdf i attached, but when I move it around they move together.

Also, I just found out that it works fine using root v6-08-00

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 28, 2019, 12:28pm UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/4 "2019-06-28T12:28:33Z")

</div>

Ok. I will check on my machine.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 28, 2019, 12:41pm UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/5 "2019-06-28T12:41:28Z")

</div>

for info i get two warnings when compiling your code:

```auto
angad.cxx:28:21: warning: field 'app' will be initialized after field 'shouldRun' [-Wreorder]
    RootWrapper() : app("-", &_dummy, nullptr), shouldRun(false)
                    ^
angad.cxx:99:14: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char *argv[]) {
             ^
2 warnings generated.

```

---

<div class="post-metadata">

**Author:** ![angad](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/angad/32/24245_2.png) [@angad](https://root-forum.cern.ch/u/angad)\
**Post date:** [June 28, 2019, 1:39pm UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/6 "2019-06-28T13:39:17Z")

</div>

Fixed the warnings:  
The first case can be run by giving the input 1, otherwise the second case will run.

EDIT : Fixed a bug.

```auto
#include <iostream>
#include <fstream>
#include <string>
#include <TMath.h>
#include <TH1.h>
#include <TCanvas.h>
#include <mutex>
#include <memory>
#include <TGraph.h>
#include <TApplication.h>
#include <TSystem.h>
#include <TROOT.h>
#include <thread>
#include <unistd.h>
#include <iostream>
#include <readline/readline.h>
#include <readline/history.h>
#include <functional>

Int_t *num;

using namespace std;
using namespace ROOT;

class RootWrapper {

public:
    RootWrapper() : app("-",num,nullptr)
    {
        canvas.Divide(1,2);
        mainPanel = canvas.cd(1);
        mainPanel->SetLogy(true);
        residualPanel = canvas.cd(2);
    }

    void run() {
        shouldRun = true;
        while (shouldRun) {
            auto l = lock();

            usleep(100);

            gSystem->ProcessEvents();
        }
    }

    void requestStop() {
        shouldRun = false;
    }

    void update() {
        canvas.Update();
    }

    std::unique_lock<std::recursive_mutex> lock() {
        return std::unique_lock<std::recursive_mutex>{mutex};
    }

    void switchToMainPad(int n = 0) {
        auto _ = lock();
        mainPanel->cd(n);
    }

    void switchToResPad(int n = 0) {
        auto _ = lock();
        residualPanel->cd(n);
    }

    void split(int n) {
        auto _ = lock();
        residualPanel->Divide(n, 1);
        mainPanel->Divide(n, 1);
        for (int i = 1; i <= n; i++) mainPanel->cd(i)->SetLogy(true);
    }

    void manipulateGui(std::function<void()> f) {
        auto _ = lock();
        f();
        update();
    }

private:
    TApplication app;
    atomic_bool shouldRun;

    std::recursive_mutex mutex;
    TCanvas canvas;
    TVirtualPad *mainPanel, *residualPanel;
};

int main(int argc, char *argv[]) {
    //(void) argc;
    int n = 2;
    auto hist1 = new TH1F("h1", "h1", 10, 0, 100);
    hist1->Fill(5);
    auto hist2 = new TH1F("h2", "h2", 10, 0, 10);
    hist2->Fill(5);

    if(argc>1) {
        if(stoi(argv[1])==1) {

            // Test 1 - Working.
            auto app = new TApplication("-",num,nullptr);
            auto canvas = new TCanvas("test1", "test1");
            canvas->Divide(1, 2);

            auto mainPad = canvas->cd(1);
            auto resPad = canvas->cd(2);

            mainPad->Divide(n, 1);
            resPad->Divide(n, 1);

            for (int i = 1; i <= n; ++i) {
                hist1->SetLineColor(i);
                hist2->SetLineColor(i);
                mainPad->cd(i);
                hist1->DrawClone();

                resPad->cd(i);
                hist2->DrawClone();
            }

            canvas->Modified();
            canvas->Update();
            app->Run();

    	    return 0;
        }
    }

    // Test 2 - Is not working.
    RootWrapper gui;

    thread rootTread([&]() {
        gui.run();
    });

    gui.split(n);
    for (int i = 1; i <= n; ++i) {
        hist1->SetLineColor(i);
        hist2->SetLineColor(i);

        gui.manipulateGui([&]() {
            gui.switchToMainPad(i);
            hist1->DrawClone();
        });

        gui.manipulateGui([&]() {
            gui.switchToResPad(i);
            hist2->DrawClone();
        });
    }
    while (true) {

        string cmd;
        cin >> cmd;
        if (cmd == "x") break;
        else cout << "Unknown command " << cmd << endl;
    }
    gui.requestStop();
    rootTread.join();
    return 0;
 }

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 28, 2019, 2:35pm UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/7 "2019-06-28T14:35:35Z")

</div>

When I run your program on Mac without parameter I get:

```auto
$ ./angad  

 ***Break*** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[<unknown binary>] (no debug info)
[/Users/couet/roottest/./angad] main (no debug info)
[/usr/lib/system/libdyld.dylib] start (no debug info)

```

When I run it with input parameter 1 I get a white canvas. I can see that it is in fact several pad on top of each other. I guess I will need to try your program on some Linux machine

---

<div class="post-metadata">

**Author:** ![angad](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/angad/32/24245_2.png) [@angad](https://root-forum.cern.ch/u/angad)\
**Post date:** [July 1, 2019, 7:33am UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/8 "2019-07-01T07:33:19Z")

</div>

I’m sorry, there was a bug when giving an empty argument. This is now fixed, and I updated the code snippet above, so it should be working now.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 1, 2019, 7:48am UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/9 "2019-07-01T07:48:53Z")

</div>

i still get a crash when i run the macro without parameter on mac.  
Also a warning (but that’s a detail)

```auto
angad.cxx:90:9: warning: private field '_dummy' is not used [-Wunused-private-field]
    int _dummy = 0;
        ^

```

---

<div class="post-metadata">

**Author:** ![angad](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/angad/32/24245_2.png) [@angad](https://root-forum.cern.ch/u/angad)\
**Post date:** [July 1, 2019, 7:57am UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/10 "2019-07-01T07:57:16Z")

</div>

I’m sorry about the errors. Its runs fine on my pc.  
I fixed it again and updated the code above.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 15, 2019, 7:57am UTC](https://root-forum.cern.ch/t/plotting-several-graphs-on-divided-canvas-using-compiled-macro/34902/11 "2019-07-15T07:57:18Z")

</div>

Closed on July 15, 2019, 7:57am UTC

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