BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
rebdsim.cc
Go to the documentation of this file.
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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 catch (const RBDSException& error)
86 {std::cerr << error.what() << std::endl; exit(1);}
87 catch (const std::exception& error)
88 {std::cerr << error.what() << std::endl; exit(1);}
89
90 bool allBranches = config->AllBranchesToBeActivated();
91 const RBDS::BranchMap* branchesToActivate = &(config->BranchesToBeActivated());
92
93 bool debug = config->Debug();
94 DataLoader* dl = nullptr;
95 try
96 {
97 dl = new DataLoader(config->InputFilePath(),
98 debug,
99 config->ProcessSamplers(),
100 allBranches,
101 branchesToActivate,
102 config->GetOptionBool("backwardscompatible"));
103 }
104 catch (const RBDSException& error)
105 {std::cerr << error.what() << std::endl; exit(1);}
106 catch (const std::exception& error)
107 {std::cerr << error.what() << std::endl; exit(1);}
108
109 auto filenames = dl->GetFileNames();
110 HeaderAnalysis* ha = new HeaderAnalysis(filenames,
111 dl->GetHeader(),
112 dl->GetHeaderTree());
113 unsigned long long int nOriginalEvents = ha->CountNOriginalEvents();
114 delete ha;
115
116 BeamAnalysis* beaAnalysis = new BeamAnalysis(dl->GetBeam(),
117 dl->GetBeamTree(),
118 config->PerEntryBeam(),
119 debug);
120 EventAnalysis* evtAnalysis;
121 try
122 {
123 evtAnalysis = new EventAnalysis(dl->GetEvent(),
124 dl->GetEventTree(),
125 config->PerEntryEvent(),
126 config->ProcessSamplers(),
127 debug,
128 config->PrintModuloFraction(),
129 config->GetOptionBool("emittanceonthefly"),
130 (long int) config->GetOptionNumber("eventstart"),
131 (long int) config->GetOptionNumber("eventend"));
132 }
133 catch (const RBDSException& error)
134 {std::cerr << error.what() << std::endl; exit(1);}
135 RunAnalysis* runAnalysis = new RunAnalysis(dl->GetRun(),
136 dl->GetRunTree(),
137 config->PerEntryRun(),
138 debug);
139 OptionsAnalysis* optAnalysis = new OptionsAnalysis(dl->GetOptions(),
140 dl->GetOptionsTree(),
141 config->PerEntryOption(),
142 debug);
143 ModelAnalysis* modAnalysis = new ModelAnalysis(dl->GetModel(),
144 dl->GetModelTree(),
145 config->PerEntryModel(),
146 debug);
147
148 std::vector<Analysis*> analyses = {beaAnalysis,
149 evtAnalysis,
150 runAnalysis,
151 optAnalysis,
152 modAnalysis};
153
154 for (auto& analysis : analyses)
155 {analysis->Execute();}
156
157 // write output
158 try
159 {
160 TFile* outputFile = new TFile(config->OutputFileName().c_str(),"RECREATE");
161
162 // add header for file type and version details
163 outputFile->cd();
165 headerOut->Fill(dl->GetFileNames()); // updates time stamp
166 headerOut->SetFileType("REBDSIM");
167 headerOut->nOriginalEvents = nOriginalEvents;
168 TTree* headerTree = new TTree("Header", "REBDSIM Header");
169 headerTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
170 headerTree->Fill();
171 headerTree->Write("", TObject::kOverwrite);
172
173 for (auto& analysis : analyses)
174 {analysis->Write(outputFile);}
175
176 // copy the model over and rename to avoid conflicts with Model directory
177 TChain* modelTree = dl->GetModelTree();
178 TTree* treeTest = modelTree->GetTree();
179 if (treeTest)
180 {// TChain can be valid but TTree might not be in corrupt / bad file
181 auto newTree = modelTree->CloneTree();
182 // unfortunately we have a folder called Model in histogram output files
183 // avoid conflict when copying the model for plotting
184 newTree->SetName("ModelTree");
185 newTree->Write("", TObject::kOverwrite);
186 }
187
188 outputFile->Close();
189 delete outputFile;
190 std::cout << "Result written to: " << config->OutputFileName() << std::endl;
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 delete dl;
198 for (auto analysis : analyses)
199 {delete analysis;}
200 return 0;
201}
virtual void Execute()
Method which calls all other methods in order.
Definition: Analysis.cc:64
Information about the software and the file.
void SetFileType(std::string fileTypeIn)
Update the file type.
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 >())
Analysis of the options tree.
Definition: BeamAnalysis.hh:36
Configuration and configuration parser class.
Definition: Config.hh:43
std::string InputFilePath() const
Accessor.
Definition: Config.hh:128
bool PerEntryEvent() const
Definition: Config.hh:138
bool AllBranchesToBeActivated() const
Definition: Config.hh:122
bool GetOptionBool(const std::string &key) const
General accessor for option.
Definition: Config.hh:86
double PrintModuloFraction() const
Accessor.
Definition: Config.hh:134
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:141
bool PerEntryOption() const
Definition: Config.hh:140
double GetOptionNumber(const std::string &key) const
General accessor for option.
Definition: Config.hh:87
bool Debug() const
Accessor.
Definition: Config.hh:131
bool PerEntryBeam() const
Definition: Config.hh:137
std::string OutputFileName() const
Accessor.
Definition: Config.hh:129
bool ProcessSamplers() const
Accessor.
Definition: Config.hh:133
bool PerEntryRun() const
Definition: Config.hh:139
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:46
std::vector< std::string > GetFileNames()
Accessor.
Definition: DataLoader.hh:81
Event * GetEvent()
Accessor.
Definition: DataLoader.hh:91
Beam * GetBeam()
Accessor.
Definition: DataLoader.hh:88
Header * GetHeader()
Accessor.
Definition: DataLoader.hh:86
TChain * GetBeamTree()
Accessor.
Definition: DataLoader.hh:95
TChain * GetOptionsTree()
Accessor.
Definition: DataLoader.hh:96
TChain * GetRunTree()
Accessor.
Definition: DataLoader.hh:99
TChain * GetModelTree()
Accessor.
Definition: DataLoader.hh:97
Options * GetOptions()
Accessor.
Definition: DataLoader.hh:89
Run * GetRun()
Accessor.
Definition: DataLoader.hh:92
TChain * GetEventTree()
Accessor.
Definition: DataLoader.hh:98
Model * GetModel()
Accessor.
Definition: DataLoader.hh:90
TChain * GetHeaderTree()
Accessor.
Definition: DataLoader.hh:93
Event level analysis.
Analysis of the model tree.
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.
Definition: RunAnalysis.hh:36