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