Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
xRooBrowser.cxx
Go to the documentation of this file.
1/*
2 * Project: xRooFit
3 * Author:
4 * Will Buttinger, RAL 2022
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13/** \class ROOT::Experimental::XRooFit::xRooBrowser
14\ingroup xroofit
15
16 \image html RooBrowser.png width=50%
17
18 \brief A version of the TBrowser that can be used to interact with RooFit models and datasets.
19
20 Also available under the ROOT::Experimental::RooBrowser alias.
21
22To get started with the xRooBrowser, open any ROOT file containing a workspace
23 and then create an instance of the xRooBrowser just like
24 creating an instance of a `TBrowser`. A window will be displayed with a navigable
25 tree structure on the left that lets you explore the content of the workspaces
26 present in the loaded file. Note that additional files, <b>including json workspace files</b>,
27 can be loaded through the `Browser --> Open` menu in the top left corner.
28
29The context menu for each node (access by right clicking on the node) in the tree structure can be used to get more
30information about the node. In particular, the `Draw` command can be selected on many of the nodes that are part of a
31statistical model, which will visualize that part of the model in the browser window. A number of options are available
32for the `Draw` command, including (some options can be combined):
33
34 - "e" : calculate and visualize propagated model uncertainty
35 - "auxratio" : Draw a ratio auxiliary plot below the main plot
36 - "auxsignif" : Draw a significance auxiliary plot below the main plot
37 - "pull" : show panel of current parameter values, which can be dragged in order to change the values and visualize the
38effect on the model (very experimental feature).
39
40 Once a node has been drawn, the styling of subsequent draws can be controlled through `TStyle` objects
41 that will now appear in the `objects` folder in the workspace.
42
43A model can be fit to a dataset from the workspace using the `fitTo` context menu command and specifying
44 the name of a dataset in the workspace (if no name is given, an expected dataset corresponding to the
45 current state of the model will be used). A dialog will display the fit result status code when the
46 fit completes and then a `fits` folder will be found under the workspace (the workspace may need to
47 be collapsed and re-expanded to make it appear) where the fit result can be found, selected, and visualized.
48 In multi-channel models the channels that are included in the fit can be controlled with the checkboxes
49 in the browser. Clicking the checkbox will cycle through three states: checked, unchecked with
50 grey-underline, and checked with grey-underline. The grey-underline indicates that channel wont be
51 included in the fit (and will appear greyed out when the model is visualized)
52
53Many more features are available in the xRooBrowser, and further documentation and development can be found at
54 the <a href="https://gitlab.cern.ch/will/xroofit">xRooFit</a> repository, which is the library where the browser has
55 been originally developed. The author (Will Buttinger) is also very happy to be contacted with questions or
56 feedback about this new functionality.
57
58 */
59
60#include "xRooFit/xRooBrowser.h"
61#include "xRooFit/xRooNode.h"
62
63#include "TSystem.h"
64#include "TKey.h"
65#include "TROOT.h"
66#include "TEnv.h"
67#include "TFile.h"
68#include "RooWorkspace.h"
69#include "TRootBrowser.h"
70#include "TGMenu.h"
71#include "TGFileDialog.h"
72#include "TObjString.h"
73
74#define GETPOPUPMENU(b, m) \
75 reinterpret_cast<TGPopupMenu *>( \
76 *reinterpret_cast<void **>(reinterpret_cast<unsigned char *>(b) + b->Class()->GetDataMemberOffset(#m)))
77
79
80xRooBrowser::xRooBrowser(xRooNode *o) : TBrowser("RooBrowser", o, "RooFit Browser"), fTopNode(o)
81{
82
83 fNode = std::shared_ptr<xRooNode>(fTopNode.get(), [](xRooNode *) {});
84
85 if (fTopNode) {
86 fTopNode->fBrowseOperation = [](xRooNode *in) {
87 for (auto file : *gROOT->GetListOfFiles()) {
88 auto _file = dynamic_cast<TFile *>(file);
89 auto keys = _file->GetListOfKeys();
90 if (keys) {
91 for (auto &&k : *keys) {
92 auto cl = TClass::GetClass((static_cast<TKey *>(k))->GetClassName());
93 if (cl == RooWorkspace::Class() || cl->InheritsFrom("RooWorkspace")) {
94 if (auto w = _file->Get<RooWorkspace>(k->GetName()); w) {
95 if (!in->contains(_file->GetName())) {
96 in->emplace_back(std::make_shared<xRooNode>(*_file));
97 }
98 if (!in->at(_file->GetName())->contains(w->GetName())) {
99 in->at(_file->GetName())
100 ->emplace_back(std::make_shared<xRooNode>(*w, in->at(_file->GetName())));
101 }
102 }
103 }
104 }
105 }
106 }
107 return *in;
108 };
109 }
110
111 // override file menu event handling so that can intercept "Open"
112 if (auto rb = dynamic_cast<TRootBrowser *>(GetBrowserImp())) {
113 rb->Disconnect(GETPOPUPMENU(rb, fMenuFile), "Activated(Int_t)", rb, "HandleMenu(Int_t)");
114 GETPOPUPMENU(rb, fMenuFile)->Connect("Activated(Int_t)", ClassName(), this, "HandleMenu(Int_t)");
115 }
116}
117
119{
120 if (id == TRootBrowser::kOpenFile) {
121 static TString dir(".");
122 TGFileInfo fi;
123 static const char *openFileTypes[] = {"ROOT files", "*.root", "JSON files", "*.json",
124 "All files", "*", nullptr, nullptr};
125 fi.fFileTypes = openFileTypes;
126 fi.SetIniDir(dir);
127 new TGFileDialog(gClient->GetDefaultRoot(), dynamic_cast<TRootBrowser *>(GetBrowserImp()), kFDOpen, &fi);
128 dir = fi.fIniDir;
129 std::vector<std::string> filesToOpen;
130 if (fi.fMultipleSelection && fi.fFileNamesList) {
131 TObjString *el;
132 TIter next(fi.fFileNamesList);
133 while ((el = static_cast<TObjString *>(next()))) {
134 filesToOpen.push_back(gSystem->UnixPathName(el->GetString()));
135 }
136 } else if (fi.fFilename) {
137 filesToOpen.push_back(gSystem->UnixPathName(fi.fFilename));
138 }
139 if (!filesToOpen.empty()) {
140 for (auto &f : filesToOpen) {
141 if (TString(f.data()).EndsWith(".json")) {
142 fTopNode->push_back(std::make_shared<xRooNode>(f.c_str()));
143 } else {
144 fTopNode->push_back(std::make_shared<xRooNode>(std::make_shared<TFile>(f.c_str())));
145 }
146 }
147 }
148 } else if (auto rb = dynamic_cast<TRootBrowser *>(GetBrowserImp())) {
149 rb->HandleMenu(id);
150 }
151}
152
154 : xRooBrowser([]() {
155 gEnv->SetValue("X11.UseXft", "no"); // for faster x11
156 gEnv->SetValue("X11.Sync", "no");
157 gEnv->SetValue("X11.FindBestVisual", "no");
158 gEnv->SetValue("Browser.Name", "TRootBrowser"); // forces classic root browser (in 6.26 onwards)
159 gEnv->SetValue("Canvas.Name", "TRootCanvas");
160 return new xRooNode("!Workspaces");
161 }())
162{
163}
164
166{
167 if (TString(filename).EndsWith(".root")) {
168 return fTopNode->emplace_back(std::make_shared<xRooNode>(std::make_shared<TFile>(filename))).get();
169 } else {
170 return fTopNode->emplace_back(std::make_shared<xRooNode>(filename)).get();
171 }
172}
173
174void xRooBrowser::ls(const char *path) const
175{
176 if (!fNode)
177 return;
178 if (!path) {
179 fNode->Print();
180 } else {
181 // will throw exception if not found
182 fNode->at(path)->Print();
183 }
184}
185
186void xRooBrowser::cd(const char *path)
187{
188 auto _node = fNode->at(path); // throws exception if not found
189 fNode = _node;
190}
191
193{
194 return dynamic_cast<xRooNode *>(TBrowser::GetSelected());
195}
196
#define f(i)
Definition RSha256.hxx:104
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
#define gClient
Definition TGClient.h:157
@ kFDOpen
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
A version of the TBrowser that can be used to interact with RooFit models and datasets.
Definition xRooBrowser.h:30
std::shared_ptr< xRooNode > fTopNode
Definition xRooBrowser.h:46
void ls(const char *path=nullptr) const override
List TNamed name and title.
std::shared_ptr< xRooNode > fNode
Definition xRooBrowser.h:45
xRooNode * Open(const char *filename)
The xRooNode class is designed to wrap over a TObject and provide functionality to aid with interacti...
Definition xRooNode.h:52
Persistable container for RooFit projects.
static TClass * Class()
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
TObject * GetSelected() const
Definition TBrowser.h:99
TBrowserImp * GetBrowserImp() const
Definition TBrowser.h:94
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4943
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:3037
TList * GetListOfKeys() const override
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
Set the value of a resource or create a new resource.
Definition TEnv.cxx:736
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
This class creates a file selection dialog.
TList * fFileNamesList
list of selected file names
char * fFilename
selected file name
const char ** fFileTypes
file types used to filter selectable files
char * fIniDir
on input: initial directory, on output: new directory
Bool_t fMultipleSelection
if true, allow multiple file selection
void SetIniDir(const char *inidir)
Set directory name.
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
Collectable string class.
Definition TObjString.h:28
const TString & GetString() const
Definition TObjString.h:46
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:225
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition TObject.cxx:654
This class creates a ROOT object browser, constituted by three main tabs.
Basic string class.
Definition TString.h:139
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2244
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1063
#define BEGIN_XROOFIT_NAMESPACE
Definition Config.h:24
#define END_XROOFIT_NAMESPACE
Definition Config.h:25
#define GETPOPUPMENU(b, m)