Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
21#include <iostream>
22#include <fstream>
23#include <sstream>
24
25// Include raw strings with initial export and import keys in JSON
28
29namespace RooFit {
30namespace JSONIO {
31
33{
34 static bool isAlreadySetup = false;
35 if (isAlreadySetup) {
36 return;
37 }
38
39 isAlreadySetup = true;
40
41 {
42 std::stringstream exportkeys;
45 }
46 {
47 std::stringstream factoryexpressions;
50 }
51}
52
54{
55 static ImportMap _importers;
56 return _importers;
57}
58
60{
61 static ExportMap _exporters;
62 return _exporters;
63}
64
71
78
79bool registerImporter(const std::string &key, std::unique_ptr<const Importer> f, bool topPriority)
80{
81 auto &vec = importers()[key];
82 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
83 return true;
84}
85
86bool registerExporter(const TClass *key, std::unique_ptr<const Exporter> f, bool topPriority)
87{
88 auto &vec = exporters()[key];
89 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
90 return true;
91}
92
93int removeImporters(const std::string &needle)
94{
95 int n = 0;
96 for (auto &element : importers()) {
97 for (size_t i = element.second.size(); i > 0; --i) {
98 auto *imp = element.second[i - 1].get();
99 std::string name(typeid(*imp).name());
100 if (name.find(needle) != std::string::npos) {
101 element.second.erase(element.second.begin() + i - 1);
102 ++n;
103 }
104 }
105 }
106 return n;
107}
108
109int removeExporters(const std::string &needle)
110{
111 int n = 0;
112 for (auto &element : exporters()) {
113 for (size_t i = element.second.size(); i > 0; --i) {
114 auto *imp = element.second[i - 1].get();
115 std::string name(typeid(*imp).name());
116 if (name.find(needle) != std::string::npos) {
117 element.second.erase(element.second.begin() + i - 1);
118 ++n;
119 }
120 }
121 }
122 return n;
123}
124
126{
127 for (const auto &x : importers()) {
128 for (const auto &ePtr : x.second) {
129 // Passing *e directory to typeid results in clang warnings.
130 auto const &e = *ePtr;
131 std::cout << x.first << "\t" << typeid(e).name() << std::endl;
132 }
133 }
134}
136{
137 for (const auto &x : exporters()) {
138 for (const auto &ePtr : x.second) {
139 // Passing *e directory to typeid results in clang warnings.
140 auto const &e = *ePtr;
141 std::cout << x.first->GetName() << "\t" << typeid(e).name() << std::endl;
142 }
143 }
144}
145
146void loadFactoryExpressions(const std::string &fname)
147{
148 // load a yml file defining the factory expressions
149 std::ifstream infile(fname);
150 if (!infile.is_open()) {
151 std::cerr << "unable to read file '" << fname << "'" << std::endl;
152 return;
153 }
155}
156
157void loadFactoryExpressions(std::istream &is)
158{
160
161 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(is);
162 const RooFit::Detail::JSONNode &n = tree->rootnode();
163 for (const auto &cl : n.children()) {
164 std::string key = cl.key();
165 if (!cl.has_child("class")) {
166 std::cerr << "error for entry '" << key << "': 'class' key is required!" << std::endl;
167 continue;
168 }
169 std::string classname(cl["class"].val());
170 TClass *c = TClass::GetClass(classname.c_str());
171 if (!c) {
172 std::cerr << "unable to find class " << classname << ", skipping." << std::endl;
173 continue;
174 }
176 ex.tclass = c;
177 if (!cl.has_child("arguments")) {
178 std::cerr << "class " << classname << " seems to have no arguments attached, skipping" << std::endl;
179 continue;
180 }
181 for (const auto &arg : cl["arguments"].children()) {
182 ex.arguments.push_back(arg.val());
183 }
184 factoryExpressions[key] = ex;
185 }
186}
187
189{
190 // clear all factory expressions
192}
193
195{
196 // print all factory expressions
197 for (auto it : RooFit::JSONIO::importExpressions()) {
198 std::cout << it.first;
199 std::cout << " " << it.second.tclass->GetName();
200 for (auto v : it.second.arguments) {
201 std::cout << " " << v;
202 }
203 std::cout << std::endl;
204 }
205}
206
207///////////////////////////////////////////////////////////////////////////////////////////////////////
208// RooProxy-based export handling
209///////////////////////////////////////////////////////////////////////////////////////////////////////
210
211void loadExportKeys(const std::string &fname)
212{
213 // load a yml file defining the export keys
214 std::ifstream infile(fname);
215 if (!infile.is_open()) {
216 std::cerr << "unable to read file '" << fname << "'" << std::endl;
217 return;
218 }
220}
221
222void loadExportKeys(std::istream &is)
223{
225
226 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(is);
227 const RooFit::Detail::JSONNode &n = tree->rootnode();
228 for (const auto &cl : n.children()) {
229 std::string classname = cl.key();
230 TClass *c = TClass::GetClass(classname.c_str());
231 if (!c) {
232 std::cerr << "unable to find class " << classname << ", skipping." << std::endl;
233 continue;
234 }
236 auto *type = cl.find("type");
237 auto *proxies = cl.find("proxies");
238 if (!type) {
239 std::cerr << "class " << classname << "has not type key set, skipping" << std::endl;
240 continue;
241 }
242 if (!proxies) {
243 std::cerr << "class " << classname << "has no proxies identified, skipping" << std::endl;
244 continue;
245 }
246 ex.type = type->val();
247 for (const auto &k : proxies->children()) {
248 ex.proxies[k.key()] = k.val();
249 }
250 exportKeys[c] = ex;
251 }
252}
253
255{
256 // clear all export keys
258}
259
261{
262 // print all export keys
263 for (const auto &it : RooFit::JSONIO::exportKeys()) {
264 std::cout << it.first->GetName() << ": " << it.second.type;
265 for (const auto &kv : it.second.proxies) {
266 std::cout << " " << kv.first << "=" << kv.second;
267 }
268 std::cout << std::endl;
269 }
270}
271
272} // namespace JSONIO
273} // namespace RooFit
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define e(i)
Definition RSha256.hxx:103
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
const_iterator begin() const
static std::unique_ptr< JSONTree > create()
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
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:3073
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:53
ExportMap & exporters()
Definition JSONIO.cxx:59
static bool registerImporter(const std::string &key, bool topPriority=true)
Definition JSONIO.h:86
void loadFactoryExpressions(std::istream &is)
Definition JSONIO.cxx:157
ImportExpressionMap & importExpressions()
Definition JSONIO.cxx:65
static bool registerExporter(const TClass *key, bool topPriority=true)
Definition JSONIO.h:91
void clearExportKeys()
Definition JSONIO.cxx:254
void clearFactoryExpressions()
Definition JSONIO.cxx:188
int removeImporters(const std::string &needle)
Definition JSONIO.cxx:93
int removeExporters(const std::string &needle)
Definition JSONIO.cxx:109
std::map< TClass const *, std::vector< std::unique_ptr< const Exporter > > > ExportMap
Definition JSONIO.h:75
void loadExportKeys(std::istream &is)
Definition JSONIO.cxx:222
void printExporters()
Definition JSONIO.cxx:135
std::map< const std::string, ImportExpression > ImportExpressionMap
Definition JSONIO.h:77
void printImporters()
Definition JSONIO.cxx:125
void printFactoryExpressions()
Definition JSONIO.cxx:194
void printExportKeys()
Definition JSONIO.cxx:260
void setupKeys()
Definition JSONIO.cxx:32
ExportKeysMap & exportKeys()
Definition JSONIO.cxx:72
std::map< TClass const *, ExportKeys > ExportKeysMap
Definition JSONIO.h:76
std::map< const std::string, std::vector< std::unique_ptr< const Importer > > > ImportMap
Definition JSONIO.h:74
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:64