BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSOutputROOTEventHistograms.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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 "BDSOutputROOTEventHistograms.hh"
20
21#include "TH1D.h"
22#include "TH2D.h"
23#include "TH3D.h"
24#include "BDSBH4DBase.hh"
25#include "BDSBH4D.hh"
26
27#ifndef USE_BOOST
28#include "BDSBH4DTypeDefs.hh"
29#include "BDSDebug.hh"
30#include "BDSException.hh"
31#endif
32
34
35BDSOutputROOTEventHistograms::BDSOutputROOTEventHistograms()
36{
37 TH1D::AddDirectory(kFALSE);
38 TH2D::AddDirectory(kFALSE);
39 TH3D::AddDirectory(kFALSE);
40}
41
42BDSOutputROOTEventHistograms::BDSOutputROOTEventHistograms(const BDSOutputROOTEventHistograms& rhs):
43 TObject(rhs)
44{
45 Fill(&rhs);
46}
47
48BDSOutputROOTEventHistograms::BDSOutputROOTEventHistograms(std::vector<TH1D*>& histograms1DIn,
49 std::vector<TH2D*>& histograms2DIn,
50 std::vector<TH3D*>& histograms3DIn,
51 std::vector<BDSBH4DBase*>& histograms4DIn):
52 histograms1D(histograms1DIn),
53 histograms2D(histograms2DIn),
54 histograms3D(histograms3DIn),
55 histograms4D(histograms4DIn)
56{;}
57
58BDSOutputROOTEventHistograms::~BDSOutputROOTEventHistograms()
59{;}
60
62{
63 if (!rhs)
64 {return;}
65
66 histograms1D = rhs->histograms1D;
67 histograms2D = rhs->histograms2D;
68 histograms3D = rhs->histograms3D;
69 histograms4D = rhs->histograms4D;
70}
71
73{
74 if (!rhs)
75 {return;}
76
77 // for each histogram, clone (ie copy) it into this object
78 for (auto h : rhs->histograms1D)
79 {histograms1D.push_back(static_cast<TH1D*>(h->Clone()));}
80 for (auto h : rhs->histograms2D)
81 {histograms2D.push_back(static_cast<TH2D*>(h->Clone()));}
82 for (auto h : rhs->histograms3D)
83 {histograms3D.push_back(static_cast<TH3D*>(h->Clone()));}
84#ifdef USE_BOOST
85 for (auto h : rhs->histograms4D)
86 {histograms4D.push_back(static_cast<BDSBH4DBase*>(h->Clone("")));}
87#endif
88
89}
90
91int BDSOutputROOTEventHistograms::Create1DHistogramSTD(std::string name, std::string title,
92 int nbins, double xmin, double xmax)
93{
94 histograms1D.push_back(new TH1D(name.c_str(),title.c_str(), nbins, xmin, xmax));
95 return (int)histograms1D.size() - 1;
96}
97
98#ifndef __ROOTBUILD__
99
100G4int BDSOutputROOTEventHistograms::Create1DHistogram(G4String name, G4String title,
101 G4int nbins, G4double xmin, G4double xmax)
102{
103 histograms1D.push_back(new TH1D(name,title, nbins, xmin, xmax));
104 return (G4int)histograms1D.size() - 1;
105}
106
107G4int BDSOutputROOTEventHistograms::Create1DHistogram(G4String name, G4String title,
108 std::vector<double>& edges)
109{
110
111 Double_t* edgesD = new Double_t[edges.size()];
112 for (int i=0;i<(int)edges.size();++i)
113 {edgesD[i] = edges[i];}
114
115 histograms1D.push_back(new TH1D(name,title,(Int_t)edges.size()-1,edgesD));
116 delete[] edgesD;
117
118 return (G4int)histograms1D.size() - 1;
119}
120
121G4int BDSOutputROOTEventHistograms::Create2DHistogram(G4String name, G4String title,
122 G4int nxbins, G4double xmin, G4double xmax,
123 G4int nybins, G4double ymin, G4double ymax)
124{
125 histograms2D.push_back(new TH2D(name,title, nxbins, xmin, xmax, nybins, ymin, ymax));
126 return (G4int)histograms2D.size() - 1;
127}
128
129G4int BDSOutputROOTEventHistograms::Create2DHistogram(G4String name, G4String title,
130 std::vector<double>& xedges,
131 std::vector<double>& yedges)
132{
133 Double_t* xedgesD = new Double_t[xedges.size()];
134 for (int i=0;i<(int)xedges.size();++i)
135 {xedgesD[i] = xedges[i];}
136
137 Double_t* yedgesD = new Double_t[yedges.size()];
138 for (int i=0;i<(int)yedges.size();++i)
139 {yedgesD[i] = yedges[i];}
140
141 histograms2D.push_back(new TH2D(name.data(),title.data(), (Int_t)xedges.size()-1, xedgesD, (Int_t)yedges.size()-1, yedgesD));
142 delete[] xedgesD;
143 delete[] yedgesD;
144
145 return (G4int)histograms2D.size() - 1;
146}
147
148G4int BDSOutputROOTEventHistograms::Create3DHistogram(G4String name, G4String title,
149 G4int nxbins, G4double xmin, G4double xmax,
150 G4int nybins, G4double ymin, G4double ymax,
151 G4int nzbins, G4double zmin, G4double zmax)
152{
153 histograms3D.push_back(new TH3D(name, title,
154 nxbins, xmin, xmax,
155 nybins, ymin, ymax,
156 nzbins, zmin, zmax));
157 return (G4int)histograms3D.size() - 1;
158}
159
160G4int BDSOutputROOTEventHistograms::Create3DHistogram(G4String name, G4String title,
161 std::vector<double>& xedges,
162 std::vector<double>& yedges,
163 std::vector<double>& zedges)
164{
165 Double_t* xedgesD = new Double_t[xedges.size()];
166 for (int i=0;i<(int)xedges.size();++i)
167 {xedgesD[i] = xedges[i];}
168
169 Double_t* yedgesD = new Double_t[yedges.size()];
170 for (int i=0;i<(int)yedges.size();++i)
171 {yedgesD[i] = yedges[i];}
172
173 Double_t* zedgesD = new Double_t[zedges.size()];
174 for (int i=0;i<(int)zedges.size();++i)
175 {zedgesD[i] = zedges[i];}
176
177 histograms3D.push_back(new TH3D(name.data(),title.data(),
178 (Int_t)xedges.size()-1, xedgesD,
179 (Int_t)yedges.size()-1, yedgesD,
180 (Int_t)zedges.size()-1, zedgesD));
181 return (G4int)histograms3D.size() - 1;
182}
183
184#ifdef USE_BOOST
185G4int BDSOutputROOTEventHistograms::Create4DHistogram(const G4String& name,
186 const G4String& title,
187 const G4String& eScale,
188 const std::vector<double>& eBinsEdges,
189 unsigned int nxbins, G4double xmin, G4double xmax,
190 unsigned int nybins, G4double ymin, G4double ymax,
191 unsigned int nzbins, G4double zmin, G4double zmax,
192 unsigned int nebins, G4double emin, G4double emax)
193{
194 std::string nameC = (std::string)name;
195 std::string titleC = (std::string)title;
196 std::string eScaleC = (std::string)eScale;
197
198 if(eScale == "linear")
199 {
200 histograms4D.push_back(new BDSBH4D<boost_histogram_linear>(nameC, titleC, eScaleC,
201 nxbins, xmin, xmax,
202 nybins, ymin, ymax,
203 nzbins, zmin, zmax,
204 nebins, emin, emax));
205 }
206 else if(eScale == "log")
207 {
208 histograms4D.push_back(new BDSBH4D<boost_histogram_log>(nameC, titleC, eScaleC,
209 nxbins, xmin, xmax,
210 nybins, ymin, ymax,
211 nzbins, zmin, zmax,
212 nebins, emin, emax));
213 }
214 else if(eScale == "user")
215 {
216 histograms4D.push_back(new BDSBH4D<boost_histogram_variable>(nameC, titleC, eScaleC, eBinsEdges,
217 nxbins, xmin, xmax,
218 nybins, ymin, ymax,
219 nzbins, zmin, zmax));
220 }
221
222 return (G4int)histograms4D.size() - 1;
223}
224#else
225G4int BDSOutputROOTEventHistograms::Create4DHistogram(const G4String&, const G4String&, const G4String&,
226 const std::vector<double>&,
227 unsigned int, G4double, G4double,
228 unsigned int, G4double, G4double,
229 unsigned int, G4double, G4double,
230 unsigned int, G4double, G4double)
231{
232 throw BDSException(__METHOD_NAME__, "BDSIM compiled without BOOST support -> no 4D histograms.");
233}
234#endif
235
236void BDSOutputROOTEventHistograms::Fill1DHistogram(G4int histoId,
237 G4double value,
238 G4double weight)
239{
240 histograms1D[histoId]->Fill(value,weight);
241}
242
243void BDSOutputROOTEventHistograms::Fill2DHistogram(G4int histoId,
244 G4double xValue,
245 G4double yValue,
246 G4double weight)
247{
248 histograms2D[histoId]->Fill(xValue,yValue,weight);
249}
250
251void BDSOutputROOTEventHistograms::Fill3DHistogram(G4int histoId,
252 G4double xValue,
253 G4double yValue,
254 G4double zValue,
255 G4double weight)
256{
257 histograms3D[histoId]->Fill(xValue,yValue,zValue,weight);
258}
259
260#ifdef USE_BOOST
261void BDSOutputROOTEventHistograms::Fill4DHistogram(G4int histoId,
262 G4double xValue,
263 G4double yValue,
264 G4double zValue,
265 G4double eValue)
266{
267 histograms4D[histoId]->Fill_BDSBH4D(xValue, yValue, zValue, eValue);
268}
269#else
270void BDSOutputROOTEventHistograms::Fill4DHistogram(G4int,
271 G4double,
272 G4double,
273 G4double,
274 G4double)
275{
276 throw BDSException(__METHOD_NAME__, "BDSIM compiled without BOOST support -> no 4D histograms.");
277}
278#endif
279
281 G4int globalBinID,
282 G4double value)
283{
284 histograms3D[histoId]->SetBinContent(globalBinID, value);
285}
286
287#ifdef USE_BOOST
288void BDSOutputROOTEventHistograms::Set4DHistogramBinContent(G4int histoId,
289 G4int x,
290 G4int y,
291 G4int z,
292 G4int e,
293 G4double value)
294{
295 histograms4D[histoId]->Set_BDSBH4D(x, y, z, e, value);
296}
297#else
298void BDSOutputROOTEventHistograms::Set4DHistogramBinContent(G4int, G4int, G4int, G4int, G4int, G4double)
299{
300 throw BDSException(__METHOD_NAME__, "BDSIM compiled without BOOST support -> no 4D histograms.");
301}
302#endif
303
305 TH3D* otherHistogram)
306{
307 histograms3D[histoId]->Add(otherHistogram);
308}
309
310void BDSOutputROOTEventHistograms::AccumulateHistogram4D(G4int histoId,
311 BDSBH4DBase* otherHistogram)
312{
313 *histograms4D[histoId] += *otherHistogram;
314}
315
316#endif
317
319{
320 for (auto h : histograms1D)
321 {h->Reset();}
322 for (auto h : histograms2D)
323 {h->Reset();}
324 for (auto h : histograms3D)
325 {h->Reset();}
326#ifdef USE_BOOST
327 for (auto h : histograms4D)
328 {h->Reset_BDSBH4D();}
329#endif
330}
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
General exception with possible name of object and message.
Definition: BDSException.hh:35
Holder for a set of histograms to be stored.
virtual void Flush()
Flush the contents.
void AccumulateHistogram3D(G4int histoId, TH3D *otherHistogram)
Add the values from one supplied 3D histogram to another. Uses TH3-Add().
int Create1DHistogramSTD(std::string name, std::string title, int nbins, double xmin, double xmax)
Interface function to create a 1D histogram using only standard types.
void Fill(const BDSOutputROOTEventHistograms *rhs)
Copy (using the TH->Clone) method from another instance.
void FillSimple(const BDSOutputROOTEventHistograms *rhs)
Copy (without using the TH->Clone) method from another instance. (Quicker).
void Set3DHistogramBinContent(G4int histoId, G4int globalBinID, G4double value)