BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
bdskim.cc
Go to the documentation of this file.
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*/
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 Long64_t nEntriesHeader = headerTree->GetEntries();
81 headerTree->GetEntry(nEntriesHeader - 1); // get the last entry (2nd is more up to date if it exists)
82 // We also want to explicitly copy the skim variables that might only be known in the 2nd instance.
83 BDSOutputROOTEventHeader* headerOut = new BDSOutputROOTEventHeader(*(headerLocal->header));
84 headerOut->skimmedFile = true;
85
86 TFile* output = new TFile(outputFile.c_str(), "RECREATE");
87 if (output->IsZombie())
88 {std::cerr << "Couldn't open output file " << outputFile << std::endl; return 1;}
89 output->cd();
90 TTree* outputHeaderTree = new TTree("Header", "BDSIM Header");
91 outputHeaderTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
92 outputHeaderTree->Fill();
93
94 std::vector<std::string> treeNames = {"ParticleData", "Beam", "Options", "Model", "Run"};
95 for (const auto& tn : treeNames)
96 {
97 TTree* original = dynamic_cast<TTree*>(input->Get(tn.c_str()));
98 if (!original)
99 {
100 std::cerr << "Failed to load Tree named " << tn << std::endl;
101 delete output;
102 delete input;
103 return 1;
104 }
105 auto clone = original->CloneTree();
106 clone->AutoSave();
107 }
108
109 TTree* allEvents = dynamic_cast<TTree*>(input->Get("Event"));
110 if (!allEvents)
111 {
112 std::cerr << "No Event tree in file" << std::endl;
113 delete input;
114 return 1;
115 }
116 TTree* selectEvents = allEvents->CopyTree(selection.c_str());
117 selectEvents->Write();
118
119 output->Write(nullptr,TObject::kOverwrite);
120 delete output;
121 delete input;
122
123 return 0;
124}
Information about the software and the file.
bool skimmedFile
Whether this is a skimmed output file.
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)