Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
rootbrowse.cxx
Go to the documentation of this file.
1// \file rootbrowse.cxx
2///
3/// Command line tool to open a ROOT file on a TBrowser
4///
5/// \author Giacomo Parolini <giacomo.parolini@cern.ch>
6/// \date 2025-08-21
7#include <ROOT/RLogger.hxx>
8
9#include "logging.hxx"
10#include "optparse.hxx"
11
12#include <TRint.h>
13#include <TBrowser.h>
14#include <TError.h>
15#include <TFile.h>
16#include <TGFrame.h>
17#include <TROOT.h>
18#include <TSystem.h>
19
20#include <chrono>
21#include <cstring>
22#include <iostream>
23#include <memory>
24#include <thread>
25#include <string_view>
26
27static const char *const kShortHelp = "usage: rootbrowse [-w WEB|-wf] <file.root>\n";
28static const char *const kLongHelp = R"(
29Open a ROOT file in a TBrowser
30
31positional arguments:
32 FILE Input file
33
34options:
35 -h, --help show this help message and exit
36 -w, --web WEB Configure webdisplay. For all possible values, see TROOT::SetWebDisplay():
37 https://root.cern/doc/latest-stable/classTROOT.html#a1749472696545b76a6b8e79769e7e773
38 -wf, --webOff Invoke the classic TBrowser (not the web version)
39
40Examples:
41- rootbrowse
42 Open a TBrowser
43
44- rootbrowse file.root
45 Open the ROOT file 'file.root' in a TBrowser
46)";
47
58
59static RootBrowseArgs ParseArgs(const char **args, int nArgs)
60{
61 RootBrowseArgs outArgs;
63 opts.AddFlag({"-h", "--help"});
65 opts.AddFlag({"-wf", "--webOff"});
66
67 opts.Parse(args, nArgs);
68
69 if (opts.ReportErrors()) {
71 return outArgs;
72 }
73
74 if (opts.GetSwitch("help")) {
76 return outArgs;
77 }
78
79 if (auto web = opts.GetFlagValue("web"); !web.empty())
80 outArgs.fWeb = web;
81
82 if (opts.GetSwitch("webOff"))
83 outArgs.fWeb = "off";
84
85 if (opts.GetArgs().empty())
87 else
88 outArgs.fFileName = opts.GetArgs()[0];
89
90 return outArgs;
91}
92
93int main(int argc, char **argv)
94{
95 InitLog("rootbrowse");
96
97 auto args = ParseArgs(const_cast<const char **>(argv) + 1, argc - 1);
98 if (args.fPrintHelp != RootBrowseArgs::EPrintUsage::kNo) {
99 std::cerr << kShortHelp;
100 if (args.fPrintHelp == RootBrowseArgs::EPrintUsage::kLong) {
101 std::cerr << kLongHelp;
102 return 0;
103 }
104 return 1;
105 }
106
107 // NOTE: we need to instantiate TApplication ourselves, otherwise TBrowser
108 // will create a batch application that cannot show graphics.
109 TRint app("rootbrowse", nullptr, nullptr);
110
111 if (!args.fWeb.empty())
112 gROOT->SetWebDisplay(std::string(args.fWeb).c_str());
113
114 std::unique_ptr<TFile> file;
115 if (!args.fFileName.empty()) {
117 file = std::unique_ptr<TFile>(TFile::Open(std::string(args.fFileName).c_str(), "READ"));
118 if (!file || file->IsZombie()) {
119 Err() << "File " << args.fFileName << " does not exist or is unreadable.\n";
120 return 1;
121 }
123 }
124
125 auto browser = std::make_unique<TBrowser>();
126
127 if (gROOT->IsBatch())
128 return 1;
129
130 // For classic graphics: ensure rootbrowse quits when the window is closed
131 if (auto imp = browser->GetBrowserImp()) {
132 if (auto mainframe = imp->GetMainFrame()) {
133 mainframe->Connect("CloseWindow()", "TRint", &app, "Terminate()");
134 }
135 }
136
137 std::cout << ".q to exit.\n";
138 while (!gROOT->IsInterrupted() && !gSystem->ProcessEvents()) {
139 std::this_thread::sleep_for(std::chrono::milliseconds(10));
140 }
141
142 return 0;
143}
constexpr Int_t kError
Definition TError.h:47
externInt_t gErrorIgnoreLevel
errors with level below this value will be ignored. Default is kUnset.
Definition TError.h:140
constexpr Int_t kUnset
Definition TError.h:43
#define gROOT
Definition TROOT.h:417
externTSystem * gSystem
Definition TSystem.h:582
void AddFlag(std::initializer_list< std::string_view > aliases, EFlagType type=EFlagType::kSwitch, std::string_view help="", std::uint32_t flagOpts=0)
Defines a new flag (either a switch or a flag with argument).
Definition optparse.hxx:208
int GetSwitch(std::string_view name) const
If name refers to a previously-defined switch (i.e.
Definition optparse.hxx:300
const std::vector< std::string > & GetArgs() const
Retrieves all positional arguments.
Definition optparse.hxx:182
std::string_view GetFlagValue(std::string_view name) const
If name refers to a previously-defined non-switch flag, gets its value.
Definition optparse.hxx:321
void Parse(const char **args, std::size_t nArgs)
Definition optparse.hxx:425
bool ReportErrors(std::ostream &stream=std::cerr) const
Conveniency method to print any errors to stream.
Definition optparse.hxx:188
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3788
Definition TRint.h:31
void file()
Definition file.C:11
int main()
void InitLog(const char *name, int defaultVerbosity=1)
Definition logging.hxx:39
std::ostream & Err()
Definition logging.hxx:55
static const char *const kShortHelp
Command line tool to open a ROOT file on a TBrowser.
static RootBrowseArgs ParseArgs(const char **args, int nArgs)
static const char *const kLongHelp
EPrintUsage fPrintHelp
std::string fWeb
std::string fFileName