BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
rebdsimHistoMerge.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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
26#include "TChain.h"
27#include "TFile.h"
28#include "TTree.h"
29
30#include "BDSOutputROOTEventHeader.hh"
31#include "BDSOutputROOTEventOptions.hh"
32#include "BDSOutputROOTEventModel.hh"
33
34#include "Analysis.hh"
35#include "Config.hh"
36#include "DataLoader.hh"
37#include "BeamAnalysis.hh"
38#include "EventAnalysis.hh"
39#include "ModelAnalysis.hh"
40#include "OptionsAnalysis.hh"
41#include "RBDSException.hh"
42#include "RebdsimTypes.hh"
43#include "RunAnalysis.hh"
44
45int main(int argc, char *argv[])
46{
47 // check input
48 if (argc < 2 || argc > 3)
49 {
50 std::cout << "usage: rebdsim <datafile> (<outputFile>)" << std::endl;
51 std::cout << " <datafile> - root file to operate on" << std::endl;
52 std::cout << " <outputfile> - output file name for analysis" << std::endl;
53 std::cout << " <outputfile> is optional - default is <datafile>_histos.root" << std::endl;
54 return 1;
55 }
56
57 std::string inputFilePath = std::string(argv[1]);
58 std::string outputFileName;
59 if (argc == 3) // optional
60 {outputFileName = std::string(argv[2]);}
61
62 // Setup config
63 Config* config = Config::Instance("", inputFilePath, outputFileName, "_histos");
64
65 bool allBranches = config->AllBranchesToBeActivated();
66 const RBDS::BranchMap* branchesToActivate = &(config->BranchesToBeActivated());
67
68 bool debug = config->Debug();
69 DataLoader* dl = nullptr;
70 try
71 {
72 dl = new DataLoader(config->InputFilePath(),
73 debug,
74 config->ProcessSamplers(),
75 allBranches,
76 branchesToActivate,
77 config->GetOptionBool("backwardscompatible"));
78 }
79 catch (const RBDSException& error)
80 {std::cerr << error.what() << std::endl; return 1;}
81 catch (const std::exception& error)
82 {std::cerr << error.what() << std::endl; return 1;}
83
84 BeamAnalysis* beaAnalysis = new BeamAnalysis(dl->GetBeam(),
85 dl->GetBeamTree(),
86 config->PerEntryBeam(),
87 debug);
88 EventAnalysis* evtAnalysis;
89 try
90 {
91 evtAnalysis = new EventAnalysis(dl->GetEvent(),
92 dl->GetEventTree(),
93 config->PerEntryEvent(),
94 config->ProcessSamplers(),
95 debug,
96 config->PrintModuloFraction(),
97 config->GetOptionBool("emittanceonthefly"));
98 }
99 catch (const RBDSException& error)
100 {std::cerr << error.what() << std::endl; return 1;}
101
102 RunAnalysis* runAnalysis = new RunAnalysis(dl->GetRun(),
103 dl->GetRunTree(),
104 config->PerEntryRun(),
105 debug);
106 OptionsAnalysis* optAnalysis = new OptionsAnalysis(dl->GetOptions(),
107 dl->GetOptionsTree(),
108 config->PerEntryOption(),
109 debug);
110 ModelAnalysis* modAnalysis = new ModelAnalysis(dl->GetModel(),
111 dl->GetModelTree(),
112 config->PerEntryModel(),
113 debug);
114
115 std::vector<Analysis*> analyses = {beaAnalysis,
116 evtAnalysis,
117 runAnalysis,
118 optAnalysis,
119 modAnalysis};
120
121 for (auto& analysis : analyses)
122 {analysis->Execute();}
123
124 // write output
125 try
126 {
127 TFile* outputFile = new TFile(config->OutputFileName().c_str(),"RECREATE");
128
129 // add header for file type and version details
130 outputFile->cd();
132 headerOut->Fill(dl->GetFileNames()); // updates time stamp
133 headerOut->SetFileType("REBDSIM");
134 TTree* headerTree = new TTree("Header", "REBDSIM Header");
135 headerTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
136 headerTree->Fill();
137 headerTree->Write("", TObject::kOverwrite);
138
139 for (auto& analysis : analyses)
140 {analysis->Write(outputFile);}
141
142 // copy the model over and rename to avoid conflicts with Model directory
143 auto modelTree = dl->GetModelTree();
144 auto newTree = modelTree->CloneTree();
145 // unfortunately we have a folder called Model in histogram output files
146 // avoid conflict when copying the model for plotting
147 newTree->SetName("ModelTree");
148 newTree->Write("", TObject::kOverwrite);
149
150 outputFile->Close();
151 delete outputFile;
152 std::cout << "Result written to: " << config->OutputFileName() << std::endl;
153 }
154 catch (const RBDSException& error)
155 {std::cerr << error.what() << std::endl; return 1;}
156 catch (const std::exception& error)
157 {std::cerr << error.what() << std::endl; return 1;}
158 delete dl;
159 for (auto analysis : analyses)
160 {delete analysis;}
161 return 0;
162}
virtual void Execute()
Method which calls all other methods in order.
Definition: Analysis.cc:64
Information about the software and the file.
void SetFileType(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 >())
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:138
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:134
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:153
bool PerEntryModel() const
Definition: Config.hh:141
bool PerEntryOption() const
Definition: Config.hh:140
bool Debug() const
Accessor.
Definition: Config.hh:131
bool PerEntryBeam() const
Definition: Config.hh:137
std::string OutputFileName() const
Accessor.
Definition: Config.hh:129
bool ProcessSamplers() const
Accessor.
Definition: Config.hh:133
bool PerEntryRun() const
Definition: Config.hh:139
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
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
Event level analysis.
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