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) Royal Holloway,
3University of London 2001 - 2024.
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,
108 G4String title,
109 std::vector<double>& edges)
110{
111 histograms1D.push_back(new TH1D(name, title, (Int_t)edges.size()-1, edges.data()));
112 return (G4int)histograms1D.size() - 1;
113}
114
115G4int BDSOutputROOTEventHistograms::Create2DHistogram(G4String name, G4String title,
116 G4int nxbins, G4double xmin, G4double xmax,
117 G4int nybins, G4double ymin, G4double ymax)
118{
119 histograms2D.push_back(new TH2D(name,title, nxbins, xmin, xmax, nybins, ymin, ymax));
120 return (G4int)histograms2D.size() - 1;
121}
122
123G4int BDSOutputROOTEventHistograms::Create2DHistogram(G4String name, G4String title,
124 std::vector<double>& xedges,
125 std::vector<double>& yedges)
126{
127 histograms2D.push_back(new TH2D(name.data(),title.data(),
128 (Int_t)xedges.size()-1, xedges.data(),
129 (Int_t)yedges.size()-1, yedges.data()));
130 return (G4int)histograms2D.size() - 1;
131}
132
133G4int BDSOutputROOTEventHistograms::Create3DHistogram(G4String name, G4String title,
134 G4int nxbins, G4double xmin, G4double xmax,
135 G4int nybins, G4double ymin, G4double ymax,
136 G4int nzbins, G4double zmin, G4double zmax)
137{
138 histograms3D.push_back(new TH3D(name, title,
139 nxbins, xmin, xmax,
140 nybins, ymin, ymax,
141 nzbins, zmin, zmax));
142 return (G4int)histograms3D.size() - 1;
143}
144
145G4int BDSOutputROOTEventHistograms::Create3DHistogram(G4String name, G4String title,
146 std::vector<double>& xedges,
147 std::vector<double>& yedges,
148 std::vector<double>& zedges)
149{
150 histograms3D.push_back(new TH3D(name.data(),title.data(),
151 (Int_t)xedges.size()-1, xedges.data(),
152 (Int_t)yedges.size()-1, yedges.data(),
153 (Int_t)zedges.size()-1, zedges.data()));
154 return (G4int)histograms3D.size() - 1;
155}
156
157#ifdef USE_BOOST
158G4int BDSOutputROOTEventHistograms::Create4DHistogram(const G4String& name,
159 const G4String& title,
160 const G4String& eScale,
161 const std::vector<double>& eBinsEdges,
162 unsigned int nxbins, G4double xmin, G4double xmax,
163 unsigned int nybins, G4double ymin, G4double ymax,
164 unsigned int nzbins, G4double zmin, G4double zmax,
165 unsigned int nebins, G4double emin, G4double emax)
166{
167 std::string nameC = (std::string)name;
168 std::string titleC = (std::string)title;
169 std::string eScaleC = (std::string)eScale;
170
171 if(eScale == "linear")
172 {
173 histograms4D.push_back(new BDSBH4D<boost_histogram_linear>(nameC, titleC, eScaleC,
174 nxbins, xmin, xmax,
175 nybins, ymin, ymax,
176 nzbins, zmin, zmax,
177 nebins, emin, emax));
178 }
179 else if(eScale == "log")
180 {
181 histograms4D.push_back(new BDSBH4D<boost_histogram_log>(nameC, titleC, eScaleC,
182 nxbins, xmin, xmax,
183 nybins, ymin, ymax,
184 nzbins, zmin, zmax,
185 nebins, emin, emax));
186 }
187 else if(eScale == "user")
188 {
189 histograms4D.push_back(new BDSBH4D<boost_histogram_variable>(nameC, titleC, eScaleC, eBinsEdges,
190 nxbins, xmin, xmax,
191 nybins, ymin, ymax,
192 nzbins, zmin, zmax));
193 }
194
195 return (G4int)histograms4D.size() - 1;
196}
197#else
198G4int BDSOutputROOTEventHistograms::Create4DHistogram(const G4String&, const G4String&, const G4String&,
199 const std::vector<double>&,
200 unsigned int, G4double, G4double,
201 unsigned int, G4double, G4double,
202 unsigned int, G4double, G4double,
203 unsigned int, G4double, G4double)
204{
205 throw BDSException(__METHOD_NAME__, "BDSIM compiled without BOOST support -> no 4D histograms.");
206}
207#endif
208
209void BDSOutputROOTEventHistograms::Fill1DHistogram(G4int histoId,
210 G4double value,
211 G4double weight)
212{
213 histograms1D[histoId]->Fill(value,weight);
214}
215
216void BDSOutputROOTEventHistograms::Fill2DHistogram(G4int histoId,
217 G4double xValue,
218 G4double yValue,
219 G4double weight)
220{
221 histograms2D[histoId]->Fill(xValue,yValue,weight);
222}
223
224void BDSOutputROOTEventHistograms::Fill3DHistogram(G4int histoId,
225 G4double xValue,
226 G4double yValue,
227 G4double zValue,
228 G4double weight)
229{
230 histograms3D[histoId]->Fill(xValue,yValue,zValue,weight);
231}
232
233#ifdef USE_BOOST
234void BDSOutputROOTEventHistograms::Fill4DHistogram(G4int histoId,
235 G4double xValue,
236 G4double yValue,
237 G4double zValue,
238 G4double eValue)
239{
240 histograms4D[histoId]->Fill_BDSBH4D(xValue, yValue, zValue, eValue);
241}
242#else
243void BDSOutputROOTEventHistograms::Fill4DHistogram(G4int,
244 G4double,
245 G4double,
246 G4double,
247 G4double)
248{
249 throw BDSException(__METHOD_NAME__, "BDSIM compiled without BOOST support -> no 4D histograms.");
250}
251#endif
252
254 G4int globalBinID,
255 G4double value)
256{
257 histograms3D[histoId]->SetBinContent(globalBinID, value);
258}
259
260#ifdef USE_BOOST
261void BDSOutputROOTEventHistograms::Set4DHistogramBinContent(G4int histoId,
262 G4int x,
263 G4int y,
264 G4int z,
265 G4int e,
266 G4double value)
267{
268 histograms4D[histoId]->Set_BDSBH4D(x, y, z, e, value);
269}
270#else
271void BDSOutputROOTEventHistograms::Set4DHistogramBinContent(G4int, G4int, G4int, G4int, G4int, G4double)
272{
273 throw BDSException(__METHOD_NAME__, "BDSIM compiled without BOOST support -> no 4D histograms.");
274}
275#endif
276
278 TH3D* otherHistogram)
279{
280 histograms3D[histoId]->Add(otherHistogram);
281}
282
283void BDSOutputROOTEventHistograms::AccumulateHistogram4D(G4int histoId,
284 BDSBH4DBase* otherHistogram)
285{
286 *histograms4D[histoId] += *otherHistogram;
287}
288
289#endif
290
292{
293 for (auto h : histograms1D)
294 {h->Reset();}
295 for (auto h : histograms2D)
296 {h->Reset();}
297 for (auto h : histograms3D)
298 {h->Reset();}
299#ifdef USE_BOOST
300 for (auto h : histograms4D)
301 {h->Reset_BDSBH4D();}
302#endif
303}
Base class for the 4D histogram classes.
4D histogram classes with linear, logarithmic and user-defined energy binning.
Definition BDSBH4D.hh:41
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)