Warning, /tutorial-analysis/_extras/tree_pruning_script.md is written in an unsupported language. File is not indexed.
0001 ---
0002 title: "Tree Pruning Script"
0003 ---
0004
0005 Included below is a short script which can be utilised to prune an input tree. This can be utilised to trim down our large EICrecon output file to a smaller, simpler subset of trees. You can modify this as needed by following the comments in the script.
0006
0007 ## TreePrune.C
0008
0009 Copy paste this to a new file called "TreePrune.C". Execute it via -
0010
0011 ```console
0012 root -l TreePrune.C
0013 ```
0014
0015 You will be prompted for an input file name. Alternatively run the script with the input already specified -
0016
0017 ```console
0018 root -l 'TreePrune.C("InputFilePath")'
0019 ```
0020
0021 ```c++
0022 // Stephen JD Kay, University of York, 21/02/25
0023 // A short script to read in a file and prune it to only retain a smaller subset of branches.
0024 // This could be utilised to trim down a full EICrecon file for a new user to look at. This avoids the potentially overwhelming number of branches normally stored
0025 #include <string>
0026
0027 void TreePrune(TString infile=""){
0028
0029 // If no input file provide as argument, promot for one
0030 if(infile == ""){
0031 cout << "Enter a filename to analyse: ";
0032 cin >> infile;
0033 }
0034
0035 // Check input file exists, exit if not
0036 if(gSystem->AccessPathName(infile) == kTRUE){
0037 cerr << "!!!!! ERROR !!!!!" << endl << infile << " not found" << endl << "!!!!! ERROR !!!!!" << endl;
0038 exit(0);
0039 }
0040
0041 TString ofile_name, ofile_tmp, BeamE;
0042 TObjArray *tmp_Name_arr;
0043
0044 // Check the input file is a .root file as we would expect
0045 if(infile.Contains(".root") == false){
0046 cerr << "!!!!!!!!!!! - ERROR - !!!!!!!!!!!!" << endl;
0047 cerr << "Input files should be a root file!" << endl;
0048 cerr << "!!!!!!!!!!! - ERROR - !!!!!!!!!!!!" << endl;
0049 exit(1);
0050 }
0051 else{
0052 cout << "Opening and pruning " << infile << endl;
0053 }
0054
0055 // If the input file name is a file path containing /, extract only the actual file name for further use. Assign the temporary name of the output to be the input, minus its .root extension
0056 if(infile.Contains("/")){
0057 tmp_Name_arr = infile.Tokenize("/");
0058 ofile_tmp = (((TObjString *)(tmp_Name_arr->At(tmp_Name_arr->GetLast())))->String()).ReplaceAll(".root","");
0059 }
0060 else{
0061 ofile_tmp = infile;
0062 ofile_tmp.ReplaceAll(".root","");
0063 }
0064 // Set output file name
0065 ofile_name = Form("%s_Pruned.root", ofile_tmp.Data());
0066
0067 // Open our full, unpruned file
0068 TFile *full_file = TFile::Open(infile);
0069 TTree* full_tree;
0070 // Get the events tree
0071 full_file->GetObject("events", full_tree);
0072 // Deactivate all branches
0073 full_tree->SetBranchStatus("*", 0);
0074 // Activate only the branches we want to keep, add more as needed via the line below
0075 // full_tree->SetBranchStatus("",1)
0076 full_tree->SetBranchStatus("MCParticles*",1);
0077 full_tree->SetBranchStatus("ReconstructedParticles**",1);
0078 full_tree->SetBranchStatus("ReconstructedChargedParticles*",1);
0079 full_tree->SetBranchStatus("ReconstructedParticleAssociations*",1);
0080 full_tree->SetBranchStatus("ReconstructedChargedParticleAssociations*",1);
0081 full_tree->SetBranchStatus("EcalEndcapNClusters*",1);
0082 full_tree->SetBranchStatus("EcalEndcapPClusters*",1);
0083
0084 // Set and open output file for the histograms
0085 TFile *ofile = TFile::Open(ofile_name,"RECREATE");
0086 // Clone the tree
0087 TTree* pruned_tree = full_tree->CloneTree(0);
0088 // Copy the branches
0089 pruned_tree->CopyEntries(full_tree);
0090
0091 // Write the new tree to file
0092 ofile->Write();
0093 ofile->Close(); // Close output file
0094 full_file->Close(); // Close input file
0095
0096 delete ofile;
0097 delete full_file;
0098
0099 }
0100 ```
0101