22#include "AnalysisUtilities.hh"
23#include "FileMapper.hh"
25#include "SelectionLoader.hh"
27#include "BDSOutputROOTEventHeader.hh"
37int main(
int argc,
char* argv[])
39 if (argc < 3 || argc > 4)
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;
46 std::string selectionFile = std::string(argv[1]);
47 std::string inputFile = std::string(argv[2]);
48 std::string outputFile;
50 {outputFile = std::string(argv[3]);}
54 std::cout <<
"Using default output file name with \"_skimmed\" suffix : " << outputFile << std::endl;
58 std::string selection;
61 catch (std::exception& e)
62 {std::cerr << e.what() << std::endl;
return 1;}
65 TFile* input =
new TFile(inputFile.c_str(),
"READ");
68 std::cerr << inputFile <<
" is not a BDSIM output file" << std::endl;
75 TTree* headerTree =
dynamic_cast<TTree*
>(input->Get(
"Header"));
77 {std::cerr <<
"Error with header" << std::endl;
return 1;}
80 Long64_t nEntriesHeader = headerTree->GetEntries();
81 headerTree->GetEntry(nEntriesHeader - 1);
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;}
90 TTree* outputHeaderTree =
new TTree(
"Header",
"BDSIM Header");
91 outputHeaderTree->Branch(
"Header.",
"BDSOutputROOTEventHeader", headerOut);
92 outputHeaderTree->Fill();
94 std::vector<std::string> treeNames = {
"ParticleData",
"Beam",
"Options",
"Model",
"Run"};
95 for (
const auto& tn : treeNames)
97 TTree* original =
dynamic_cast<TTree*
>(input->Get(tn.c_str()));
100 std::cerr <<
"Failed to load Tree named " << tn << std::endl;
105 auto clone = original->CloneTree();
109 TTree* allEvents =
dynamic_cast<TTree*
>(input->Get(
"Event"));
112 std::cerr <<
"No Event tree in file" << std::endl;
116 TTree* selectEvents = allEvents->CopyTree(selection.c_str());
117 selectEvents->Write();
119 output->Write(
nullptr,TObject::kOverwrite);
bool IsBDSIMOutputFile(TFile *file, int *dataVersion=nullptr)
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)