BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
HistogramMeanFromFile.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 "HistogramAccumulator.hh"
20#include "HistogramMeanFromFile.hh"
21
22#include "BDSOutputROOTEventHistograms.hh"
23
24#include "TDirectory.h"
25#include "TH1D.h"
26#include "TH2D.h"
27#include "TH3D.h"
28#include "BDSBH4DBase.hh"
29
30#include "TTree.h"
31#include "TFile.h"
32
33#include <string>
34#include <vector>
35
37
39{;}
40
42{
43 for (auto hist : h->Get1DHistograms())
44 {
45 std::string name = std::string(hist->GetName());
46 std::string title = std::string(hist->GetTitle());
47 histograms1d.push_back(new HistogramAccumulator(hist, 1, name, title));
48 }
49
50 for (auto hist : h->Get2DHistograms())
51 {
52 std::string name = std::string(hist->GetName());
53 std::string title = std::string(hist->GetTitle());
54 histograms2d.push_back(new HistogramAccumulator(hist, 2, name, title));
55 }
56
57 for (auto hist : h->Get3DHistograms())
58 {
59 std::string name = std::string(hist->GetName());
60 std::string title = std::string(hist->GetTitle());
61 histograms3d.push_back(new HistogramAccumulator(hist, 3, name, title));
62 }
63
64 for (auto hist : h->Get4DHistograms())
65 {
66 std::string name = hist->GetName();
67 std::string title = hist->GetTitle();
68 histograms4d.push_back(new HistogramAccumulator(hist, 4, name, title));
69 }
70
71 Accumulate(h);
72}
73
74HistogramMeanFromFile::~HistogramMeanFromFile()
75{
76 for (auto h : histograms1d)
77 {delete h;}
78 for (auto h : histograms2d)
79 {delete h;}
80 for (auto h : histograms3d)
81 {delete h;}
82 for (auto h : histograms4d)
83 {delete h;}
84}
85
87{
88 auto h1i = hNew->Get1DHistograms();
89 for (unsigned int i = 0; i < (unsigned int)histograms1d.size(); ++i)
90 {histograms1d[i]->Accumulate(h1i[i]);}
91 auto h2i = hNew->Get2DHistograms();
92 for (unsigned int i = 0; i < (unsigned int)histograms2d.size(); ++i)
93 {histograms2d[i]->Accumulate(h2i[i]);}
94 auto h3i = hNew->Get3DHistograms();
95 for (unsigned int i = 0; i < (unsigned int)histograms3d.size(); ++i)
96 {histograms3d[i]->Accumulate(h3i[i]);}
97 auto h4i = hNew->Get4DHistograms();
98 for (unsigned int i = 0; i < (unsigned int)histograms4d.size(); ++i)
99 {histograms4d[i]->Accumulate(h4i[i]);}
100}
101
103{
104 // terminate each accumulator
105 // this returns a pointer to the result but no need to store
106 for (auto& h : histograms1d)
107 {h->Terminate();}
108 for (auto& h : histograms2d)
109 {h->Terminate();}
110 for (auto& h : histograms3d)
111 {h->Terminate();}
112 for (auto& h : histograms4d)
113 {h->Terminate();}
114}
115
116void HistogramMeanFromFile::Write(TDirectory* dir)
117{
118 if (dir)
119 {// move to directory in output file
120 for (auto& h : histograms1d)
121 {dir->Add(h->Result());}
122 for (auto& h : histograms2d)
123 {dir->Add(h->Result());}
124 for (auto& h : histograms3d)
125 {dir->Add(h->Result());}
126 for (auto& h : histograms4d)
127 {dir->Add(h->Result());}
128 }
129
130 // write to currently open file.
131 for (auto& h : histograms1d)
132 {h->Result()->Write();}
133 for (auto& h : histograms2d)
134 {h->Result()->Write();}
135 for (auto& h : histograms3d)
136 {h->Result()->Write();}
137 for (auto& h : histograms4d)
138 {h->Result()->Write();}
139}
140
Holder for a set of histograms to be stored.
std::vector< BDSBH4DBase * > & Get4DHistograms()
Accessors.
std::vector< TH2D * > & Get2DHistograms()
Accessors.
std::vector< TH3D * > & Get3DHistograms()
Accessors.
std::vector< TH1D * > & Get1DHistograms()
Accessors.
Class to accumulate and merge histograms in different ways.
Accumulator to merge pre-made per-entry histograms.
void Write(TDirectory *dir=nullptr)
Write to file.
HistogramMeanFromFile()
Public constructor only for compatibility with ROOT - not indended for use.
void Accumulate(BDSOutputROOTEventHistograms *hNew)
void Terminate()
Finish calculation.