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