ROOT
master
Reference Guide
Loading...
Searching...
No Matches
rootalias.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_legacy
3
/// Defines aliases:
4
/// - `ls(path)`
5
/// - `edit(filename)`
6
/// - `dir(path)`
7
/// - `pwd()`
8
/// - `cd(path)`
9
///
10
/// \macro_code
11
///
12
/// \author Rene Brun
13
14
#include <
TSystem.h
>
15
#include <
TROOT.h
>
16
#include <
TCanvas.h
>
17
#include <
TPaveText.h
>
18
#include <
TText.h
>
19
20
#include <string>
21
#include <cstdlib>
// std::getenv
22
#include <cstring>
// std::strcmp
23
#include <algorithm>
// std::replace
24
#include <cstdio>
// std::printf
25
26
namespace
{
27
inline
bool
IsWindows
()
28
{
29
return
std::strcmp(
gSystem
->
GetName
(),
"WinNT"
) == 0;
30
}
31
inline
bool
IsMac
()
32
{
33
return
std::strcmp(
gSystem
->
GetName
(),
"Macosx"
) == 0;
34
}
35
36
// Minimal, pragmatic shell quoting for filenames/paths.
37
// Good enough for normal paths; not a full shell-escaping library.
38
std::string
QuoteForShell
(
const
std::string &s)
39
{
40
if
(s.empty())
41
return
"''"
;
42
43
if
(
IsWindows
()) {
44
// Wrap in double quotes; replace embedded " with ' (rare in paths)
45
std::string
q
= s;
46
std::replace(
q
.begin(),
q
.end(),
'"'
,
'\''
);
47
return
"\""
+
q
+
"\""
;
48
}
else
{
49
// POSIX: single-quote, escape internal single quotes with '"'"'
50
std::string out;
51
out.reserve(s.size() + 2);
52
out.push_back(
'\''
);
53
for
(
char
c
: s) {
54
if
(
c
==
'\''
)
55
out +=
"'\"'\"'"
;
56
else
57
out.push_back(
c
);
58
}
59
out.push_back(
'\''
);
60
return
out;
61
}
62
}
63
64
std::string
GetEnvOrEmpty
(
const
char
*
name
)
65
{
66
if
(
const
char
*
v
= std::getenv(
name
))
67
return
std::string(
v
);
68
return
{};
69
}
70
71
// Build a command that (on POSIX) returns immediately to keep the ROOT prompt usable.
72
std::string
MaybeBackground
(std::string
cmd
)
73
{
74
if
(!
IsWindows
())
75
cmd
+=
" &"
;
76
return
cmd
;
77
}
78
}
// namespace
79
80
//______________________________________________________________________________
81
// Open a file in the user's editor, with robust cross-platform fallbacks.
82
void
edit
(
const
char
*file)
83
{
84
const
std::string
f
= (file ? file :
""
);
85
const
std::string
qf
=
QuoteForShell
(
f
);
86
const
std::string
editor
=
GetEnvOrEmpty
(
"EDITOR"
);
87
88
std::string
cmd
;
89
90
if
(
IsWindows
()) {
91
// Use "start" to detach a new window (cmd.exe builtin).
92
if
(!
editor
.empty())
93
cmd
=
"start "
+
editor
+
" "
+
qf
;
94
else
95
cmd
=
"start notepad "
+
qf
;
96
}
else
if
(
IsMac
()) {
97
// macOS: prefer $EDITOR, else TextEdit
98
if
(!
editor
.empty())
99
cmd
=
MaybeBackground
(
editor
+
" "
+
qf
);
100
else
101
cmd
=
MaybeBackground
(
"open -e "
+
qf
);
102
}
else
{
103
// Linux/Unix: $EDITOR if set; else xdg-open; else xterm+vi
104
if
(!
editor
.empty())
105
cmd
=
MaybeBackground
(
editor
+
" "
+
qf
);
106
else
107
cmd
=
108
MaybeBackground
(
"(command -v xdg-open >/dev/null 2>&1 && xdg-open "
+
qf
+
") || (xterm -e vi "
+
qf
+
")"
);
109
}
110
111
gSystem
->
Exec
(
cmd
.c_str());
112
}
113
114
//______________________________________________________________________________
115
// List a directory in a compact, friendly way.
116
void
ls(
const
char
*path =
nullptr
)
117
{
118
std::string
cmd
=
IsWindows
() ?
"dir /w"
:
"ls"
;
119
if
(path && *path) {
120
cmd
+=
" "
;
121
cmd
+=
QuoteForShell
(path);
122
}
123
gSystem
->
Exec
(
cmd
.c_str());
124
}
125
126
//______________________________________________________________________________
127
// More verbose directory view (traditional Unix-y default).
128
void
dir(
const
char
*path =
nullptr
)
129
{
130
std::string
cmd
=
IsWindows
() ?
"dir"
:
"ls -alF"
;
131
if
(path && *path) {
132
cmd
+=
" "
;
133
cmd
+=
QuoteForShell
(path);
134
}
135
gSystem
->
Exec
(
cmd
.c_str());
136
}
137
138
//______________________________________________________________________________
139
// Return current working directory (keeps macro API stable: returns const char*).
140
const
char
*pwd()
141
{
142
static
std::string
wd
;
// static so c_str() stays valid after return
143
wd
=
gSystem
->
WorkingDirectory
();
144
return
wd
.c_str();
145
}
146
147
//______________________________________________________________________________
148
// Change directory; if no path is given, just report where we are.
149
const
char
*cd(
const
char
*path =
nullptr
)
150
{
151
if
(path && *path)
152
gSystem
->
ChangeDirectory
(path);
153
return
pwd();
154
}
155
156
// ===
157
// The following benchmark helper (seen in your file) is kept as-is in spirit,
158
// just minor cleanups for clarity. If you have more helpers in your local copy,
159
// you can apply the same style: std::string, const-correctness, early returns.
160
// ===
161
162
TCanvas
*
bench
=
nullptr
;
163
164
//______________________________________________________________________________
165
// Colorize a macro name in the summary before/after execution and run it.
166
void
bexec2
(
const
char
*
macro
)
167
{
168
std::printf(
"in bexec dir=%s\n"
, pwd());
169
if
(
gROOT
->IsBatch())
170
std::printf(
"Processing benchmark: %s\n"
,
macro
);
171
172
if
(!
bench
) {
173
// If bench isn't prepared yet, just run the macro.
174
gROOT
->Macro(
macro
);
175
return
;
176
}
177
178
auto
*
summary
=
dynamic_cast<
TPaveText
*
>
(
bench
->GetPrimitive(
"TPave"
));
179
if
(
summary
) {
180
if
(
auto
*
tmacro
=
summary
->GetLineWith(
macro
))
181
tmacro
->SetTextColor(4);
182
bench
->Modified();
183
bench
->Update();
184
}
185
186
gROOT
->Macro(
macro
);
187
188
auto
*
summary2
=
dynamic_cast<
TPaveText
*
>
(
bench
->GetPrimitive(
"TPave"
));
189
if
(
summary2
) {
190
if
(
auto
*
tmacro2
=
summary2
->GetLineWith(
macro
))
191
tmacro2
->SetTextColor(2);
192
bench
->Modified();
193
bench
->Update();
194
}
195
}
f
#define f(i)
Definition
RSha256.hxx:104
c
#define c(i)
Definition
RSha256.hxx:101
TCanvas.h
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:360
name
char name[80]
Definition
TGX11.cxx:148
q
float * q
Definition
THbookFile.cxx:89
TPaveText.h
TROOT.h
gROOT
#define gROOT
Definition
TROOT.h:417
TSystem.h
gSystem
R__EXTERN TSystem * gSystem
Definition
TSystem.h:582
TText.h
ROOT::Detail::TRangeCast
Definition
TCollection.h:313
TCanvas
The Canvas class.
Definition
TCanvas.h:23
TNamed::GetName
const char * GetName() const override
Returns name of object.
Definition
TNamed.h:49
TPaveText
A Pave (see TPave) with text, lines or/and boxes inside.
Definition
TPaveText.h:21
TSystem::Exec
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition
TSystem.cxx:655
TSystem::ChangeDirectory
virtual Bool_t ChangeDirectory(const char *path)
Change directory.
Definition
TSystem.cxx:876
TSystem::WorkingDirectory
virtual const char * WorkingDirectory()
Return working directory.
Definition
TSystem.cxx:885
v
@ v
Definition
rootcling_impl.cxx:3554
tutorials
legacy
rootalias.C
ROOTmaster - Reference Guide Generated on Wed Jun 24 2026 04:36:26 (GVA Time) using Doxygen 1.10.0