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 - 2023.
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 auto filenames = dl->GetFileNames();
97 HeaderAnalysis* ha = new HeaderAnalysis(filenames,
98 dl->GetHeader(),
99 dl->GetHeaderTree());
100 unsigned long long int nEventsRequested = 0;
101 unsigned long long int nEventsInFileTotal = 0;
102 unsigned long long int nEventsInFileSkippedTotal = 0;
103 unsigned int distrFileLoopNTimes = 0;
104 unsigned long long int nOriginalEvents = ha->CountNOriginalEvents(nEventsInFileTotal,
105 nEventsInFileSkippedTotal,
106 nEventsRequested,
107 distrFileLoopNTimes);
108 delete ha;
109
110 BeamAnalysis* beaAnalysis = new BeamAnalysis(dl->GetBeam(),
111 dl->GetBeamTree(),
112 config->PerEntryBeam(),
113 debug);
114 EventAnalysis* evtAnalysis;
115 evtAnalysis = new EventAnalysis(dl->GetEvent(),
116 dl->GetEventTree(),
117 config->PerEntryEvent(),
118 config->ProcessSamplers(),
119 debug,
120 config->PrintOut(),
121 config->PrintModuloFraction(),
122 config->EmittanceOnTheFly(),
123 (long int) config->GetOptionNumber("eventstart"),
124 (long int) config->GetOptionNumber("eventend"));
125
126 RunAnalysis* runAnalysis = new RunAnalysis(dl->GetRun(),
127 dl->GetRunTree(),
128 config->PerEntryRun(),
129 debug);
130 OptionsAnalysis* optAnalysis = new OptionsAnalysis(dl->GetOptions(),
131 dl->GetOptionsTree(),
132 config->PerEntryOption(),
133 debug);
134 ModelAnalysis* modAnalysis = new ModelAnalysis(dl->GetModel(),
135 dl->GetModelTree(),
136 config->PerEntryModel(),
137 debug);
138
139 std::vector<Analysis*> analyses = {beaAnalysis,
140 evtAnalysis,
141 runAnalysis,
142 optAnalysis,
143 modAnalysis};
144
145 for (auto &analysis: analyses)
146 {analysis->Execute();}
147
148 // write output
149 TFile* outputFile = new TFile(config->OutputFileName().c_str(),"RECREATE");
150
151 // add header for file type and version details
152 outputFile->cd();
154 headerOut->Fill(dl->GetFileNames()); // updates time stamp
155 headerOut->SetFileType("REBDSIM");
156 headerOut->nOriginalEvents = nOriginalEvents;
157 headerOut->nEventsInFile = nEventsInFileTotal;
158 headerOut->nEventsInFileSkipped = nEventsInFileSkippedTotal;
159 headerOut->nEventsRequested = nEventsRequested;
160 headerOut->distrFileLoopNTimes = distrFileLoopNTimes;
161 TTree* headerTree = new TTree("Header", "REBDSIM Header");
162 headerTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
163 headerTree->Fill();
164 headerTree->Write("", TObject::kOverwrite);
165
166 for (auto& analysis : analyses)
167 {analysis->Write(outputFile);}
168
169 // copy the model over and rename to avoid conflicts with Model directory
170 TChain* modelTree = dl->GetModelTree();
171 TTree* treeTest = modelTree->GetTree();
172 if (treeTest)
173 {// TChain can be valid but TTree might not be in corrupt / bad file
174 auto newTree = modelTree->CloneTree();
175 // unfortunately we have a folder called Model in histogram output files
176 // avoid conflict when copying the model for plotting
177 newTree->SetName("ModelTree");
178 newTree->Write("", TObject::kOverwrite);
179 }
180
181 outputFile->Close();
182 delete outputFile;
183 std::cout << "Result written to: " << config->OutputFileName() << std::endl;
184
185 delete dl;
186 for (auto analysis : analyses)
187 {delete analysis;}
188 }
189 catch (const RBDSException& error)
190 {std::cerr << error.what() << std::endl; exit(1);}
191 catch (const std::exception& error)
192 {std::cerr << error.what() << std::endl; exit(1);}
193
194 return 0;
195}
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.
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:140
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:136
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:155
bool PerEntryModel() const
Definition: Config.hh:143
bool PerEntryOption() const
Definition: Config.hh:142
bool PrintOut() const
Accessor.
Definition: Config.hh:135
double GetOptionNumber(const std::string &key) const
General accessor for option.
Definition: Config.hh:87
bool Debug() const
Accessor.
Definition: Config.hh:131
bool EmittanceOnTheFly() const
Accessor.
Definition: Config.hh:133
bool PerEntryBeam() const
Definition: Config.hh:139
std::string OutputFileName() const
Accessor.
Definition: Config.hh:129
bool ProcessSamplers() const
Accessor.
Definition: Config.hh:134
bool PerEntryRun() const
Definition: Config.hh:141
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.
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.
Definition: RunAnalysis.hh:36