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