9#if !defined(__CLING__) || defined(__MAKECLING__)
35 vector<TString> files;
37 stringstream ss(pcFileList ==
nullptr ?
"" : pcFileList);
40 TString fileName(token.c_str());
41 gSystem->ExpandPathName(fileName);
42 files.push_back(fileName);
52 if (directory ==
nullptr || directory->
GetListOfKeys() ==
nullptr)
58 while ((key = (
TKey*)next()) !=
nullptr) {
64 TString fullName = (prefix.Length() == 0) ? objectName : prefix +
"/" + objectName;
85 if (file ==
nullptr || file->
IsZombie()) {
86 cout <<
"ERROR: cannot open ROOT file \"" << fileName <<
"\" to detect TTree name" << endl;
87 if (file !=
nullptr) {
107 for (
size_t i = 1;
i < files.size(); ++
i) {
109 if (treeName.
Length() > 0) {
110 cout <<
"Detected TTree name for TChain: \"" << treeName <<
"\" from file \"" << files[
i] <<
"\"" << endl;
114 cout <<
"WARNING: no TTree object was found in partial file \"" << files[
i] <<
"\"" << endl;
125 if (file ==
nullptr || file->
IsZombie()) {
126 cout <<
"ERROR: cannot open ROOT file \"" << fileName <<
"\"" << endl;
127 if (file !=
nullptr) {
148void merge(
const char* pcFileList,
int mode = 1)
152 if ((mode < 0) || (mode > 2)) {
153 cout <<
"Second parameter ('mode') can be 0-2 only. The mode is assigned to a default value (1)." << endl;
158 cout <<
"ERROR: empty file list, nothing has been merged" << endl;
162 if (files.size() == 1) {
163 cout <<
"Only one file name in the input string, nothing has been merged" << endl;
167 TString sResultFile = files[0];
174 bool isError =
false;
181 for (
size_t i = 2;
i < files.size(); ++
i)
182 mergerPart.
AddFile(files[
i].Data());
189 if (treeName.
Length() > 0) {
194 treeName.
Remove(0, slash + 1);
198 cout <<
"WARNING: no TTree was found in partial output files, merging all their objects" << endl;
204 cout <<
"ERROR: partial merging to the result file failed" << endl;
214 res = mergerFull.
Merge();
217 cout <<
"ERROR: full merging to the result file failed" << endl;
226 for (
size_t i = 1;
i < files.size(); ++
i) {
227 if (remove(files[
i].Data()) != 0) {
229 cout <<
"ERROR: deleting temporary file " << files[
i] <<
" failed" << endl;
234 cout <<
"There were errors while temporary files removing" << endl;
236 cout << endl <<
"Temporary files were successfully removed" << endl;
238 cout <<
"Partial result files were merged and preserved" << endl;
244 if (treeName.
Length() == 0) {
245 cout <<
"ERROR: no TTree was found in partial output files. "
246 <<
"TChain cannot be created. Check that merge=\"chain\" is used only for ROOT files containing TTree "
253 for (
size_t i = 1;
i < files.size(); ++
i) {
255 cout <<
"ERROR: partial file \"" << files[
i] <<
"\" does not contain TTree \"" << treeName
256 <<
"\". TChain was not created." << endl;
263 for (
size_t i = 1;
i < files.size(); ++
i)
264 chainUnion.
Add(files[
i].Data());
266 TFile fChain(sResultFile,
"RECREATE");
268 cout <<
"ERROR: result TChain file \"" << sResultFile <<
"\" was not created" << endl;
276 cout <<
"The Chain with " << events <<
" event(s) was written to file \"" << sResultFile
277 <<
"\" using TTree name \"" << treeName <<
"\" and pointing to following files:" << endl;
280 TIter next(fileElements);
R__EXTERN TSystem * gSystem
TObjArray * GetListOfFiles() const
virtual Int_t Add(const char *name, Long64_t nentries=TTree::kMaxEntries)
Long64_t GetEntries() const override
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
Bool_t InheritsFrom(const char *cl) const override
T * Get(const char *namecycle)
virtual TList * GetListOfKeys() const
virtual Bool_t OutputFile(const char *url, Bool_t force)
virtual Bool_t Merge(Bool_t=kTRUE)
void AddObjectNames(const char *name)
virtual Bool_t AddFile(const char *url, Bool_t cpProgress=kTRUE)
virtual Bool_t PartialMerge(Int_t type=kAll|kIncremental)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
void Close(Option_t *option="") override
virtual const char * GetClassName() const
virtual TObject * ReadObj()
const char * GetName() const override
const char * GetTitle() const override
R__ALWAYS_INLINE Bool_t IsZombie() const
virtual Bool_t InheritsFrom(const char *classname) const
const char * Data() const
Ssiz_t Last(char c) const
TString & Remove(EStripType s, char c)
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) const override
TString detect_tree_name_from_partial_files(const vector< TString > &files)
vector< TString > split_file_list(const char *pcFileList)
bool file_contains_tree(const TString &fileName, const TString &treeName)
TString find_tree_name_in_file(const TString &fileName)
void merge(const char *pcFileList, int mode=1)
TString find_tree_name_in_directory(TDirectory *directory, const TString &prefix="")