b008aa5cab55a06dc188a2edc906cf91f0929b3b
[u/mrichter/AliRoot.git] / PWGCF / FLOW / macros / runFlowTaskCentralityPIDTrain.C
1 enum anaModes {mLocal,mPROOF,mGrid};
2 Int_t binfirst = 0;  //where do we start numbering bins
3 Int_t binlast = 8;  //where do we stop numbering bins
4 const Int_t numberOfCentralityBins = 9;
5 Float_t centralityArray[numberOfCentralityBins+1] = {0.,5.,10.,20.,30.,40.,50.,60.,70.,80.}; // in centrality percentile
6 //Int_t centralityArray[numberOfCentralityBins+1] = {41,80,146,245,384,576,835,1203,1471,10000}; // in terms of TPC only reference multiplicity
7
8 TString commonOutputFileName = "outputCentrality"; // e.g.: result for centrality bin 0 will be in the file "outputCentrality0.root", etc
9
10 void runFlowTaskCentralityPIDTrain( Int_t mode = mLocal,
11                                     Bool_t useFlowParFiles = kFALSE,
12                                     Bool_t DATA = kTRUE,
13                                     const Char_t* dataDir="fileList",
14                                     Int_t nEvents = 1e4,
15                                     Int_t offset=0 )
16 {
17   // Time:
18   TStopwatch timer;
19   timer.Start();
20
21   // Load needed libraries:
22   LoadLibraries(mode,useFlowParFiles);
23
24   // Create analysis manager:
25   AliAnalysisManager *mgr = new AliAnalysisManager("FlowAnalysisManager");
26
27   // Chains:
28   if(mode == mLocal)
29   {
30     gROOT->LoadMacro("$ALICE_ROOT/PWGUD/macros/CreateESDChain.C");
31     TChain* chain = CreateESDChain(dataDir, nEvents, offset);
32     //TChain* chain = CreateAODChain(dataDir, nEvents, offset);
33   }
34
35   // Connect plug-in to the analysis manager:
36   if(mode == mGrid)
37   {
38     gROOT->LoadMacro("CreateAlienHandler.C");
39     AliAnalysisGrid *alienHandler = CreateAlienHandler(useFlowParFiles);
40     if(!alienHandler) return;
41     mgr->SetGridHandler(alienHandler);
42   }
43
44   // Event handlers:
45   AliVEventHandler* esdH = new AliESDInputHandler;
46   mgr->SetInputEventHandler(esdH);
47   if (!DATA)
48   {
49     AliMCEventHandler *mc = new AliMCEventHandler();
50     mgr->SetMCtruthEventHandler(mc);
51   }
52
53   // Task to check the offline trigger:
54   gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
55   AddTaskPhysicsSelection(!DATA);
56
57   //Add the centrality determination task
58   gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
59   AliCentralitySelectionTask* centSelTask = AddTaskCentrality();
60   if (!DATA) centSelTask->SetMCInput();
61
62   //add the PID response task
63   gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
64   AliAnalysisTaskPIDResponse* pidresponsetask = AddTaskPIDResponse(!DATA);
65
66   //Add the TOF tender
67   //gROOT->LoadMacro("$ALICE_ROOT/PWGCF/FLOW/macros/AddTaskTenderTOF.C");
68   //AddTaskTenderTOF();
69
70   // Setup analysis per centrality bin:
71   gROOT->LoadMacro("AddTaskFlowCentralityPID.C");
72   for (Int_t i=binfirst; i<binlast+1; i++)
73   {
74     Float_t lowCentralityBinEdge = centralityArray[i];
75     Float_t highCentralityBinEdge = centralityArray[i+1];
76     AddTaskFlowCentralityPID( lowCentralityBinEdge,
77                               highCentralityBinEdge,
78                               commonOutputFileName,
79                               AliPID::kPion,
80                               AliFlowTrackCuts::kTOFbeta,
81                               -1,2,kTRUE );
82     AddTaskFlowCentralityPID( lowCentralityBinEdge,
83                               highCentralityBinEdge,
84                               commonOutputFileName,
85                               AliPID::kPion,
86                               AliFlowTrackCuts::kTOFbeta,
87                               1,2,kTRUE );
88     AddTaskFlowCentralityPID( lowCentralityBinEdge,
89                               highCentralityBinEdge,
90                               commonOutputFileName,
91                               AliPID::kKaon,
92                               AliFlowTrackCuts::kTOFbeta,
93                               -1,2,kTRUE );
94     AddTaskFlowCentralityPID( lowCentralityBinEdge,
95                               highCentralityBinEdge,
96                               commonOutputFileName,
97                               AliPID::kKaon,
98                               AliFlowTrackCuts::kTOFbeta,
99                               1,2,kTRUE );
100     AddTaskFlowCentralityPID( lowCentralityBinEdge,
101                               highCentralityBinEdge,
102                               commonOutputFileName,
103                               AliPID::kProton,
104                               AliFlowTrackCuts::kTOFbeta,
105                               -1,2,kTRUE );
106     AddTaskFlowCentralityPID( lowCentralityBinEdge,
107                               highCentralityBinEdge,
108                               commonOutputFileName,
109                               AliPID::kProton,
110                               AliFlowTrackCuts::kTOFbeta,
111                               1,2,kTRUE );
112     AddTaskFlowCentralityPID( lowCentralityBinEdge,
113                               highCentralityBinEdge,
114                               commonOutputFileName,
115                               AliPID::kPion,
116                               AliFlowTrackCuts::kTOFbeta,
117                               -1,3 );
118     AddTaskFlowCentralityPID( lowCentralityBinEdge,
119                               highCentralityBinEdge,
120                               commonOutputFileName,
121                               AliPID::kPion,
122                               AliFlowTrackCuts::kTOFbeta,
123                               1,3 );
124     AddTaskFlowCentralityPID( lowCentralityBinEdge,
125                               highCentralityBinEdge,
126                               commonOutputFileName,
127                               AliPID::kKaon,
128                               AliFlowTrackCuts::kTOFbeta,
129                               -1,3 );
130     AddTaskFlowCentralityPID( lowCentralityBinEdge,
131                               highCentralityBinEdge,
132                               commonOutputFileName,
133                               AliPID::kKaon,
134                               AliFlowTrackCuts::kTOFbeta,
135                               1,3 );
136     AddTaskFlowCentralityPID( lowCentralityBinEdge,
137                               highCentralityBinEdge,
138                               commonOutputFileName,
139                               AliPID::kProton,
140                               AliFlowTrackCuts::kTOFbeta,
141                               -1,3 );
142     AddTaskFlowCentralityPID( lowCentralityBinEdge,
143                               highCentralityBinEdge,
144                               commonOutputFileName,
145                               AliPID::kProton,
146                               AliFlowTrackCuts::kTOFbeta,
147                               1,3 );
148   } // end of for (Int_t i=0; i<numberOfCentralityBins; i++)
149
150   // Enable debug printouts:
151   mgr->SetDebugLevel(2);
152   // Run the analysis:
153   if(!mgr->InitAnalysis()) return;
154   mgr->PrintStatus();
155   if(mode == mLocal)
156   {
157     mgr->StartAnalysis("local",chain);
158   }
159   else if(mode == mPROOF)
160   {
161     mgr->StartAnalysis("proof",dataDir,nEvents,offset);
162   }
163   else if(mode == mGrid)
164   {
165     mgr->StartAnalysis("grid");
166   }
167
168   // Print real and CPU time used for analysis:
169   timer.Stop();
170   timer.Print();
171
172 } // end of void runFlowTaskCentralityPIDTrain(...)
173
174 //===============================================================================================
175 /*
176 void CrossCheckUserSettings(Bool_t bData)
177 {
178  // Check in this method if the user settings make sense.
179  if(LYZ1SUM && LYZ2SUM) {cout<<" WARNING: You cannot run LYZ1 and LYZ2 at the same time! LYZ2 needs the output from LYZ1 !!!!"<<endl; exit(0); }
180  if(LYZ1PROD && LYZ2PROD) {cout<<" WARNING: You cannot run LYZ1 and LYZ2 at the same time! LYZ2 needs the output from LYZ1 !!!!"<<endl; exit(0); }
181  if(LYZ2SUM && LYZEP) {cout<<" WARNING: You cannot run LYZ2 and LYZEP at the same time! LYZEP needs the output from LYZ2 !!!!"<<endl; exit(0); }
182  if(LYZ1SUM && LYZEP) {cout<<" WARNING: You cannot run LYZ1 and LYZEP at the same time! LYZEP needs the output from LYZ2 !!!!"<<endl; exit(0); }
183 } // end of void CrossCheckUserSettings()
184 */
185 //===============================================================================================
186
187 void LoadLibraries(const anaModes mode, Bool_t useFlowParFiles )
188 {
189   //--------------------------------------
190   // Load the needed libraries most of them already loaded by aliroot
191   //--------------------------------------
192
193   gSystem->Load("libCore");
194   gSystem->Load("libTree");
195   gSystem->Load("libGeom");
196   gSystem->Load("libVMC");
197   gSystem->Load("libXMLIO");
198   gSystem->Load("libPhysics");
199   gSystem->Load("libXMLParser");
200   gSystem->Load("libProof");
201   gSystem->Load("libMinuit");
202
203   if (mode==mLocal || mode==mGrid)
204   {
205     gSystem->Load("libSTEERBase");
206     gSystem->Load("libCDB");
207     gSystem->Load("libRAWDatabase");
208     gSystem->Load("libRAWDatarec");
209     gSystem->Load("libESD");
210     gSystem->Load("libAOD");
211     gSystem->Load("libSTEER");
212     gSystem->Load("libANALYSIS");
213     gSystem->Load("libANALYSISalice");
214     gSystem->Load("libTPCbase");
215     gSystem->Load("libTOFbase");
216     gSystem->Load("libTOFrec");
217     gSystem->Load("libTRDbase");
218     gSystem->Load("libVZERObase");
219     gSystem->Load("libVZEROrec");
220     gSystem->Load("libT0base");
221     gSystem->Load("libT0rec");
222     gSystem->Load("libTENDER");
223     gSystem->Load("libTENDERSupplies");
224
225     if (useFlowParFiles)
226     {
227       AliAnalysisAlien::SetupPar("PWGflowBase");
228       AliAnalysisAlien::SetupPar("PWGflowTasks");
229     }
230     else
231     {
232       gSystem->Load("libPWGflowBase");
233       gSystem->Load("libPWGflowTasks");
234     }
235   }
236   else if (mode==mPROOF)
237   {
238     TList* list = new TList();
239     list->Add(new TNamed("ALIROOT_MODE", "ALIROOT"));
240     if (useFlowParFiles)
241       list->Add(new TNamed("ALIROOT_EXTRA_LIBS", "ANALYSIS:ANALYSISalice:TENDER:TENDERSupplies"));
242     else
243       list->Add(new TNamed("ALIROOT_EXTRA_LIBS", "ANALYSIS:ANALYSISalice:TENDER:TENDERSupplies:PWGflowBase:PWGflowTasks"));
244
245     //list->Add(new TNamed("ALIROOT_EXTRA_INCLUDES","PWG/FLOW/Base:PWG/FLOW/Tasks"));
246
247     // Connect to proof
248     printf("*** Connect to PROOF ***\n");
249     gEnv->SetValue("XSec.GSI.DelegProxy","2");
250     //TProof* proof = TProof::Open("alice-caf.cern.ch");
251     TProof* proof = TProof::Open("skaf.saske.sk");
252
253     // list the data available
254     //gProof->ShowDataSets("/*/*");
255     //gProof->ShowDataSets("/alice/sim/"); //for MC Data
256     //gProof->ShowDataSets("/alice/data/"); //for REAL Data
257
258     proof->ClearPackages();
259     proof->EnablePackage("VO_ALICE@AliRoot::v4-21-14-AN",list);
260
261     if (useFlowParFiles)
262     {
263       gProof->UploadPackage("PWGflowBase.par");
264       gProof->UploadPackage("PWGflowTasks.par");
265     }
266
267     // Show enables Packages
268     gProof->ShowEnabledPackages();
269   }
270 } // end of void LoadLibraries(const anaModes mode)
271
272 //===============================================================================================
273
274 TChain* CreateAODChain(const char* aDataDir, Int_t aRuns, Int_t offset)
275 {
276   // creates chain of files in a given directory or file containing a list.
277   // In case of directory the structure is expected as:
278   // <aDataDir>/<dir0>/AliAOD.root
279   // <aDataDir>/<dir1>/AliAOD.root
280   // ...
281
282   if (!aDataDir)
283     return 0;
284
285   Long_t id, size, flags, modtime;
286   if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
287   {
288     printf("%s not found.\n", aDataDir);
289     return 0;
290   }
291
292   TChain* chain = new TChain("aodTree");
293   TChain* chaingAlice = 0;
294
295   if (flags & 2)
296   {
297     TString execDir(gSystem->pwd());
298     TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
299     TList* dirList            = baseDir->GetListOfFiles();
300     Int_t nDirs               = dirList->GetEntries();
301     gSystem->cd(execDir);
302
303     Int_t count = 0;
304
305     for (Int_t iDir=0; iDir<nDirs; ++iDir)
306     {
307       TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
308       if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
309         continue;
310
311       if (offset > 0)
312       {
313         --offset;
314         continue;
315       }
316
317       if (count++ == aRuns)
318         break;
319
320       TString presentDirName(aDataDir);
321       presentDirName += "/";
322       presentDirName += presentDir->GetName();
323       chain->Add(presentDirName + "/AliAOD.root/aodTree");
324       // cerr<<presentDirName<<endl;
325     }
326
327   }
328   else
329   {
330     // Open the input stream
331     ifstream in;
332     in.open(aDataDir);
333
334     Int_t count = 0;
335
336     // Read the input list of files and add them to the chain
337     TString aodfile;
338     while(in.good())
339     {
340       in >> aodfile;
341       if (!aodfile.Contains("root")) continue; // protection
342
343       if (offset > 0)
344       {
345         --offset;
346         continue;
347       }
348
349       if (count++ == aRuns)
350         break;
351
352       // add aod file
353       chain->Add(aodfile);
354     }
355
356     in.close();
357   }
358
359   return chain;
360
361 } // end of TChain* CreateAODChain(const char* aDataDir, Int_t aRuns, Int_t offset)
362