BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSOutputLoader.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 "BDSOutputLoader.hh"
22#include "BDSOutputROOTEventBeam.hh"
23#include "BDSOutputROOTEventHeader.hh"
24#include "BDSOutputROOTEventInfo.hh"
25#include "BDSOutputROOTEventOptions.hh"
26
27#include "parser/beam.h"
28#include "parser/beamBase.h"
29#include "parser/options.h"
30#include "parser/optionsBase.h"
31
32#include "globals.hh" // geant4 types / globals
33
34#include "TFile.h"
35#include "TList.h"
36#include "TTree.h"
37
38
39BDSOutputLoader::BDSOutputLoader(const G4String& filePath):
40 dataVersion(-1),
41 badFilePath(true),
42 rootEventFile(false),
43 localBeam(nullptr),
44 localOptions(nullptr),
45 localEventSummary(nullptr),
46 beamTree(nullptr),
47 optionsTree(nullptr),
48 eventTree(nullptr)
49{
50 // open file - READ mode to prevent accidental corruption by adding new things
51 file = new TFile(filePath.c_str(), "READ");
52
53 // check it's a valid file
54 badFilePath = file->IsZombie();
55 if (badFilePath)
56 {throw BDSException(__METHOD_NAME__, "No such file \"" + filePath + "\"");}
57 else
58 {// check it's a rootevent file - TBC - use analysis IsBDSIMFile after restructure
59 rootEventFile = file->GetListOfKeys()->Contains("Event");
60 if (!rootEventFile)
61 {throw BDSException(__METHOD_NAME__, "Not a BDSIM rootevent output format ROOT file");}
62 }
63
64 // extract data version
65 TTree* headerTree = static_cast<TTree*>(file->Get("Header"));
66 if (!headerTree) // no header -> definitely not a bdsim file
67 {throw BDSException(__METHOD_NAME__, "\"" + filePath + "\" Not a BDSIM output file");}
69 headerTree->SetBranchAddress("Header.", &headerLocal);
70 headerTree->GetEntry(0);
71 dataVersion = headerLocal->dataVersion;
72 delete headerLocal;
73
74 beamTree = static_cast<TTree*>(file->Get("Beam"));
75 if (!beamTree)
76 {throw BDSException(__METHOD_NAME__, "Invalid file \"" + filePath + "\" - doesn't contain beam Tree");}
77 localBeam = new BDSOutputROOTEventBeam();
78 beamTree->SetBranchAddress("Beam.", &localBeam);
79 beamTree->GetEntry(0);
80
81 // set up local structure copies.
82 optionsTree = static_cast<TTree*>(file->Get("Options"));
83 if (!optionsTree)
84 {throw BDSException(__METHOD_NAME__, "Invalid file \"" + filePath + "\" - doesn't contain options structure.");}
85 localOptions = new BDSOutputROOTEventOptions();
86 optionsTree->SetBranchAddress("Options.", &localOptions);
87 optionsTree->GetEntry(0);
88
89 eventTree = static_cast<TTree*>(file->Get("Event"));
90 localEventSummary = new BDSOutputROOTEventInfo();
91 if (dataVersion < 4)
92 {eventTree->SetBranchAddress("Info.", &localEventSummary);}
93 else
94 {eventTree->SetBranchAddress("Summary.", &localEventSummary);}
95}
96
97BDSOutputLoader::~BDSOutputLoader()
98{
99 delete file; // closes if open
100 delete localBeam;
101 delete localOptions;
102 delete localEventSummary;
103}
104
105GMAD::BeamBase BDSOutputLoader::BeamBaseClass()
106{
107 // always change back to this file - assuming other root files could be open
108 file->cd();
109 beamTree->GetEntry(0);
110 return *(static_cast<GMAD::BeamBase*>(localBeam));
111}
112
113GMAD::Beam BDSOutputLoader::Beam()
114{
115 return GMAD::Beam(BeamBaseClass());
116}
117
118GMAD::OptionsBase BDSOutputLoader::OptionsBaseClass()
119{
120 // always change back to this file - assuming other root files could be open
121 file->cd();
122 optionsTree->GetEntry(0);
123 return *(static_cast<GMAD::OptionsBase*>(localOptions));
124}
125
126GMAD::Options BDSOutputLoader::Options()
127{
128 return GMAD::Options(OptionsBaseClass());
129}
130
131G4String BDSOutputLoader::SeedState(G4int eventNumber)
132{
133 // always change back to this file - assuming other root files could be open
134 file->cd();
135
136 // cannot retrieve a seed state beyond that in the file - protection here to
137 // make life simpler elsewhere
138 if (eventNumber > eventTree->GetEntries())
139 {
140 G4cout << __METHOD_NAME__ << "event index beyond number stored in file - no seed state loaded" << G4endl;
141 return "";
142 }
143 eventTree->GetEntry((int)eventNumber);
144
145 return G4String(localEventSummary->seedStateAtStart);
146}
General exception with possible name of object and message.
Definition: BDSException.hh:35
Class to store all beam options for a BDSIM run.
Information about the software and the file.
int dataVersion
Our data format version.
Information pertaining to an individual event.
std::string seedStateAtStart
Seed state at the start of the event.
Class to store all options for a BDSIM run.
Options for a beam distribution.
Definition: beamBase.h:35
Beam class.
Definition: beam.h:44
Basic options class independent of Geant4.
Definition: optionsBase.h:36
Options class.
Definition: options.h:44