Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
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 */
13/** \class ROOT::Experimental::XRooFit::xRooBrowser
14\ingroup xroofit
16 \image html RooBrowser.png width=50%
18 \brief A version of the TBrowser that can be used to interact with RooFit models and datasets.
20 Also available under the ROOT::Experimental::RooBrowser alias.
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.
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):
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).
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.
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)
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.
58 */
61#include "xRooFit/xRooBrowser.h"
62#include "xRooFit/xRooNode.h"
64#include "TSystem.h"
65#include "TKey.h"
66#include "TROOT.h"
67#include "TEnv.h"
68#include "TFile.h"
69#include "RooWorkspace.h"
70#include "TRootBrowser.h"
71#include "TGMenu.h"
72#include "TGFileDialog.h"
73#include "TObjString.h"
75#define GETPOPUPMENU(b, m) reinterpret_cast<TGPopupMenu *>(*reinterpret_cast<void **>(reinterpret_cast<unsigned char *>(b) + b->Class()->GetDataMemberOffset(#m)))
79xRooBrowser::xRooBrowser(xRooNode *o) : TBrowser("RooBrowser", o, "RooFit Browser"), fTopNode(o)
82 fNode = std::shared_ptr<xRooNode>(fTopNode.get(), [](xRooNode *) {});
84 if (fTopNode) {
85 fTopNode->fBrowseOperation = [](xRooNode *in) {
86 for (auto file : *gROOT->GetListOfFiles()) {
87 auto _file = dynamic_cast<TFile *>(file);
88 auto keys = _file->GetListOfKeys();
89 if (keys) {
90 for (auto &&k : *keys) {
91 auto cl = TClass::GetClass((static_cast<TKey *>(k))->GetClassName());
92 if (cl == RooWorkspace::Class() || cl->InheritsFrom("RooWorkspace")) {
93 if (auto w = _file->Get<RooWorkspace>(k->GetName()); w) {
94 if (!in->contains(_file->GetName())) {
95 in->emplace_back(std::make_shared<xRooNode>(*_file));
96 }
97 if (!in->at(_file->GetName())->contains(w->GetName())) {
98 in->at(_file->GetName())
99 ->emplace_back(std::make_shared<xRooNode>(*w, in->at(_file->GetName())));
100 }
101 }
102 }
103 }
104 }
105 }
106 return *in;
107 };
108 }
110 // override file menu event handling so that can intercept "Open"
111 if (auto rb = dynamic_cast<TRootBrowser *>(GetBrowserImp())) {
112 rb->Disconnect(GETPOPUPMENU(rb, fMenuFile), "Activated(Int_t)", rb, "HandleMenu(Int_t)");
113 GETPOPUPMENU(rb, fMenuFile)->Connect("Activated(Int_t)", ClassName(), this, "HandleMenu(Int_t)");
114 }
119 if (id == TRootBrowser::kOpenFile) {
120 static TString dir(".");
121 TGFileInfo fi;
122 static const char *openFileTypes[] = {"ROOT files", "*.root", "JSON files", "*.json", "All files", "*", nullptr, nullptr};
123 fi.fFileTypes = openFileTypes;
124 fi.SetIniDir(dir);
125 new TGFileDialog(gClient->GetDefaultRoot(), dynamic_cast<TRootBrowser *>(GetBrowserImp()), kFDOpen, &fi);
126 dir = fi.fIniDir;
127 std::vector<std::string> filesToOpen;
128 if (fi.fMultipleSelection && fi.fFileNamesList) {
129 TObjString *el;
130 TIter next(fi.fFileNamesList);
131 while ((el = static_cast<TObjString *>(next()))) {
132 filesToOpen.push_back(gSystem->UnixPathName(el->GetString()));
133 }
134 } else if (fi.fFilename) {
135 filesToOpen.push_back(gSystem->UnixPathName(fi.fFilename));
136 }
137 if (!filesToOpen.empty()) {
138 for (auto &f : filesToOpen) {
139 if (TString(f.data()).EndsWith(".json")) {
140 fTopNode->push_back(std::make_shared<xRooNode>(f.c_str()));
141 } else {
142 fTopNode->push_back(std::make_shared<xRooNode>(std::make_shared<TFile>(f.c_str())));
143 }
144 }
145 }
146 } else if (auto rb = dynamic_cast<TRootBrowser *>(GetBrowserImp())) {
147 rb->HandleMenu(id);
148 }
152 : xRooBrowser([]() {
153 gEnv->SetValue("X11.UseXft", "no"); // for faster x11
154 gEnv->SetValue("X11.Sync", "no");
155 gEnv->SetValue("X11.FindBestVisual", "no");
156 gEnv->SetValue("Browser.Name", "TRootBrowser"); // forces classic root browser (in 6.26 onwards)
157 gEnv->SetValue("Canvas.Name", "TRootCanvas");
158 return new xRooNode("!Workspaces");
159 }())
165 if (TString(filename).EndsWith(".root")) {
166 return fTopNode->emplace_back(std::make_shared<xRooNode>(std::make_shared<TFile>(filename))).get();
167 } else {
168 return fTopNode->emplace_back(std::make_shared<xRooNode>(filename)).get();
169 }
172void xRooBrowser::ls(const char *path) const
174 if (!fNode)
175 return;
176 if (!path) {
177 fNode->Print();
178 } else {
179 // will throw exception if not found
180 fNode->at(path)->Print();
181 }
184void xRooBrowser::cd(const char *path)
186 auto _node = fNode->at(path); // throws exception if not found
187 fNode = _node;
192 return dynamic_cast<xRooNode *>(TBrowser::GetSelected());
#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:560
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:51
Persistable container for RooFit projects.
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
TObject * GetSelected() const
Definition TBrowser.h:98
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:4874
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:2968
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 composed of a header, followed by consecutive data records (TKey instances) with a wel...
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
static TClass * Class()
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:207
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition TObject.cxx:636
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:2223
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1050
Definition file.py:1
Definition Config.h:25
#define GETPOPUPMENU(b, m)