File indexing completed on 2025-04-02 09:14:14
0001
0002
0003
0004 #include <string>
0005
0006 void TreePrune(TString infile=""){
0007
0008
0009 if(infile == ""){
0010 cout << "Enter a filename to analyse: ";
0011 cin >> infile;
0012 }
0013
0014
0015 if(gSystem->AccessPathName(infile) == kTRUE){
0016 cerr << "!!!!! ERROR !!!!!" << endl << infile << " not found" << endl << "!!!!! ERROR !!!!!" << endl;
0017 exit(0);
0018 }
0019
0020 TString ofile_name, ofile_tmp, BeamE;
0021 TObjArray *tmp_Name_arr;
0022
0023
0024 if(infile.Contains(".root") == false){
0025 cerr << "!!!!!!!!!!! - ERROR - !!!!!!!!!!!!" << endl;
0026 cerr << "Input files should be a root file!" << endl;
0027 cerr << "!!!!!!!!!!! - ERROR - !!!!!!!!!!!!" << endl;
0028 exit(1);
0029 }
0030 else{
0031 cout << "Opening and pruning " << infile << endl;
0032 }
0033
0034
0035 if(infile.Contains("/")){
0036 tmp_Name_arr = infile.Tokenize("/");
0037 ofile_tmp = (((TObjString *)(tmp_Name_arr->At(tmp_Name_arr->GetLast())))->String()).ReplaceAll(".root","");
0038 }
0039 else{
0040 ofile_tmp = infile;
0041 ofile_tmp.ReplaceAll(".root","");
0042 }
0043
0044 ofile_name = Form("%s_Pruned.root", ofile_tmp.Data());
0045
0046
0047 TFile *full_file = TFile::Open(infile);
0048 TTree* full_tree;
0049
0050 full_file->GetObject("events", full_tree);
0051
0052 full_tree->SetBranchStatus("*", 0);
0053
0054
0055 full_tree->SetBranchStatus("MCParticles*",1);
0056 full_tree->SetBranchStatus("ReconstructedChargedParticles*",1);
0057 full_tree->SetBranchStatus("ReconstructedChargedParticleAssociations*",1);
0058
0059
0060
0061 TFile *ofile = TFile::Open(ofile_name,"RECREATE");
0062
0063 TTree* pruned_tree = full_tree->CloneTree(0);
0064
0065 pruned_tree->CopyEntries(full_tree);
0066
0067
0068 ofile->Write();
0069 ofile->Close();
0070 full_file->Close();
0071
0072 delete ofile;
0073 delete full_file;
0074
0075 }