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) 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 "HistogramAccumulator.hh"
19#include "HistogramMeanFromFile.hh"
20
21#include "BDSOutputROOTEventHistograms.hh"
22
23#include "TDirectory.h"
24#include "TH1D.h"
25#include "TH2D.h"
26#include "TH3D.h"
27#include "BDSBH4DBase.hh"
28
29#include "TTree.h"
30#include "TFile.h"
31
32#include <string>
33#include <vector>
34
36
38{;}
39
41{
42 for (auto hist : h->Get1DHistograms())
43 {
44 std::string name = std::string(hist->GetName());
45 std::string title = std::string(hist->GetTitle());
46 histograms1d.push_back(new HistogramAccumulator(hist, 1, name, title));
47 }
48
49 for (auto hist : h->Get2DHistograms())
50 {
51 std::string name = std::string(hist->GetName());
52 std::string title = std::string(hist->GetTitle());
53 histograms2d.push_back(new HistogramAccumulator(hist, 2, name, title));
54 }
55
56 for (auto hist : h->Get3DHistograms())
57 {
58 std::string name = std::string(hist->GetName());
59 std::string title = std::string(hist->GetTitle());
60 histograms3d.push_back(new HistogramAccumulator(hist, 3, name, title));
61 }
62
63 for (auto hist : h->Get4DHistograms())
64 {
65 std::string name = hist->GetName();
66 std::string title = hist->GetTitle();
67 histograms4d.push_back(new HistogramAccumulator(hist, 4, name, title));
68 }
69
70 Accumulate(h);
71}
72
73HistogramMeanFromFile::~HistogramMeanFromFile()
74{
75 for (auto h : histograms1d)
76 {delete h;}
77 for (auto h : histograms2d)
78 {delete h;}
79 for (auto h : histograms3d)
80 {delete h;}
81 for (auto h : histograms4d)
82 {delete h;}
83}
84
86{
87 auto h1i = hNew->Get1DHistograms();
88 for (unsigned int i = 0; i < (unsigned int)histograms1d.size(); ++i)
89 {histograms1d[i]->Accumulate(h1i[i]);}
90 auto h2i = hNew->Get2DHistograms();
91 for (unsigned int i = 0; i < (unsigned int)histograms2d.size(); ++i)
92 {histograms2d[i]->Accumulate(h2i[i]);}
93 auto h3i = hNew->Get3DHistograms();
94 for (unsigned int i = 0; i < (unsigned int)histograms3d.size(); ++i)
95 {histograms3d[i]->Accumulate(h3i[i]);}
96 auto h4i = hNew->Get4DHistograms();
97 for (unsigned int i = 0; i < (unsigned int)histograms4d.size(); ++i)
98 {histograms4d[i]->Accumulate(h4i[i]);}
99}
100
102{
103 // terminate each accumulator
104 // this returns a pointer to the result but no need to store
105 for (auto& h : histograms1d)
106 {h->Terminate();}
107 for (auto& h : histograms2d)
108 {h->Terminate();}
109 for (auto& h : histograms3d)
110 {h->Terminate();}
111 for (auto& h : histograms4d)
112 {h->Terminate();}
113}
114
115void HistogramMeanFromFile::Write(TDirectory* dir)
116{
117 if (dir)
118 {// move to directory in output file
119 for (auto& h : histograms1d)
120 {dir->Add(h->Result());}
121 for (auto& h : histograms2d)
122 {dir->Add(h->Result());}
123 for (auto& h : histograms3d)
124 {dir->Add(h->Result());}
125 for (auto& h : histograms4d)
126 {dir->Add(h->Result());}
127 }
128
129 // write to currently open file.
130 for (auto& h : histograms1d)
131 {h->Result()->Write();}
132 for (auto& h : histograms2d)
133 {h->Result()->Write();}
134 for (auto& h : histograms3d)
135 {h->Result()->Write();}
136 for (auto& h : histograms4d)
137 {h->Result()->Write();}
138}
139
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.