]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/FLOW/macros/runFlowTaskCentralityTrain.C
7c206a83e276e7dd652d93476ba87ed390333666
[u/mrichter/AliRoot.git] / PWG2 / FLOW / macros / runFlowTaskCentralityTrain.C
1 enum anaModes {mLocal,mLocalPAR,mPROOF,mGrid,mGridPAR};
2 //mLocal: Analyze locally files in your computer using aliroot
3 //mLocalPAR: Analyze locally files in your computer using root + PAR files
4 //mPROOF: Analyze CAF files with PROOF
5 //mGrid: Analyze files on Grid via AliEn plug-in and using precompiled FLOW libraries
6 //       (Remark: When using this mode set also Bool_t bUseParFiles = kFALSE; in CreateAlienHandler.C)
7 //mGridPAR: Analyze files on Grid via AliEn plug-in and using par files for FLOW package
8 //          (Remark: when using this mode set also Bool_t bUseParFiles = kTRUE; in CreateAlienHandler.C)
9  
10 // CENTRALITY DEFINITION
11 const Int_t numberOfCentralityBins = 1;
12 Int_t centralityArray[numberOfCentralityBins+1] = {0,1000000}; // in terms of reference multiplicity
13 TString commonOutputFileName = "outputCentrality"; // e.g.: result for centrality bin 0 will be in the file "outputCentrality0.root", etc
14
15 //void runFlowTaskCentralityTrain(Int_t mode=mLocal, Int_t nRuns = 10, 
16 //Bool_t DATA = kFALSE, const Char_t* dataDir="/Users/snelling/alice_data/Therminator_midcentral", Int_t offset = 0)
17
18 void runFlowTaskCentralityTrain(Int_t mode = mPROOF, Int_t nRuns = 50000000, 
19                  Bool_t DATA = kTRUE, const Char_t* dataDir="/alice/data/LHC10h_000137045_p1", Int_t offset=0) 
20
21 //void runFlowTaskCentralityTrain(Int_t mode = mGrid, Bool_t DATA = kTRUE)
22 {
23   // Time:
24   TStopwatch timer;
25   timer.Start();
26   // Cross-check user settings before starting:
27   //  CrossCheckUserSettings(DATA);
28   // Load needed libraries:
29   LoadLibraries(mode);
30   // Create and configure the AliEn plug-in:
31   if(mode == mGrid || mode == mGridPAR) 
32     {    
33       gROOT->LoadMacro("CreateAlienHandler.C");
34       AliAnalysisGrid *alienHandler = CreateAlienHandler();  
35       if(!alienHandler) return;
36     }
37   // Chains: 
38   if(mode == mLocal || mode == mLocalPAR) {
39     TChain* chain = CreateESDChain(dataDir, nRuns, offset);
40     //TChain* chain = CreateAODChain(dataDir, nRuns, offset);
41   }
42   
43   // Create analysis manager:
44   AliAnalysisManager *mgr = new AliAnalysisManager("FlowAnalysisManager"); 
45   // Connect plug-in to the analysis manager:
46   if(mode == mGrid || mode == mGridPAR) 
47     { 
48       mgr->SetGridHandler(alienHandler);
49     }
50   
51   // Event handlers:
52   AliVEventHandler* esdH = new AliESDInputHandler;
53   mgr->SetInputEventHandler(esdH);
54   AliMCEventHandler *mc = new AliMCEventHandler();
55   mgr->SetMCtruthEventHandler(mc); 
56   
57   // Load the analysis task:
58   gROOT->LoadMacro("AddTaskFlowCentrality.C");
59   
60   // Setup analysis per centrality bin:
61   for (Int_t i=0; i<numberOfCentralityBins; i++)
62   {
63     Int_t lowCentralityBinEdge = centralityArray[i];
64     Int_t highCentralityBinEdge = centralityArray[i+1];
65     TString filename(commonOutputFileName.Data());
66     filename += i;
67     //TDirectory* dir = new TDirectory(filename.Data(),"");
68     filename += ".root";
69     Printf("\nWagon for centrality bin %i:",i);
70     AddTaskFlowCentrality( lowCentralityBinEdge,
71                            highCentralityBinEdge,
72                            filename );
73    
74   } // end of for (Int_t i=0; i<numberOfCentralityBins; i++)
75
76   // Task to check the offline trigger:
77   if(mode == mLocal || mode == mGrid || mode == mGridPAR) {
78     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C"); 
79   } else if(mode == mPROOF || mode == mLocalPAR) {
80     gROOT->LoadMacro("AddTaskPhysicsSelection.C"); 
81   }
82
83   AliPhysicsSelectionTask* physicsSelTask = AddTaskPhysicsSelection();
84   physicsSelTask->GetPhysicsSelection()->AddCollisionTriggerClass("+C0SM1-B-NOPF-ALL");
85   physicsSelTask->GetPhysicsSelection()->AddCollisionTriggerClass("+C0VBA-B-NOPF-ALL");
86   physicsSelTask->GetPhysicsSelection()->AddCollisionTriggerClass("+C0VBC-B-NOPF-ALL");
87
88   if(!DATA){physicsSelTask->GetPhysicsSelection()->SetAnalyzeMC();}
89   // Enable debug printouts:
90   mgr->SetDebugLevel(2);
91   // Run the analysis:
92   if(!mgr->InitAnalysis()) return;  
93   mgr->PrintStatus();
94   if(mode == mLocal || mode == mLocalPAR) {
95     mgr->StartAnalysis("local",chain);
96   } else if(mode == mPROOF) {
97     mgr->StartAnalysis("proof",dataDir,nRuns,offset);
98   } else if(mode == mGrid || mode == mGridPAR) { 
99     mgr->StartAnalysis("grid");
100   }
101
102   // Print real and CPU time used for analysis:
103   timer.Stop();
104   timer.Print();  
105   
106 } // end of void runFlowTaskCentralityTrain(...)
107
108 //===============================================================================================
109 /*
110 void CrossCheckUserSettings(Bool_t bData) 
111 {
112  // Check in this method if the user settings make sense. 
113  if(LYZ1SUM && LYZ2SUM) {cout<<" WARNING: You cannot run LYZ1 and LYZ2 at the same time! LYZ2 needs the output from LYZ1 !!!!"<<endl; exit(0); }
114  if(LYZ1PROD && LYZ2PROD) {cout<<" WARNING: You cannot run LYZ1 and LYZ2 at the same time! LYZ2 needs the output from LYZ1 !!!!"<<endl; exit(0); }
115  if(LYZ2SUM && LYZEP) {cout<<" WARNING: You cannot run LYZ2 and LYZEP at the same time! LYZEP needs the output from LYZ2 !!!!"<<endl; exit(0); }
116  if(LYZ1SUM && LYZEP) {cout<<" WARNING: You cannot run LYZ1 and LYZEP at the same time! LYZEP needs the output from LYZ2 !!!!"<<endl; exit(0); }
117 } // end of void CrossCheckUserSettings()
118 */
119 //===============================================================================================
120
121 void LoadLibraries(const anaModes mode) 
122 {
123   //--------------------------------------
124   // Load the needed libraries most of them already loaded by aliroot
125   //--------------------------------------
126   //gSystem->Load("libTree");
127   gSystem->Load("libGeom");
128   gSystem->Load("libVMC");
129   gSystem->Load("libXMLIO");
130   gSystem->Load("libPhysics");
131   
132   //----------------------------------------------------------
133   // >>>>>>>>>>> Local mode <<<<<<<<<<<<<< 
134   //----------------------------------------------------------
135   if (mode==mLocal || mode==mGrid || mode == mGridPAR) {
136     //--------------------------------------------------------
137     // If you want to use already compiled libraries 
138     // in the aliroot distribution
139     //--------------------------------------------------------
140     gSystem->Load("libSTEERBase");
141     gSystem->Load("libESD");
142     gSystem->Load("libAOD");
143     gSystem->Load("libANALYSIS");
144     gSystem->Load("libANALYSISalice");
145     gSystem->Load("libCORRFW");
146     gSystem->Load("libPWG2forward");
147     if(mode==mLocal || mode==mGrid) 
148     {
149      gSystem->Load("libPWG2flowCommon");
150      cerr<<"libPWG2flowCommon loaded..."<<endl;
151      gSystem->Load("libPWG2flowTasks");
152      cerr<<"libPWG2flowTasks loaded..."<<endl;
153     }
154     if(mode==mGridPAR) 
155     {
156      SetupPar("PWG2flowCommon");
157      cerr<<"PWG2flowCommon.par loaded..."<<endl;
158      SetupPar("PWG2flowTasks");
159      cerr<<"PWG2flowTasks.par loaded..."<<endl;
160     }
161   }
162   
163   else if (mode == mLocalPAR) {
164     //--------------------------------------------------------
165     //If you want to use root and par files from aliroot
166     //--------------------------------------------------------  
167     SetupPar("STEERBase");
168     SetupPar("ESD");
169     SetupPar("AOD");
170     SetupPar("ANALYSIS");
171     SetupPar("ANALYSISalice");
172     SetupPar("CORRFW");
173     SetupPar("PWG2flowCommon");
174     cerr<<"PWG2flowCommon.par loaded..."<<endl;
175     SetupPar("PWG2flowTasks");
176     cerr<<"PWG2flowTasks.par loaded..."<<endl;
177   }
178   
179   //---------------------------------------------------------
180   // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
181   //---------------------------------------------------------
182   else if (mode==mPROOF) {
183     //  set to debug root versus if needed
184     //TProof::Mgr("alicecaf")->SetROOTVersion("v5-24-00a_dbg");
185     //TProof::Mgr("alicecaf")->SetROOTVersion("v5-24-00a");
186     //TProof::Reset("proof://snelling@alicecaf.cern.ch");     
187     // Connect to proof
188     printf("*** Connect to PROOF ***\n");
189     gEnv->SetValue("XSec.GSI.DelegProxy","2");
190     TProof::Open("alice-caf.cern.ch");
191     //TProof::Open("skaf.saske.sk");
192      // list the data available
193     //gProof->ShowDataSets("/*/*"); 
194     //gProof->ShowDataSets("/alice/sim/"); //for MC Data
195     //gProof->ShowDataSets("/alice/data/"); //for REAL Data 
196  
197     // Clear the Packages
198     /*    
199     gProof->ClearPackage("STEERBase.par");
200     gProof->ClearPackage("ESD.par");
201     gProof->ClearPackage("AOD.par");
202     */
203     gProof->ClearPackage("ANALYSIS.par");
204     gProof->ClearPackage("ANALYSISalice.par");
205     gProof->ClearPackage("CORRFW.par");
206     
207     gProof->ClearPackage("PWG2flowCommon");
208     gProof->ClearPackage("PWG2flowTasks");
209     
210     // Upload the Packages
211     //gProof->UploadPackage("STEERBase.par");
212     //gProof->UploadPackage("ESD.par");    
213     //gProof->UploadPackage("AOD.par");
214        
215     gProof->UploadPackage("ANALYSIS.par"); 
216     gProof->UploadPackage("ANALYSISalice.par");
217     gProof->UploadPackage("CORRFW.par");
218     gProof->UploadPackage("PWG2flowCommon.par");
219     gProof->UploadPackage("PWG2flowTasks.par");
220
221     // Enable the Packages 
222     // The global package
223     gProof->EnablePackage("VO_ALICE@AliRoot::v4-21-04-AN");
224     //gProof->EnablePackage("aliroot_v4-19-05-AN",kTRUE);
225     // Or separate
226     
227     //gProof->EnablePackage("STEERBase");
228     //gProof->EnablePackage("ESD");
229     //gProof->EnablePackage("AOD");
230     
231     // Always needed
232     gProof->EnablePackage("ANALYSIS");
233     gProof->EnablePackage("ANALYSISalice");
234     gProof->EnablePackage("CORRFW");
235     gProof->EnablePackage("PWG2flowCommon");
236     gProof->EnablePackage("PWG2flowTasks");
237
238     // Show enables Packages
239     gProof->ShowEnabledPackages();
240   }  
241   
242 } // end of void LoadLibraries(const anaModes mode) 
243
244 //===============================================================================================
245
246 void SetupPar(char* pararchivename) 
247 {
248   //Load par files, create analysis libraries
249   //For testing, if par file already decompressed and modified
250   //classes then do not decompress.
251   
252   TString cdir(Form("%s", gSystem->WorkingDirectory() )) ; 
253   TString parpar(Form("%s.par", pararchivename)) ; 
254   if ( gSystem->AccessPathName(parpar.Data()) ) {
255     gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
256     TString processline(Form(".! make %s", parpar.Data())) ; 
257     gROOT->ProcessLine(processline.Data()) ;
258     gSystem->ChangeDirectory(cdir) ; 
259     processline = Form(".! mv /tmp/%s .", parpar.Data()) ;
260     gROOT->ProcessLine(processline.Data()) ;
261   } 
262   if ( gSystem->AccessPathName(pararchivename) ) {  
263     TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
264     gROOT->ProcessLine(processline.Data());
265   }
266   
267   TString ocwd = gSystem->WorkingDirectory();
268   gSystem->ChangeDirectory(pararchivename);
269   
270   // check for BUILD.sh and execute
271   if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
272     printf("*******************************\n");
273     printf("*** Building PAR archive    ***\n");
274     cout<<pararchivename<<endl;
275     printf("*******************************\n");
276     if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
277       Error("runProcess","Cannot Build the PAR Archive! - Abort!");
278       return -1;
279     }
280   }
281   // check for SETUP.C and execute
282   if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
283     printf("*******************************\n");
284     printf("*** Setup PAR archive       ***\n");
285     cout<<pararchivename<<endl;
286     printf("*******************************\n");
287     gROOT->Macro("PROOF-INF/SETUP.C");
288   }
289   
290   gSystem->ChangeDirectory(ocwd.Data());
291   printf("Current dir: %s\n", ocwd.Data());
292
293 } // end of void SetupPar(char* pararchivename) 
294
295 //===============================================================================================
296
297 // Helper macros for creating chains
298 // from: CreateESDChain.C,v 1.10 jgrosseo Exp
299
300 TChain* CreateESDChain(const char* aDataDir, Int_t aRuns, Int_t offset)
301 {
302   // creates chain of files in a given directory or file containing a list.
303   // In case of directory the structure is expected as:
304   // <aDataDir>/<dir0>/AliESDs.root
305   // <aDataDir>/<dir1>/AliESDs.root
306   // ...
307   
308   if (!aDataDir)
309     return 0;
310   
311   Long_t id, size, flags, modtime;
312   if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
313     {
314       printf("%s not found.\n", aDataDir);
315       return 0;
316     }
317   
318   TChain* chain = new TChain("esdTree");
319   TChain* chaingAlice = 0;
320   
321   if (flags & 2)
322     {
323       TString execDir(gSystem->pwd());
324       TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
325       TList* dirList            = baseDir->GetListOfFiles();
326       Int_t nDirs               = dirList->GetEntries();
327       gSystem->cd(execDir);
328       
329       Int_t count = 0;
330       
331       for (Int_t iDir=0; iDir<nDirs; ++iDir)
332         {
333           TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
334           if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
335             continue;
336           
337           if (offset > 0)
338             {
339               --offset;
340               continue;
341             }
342           
343           if (count++ == aRuns)
344             break;
345           
346           TString presentDirName(aDataDir);
347           presentDirName += "/";
348           presentDirName += presentDir->GetName();        
349           chain->Add(presentDirName + "/AliESDs.root/esdTree");
350           //  cerr<<presentDirName<<endl;
351         }
352       
353     }
354   else
355     {
356       // Open the input stream
357       ifstream in;
358       in.open(aDataDir);
359       
360       Int_t count = 0;
361       
362       // Read the input list of files and add them to the chain
363       TString esdfile;
364       while(in.good()) {
365         in >> esdfile;
366         if (!esdfile.Contains("root")) continue; // protection
367         
368         if (offset > 0)
369           {
370             --offset;
371             continue;
372           }
373         
374         if (count++ == aRuns)
375           break;
376         
377         // add esd file
378         chain->Add(esdfile);
379       }
380       
381       in.close();
382     }
383   
384   return chain;
385
386 } // end of TChain* CreateESDChain(const char* aDataDir, Int_t aRuns, Int_t offset)
387
388 //===============================================================================================
389
390 TChain* CreateAODChain(const char* aDataDir, Int_t aRuns, Int_t offset)
391 {
392   // creates chain of files in a given directory or file containing a list.
393   // In case of directory the structure is expected as:
394   // <aDataDir>/<dir0>/AliAOD.root
395   // <aDataDir>/<dir1>/AliAOD.root
396   // ...
397   
398   if (!aDataDir)
399     return 0;
400   
401   Long_t id, size, flags, modtime;
402   if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
403     {
404       printf("%s not found.\n", aDataDir);
405       return 0;
406     }
407   
408   TChain* chain = new TChain("aodTree");
409   TChain* chaingAlice = 0;
410   
411   if (flags & 2)
412     {
413       TString execDir(gSystem->pwd());
414       TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
415       TList* dirList            = baseDir->GetListOfFiles();
416       Int_t nDirs               = dirList->GetEntries();
417       gSystem->cd(execDir);
418       
419       Int_t count = 0;
420       
421       for (Int_t iDir=0; iDir<nDirs; ++iDir)
422         {
423           TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
424           if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
425             continue;
426           
427           if (offset > 0)
428             {
429               --offset;
430               continue;
431             }
432           
433           if (count++ == aRuns)
434             break;
435           
436           TString presentDirName(aDataDir);
437           presentDirName += "/";
438           presentDirName += presentDir->GetName();        
439           chain->Add(presentDirName + "/AliAOD.root/aodTree");
440           // cerr<<presentDirName<<endl;
441         }
442       
443     }
444   else
445     {
446       // Open the input stream
447       ifstream in;
448       in.open(aDataDir);
449       
450       Int_t count = 0;
451       
452       // Read the input list of files and add them to the chain
453       TString aodfile;
454       while(in.good()) {
455         in >> aodfile;
456         if (!aodfile.Contains("root")) continue; // protection
457         
458         if (offset > 0)
459           {
460             --offset;
461             continue;
462           }
463         
464         if (count++ == aRuns)
465           break;
466         
467         // add aod file
468         chain->Add(aodfile);
469       }
470       
471       in.close();
472     }
473   
474   return chain;
475
476 } // end of TChain* CreateAODChain(const char* aDataDir, Int_t aRuns, Int_t offset)
477