Painting a TCanvas to the screen in a compiled ROOT (CERN) application

浪子不回头ぞ 提交于 2019-12-01 04:12:20

问题


What are the rules for painting to the screen?

My end goal is to put the TCanvas into a class and paint from there, but for now I think that maybe looking at a less complicated example might help. Below is some code that compiles and paints to the screen, on my computer.

# include <TApplication.h>
# include <TCanvas.h>
# include <TH1D.h>
# include <thread>
# include <chrono>

//TCanvas canvas ("fCanvas", "fCanvas", 600, 400);

int main ( int argc, char* argv[] )
{
    TApplication app ("app",&argc,argv);

    TCanvas canvas ("fCanvas", "fCanvas", 600, 400);
    //TCanvas* canvas = new TCanvas("fCanvas", "fCanvas", 600, 400);

    TH1D h ("h","h",10,0,10);
    h.Fill(1);
    h.Fill(2);
    h.Fill(2);
    h.Fill(2);
    h.Fill(3);
    h.Fill(3);
    h.Draw();

    canvas.Update();
    canvas.Draw();

    std::this_thread::sleep_for( std::chrono::seconds(3) );

    return 0;
}

You may notice some commented-out lines. If I use either of those definitions of canvas, using the appropriate member access operators on the later-called Update and Draw methods, the application crashes after printing a blank TCanvas-window to the screen. It also crashes if I change app and h to pointers.

If I try to instantiate a class using any sort of ROOT object at all, it crashes the application.

Right now, I'm compiling with MSVC++'s cl.exe and linking with link.exe. I'm working on a 64-bit Windows 7 Enterprise N. I'm trying to port an application that I built in Unix, for which a simple new TApplication("name",0,0); at the start of main made everything work.

So, to reiterate: how can I get my histograms onto the screen in this OS, and maybe others? I doubt that I'll be able to understand the "why", but it might be nice to write something about that for others reading this who can. Otherwise, just a step-by-step description of how to use these objects to paint would be wonderful.

Many thanks for any help on this; I'll gladly provide more information / examples if that should prove useful.


Update: it works in my particular case if I compile with something like

cl -nologo -DWIN32 -W3 -D_WINDOWS -Z7 -MDd -GR -EHsc main.cxx -I %ROOTSYS%\include -FIw32pragma.h /link -debug -LIBPATH:%ROOTSYS%\lib libCore.lib libRIO.lib libHist.lib libGpad.lib

Not sure why.

See https://root.cern.ch/phpBB3/viewtopic.php?f=3&t=3402&p=85329&hilit=Vector+stl+of+TH1F*+Objects#p85329 .


回答1:


I usually use TApplications like below to have TCanvases really appear as window on the screen.

#include "TApplication.h"
// other stuff

int main(int argc, char** argv) {
  TApplication theApp("App",&argc, argv);
  // your code
  // here you can Draw() things
  theApp.Run();
  return 0;
}

The program then just stops at Run() and I end the process with ^C.



来源:https://stackoverflow.com/questions/30932725/painting-a-tcanvas-to-the-screen-in-a-compiled-root-cern-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!