ATLAS Offline Software
Functions
compressionTool.cxx File Reference
#include "TFile.h"
#include "TTree.h"
#include "TKey.h"
#include <iostream>
Include dependency graph for compressionTool.cxx:

Go to the source code of this file.

Functions

void compression_tool (const char *input_filename, const char *output_filename, int comp_alg=-1, int comp_level=-1, int auto_flush=-1)
 
int usage ()
 
int main (int argc, char **argv)
 

Function Documentation

◆ 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 at line 31 of file compressionTool.cxx.

31  {
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::RCompressionSetting::EAlgorithm::kZLIB )
51  comp_level = ROOT::RCompressionSetting::ELevel::kDefaultZLIB;
52  else if( comp_alg == ROOT::RCompressionSetting::EAlgorithm::kLZMA)
53  comp_level = ROOT::RCompressionSetting::ELevel::kDefaultLZMA;
54  else if( comp_alg == ROOT::RCompressionSetting::EAlgorithm::kLZ4 )
55  comp_level = ROOT::RCompressionSetting::ELevel::kDefaultLZ4;
56  else comp_level = ROOT::RCompressionSetting::ELevel::kUseMin;
57  }
58 
59  if (auto_flush == -1) {
60  auto_flush = (static_cast<TTree*>(input_file->Get("CollectionTree")))->GetAutoFlush();
61  }
62 
63  // Set comp_alg and comp_level in the output file
64  output_file->SetCompressionAlgorithm(comp_alg);
65  output_file->SetCompressionLevel(comp_level);
66 
67  // Copy all the TTrees of the input file to the output file
68  // Set the auto flush of the CollectionTree to the variable auto_flush
69  TIter nextTree(input_file->GetListOfKeys());
70  for ( TKey* tree_key = static_cast<TKey*>(nextTree()); tree_key; tree_key = static_cast<TKey*>(nextTree()) ) {
71  if (strcmp(tree_key->GetClassName(), "TTree")) continue;
72  input_file->cd();
73  TTree* input_tree = static_cast<TTree*>(input_file->Get(tree_key->GetName()));
74  output_file->cd();
75  TTree* output_tree = static_cast<TTree*>(output_file->Get(tree_key->GetName()));
76  output_tree = input_tree->CloneTree(0);
77 
78  // Set auto flush in the new CollectionTree
79  if (!strcmp(tree_key->GetName(), "CollectionTree")) output_tree->SetAutoFlush(auto_flush);
80 
81  // Copy addresses to output_tree
82  input_tree->CopyAddresses(output_tree);
83 
84  // iterate over all the entries of the input tree, copying them to the output tree
85  int nentries = input_tree->GetEntriesFast();
86  for (int i=0; i < nentries; i++) {
87  input_tree->GetEntry(i);
88  output_tree->Fill();
89  }
90  }
91 
92  // write and close output file
93  output_file->Write();
94  output_file->Close();
95 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 109 of file compressionTool.cxx.

109  {
110  const char* input_filename = "";
111  const char* output_filename = "";
112  int comp_alg = -1;
113  int comp_level = -1;
114  int auto_flush = -1;
115 
116  // Read input arguments
117  for (int i=1; i<argc; ++i) {
118  if (*argv[i] == '-') {
119  switch(::toupper(*(argv[i]+1))) {
120  case 'I':
121  if (i+1<argc) input_filename = argv[i+1];
122  ++i;
123  break;
124  case 'O':
125  if (i+1<argc) output_filename = argv[i+1];
126  ++i;
127  break;
128  case 'C':
129  if (i+1<argc) comp_alg = atoi(argv[i+1]);
130  ++i;
131  break;
132  case 'L':
133  if (i+1<argc) comp_level = atoi(argv[i+1]);
134  ++i;
135  break;
136  case 'A':
137  if (i+1<argc) auto_flush = atoi(argv[i+1]);
138  ++i;
139  break;
140  case 'H':
141  return usage();
142  default:
143  return usage();
144  }
145  }
146  }
147 
148  // Verify valid input arguments
149  if (input_filename == nullptr) {
150  cout << "No input filename received" << endl;
151  return usage();
152  }
153  if (output_filename == nullptr) {
154  cout << "No output filename received" << endl;
155  return usage();
156  }
157 
158  compression_tool(input_filename, output_filename, comp_alg, comp_level, auto_flush);
159 
160  return 0;
161 }

◆ usage()

StatusCode usage ( )

Definition at line 97 of file compressionTool.cxx.

97  {
98  cout << "Tool to change compression settings of a ROOT file.\n"
99  "Usage: \n"
100  "-i <input filename>\n"
101  "-o <output filename>\n"
102  "-c <compression algorithm> 1==ZLIB, 2==LZMA, 4==LZ4, -1==Match input file\n"
103  "-l <compression level> Possible values: 1-9, -1==Match input file or default\n"
104  "-a <auto flush> Possible values: any int, -1==Match input file\n"
105  "-h Return usage details\n";
106  return 1;
107 }
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
usage
int usage()
Definition: compressionTool.cxx:97
PlotCalibFromCool.nentries
nentries
Definition: PlotCalibFromCool.py:798
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
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
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