]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/FLOW/macros/runFlowTaskCentralityTrain.C
ffbabfa303451bdf82066afd535b9d9024c96246
[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,10000}; // 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/LHC10e_000130795_p1", Int_t offset=0) //hijing Pb Pb pilot
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("+COSM1-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     gProof->ClearPackage("ANALYSIS.par");
203     gProof->ClearPackage("ANALYSISalice.par");
204     gProof->ClearPackage("CORRFW.par");
205     
206     gProof->ClearPackage("PWG2flowCommon");
207     gProof->ClearPackage("PWG2flowTasks");
208     */
209     // Upload the Packages
210     gProof->UploadPackage("STEERBase.par");
211     gProof->UploadPackage("ESD.par");    
212     gProof->UploadPackage("AOD.par");
213        
214     gProof->UploadPackage("ANALYSIS.par"); 
215     gProof->UploadPackage("ANALYSISalice.par");
216     gProof->UploadPackage("CORRFW.par");
217     gProof->UploadPackage("PWG2flowCommon.par");
218     gProof->UploadPackage("PWG2flowTasks.par");
219
220     // Enable the Packages 
221     // The global package
222     //gProof->EnablePackage("aliroot_v4-19-05-AN",kTRUE);
223     // Or separate
224     
225     gProof->EnablePackage("STEERBase");
226     gProof->EnablePackage("ESD");
227     gProof->EnablePackage("AOD");
228     
229     // Always needed
230     gProof->EnablePackage("ANALYSIS");
231     gProof->EnablePackage("ANALYSISalice");
232     gProof->EnablePackage("CORRFW");
233     gProof->EnablePackage("PWG2flowCommon");
234     gProof->EnablePackage("PWG2flowTasks");
235
236     // Show enables Packages
237     gProof->ShowEnabledPackages();
238   }  
239   
240 } // end of void LoadLibraries(const anaModes mode) 
241
242 //===============================================================================================
243
244 void SetupPar(char* pararchivename) 
245 {
246   //Load par files, create analysis libraries
247   //For testing, if par file already decompressed and modified
248   //classes then do not decompress.
249   
250   TString cdir(Form("%s", gSystem->WorkingDirectory() )) ; 
251   TString parpar(Form("%s.par", pararchivename)) ; 
252   if ( gSystem->AccessPathName(parpar.Data()) ) {
253     gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
254     TString processline(Form(".! make %s", parpar.Data())) ; 
255     gROOT->ProcessLine(processline.Data()) ;
256     gSystem->ChangeDirectory(cdir) ; 
257     processline = Form(".! mv /tmp/%s .", parpar.Data()) ;
258     gROOT->ProcessLine(processline.Data()) ;
259   } 
260   if ( gSystem->AccessPathName(pararchivename) ) {  
261     TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
262     gROOT->ProcessLine(processline.Data());
263   }
264   
265   TString ocwd = gSystem->WorkingDirectory();
266   gSystem->ChangeDirectory(pararchivename);
267   
268   // check for BUILD.sh and execute
269   if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
270     printf("*******************************\n");
271     printf("*** Building PAR archive    ***\n");
272     cout<<pararchivename<<endl;
273     printf("*******************************\n");
274     if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
275       Error("runProcess","Cannot Build the PAR Archive! - Abort!");
276       return -1;
277     }
278   }
279   // check for SETUP.C and execute
280   if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
281     printf("*******************************\n");
282     printf("*** Setup PAR archive       ***\n");
283     cout<<pararchivename<<endl;
284     printf("*******************************\n");
285     gROOT->Macro("PROOF-INF/SETUP.C");
286   }
287   
288   gSystem->ChangeDirectory(ocwd.Data());
289   printf("Current dir: %s\n", ocwd.Data());
290
291 } // end of void SetupPar(char* pararchivename) 
292
293 //===============================================================================================
294
295 // Helper macros for creating chains
296 // from: CreateESDChain.C,v 1.10 jgrosseo Exp
297
298 TChain* CreateESDChain(const char* aDataDir, Int_t aRuns, Int_t offset)
299 {
300   // creates chain of files in a given directory or file containing a list.
301   // In case of directory the structure is expected as:
302   // <aDataDir>/<dir0>/AliESDs.root
303   // <aDataDir>/<dir1>/AliESDs.root
304   // ...
305   
306   if (!aDataDir)
307     return 0;
308   
309   Long_t id, size, flags, modtime;
310   if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
311     {
312       printf("%s not found.\n", aDataDir);
313       return 0;
314     }
315   
316   TChain* chain = new TChain("esdTree");
317   TChain* chaingAlice = 0;
318   
319   if (flags & 2)
320     {
321       TString execDir(gSystem->pwd());
322       TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
323       TList* dirList            = baseDir->GetListOfFiles();
324       Int_t nDirs               = dirList->GetEntries();
325       gSystem->cd(execDir);
326       
327       Int_t count = 0;
328       
329       for (Int_t iDir=0; iDir<nDirs; ++iDir)
330         {
331           TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
332           if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
333             continue;
334           
335           if (offset > 0)
336             {
337               --offset;
338               continue;
339             }
340           
341           if (count++ == aRuns)
342             break;
343           
344           TString presentDirName(aDataDir);
345           presentDirName += "/";
346           presentDirName += presentDir->GetName();        
347           chain->Add(presentDirName + "/AliESDs.root/esdTree");
348           //  cerr<<presentDirName<<endl;
349         }
350       
351     }
352   else
353     {
354       // Open the input stream
355       ifstream in;
356       in.open(aDataDir);
357       
358       Int_t count = 0;
359       
360       // Read the input list of files and add them to the chain
361       TString esdfile;
362       while(in.good()) {
363         in >> esdfile;
364         if (!esdfile.Contains("root")) continue; // protection
365         
366         if (offset > 0)
367           {
368             --offset;
369             continue;
370           }
371         
372         if (count++ == aRuns)
373           break;
374         
375         // add esd file
376         chain->Add(esdfile);
377       }
378       
379       in.close();
380     }
381   
382   return chain;
383
384 } // end of TChain* CreateESDChain(const char* aDataDir, Int_t aRuns, Int_t offset)
385
386 //===============================================================================================
387
388 TChain* CreateAODChain(const char* aDataDir, Int_t aRuns, Int_t offset)
389 {
390   // creates chain of files in a given directory or file containing a list.
391   // In case of directory the structure is expected as:
392   // <aDataDir>/<dir0>/AliAOD.root
393   // <aDataDir>/<dir1>/AliAOD.root
394   // ...
395   
396   if (!aDataDir)
397     return 0;
398   
399   Long_t id, size, flags, modtime;
400   if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
401     {
402       printf("%s not found.\n", aDataDir);
403       return 0;
404     }
405   
406   TChain* chain = new TChain("aodTree");
407   TChain* chaingAlice = 0;
408   
409   if (flags & 2)
410     {
411       TString execDir(gSystem->pwd());
412       TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
413       TList* dirList            = baseDir->GetListOfFiles();
414       Int_t nDirs               = dirList->GetEntries();
415       gSystem->cd(execDir);
416       
417       Int_t count = 0;
418       
419       for (Int_t iDir=0; iDir<nDirs; ++iDir)
420         {
421           TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
422           if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
423             continue;
424           
425           if (offset > 0)
426             {
427               --offset;
428               continue;
429             }
430           
431           if (count++ == aRuns)
432             break;
433           
434           TString presentDirName(aDataDir);
435           presentDirName += "/";
436           presentDirName += presentDir->GetName();        
437           chain->Add(presentDirName + "/AliAOD.root/aodTree");
438           // cerr<<presentDirName<<endl;
439         }
440       
441     }
442   else
443     {
444       // Open the input stream
445       ifstream in;
446       in.open(aDataDir);
447       
448       Int_t count = 0;
449       
450       // Read the input list of files and add them to the chain
451       TString aodfile;
452       while(in.good()) {
453         in >> aodfile;
454         if (!aodfile.Contains("root")) continue; // protection
455         
456         if (offset > 0)
457           {
458             --offset;
459             continue;
460           }
461         
462         if (count++ == aRuns)
463           break;
464         
465         // add aod file
466         chain->Add(aodfile);
467       }
468       
469       in.close();
470     }
471   
472   return chain;
473
474 } // end of TChain* CreateAODChain(const char* aDataDir, Int_t aRuns, Int_t offset)
475