BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSOutputROOT.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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 "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSGlobalConstants.hh"
22#include "BDSOutputROOT.hh"
23#include "BDSOutputROOTEventAperture.hh"
24#include "BDSOutputROOTEventBeam.hh"
25#include "BDSOutputROOTEventCollimator.hh"
26#include "BDSOutputROOTEventCoords.hh"
27#include "BDSOutputROOTEventLossWorld.hh"
28#include "BDSOutputROOTEventHeader.hh"
29#include "BDSOutputROOTEventHistograms.hh"
30#include "BDSOutputROOTEventInfo.hh"
31#include "BDSOutputROOTEventLoss.hh"
32#include "BDSOutputROOTEventModel.hh"
33#include "BDSOutputROOTEventOptions.hh"
34#include "BDSOutputROOTEventRunInfo.hh"
35#include "BDSOutputROOTEventSampler.hh"
36#include "BDSOutputROOTEventSamplerC.hh"
37#include "BDSOutputROOTEventSamplerS.hh"
38#include "BDSOutputROOTEventTrajectory.hh"
39#include "BDSOutputROOTParticleData.hh"
40
41#include "parser/options.h"
42
43#include "TFile.h"
44#include "TObject.h"
45#include "TTree.h"
46
47BDSOutputROOT::BDSOutputROOT(const G4String& fileName,
48 G4int fileNumberOffset,
49 G4int compressionLevelIn):
50 BDSOutput(fileName, ".root", fileNumberOffset),
51 compressionLevel(compressionLevelIn),
52 theRootOutputFile(nullptr),
53 theHeaderOutputTree(nullptr),
54 theParticleDataTree(nullptr),
55 theBeamOutputTree(nullptr),
56 theOptionsOutputTree(nullptr),
57 theModelOutputTree(nullptr),
58 theEventOutputTree(nullptr),
59 theRunOutputTree(nullptr)
60{;}
61
62BDSOutputROOT::~BDSOutputROOT()
63{
64 Close();
65}
66
68{
69 G4String newFileName = GetNextFileName();
70
71 theRootOutputFile = new TFile(newFileName,"RECREATE", "BDS output file");
72 if (theRootOutputFile->IsZombie())
73 {throw BDSException(__METHOD_NAME__, "Unable to open output file: \"" + newFileName +"\"");}
74
75 if (compressionLevel > 9 || compressionLevel < -1)
76 {throw BDSException(__METHOD_NAME__, "invalid ROOT compression level (" + std::to_string(compressionLevel) + ") must be 0 - 9.");}
77 if (compressionLevel > -1)
78 {theRootOutputFile->SetCompressionLevel(compressionLevel);}
79
80 // root file - note this sets the current 'directory' to this file!
82
83 theHeaderOutputTree = new TTree("Header", "BDSIM Header"); // header
84 theParticleDataTree = new TTree("ParticleData", "Particle Data"); // particle data
85 theBeamOutputTree = new TTree("Beam", "BDSIM beam"); // beam data tree
86 theOptionsOutputTree = new TTree("Options","BDSIM options"); // options data tree
87 theModelOutputTree = new TTree("Model","BDSIM model"); // model data tree
88 theRunOutputTree = new TTree("Run","BDSIM run histograms/information"); // run info tree
89 theEventOutputTree = new TTree("Event","BDSIM event"); // event data tree
90
91 // Build branches for each object
92 theHeaderOutputTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOutput, 32000, 1);
93 theParticleDataTree->Branch("ParticleData.", "BDSOutputROOTParticleData", particleDataOutput,32000, 1);
94 theBeamOutputTree->Branch("Beam.", "BDSOutputROOTEventBeam", beamOutput, 32000, 2);
95 theOptionsOutputTree->Branch("Options.", "BDSOutputROOTEventOptions", optionsOutput, 32000, 2);
96 theModelOutputTree->Branch("Model.", "BDSOutputROOTEventModel", modelOutput, 32000, 1);
97 theRunOutputTree->Branch("Histos.", "BDSOutputROOTEventHistograms",runHistos, 32000, 1);
98 theRunOutputTree->Branch("Summary.", "BDSOutputROOTEventRunInfo", runInfo, 32000, 1);
99
100 // Branches for event...
101 // Event info output
102 theEventOutputTree->Branch("Summary.", "BDSOutputROOTEventInfo",evtInfo,32000,1);
103
104 // Build primary structures
105 if (storePrimaries)
106 {
107 theEventOutputTree->Branch("Primary.", "BDSOutputROOTEventSampler",primary, 32000, 1);
108 theEventOutputTree->Branch("PrimaryGlobal.", "BDSOutputROOTEventCoords", primaryGlobal, 3200, 1);
109 }
110
111 // Build loss and hit structures
112 if (storeELoss)
113 {theEventOutputTree->Branch("Eloss.", "BDSOutputROOTEventLoss", eLoss, 4000, 1);}
115 {theEventOutputTree->Branch("ElossVacuum.", "BDSOutputROOTEventLoss", eLossVacuum, 4000, 1);}
117 {theEventOutputTree->Branch("ElossTunnel.", "BDSOutputROOTEventLoss", eLossTunnel, 4000, 1);}
118 if (storeELossWorld)
119 {
120 theEventOutputTree->Branch("ElossWorld.", "BDSOutputROOTEventLossWorld", eLossWorld, 4000, 1);
121 theEventOutputTree->Branch("ElossWorldExit.", "BDSOutputROOTEventLossWorld", eLossWorldExit, 4000, 1);
122 }
124 {theEventOutputTree->Branch("ElossWorldContents.", "BDSOutputROOTEventLossWorld", eLossWorldContents, 4000, 1);}
125 theEventOutputTree->Branch("PrimaryFirstHit.","BDSOutputROOTEventLoss", pFirstHit, 4000, 2);
126 theEventOutputTree->Branch("PrimaryLastHit.", "BDSOutputROOTEventLoss", pLastHit, 4000, 2);
128 {theEventOutputTree->Branch("ApertureImpacts.", "BDSOutputROOTEventAperture", apertureImpacts, 4000, 1);}
129
130 // Build trajectory structures
131 if (storeTrajectory)
132 {theEventOutputTree->Branch("Trajectory.", "BDSOutputROOTEventTrajectory", traj, 4000, 2);}
133
134 // Build event histograms
135 theEventOutputTree->Branch("Histos.", "BDSOutputROOTEventHistograms", evtHistos, 32000, 1);
136
137 // build sampler structures
139 for (G4int i = 0; i < (G4int)samplerTrees.size(); ++i)
140 {
141 auto samplerTreeLocal = samplerTrees.at(i);
142 auto samplerName = samplerNames.at(i);
143 theEventOutputTree->Branch((samplerName+".").c_str(),
144 "BDSOutputROOTEventSampler",
145 samplerTreeLocal, 32000, globals->SamplersSplitLevel());
146 }
147 for (G4int i = 0; i < (G4int)samplerCTrees.size(); ++i)
148 {
149 auto samplerTreeLocal = samplerCTrees.at(i);
150 auto samplerName = samplerCNames.at(i);
151 theEventOutputTree->Branch((samplerName+".").c_str(),
152 "BDSOutputROOTEventSamplerC",
153 samplerTreeLocal, 32000, globals->SamplersSplitLevel());
154 }
155 for (G4int i = 0; i < (G4int)samplerSTrees.size(); ++i)
156 {
157 auto samplerTreeLocal = samplerSTrees.at(i);
158 auto samplerName = samplerSNames.at(i);
159 theEventOutputTree->Branch((samplerName+".").c_str(),
160 "BDSOutputROOTEventSamplerS",
161 samplerTreeLocal, 32000, globals->SamplersSplitLevel());
162 }
163
164 // build collimator structures
166 {
167 for (G4int i = 0; i < (G4int) collimators.size(); ++i)
168 {
169 auto collimatorLocal = collimators.at(i);
170 auto collimatorName = collimatorNames.at(i);
171 // set the tree branches
172 theEventOutputTree->Branch((collimatorName + ".").c_str(),
173 "BDSOutputROOTEventCollimator",
174 collimatorLocal, 32000, globals->SamplersSplitLevel());
175 }
176 }
177
178 FillHeader(); // this fills and then calls WriteHeader() pure virtual implemented here
179}
180
182{
183 theHeaderOutputTree->Fill();
184}
185
187{
188 theParticleDataTree->Fill();
189}
190
192{
193 theBeamOutputTree->Fill();
194}
195
197{
198 theOptionsOutputTree->Fill();
199}
200
202{
203 theModelOutputTree->Fill();
204}
205
207{
209 {theRootOutputFile->cd();}
210 theEventOutputTree->Fill();
211}
212
214{
216 {theRootOutputFile->cd();}
217 theRunOutputTree->Fill();
218
220 {
221 if (theRootOutputFile->IsOpen())
222 {theRootOutputFile->Write(nullptr,TObject::kOverwrite);}
223 }
224}
225
227{
228 Close();
229}
230
232{
234 {
235 if (theRootOutputFile->IsOpen())
236 {
237 theRootOutputFile->cd();
238 theRootOutputFile->Write(0,TObject::kOverwrite);
239 G4cout << __METHOD_NAME__ << "Data written to file: " << theRootOutputFile->GetName() << G4endl;
240 theRootOutputFile->Close();
241 delete theRootOutputFile;
242 theRootOutputFile = nullptr;
243 }
244 }
245}
246
248{
250 G4int nSamplers = (G4int)samplerTrees.size();
251 for (G4int i = nSamplers - nNewSamplers; i < nSamplers; ++i)
252 {
253 auto samplerTreeLocal = samplerTrees.at(i);
254 auto samplerName = samplerNames.at(i);
255 // set tree branches
256 theEventOutputTree->Branch((samplerName+".").c_str(),
257 "BDSOutputROOTEventSampler",
258 samplerTreeLocal,32000,0);
259 }
260}
General exception with possible name of object and message.
Definition: BDSException.hh:35
A class that holds global options and constants.
static BDSGlobalConstants * Instance()
Access method.
G4int compressionLevel
ROOT compression level for files.
virtual void WriteModel()
Copy model and write to file.
virtual void WriteHeader()
Copy header and write to file.
virtual void WriteFileRunLevel()
virtual void WriteParticleData()
Copy geant4 data to file.
TTree * theBeamOutputTree
Beam Tree.
virtual void UpdateSamplers()
Implementation for ROOT output. Only for link - not for regular use.
TTree * theParticleDataTree
Geant4 Data Tree.
TTree * theOptionsOutputTree
Options tree.
virtual void WriteOptions()
Copy options and write to file.
virtual void WriteBeam()
Copy beam and write to file.
TTree * theEventOutputTree
Event tree.
TTree * theModelOutputTree
Model tree.
virtual void CloseFile()
Write contents and close file.
virtual void NewFile()
Open a new file.
TTree * theRunOutputTree
Output histogram tree.
virtual void WriteFileEventLevel()
TTree * theHeaderOutputTree
Header Tree.
BDSOutputROOT()=delete
No default constructor.
TFile * theRootOutputFile
Output file.
BDSOutputROOTEventModel * modelOutput
Model output.
std::vector< G4String > collimatorNames
Names of collimators in output structures.
std::vector< BDSOutputROOTEventCollimator * > collimators
Collimator output structures.
std::vector< std::string > samplerNames
Sampler names to use.
BDSOutputROOTEventSampler< double > * primary
Primary sampler structure.
BDSOutputROOTEventRunInfo * runInfo
Run information.
BDSOutputROOTEventHistograms * runHistos
Run level histograms.
BDSOutputROOTEventLossWorld * eLossWorldExit
World exit hits.
BDSOutputROOTEventLossWorld * eLossWorldContents
Externally supplied world contents hits.
std::vector< BDSOutputROOTEventSampler< double > * > samplerTrees
Sampler structures.
BDSOutputROOTEventHeader * headerOutput
Information about the file.
BDSOutputROOTEventLossWorld * eLossWorld
World energy deposition.
BDSOutputROOTEventLoss * eLossVacuum
General energy deposition.
BDSOutputROOTEventInfo * evtInfo
Event information.
BDSOutputROOTEventLoss * pLastHit
Primary loss point.
BDSOutputROOTEventHistograms * evtHistos
Event level histograms.
BDSOutputROOTEventLoss * eLossTunnel
Tunnel energy deposition.
G4int UpdateSamplerStructures()
Interface to allow setting up samplers later for dynamic geometry construction a la SixTrack....
BDSOutputROOTEventLoss * pFirstHit
Primary hit point.
BDSOutputROOTEventTrajectory * traj
Trajectories.
BDSOutputROOTEventBeam * beamOutput
Beam output.
BDSOutputROOTEventLoss * eLoss
General energy deposition.
BDSOutputROOTEventAperture * apertureImpacts
Impacts on the aperture.
BDSOutputROOTParticleData * particleDataOutput
Geant4 information / particle tables.
BDSOutputROOTEventOptions * optionsOutput
Options output.
Output base class that defines interface for all output types.
Definition: BDSOutput.hh:73
G4bool CreateCollimatorOutputStructures() const
Whether to create the collimator structures in the output or not.
Definition: BDSOutput.hh:160
G4bool storePrimaries
Options for dynamic bits of output.
Definition: BDSOutput.hh:170
G4bool storeTrajectory
Options for dynamic bits of output.
Definition: BDSOutput.hh:171
G4String GetNextFileName()
Get the next file name based on the base file name and the accrued number of files.
Definition: BDSOutput.cc:390
G4bool storeELoss
Options for dynamic bits of output.
Definition: BDSOutput.hh:163
G4bool storeELossWorldContents
Options for dynamic bits of output.
Definition: BDSOutput.hh:167
void FillHeader()
Fill the local structure header with information - updates time stamp.
Definition: BDSOutput.cc:178
G4bool storeELossWorld
Options for dynamic bits of output.
Definition: BDSOutput.hh:166
G4bool storeELossVacuum
Options for dynamic bits of output.
Definition: BDSOutput.hh:165
G4bool storeELossTunnel
Options for dynamic bits of output.
Definition: BDSOutput.hh:164
G4bool storeApertureImpacts
Options for dynamic bits of output.
Definition: BDSOutput.hh:168