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