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