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