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