BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
HistogramFactory.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BinGeneration.hh"
19#include "HistogramDef.hh"
20#include "HistogramDef1D.hh"
21#include "HistogramDef2D.hh"
22#include "HistogramDef3D.hh"
23#include "HistogramDef4D.hh"
24#include "HistogramFactory.hh"
25
26#include "TH1.h"
27#include "TH1D.h"
28#include "TH2D.h"
29#include "TH3D.h"
30
31#ifdef USE_BOOST
32#include "BDSBH4D.hh"
33#include "BDSBH4DBase.hh"
34#include "BDSBH4DTypeDefs.hh"
35#else
36#include "RBDSException.hh"
37#include <iostream>
38#endif
39
40#include <string>
41#include <vector>
42
43ClassImp(HistogramFactory)
44
45HistogramFactory::HistogramFactory()
46{;}
47
48HistogramFactory::~HistogramFactory()
49{;}
50
52 const std::string& overRideName,
53 const std::string& overRideTitle)
54{
55 TH1* result = nullptr;
56 const int nDimensions = definition->nDimensions;
57 switch (nDimensions)
58 {
59 case 1:
60 {
61 const HistogramDef1D* d = dynamic_cast<const HistogramDef1D*>(definition);
62 if (d)
63 {result = CreateHistogram1D(d, overRideName, overRideTitle);}
64 break;
65 }
66 case 2:
67 {
68 const HistogramDef2D* d = dynamic_cast<const HistogramDef2D*>(definition);
69 if (d)
70 {result = CreateHistogram2D(d, overRideName, overRideTitle);}
71 break;
72 }
73 case 3:
74 {
75 const HistogramDef3D* d = dynamic_cast<const HistogramDef3D*>(definition);
76 if (d)
77 {result = CreateHistogram3D(d, overRideName, overRideTitle);}
78 break;
79 }
80 case 4:
81 {
82#ifdef USE_BOOST
83 const HistogramDef4D* d = static_cast<const HistogramDef4D*>(definition);
84 if (d)
85 {result = CreateHistogram4D(d, overRideName, overRideTitle);}
86#else
87 throw RBDSException("HistogramFactory::CreateHistogram>", "Not compiled with BOOST libraries - no 4D histograms.");
88#endif
89 break;
90 }
91 default:
92 {break;}
93 }
94 return result;
95}
96
98 std::string& title,
99 const std::string& overRideName,
100 const std::string& overRideTitle)
101{
102 if (!overRideName.empty())
103 {name = overRideName;}
104 if (!overRideTitle.empty())
105 {title = overRideTitle;}
106}
107
109 const std::string& overRideName,
110 const std::string& overRideTitle)
111{
112 TH1D* result = nullptr;
113 std::string name = d->histName;
114 std::string title = name;
115 CheckNameAndTitle(name, title, overRideName, overRideTitle);
116 const BinSpecification& xbs = d->xBinning;
117 if (xbs.edges)
118 {result = new TH1D(name.c_str(), title.c_str(), xbs.n, xbs.edges->data());}
119 else
120 {result = new TH1D(name.c_str(), title.c_str(), xbs.n, xbs.low, xbs.high);}
121 return result;
122}
123
125 const std::string& overRideName,
126 const std::string& overRideTitle)
127{
128 TH2D* result = nullptr;
129 std::string name = d->histName;
130 std::string title = name;
131 CheckNameAndTitle(name, title, overRideName, overRideTitle);
132
133 const BinSpecification& xbs = d->xBinning;
134 const BinSpecification& ybs = d->yBinning;
135 if (xbs.edges && ybs.edges)
136 {
137 result = new TH2D(name.c_str(), title.c_str(),
138 xbs.n, xbs.edges->data(),
139 ybs.n, ybs.edges->data());
140 }
141 else if (xbs.edges)
142 {
143 result = new TH2D(name.c_str(), title.c_str(),
144 xbs.n, xbs.edges->data(),
145 ybs.n, ybs.low, ybs.high);
146 }
147 else if (ybs.edges)
148 {
149 result = new TH2D(name.c_str(), title.c_str(),
150 xbs.n, xbs.low, xbs.high,
151 ybs.n, ybs.edges->data());
152 }
153 else
154 {
155 result = new TH2D(name.c_str(), title.c_str(),
156 xbs.n, xbs.low, xbs.high,
157 ybs.n, ybs.low, ybs.high);
158 }
159 return result;
160}
161
163 const std::string& overRideName,
164 const std::string& overRideTitle)
165{
166 TH3D* result = nullptr;
167 std::string name = d->histName;
168 std::string title = name;
169 CheckNameAndTitle(name, title, overRideName, overRideTitle);
170
171 const BinSpecification& xbs = d->xBinning;
172 const BinSpecification& ybs = d->yBinning;
173 const BinSpecification& zbs = d->zBinning;
174
175 if (xbs.edges || ybs.edges || zbs.edges)
176 {
177 std::vector<double> binEdgesX = xbs.edges ? *(xbs.edges) : RBDS::LinSpace(xbs.low, xbs.high, xbs.n);
178 std::vector<double> binEdgesY = ybs.edges ? *(ybs.edges) : RBDS::LinSpace(ybs.low, ybs.high, ybs.n);
179 std::vector<double> binEdgesZ = zbs.edges ? *(zbs.edges) : RBDS::LinSpace(zbs.low, zbs.high, zbs.n);
180 result = new TH3D(name.c_str(), title.c_str(),
181 xbs.n, binEdgesX.data(),
182 ybs.n, binEdgesY.data(),
183 zbs.n, binEdgesZ.data());
184 }
185 else
186 {
187 result = new TH3D(name.c_str(), title.c_str(),
188 xbs.n, xbs.low, xbs.high,
189 ybs.n, ybs.low, ybs.high,
190 zbs.n, zbs.low, zbs.high);
191 }
192 return result;
193}
194
195
196#ifdef USE_BOOST
198 const std::string& overRideName,
199 const std::string& overRideTitle)
200{
201 BDSBH4DBase* result = nullptr;
202
203 std::string name = d->histName;
204 std::string title = name;
205 CheckNameAndTitle(name, title, overRideName, overRideTitle);
206
207 const BinSpecification& xbs = d->xBinning;
208 const BinSpecification& ybs = d->yBinning;
209 const BinSpecification& zbs = d->zBinning;
210 const BinSpecification& ebs = d->eBinning;
211
212 std::vector<double> binEdgesE = ebs.edges ? *(ebs.edges) : RBDS::LinSpace(ebs.low, ebs.high, ebs.n);
213
214 if(d->eScale == "linear")
215 {
216 result = new BDSBH4D<boost_histogram_linear>(name, title, d->eScale,
217 xbs.n, xbs.low, xbs.high,
218 ybs.n, ybs.low, ybs.high,
219 zbs.n, zbs.low, zbs.high,
220 ebs.n, ebs.low, ebs.high);
221 }
222 else if(d->eScale == "log")
223 {
224 result = new BDSBH4D<boost_histogram_log>(name, title, d->eScale,
225 xbs.n, xbs.low, xbs.high,
226 ybs.n, ybs.low, ybs.high,
227 zbs.n, zbs.low, zbs.high,
228 ebs.n, ebs.low, ebs.high);
229 }
230 else if(d->eScale == "user")
231 {
232 result = new BDSBH4D<boost_histogram_variable>(name, title, d->eScale, binEdgesE,
233 xbs.n, xbs.low, xbs.high,
234 ybs.n, ybs.low, ybs.high,
235 zbs.n, zbs.low, zbs.high);
236 }
237
238 return result;
239}
240#else
241BDSBH4DBase* HistogramFactory::CreateHistogram4D(const HistogramDef4D*, const std::string&, const std::string&) {return nullptr;}
242#endif
Base class for the 4D histogram classes.
4D histogram classes with linear, logarithmic and user-defined energy binning.
Definition BDSBH4D.hh:40
Binning specification for a single dimension.
Specification for 1D histogram.
Specification for 2D histogram.
Specification for 3D Histogram.
Specification for 4D Histogram.
Common specification for a histogram.
Class to manufacture histograms.
void CheckNameAndTitle(std::string &name, std::string &title, const std::string &overRideName="", const std::string &overRideTitle="")
Whether name is set or not.
TH3D * CreateHistogram3D(const HistogramDef3D *d, const std::string &overRideName="", const std::string &overRideTitle="")
Create 3D histogram.
TH2D * CreateHistogram2D(const HistogramDef2D *d, const std::string &overRideName="", const std::string &overRideTitle="")
Create 2D histogram.
BDSBH4DBase * CreateHistogram4D(const HistogramDef4D *d, const std::string &overRideName="", const std::string &overRideTitle="")
Create 4D histogram.
TH1D * CreateHistogram1D(const HistogramDef1D *d, const std::string &overRideName="", const std::string &overRideTitle="")
Create 1D histogram.
TH1 * CreateHistogram(const HistogramDef *definition, const std::string &overRideName="", const std::string &overRideTitle="")
General exception with possible name of object and message.
std::vector< double > LinSpace(double start, double stop, int nBins, bool includeLastPoint=true)
Linear range of values.