BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSOutputROOT.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 "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();
71
72 theRootOutputFile = new TFile(newFileName,"RECREATE", "BDS output file");
73 if (theRootOutputFile->IsZombie())
74 {throw BDSException(__METHOD_NAME__, "Unable to open output file: \"" + newFileName +"\"");}
75
76 if (compressionLevel > 9 || compressionLevel < -1)
77 {throw BDSException(__METHOD_NAME__, "invalid ROOT compression level (" + std::to_string(compressionLevel) + ") must be 0 - 9.");}
78 if (compressionLevel > -1)
79 {theRootOutputFile->SetCompressionLevel(compressionLevel);}
80
81 // root file - note this sets the current 'directory' to this file!
83
84 theHeaderOutputTree = new TTree("Header", "BDSIM Header"); // header
85 theParticleDataTree = new TTree("ParticleData", "Particle Data"); // particle data
86 theBeamOutputTree = new TTree("Beam", "BDSIM beam"); // beam data tree
87 theOptionsOutputTree = new TTree("Options","BDSIM options"); // options data tree
88 theModelOutputTree = new TTree("Model","BDSIM model"); // model data tree
89 theRunOutputTree = new TTree("Run","BDSIM run histograms/information"); // run info tree
90 theEventOutputTree = new TTree("Event","BDSIM event"); // event data tree
91
92 // Build branches for each object
93 theHeaderOutputTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOutput, 32000, 1);
94 theParticleDataTree->Branch("ParticleData.", "BDSOutputROOTParticleData", particleDataOutput,32000, 1);
95 theBeamOutputTree->Branch("Beam.", "BDSOutputROOTEventBeam", beamOutput, 32000, 2);
96 theOptionsOutputTree->Branch("Options.", "BDSOutputROOTEventOptions", optionsOutput, 32000, 2);
97 theModelOutputTree->Branch("Model.", "BDSOutputROOTEventModel", modelOutput, 32000, globals->ModelSplitLevel());
98 theRunOutputTree->Branch("Histos.", "BDSOutputROOTEventHistograms",runHistos, 32000, 1);
99 theRunOutputTree->Branch("Summary.", "BDSOutputROOTEventRunInfo", runInfo, 32000, 1);
100
101 // Branches for event...
102 // Event info output
103 theEventOutputTree->Branch("Summary.", "BDSOutputROOTEventInfo",evtInfo,32000,1);
104
105 // Build primary structures
106 if (storePrimaries)
107 {
108 theEventOutputTree->Branch("Primary.", "BDSOutputROOTEventSampler",primary, 32000, 1);
109 theEventOutputTree->Branch("PrimaryGlobal.", "BDSOutputROOTEventCoords", primaryGlobal, 3200, 1);
110 }
111
112 // Build loss and hit structures
113 if (storeELoss)
114 {theEventOutputTree->Branch("Eloss.", "BDSOutputROOTEventLoss", eLoss, 4000, 1);}
116 {theEventOutputTree->Branch("ElossVacuum.", "BDSOutputROOTEventLoss", eLossVacuum, 4000, 1);}
118 {theEventOutputTree->Branch("ElossTunnel.", "BDSOutputROOTEventLoss", eLossTunnel, 4000, 1);}
119 if (storeELossWorld)
120 {
121 theEventOutputTree->Branch("ElossWorld.", "BDSOutputROOTEventLossWorld", eLossWorld, 4000, 1);
122 theEventOutputTree->Branch("ElossWorldExit.", "BDSOutputROOTEventLossWorld", eLossWorldExit, 4000, 1);
123 }
125 {theEventOutputTree->Branch("ElossWorldContents.", "BDSOutputROOTEventLossWorld", eLossWorldContents, 4000, 1);}
126 theEventOutputTree->Branch("PrimaryFirstHit.","BDSOutputROOTEventLoss", pFirstHit, 4000, 2);
127 theEventOutputTree->Branch("PrimaryLastHit.", "BDSOutputROOTEventLoss", pLastHit, 4000, 2);
129 {theEventOutputTree->Branch("ApertureImpacts.", "BDSOutputROOTEventAperture", apertureImpacts, 4000, 1);}
130
131 // Build trajectory structures
132 if (storeTrajectory)
133 {theEventOutputTree->Branch("Trajectory.", "BDSOutputROOTEventTrajectory", traj, 4000, 2);}
134
135 // Build event histograms
136 theEventOutputTree->Branch("Histos.", "BDSOutputROOTEventHistograms", evtHistos, 32000, 1);
137
138 // 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 // there's no way to overwrite an entry in a ttree so we just add another entry with updated information
189 theHeaderOutputTree->Fill();
190}
191
193{
194 theParticleDataTree->Fill();
195}
196
198{
199 theBeamOutputTree->Fill();
200}
201
203{
204 theOptionsOutputTree->Fill();
205}
206
208{
209 theModelOutputTree->Fill();
210}
211
213{
215 {theRootOutputFile->cd();}
216 theEventOutputTree->Fill();
217}
218
220{
222 {theRootOutputFile->cd();}
223 theRunOutputTree->Fill();
224
226 {
227 if (theRootOutputFile->IsOpen())
228 {theRootOutputFile->Write(nullptr,TObject::kOverwrite);}
229 }
230}
231
233{
234 Close();
235}
236
238{
240 {
241 if (theRootOutputFile->IsOpen())
242 {
243 theRootOutputFile->cd();
244 theRootOutputFile->Write(0,TObject::kOverwrite);
245 G4cout << __METHOD_NAME__ << "Data written to file: " << theRootOutputFile->GetName() << G4endl;
246 theRootOutputFile->Close();
247 delete theRootOutputFile;
248 theRootOutputFile = nullptr;
249 }
250 }
251}
252
254{
256 G4int nSamplers = (G4int)samplerTrees.size();
257 for (G4int i = nSamplers - nNewSamplers; i < nSamplers; ++i)
258 {
259 auto samplerTreeLocal = samplerTrees.at(i);
260 auto samplerName = samplerNames.at(i);
261 // set tree branches
262 theEventOutputTree->Branch((samplerName+".").c_str(),
263 "BDSOutputROOTEventSampler",
264 samplerTreeLocal,32000,0);
265 }
266}
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 WriteHeaderEndOfFile()
Overwrite contents of header in the 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:165
G4bool storePrimaries
Options for dynamic bits of output.
Definition: BDSOutput.hh:175
G4bool storeTrajectory
Options for dynamic bits of output.
Definition: BDSOutput.hh:176
G4String GetNextFileName()
Get the next file name based on the base file name and the accrued number of files.
Definition: BDSOutput.cc:405
G4bool storeELoss
Options for dynamic bits of output.
Definition: BDSOutput.hh:168
G4bool storeELossWorldContents
Options for dynamic bits of output.
Definition: BDSOutput.hh:172
void FillHeader()
Fill the local structure header with information - updates time stamp.
Definition: BDSOutput.cc:182
G4bool storeELossWorld
Options for dynamic bits of output.
Definition: BDSOutput.hh:171
G4bool storeELossVacuum
Options for dynamic bits of output.
Definition: BDSOutput.hh:170
G4bool storeELossTunnel
Options for dynamic bits of output.
Definition: BDSOutput.hh:169
G4bool storeApertureImpacts
Options for dynamic bits of output.
Definition: BDSOutput.hh:173