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