Standalone TBrowser

Hi, I’m trying to create a standalone TBrowser. That is, I could type tbrowser at the command line and the program pops up. When I click close, it closes. With this it would be possible to double-click on a ROOT file and have it automatically open in TBrowser.

This is a little more difficult than I thought though. The main problem is getting the program to close as TBrowser won’t close a TApplication by default. Here’s my code so far, trying to get it to work. Advice would be much appreciated.

// KRootBrowser.h

#include <TQObject.h>
#include <RQ_OBJECT.h>
#include <TRootBrowser.h>

class KRootBrowser : public TRootBrowser {

public:
KRootBrowser();
virtual ~KRootBrowser();
void CloseWindow();

ClassDef(KRootBrowser,0) //ROOT native GUI version of browser
};

// --------------------------------------------------------------------

// KRootBrowser.cpp

#include <TApplication.h>
#include <TGClient.h>
#include <TBrowser.h>
#include “KRootBrowser.h”

KRootBrowser::KRootBrowser()
:TRootBrowser(){}
KRootBrowser::~KRootBrowser(){}

void KRootBrowser::CloseWindow()
{
TRootBrowser::DeleteWindow();
gApplication->Terminate(0);
}

int main(int argc, char **argv)
{

    TApplication app("App", &argc, argv);
    KRootBrowser *kb = new KRootBrowser();
    TBrowser *tb = kb->Browser();

    app.Run();
    return 0;

}

//=============================================

//KLinkDef.h
#pragma link C++ class KRootBrowser;

//=============================================

Compile with :
rootcint -f KDict.cpp -c KRootBrowser.h KLinkDef.h
g++ -Wall -g KRootBrowser.cpp KDict.cpp root-config --cflags --glibs -o tbrowser

Right, I’ve decided. Sod c++ I hate it anyway.

If you’ve pyroot set up, it’s easy. If not, set up pyroot like this:

bash: export PYTHONPATH=$PYTHONPATH:/path/to/root/lib
bash: setenv PYTHONPATH $PYTHONPATH:/path/to/root/lib

So here’s my quick and dirty tbrowser script:

#!/usr/bin/env python

from ROOT import TBrowser, TFile
import time, sys

f=None
if len(sys.argv)>1 :
        f=TFile(sys.argv[1])

tb = TBrowser()
while tb :
        time.sleep(7)

if f :
        f.Close()

Hi,

another very easy way is to have a macro startbrowser.C:
void startbrowser() { new TBrowser(_file0->GetName(), _file0); }
and then (for windows) edit the file associations such that root files are opened by root -l “%1” whereverThatScriptIs\startbrowser.C Obviously, that’s platform independent, and also works for KDE etc.

Axel.

Yeah, but that leaves you with a stagnant root session. You need something that will quit root when you click close. Otherwise you’ve loads of memory just sitting there.