Split: fix refs to AddTaskPhysicsSelection.C
[u/mrichter/AliRoot.git] / PWGLF / SPECTRA / ChargedHadrons / multPbPb / run.C
1 // TODO:
2 // 1. Check cuts for 2010 (Jochen?)
3 // 2. Run with many centrality bins at once
4 #include <string.h>
5
6 enum { kMyRunModeLocal = 0, kMyRunModeCAF, kMyRunModeGRID, kMyRunModeProofLite};
7
8 TList * listToLoad = new TList();
9
10 TChain * GetAnalysisChain(const char * incollection);
11
12 void run(Char_t* data, Long64_t nev = -1, Long64_t offset = 0, Bool_t debug = kFALSE, Int_t runMode = 0, Bool_t isMC = 0, 
13          Int_t centrBin = 0, const char * centrEstimator = "VOM", Int_t useOtherCentralityCut = 0, Int_t trackMin=0, Int_t trackMax=10000, 
14          const char* option = "",TString customSuffix = "", Int_t workers = -1, Bool_t useSingleBin=kTRUE, const char * runList = 0)
15 {
16   // runMode:
17   //
18   // 0 local 
19   // 1 proof
20
21   if (nev < 0)
22     nev = 1234567890;
23
24   InitAndLoadLibs(runMode,workers,debug);
25
26   // Create the analysis manager
27   mgr = new AliAnalysisManager;
28
29   // Add ESD handler
30   AliESDInputHandler* esdH = new AliESDInputHandler;
31   // Do I need any of this? 
32   esdH->SetInactiveBranches("AliESDACORDE FMD ALIESDTZERO ALIESDZDC AliRawDataErrorLogs CaloClusters Cascades EMCALCells EMCALTrigger ESDfriend Kinks AliESDTZERO ALIESDACORDE MuonTracks TrdTracks");
33   mgr->SetInputEventHandler(esdH);
34
35   if(isMC) {
36     AliMCEventHandler* handler = new AliMCEventHandler;
37     handler->SetPreReadMode(AliMCEventHandler::kLmPreRead);
38     mgr->SetMCtruthEventHandler(handler);
39   }
40
41
42
43   // Parse option strings
44   TString optionStr(option);
45   
46   // remove SAVE option if set
47   // This  is copied from a macro by Jan. The reason I kept it is that I may want to pass textual options to the new task at some point
48   Bool_t doSave = kFALSE;
49   TString optionStr(option);
50   if (optionStr.Contains("SAVE"))
51     {
52       optionStr = optionStr(0,optionStr.Index("SAVE")) + optionStr(optionStr.Index("SAVE")+4, optionStr.Length());
53       doSave = kTRUE;
54     }
55
56   AliESDtrackCuts * cuts = AliESDtrackCuts::GetStandardITSTPCTrackCuts2010(kFALSE);  
57   TString pathsuffix = "";
58
59   if(!useSingleBin) pathsuffix += "_AllCentr";
60
61   if (optionStr.Contains("DCA")) {
62     delete cuts;
63     //    cuts = AliESDtrackCuts::GetStandardITSPureSATrackCuts2009();
64     cout << ">>>> USING DCA cut" << endl;
65     cuts = AliESDtrackCuts::GetStandardITSTPCTrackCuts2010();  
66     cuts->SetMaxChi2TPCConstrainedGlobal(); // remove golden cut
67     pathsuffix+="_DCAcut";
68   }
69
70   if (optionStr.Contains("ITSsa")) {
71     delete cuts;
72     cuts = AliESDtrackCuts::GetStandardITSPureSATrackCuts2009();
73     cout << ">>>> USING ITS sa tracks" << endl;
74     pathsuffix+="_ITSsa";
75   }
76
77   if (optionStr.Contains("TPC")) {
78     delete cuts;
79     cuts = AliESDtrackCuts::GetStandardTPCOnlyTrackCuts();
80     cout << ">>>> USING TPC only tracks" << endl;
81     pathsuffix+="_TPC";
82   }
83
84   if(optionStr.Contains("NoElectrons")) pathsuffix +="_NoElectrons";
85
86
87
88   Bool_t useMCKinematics = isMC;
89   if (optionStr.Contains("NOMCKIN")) {
90     cout << ">>>> Ignoring MC kinematics" << endl;
91     useMCKinematics=kFALSE;
92     pathsuffix+="_NOMCKIN";
93   }
94   
95   // If we are running on grid, we need the alien handler
96   if (runMode == kMyRunModeGRID) {
97     // Create and configure the alien handler plugin
98     gROOT->LoadMacro("CreateAlienHandler.C");
99     AliAnalysisGrid *alienHandler = CreateAlienHandler(data, runList, pathsuffix.Data(), listToLoad, "full", isMC);  // full
100     if (!alienHandler) {
101       cout << "Cannot create alien handler" << endl;    
102       exit(1);
103     }
104     mgr->SetGridHandler(alienHandler);  
105   }
106
107
108
109   // Add tasks
110   // physics selection
111   gROOT->ProcessLine(".L $ALICE_ROOT/OADB/macros/AddTaskPhysicsSelection.C");
112   physicsSelectionTask = AddTaskPhysicsSelection(isMC);
113
114   //PID
115   gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
116   AddTaskPIDResponse(isMC); 
117
118   // // PID QA:
119   // gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDqa.C");
120   // AddTaskPIDqa();
121
122
123   // Centrality
124   gROOT->LoadMacro("$ALICE_ROOT/OADB/macros/AddTaskCentrality.C");
125   AliCentralitySelectionTask *taskCentr = AddTaskCentrality();
126   // OBSOLETE
127   const char * file1 = "$ALICE_ROOT/ANALYSIS/macros/test_AliCentralityBy1D.root";
128   const char * file2 = "$ALICE_ROOT/ANALYSIS/macros/test_AliCentralityByFunction.root";
129   // END of OBSOLETE
130   if(isMC) taskCentr-> SetMCInput();
131   taskCentr->SetPass(2);
132
133   // Create my own centrality selector
134   AliAnalysisMultPbCentralitySelector * centrSelector = new AliAnalysisMultPbCentralitySelector();
135   centrSelector->SetIsMC(isMC);
136   centrSelector->SetCentrTaskFiles(file1,file2); // for bookkeping only
137   centrSelector->SetCentralityBin(centrBin);
138   if (!useSingleBin) centrSelector->SetCentralityBin(0); // FIXME: ok?
139   centrSelector->SetCentralityEstimator(centrEstimator);
140
141   if(useOtherCentralityCut == 1){
142     cout << "Setting centrality by MULT" << endl;
143     centrSelector->SetUseMultRange();
144     centrSelector->SetMultRange(trackMin,trackMax);
145   }
146   if(useOtherCentralityCut == 2){
147     cout << "Setting centrality by V0" << endl;
148     
149     centrSelector->SetUseV0Range();
150     centrSelector->SetMultRange(trackMin,trackMax);
151   }
152   if(useOtherCentralityCut == 3){
153     cout << "Setting centrality by SPD outer" << endl;    
154     centrSelector->SetUseSPDOuterRange();
155     centrSelector->SetMultRange(trackMin,trackMax);
156   }
157
158   // load my task
159   if (useSingleBin) {
160     gROOT->ProcessLine(".L $ALICE_ROOT/PWG0/multPbPb/AddTaskMultPbPbTracks.C");
161     AliAnalysisTaskMultPbTracks * task = AddTaskMultPbPbTracks("multPbPbtracks.root", cuts, centrSelector); 
162     task->SetIsMC(useMCKinematics);
163     task->SetOfflineTrigger(AliVEvent::kMB);
164     if(optionStr.Contains("TPC")) task->SetTPCOnly();
165     if(optionStr.Contains("NoElectrons")) task->RejectElectrons(kTRUE);
166     if(useMCKinematics) task->GetHistoManager()->SetSuffix("MC");
167     if(customSuffix!=""){
168       cout << "Setting custom suffix: " << customSuffix << endl;    
169       task->GetHistoManager()->SetSuffix(customSuffix);
170     }
171   } else {
172     gROOT->ProcessLine(".L $ALICE_ROOT/PWG0/multPbPb/AddTaskMultPbPbTracksAllCentrality.C");
173     centrSelector->SetUseV0Range(kTRUE);
174     Int_t ncentr = 11;
175    
176     const Float_t minCentr[] = {0 ,79 ,239,559 ,1165,2135,3555,5525,8213 ,12191,15079};
177     const Float_t maxCentr[] = {79,239,559,1165,2135,3555,5525,8213,12191,15079,21000};
178     AliAnalysisTaskMultPbTracks ** tasks = AddTaskMultPbPbTracksAllCentrality("multPbPbtracks.root", cuts, centrSelector, ncentr,minCentr,maxCentr); 
179     for(Int_t icentr = 0; icentr < ncentr; icentr++){
180       tasks[icentr]->Print();
181       cout << "MC KINEMATICS:" << useMCKinematics << endl;
182       
183       tasks[icentr]->SetIsMC(useMCKinematics);
184       tasks[icentr]->SetOfflineTrigger(AliVEvent::kMB);
185       if(optionStr.Contains("TPC")) tasks[icentr]->SetTPCOnly();
186       if(optionStr.Contains("NoElectrons")) task[icentr]->RejectElectrons(kTRUE);
187       if(useMCKinematics) tasks[icentr]->GetHistoManager()->SetSuffix("MC");
188       if(customSuffix!=""){
189         cout << "Setting custom suffix: " << customSuffix+long(icentr) << endl;    
190         tasks[icentr]->GetHistoManager()->SetSuffix(customSuffix+long(icentr));
191       } 
192     }    
193   }
194   // Init and run the analy
195   if (!mgr->InitAnalysis()) return;
196
197   mgr->PrintStatus();
198   
199   if (runMode == kMyRunModeLocal ) {
200     // If running in local mode, create chain of ESD files
201     cout << "RUNNING LOCAL, CHAIN" << endl;    
202     TChain * chain = GetAnalysisChain(data);
203     chain->Print();
204     mgr->StartAnalysis("local",chain,nev);
205   } else if (runMode == kMyRunModeProofLite) {
206     TChain * chain = GetAnalysisChain(data);
207     mgr->StartAnalysis("proof",chain,nev);
208   } 
209   else if (runMode == kMyRunModeCAF) {
210     mgr->StartAnalysis("proof",TString(data)+"#esdTree",nev);
211   } else if (runMode == kMyRunModeGRID) {
212     mgr->StartAnalysis("grid");
213   } else {
214     cout << "ERROR: unknown run mode" << endl;        
215   }
216
217   if (!useOtherCentralityCut) {
218     pathsuffix = pathsuffix + "_" + centrEstimator + "_bin_"+long(centrBin);
219   } else if(useOtherCentralityCut==1){
220     pathsuffix = pathsuffix + "_TrackRange_" + long(trackMin) + "_" + long(trackMax);
221   } else if(useOtherCentralityCut==2){
222     pathsuffix = pathsuffix + "_V0Range_" + long(trackMin) + "_" + long(trackMax);
223   } else if(useOtherCentralityCut==3){
224     pathsuffix = pathsuffix + "_SPDOutRange_" + long(trackMin) + "_" + long(trackMax);
225   }
226   pathsuffix += customSuffix;
227
228   if (doSave) MoveOutput(data, pathsuffix.Data());
229
230   
231
232   
233 }
234
235
236 void MoveOutput(const char * data, const char * suffix = ""){
237
238   TString path("output/");
239   path = path + TString(data).Tokenize("/")->Last()->GetName() + suffix;
240   
241   TString fileName = "multPbPbtracks.root";
242   gSystem->mkdir(path, kTRUE);
243   gSystem->Rename(fileName, path + "/" + fileName);
244   for(Int_t ibin = 0; ibin < 20; ibin++){
245     TString fileBin = fileName;
246     fileBin.ReplaceAll(".root",Form("_%2.2d.root",ibin));
247     gSystem->Rename(fileBin, path + "/" + fileBin);    
248   }
249   
250   gSystem->Rename("event_stat.root", path + "/event_stat.root");      
251   Printf(">>>>> Moved files to %s", path.Data());
252 }  
253
254
255
256 TChain * GetAnalysisChain(const char * incollection){
257   // Builds a chain of esd files
258   // incollection can be
259   // - a single root file
260   // - an xml collection of files on alien
261   // - a ASCII containing a list of local root files
262   TChain* analysisChain = 0;
263   // chain
264   analysisChain = new TChain("esdTree");
265   if (TString(incollection).Contains(".root")){
266     analysisChain->Add(incollection);
267   }
268   else if (TString(incollection).Contains("xml")){
269     TGrid::Connect("alien://");
270     TAlienCollection * coll = TAlienCollection::Open (incollection);
271     while(coll->Next()){
272       analysisChain->Add(TString("alien://")+coll->GetLFN());
273     }
274   } else {
275     ifstream file_collect(incollection);
276     TString line;
277     while (line.ReadLine(file_collect) ) {
278       analysisChain->Add(line.Data());
279     }
280   }
281   analysisChain->GetListOfFiles()->Print();
282
283   return analysisChain;
284 }
285
286
287 void InitAndLoadLibs(Int_t runMode=kMyRunModeLocal, Int_t workers=0,Bool_t debug=0) {
288   // Loads libs and par files + custom task and classes
289
290   // Custom stuff to be loaded
291   //  listToLoad->Add(new TObjString("$ALICE_ROOT/ANALYSIS/AliCentralitySelectionTask.cxx+"));
292   listToLoad->Add(new TObjString("/Users/mfloris/Work/AliSoft/AliRoot-trunk/PWGPP/background/AliHistoListWrapper.cxx+")); // FIXME
293   listToLoad->Add(new TObjString("AliAnalysisMultPbTrackHistoManager.cxx+"));
294   listToLoad->Add(new TObjString("AliAnalysisMultPbCentralitySelector.cxx+"));
295   listToLoad->Add(new TObjString("AliAnalysisTaskMultPbTracks.cxx+"));
296   // listToLoad->Add(new TObjString("$ALICE_ROOT/PWG0/multPbPb/AliAnalysisMultPbTrackHistoManager.cxx+"));
297   // listToLoad->Add(new TObjString("$ALICE_ROOT/PWG0/multPbPb/AliAnalysisMultPbCentralitySelector.cxx+"));
298   // listToLoad->Add(new TObjString("$ALICE_ROOT/PWG0/multPbPb/AliAnalysisTaskMultPbTracks.cxx+"));
299
300
301   if (runMode == kMyRunModeCAF)
302     {
303       cout << "Init in CAF mode" << endl;
304     
305       gEnv->SetValue("XSec.GSI.DelegProxy", "2");
306       //      TProof::Mgr("alice-caf.cern.ch")->SetROOTVersion("VO_ALICE@ROOT::v5-30-03-1");
307       TProof::Mgr("alice-caf.cern.ch")->SetROOTVersion("current");
308       TProof * p = TProof::Open("alice-caf.cern.ch", workers>0 ? Form("workers=%d",workers) : "1x");
309       //      TProof * p = TProof::Open("skaf.saske.sk", workers>0 ? Form("workers=%d",workers) : "");    
310       p->Exec("TObject *o = gEnv->GetTable()->FindObject(\"Proof.UseMergers\"); gEnv->GetTable()->Remove(o);", kTRUE);
311
312       //      TProof::Mgr("alice-caf.cern.ch")->SetROOTVersion("VO_ALICE@ROOT::v5-28-00f");
313       gProof->EnablePackage("VO_ALICE@AliRoot::v5-02-12-AN");
314       gSystem->Load("libCore");
315       gSystem->Load("libTree");
316       gSystem->Load("libGeom");
317       gSystem->Load("libVMC");
318       gSystem->Load("libPhysics");
319       gSystem->Load("libMinuit");
320       gSystem->Load("libSTEERBase");
321       gSystem->Load("libESD");
322       gSystem->Load("libAOD");
323       gSystem->Load("libANALYSIS");
324       gSystem->Load("libOADB");
325       gSystem->Load("libANALYSISalice");   
326
327       // Enable the needed package
328       // gProof->UploadPackage("$ALICE_ROOT/obj/STEERBase");
329       // gProof->EnablePackage("$ALICE_ROOT/obj/STEERBase");
330       // gProof->UploadPackage("$ALICE_ROOT/obj/ESD");
331       // gProof->EnablePackage("$ALICE_ROOT/obj/ESD");
332       // gProof->UploadPackage("$ALICE_ROOT/obj/AOD");
333       // gProof->EnablePackage("$ALICE_ROOT/obj/AOD");
334       // gProof->UploadPackage("$ALICE_ROOT/obj/ANALYSIS");
335       // gProof->EnablePackage("$ALICE_ROOT/obj/ANALYSIS");
336       // gProof->UploadPackage("$ALICE_ROOT/obj/OADB");
337       // gProof->EnablePackage("$ALICE_ROOT/obj/OADB");
338       // gProof->UploadPackage("$ALICE_ROOT/obj/ANALYSISalice");
339       // gProof->EnablePackage("$ALICE_ROOT/obj/ANALYSISalice");
340       // gProof->UploadPackage("$ALICE_ROOT/obj/PWG0base");
341       // gProof->EnablePackage("$ALICE_ROOT/obj/PWG0base");
342       // gROOT->ProcessLine(gSystem->ExpandPathName(".include $ALICE_ROOT/include"));
343       // gROOT->ProcessLine(gSystem->ExpandPathName(".include $ALICE_ROOT/PWG0/multPb"));
344       // gROOT->ProcessLine(gSystem->ExpandPathName(".include $ALICE_ROOT/PWG1/background"));
345     }
346   else if (runMode == kMyRunModeProofLite)
347     {
348       cout << "Init in CAF mode" << endl;
349     
350       gEnv->SetValue("XSec.GSI.DelegProxy", "2");
351       TProof * p = TProof::Open("");
352       //      TProof * p = TProof::Open("skaf.saske.sk", workers>0 ? Form("workers=%d",workers) : "");    
353       //      p->Exec("TObject *o = gEnv->GetTable()->FindObject(\"Proof.UseMergers\"); gEnv->GetTable()->Remove(o);", kTRUE);
354
355       //      TProof::Mgr("alice-caf.cern.ch")->SetROOTVersion("VO_ALICE@ROOT::v5-28-00f");
356       //      TProof::Mgr("alice-caf.cern.ch")->SetROOTVersion("5.28/00f");
357       gProof->UploadPackage("$ALICE_ROOT/ANALYSIS/macros/AliRootProofLite.par");
358       gProof->EnablePackage("AliRootProofLite");
359
360     }
361   else
362     {
363       cout << "Init in Local or Grid mode" << endl;
364       gSystem->Load("libCore");
365       gSystem->Load("libTree");
366       gSystem->Load("libGeom");
367       gSystem->Load("libVMC");
368       gSystem->Load("libPhysics");
369       gSystem->Load("libMinuit");
370       gSystem->Load("libSTEERBase");
371       gSystem->Load("libESD");
372       gSystem->Load("libAOD");
373       gSystem->Load("libANALYSIS");
374       gSystem->Load("libOADB");
375       gSystem->Load("libANALYSISalice");   
376       // Use AliRoot includes to compile our task
377       gROOT->ProcessLine(".include $ALICE_ROOT/include");
378
379       // gSystem->Load("libVMC");
380       // gSystem->Load("libTree");
381       // gSystem->Load("libSTEERBase");
382       // gSystem->Load("libESD");
383       // gSystem->Load("libAOD");
384       // gSystem->Load("libANALYSIS");
385       // gSystem->Load("libANALYSISalice");
386       // gSystem->Load("libPWG0base");
387     
388       gROOT->ProcessLine(gSystem->ExpandPathName(".include $ALICE_ROOT/PWG0/multPb"));
389       gROOT->ProcessLine(gSystem->ExpandPathName(".include $ALICE_ROOT/PWGPP/background"));
390       //    gROOT->ProcessLine(gSystem->ExpandPathName(".include $ALICE_ROOT/PWGPP/background/"));
391     }
392   // Load helper classes
393   TIterator * iter = listToLoad->MakeIterator();
394   TObjString * name = 0;
395   while (name = (TObjString *)iter->Next()) {
396     gSystem->ExpandPathName(name->String());
397     cout << name->String().Data();
398     if (runMode == kMyRunModeCAF || runMode == kMyRunModeProofLite) {
399       gProof->Load(name->String()+(debug?"+g":""));   
400     } else {
401       gROOT->LoadMacro(name->String()+(debug?"+g":""));   
402     }
403   }
404
405 }