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 - 2024.
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 G4cout << "Store per event histos " << storePerEventHistos << G4endl;
138 {theEventOutputTree->Branch("Histos.", "BDSOutputROOTEventHistograms", evtHistos, 32000, 1);}
139
140 // build sampler structures
141 for (G4int i = 0; i < (G4int)samplerTrees.size(); ++i)
142 {
143 auto samplerTreeLocal = samplerTrees.at(i);
144 auto samplerName = samplerNames.at(i);
145 theEventOutputTree->Branch((samplerName+".").c_str(),
146 "BDSOutputROOTEventSampler",
147 samplerTreeLocal, 32000, globals->SamplersSplitLevel());
148 }
149 for (G4int i = 0; i < (G4int)samplerCTrees.size(); ++i)
150 {
151 auto samplerTreeLocal = samplerCTrees.at(i);
152 auto samplerName = samplerCNames.at(i);
153 theEventOutputTree->Branch((samplerName+".").c_str(),
154 "BDSOutputROOTEventSamplerC",
155 samplerTreeLocal, 32000, globals->SamplersSplitLevel());
156 }
157 for (G4int i = 0; i < (G4int)samplerSTrees.size(); ++i)
158 {
159 auto samplerTreeLocal = samplerSTrees.at(i);
160 auto samplerName = samplerSNames.at(i);
161 theEventOutputTree->Branch((samplerName+".").c_str(),
162 "BDSOutputROOTEventSamplerS",
163 samplerTreeLocal, 32000, globals->SamplersSplitLevel());
164 }
165
166 // build collimator structures
168 {
169 for (G4int i = 0; i < (G4int) collimators.size(); ++i)
170 {
171 auto collimatorLocal = collimators.at(i);
172 auto collimatorName = collimatorNames.at(i);
173 // set the tree branches
174 theEventOutputTree->Branch((collimatorName + ".").c_str(),
175 "BDSOutputROOTEventCollimator",
176 collimatorLocal, 32000, globals->SamplersSplitLevel());
177 }
178 }
179
180 FillHeader(); // this fills and then calls WriteHeader() pure virtual implemented here
181}
182
187
189{
190 // there's no way to overwrite an entry in a ttree so we just add another entry with updated information
191 theHeaderOutputTree->Fill();
192}
193
198
200{
201 theBeamOutputTree->Fill();
202}
203
208
210{
211 theModelOutputTree->Fill();
212}
213
220
222{
224 {theRootOutputFile->cd();}
225 theRunOutputTree->Fill();
226
228 {
229 if (theRootOutputFile->IsOpen())
230 {theRootOutputFile->Write(nullptr,TObject::kOverwrite);}
231 }
232}
233
235{
236 Close();
237}
238
240{
242 {
243 if (theRootOutputFile->IsOpen())
244 {
245 theRootOutputFile->cd();
246 theRootOutputFile->Write(0,TObject::kOverwrite);
247 G4cout << __METHOD_NAME__ << "Data written to file: " << theRootOutputFile->GetName() << G4endl;
248 theRootOutputFile->Close();
249 delete theRootOutputFile;
250 theRootOutputFile = nullptr;
251 }
252 }
253}
254
256{
258 G4int nSamplers = (G4int)samplerTrees.size();
259 for (G4int i = nSamplers - nNewSamplers; i < nSamplers; ++i)
260 {
261 auto samplerTreeLocal = samplerTrees.at(i);
262 auto samplerName = samplerNames.at(i);
263 // set tree branches
264 theEventOutputTree->Branch((samplerName+".").c_str(),
265 "BDSOutputROOTEventSampler",
266 samplerTreeLocal,32000,0);
267 }
268}
General exception with possible name of object and message.
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:167
G4bool storePerEventHistos
Options for dynamic bits of output.
Definition BDSOutput.hh:179
G4bool storePrimaries
Options for dynamic bits of output.
Definition BDSOutput.hh:177
G4bool storeTrajectory
Options for dynamic bits of output.
Definition BDSOutput.hh:178
G4String GetNextFileName()
Get the next file name based on the base file name and the accrued number of files.
Definition BDSOutput.cc:407
G4bool storeELoss
Options for dynamic bits of output.
Definition BDSOutput.hh:170
G4bool storeELossWorldContents
Options for dynamic bits of output.
Definition BDSOutput.hh:174
void FillHeader()
Fill the local structure header with information - updates time stamp.
Definition BDSOutput.cc:185
G4bool storeELossWorld
Options for dynamic bits of output.
Definition BDSOutput.hh:173
G4bool storeELossVacuum
Options for dynamic bits of output.
Definition BDSOutput.hh:172
G4bool storeELossTunnel
Options for dynamic bits of output.
Definition BDSOutput.hh:171
G4bool storeApertureImpacts
Options for dynamic bits of output.
Definition BDSOutput.hh:175