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
150int removeImporters(const std::string &needle)
151{
152 int n = 0;
153 for (auto &element : importers()) {
154 for (size_t i = element.second.size(); i > 0; --i) {
155 auto *imp = element.second[i - 1].get();
156 std::string name(typeid(*imp).name());
157 if (name.find(needle) != std::string::npos) {
158 element.second.erase(element.second.begin() + i - 1);
159 ++n;
160 }
161 }
162 }
163 return n;
164}
165
166int removeExporters(const std::string &needle)
167{
168 int n = 0;
169 for (auto &element : exporters()) {
170 for (size_t i = element.second.size(); i > 0; --i) {
171 auto *imp = element.second[i - 1].get();
172 std::string name(typeid(*imp).name());
173 if (name.find(needle) != std::string::npos) {
174 element.second.erase(element.second.begin() + i - 1);
175 ++n;
176 }
177 }
178 }
179 return n;
180}
181
183{
184 for (const auto &x : importers()) {
185 for (const auto &ePtr : x.second) {
186 // Passing *e directory to typeid results in clang warnings.
187 auto const &e = *ePtr;
188 std::cout << x.first << "\t" << typeid(e).name() << std::endl;
189 }
190 }
191}
193{
194 for (const auto &x : exporters()) {
195 for (const auto &ePtr : x.second) {
196 // Passing *e directory to typeid results in clang warnings.
197 auto const &e = *ePtr;
198 std::cout << x.first->GetName() << "\t" << typeid(e).name() << std::endl;
199 }
200 }
201}
202
203void loadFactoryExpressions(const std::string &fname)
204{
205 // load a yml file defining the factory expressions
206 std::ifstream infile(fname);
207 if (!infile.is_open()) {
208 std::cerr << "unable to read file '" << fname << "'" << std::endl;
209 return;
210 }
212}
213
214void loadFactoryExpressions(std::istream &is)
215{
217
218 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(is);
219 const RooFit::Detail::JSONNode &n = tree->rootnode();
220 for (const auto &cl : n.children()) {
221 std::string key = cl.key();
222 if (!cl.has_child("class")) {
223 std::cerr << "error for entry '" << key << "': 'class' key is required!" << std::endl;
224 continue;
225 }
226 std::string classname(cl["class"].val());
227 TClass *c = TClass::GetClass(classname.c_str());
228 if (!c) {
229 std::cerr << "unable to find class " << classname << ", skipping." << std::endl;
230 continue;
231 }
233 ex.tclass = c;
234 if (!cl.has_child("arguments")) {
235 std::cerr << "class " << classname << " seems to have no arguments attached, skipping" << std::endl;
236 continue;
237 }
238 for (const auto &arg : cl["arguments"].children()) {
239 ex.arguments.push_back(arg.val());
240 }
241 factoryExpressions[key] = ex;
242 }
243}
244
246{
247 // clear all factory expressions
249}
250
252{
253 // print all factory expressions
254 for (auto it : RooFit::JSONIO::importExpressions()) {
255 std::cout << it.first;
256 std::cout << " " << it.second.tclass->GetName();
257 for (auto v : it.second.arguments) {
258 std::cout << " " << v;
259 }
260 std::cout << std::endl;
261 }
262}
263
264///////////////////////////////////////////////////////////////////////////////////////////////////////
265// RooProxy-based export handling
266///////////////////////////////////////////////////////////////////////////////////////////////////////
267
268void loadExportKeys(const std::string &fname)
269{
270 // load a yml file defining the export keys
271 std::ifstream infile(fname);
272 if (!infile.is_open()) {
273 std::cerr << "unable to read file '" << fname << "'" << std::endl;
274 return;
275 }
277}
278
279void loadExportKeys(std::istream &is)
280{
282
283 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(is);
284 const RooFit::Detail::JSONNode &n = tree->rootnode();
285 for (const auto &cl : n.children()) {
286 std::string classname = cl.key();
287 TClass *c = TClass::GetClass(classname.c_str());
288 if (!c) {
289 std::cerr << "unable to find class " << classname << ", skipping." << std::endl;
290 continue;
291 }
293 auto *type = cl.find("type");
294 auto *proxies = cl.find("proxies");
295 if (!type) {
296 std::cerr << "class " << classname << "has not type key set, skipping" << std::endl;
297 continue;
298 }
299 if (!proxies) {
300 std::cerr << "class " << classname << "has no proxies identified, skipping" << std::endl;
301 continue;
302 }
303 ex.type = type->val();
304 for (const auto &k : proxies->children()) {
305 ex.proxies[k.key()] = k.val();
306 }
307 exportKeys[c] = ex;
308 }
309}
310
312{
313 // clear all export keys
315}
316
318{
319 // print all export keys
320 for (const auto &it : RooFit::JSONIO::exportKeys()) {
321 std::cout << it.first->GetName() << ": " << it.second.type;
322 for (const auto &kv : it.second.proxies) {
323 std::cout << " " << kv.first << "=" << kv.second;
324 }
325 std::cout << std::endl;
326 }
327}
328
329} // 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:110
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:2973
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:214
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:311
void clearFactoryExpressions()
Definition JSONIO.cxx:245
int removeImporters(const std::string &needle)
Definition JSONIO.cxx:150
int removeExporters(const std::string &needle)
Definition JSONIO.cxx:166
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:279
void printExporters()
Definition JSONIO.cxx:192
std::map< const std::string, ImportExpression > ImportExpressionMap
Definition JSONIO.h:77
void printImporters()
Definition JSONIO.cxx:182
void printFactoryExpressions()
Definition JSONIO.cxx:251
void printExportKeys()
Definition JSONIO.cxx:317
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