]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/FLOW/macros/mergeOutput.C
In AliMUONSimpleClusterServer
[u/mrichter/AliRoot.git] / PWG2 / FLOW / macros / mergeOutput.C
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:
4 //  1.) In directory <dir> there are subdirectories <subdir1>, <subdir2>, ...;   
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>;
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
19 enum libModes {mLocal,mLocalSource};
20
21 void mergeOutput(Int_t mode=mLocal)
22 {
23  // mode: if mode = mLocal: analyze data on your computer using aliroot
24  //       if mode = mLocalSource: analyze data on your computer using root + source files 
25  // Name of the output files to be merged:
26  TString outputFileName = "AnalysisResults.root";
27  // Name of the merged, large statistics file:
28  TString mergedFileName = "mergedAnalysisResults.root";
29  // For a large number of output files merging is done in cycles
30  // and this is the cycle period: 
31  const Int_t cycle = 500;
32  if(cycle>500)
33  {
34   cout<<"WARNING: Cycle is too big !!!!"<<endl; 
35   cout<<"         Set \"const Int_t cycle\" to smaller value in the macro."<<endl;
36   exit(0);
37  }
38  // Load needed flow libraries:
39  LoadLibrariesMO(mode);  
40  // Standard magic:
41  TString *baseDirPath = new TString(gSystem->pwd());
42  TSystemDirectory *baseDir = new TSystemDirectory(".",baseDirPath->Data());          
43  TList *listOfFilesInBaseDir = baseDir->GetListOfFiles();
44  TStopwatch timer;
45  timer.Start();
46  // listOfFilesInBaseDir->Print();
47  Int_t nFiles = listOfFilesInBaseDir->GetEntries();
48  // loop over all files and from each subdirectory add file AnalysisResults.root to TFileMerger:
49  Int_t fileCounter = 0;
50  TFileMerger *fileMerger = new TFileMerger(); 
51  for(Int_t iFile=0;iFile<nFiles;iFile++)
52  {
53   TSystemFile *currentFile = (TSystemFile*)listOfFilesInBaseDir->At(iFile);
54   // Consider only subdirectories: 
55   if(!currentFile || 
56      !currentFile->IsDirectory() || 
57      strcmp(currentFile->GetName(), ".") == 0 || 
58      strcmp(currentFile->GetName(), "..") == 0) continue; 
59   // Accessing the output file "AnalysisResults.root" in current subdirectory: 
60   TString currentSubDirName = baseDirPath->Data();
61   (currentSubDirName+="/")+=currentFile->GetName();
62   currentSubDirName+="/";
63   TString fileName = currentSubDirName; 
64   fileName+=outputFileName.Data();
65   if(!(gSystem->AccessPathName(fileName.Data(),kFileExists)))
66   {
67    fileCounter++;
68    fileMerger->AddFile(fileName.Data());
69    // Merging in cycles:
70    if(fileCounter % cycle == 0)
71    {
72     // If the merged output from previous cycle exists add it to TFileMerger:
73     TString *mergedFileForPreviousCycle = new TString("mergedCycle"); 
74     (*mergedFileForPreviousCycle)+=(fileCounter/cycle - 1);
75     (*mergedFileForPreviousCycle)+=".root";
76     if(!(gSystem->AccessPathName(mergedFileForPreviousCycle->Data(),kFileExists)))
77     {
78      fileMerger->AddFile(mergedFileForPreviousCycle->Data());
79      // Delete merged output from previous cycle:
80      TSystemFile *file = new TSystemFile(mergedFileForPreviousCycle->Data(),baseDirPath->Data());
81      file->Delete();
82      delete file;
83     }    
84     // Create merged output for current cycle:
85     TString *mergedFileForCurrentCycle = new TString("mergedCycle"); 
86     (*mergedFileForCurrentCycle)+=(fileCounter/cycle);
87     (*mergedFileForCurrentCycle)+=".root";    
88     fileMerger->OutputFile(mergedFileForCurrentCycle->Data());
89     fileMerger->Merge();
90     fileMerger->Reset();    
91     delete mergedFileForPreviousCycle;
92     delete mergedFileForCurrentCycle;
93    } // end of if(fileCounter % cycle == 0) 
94   } // end of if(!(gSystem->AccessPathName(fileName.Data(),kFileExists))) 
95  } // end of for(Int_t iFile=0;iFile<nFiles;iFile++)
96  
97  
98  //=================================================================================================
99  
100  
101  // Final merging at the end of the day (3 distinct cases):
102  if(fileCounter==0)
103  {
104   cout<<endl;
105   cout<<"Merger wasn't lucky: Couldn't find a single file "<<outputFileName.Data()<<" to merge"<<endl;
106   cout<<"in subdirectories of directory "<<baseDirPath->Data()<<endl;
107   cout<<endl;
108   exit(0);
109  }
110  gSystem->cd(baseDirPath->Data());
111  if(fileCounter < cycle)
112  {
113   fileMerger->OutputFile(mergedFileName.Data());
114   fileMerger->Merge();
115  } else if(fileCounter % cycle == 0)
116    {
117     TString *mergedFileForPreviousCycle = new TString("mergedCycle"); 
118     (*mergedFileForPreviousCycle)+=(fileCounter/cycle);
119     (*mergedFileForPreviousCycle)+=".root";    
120     if(!(gSystem->AccessPathName(mergedFileForPreviousCycle->Data(),kFileExists)))
121     {
122      gSystem->Rename(mergedFileForPreviousCycle->Data(),mergedFileName.Data());
123     }     
124    } else
125      {
126       TString *mergedFileForPreviousCycle = new TString("mergedCycle"); 
127       (*mergedFileForPreviousCycle)+=((Int_t)fileCounter/cycle);
128       (*mergedFileForPreviousCycle)+=".root";      
129       fileMerger->AddFile(mergedFileForPreviousCycle->Data());
130       // Delete merged output from previous cycle:
131       TSystemFile *file = new TSystemFile(mergedFileForPreviousCycle->Data(),baseDirPath->Data());
132       file->Delete();
133       delete file;
134       fileMerger->OutputFile(mergedFileName.Data());
135       fileMerger->Merge();
136       delete mergedFileForPreviousCycle;
137      }
138  delete fileMerger;
139  delete baseDirPath;
140  delete baseDir;
141  
142  cout<<endl;
143  timer.Stop();
144  timer.Print(); 
145  cout<<endl;
146  if(!(gSystem->AccessPathName(mergedFileName.Data(),kFileExists)))
147  {
148   cout<<"Merging went successfully: "<<fileCounter<<" files "<<outputFileName.Data()<<" were"<<endl;
149   cout<<"merged into the newly created file "<<mergedFileName.Data()<<"."<<endl;
150   cout<<endl;
151   cout<<"Launch now macro redoFinish.C to get the correct final results."<<endl;
152  } else
153    {
154     cout<<"WARNING: Merging failed !!!!"<<endl;
155    } 
156  cout<<endl;
157 } // End of void mergeOutput(Int_t mode=mLocal)
158
159
160 void LoadLibrariesMO(const libModes mode) {
161   
162   //--------------------------------------
163   // Load the needed libraries most of them already loaded by aliroot
164   //--------------------------------------
165   //gSystem->Load("libTree");
166   gSystem->Load("libGeom");
167   gSystem->Load("libVMC");
168   gSystem->Load("libXMLIO");
169   gSystem->Load("libPhysics");
170   
171   //----------------------------------------------------------
172   // >>>>>>>>>>> Local mode <<<<<<<<<<<<<< 
173   //----------------------------------------------------------
174   if (mode==mLocal) {
175     //--------------------------------------------------------
176     // If you want to use already compiled libraries 
177     // in the aliroot distribution
178     //--------------------------------------------------------
179
180   //==================================================================================  
181   //load needed libraries:
182   gSystem->AddIncludePath("-I$ROOTSYS/include");
183   //gSystem->Load("libTree");
184
185   // for AliRoot
186   gSystem->AddIncludePath("-I$ALICE_ROOT/include");
187   gSystem->Load("libANALYSIS");
188   gSystem->Load("libPWG2flowCommon");
189   //cerr<<"libPWG2flowCommon loaded ..."<<endl;
190   
191   }
192   
193   else if (mode==mLocalSource) {
194  
195     // In root inline compile
196   
197     // Constants  
198     gROOT->LoadMacro("AliFlowCommon/AliFlowCommonConstants.cxx+");
199     gROOT->LoadMacro("AliFlowCommon/AliFlowLYZConstants.cxx+");
200     gROOT->LoadMacro("AliFlowCommon/AliFlowCumuConstants.cxx+");
201     
202     // Flow event
203     gROOT->LoadMacro("AliFlowCommon/AliFlowVector.cxx+"); 
204     gROOT->LoadMacro("AliFlowCommon/AliFlowTrackSimple.cxx+");
205     gROOT->LoadMacro("AliFlowCommon/AliFlowEvent.cxx+");
206     gROOT->LoadMacro("AliFlowCommon/AliFlowEventSimple.cxx+");
207     
208     // Cuts
209     gROOT->LoadMacro("AliFlowCommon/AliFlowTrackSimpleCuts.cxx+");    
210     
211     // Output histosgrams
212     gROOT->LoadMacro("AliFlowCommon/AliFlowCommonHist.cxx+");
213     gROOT->LoadMacro("AliFlowCommon/AliFlowCommonHistResults.cxx+");
214     gROOT->LoadMacro("AliFlowCommon/AliFlowLYZHist1.cxx+");
215     gROOT->LoadMacro("AliFlowCommon/AliFlowLYZHist2.cxx+");
216        
217     cout << "finished loading macros!" << endl;  
218     
219   } // end of else if (mode==mLocalSource) 
220   
221 } // end of void LoadLibrariesMO(const libModes mode)
222