]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/STRANGENESS/LambdaK0PbPb/run.C
pA option added into the task,
[u/mrichter/AliRoot.git] / PWGLF / STRANGENESS / LambdaK0PbPb / run.C
1 // #include <iostream>
2 // #include "AliAnalysisManager.h"
3 // #include "AliESDInputHandler.h"
4 // #include "AliMCEventHandler.h"
5 // #include "AliAnalysisGrid.h"
6 // #include "AliCentralitySelectionTask.h"
7 // #include "AliAnalysisCentralitySelector.h"
8 // #include "AliAnalysisTaskPerformanceStrange.h"
9 // #include "TString.h"
10 // #include "TChain.h"
11 // #include "TAlienCollection.h"
12 // #include <fstream>
13 // #include "TObjString.h"
14 // #include "TIterator.h"
15 // #include "TGrid.h"
16 // #include "TROOT.h"
17
18 // #include "CreateAlienHandler.C"
19 // #include 
20
21 using namespace std;
22
23 enum { kMyRunModeLocal = 0, kMyRunModeCAF, kMyRunModeGRID};
24
25 TList * listToLoad = new TList(); // Additional classes to be loaded, see InitAndLoadLibs
26
27 TChain * GetAnalysisChain(const char * incollection);
28 void InitAndLoadLibs(Int_t runMode=kMyRunModeLocal, Int_t workers=0,Bool_t debug=0) ;
29
30 void run(const char * data, const char * passOrPath, Long64_t nev = -1, Long64_t offset = 0, Bool_t debug = kFALSE, Int_t runMode = 2, Bool_t isMC = 0, Bool_t usePID = kTRUE, const char* option = "",TString customSuffix = "", Int_t workers = -1, const char * gridMode="full", Int_t binMin=0, Int_t binMax = 10)
31 {
32   // runMode:
33   //
34   // 0 local 
35   // 1 proof
36   // 2 grid
37
38   if (nev < 0)
39     //    nev = 1234567890;
40     nev = 5000;
41   InitAndLoadLibs(runMode,workers,debug);
42   
43   // Create the analysis manager
44   AliAnalysisManager * mgr = new AliAnalysisManager;
45
46   // Add ESD handler
47   AliESDInputHandler* esdH = new AliESDInputHandler;
48   mgr->SetInputEventHandler(esdH);
49
50   if(isMC) {
51     AliMCEventHandler* handler = new AliMCEventHandler;
52     handler->SetPreReadMode(AliMCEventHandler::kLmPreRead);
53     mgr->SetMCtruthEventHandler(handler);
54   }
55
56
57   // If we are running on grid, we need the alien handler
58   if (runMode == kMyRunModeGRID) {
59     // Create and configure the alien handler plugin
60     TGrid::Connect("alien://");// Why do I need this? Without a get a bus error...
61         gROOT->LoadMacro("CreateAlienHandler.C");
62     AliAnalysisGrid *alienHandler = CreateAlienHandler(data, listToLoad, gridMode, isMC);  
63     if (!alienHandler) {
64       cout << "Cannot create alien handler" << endl;    
65       exit(1);
66     }
67     mgr->SetGridHandler(alienHandler);  
68   }
69   
70   // PID task
71   gROOT->ProcessLine(".L $ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
72   AddTaskPIDResponse(isMC,kTRUE);
73   //AddTaskPIDResponse();
74   // Physics selection
75   gROOT->ProcessLine(".L $ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
76   AliPhysicsSelectionTask * physicsSelectionTask = AddTaskPhysicsSelection(isMC,kTRUE,0);
77
78   // Centrality
79   gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
80   AliCentralitySelectionTask *taskCentrality = AddTaskCentrality();
81   //taskCentrality->SetPass(2);
82   if(isMC) taskCentrality->SetMCInput();
83
84   AliAnalysisCentralitySelector * centrSelector = new AliAnalysisCentralitySelector();
85   centrSelector->SetIsMC(isMC);
86   centrSelector->SetCentralityEstimator("V0M"); // Todo: add parameter to macro?
87
88   // Parse option strings
89   TString optionStr(option);
90   
91   // remove SAVE option if set
92   Bool_t doSave = kFALSE;
93
94   if (optionStr.Contains("SAVE"))
95     {
96       optionStr = optionStr(0,optionStr.Index("SAVE")) + optionStr(optionStr.Index("SAVE")+4, optionStr.Length());
97       doSave = kTRUE;
98     }
99   TString pathsuffix = "";
100   // Not used, but may be useful
101   Bool_t useMCKinematics = isMC;
102   if (optionStr.Contains("NOMCKIN")) {
103     cout << ">>>> Ignoring MC kinematics" << endl;
104     useMCKinematics=kFALSE;
105     pathsuffix+="_NOMCKIN";
106   }
107   
108   gROOT->ProcessLine(".L AddTaskLambdaK0PbPb.C");
109   Int_t nbin = 0; // will contain the number of centrality bins
110   AliAnalysisTaskPerformanceStrange ** task = AddTaskLambdaK0PbPb("lambdak0.root", centrSelector, nbin, binMin, binMax,isMC);
111   // configure task
112   //  else if (iAODanalysis) task->SetAnalysisType("AOD");
113   // FIXME: add options to macro
114   // FIXME: put physics selection inside the task
115   cout << nbin << endl;
116   
117   for(Int_t ibin = 0; ibin < nbin; ibin++){
118     cout << "ibin " << ibin << "  "<< endl;//task[ibin] << endl;
119     
120     task[ibin]->SetAnalysisType("ESD");
121     cout << "1" << endl;
122     task[ibin]->SetAnalysisMC(isMC); // 0 or 1
123     cout << "2" << endl;
124     task[ibin]->SetCollidingSystems(2); // 0 =pp, 1=AA  2=pA
125     cout << "3" << endl;
126     task[ibin]->SetAnalysisCut("no");
127     cout << "4" << endl;
128     task[ibin]->SetQASelector(kFALSE);  // Todo -> put trees for QA
129     cout<< "5" << endl;
130     if(usePID) 
131       task[ibin]->SetUsePID("withPID"); // withPID or withoutPID
132     else
133       task[ibin]->SetUsePID("withoutPID"); // withPID or withoutPID
134     cout << "5" << endl;
135   }
136
137   // Init and run the analy
138   if (!mgr->InitAnalysis()) return;
139
140   mgr->PrintStatus();
141   
142   if (runMode == kMyRunModeLocal ) {
143     // If running in local mode, create chain of ESD files
144     cout << "RUNNING LOCAL, CHAIN" << endl;    
145     TChain * chain = GetAnalysisChain(data);
146     //    chain->Print();
147     mgr->StartAnalysis("local",chain,nev);
148   } else if (runMode == kMyRunModeCAF) {
149     mgr->StartAnalysis("proof",TString(passOrPath)+data+"#esdTree",nev);
150   } else if (runMode == kMyRunModeGRID) {
151     mgr->StartAnalysis("grid");
152   } else {
153     cout << "ERROR: unknown run mode" << endl;        
154   }
155
156   pathsuffix += customSuffix;
157
158       if (doSave) MoveOutput(data, pathsuffix.Data());
159
160   
161 }
162
163 void MoveOutput(const char * data, const char * suffix = ""){
164
165   //  TString path("output10bins/");
166   TString path("output10binsNew/");
167   path = path + TString(data).Tokenize("/")->Last()->GetName() + suffix;
168   
169   TString fileName = "lambdak0.root";
170   gSystem->mkdir(path, kTRUE);
171   gSystem->Rename(fileName, path + "/" + fileName);
172   for(Int_t ibin = 0; ibin < 20; ibin++){
173     TString fileBin = fileName;
174     fileBin.ReplaceAll(".root",Form("_%2.2d.root",ibin));
175     gSystem->Rename(fileBin, path + "/" + fileBin);    
176   }
177   
178   gSystem->Rename("event_stat.root", path + "/event_stat.root");      
179   gSystem->Rename("EventStat_temp.root", path + "/EventStat_temp.root");      
180   Printf(">>>>> Moved files to %s", path.Data());
181 }  
182
183
184
185 TChain * GetAnalysisChain(const char * incollection){
186   // Builds a chain of esd files
187   // incollection can be
188   // - a single root file
189   // - an xml collection of files on alien
190   // - a ASCII containing a list of local root files
191
192   TChain* analysisChain = 0;
193   // chain
194   analysisChain = new TChain("esdTree");
195   if (TString(incollection).Contains(".root")){
196     analysisChain->Add(incollection);
197   }
198   else if (TString(incollection).Contains("xml")){
199     TGrid::Connect("alien://");
200     TGridCollection * coll = TAlienCollection::Open (incollection);
201     while(coll->Next()){
202       analysisChain->Add(TString("alien://")+coll->GetLFN());
203     }
204   } else {
205     ifstream file_collect(incollection);
206     TString line;
207     while (line.ReadLine(file_collect) ) {
208       analysisChain->Add(line.Data());
209     }
210   }
211   analysisChain->GetListOfFiles()->Print();
212
213   return analysisChain;
214 }
215
216
217 void InitAndLoadLibs(Int_t runMode, Int_t workers,Bool_t debug) {
218   // Loads libs and par files + custom task and classes (the order is important)
219   // listToLoad->Add(new TObjString("$ALICE_ROOT/STEER/AliCentrality.cxx")); // FIXME: why do I have to load it?!?
220   listToLoad->Add(new TObjString("AliAnalysisCentralitySelector.cxx"));
221   listToLoad->Add(new TObjString("AliAnalysisTaskPerformanceStrange.cxx"));
222
223   if (runMode == kMyRunModeCAF)
224     {
225       cout << "Init in CAF mode" << endl;
226     
227       gEnv->SetValue("XSec.GSI.DelegProxy", "2");
228       Char_t* alienuser = gSystem->Getenv("alien_API_USER");
229       TProof * p = TProof::Open(alienuser!=0 ? Form("%s@alice-caf.cern.ch",alienuser) : "alice-caf.cern.ch", workers>0 ? Form("workers=%d",workers) : "");
230       //TProof * p = TProof::Open("skaf.saske.sk", workers>0 ? Form("workers=%d",workers) : "");    
231       //p->Exec("TObject *o = gEnv->GetTable()->FindObject(\"Proof.UseMergers\"); gEnv->GetTable()->Remove(o);", kTRUE); // avoid submerging
232              gProof->EnablePackage("VO_ALICE@AliRoot::v5-04-11-AN");
233             gProof->GetManager()->SetROOTVersion("VO_ALICE@ROOT::v5-34-02");
234       // gProof->EnablePackage("VO_ALICE@AliRoot::v5-02-04-AN");
235
236
237       // Enable the needed package
238       // FIXME: what if I don't want to use par files?
239       gSystem->AddIncludePath("-I${ALICE_ROOT}/include/");
240       gSystem->AddIncludePath("-I${ALICE_ROOT}/STEER/");
241       // gProof->UploadPackage("$ALICE_ROOT/obj/STEERBase");
242       // gProof->EnablePackage("$ALICE_ROOT/obj/STEERBase");
243       // gProof->UploadPackage("$ALICE_ROOT/obj/ESD");
244       // gProof->EnablePackage("$ALICE_ROOT/obj/ESD");
245       // gProof->UploadPackage("$ALICE_ROOT/obj/AOD");
246       // gProof->EnablePackage("$ALICE_ROOT/obj/AOD");
247       // gProof->UploadPackage("$ALICE_ROOT/obj/ANALYSIS");
248       // gProof->EnablePackage("$ALICE_ROOT/obj/ANALYSIS");
249       // gProof->UploadPackage("$ALICE_ROOT/obj/ANALYSISalice");
250       // gProof->EnablePackage("$ALICE_ROOT/obj/ANALYSISalice");
251       // gProof->UploadPackage("$ALICE_ROOT/obj/CORRFW");
252       // gProof->EnablePackage("$ALICE_ROOT/obj/CORRFW");
253       // gProof->UploadPackage("~/Desktop/OADB");//FIXME
254       // gProof->EnablePackage("~/Desktop/OADB");//FIXME
255       
256     }
257   else
258     {
259       cout << "Init in Local or Grid mode" << endl;
260       Int_t ret=-1;
261
262       if ( gSystem->Load("libCore") < 0 ) return ret; ret--;
263       if ( gSystem->Load("libTree") < 0 ) return ret; ret--;
264       if ( gSystem->Load("libGeom") < 0 ) return ret; ret--;
265       if ( gSystem->Load("libVMC") < 0 ) return ret; ret--;
266       if ( gSystem->Load("libPhysics") < 0 ) return ret; ret--;
267       if ( gSystem->Load("libMinuit") < 0 ) return ret; ret--;
268       if ( gSystem->Load("libSTEERBase") < 0 ){ cout<<"libSTEERBase coul not be loaded!!!"<<endl; }//return ret; ret--;}
269       if ( gSystem->Load("libESD") < 0 ) return ret; ret--;
270       if ( gSystem->Load("libAOD") < 0 ) return ret; ret--;
271       if ( gSystem->Load("libANALYSIS") < 0 ) return ret; ret--;
272       if ( gSystem->Load("libANALYSISalice") < 0 ) return ret; ret--;
273
274
275       gROOT->ProcessLine(".include $ALICE_ROOT/include");
276       gROOT->ProcessLine(".include $ALICE_ROOT/STEER");
277       cout<<"/////////////////////////////////////"<<endl;
278       cout<<endl<<"libraries loaded !"<<endl;
279       cout<<"/////////////////////////////////////"<<endl;
280     }
281   // Load helper classes
282   TIterator * iter = listToLoad->MakeIterator();
283   TObjString * name = 0;
284   while ((name = (TObjString *)iter->Next())) {
285     gSystem->ExpandPathName(name->String());
286     cout << name->String().Data() << endl;
287     if (runMode == kMyRunModeCAF) {
288       gProof->Load(name->String()+(debug?"++g":"+"));   
289     } else {
290       gROOT->LoadMacro(name->String()+(debug?"++g":"+"));   
291     }
292   }
293
294 }