BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
PerEntryHistogram.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 "HistogramDef.hh"
21#include "HistogramDef1D.hh"
22#include "HistogramDef2D.hh"
23#include "HistogramDef3D.hh"
24#include "HistogramDef4D.hh"
25#include "HistogramFactory.hh"
26#include "PerEntryHistogram.hh"
27#include "RBDSException.hh"
28
29#include "TChain.h"
30#include "TDirectory.h"
31#include "TH1.h"
32#include "TH1D.h"
33#include "TH2D.h"
34#include "TH3D.h"
35#include "BDSBH4DBase.hh"
36
37#include <string>
38
39ClassImp(PerEntryHistogram)
40
42 accumulator(nullptr),
43 chain(nullptr),
44 selection(""),
45 temp(nullptr),
46 result(nullptr),
47 command("")
48{;}
49
51 TChain* chainIn):
52 accumulator(nullptr),
53 chain(chainIn),
54 selection(definition->selection),
55 temp(nullptr),
56 result(nullptr),
57 command("")
58{
59 int nDimensions = definition->nDimensions;
60 TH1* baseHist = nullptr;
61 std::string histName = definition->histName;
62 std::string baseName = histName + "_base";
63 std::string tempName = histName + "Temp";
64 command = definition->variable + " >> " + tempName;
65
66 HistogramFactory factory;
67
68 switch (nDimensions)
69 {
70 case 1:
71 {
72 const HistogramDef1D* d = dynamic_cast<const HistogramDef1D*>(definition);
73 baseHist = factory.CreateHistogram1D(d, baseName, baseName);
74 temp = dynamic_cast<TH1D*>(baseHist->Clone(tempName.c_str()));
75 break;
76 }
77 case 2:
78 {
79 const HistogramDef2D* d = dynamic_cast<const HistogramDef2D*>(definition);
80 baseHist = factory.CreateHistogram2D(d, baseName, baseName);
81 temp = dynamic_cast<TH2D*>(baseHist->Clone(tempName.c_str()));
82 break;
83 }
84 case 3:
85 {
86 const HistogramDef3D* d = dynamic_cast<const HistogramDef3D*>(definition);
87 baseHist = factory.CreateHistogram3D(d, baseName, baseName);
88 temp = dynamic_cast<TH3D*>(baseHist->Clone(tempName.c_str()));
89 break;
90 }
91 case 4:
92 {
93 const HistogramDef4D* d = static_cast<const HistogramDef4D*>(definition);
94 baseHist = factory.CreateHistogram4D(d, baseName, baseName);
95 temp = dynamic_cast<BDSBH4DBase*>(baseHist->Clone(tempName.c_str()));
96 break;
97 }
98 default:
99 {throw RBDSException("Invalid number of dimensions"); break;}
100 }
101 if (temp)
102 {// technically, temp might be nullptr
103 temp->Reset();
104 temp->SetTitle(tempName.c_str());
105 }
106
107 accumulator = new HistogramAccumulator(baseHist, nDimensions, histName, histName);
108}
109
110PerEntryHistogram::~PerEntryHistogram()
111{
112 delete temp; // this removes it from the current ROOT file
113 delete accumulator;
114}
115
117{
118 // Fill the temporary histogram with 1 event - the current one
119 // This is used as it doesn't matter if the variable is a vector
120 // or singly valued - therefore we don't need to keep a map of
121 // which variables to loop over and which not to.
122 temp->Reset();
123 chain->Draw(command.c_str(), selection.c_str(), "goff", 1, entryNumber);
124 accumulator->Accumulate(temp);
125}
126
128{
129 result = accumulator->Terminate();
130}
131
132void PerEntryHistogram::Write(TDirectory* dir)
133{
134 if (result)
135 {
136 if (dir)
137 {dir->Add(result);}
138 result->Write();
139 }
140}
141
143{
144 if (!result)
145 {return 0;}
146 else
147 {return result->Integral();}
148}
Base class for the 4D histogram classes.
Definition: BDSBH4DBase.hh:37
Class to accumulate and merge histograms in different ways.
virtual void Accumulate(TH1 *newValue)
Specification for 1D histogram.
Specification for 2D histogram.
Specification for 3D Histogram.
Specification for 4D Histogram.
Common specification for a histogram.
Definition: HistogramDef.hh:34
Class to manufacture histograms.
TH3D * CreateHistogram3D(const HistogramDef3D *d, const std::string &overRideName="", const std::string &overRideTitle="")
Create 3D histogram.
TH2D * CreateHistogram2D(const HistogramDef2D *d, const std::string &overRideName="", const std::string &overRideTitle="")
Create 2D histogram.
BDSBH4DBase * CreateHistogram4D(const HistogramDef4D *d, const std::string &overRideName="", const std::string &overRideTitle="")
Create 4D histogram.
TH1D * CreateHistogram1D(const HistogramDef1D *d, const std::string &overRideName="", const std::string &overRideTitle="")
Create 1D histogram.
Holder for information to calculate per entry histograms.
TChain * chain
Cache of chain pointer that provides data.
virtual void Terminate()
Terminate the accumulator and save the result to the result member variable.
double Integral() const
Get the Integral() from the result member histogram if it exists, otherwise 0.
PerEntryHistogram()
Public constructor only for compatibility with ROOT - not intended for use.
virtual void Write(TDirectory *dir=nullptr)
std::string selection
Selection command.
virtual void AccumulateCurrentEntry(long int entryNumber)
TH1 * result
Final result with errors as the error on the mean.
TH1 * temp
Histogram for temporary 1 event data.
std::string command
Draw command.
General exception with possible name of object and message.