15 # include "TDirIter.h"
16 # include "StFileIter.h"
17 # include "TFileIter.h"
21 # include "TStopwatch.h"
25 void MergeSimpleHistogramFile(
const Char_t *TargetName=0,
const Char_t *inputFilesPattern=0)
28 if (TargetName && TargetName[0] && inputFilesPattern && inputFilesPattern[0] ) {
30 Int_t fileCounter = 0;
31 Int_t histogramCounter = 0;
33 TFile *outFile = TFile::Open(TargetName,
"RECREATE");
34 TDirIter listOfFiles(inputFilesPattern);
35 const char *fileName = 0;
36 while ( (fileName = listOfFiles.NextFile() ) ) {
41 while ( (obj = *file) ) {
42 if ( obj->IsA()->InheritsFrom(
"TH1" ) ) {
47 TH1 *dstHistogram = 0;
49 if ( (dstHistogram = (TH1 *)outFile->FindObject(h1->GetName()))) {
51 dstHistogram->Add(h1);
56 h1->SetDirectory(outFile);
57 printf(
" The new Histogram found: %s \n", h1->GetName() );
67 printf(
"\n Finishing . . . \n");
72 printf(
" Total files merged: %d \n", fileCounter);
73 printf(
" Total histograms merged: %d \n", histogramCounter);
76 printf(
"\nUsage: root MergeHistogramFile.C(\"DestinationFileName\",\"InputFilesPattern\",kTRUE)\n");
77 printf(
"------ where InputFilesPattern ::= <regexp_pattern_for_the_input_files>|@indirect_file_list\n");
78 printf(
" indirect_file_list ::= a text file with the list of the files\n");
79 printf(
" indirect_file_list can be create by the shell command:\n");
80 printf(
" ls -1 --color=never *.root>indirect_file_list \n\n");
85 void MergeComplexHistogramFile(
const Char_t *TargetName=0,
const Char_t *inputFilesPattern=0)
87 if (TargetName && TargetName[0] && inputFilesPattern && inputFilesPattern[0] ) {
88 printf(
" An experimental version of macro.\n");
90 Int_t fileCounter = 0;
92 Int_t treeCounter = 0;
93 Int_t histogramCounter = 0;
95 TFile *outFile = TFile::Open(TargetName,
"RECREATE");
96 TDirectory *outDir = outFile;
97 TDirIter listOfFiles(inputFilesPattern);
98 const char *fileName = 0;
99 while ( (fileName = listOfFiles.NextFile() ) ) {
100 Int_t currentDirDepth = 0;
105 while ( (obj = *file) ) {
106 Int_t depth = file.GetDepth();
107 while (depth < currentDirDepth) {
108 outDir = outDir->GetMotherDir();
111 if ( obj->IsA()->InheritsFrom(TH1::Class()) ) {
116 TH1 *dstHistogram = 0;
118 if ( (dstHistogram = (TH1 *)outDir->FindObject(h1->GetName()))) {
120 dstHistogram->Add(h1);
125 h1->SetDirectory(outDir);
126 printf(
" The new Histogram found: %s \n", h1->GetName() );
129 }
else if ( obj->IsA()->InheritsFrom(TTree::Class()) ) {
132 TTree *tree = (TTree*)obj;
135 if ( (dstTree = (TTree *)outDir->FindObject(tree->GetName()))) {
139 TList *nextTree =
new TList(); nextTree->Add(tree);
140 dstTree->Merge(nextTree);
146 TDirectory *saveDir = 0;
147 if (outDir != gDirectory) {
148 saveDir = gDirectory;
151 TList *nextTree =
new TList(); nextTree->Add(tree);
152 dstTree = TTree::MergeTrees(nextTree);
153 if (saveDir) saveDir->cd();
160 }
else if ( obj->IsA()->InheritsFrom(TDirectory::Class()) ) {
161 printf(
"The input sub-TDirectory object: %s depth=%d\n",obj->GetName(), depth);
162 TDirectory *d = (TDirectory *)outDir->FindObject(obj->GetName());
164 d = outDir->mkdir(obj->GetName());
166 printf(
"The new TDirectory object: %s depth=%d\n",d->GetPathStatic(), depth);
170 printf(
"The output sub-TDirectory object: %s depth=%d\n",outDir->GetPathStatic(), depth);
174 printf(
"I have no idea how to merge the %s objects of the %s class. Skipping .... \n",obj->GetName(), obj->ClassName() );
180 printf(
"\n Finishing . . . \n");
184 if (fileCounter) printf(
" Total files merged: %d \n", fileCounter);
185 if (dirCounter) printf(
" Total TDirectory objects merged: %d \n", dirCounter);
186 if (histogramCounter) printf(
" Total histograms merged: %d \n", histogramCounter);
187 if (treeCounter) printf(
" Total TTree\'s merged: %d \n",treeCounter);
188 if (dirCounter || treeCounter) printf(
" You have used the experimental version of the program. Please check the output file\n");
192 printf(
"\nUsage: root MergeHistogramFile.C(\"DestinationFileName\",\"InputFilesPattern\")\n");
193 printf(
"------ where InputFilesPattern ::= <regexp_pattern_for_the_input_files>|@indirect_file_list\n");
194 printf(
" indirect_file_list ::= a text file with the list of the files\n");
195 printf(
" indirect_file_list can be create by the shell command:\n");
196 printf(
" ls -1 *.root>indirect_file_list \n\n");
203 void MergeHistogramFile(
const Char_t *TargetName=0,
const Char_t *inputFilesPattern=0, Bool_t simple=kFALSE)
205 if (TargetName && TargetName[0] && inputFilesPattern && inputFilesPattern[0] ) {
207 Long64_t maxsize = 100000000;
209 TTree::SetMaxTreeSize(maxsize);
212 MergeSimpleHistogramFile(TargetName, inputFilesPattern);
215 gSystem->Load(
"St_base");
217 MergeComplexHistogramFile(TargetName, inputFilesPattern);
220 printf(
"\nUsage: root MergeHistogramFile.C(\"DestinationFileName\",\"InputFilesPattern\",kTRUE)\n");
221 printf(
"------ where InputFilesPattern ::= <regexp_pattern_for_the_input_files>|@indirect_file_list\n");
222 printf(
" indirect_file_list ::= a text file with the list of the files\n");
223 printf(
" indirect_file_list can be create by the shell command:\n\n");
224 printf(
" ls -1 --color=never *.root>indirect_file_list \n\n");
225 printf(
" The last parameter defines whether one wants to merge the \"simple\" ROOT files\n");
226 printf(
" The \"simple\" ROOT files are those with no sub-TDirectrory objects inside and with no TTree/TNtuples\n");
227 printf(
" This is the default option and it can be omitted\n");
228 printf(
" To merge the ROOT files with sub-TDirectory or / and TTree pass the kFALSE as the third macro parameter\n\n");
229 printf(
"------------- To use with ACliC - load the \"St_base.so\" shared library first\n");