Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonDigitizationConfig.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JsonDigitizationConfig.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
18 
19 #include <cstddef>
20 #include <fstream>
21 #include <initializer_list>
22 #include <stdexcept>
23 #include <utility>
24 #include <vector>
25 
26 namespace ActsExamples {
27 namespace {
28 void to_json(nlohmann::json& j, const ActsFatras::SingleParameterSmearFunction<
30  // Gauss:
31  auto gauss = f.target<const Digitization::Gauss>();
32  if (gauss != nullptr) {
33  j["type"] = "Gauss";
34  j["mean"] = 0;
35  j["stddev"] = gauss->sigma;
36  return;
37  }
38  // Truncated gauss:
39  auto gaussT = f.target<const Digitization::GaussTrunc>();
40  if (gaussT != nullptr) {
41  j["type"] = "GaussTrunc";
42  j["mean"] = 0;
43  j["stddev"] = gaussT->sigma;
44  j["range"] = gaussT->range;
45  return;
46  }
47  // Clipped gauss:
48  auto gaussC = f.target<const Digitization::GaussClipped>();
49  if (gaussC != nullptr) {
50  j["type"] = "GaussClipped";
51  j["mean"] = 0;
52  j["stddev"] = gaussC->sigma;
53  j["range"] = gaussC->range;
54  j["max_attempts"] = gaussC->maxAttemps;
55  return;
56  }
57  // Uniform
58  auto uniform = f.target<const Digitization::Uniform>();
59  if (uniform != nullptr) {
60  j["type"] = "Uniform";
61  j["bindata"] = nlohmann::json(uniform->binningData);
62  return;
63  }
64  // Digital
65  auto digital = f.target<const Digitization::Digital>();
66  if (uniform != nullptr) {
67  j["type"] = "Digitial";
68  j["bindata"] = nlohmann::json(digital->binningData);
69  return;
70  }
71  // Exact
72  auto exact = f.target<const Digitization::Digital>();
73  if (exact != nullptr) {
74  j["type"] = "Exact";
75  return;
76  }
77 
78  throw std::runtime_error("Unable to serialize smearer");
79 }
80 
81 void from_json(
82  const nlohmann::json& j,
84  std::string sType = j["type"];
85 
86  if (sType == "Gauss") {
87  f = Digitization::Gauss(j["stddev"]);
88  } else if (sType == "GaussTrunc") {
89  Acts::ActsScalar sigma = j["stddev"];
90  std::pair<Acts::ActsScalar, Acts::ActsScalar> range = j["range"];
91  f = Digitization::GaussTrunc(sigma, range);
92  } else if (sType == "GaussClipped") {
93  Acts::ActsScalar sigma = j["stddev"];
94  std::pair<Acts::ActsScalar, Acts::ActsScalar> range = j["range"];
95  f = Digitization::GaussClipped(sigma, range);
96  } else if (sType == "Uniform") {
98  from_json(j["bindata"], bd);
99  f = Digitization::Uniform(std::move(bd));
100  } else if (sType == "Digitial") {
102  from_json(j["bindata"], bd);
103  f = Digitization::Uniform(std::move(bd));
104  } else if (sType == "Exact") {
105  f = Digitization::Exact{};
106  } else {
107  throw std::invalid_argument("Unknown smearer type '" + sType + "'");
108  }
109 }
110 
111 } // namespace
112 } // namespace ActsExamples
113 
114 void ActsExamples::to_json(nlohmann::json& j,
116  j["index"] = psc.index;
117  to_json(j, psc.smearFunction);
118 }
119 
120 void ActsExamples::from_json(const nlohmann::json& j,
122  psc.index = static_cast<Acts::BoundIndices>(j["index"]);
123  from_json(j, psc.smearFunction);
124 }
125 
126 void ActsExamples::to_json(nlohmann::json& j,
128  std::vector<size_t> indices;
129  for (const auto& idx : gdc.indices) {
130  indices.push_back(static_cast<size_t>(idx));
131  }
132  j["indices"] = indices;
133  j["segmentation"] = nlohmann::json(gdc.segmentation);
134  j["thickness"] = gdc.thickness;
135  j["threshold"] = gdc.threshold;
136  j["digital"] = gdc.digital;
137  to_json(j["charge-smearing"], gdc.chargeSmearer);
138 }
139 
140 void ActsExamples::from_json(const nlohmann::json& j,
142  for (const auto& jidx : j["indices"]) {
143  gdc.indices.push_back(static_cast<Acts::BoundIndices>(jidx));
144  }
145  from_json(j["segmentation"], gdc.segmentation);
146  gdc.thickness = j["thickness"];
147  gdc.threshold = j["threshold"];
148  gdc.digital = j["digital"];
149  from_json(j["charge-smearing"], gdc.chargeSmearer);
150 }
151 
152 void ActsExamples::to_json(nlohmann::json& j,
153  const ActsExamples::SmearingConfig& sdc) {
154  for (const auto& sc : sdc) {
155  j.push_back(nlohmann::json(sc));
156  }
157 }
158 
159 void ActsExamples::from_json(const nlohmann::json& j,
161  for (const auto& jpsc : j) {
163  from_json(jpsc, psc);
164  sdc.push_back(psc);
165  }
166 }
167 
168 void ActsExamples::to_json(nlohmann::json& j,
170  if (not dc.geometricDigiConfig.indices.empty()) {
171  j["geometric"] = nlohmann::json(dc.geometricDigiConfig);
172  }
173  if (not dc.smearingDigiConfig.empty()) {
174  j["smearing"] = nlohmann::json(dc.smearingDigiConfig);
175  }
176 }
177 
178 void ActsExamples::from_json(const nlohmann::json& j,
180  if (j.find("geometric") != j.end()) {
181  nlohmann::json jgdc = j["geometric"];
182  from_json(jgdc, dc.geometricDigiConfig);
183  }
184  if (j.find("smearing") != j.end()) {
185  nlohmann::json jsdc = j["smearing"];
186  from_json(jsdc, dc.smearingDigiConfig);
187  }
188 }
189 
192  nlohmann::json djson;
193  if (path.empty()) {
195  }
196  std::ifstream infile(path, std::ifstream::in | std::ifstream::binary);
197  // rely on exception for error handling
198  infile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
199  infile >> djson;
200  return DigiConfigConverter("digitization-configuration").fromJson(djson);
201 }
202 
205  const std::string& path) {
206  std::ofstream outfile(path, std::ofstream::out | std::ofstream::binary);
207  // rely on exception for error handling
208  outfile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
209  outfile << DigiConfigConverter("digitization-configuration")
210  .toJson(cfg, nullptr)
211  .dump(2);
212 }