-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[main] replace rootbrowse.py with C++ version #19722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd91a22
[main] Create C++ version of rootbrowse
silverweed 797cf93
[main] Remove rootbrowse.py
silverweed 1a0fefe
[main] rootbrowse: order includes, extract short help
silverweed 99d004f
[rootbrowse] better option parsing, exit if in batch mode
silverweed b717f19
[rootbrowse] Change doc URL to latest-stable
silverweed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// \file rootbrowse.cxx | ||
/// | ||
/// Command line tool to open a ROOT file on a TBrowser | ||
/// | ||
/// \author Giacomo Parolini <[email protected]> | ||
/// \date 2025-08-21 | ||
#include <ROOT/RLogger.hxx> | ||
|
||
#include <TApplication.h> | ||
#include <TBrowser.h> | ||
#include <TError.h> | ||
#include <TFile.h> | ||
#include <TGFrame.h> | ||
#include <TROOT.h> | ||
#include <TSystem.h> | ||
|
||
#include <chrono> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <memory> | ||
#include <thread> | ||
#include <string_view> | ||
|
||
static const char *const kShortHelp = "usage: rootbrowse [-w WEB|-wf] <file.root>\n"; | ||
static const char *const kLongHelp = R"( | ||
Open a ROOT file in a TBrowser | ||
|
||
positional arguments: | ||
FILE Input file | ||
|
||
options: | ||
-h, --help show this help message and exit | ||
-w, --web WEB Configure webdisplay. For all possible values, see TROOT::SetWebDisplay(): | ||
https://root.cern/doc/latest-stable/classTROOT.html#a1749472696545b76a6b8e79769e7e773 | ||
-wf, --webOff Invoke the classic TBrowser (not the web version) | ||
|
||
Examples: | ||
- rootbrowse | ||
Open a TBrowser | ||
|
||
- rootbrowse file.root | ||
Open the ROOT file 'file.root' in a TBrowser | ||
)"; | ||
|
||
static ROOT::RLogChannel &RootBrowseLog() | ||
{ | ||
static ROOT::RLogChannel channel("RootBrowse"); | ||
return channel; | ||
} | ||
|
||
struct RootBrowseArgs { | ||
enum class EPrintUsage { | ||
kNo, | ||
kShort, | ||
kLong | ||
}; | ||
EPrintUsage fPrintHelp = EPrintUsage::kNo; | ||
std::string_view fWeb; | ||
std::string_view fFileName; | ||
}; | ||
|
||
static RootBrowseArgs ParseArgs(const char **args, int nArgs) | ||
{ | ||
RootBrowseArgs outArgs; | ||
bool forcePositional = false; | ||
|
||
for (int i = 0; i < nArgs; ++i) { | ||
const char *arg = args[i]; | ||
|
||
if (strcmp(arg, "--") == 0) { | ||
forcePositional = true; | ||
continue; | ||
} | ||
|
||
bool isFlag = !forcePositional && arg[0] == '-'; | ||
if (isFlag) { | ||
++arg; | ||
// Parse long or short flag and its argument into `argStr` / `nxtArgStr`. | ||
std::string_view argStr, nxtArgStr; | ||
if (arg[0] == '-') { | ||
++arg; | ||
// long flag: may be either of the form `--web off` or `--web=off` | ||
const char *eq = strchr(arg, '='); | ||
if (eq) { | ||
argStr = std::string_view(arg, eq - arg); | ||
nxtArgStr = std::string_view(eq + 1); | ||
} else { | ||
argStr = std::string_view(arg); | ||
if (i < nArgs - 1 && args[i + 1][0] != '-') { | ||
nxtArgStr = args[i + 1]; | ||
++i; | ||
} | ||
} | ||
} else { | ||
// short flag (note that it might be more than 1 character long, like `-wf`) | ||
argStr = std::string_view(arg); | ||
if (i < nArgs - 1 && args[i + 1][0] != '-') { | ||
nxtArgStr = args[i + 1]; | ||
++i; | ||
} | ||
} | ||
|
||
if (argStr == "w" || argStr == "web") { | ||
outArgs.fWeb = nxtArgStr.empty() ? "on" : nxtArgStr; | ||
} else if (argStr == "h" || argStr == "help") { | ||
outArgs.fPrintHelp = RootBrowseArgs::EPrintUsage::kLong; | ||
break; | ||
} else if (argStr == "wf" || argStr == "--webOff") { | ||
outArgs.fWeb = "off"; | ||
} | ||
|
||
} else if (!outArgs.fFileName.empty()) { | ||
outArgs.fPrintHelp = RootBrowseArgs::EPrintUsage::kShort; | ||
break; | ||
} else { | ||
outArgs.fFileName = arg; | ||
} | ||
} | ||
|
||
return outArgs; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
auto args = ParseArgs(const_cast<const char **>(argv) + 1, argc - 1); | ||
if (args.fPrintHelp != RootBrowseArgs::EPrintUsage::kNo) { | ||
std::cerr << kShortHelp; | ||
if (args.fPrintHelp == RootBrowseArgs::EPrintUsage::kLong) { | ||
std::cerr << kLongHelp; | ||
return 0; | ||
} | ||
return 1; | ||
} | ||
|
||
// NOTE: we need to instantiate TApplication ourselves, otherwise TBrowser | ||
// will create a batch application that cannot show graphics. | ||
TApplication app("rootbrowse", nullptr, nullptr); | ||
|
||
if (!args.fWeb.empty()) | ||
gROOT->SetWebDisplay(std::string(args.fWeb).c_str()); | ||
|
||
std::unique_ptr<TFile> file; | ||
if (!args.fFileName.empty()) { | ||
gErrorIgnoreLevel = kError; | ||
file = std::unique_ptr<TFile>(TFile::Open(std::string(args.fFileName).c_str(), "READ")); | ||
if (!file || file->IsZombie()) { | ||
R__LOG_WARNING(RootBrowseLog()) << "File " << args.fFileName << " does not exist or is unreadable."; | ||
} | ||
gErrorIgnoreLevel = kUnset; | ||
} | ||
|
||
auto browser = std::make_unique<TBrowser>(); | ||
|
||
if (gROOT->IsBatch()) | ||
return 1; | ||
|
||
// For classic graphics: ensure rootbrowse quits when the window is closed | ||
if (auto imp = browser->GetBrowserImp()) { | ||
if (auto mainframe = imp->GetMainFrame()) { | ||
mainframe->Connect("CloseWindow()", "TApplication", &app, "Terminate()"); | ||
} | ||
} | ||
|
||
std::cout << "Press ctrl+c to exit.\n"; | ||
while (!gROOT->IsInterrupted() && !gSystem->ProcessEvents()) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
silverweed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.