1313#include < fstream>
1414#include < getopt.h>
1515
16+ #include " TSystem.h"
1617#include " TFile.h"
1718#include " TTree.h"
1819#include " TList.h"
@@ -28,13 +29,16 @@ int main(int argc, char* argv[])
2829 std::string inputCollection (" input.txt" );
2930 std::string outputFileName (" AO2D.root" );
3031 long maxDirSize = 100000000 ;
32+ bool skipNonExistingFiles = false ;
33+ int exitCode = 0 ; // 0: success, 1: failure
3134
3235 int option_index = 0 ;
3336 static struct option long_options[] = {
3437 {" input" , required_argument, nullptr , 0 },
3538 {" output" , required_argument, nullptr , 1 },
3639 {" max-size" , required_argument, nullptr , 2 },
37- {" help" , no_argument, nullptr , 3 },
40+ {" skip-non-existing-files" , no_argument, nullptr , 3 },
41+ {" help" , no_argument, nullptr , 4 },
3842 {nullptr , 0 , nullptr , 0 }};
3943
4044 while (true ) {
@@ -48,10 +52,13 @@ int main(int argc, char* argv[])
4852 } else if (c == 2 ) {
4953 maxDirSize = atol (optarg);
5054 } else if (c == 3 ) {
55+ skipNonExistingFiles = true ;
56+ } else if (c == 4 ) {
5157 printf (" AOD merging tool. Options: \n " );
5258 printf (" --input <inputfile.txt> Contains path to files to be merged. Default: %s\n " , inputCollection.c_str ());
5359 printf (" --output <outputfile.root> Target output ROOT file. Default: %s\n " , outputFileName.c_str ());
54- printf (" --max-size <size in Bytes> Target directory size: %ld \n " , maxDirSize);
60+ printf (" --max-size <size in Bytes> Target directory size. Default: %ld\n " , maxDirSize);
61+ printf (" --skip-non-existing-files Flag to allow skipping of non-existing files in the intput list.\n " );
5562 return -1 ;
5663 } else {
5764 return -2 ;
@@ -62,6 +69,9 @@ int main(int argc, char* argv[])
6269 printf (" Input file: %s\n " , inputCollection.c_str ());
6370 printf (" Ouput file name: %s\n " , outputFileName.c_str ());
6471 printf (" Maximal folder size (uncompressed): %ld\n " , maxDirSize);
72+ if (skipNonExistingFiles) {
73+ printf (" WARNING: Skipping non-existing files.\n " );
74+ }
6575
6676 std::map<std::string, TTree*> trees;
6777 std::map<std::string, int > offsets;
@@ -93,6 +103,17 @@ int main(int argc, char* argv[])
93103 printf (" Processing input file: %s\n " , line.Data ());
94104
95105 auto inputFile = TFile::Open (line);
106+ if (!inputFile) {
107+ printf (" Error: Could not open input file %s.\n " , line.Data ());
108+ if (skipNonExistingFiles) {
109+ continue ;
110+ } else {
111+ printf (" Aborting merge!\n " );
112+ exitCode = 1 ;
113+ break ;
114+ }
115+ }
116+
96117 TList* keyList = inputFile->GetListOfKeys ();
97118 keyList->Sort ();
98119
@@ -234,9 +255,9 @@ int main(int argc, char* argv[])
234255 }
235256
236257 if (currentDirSize > maxDirSize) {
237- printf (" Maximum size reached: %ld. Closing folder.\n " , currentDirSize);
258+ printf (" Maximum size reached: %ld. Closing folder %s .\n " , currentDirSize, dfName );
238259 for (auto const & tree : trees) {
239- // printf("Writing %s\n", tree.first.c_str());
260+ // printf("Writing %s\n", tree.first.c_str());
240261 outputDir->cd ();
241262 tree.second ->Write ();
242263 delete tree.second ;
@@ -249,10 +270,17 @@ int main(int argc, char* argv[])
249270 }
250271 inputFile->Close ();
251272 }
273+
252274 outputFile->Write ();
253275 outputFile->Close ();
254276
277+ // in case of failure, remove the incomplete file
278+ if (exitCode) {
279+ printf (" Removing incomplete output file %s.\n " , outputFile->GetName ());
280+ gSystem ->Unlink (outputFile->GetName ());
281+ }
282+
255283 printf (" AOD merger finished.\n " );
256284
257- return 0 ;
285+ return exitCode ;
258286}
0 commit comments