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