BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
rebdsimOptics.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*/
22#include <iostream>
23#include <set>
24#include <string>
25#include <vector>
26
27#include "AnalysisUtilities.hh"
28#include "Beam.hh"
29#include "Config.hh"
30#include "DataLoader.hh"
31#include "EventAnalysis.hh"
32#include "Options.hh"
33#include "RBDSException.hh"
34
35#include "BDSOutputROOTEventBeam.hh"
36#include "BDSOutputROOTEventHeader.hh"
37#include "BDSOutputROOTEventOptions.hh"
38
39#include "TFile.h"
40#include "TChain.h"
41
42void usage()
43{
44 std::cout << "usage: rebdsimOptics <datafile> (<outputfile>) (--emittanceOnFly)" << std::endl;
45 std::cout << " <datafile> - root file to operate on ie run1.root" << std::endl;
46 std::cout << " <outputfile> - name of output file ie optics.root. Must be different to datafile" << std::endl;
47 std::cout << " --emittanceOnTheFly - calculate emittance per sampler (optional)" << std::endl;
48 std::cout << " Quotes should be used if * is used in the input file name." << std::endl;
49 std::cout << " <outputfile> is optional - default is <datafile>_optics.root" << std::endl;
50}
51
52int main(int argc, char* argv[])
53{
54 if (argc < 2 || argc > 4)
55 {
56 std::cout << "Incorrect number of arguments." << std::endl;
57 usage();
58 return 1;
59 }
60
61 // parse arguments
62 std::vector<std::string> arguments;
63 for (int i = 1; i < argc; i++) // skip name of the program
64 {arguments.emplace_back(std::string(argv[i]));}
65 std::set<std::string> argumentsSet = {arguments.begin(), arguments.end()};
66
67 // emittance on the fly
68 bool emittanceOnFly = argumentsSet.count("--emittanceOnTheFly") > 0 || argumentsSet.count("--emittanceOnFly") > 0;
69 if (emittanceOnFly)
70 {std::cout << "Calculating emittance per sampler" << std::endl;}
71 // remove this option from vector of arguments
72 arguments.erase(std::remove_if(arguments.begin(),
73 arguments.end(),
74 [](const std::string& s){ return s == "--emittanceOnTheFly" || s == "--emittanceOnFly";}),
75 arguments.end());
76
77 std::string inputFileName = arguments[0];
78 std::string outputFileName;
79 if (arguments.size() > 1)
80 {outputFileName = arguments[1];}
81 if (arguments.size() > 2)
82 {
83 for (int i = 2; i < (int)arguments.size(); i++)
84 {std::cout << "Unknown option \"" << arguments[i] << "\"" << std::endl;}
85 usage();
86 return 1;
87 }
88
89 if (inputFileName == outputFileName)
90 {
91 std::cout << "outputfile same as datafile" << std::endl;
92 usage();
93 return 1;
94 }
95
96 if (outputFileName.empty())
97 {
98 outputFileName = RBDS::DefaultOutputName(inputFileName, "_optics");
99 std::cout << "Using default output file name with \"_optics\" suffix : " << outputFileName << std::endl;
100 }
101
102 DataLoader* dl = nullptr;
103 try
104 {dl = new DataLoader(inputFileName, false, true);}
105 catch (const RBDSException& error)
106 {std::cerr << error.what() << std::endl; return 1;}
107 catch (const std::exception& error)
108 {std::cerr << error.what() << std::endl; return 1;}
109
110 // beam required to get the mass of the primary particle in EventAnalysis
111 Beam* beam = dl->GetBeam();
112 TChain* beamTree = dl->GetBeamTree();
113 BDSOutputROOTEventBeam* outputBeam = beam->beam;
114 beamTree->GetEntry(0);
115 const std::string& particleName = outputBeam->particle;
116
117 TChain* modelTree = dl->GetModelTree();
118 if (modelTree->GetEntries() == 0)
119 {
120 std::cout << "Warning: data file written without Model tree that is required to know the sampler names" << std::endl;
121 std::cout << " only the primary sampler will be analysed if available" << std::endl;
122 }
123
124 EventAnalysis* evtAnalysis;
125 try
126 {
127 evtAnalysis = new EventAnalysis(dl->GetEvent(), dl->GetEventTree(),
128 false, true, false, true, -1, emittanceOnFly, 0, -1, particleName);
129 evtAnalysis->Execute();
130 }
131 catch (const RBDSException& error)
132 {std::cerr << error.what() << std::endl; return 1;}
133
134 TFile* outputFile = new TFile(outputFileName.c_str(), "RECREATE");
135
136 // add header for file type and version details
137 outputFile->cd();
139 headerOut->Fill(dl->GetFileNames()); // updates time stamp
140 headerOut->SetFileType("REBDSIM");
141 TTree* headerTree = new TTree("Header", "REBDSIM Header");
142 headerTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
143 headerTree->Fill();
144 headerTree->Write("", TObject::kOverwrite);
145
146 // write merged histograms and optics
147 evtAnalysis->Write(outputFile);
148
149 // Don't clone the model tree if only primaries are generated - model not created in BDSIM
150 Options* options = dl->GetOptions();
151 TChain* optionsTree = dl->GetOptionsTree();
152 BDSOutputROOTEventOptions* ob = options->options;
153 optionsTree->GetEntry(0);
154 if (!ob->generatePrimariesOnly)
155 {
156 // clone model tree for nice built in optics plotting
157 auto newTree = modelTree->CloneTree();
158 newTree->Write("", TObject::kOverwrite);
159 }
160
161 outputFile->Close();
162 delete outputFile;
163 std::cout << "Result written to: " << outputFileName << std::endl;
164 delete dl;
165 delete evtAnalysis;
166
167 return 0;
168}
Class to store all beam options for a BDSIM run.
Information about the software and the file.
void SetFileType(const std::string &fileTypeIn)
Update the file type.
void Fill(const std::vector< std::string > &analysedFilesIn=std::vector< std::string >(), const std::vector< std::string > &combinedFilesIn=std::vector< std::string >())
Class to store all options for a BDSIM run.
Beam loader.
Definition: Beam.hh:37
BDSOutputROOTEventBeam * beam
Member that ROOT can map file data to locally.
Definition: Beam.hh:49
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
TChain * GetBeamTree()
Accessor.
Definition: DataLoader.hh:95
TChain * GetOptionsTree()
Accessor.
Definition: DataLoader.hh:96
TChain * GetModelTree()
Accessor.
Definition: DataLoader.hh:97
Options * GetOptions()
Accessor.
Definition: DataLoader.hh:89
TChain * GetEventTree()
Accessor.
Definition: DataLoader.hh:98
Event level analysis.
virtual void Execute()
virtual void Write(TFile *outputFileName)
Write analysis including optical functions to an output file.
std::string particle
beam parameters
Definition: beamBase.h:40
bool generatePrimariesOnly
Whether to only generate primary coordinates and quit, or not.
Definition: optionsBase.h:102
Options loader.
Definition: Options.hh:36
BDSOutputROOTEventOptions * options
Member that ROOT can map file data to locally.
Definition: Options.hh:48
General exception with possible name of object and message.
const char * what() const noexcept override
Override message in std::exception.
std::string DefaultOutputName(const std::string &inputFilePath, const std::string &suffix)