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 headerTree->GetEntry(0);
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;}
88 TTree* outputHeaderTree =
new TTree(
"Header",
"BDSIM Header");
89 outputHeaderTree->Branch(
"Header.",
"BDSOutputROOTEventHeader", headerOut);
91 std::vector<std::string> treeNames = {
"ParticleData",
"Beam",
"Options",
"Model",
"Run"};
92 for (
const auto& tn : treeNames)
94 TTree* original =
dynamic_cast<TTree*
>(input->Get(tn.c_str()));
97 std::cerr <<
"Failed to load Tree named " << tn << std::endl;
102 auto clone = original->CloneTree();
106 TTree* allEvents =
dynamic_cast<TTree*
>(input->Get(
"Event"));
109 std::cerr <<
"No Event tree in file" << std::endl;
113 TTree* selectEvents = allEvents->CopyTree(selection.c_str());
114 selectEvents->Write();
117 outputHeaderTree->Fill();
118 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)