.so cleanup: more gSystem->Load()
[u/mrichter/AliRoot.git] / PWGLF / RESONANCES / macros / lego_train / old / RunALICE.C
CommitLineData
3248b3db 1#ifndef __CINT__
2#include <TSystem.h>
3#include <TROOT.h>
4#include <Rtypes.h>
5#include <TString.h>
6#include <TNamed.h>
7#include <TObjArray.h>
8#include <TObjString.h>
9#include <TList.h>
3248b3db 10#include <TStopwatch.h>
11#endif
12
13Bool_t RunALICE(TString anSrc = "grid",
14 TString anMode = "terminate",
15 TString input="aod" /*or "esd"*/,
16 TString inputMC="" /*or "mc"*/,
17 Long64_t nEvents = 1e10,
18 Long64_t nSkip = 0,
19 TString dsName="",
deb5c67a 20 TString alirsnliteManagers ="AddAMRsn",
3248b3db 21 Bool_t useMultiHandler=kTRUE,
22 TString alirsnlitesrc ="$ALICE_ROOT",
23 TString alirsnlitetasks =""
24 ) {
25
26 // some init work
27 anSrc.ToLower(); anMode.ToLower(); input.ToLower(); inputMC.ToLower();
28
29 // loads libs and setup include paths
30 if (LoadLibsBase(alirsnlitesrc)) return kFALSE;
31
32 // reset manager if already exists
33 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
34 if (mgr) delete mgr;
35 mgr = new AliAnalysisManager("AliRsnLiteAM","AliRsnLite Analysis Manager");
36
37 Bool_t useAODOut = kFALSE;
38 CreateInputHandlers(input,inputMC,useAODOut,useMultiHandler);
39
40 // add default grid handler
41 gROOT->LoadMacro("SetupAnalysisPlugin.C");
42 AliAnalysisGrid *analysisPlugin = SetupAnalysisPlugin(anMode.Data());
43 if (!analysisPlugin) { Printf("Error : analysisPlugin is null !!!"); return kFALSE; }
44 mgr->SetGridHandler(analysisPlugin);
45 if (!dsName.IsNull()) {
46 if (!anSrc.CompareTo("proof") && !anMode.CompareTo("full")) {
47 analysisPlugin->SetProofDataSet(dsName.Data());
48 Printf(Form("Using DataSet %s ...",dsName.Data()));
49 } else {
50 analysisPlugin->SetFileForTestMode(dsName.Data());
51 Printf(Form("Using Test file %s ...",dsName.Data()));
52 }
53 }
54
55 TList *listManagers = CreateListOfManagersFromDir(alirsnliteManagers,alirsnlitetasks);
56 if (!listManagers) { Printf("Error : CreateListOfManagersFromDir failed !!!"); return kFALSE;}
57
58 // adds all tasks
59 if (!AddAllManagers(listManagers, anSrc, anMode,input,inputMC)) { Printf("Error : AddAllManagers failed !!!"); return kFALSE;}
60
43d3541a 61 gSystem->ListLibraries("ls");
62
3248b3db 63 TStopwatch timer;
64 timer.Start();
65 // runs analysis
66 if (!RunAnalysisManager(anSrc, anMode.Data(), nEvents, nSkip)) { Printf("Error : RunAnalysisManager failed !!!"); return kFALSE;}
67
68 timer.Stop();
69 timer.Print();
70 Printf("Working directory is %s ...", gSystem->WorkingDirectory());
71 Printf("Done OK");
72 return kTRUE;
73
74}
75
76Int_t LoadLibsBase(TString alirsnlitesrc) {
77 Int_t num = 0;
b0635849 78 if (gSystem->Load("libTree") < 0) {num++; return num;}
79 if (gSystem->Load("libGeom") < 0) {num++; return num;}
80 if (gSystem->Load("libVMC") < 0) {num++; return num;}
81 if (gSystem->Load("libMinuit") < 0) {num++; return num;}
82 if (gSystem->Load("libPhysics") < 0) {num++; return num;}
83 if (gSystem->Load("libSTEERBase") < 0) {num++; return num;}
84 if (gSystem->Load("libESD") < 0) {num++; return num;}
85 if (gSystem->Load("libAOD") < 0) {num++; return num;}
86 if (gSystem->Load("libANALYSIS") < 0) {num++; return num;}
87 if (gSystem->Load("libOADB") < 0) {num++; return num;}
88 if (gSystem->Load("libANALYSISalice") < 0) {num++; return num;}
3248b3db 89
90 gSystem->AddIncludePath(Form("-I\"%s/include\"", gSystem->ExpandPathName(alirsnlitesrc.Data())));
91 gROOT->ProcessLine(Form(".include %s/include", gSystem->ExpandPathName(alirsnlitesrc.Data())));
deb5c67a 92
93 return 0;
3248b3db 94}
95
96Bool_t CreateInputHandlers(TString input,TString inputMC,Bool_t useAODOut=kFALSE,Bool_t useMultiHandler=kTRUE) {
97
98 Bool_t useMC = !inputMC.CompareTo("mc");
99
100 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
101 if (!mgr) { Printf("Error [CreateInputHandlers] : mgr is null !!!"); return kFALSE; }
102
103 if (useMultiHandler) {
104 AliMultiInputEventHandler *inputHandler = new AliMultiInputEventHandler();
105 if (!input.CompareTo("esd")) {
106 inputHandler->AddInputEventHandler(new AliESDInputHandler());
107 if (useMC) inputHandler->AddInputEventHandler(new AliMCEventHandler());
108 } else if (!input.CompareTo("aod")) {
109 inputHandler->AddInputEventHandler(new AliAODInputHandler());
110 }
111
112 mgr->SetInputEventHandler(inputHandler);
113 } else {
114 if (!input.CompareTo("esd")) {
115 mgr->SetInputEventHandler(new AliESDInputHandler());
116 if (useMC) mgr->SetMCtruthEventHandler(new AliMCEventHandler());
117 } else if (!input.CompareTo("aod")) {
118 mgr->SetInputEventHandler(new AliAODInputHandler());
119 }
120 mgr->SetInputEventHandler(inputHandler);
121 }
122
123 if (useAODOut) {
124 AliAODHandler *aodHandler = new AliAODHandler();
125 aodHandler->SetOutputFileName("AliAOD.root");
126 mgr->SetOutputEventHandler(aodHandler);
127 }
128
deb5c67a 129 return kTRUE;
130
3248b3db 131}
132
133TList *CreateListOfManagersFromDir(TString listManagersNames="",TString dir="") {
134
135 TList *listManagers = new TList;
136 TString dirsStr;
137 TObjArray *dirs=0;
138
139 if (listManagersNames.IsNull()) {
140 if (dir.IsNull() || gSystem->AccessPathName(gSystem->ExpandPathName(dir.Data()))) {
141 Printf("Error [CreateListOfManagersFromDir] : Dir '%s' doesn't exists !!!",dir.Data());
142 return 0;
143 }
144 dirsStr = gSystem->GetFromPipe(Form("ls %s",dir.Data()));
145 dirs = dirsStr.Tokenize("\n");
146 } else {
147 dirsStr = listManagersNames;
148 dirs = dirsStr.Tokenize(" ");
149 }
150
151 TIter next(dirs);
152 Int_t counter=0;
153 TObjString *str,*strtmp;
154 TObjArray *mydirstrTok;
155 TString mydirstr,main,prefix;
156 while ((str = (TObjString *)next.Next())) {
157 // TODO add direcotry
158 mydirstr = str->GetString();
159 if (mydirstr.IsNull()) continue;
160
161 Printf("Adding %s .,,",mydirstr.Data());
162 mydirstrTok = mydirstr.Tokenize("_");
163
164 main = ((TObjString *)mydirstrTok->At(0))->GetString();
165
166 strtmp = (TObjString *)mydirstrTok->At(1);
167 if (strtmp) prefix = strtmp->GetString(); else prefix="";
168
169 listManagers->Add(new TNamed(main,prefix));
170 }
171
172 return listManagers;
173}
174
175Bool_t AddAllManagers(TList *listManagers,TString anSrc, TString anMode,TString input,TString inputMC) {
176 TIter next(listManagers);
177 Int_t counter=0;
178 TNamed *name;
179 while ((name = (TNamed *)next.Next())) {
180 if (!AddAnalysisManager(name->GetName(), anSrc, anMode,input,inputMC,name->GetTitle(),Form("%d",counter++))) {
181 Printf("Error: Problem adding %s",name->GetName());
182 return kFALSE;
183 }
184 }
185
186 return kTRUE;
187}
188
189Bool_t AddAnalysisManager(TString managerMacro, TString anSrc, TString anMode,TString input,TString inputMC, TString postfix,TString idStr) {
190 gROOT->LoadMacro(Form("%s.C", managerMacro.Data()));
191 return gROOT->ProcessLine(Form("%s\(\"%s\",\"%s\",\"%s\"\,\"%s\",\"%s\",\"%s\"\);", managerMacro.Data(), anSrc.Data(), anMode.Data(),input.Data(),inputMC.Data(), postfix.Data(),idStr.Data()));
192}
193
194Bool_t RunAnalysisManager(TString anSrc = "proof", TString anMode = "test", Long64_t nEvents = 1e10, Long64_t nSkip = 0) {
195
196 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
197
198 if (!mgr) { Printf("Error [RunAnalysisManager] : mgr is null !!!"); return kFALSE; }
199
200 // Run analysis
201 mgr->InitAnalysis();
202 mgr->PrintStatus();
203
204 if ((!anSrc.CompareTo("proof")) || (!anSrc.CompareTo("local"))) {
205 mgr->StartAnalysis(anSrc.Data(), nEvents, nSkip);
206 } else {
207 mgr->StartAnalysis(anSrc.Data());
208 }
209
210 return kTRUE;
211}