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
21#include <fstream>
22#include <iostream>
23#include <mutex>
24#include <sstream>
25
26// Include raw strings with initial export and import keys in JSON
29
30namespace RooFit::JSONIO {
31
33{
34 static bool isAlreadySetup = false;
35 if (isAlreadySetup) {
36 return;
37 }
38
39 isAlreadySetup = true;
40
41 std::stringstream exportkeys;
44}
45
47{
48 static bool isAlreadySetup = false;
49 if (isAlreadySetup) {
50 return;
51 }
52
53 isAlreadySetup = true;
54
55 std::stringstream factoryexpressions;
58}
59
61{
62 static ImportMap _importers;
63 return _importers;
64}
65
66namespace {
67
68auto &exportersToAdd()
69{
70 static std::map<const std::string, std::vector<std::unique_ptr<const Exporter>>> toAdd;
71 return toAdd;
72}
73
75{
76 static ExportMap _exporters;
77 return _exporters;
78}
79
80} // namespace
81
83{
84 // If there are exporters to be added, do this now.
85 for (auto &item : exportersToAdd()) {
86 TClass *klass = TClass::GetClass(item.first.c_str());
87 auto &exporters = exportersImpl()[klass]; // registered exporters so far
88 auto &toAdd = item.second; // exporters to add
89
90 // Find the nullptr separator
91 auto nullIt = std::find(toAdd.begin(), toAdd.end(), nullptr);
92
93 if (nullIt == toAdd.end()) {
94 throw std::runtime_error("toAdd does not contain nullptr separator");
95 }
96
97 // Move elements after nullptr to the back
98 exporters.insert(exporters.end(), std::make_move_iterator(nullIt + 1), std::make_move_iterator(toAdd.end()));
99
100 // Move elements before nullptr to the front
101 exporters.insert(exporters.begin(), std::make_move_iterator(toAdd.begin()), std::make_move_iterator(nullIt));
102
103 toAdd.clear();
104 }
105 exportersToAdd().clear();
106 return exportersImpl();
107}
108
115
122
123bool registerImporter(const std::string &key, std::unique_ptr<const Importer> f, bool topPriority)
124{
125 auto &vec = importers()[key];
126 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
127 return true;
128}
129
130bool registerExporter(const TClass *key, std::unique_ptr<const Exporter> f, bool topPriority)
131{
132 auto &vec = exporters()[key];
133 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
134 return true;
135}
136
137bool registerExporter(const std::string &key, std::unique_ptr<const Exporter> f, bool topPriority)
138{
139 auto &vec = exportersToAdd()[key];
140 // The vector starts out with just a nullptr separator. Elements before it
141 // will be added to the top of the exporter queue, and the elements after
142 // get appended to the end.
143 if (vec.empty()) {
144 vec.emplace_back(nullptr);
145 }
146 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
147 return true;
148}
149
150namespace {
151
152template <class Map>
153int removeByTypeName(Map &map, const std::string &needle)
154{
155 int n = 0;
156 for (auto &element : map) {
157 for (size_t i = element.second.size(); i > 0; --i) {
158 auto *imp = element.second[i - 1].get();
159 std::string name(typeid(*imp).name());
160 if (name.find(needle) != std::string::npos) {
161 element.second.erase(element.second.begin() + i - 1);
162 ++n;
163 }
164 }
165 }
166 return n;
167}
168
169template <class Map, class KeyPrinter>
170void printByTypeName(Map &map, KeyPrinter printKey)
171{
172 for (const auto &x : map) {
173 for (const auto &ePtr : x.second) {
174 // Passing *e directory to typeid results in clang warnings.
175 auto const &e = *ePtr;
176 std::cout << printKey(x.first) << "\t" << typeid(e).name() << std::endl;
177 }
178 }
179}
180
181} // namespace
182
183int removeImporters(const std::string &needle)
184{
186}
187
188int removeExporters(const std::string &needle)
189{
191}
192
194{
195 printByTypeName(importers(), [](auto const &key) { return key; });
196}
198{
199 printByTypeName(exporters(), [](auto const &key) { return key->GetName(); });
200}
201
202void loadFactoryExpressions(const std::string &fname)
203{
204 // load a yml file defining the factory expressions
205 std::ifstream infile(fname);
206 if (!infile.is_open()) {
207 std::cerr << "unable to read file '" << fname << "'" << std::endl;
208 return;
209 }
211}
212
213void loadFactoryExpressions(std::istream &is)
214{
216
217 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(is);
218 const RooFit::Detail::JSONNode &n = tree->rootnode();
219 for (const auto &cl : n.children()) {
220 std::string key = cl.key();
221 if (!cl.has_child("class")) {
222 std::cerr << "error for entry '" << key << "': 'class' key is required!" << std::endl;
223 continue;
224 }
225 std::string classname(cl["class"].val());
226 TClass *c = TClass::GetClass(classname.c_str());
227 if (!c) {
228 std::cerr << "unable to find class " << classname << ", skipping." << std::endl;
229 continue;
230 }
232 ex.tclass = c;
233 if (!cl.has_child("arguments")) {
234 std::cerr << "class " << classname << " seems to have no arguments attached, skipping" << std::endl;
235 continue;
236 }
237 for (const auto &arg : cl["arguments"].children()) {
238 ex.arguments.push_back(arg.val());
239 }
240 factoryExpressions[key] = ex;
241 }
242}
243
245{
246 // clear all factory expressions
248}
249
251{
252 // print all factory expressions
253 for (auto it : RooFit::JSONIO::importExpressions()) {
254 std::cout << it.first;
255 std::cout << " " << it.second.tclass->GetName();
256 for (auto v : it.second.arguments) {
257 std::cout << " " << v;
258 }
259 std::cout << std::endl;
260 }
261}
262
263///////////////////////////////////////////////////////////////////////////////////////////////////////
264// RooProxy-based export handling
265///////////////////////////////////////////////////////////////////////////////////////////////////////
266
267void loadExportKeys(const std::string &fname)
268{
269 // load a yml file defining the export keys
270 std::ifstream infile(fname);
271 if (!infile.is_open()) {
272 std::cerr << "unable to read file '" << fname << "'" << std::endl;
273 return;
274 }
276}
277
278void loadExportKeys(std::istream &is)
279{
281
282 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(is);
283 const RooFit::Detail::JSONNode &n = tree->rootnode();
284 for (const auto &cl : n.children()) {
285 std::string classname = cl.key();
286 TClass *c = TClass::GetClass(classname.c_str());
287 if (!c) {
288 std::cerr << "unable to find class " << classname << ", skipping." << std::endl;
289 continue;
290 }
292 auto *type = cl.find("type");
293 auto *proxies = cl.find("proxies");
294 if (!type) {
295 std::cerr << "class " << classname << "has not type key set, skipping" << std::endl;
296 continue;
297 }
298 if (!proxies) {
299 std::cerr << "class " << classname << "has no proxies identified, skipping" << std::endl;
300 continue;
301 }
302 ex.type = type->val();
303 for (const auto &k : proxies->children()) {
304 ex.proxies[k.key()] = k.val();
305 }
306 exportKeys[c] = ex;
307 }
308}
309
311{
312 // clear all export keys
314}
315
317{
318 // print all export keys
319 for (const auto &it : RooFit::JSONIO::exportKeys()) {
320 std::cout << it.first->GetName() << ": " << it.second.type;
321 for (const auto &kv : it.second.proxies) {
322 std::cout << " " << kv.first << "=" << kv.second;
323 }
324 std::cout << std::endl;
325 }
326}
327
328} // namespace RooFit::JSONIO
#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:145
const_iterator begin() const
const_iterator end() 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:2994
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:60
ExportMap & exporters()
Definition JSONIO.cxx:82
static bool registerImporter(const std::string &key, bool topPriority=true)
Definition JSONIO.h:85
void loadFactoryExpressions(std::istream &is)
Definition JSONIO.cxx:213
void setupFactoryExpressions()
Definition JSONIO.cxx:46
ImportExpressionMap & importExpressions()
Definition JSONIO.cxx:109
static bool registerExporter(const TClass *key, bool topPriority=true)
Definition JSONIO.h:90
void clearExportKeys()
Definition JSONIO.cxx:310
void clearFactoryExpressions()
Definition JSONIO.cxx:244
int removeImporters(const std::string &needle)
Definition JSONIO.cxx:183
int removeExporters(const std::string &needle)
Definition JSONIO.cxx:188
std::map< TClass const *, std::vector< std::unique_ptr< const Exporter > > > ExportMap
Definition JSONIO.h:75
void setupExportKeys()
Definition JSONIO.cxx:32
void loadExportKeys(std::istream &is)
Definition JSONIO.cxx:278
void printExporters()
Definition JSONIO.cxx:197
std::map< const std::string, ImportExpression > ImportExpressionMap
Definition JSONIO.h:77
void printImporters()
Definition JSONIO.cxx:193
void printFactoryExpressions()
Definition JSONIO.cxx:250
void printExportKeys()
Definition JSONIO.cxx:316
ExportKeysMap & exportKeys()
Definition JSONIO.cxx:116
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