26#include "AnalysisUtilities.hh"
29#include "DataLoader.hh"
30#include "EventAnalysis.hh"
32#include "RBDSException.hh"
34#include "BDSOutputROOTEventBeam.hh"
35#include "BDSOutputROOTEventHeader.hh"
36#include "BDSOutputROOTEventOptions.hh"
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;
51int main(
int argc,
char* argv[])
53 if (argc < 2 || argc > 4)
55 std::cout <<
"Incorrect number of arguments." << std::endl;
61 std::vector<std::string> arguments;
62 for (
int i = 1; i < argc; i++)
63 {arguments.emplace_back(std::string(argv[i]));}
64 std::set<std::string> argumentsSet = {arguments.begin(), arguments.end()};
67 bool emittanceOnFly = argumentsSet.count(
"--emittanceOnTheFly") > 0 || argumentsSet.count(
"--emittanceOnFly") > 0;
69 {std::cout <<
"Calculating emittance per sampler" << std::endl;}
71 arguments.erase(std::remove_if(arguments.begin(),
73 [](
const std::string& s){ return s ==
"--emittanceOnTheFly" || s ==
"--emittanceOnFly";}),
76 std::string inputFileName = arguments[0];
77 std::string outputFileName;
78 if (arguments.size() > 1)
79 {outputFileName = arguments[1];}
80 if (arguments.size() > 2)
82 for (
int i = 2; i < (int)arguments.size(); i++)
83 {std::cout <<
"Unknown option \"" << arguments[i] <<
"\"" << std::endl;}
88 if (inputFileName == outputFileName)
90 std::cout <<
"outputfile same as datafile" << std::endl;
95 if (outputFileName.empty())
98 std::cout <<
"Using default output file name with \"_optics\" suffix : " << outputFileName << std::endl;
103 {dl =
new DataLoader(inputFileName,
false,
true);}
105 {std::cerr << error.
what() << std::endl;
return 1;}
106 catch (
const std::exception& error)
107 {std::cerr << error.what() << std::endl;
return 1;}
113 beamTree->GetEntry(0);
114 const std::string& particleName = outputBeam->
particle;
117 if (modelTree->GetEntries() == 0)
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;
127 false,
true,
false,
true, -1, emittanceOnFly, 0, -1, particleName);
131 {std::cerr << error.
what() << std::endl;
return 1;}
133 TFile* outputFile =
new TFile(outputFileName.c_str(),
"RECREATE");
140 TTree* headerTree =
new TTree(
"Header",
"REBDSIM Header");
141 headerTree->Branch(
"Header.",
"BDSOutputROOTEventHeader", headerOut);
143 headerTree->Write(
"", TObject::kOverwrite);
146 evtAnalysis->
Write(outputFile);
152 optionsTree->GetEntry(0);
156 auto newTree = modelTree->CloneTree();
157 newTree->Write(
"", TObject::kOverwrite);
162 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)