Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONIO.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Carsten D. Burgard, DESY/ATLAS, Dec 2021
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 <RooFitHS3/JSONIO.h>
14
16
17#include <RooAbsPdf.h>
18
19#include <TClass.h>
20#include <TROOT.h>
21
22#include <iostream>
23#include <fstream>
24
25namespace RooFit {
26namespace JSONIO {
27
29{
30 static bool isAlreadySetup = false;
31 if (isAlreadySetup) {
32 return;
33 }
34
35 isAlreadySetup = true;
36
37 auto etcDir = std::string(TROOT::GetEtcDir());
38 loadExportKeys(etcDir + "/RooFitHS3_wsexportkeys.json");
39 loadFactoryExpressions(etcDir + "/RooFitHS3_wsfactoryexpressions.json");
40}
41
43{
44 static ImportMap _importers;
45 return _importers;
46}
47
49{
50 static ExportMap _exporters;
51 return _exporters;
52}
53
55{
56 setupKeys();
57 static ImportExpressionMap _factoryExpressions;
58 return _factoryExpressions;
59}
60
62{
63 setupKeys();
64 static ExportKeysMap _exportKeys;
65 return _exportKeys;
66}
67
68bool registerImporter(const std::string &key, std::unique_ptr<const Importer> f, bool topPriority)
69{
70 auto &vec = importers()[key];
71 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
72 return true;
73}
74
75bool registerExporter(const TClass *key, std::unique_ptr<const Exporter> f, bool topPriority)
76{
77 auto &vec = exporters()[key];
78 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
79 return true;
80}
81
82int removeImporters(const std::string &needle)
83{
84 int n = 0;
85 for (auto &element : importers()) {
86 for (size_t i = element.second.size(); i > 0; --i) {
87 auto *imp = element.second[i - 1].get();
88 std::string name(typeid(*imp).name());
89 if (name.find(needle) != std::string::npos) {
90 element.second.erase(element.second.begin() + i - 1);
91 ++n;
92 }
93 }
94 }
95 return n;
96}
97
98int removeExporters(const std::string &needle)
99{
100 int n = 0;
101 for (auto &element : exporters()) {
102 for (size_t i = element.second.size(); i > 0; --i) {
103 auto *imp = element.second[i - 1].get();
104 std::string name(typeid(*imp).name());
105 if (name.find(needle) != std::string::npos) {
106 element.second.erase(element.second.begin() + i - 1);
107 ++n;
108 }
109 }
110 }
111 return n;
112}
113
115{
116 for (const auto &x : importers()) {
117 for (const auto &ePtr : x.second) {
118 // Passing *e directory to typeid results in clang warnings.
119 auto const &e = *ePtr;
120 std::cout << x.first << "\t" << typeid(e).name() << std::endl;
121 }
122 }
123}
125{
126 for (const auto &x : exporters()) {
127 for (const auto &ePtr : x.second) {
128 // Passing *e directory to typeid results in clang warnings.
129 auto const &e = *ePtr;
130 std::cout << x.first->GetName() << "\t" << typeid(e).name() << std::endl;
131 }
132 }
133}
134
135void loadFactoryExpressions(const std::string &fname)
136{
137 auto &factoryExpressions = RooFit::JSONIO::importExpressions();
138
139 // load a yml file defining the factory expressions
140 std::ifstream infile(fname);
141 if (!infile.is_open()) {
142 std::cerr << "unable to read file '" << fname << "'" << std::endl;
143 return;
144 }
145
146 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(infile);
147 const RooFit::Detail::JSONNode &n = tree->rootnode();
148 for (const auto &cl : n.children()) {
149 std::string key = cl.key();
150 if (!cl.has_child("class")) {
151 std::cerr << "error in file '" << fname << "' for entry '" << key << "': 'class' key is required!"
152 << std::endl;
153 continue;
154 }
155 std::string classname(cl["class"].val());
156 TClass *c = TClass::GetClass(classname.c_str());
157 if (!c) {
158 std::cerr << "unable to find class " << classname << ", skipping." << std::endl;
159 continue;
160 }
162 ex.tclass = c;
163 if (!cl.has_child("arguments")) {
164 std::cerr << "class " << classname << " seems to have no arguments attached, skipping" << std::endl;
165 continue;
166 }
167 for (const auto &arg : cl["arguments"].children()) {
168 ex.arguments.push_back(arg.val());
169 }
170 factoryExpressions[key] = ex;
171 }
172}
173
175{
176 // clear all factory expressions
178}
179
181{
182 // print all factory expressions
183 for (auto it : RooFit::JSONIO::importExpressions()) {
184 std::cout << it.first;
185 std::cout << " " << it.second.tclass->GetName();
186 for (auto v : it.second.arguments) {
187 std::cout << " " << v;
188 }
189 std::cout << std::endl;
190 }
191}
192
193///////////////////////////////////////////////////////////////////////////////////////////////////////
194// RooProxy-based export handling
195///////////////////////////////////////////////////////////////////////////////////////////////////////
196
197void loadExportKeys(const std::string &fname)
198{
200
201 // load a yml file defining the export keys
202 std::ifstream infile(fname);
203 if (!infile.is_open()) {
204 std::cerr << "unable to read file '" << fname << "'" << std::endl;
205 return;
206 }
207
208 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(infile);
209 const RooFit::Detail::JSONNode &n = tree->rootnode();
210 for (const auto &cl : n.children()) {
211 std::string classname = cl.key();
212 TClass *c = TClass::GetClass(classname.c_str());
213 if (!c) {
214 std::cerr << "unable to find class " << classname << ", skipping." << std::endl;
215 continue;
216 }
218 auto *type = cl.find("type");
219 auto *proxies = cl.find("proxies");
220 if (!type) {
221 std::cerr << "class " << classname << "has not type key set, skipping" << std::endl;
222 continue;
223 }
224 if (!proxies) {
225 std::cerr << "class " << classname << "has no proxies identified, skipping" << std::endl;
226 continue;
227 }
228 ex.type = type->val();
229 for (const auto &k : proxies->children()) {
230 ex.proxies[k.key()] = k.val();
231 }
232 exportKeys[c] = ex;
233 }
234}
235
237{
238 // clear all export keys
240}
241
243{
244 // print all export keys
245 for (const auto &it : RooFit::JSONIO::exportKeys()) {
246 std::cout << it.first->GetName() << ": " << it.second.type;
247 for (const auto &kv : it.second.proxies) {
248 std::cout << " " << kv.first << "=" << kv.second;
249 }
250 std::cout << std::endl;
251 }
252}
253
254} // namespace JSONIO
255} // namespace RooFit
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define e(i)
Definition RSha256.hxx:103
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 Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
static std::unique_ptr< JSONTree > create()
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
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
static const TString & GetEtcDir()
Get the sysconfig directory in the installation. Static utility function.
Definition TROOT.cxx:3056
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Double_t ex[n]
Definition legend1.C:17
ImportMap & importers()
Definition JSONIO.cxx:42
ExportMap & exporters()
Definition JSONIO.cxx:48
static bool registerImporter(const std::string &key, bool topPriority=true)
Definition JSONIO.h:86
void loadFactoryExpressions(const std::string &fname)
Definition JSONIO.cxx:135
std::map< const std::string, ImportExpression > ImportExpressionMap
Definition JSONIO.h:77
std::map< TClass const *, ExportKeys > ExportKeysMap
Definition JSONIO.h:76
ImportExpressionMap & importExpressions()
Definition JSONIO.cxx:54
void loadExportKeys(const std::string &fname)
Definition JSONIO.cxx:197
static bool registerExporter(const TClass *key, bool topPriority=true)
Definition JSONIO.h:91
void clearExportKeys()
Definition JSONIO.cxx:236
void clearFactoryExpressions()
Definition JSONIO.cxx:174
int removeImporters(const std::string &needle)
Definition JSONIO.cxx:82
int removeExporters(const std::string &needle)
Definition JSONIO.cxx:98
std::map< TClass const *, std::vector< std::unique_ptr< const Exporter > > > ExportMap
Definition JSONIO.h:75
void printExporters()
Definition JSONIO.cxx:124
std::map< const std::string, std::vector< std::unique_ptr< const Importer > > > ImportMap
Definition JSONIO.h:74
void printImporters()
Definition JSONIO.cxx:114
void printFactoryExpressions()
Definition JSONIO.cxx:180
void printExportKeys()
Definition JSONIO.cxx:242
void setupKeys()
Definition JSONIO.cxx:28
ExportKeysMap & exportKeys()
Definition JSONIO.cxx:61
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26