27#include "AnalysisUtilities.hh"
30#include "DataLoader.hh"
31#include "EventAnalysis.hh"
33#include "RBDSException.hh"
35#include "BDSOutputROOTEventBeam.hh"
36#include "BDSOutputROOTEventHeader.hh"
37#include "BDSOutputROOTEventOptions.hh"
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;
52int main(
int argc,
char* argv[])
54 if (argc < 2 || argc > 4)
56 std::cout <<
"Incorrect number of arguments." << std::endl;
62 std::vector<std::string> arguments;
63 for (
int i = 1; i < argc; i++)
64 {arguments.emplace_back(std::string(argv[i]));}
65 std::set<std::string> argumentsSet = {arguments.begin(), arguments.end()};
68 bool emittanceOnFly = argumentsSet.count(
"--emittanceOnTheFly") > 0 || argumentsSet.count(
"--emittanceOnFly") > 0;
70 {std::cout <<
"Calculating emittance per sampler" << std::endl;}
72 arguments.erase(std::remove_if(arguments.begin(),
74 [](
const std::string& s){ return s ==
"--emittanceOnTheFly" || s ==
"--emittanceOnFly";}),
77 std::string inputFileName = arguments[0];
78 std::string outputFileName;
79 if (arguments.size() > 1)
80 {outputFileName = arguments[1];}
81 if (arguments.size() > 2)
83 for (
int i = 2; i < (int)arguments.size(); i++)
84 {std::cout <<
"Unknown option \"" << arguments[i] <<
"\"" << std::endl;}
89 if (inputFileName == outputFileName)
91 std::cout <<
"outputfile same as datafile" << std::endl;
96 if (outputFileName.empty())
99 std::cout <<
"Using default output file name with \"_optics\" suffix : " << outputFileName << std::endl;
104 {dl =
new DataLoader(inputFileName,
false,
true);}
106 {std::cerr << error.
what() << std::endl;
return 1;}
107 catch (
const std::exception& error)
108 {std::cerr << error.what() << std::endl;
return 1;}
114 beamTree->GetEntry(0);
115 const std::string& particleName = outputBeam->
particle;
118 if (modelTree->GetEntries() == 0)
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;
128 false,
true,
false,
true, -1, emittanceOnFly, 0, -1, particleName);
132 {std::cerr << error.
what() << std::endl;
return 1;}
134 TFile* outputFile =
new TFile(outputFileName.c_str(),
"RECREATE");
141 TTree* headerTree =
new TTree(
"Header",
"REBDSIM Header");
142 headerTree->Branch(
"Header.",
"BDSOutputROOTEventHeader", headerOut);
144 headerTree->Write(
"", TObject::kOverwrite);
147 evtAnalysis->
Write(outputFile);
153 optionsTree->GetEntry(0);
157 auto newTree = modelTree->CloneTree();
158 newTree->Write(
"", TObject::kOverwrite);
163 std::cout <<
"Result written to: " << outputFileName << std::endl;
Class to store all beam options for a BDSIM run.
Class to store all options for a BDSIM run.
BDSOutputROOTEventBeam * beam
Member that ROOT can map file data to locally.
Loader for a ROOT file using classes used to generate the file.
std::vector< std::string > GetFileNames()
Accessor.
Event * GetEvent()
Accessor.
Beam * GetBeam()
Accessor.
TChain * GetBeamTree()
Accessor.
TChain * GetOptionsTree()
Accessor.
TChain * GetModelTree()
Accessor.
Options * GetOptions()
Accessor.
TChain * GetEventTree()
Accessor.
virtual void Write(TFile *outputFileName)
Write analysis including optical functions to an output file.
std::string particle
beam parameters
bool generatePrimariesOnly
Whether to only generate primary coordinates and quit, or not.
BDSOutputROOTEventOptions * options
Member that ROOT can map file data to locally.
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)