sum_root.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/perl
  2. sub print_help {
  3. print "sum_root v0.4 (Author: Nikita Ermakov, E-Mail: coffe92\@gmail.com)";
  4. print "\nExample of usage:\n\tperl sum_root.pl --path PATH --basename BASE_NAME --max MAX_FILES --comp COMPRESSION";
  5. print "\nOptions:";
  6. print "\n\t--path (-p)\tpath to the list of root files to merge (.list)";
  7. print "\n\t--basename (-bn)\tpart of the temp files name (default: tmp)\n";
  8. print "\n\t--max (-m)\tmaximum files to read (default: 10)\n";
  9. print "\n\t--comp (-c)\tcompression level (default: 9)\n";
  10. }
  11. my $comp = 9; # compression of the input files
  12. my $maxFiles = 10; # maximum files to merge
  13. sub sum_files {
  14. my $files = @_[0];
  15. my $baseName = @_[1];
  16. my $iter = @_[2];
  17. my $filesNum = $#files + 1;
  18. my $outFile;
  19. my $listAdd;
  20. my $list;
  21. my $counter = 0;
  22. $#list = -1;
  23. for (my $i = 1; $i <= $filesNum + 1; $i++) {
  24. if ((($i % ($maxFiles + 1)) == 0) || ($i == $filesNum + 1)) {
  25. if ($#listAdd != -1) {
  26. $outFile = $baseName."_".$iter."_".$counter.".root";
  27. push(@list, $outFile);
  28. system("hadd $outFile ".join(" ", @listAdd));
  29. print $listAdd."\n";
  30. $counter++;
  31. if ($iter > 0) {
  32. system("rm ".join(" ", @listAdd));
  33. }
  34. $#listAdd = -1;
  35. }
  36. }
  37. push(@listAdd, $files[$i - 1]);
  38. }
  39. return @list;
  40. }
  41. ###############
  42. # #
  43. # ENTRY POINT #
  44. # #
  45. ###############
  46. # Get and check arguments number
  47. my $argNum = $#ARGV + 1;
  48. if ($argNum == 0) {
  49. print_help();
  50. exit 0;
  51. }
  52. my $path = "";
  53. my $baseName = "";
  54. for (my $i = 0; $i < $argNum; $i++) {
  55. # Searching path argument
  56. if (($ARGV[$i] eq "--path") ||
  57. ($ARGV[$i] eq "-p")) {
  58. if (++$i < $argNum) {
  59. print "Using list file: ".$ARGV[$i]."\n";
  60. $path = $ARGV[$i];
  61. }
  62. }
  63. # Searching basename argument
  64. if (($ARGV[$i] eq "--basename") ||
  65. ($ARGV[$i] eq "-bn")) {
  66. if (++$i < $argNum) {
  67. print "Using basename for temporary files: ".$ARGV[$i]."\n";
  68. $baseName = $ARGV[$i];
  69. }
  70. }
  71. # Searching max files argument
  72. if (($ARGV[$i] eq "--max") ||
  73. ($ARGV[$i] eq "-m")) {
  74. if (++$i < $argNum) {
  75. print "Max files set to: ".$ARGV[$i]."\n";
  76. $maxFiles = $ARGV[$i];
  77. }
  78. }
  79. # Searching max files argument
  80. if (($ARGV[$i] eq "--comp") ||
  81. ($ARGV[$i] eq "-c")) {
  82. if (++$i < $argNum) {
  83. print "Compression set to: ".$ARGV[$i]."\n";
  84. $comp = $ARGV[$i];
  85. }
  86. }
  87. }
  88. if ($path eq "") {
  89. print "Please sepcify path to the list file\n";
  90. print_help();
  91. exit 0;
  92. }
  93. if ($baseName eq "") {
  94. $baseName = "tmp";
  95. print "Warning: using default basename for temporary files: tmp\n";
  96. }
  97. my $files;
  98. open(my $fd, $path)
  99. or die "Could not open file '$path' $!";
  100. # Create array with list of input files
  101. while (my $row = <$fd>) {
  102. chomp $row;
  103. push (@files, $row);
  104. }
  105. close($fd);
  106. my $iter = 0;
  107. while ($#files > 0) {
  108. @files = sum_files($files, $baseName, $iter++);
  109. }
  110. print "Output file name: $files[0]\n";
  111. exit 0;