BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
rebdsim.cc
Go to the documentation of this file.
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*/
22#include <iostream>
23#include <string>
24#include <vector>
25
26#include "TChain.h"
27#include "TFile.h"
28#include "TTree.h"
29
30#include "BDSOutputROOTEventHeader.hh"
31#include "BDSOutputROOTEventOptions.hh"
32#include "BDSOutputROOTEventModel.hh"
33
34#include "Analysis.hh"
35#include "BeamAnalysis.hh"
36#include "Config.hh"
37#include "DataLoader.hh"
38#include "EventAnalysis.hh"
39#include "HeaderAnalysis.hh"
40#include "ModelAnalysis.hh"
41#include "OptionsAnalysis.hh"
42#include "RBDSException.hh"
43#include "RebdsimTypes.hh"
44#include "RunAnalysis.hh"
45
46int main(int argc, char *argv[])
47{
48 // check input
49 if (argc < 2 || argc > 4)
50 {
51 std::cout << "usage: rebdsim <analysisConfig> (<dataFile>) (<outputFile>)" << std::endl;
52 std::cout << " <datafile> (optional) - root file to operate on" << std::endl;
53 std::cout << " <outputfile> (optional) - output file name for analysis" << std::endl;
54 std::cout << " if no <datafile> and <outputfile> are specified, those from <analysisConfig> are used." << std::endl;
55 exit(1);
56 }
57
58 std::string configFilePath = std::string(argv[1]); //create a string from arguments so able to use find_last_of and substr methods
59 std::string configFileExtension = configFilePath.substr(configFilePath.find_last_of('.') + 1) ;
60 if (configFileExtension != "txt")
61 {
62 std::cerr << "Unrecognised extension for file: " << configFilePath << ". Extension: " << configFileExtension << std::endl;
63 std::cerr << "Make sure the config file is plain text with the .txt extension!" << std::endl;
64 exit(1);
65 }
66
67 std::cout << "rebdsim> configuration file name : " << configFilePath << std::endl;
68
69 std::string inputFilePath = ""; // default of "" means use ones specified in analysisConfig.txt
70 std::string outputFileName = "";
71
72 if (argc > 2)
73 {inputFilePath = std::string(argv[2]);}
74 if (argc > 3)
75 {outputFileName = std::string(argv[3]);}
76
77 // parse input file with options and histogram definitions
78 Config* config = nullptr;
79 try
80 {
81 Config::Instance(configFilePath, inputFilePath, outputFileName);
82 config = Config::Instance();
83
84 bool allBranches = config->AllBranchesToBeActivated();
85 const RBDS::BranchMap* branchesToActivate = &(config->BranchesToBeActivated());
86
87 bool debug = config->Debug();
88 DataLoader* dl = new DataLoader(config->InputFilePath(),
89 debug,
90 config->ProcessSamplers(),
91 allBranches,
92 branchesToActivate,
93 config->GetOptionBool("backwardscompatible"));
94
95 config->FixCylindricalAndSphericalSamplerVariablesInSets(dl->GetAllCylindricalSamplerNames(),
96 dl->GetAllSphericalSamplerNames());
97
98 auto filenames = dl->GetFileNames();
99 HeaderAnalysis* ha = new HeaderAnalysis(filenames,
100 dl->GetHeader(),
101 dl->GetHeaderTree());
102 unsigned long long int nEventsRequested = 0;
103 unsigned long long int nEventsInFileTotal = 0;
104 unsigned long long int nEventsInFileSkippedTotal = 0;
105 unsigned int distrFileLoopNTimes = 0;
106 unsigned long long int nOriginalEvents = ha->CountNOriginalEvents(nEventsInFileTotal,
107 nEventsInFileSkippedTotal,
108 nEventsRequested,
109 distrFileLoopNTimes);
110 delete ha;
111
112 BeamAnalysis* beaAnalysis = new BeamAnalysis(dl->GetBeam(),
113 dl->GetBeamTree(),
114 config->PerEntryBeam(),
115 debug);
116 EventAnalysis* evtAnalysis;
117 evtAnalysis = new EventAnalysis(dl->GetEvent(),
118 dl->GetEventTree(),
119 config->PerEntryEvent(),
120 config->ProcessSamplers(),
121 debug,
122 config->PrintOut(),
123 config->PrintModuloFraction(),
124 config->EmittanceOnTheFly(),
125 (long int) config->GetOptionNumber("eventstart"),
126 (long int) config->GetOptionNumber("eventend"));
127
128 RunAnalysis* runAnalysis = new RunAnalysis(dl->GetRun(),
129 dl->GetRunTree(),
130 config->PerEntryRun(),
131 debug);
132 OptionsAnalysis* optAnalysis = new OptionsAnalysis(dl->GetOptions(),
133 dl->GetOptionsTree(),
134 config->PerEntryOption(),
135 debug);
136 ModelAnalysis* modAnalysis = new ModelAnalysis(dl->GetModel(),
137 dl->GetModelTree(),
138 config->PerEntryModel(),
139 debug);
140
141 std::vector<Analysis*> analyses = {beaAnalysis,
142 evtAnalysis,
143 runAnalysis,
144 optAnalysis,
145 modAnalysis};
146
147 for (auto &analysis: analyses)
148 {analysis->Execute();}
149
150 // write output
151 TFile* outputFile = new TFile(config->OutputFileName().c_str(),"RECREATE");
152
153 // add header for file type and version details
154 outputFile->cd();
156 headerOut->Fill(dl->GetFileNames()); // updates time stamp
157 headerOut->SetFileType("REBDSIM");
158 headerOut->nOriginalEvents = nOriginalEvents;
159 headerOut->nEventsInFile = nEventsInFileTotal;
160 headerOut->nEventsInFileSkipped = nEventsInFileSkippedTotal;
161 headerOut->nEventsRequested = nEventsRequested;
162 headerOut->distrFileLoopNTimes = distrFileLoopNTimes;
163 TTree* headerTree = new TTree("Header", "REBDSIM Header");
164 headerTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
165 headerTree->Fill();
166 headerTree->Write("", TObject::kOverwrite);
167
168 for (auto& analysis : analyses)
169 {analysis->Write(outputFile);}
170
171 // copy the model over and rename to avoid conflicts with Model directory
172 TChain* modelTree = dl->GetModelTree();
173 TTree* treeTest = modelTree->GetTree();
174 if (treeTest)
175 {// TChain can be valid but TTree might not be in corrupt / bad file
176 auto newTree = modelTree->CloneTree();
177 // unfortunately we have a folder called Model in histogram output files
178 // avoid conflict when copying the model for plotting
179 newTree->SetName("ModelTree");
180 newTree->Write("", TObject::kOverwrite);
181 }
182
183 outputFile->Close();
184 delete outputFile;
185 std::cout << "Result written to: " << config->OutputFileName() << std::endl;
186
187 delete dl;
188 for (auto analysis : analyses)
189 {delete analysis;}
190 }
191 catch (const RBDSException& error)
192 {std::cerr << error.what() << std::endl; exit(1);}
193 catch (const std::exception& error)
194 {std::cerr << error.what() << std::endl; exit(1);}
195
196 return 0;
197}
virtual void Execute()
Method which calls all other methods in order.
Definition Analysis.cc:63
Information about the software and the file.
unsigned long long int nEventsRequested
Number of events requested to be simulated from the file.
unsigned int distrFileLoopNTimes
Number of times a distribution file was replayed.
void SetFileType(const std::string &fileTypeIn)
Update the file type.
unsigned long long int nEventsInFileSkipped
Number of events from distribution file that were skipped due to filters.
unsigned long long int nOriginalEvents
Number of original events if skimmed.
void Fill(const std::vector< std::string > &analysedFilesIn=std::vector< std::string >(), const std::vector< std::string > &combinedFilesIn=std::vector< std::string >())
unsigned long long int nEventsInFile
Number of events in the input distribution file irrespective of filters.
Analysis of the options tree.
Configuration and configuration parser class.
Definition Config.hh:42
std::string InputFilePath() const
Accessor.
Definition Config.hh:123
bool PerEntryEvent() const
Definition Config.hh:136
bool AllBranchesToBeActivated() const
Accessor.
Definition Config.hh:126
bool GetOptionBool(const std::string &key) const
General accessor for option.
Definition Config.hh:85
double PrintModuloFraction() const
Accessor.
Definition Config.hh:132
void FixCylindricalAndSphericalSamplerVariablesInSets(const std::set< std::string > &allCNames, const std::set< std::string > &allSNames)
Definition Config.cc:565
static Config * Instance(const std::string &fileName="", const std::string &inputFilePath="", const std::string &outputFileName="", const std::string &defaultOutputFileSuffix="_ana")
Singleton accessor.
Definition Config.cc:153
bool PerEntryModel() const
Definition Config.hh:139
bool PerEntryOption() const
Definition Config.hh:138
bool PrintOut() const
Accessor.
Definition Config.hh:131
double GetOptionNumber(const std::string &key) const
General accessor for option.
Definition Config.hh:86
bool Debug() const
Accessor.
Definition Config.hh:127
bool EmittanceOnTheFly() const
Accessor.
Definition Config.hh:129
bool PerEntryBeam() const
Definition Config.hh:135
std::string OutputFileName() const
Accessor.
Definition Config.hh:124
bool ProcessSamplers() const
Accessor.
Definition Config.hh:130
bool PerEntryRun() const
Definition Config.hh:137
const RBDS::VectorString & BranchesToBeActivated(const std::string &treeName) const
Definition Config.hh:113
Loader for a ROOT file using classes used to generate the file.
Definition DataLoader.hh:46
std::vector< std::string > GetFileNames()
Accessor.
Definition DataLoader.hh:81
Event * GetEvent()
Accessor.
Definition DataLoader.hh:93
Beam * GetBeam()
Accessor.
Definition DataLoader.hh:90
Header * GetHeader()
Accessor.
Definition DataLoader.hh:88
TChain * GetBeamTree()
Accessor.
Definition DataLoader.hh:97
TChain * GetOptionsTree()
Accessor.
Definition DataLoader.hh:98
TChain * GetRunTree()
Accessor.
TChain * GetModelTree()
Accessor.
Definition DataLoader.hh:99
Options * GetOptions()
Accessor.
Definition DataLoader.hh:91
Run * GetRun()
Accessor.
Definition DataLoader.hh:94
TChain * GetEventTree()
Accessor.
Model * GetModel()
Accessor.
Definition DataLoader.hh:92
TChain * GetHeaderTree()
Accessor.
Definition DataLoader.hh:95
Event level analysis.
Analysis of the model tree.
unsigned long long int CountNOriginalEvents(unsigned long long int &nEventsInFileIn, unsigned long long int &nEventsInFileSkippedIn, unsigned long long int &nEventsRequestedIn, unsigned int &distrFileLoopNTimesIn)
Analysis of the model tree.
Analysis of the options tree.
General exception with possible name of object and message.
const char * what() const noexcept override
Override message in std::exception.
Analysis of a run.