BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSOutputLoaderSampler.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 "BDSOutputLoaderSampler.hh"
21#include "BDSOutputROOTEventOptions.hh"
22#include "BDSOutputROOTEventSampler.hh"
23#include "BDSUtilities.hh"
24
25#include "globals.hh"
26
27#include "RtypesCore.h"
28#include "TFile.h"
29#include "TTree.h"
30
31#include <string>
32
33BDSOutputLoaderSampler::BDSOutputLoaderSampler(const G4String& filePath,
34 const G4String& samplerName):
35 BDSOutputLoader(filePath),
36 doublePrecision(false),
37 nEvents(0),
38 localSamplerFloat(nullptr),
39 localSamplerDouble(nullptr)
40{
41 G4String samplerNameLocal = samplerName;
42 if (!BDS::EndsWith(samplerNameLocal, "."))
43 {samplerNameLocal += ".";}
44
45 TBranch* search = eventTree->GetBranch(samplerNameLocal);
46 if (!search)
47 {throw BDSException(__METHOD_NAME__, "no such sampler name \"" + samplerName + "\"");}
48
49 localSamplerDouble = new BDSOutputROOTEventSampler<double>();
50 localSamplerFloat = new BDSOutputROOTEventSampler<float>();
51 doublePrecision = localOptions->outputDoublePrecision;
52 if (localOptions->outputDoublePrecision)
53 {eventTree->SetBranchAddress(samplerNameLocal, &localSamplerDouble);}
54 else
55 {eventTree->SetBranchAddress(samplerNameLocal, &localSamplerFloat);}
56 nEvents = (G4long)eventTree->GetEntries();
57}
58
59BDSOutputLoaderSampler::~BDSOutputLoaderSampler()
60{
61 delete localSamplerFloat;
62 delete localSamplerDouble;
63}
64
65const BDSOutputROOTEventSampler<float>* BDSOutputLoaderSampler::SamplerDataFloat(G4long eventNumber)
66{
67 localSamplerFloat->Flush();
68 Common(eventNumber);
69 return localSamplerFloat;
70}
71
72const BDSOutputROOTEventSampler<double>* BDSOutputLoaderSampler::SamplerDataDouble(G4long eventNumber)
73{
74 localSamplerDouble->Flush();
75 Common(eventNumber);
76 return localSamplerDouble;
77}
78
79void BDSOutputLoaderSampler::Common(G4long eventNumber)
80{
81 if (eventNumber > nEvents)
82 {
83 G4String msg = "requested event number " + std::to_string(eventNumber);
84 msg += " is beyond number of entries in the file (" + std::to_string(nEvents) + ")";
85 throw BDSException(__METHOD_NAME__, msg);
86 }
87 // always change back to this file - assuming other root files could be open
88 file->cd();
89
90 // cannot retrieve a seed state beyond that in the file - protection here to
91 // make life simpler elsewhere
92 if (eventNumber > eventTree->GetEntries())
93 {
94 G4cout << __METHOD_NAME__ << "event index beyond number stored in file - no seed state loaded" << G4endl;
95 return;
96 }
97 eventTree->GetEntry((Long64_t)eventNumber);
98}
General exception with possible name of object and message.
Loader of ROOT Event output for recreating events.
Information stored per sampler per event.
virtual void Flush()
Clean Sampler.
G4bool EndsWith(const std::string &expression, const std::string &suffix)
Return true if a string "expression" ends with "suffix".