]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG2/FLOW/macros/mergeOutput.C
Add include
[u/mrichter/AliRoot.git] / PWG2 / FLOW / macros / mergeOutput.C
CommitLineData
57340a27 1// Macro mergeOutput.C is used to merge output files "AnalysisResults.root" locally.
2// (To merge output files "AnalysisResults.root" on Grid use macro mergeOutputOnGrid.C.)
3// Its usage relies on the following directory structure and naming conventions:
ad87ae62 4// 1.) In directory <dir> there are subdirectories <subdir1>, <subdir2>, ...;
57340a27 5// 2.) In subdirectory <subdir*> there is a file named "AnalysisResults.root";
6// 3.) Copy macro mergeOutput.C in directory <dir> and launch it. It will automatically
7// access from each subdirectory a file "AnalysisResults.root" and by making use of
8// TFileMerger utility it will produce a merged file "mergedAnalysisResults.root"
9// and save it in directory <dir>;
ad87ae62 10// 4.) IMPORTANT: Macro mergeOutput.C must be used in a combination with macro redoFinish.C
11// in order to get the analysis done on merged, large statistics sample. In particular,
12// after you got the file "mergedAnalysisResults.root", copy macro redoFinish.C in
13// directory <dir> and launch it. This macro will access "mergedAnalysisResults.root"
14// and produce the output file "AnalysisResults.root" in directory <dir>. This file will
15// hold the final results for merged, large statistics sample.
16// 5.) REMARK: To see plots for some of the results use macro compareFlowResults.C. This macro
17// accesses file "AnalysisResults.root" and produces couple of predefined example plots.
18
57340a27 19void mergeOutput()
fe11f681 20{
57340a27 21 // Name of the output files to be merged:
22 TString outputFileName = "AnalysisResults.root";
23 // Name of the merged, large statistics file:
24 TString mergedFileName = "mergedAnalysisResults.root";
25 // Load needed flow libraries:
26 gSystem->AddIncludePath("-I$ROOTSYS/include");
27 gSystem->AddIncludePath("-I$ALICE_ROOT/include");
28 gSystem->Load("libPWG2flowCommon");
29 cerr<<"Library \"libPWG2flowCommon\" loaded ...."<<endl;
30 // For a large number of output files merging is done in cycles
31 // and this is the cycle period:
32 const Int_t cycle = 500;
33 if(cycle>500)
fe11f681 34 {
9455e15e 35 cout<<"WARNING: Cycle is too big !!!!"<<endl;
57340a27 36 cout<<" Set \"const Int_t cycle\" to smaller value in the macro."<<endl;
9455e15e 37 exit(0);
fe11f681 38 }
57340a27 39 // Standard magic:
9455e15e 40 TString *baseDirPath = new TString(gSystem->pwd());
ad87ae62 41 TSystemDirectory *baseDir = new TSystemDirectory(".",baseDirPath->Data());
42 TList *listOfFilesInBaseDir = baseDir->GetListOfFiles();
57340a27 43 TStopwatch timer;
44 timer.Start();
9455e15e 45 // listOfFilesInBaseDir->Print();
46 Int_t nFiles = listOfFilesInBaseDir->GetEntries();
57340a27 47 // loop over all files and from each subdirectory add file AnalysisResults.root to TFileMerger:
ad87ae62 48 Int_t fileCounter = 0;
57340a27 49 TFileMerger *fileMerger = new TFileMerger();
9455e15e 50 for(Int_t iFile=0;iFile<nFiles;iFile++)
fe11f681 51 {
57340a27 52 TSystemFile *currentFile = (TSystemFile*)listOfFilesInBaseDir->At(iFile);
53 // Consider only subdirectories:
54 if(!currentFile ||
55 !currentFile->IsDirectory() ||
56 strcmp(currentFile->GetName(), ".") == 0 ||
57 strcmp(currentFile->GetName(), "..") == 0) continue;
58 // Accessing the output file "AnalysisResults.root" in current subdirectory:
59 TString currentSubDirName = baseDirPath->Data();
60 (currentSubDirName+="/")+=currentFile->GetName();
61 currentSubDirName+="/";
62 TString fileName = currentSubDirName;
ad87ae62 63 fileName+=outputFileName.Data();
ad87ae62 64 if(!(gSystem->AccessPathName(fileName.Data(),kFileExists)))
947cc449 65 {
ad87ae62 66 fileCounter++;
67 fileMerger->AddFile(fileName.Data());
57340a27 68 // Merging in cycles:
ad87ae62 69 if(fileCounter % cycle == 0)
9455e15e 70 {
57340a27 71 // If the merged output from previous cycle exists add it to TFileMerger:
72 TString *mergedFileForPreviousCycle = new TString("mergedCycle");
ad87ae62 73 (*mergedFileForPreviousCycle)+=(fileCounter/cycle - 1);
74 (*mergedFileForPreviousCycle)+=".root";
ad87ae62 75 if(!(gSystem->AccessPathName(mergedFileForPreviousCycle->Data(),kFileExists)))
9455e15e 76 {
ad87ae62 77 fileMerger->AddFile(mergedFileForPreviousCycle->Data());
57340a27 78 // Delete merged output from previous cycle:
79 TSystemFile *file = new TSystemFile(mergedFileForPreviousCycle->Data(),baseDirPath->Data());
9455e15e 80 file->Delete();
81 delete file;
57340a27 82 }
83 // Create merged output for current cycle:
84 TString *mergedFileForCurrentCycle = new TString("mergedCycle");
ad87ae62 85 (*mergedFileForCurrentCycle)+=(fileCounter/cycle);
57340a27 86 (*mergedFileForCurrentCycle)+=".root";
ad87ae62 87 fileMerger->OutputFile(mergedFileForCurrentCycle->Data());
88 fileMerger->Merge();
57340a27 89 fileMerger->Reset();
ad87ae62 90 delete mergedFileForPreviousCycle;
91 delete mergedFileForCurrentCycle;
92 } // end of if(fileCounter % cycle == 0)
93 } // end of if(!(gSystem->AccessPathName(fileName.Data(),kFileExists)))
9455e15e 94 } // end of for(Int_t iFile=0;iFile<nFiles;iFile++)
fe11f681 95
9455e15e 96
97 //=================================================================================================
98
99
57340a27 100 // Final merging at the end of the day (3 distinct cases):
101 if(fileCounter==0)
102 {
103 cout<<endl;
104 cout<<"Merger wasn't lucky: Couldn't find a single file "<<outputFileName.Data()<<" to merge"<<endl;
105 cout<<"in subdirectories of directory "<<baseDirPath->Data()<<endl;
106 cout<<endl;
107 exit(0);
108 }
9455e15e 109 gSystem->cd(baseDirPath->Data());
ad87ae62 110 if(fileCounter < cycle)
111 {
57340a27 112 fileMerger->OutputFile(mergedFileName.Data());
ad87ae62 113 fileMerger->Merge();
57340a27 114 } else if(fileCounter % cycle == 0)
ad87ae62 115 {
57340a27 116 TString *mergedFileForPreviousCycle = new TString("mergedCycle");
ad87ae62 117 (*mergedFileForPreviousCycle)+=(fileCounter/cycle);
118 (*mergedFileForPreviousCycle)+=".root";
119 if(!(gSystem->AccessPathName(mergedFileForPreviousCycle->Data(),kFileExists)))
120 {
57340a27 121 gSystem->Rename(mergedFileForPreviousCycle->Data(),mergedFileName.Data());
122 }
9455e15e 123 } else
124 {
57340a27 125 TString *mergedFileForPreviousCycle = new TString("mergedCycle");
ad87ae62 126 (*mergedFileForPreviousCycle)+=((Int_t)fileCounter/cycle);
57340a27 127 (*mergedFileForPreviousCycle)+=".root";
ad87ae62 128 fileMerger->AddFile(mergedFileForPreviousCycle->Data());
57340a27 129 // Delete merged output from previous cycle:
130 TSystemFile *file = new TSystemFile(mergedFileForPreviousCycle->Data(),baseDirPath->Data());
9455e15e 131 file->Delete();
132 delete file;
57340a27 133 fileMerger->OutputFile(mergedFileName.Data());
ad87ae62 134 fileMerger->Merge();
ad87ae62 135 delete mergedFileForPreviousCycle;
9455e15e 136 }
ad87ae62 137 delete fileMerger;
9455e15e 138 delete baseDirPath;
139 delete baseDir;
fe11f681 140
57340a27 141 cout<<endl;
142 timer.Stop();
143 timer.Print();
144 cout<<endl;
145 if(!(gSystem->AccessPathName(mergedFileName.Data(),kFileExists)))
146 {
147 cout<<"Merging went successfully: "<<fileCounter<<" files "<<outputFileName.Data()<<" were"<<endl;
148 cout<<"merged into the newly created file "<<mergedFileName.Data()<<"."<<endl;
149 cout<<endl;
150 cout<<"Launch now macro redoFinish.C to get the correct final results."<<endl;
151 } else
152 {
153 cout<<"WARNING: Merging failed !!!!"<<endl;
154 }
155 cout<<endl;
156} // End of void mergeOutput()