ATLAS Offline Software
compressionTool.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 // Use CloneTree(0) to get the structure of the tree, then copy events one event at a time from old to new tree
6 // Successfully changes compression algorithm, compression level, and auto flush
7 // Output basket (when controlling for other variables) is slightly smaller than original
8 //
9 // Macro Options:
10 // comp_alg_input: String denoting desired compression algorithm
11 // Options: -1, 1, 2, 4
12 // if -1 is given, new file is compressed with the same comp alg as the old file
13 //
14 // comp_level:
15 // Options: -1, 0-9
16 // if -1 is given, and comp_alg=-1, then comp_level will be copied from the input file
17 // else it will be set to the default comp_levl for that alg
18 //
19 // auto_flush:
20 // Options: -1, any int
21 // if -1 is given, auto_flush for the CollectionTree will be set to the auto flush of the input CollectionTree
22 
23 #include "TFile.h"
24 #include "TTree.h"
25 #include "TKey.h"
26 
27 #include <iostream>
28 
29 using namespace std;
30 
31 void compression_tool(const char* input_filename, const char* output_filename, int comp_alg=-1, int comp_level=-1, int auto_flush=-1) {
32 
33  // Create output file, open input file
34  TFile* output_file = new TFile(output_filename, "recreate");
35  TFile* input_file = TFile::Open(input_filename);
36 
37  // Set comp_alg, comp_level, auto_flush if any were left -1
38  // comp_alg gets set to whatever the input file's algorithm is
39  // comp_level gets set to the default compression level for the given algorithm is
40  // unless comp_alg == -1, in which case it gets set to the input file's comp level
41  // auto_flush gets set to the auto flush value of the input file's CollectionTree
42  if (comp_alg == -1) {
43  comp_alg = input_file->GetCompressionAlgorithm();
44  if (comp_level == -1) {
45  comp_level = input_file->GetCompressionLevel();
46  }
47  }
48 
49  if (comp_level == -1) {
50  if (comp_alg == ROOT::kZLIB) comp_level = ROOT::RCompressionSetting::ELevel::kDefaultZLIB;
51  else if (comp_alg == ROOT::kLZMA) comp_level = ROOT::RCompressionSetting::ELevel::kDefaultLZMA;
52  else if (comp_alg == ROOT::kLZ4) comp_level = ROOT::RCompressionSetting::ELevel::kDefaultLZ4;
53  else comp_level = ROOT::RCompressionSetting::ELevel::kUseMin;
54  }
55 
56  if (auto_flush == -1) {
57  auto_flush = (static_cast<TTree*>(input_file->Get("CollectionTree")))->GetAutoFlush();
58  }
59 
60  // Set comp_alg and comp_level in the output file
61  output_file->SetCompressionAlgorithm(comp_alg);
62  output_file->SetCompressionLevel(comp_level);
63 
64  // Copy all the TTrees of the input file to the output file
65  // Set the auto flush of the CollectionTree to the variable auto_flush
66  TIter nextTree(input_file->GetListOfKeys());
67  for ( TKey* tree_key = static_cast<TKey*>(nextTree()); tree_key; tree_key = static_cast<TKey*>(nextTree()) ) {
68  if (strcmp(tree_key->GetClassName(), "TTree")) continue;
69  input_file->cd();
70  TTree* input_tree = static_cast<TTree*>(input_file->Get(tree_key->GetName()));
71  output_file->cd();
72  TTree* output_tree = static_cast<TTree*>(output_file->Get(tree_key->GetName()));
73  output_tree = input_tree->CloneTree(0);
74 
75  // Set auto flush in the new CollectionTree
76  if (!strcmp(tree_key->GetName(), "CollectionTree")) output_tree->SetAutoFlush(auto_flush);
77 
78  // Copy addresses to output_tree
79  input_tree->CopyAddresses(output_tree);
80 
81  // iterate over all the entries of the input tree, copying them to the output tree
82  int nentries = input_tree->GetEntriesFast();
83  for (int i=0; i < nentries; i++) {
84  input_tree->GetEntry(i);
85  output_tree->Fill();
86  }
87  }
88 
89  // write and close output file
90  output_file->Write();
91  output_file->Close();
92 }
93 
94 int usage() {
95  cout << "Tool to change compression settings of a ROOT file.\n"
96  "Usage: \n"
97  "-i <input filename>\n"
98  "-o <output filename>\n"
99  "-c <compression algorithm> 1==ZLIB, 2==LZMA, 4==LZ4, -1==Match input file\n"
100  "-l <compression level> Possible values: 1-9, -1==Match input file or default\n"
101  "-a <auto flush> Possible values: any int, -1==Match input file\n"
102  "-h Return usage details\n";
103  return 1;
104 }
105 
106 int main(int argc, char** argv) {
107  const char* input_filename = "";
108  const char* output_filename = "";
109  int comp_alg = -1;
110  int comp_level = -1;
111  int auto_flush = -1;
112 
113  // Read input arguments
114  for (int i=1; i<argc; ++i) {
115  if (*argv[i] == '-') {
116  switch(::toupper(*(argv[i]+1))) {
117  case 'I':
118  if (i+1<argc) input_filename = argv[i+1];
119  ++i;
120  break;
121  case 'O':
122  if (i+1<argc) output_filename = argv[i+1];
123  ++i;
124  break;
125  case 'C':
126  if (i+1<argc) comp_alg = atoi(argv[i+1]);
127  ++i;
128  break;
129  case 'L':
130  if (i+1<argc) comp_level = atoi(argv[i+1]);
131  ++i;
132  break;
133  case 'A':
134  if (i+1<argc) auto_flush = atoi(argv[i+1]);
135  ++i;
136  break;
137  case 'H':
138  return usage();
139  default:
140  return usage();
141  }
142  }
143  }
144 
145  // Verify valid input arguments
146  if (input_filename == nullptr) {
147  cout << "No input filename received" << endl;
148  return usage();
149  }
150  if (output_filename == nullptr) {
151  cout << "No output filename received" << endl;
152  return usage();
153  }
154 
155  compression_tool(input_filename, output_filename, comp_alg, comp_level, auto_flush);
156 
157  return 0;
158 }
python.resample_meson.input_file
input_file
Definition: resample_meson.py:164
compression_tool
void compression_tool(const char *input_filename, const char *output_filename, int comp_alg=-1, int comp_level=-1, int auto_flush=-1)
Definition: compressionTool.cxx:31
LArCellConditions.argv
argv
Definition: LArCellConditions.py:112
usage
int usage()
Definition: compressionTool.cxx:94
PlotCalibFromCool.nentries
nentries
Definition: PlotCalibFromCool.py:798
lumiFormat.i
int i
Definition: lumiFormat.py:92
MergeEBWeightsFiles.output_filename
output_filename
Definition: MergeEBWeightsFiles.py:119
mergePhysValFiles.output_file
output_file
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:27
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
main
int main(int argc, char **argv)
Definition: compressionTool.cxx:106
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
RTTAlgmain.output_tree
output_tree
Definition: RTTAlgmain.py:44