BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
bdskim.cc
Go to the documentation of this file.
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*/
22#include "AnalysisUtilities.hh"
23#include "FileMapper.hh"
24#include "Header.hh"
25#include "SelectionLoader.hh"
26
27#include "BDSOutputROOTEventHeader.hh"
28
29#include "TFile.h"
30#include "TTree.h"
31
32#include <iostream>
33#include <stdexcept>
34#include <string>
35#include <vector>
36
37int main(int argc, char* argv[])
38{
39 if (argc < 3 || argc > 4)
40 {
41 std::cout << "usage: bdskim skimselection.txt input_bdsim_raw.root (output_bdsim_raw.root)" << std::endl;
42 std::cout << "default output name if none given is <inputname>_skimmed.root" << std::endl;
43 return 1;
44 }
45
46 std::string selectionFile = std::string(argv[1]);
47 std::string inputFile = std::string(argv[2]);
48 std::string outputFile;
49 if (argc == 4)
50 {outputFile = std::string(argv[3]);}
51 else
52 {
53 outputFile = RBDS::DefaultOutputName(inputFile, "_skimmed");
54 std::cout << "Using default output file name with \"_skimmed\" suffix : " << outputFile << std::endl;
55 }
56
57 // load selection
58 std::string selection;
59 try
60 {selection = RBDS::LoadSelection(selectionFile);}
61 catch (std::exception& e)
62 {std::cerr << e.what() << std::endl; return 1;}
63
64 // open and check input file
65 TFile* input = new TFile(inputFile.c_str(), "READ");
66 if (!RBDS::IsBDSIMOutputFile(input))
67 {
68 std::cerr << inputFile << " is not a BDSIM output file" << std::endl;
69 delete input;
70 return 1;
71 }
72
73 // note we create trees in the new output file in order the same as the original to preserve the 'look' of the file
74
75 TTree* headerTree = dynamic_cast<TTree*>(input->Get("Header")); // should be safe given check we've just done
76 if (!headerTree)
77 {std::cerr << "Error with header" << std::endl; return 1;}
78 Header* headerLocal = new Header();
79 headerLocal->SetBranchAddress(headerTree);
80 headerTree->GetEntry(0);
81 BDSOutputROOTEventHeader* headerOut = new BDSOutputROOTEventHeader(*(headerLocal->header));
82 headerOut->skimmedFile = true;
83
84 TFile* output = new TFile(outputFile.c_str(), "RECREATE");
85 if (output->IsZombie())
86 {std::cerr << "Couldn't open output file " << outputFile << std::endl; return 1;}
87 output->cd();
88 TTree* outputHeaderTree = new TTree("Header", "BDSIM Header");
89 outputHeaderTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
90
91 std::vector<std::string> treeNames = {"ParticleData", "Beam", "Options", "Model", "Run"};
92 for (const auto& tn : treeNames)
93 {
94 TTree* original = dynamic_cast<TTree*>(input->Get(tn.c_str()));
95 if (!original)
96 {
97 std::cerr << "Failed to load Tree named " << tn << std::endl;
98 delete output;
99 delete input;
100 return 1;
101 }
102 auto clone = original->CloneTree();
103 clone->AutoSave();
104 }
105
106 TTree* allEvents = dynamic_cast<TTree*>(input->Get("Event"));
107 if (!allEvents)
108 {
109 std::cerr << "No Event tree in file" << std::endl;
110 delete input;
111 return 1;
112 }
113 TTree* selectEvents = allEvents->CopyTree(selection.c_str());
114 selectEvents->Write();
115
116 headerOut->nOriginalEvents = allEvents->GetEntries(); // update original number of entries
117 outputHeaderTree->Fill();
118 output->Write(nullptr,TObject::kOverwrite);
119 delete output;
120 delete input;
121
122 return 0;
123}
Information about the software and the file.
bool skimmedFile
Whether this is a skimmed output file.
unsigned long long int nOriginalEvents
Number of original events if skimmed.
Header loader.
Definition: Header.hh:34
BDSOutputROOTEventHeader * header
Member that ROOT can map file data to locally.
Definition: Header.hh:44
void SetBranchAddress(TTree *t)
Set the branch addresses to address the contents of the file.
Definition: Header.cc:41
bool IsBDSIMOutputFile(TFile *file, int *dataVersion=nullptr)
Definition: FileMapper.cc:66
std::string LoadSelection(const std::string &selectionFile)
Load a selection from a text file.
std::string DefaultOutputName(const std::string &inputFilePath, const std::string &suffix)