BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
Analysis.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 "Analysis.hh"
19#include "BinGeneration.hh"
20#include "Config.hh"
21#include "HistogramDef.hh"
22#include "HistogramDef1D.hh"
23#include "HistogramDef2D.hh"
24#include "HistogramDef3D.hh"
25#include "HistogramDef4D.hh"
26#include "HistogramFactory.hh"
27#include "HistogramMeanFromFile.hh"
28#include "PerEntryHistogram.hh"
29#include "rebdsim.hh"
30
31#include "TChain.h"
32#include "TFile.h"
33#include "TH1D.h"
34#include "TH2D.h"
35#include "TH3D.h"
36#include "BDSBH4DBase.hh"
37
38#include <iostream>
39#include <string>
40#include <vector>
41
42Analysis::Analysis(const std::string& treeNameIn,
43 TChain* chainIn,
44 const std::string& mergedHistogramNameIn,
45 bool perEntryAnalysis,
46 bool debugIn):
47 treeName(treeNameIn),
48 chain(chainIn),
49 mergedHistogramName(mergedHistogramNameIn),
50 histoSum(nullptr),
51 debug(debugIn),
52 entries(chain->GetEntries()),
53 perEntry(perEntryAnalysis)
54{;}
55
56Analysis::~Analysis()
57{
58 delete histoSum;
59 for (auto pe : perEntryHistograms)
60 {delete pe;}
61}
62
64{
65 std::cout << "Analysis on \"" << treeName << "\" beginning" << std::endl;
66 if (perEntry)
67 {
68 // ensure new histograms are added to file
69 // crucial for draw command to work as it identifies the histograms by name
70 TH1::AddDirectory(kTRUE);
71 TH2::AddDirectory(kTRUE);
72 TH3::AddDirectory(kTRUE);
73 BDSBH4DBase::AddDirectory(kTRUE);
75 Process();
76 }
78 Terminate();
79 std::cout << "Analysis on \"" << treeName << "\" complete" << std::endl;
80}
81
84
86{
87 // loop over histogram specifications and fill
88 // TODO - in future we should avoid the singleton accessor as rebdsimOptics
89 // doesn't use it but uses the event analysis.
90 auto c = Config::Instance();
91 if (c)
92 {
93 auto definitions = Config::Instance()->HistogramDefinitionsSimple(treeName);
94 for (auto definition : definitions)
95 {FillHistogram(definition);}
96 }
97}
98
100{
101 auto c = Config::Instance();
102 if (c)
103 {
104 const auto& definitions = c->HistogramDefinitionsPerEntry(treeName);
105 for (const auto& def : definitions)
106 {perEntryHistograms.push_back(new PerEntryHistogram(def, chain));}
107 }
108}
109
111{
112 for (auto& peHist : perEntryHistograms)
113 {peHist->AccumulateCurrentEntry(entryNumber);}
114}
115
117{
118 for (auto& peHist : perEntryHistograms)
119 {peHist->Terminate();}
120}
121
123{
124 if (histoSum)
125 {histoSum->Terminate();}
126 if (perEntry)
128}
129
130void Analysis::Write(TFile* outputFile)
131{
132 // treeName typically has a "." at the end, deleting it here:
133 std::string cleanedName = treeName.erase(treeName.size() - 1);
134 std::string perEntryDirName = "PerEntryHistograms";
135 std::string simpleDirName = "SimpleHistograms";
136 std::string mergedDirName = "MergedHistograms";
137 TDirectory* rebdsimDir = outputFile->mkdir(cleanedName.c_str());
138 TDirectory* perEntryDir = rebdsimDir->mkdir(perEntryDirName.c_str());
139 TDirectory* simpleDir = rebdsimDir->mkdir(simpleDirName.c_str());
140 TDirectory* mergedDir = rebdsimDir->mkdir(mergedDirName.c_str());
141
142 // per entry histograms
143 perEntryDir->cd();
144 for (auto h : perEntryHistograms)
145 {h->Write(perEntryDir);}
146
147 // simple histograms
148 simpleDir->cd();
149 for (auto& h : simpleHistograms)
150 {simpleDir->Add(h);}
151 for (auto& h : simpleHistograms)
152 {h->Write();}
153
154 // merged histograms
155 if (histoSum)
156 {
157 mergedDir->cd();
158 std::cout << "Merging histograms from \"" << treeName << "\" analysis" << std::endl;
159 histoSum->Write(mergedDir);
160 }
161
162 outputFile->cd("/"); // return to root of the file
163}
164
166 std::vector<TH1*>* outputHistograms)
167{
168 // ensure new histograms are added to file..
169 // this is crucial for the draw command to work as it finds the histograms by name
170 TH1::AddDirectory(kTRUE);
171 TH2::AddDirectory(kTRUE);
172 TH3::AddDirectory(kTRUE);
173 BDSBH4DBase::AddDirectory(kTRUE);
174
175
176 // pull out communal information in base class
177 std::string name = definition->histName;
178 std::string command = definition->variable + " >> " + definition->histName;
179 std::string selection = definition->selection;
180
181 HistogramFactory factory;
182 TH1* h = factory.CreateHistogram(definition);
183 chain->Draw(command.c_str(), selection.c_str(),"goff");
184
185 if (outputHistograms)
186 {outputHistograms->push_back(h);}
187 else
188 {simpleHistograms.push_back(h);}
189
190}
virtual void Execute()
Method which calls all other methods in order.
Definition Analysis.cc:63
virtual void Terminate()
Definition Analysis.cc:122
virtual void SimpleHistograms()
Process histogram definitions from configuration instance.
Definition Analysis.cc:85
void PreparePerEntryHistograms()
Create structures necessary for per entry histograms.
Definition Analysis.cc:99
HistogramMeanFromFile * histoSum
Merge of per event stored histograms.
Definition Analysis.hh:96
void AccumulatePerEntryHistograms(long int entryNumber)
Accumulate means and variances for per entry histograms.
Definition Analysis.cc:110
virtual void Write(TFile *outputFile)
Write rebdsim histograms.
Definition Analysis.cc:130
Analysis()=delete
No default constructor for this base class.
void TerminatePerEntryHistograms()
Prepare result of per entry histogram accumulation.
Definition Analysis.cc:116
virtual void UserProcess()
Virtual function for user to overload and use. Does nothing by default.
Definition Analysis.cc:82
bool perEntry
Whether to analyse each entry in the tree in a for loop or not.
Definition Analysis.hh:99
void FillHistogram(HistogramDef *definition, std::vector< TH1 * > *outputHistograms=nullptr)
Create an individual histogram based on a definition.
Definition Analysis.cc:165
virtual void Process()=0
static Config * Instance(const std::string &fileName="", const std::string &inputFilePath="", const std::string &outputFileName="", const std::string &defaultOutputFileSuffix="_ana")
Singleton accessor.
Definition Config.cc:153
const std::vector< HistogramDef * > & HistogramDefinitionsSimple(const std::string &treeName) const
Access all simple histogram definitions - throws exception if out of range.
Definition Config.hh:94
Common specification for a histogram.
Class to manufacture histograms.
TH1 * CreateHistogram(const HistogramDef *definition, const std::string &overRideName="", const std::string &overRideTitle="")
void Write(TDirectory *dir=nullptr)
Write to file.
void Terminate()
Finish calculation.
Holder for information to calculate per entry histograms.