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) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSDebug.hh"
19#include "BDSException.hh"
20#include "BDSOutputLoader.hh"
21#include "BDSOutputROOTEventBeam.hh"
22#include "BDSOutputROOTEventHeader.hh"
23#include "BDSOutputROOTEventInfo.hh"
24#include "BDSOutputROOTEventOptions.hh"
25
26#include "parser/beam.h"
27#include "parser/beamBase.h"
28#include "parser/options.h"
29#include "parser/optionsBase.h"
30
31#include "globals.hh" // geant4 types / globals
32
33#include "TFile.h"
34#include "TList.h"
35#include "TTree.h"
36
37
38BDSOutputLoader::BDSOutputLoader(const G4String& filePath):
39 dataVersion(-1),
40 nOriginalEvents(0),
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 G4cout << __METHOD_NAME__ << "Opening file: " << filePath << G4endl;
51 // open file - READ mode to prevent accidental corruption by adding new things
52 file = new TFile(filePath.c_str(), "READ");
53
54 // check it's a valid file
55 badFilePath = file->IsZombie();
56 if (badFilePath)
57 {throw BDSException(__METHOD_NAME__, "No such file \"" + filePath + "\"");}
58 else
59 {// check it's a rootevent file - TBC - use analysis IsBDSIMFile after restructure
60 rootEventFile = file->GetListOfKeys()->Contains("Event");
61 if (!rootEventFile)
62 {throw BDSException(__METHOD_NAME__, "Not a BDSIM rootevent output format ROOT file");}
63 }
64
65 // extract data version
66 TTree* headerTree = dynamic_cast<TTree*>(file->Get("Header"));
67 if (!headerTree) // no header -> definitely not a bdsim file
68 {throw BDSException(__METHOD_NAME__, "\"" + filePath + "\" Not a BDSIM output file");}
70 headerTree->SetBranchAddress("Header.", &headerLocal);
71 headerTree->GetEntry(0);
72 dataVersion = headerLocal->dataVersion;
73 nOriginalEvents = headerLocal->nOriginalEvents;
74 delete headerLocal;
75
76 beamTree = dynamic_cast<TTree*>(file->Get("Beam"));
77 if (!beamTree)
78 {throw BDSException(__METHOD_NAME__, "Invalid file \"" + filePath + "\" - doesn't contain beam Tree");}
79 localBeam = new BDSOutputROOTEventBeam();
80 beamTree->SetBranchAddress("Beam.", &localBeam);
81 beamTree->GetEntry(0);
82
83 // set up local structure copies.
84 optionsTree = dynamic_cast<TTree*>(file->Get("Options"));
85 if (!optionsTree)
86 {throw BDSException(__METHOD_NAME__, "Invalid file \"" + filePath + "\" - doesn't contain options structure.");}
87 localOptions = new BDSOutputROOTEventOptions();
88 optionsTree->SetBranchAddress("Options.", &localOptions);
89 optionsTree->GetEntry(0);
90
91 eventTree = dynamic_cast<TTree*>(file->Get("Event"));
92 localEventSummary = new BDSOutputROOTEventInfo();
93 if (dataVersion < 4)
94 {eventTree->SetBranchAddress("Info.", &localEventSummary);}
95 else
96 {eventTree->SetBranchAddress("Summary.", &localEventSummary);}
97}
98
99BDSOutputLoader::~BDSOutputLoader()
100{
101 delete file; // closes if open
102 delete localBeam;
103 delete localOptions;
104 delete localEventSummary;
105}
106
107GMAD::BeamBase BDSOutputLoader::BeamBaseClass()
108{
109 // always change back to this file - assuming other root files could be open
110 file->cd();
111 beamTree->GetEntry(0);
112 return *(static_cast<GMAD::BeamBase*>(localBeam));
113}
114
115GMAD::Beam BDSOutputLoader::Beam()
116{
117 return GMAD::Beam(BeamBaseClass());
118}
119
120GMAD::OptionsBase BDSOutputLoader::OptionsBaseClass()
121{
122 // always change back to this file - assuming other root files could be open
123 file->cd();
124 optionsTree->GetEntry(0);
125 return *(static_cast<GMAD::OptionsBase*>(localOptions));
126}
127
128GMAD::Options BDSOutputLoader::Options()
129{
130 return GMAD::Options(OptionsBaseClass());
131}
132
133G4String BDSOutputLoader::SeedState(G4int eventNumber)
134{
135 // always change back to this file - assuming other root files could be open
136 file->cd();
137
138 // cannot retrieve a seed state beyond that in the file - protection here to
139 // make life simpler elsewhere
140 if (eventNumber > eventTree->GetEntries())
141 {
142 G4cout << __METHOD_NAME__ << "event index beyond number stored in file - no seed state loaded" << G4endl;
143 return "";
144 }
145 eventTree->GetEntry((int)eventNumber);
146
147 return G4String(localEventSummary->seedStateAtStart);
148}
General exception with possible name of object and message.
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:34
Beam class.
Definition beam.h:43
Basic options class independent of Geant4.
Definition optionsBase.h:35
Options class.
Definition options.h:43