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#include "xRooFit/xRooBrowser.h"
14#include "xRooFit/xRooNode.h"
15
16#include "TSystem.h"
17#include "TKey.h"
18#include "TROOT.h"
19#include "TEnv.h"
20#include "TFile.h"
21#include "RooWorkspace.h"
22#include "TRootBrowser.h"
23#include "TGMenu.h"
24#include "TGFileDialog.h"
25#include "TObjString.h"
26
27#define GETPOPUPMENU(b,m) ((TGPopupMenu*)(*(void**)(((unsigned char*)b) + b->Class()->GetDataMemberOffset(#m))))
28
30
31xRooBrowser::xRooBrowser(xRooNode *o) : TBrowser("RooBrowser", o, "RooFit Browser"), fTopNode(o)
32{
33
34 fNode = std::shared_ptr<xRooNode>(fTopNode.get(), [](xRooNode *) {});
35
36 if (fTopNode) {
37 fTopNode->fBrowseOperation = [](xRooNode *in) {
38 for (auto file : *gROOT->GetListOfFiles()) {
39 auto _file = dynamic_cast<TFile *>(file);
40 auto keys = _file->GetListOfKeys();
41 if (keys) {
42 for (auto &&k : *keys) {
43 auto cl = TClass::GetClass(((TKey *)k)->GetClassName());
44 if (cl == RooWorkspace::Class() || cl->InheritsFrom("RooWorkspace")) {
45 if (auto w = _file->Get<RooWorkspace>(k->GetName()); w) {
46 if (!in->contains(_file->GetName())) {
47 in->emplace_back(std::make_shared<xRooNode>(*_file));
48 }
49 if (!in->at(_file->GetName())->contains(w->GetName())) {
50 in->at(_file->GetName())
51 ->emplace_back(std::make_shared<xRooNode>(*w, in->at(_file->GetName())));
52 }
53 }
54 }
55 }
56 }
57 }
58 return *in;
59 };
60 }
61
62 // override file menu event handling so that can intercept "Open"
63 if (auto rb = dynamic_cast<TRootBrowser *>(GetBrowserImp())) {
64 rb->Disconnect(GETPOPUPMENU(rb,fMenuFile), "Activated(Int_t)", rb, "HandleMenu(Int_t)");
65 GETPOPUPMENU(rb,fMenuFile)->Connect("Activated(Int_t)", ClassName(), this, "HandleMenu(Int_t)");
66 }
67}
68
70{
71 if (id == TRootBrowser::kOpenFile) {
72 static TString dir(".");
73 TGFileInfo fi;
74 static const char *openFileTypes[] = {"ROOT files", "*.root", "JSON files", "*.json", "All files", "*", 0, 0};
75 fi.fFileTypes = openFileTypes;
76 fi.SetIniDir(dir);
77 new TGFileDialog(gClient->GetDefaultRoot(), dynamic_cast<TRootBrowser *>(GetBrowserImp()), kFDOpen, &fi);
78 dir = fi.fIniDir;
79 std::vector<std::string> filesToOpen;
81 TObjString *el;
82 TIter next(fi.fFileNamesList);
83 while ((el = (TObjString *)next())) {
84 filesToOpen.push_back(gSystem->UnixPathName(el->GetString()));
85 }
86 } else if (fi.fFilename) {
87 filesToOpen.push_back(gSystem->UnixPathName(fi.fFilename));
88 }
89 if (!filesToOpen.empty()) {
90 for (auto &f : filesToOpen) {
91 if (TString(f.data()).EndsWith(".json")) {
92 fTopNode->push_back(std::make_shared<xRooNode>(f.c_str()));
93 } else {
94 fTopNode->push_back(std::make_shared<xRooNode>(std::make_shared<TFile>(f.c_str())));
95 }
96 }
97 }
98 } else if (auto rb = dynamic_cast<TRootBrowser *>(GetBrowserImp())) {
99 rb->HandleMenu(id);
100 }
101}
102
104 : xRooBrowser([]() {
105 gEnv->SetValue("X11.UseXft", "no"); // for faster x11
106 gEnv->SetValue("X11.Sync", "no");
107 gEnv->SetValue("X11.FindBestVisual", "no");
108 gEnv->SetValue("Browser.Name", "TRootBrowser"); // forces classic root browser (in 6.26 onwards)
109 return new xRooNode("!Workspaces");
110 }())
111{
112}
113
114void xRooBrowser::ls(const char *path) const
115{
116 if (!fNode)
117 return;
118 if (!path)
119 fNode->Print();
120 else {
121 // will throw exception if not found
122 fNode->at(path)->Print();
123 }
124}
125
126void xRooBrowser::cd(const char *path)
127{
128 auto _node = fNode->at(path); // throws exception if not found
129 fNode = _node;
130}
131
133{
134 return dynamic_cast<xRooNode *>(TBrowser::GetSelected());
135}
136
#define f(i)
Definition RSha256.hxx:104
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
#define gClient
Definition TGClient.h:157
@ kFDOpen
#define gROOT
Definition TROOT.h:405
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
void ls(const char *path=nullptr) const override
List TNamed name and title.
std::shared_ptr< xRooNode > fNode
Definition xRooBrowser.h:43
std::shared_ptr< xRooNode > fTopNode
Definition xRooBrowser.h:44
The RooWorkspace is a 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: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 a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:51
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
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
void Print(Option_t *option="") const override
Print TNamed name and title.
Definition TNamed.cxx:128
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
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:2222
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1066
Definition file.py:1
#define BEGIN_XROOFIT_NAMESPACE
Definition Config.h:24
#define END_XROOFIT_NAMESPACE
Definition Config.h:25
#define GETPOPUPMENU(b, m)