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) 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*/
22#include <iostream>
23#include <string>
24
25#include "TChain.h"
26#include "TFile.h"
27#include "TTree.h"
28
29#include "BDSOutputROOTEventHeader.hh"
30#include "BDSOutputROOTEventOptions.hh"
31#include "BDSOutputROOTEventModel.hh"
32
33#include "Analysis.hh"
34#include "Config.hh"
35#include "DataLoader.hh"
36#include "BeamAnalysis.hh"
37#include "EventAnalysis.hh"
38#include "HeaderAnalysis.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 try
63 {
64 // Setup config
65 Config* config = Config::Instance("", inputFilePath, outputFileName, "_histos");
66
67 bool allBranches = config->AllBranchesToBeActivated();
68 const RBDS::BranchMap* branchesToActivate = &(config->BranchesToBeActivated());
69
70 bool debug = config->Debug();
71 DataLoader* dl = new DataLoader(config->InputFilePath(),
72 debug,
73 config->ProcessSamplers(),
74 allBranches,
75 branchesToActivate,
76 config->GetOptionBool("backwardscompatible"));
77
78 BeamAnalysis* beaAnalysis = new BeamAnalysis(dl->GetBeam(),
79 dl->GetBeamTree(),
80 config->PerEntryBeam(),
81 debug);
82
83 EventAnalysis* evtAnalysis = new EventAnalysis(dl->GetEvent(),
84 dl->GetEventTree(),
85 config->PerEntryEvent(),
86 config->ProcessSamplers(),
87 debug,
88 config->PrintOut(),
89 config->PrintModuloFraction(),
90 config->GetOptionBool("emittanceonthefly"));
91
92 RunAnalysis* runAnalysis = new RunAnalysis(dl->GetRun(),
93 dl->GetRunTree(),
94 config->PerEntryRun(),
95 debug);
96 OptionsAnalysis* optAnalysis = new OptionsAnalysis(dl->GetOptions(),
97 dl->GetOptionsTree(),
98 config->PerEntryOption(),
99 debug);
100 ModelAnalysis* modAnalysis = new ModelAnalysis(dl->GetModel(),
101 dl->GetModelTree(),
102 config->PerEntryModel(),
103 debug);
104
105 auto filenames = dl->GetFileNames();
106 HeaderAnalysis* ha = new HeaderAnalysis(filenames,
107 dl->GetHeader(),
108 dl->GetHeaderTree());
109 unsigned long long int nEventsInFileTotal;
110 unsigned long long int nEventsInFileSkippedTotal;
111 unsigned long long int nEventsRequested;
112 unsigned int distrFileLoopNTimes;
113 unsigned long long int nOriginalEvents = ha->CountNOriginalEvents(nEventsInFileTotal,
114 nEventsInFileSkippedTotal,
115 nEventsRequested,
116 distrFileLoopNTimes);
117
118 std::vector<Analysis*> analyses = {beaAnalysis,
119 evtAnalysis,
120 runAnalysis,
121 optAnalysis,
122 modAnalysis};
123
124 for (auto& analysis : analyses)
125 {analysis->Execute();}
126
127 // write output
128 TFile* outputFile = new TFile(config->OutputFileName().c_str(),"RECREATE");
129
130 // add header for file type and version details
131 outputFile->cd();
133 headerOut->Fill(dl->GetFileNames()); // updates time stamp
134 headerOut->SetFileType("REBDSIM");
135 headerOut->nOriginalEvents = nOriginalEvents;
136 headerOut->nEventsInFile = nEventsInFileTotal;
137 headerOut->nEventsInFileSkipped = nEventsInFileSkippedTotal;
138 headerOut->nEventsRequested = nEventsRequested;
139 TTree* headerTree = new TTree("Header", "REBDSIM Header");
140 headerTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
141 headerTree->Fill();
142 headerTree->Write("", TObject::kOverwrite);
143
144 for (auto& analysis : analyses)
145 {analysis->Write(outputFile);}
146
147 // copy the model over and rename to avoid conflicts with Model directory
148 auto modelTree = dl->GetModelTree();
149 auto newTree = modelTree->CloneTree();
150 // unfortunately we have a folder called Model in histogram output files
151 // avoid conflict when copying the model for plotting
152 newTree->SetName("ModelTree");
153 newTree->Write("", TObject::kOverwrite);
154
155 outputFile->Close();
156 delete outputFile;
157 std::cout << "Result written to: " << config->OutputFileName() << std::endl;
158 delete dl;
159 for (auto analysis : analyses)
160 {delete analysis;}
161 }
162 catch (const RBDSException& error)
163 {std::cerr << error.what() << std::endl; return 1;}
164 catch (const std::exception& error)
165 {std::cerr << error.what() << std::endl; return 1;}
166 return 0;
167}
virtual void Execute()
Method which calls all other methods in order.
Definition Analysis.cc:63
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.
Configuration and configuration parser class.
Definition Config.hh:42
std::string InputFilePath() const
Accessor.
Definition Config.hh:123
bool PerEntryEvent() const
Definition Config.hh:136
bool AllBranchesToBeActivated() const
Accessor.
Definition Config.hh:126
bool GetOptionBool(const std::string &key) const
General accessor for option.
Definition Config.hh:85
double PrintModuloFraction() const
Accessor.
Definition Config.hh:132
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:139
bool PerEntryOption() const
Definition Config.hh:138
bool PrintOut() const
Accessor.
Definition Config.hh:131
bool Debug() const
Accessor.
Definition Config.hh:127
bool PerEntryBeam() const
Definition Config.hh:135
std::string OutputFileName() const
Accessor.
Definition Config.hh:124
bool ProcessSamplers() const
Accessor.
Definition Config.hh:130
bool PerEntryRun() const
Definition Config.hh:137
const RBDS::VectorString & BranchesToBeActivated(const std::string &treeName) const
Definition Config.hh:113
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
Header * GetHeader()
Accessor.
Definition DataLoader.hh:88
TChain * GetBeamTree()
Accessor.
Definition DataLoader.hh:97
TChain * GetOptionsTree()
Accessor.
Definition DataLoader.hh:98
TChain * GetRunTree()
Accessor.
TChain * GetModelTree()
Accessor.
Definition DataLoader.hh:99
Options * GetOptions()
Accessor.
Definition DataLoader.hh:91
Run * GetRun()
Accessor.
Definition DataLoader.hh:94
TChain * GetEventTree()
Accessor.
Model * GetModel()
Accessor.
Definition DataLoader.hh:92
TChain * GetHeaderTree()
Accessor.
Definition DataLoader.hh:95
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.