AliPhysics  cda3415 (cda3415)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GridTerminate.C
Go to the documentation of this file.
1 
11 #ifndef __CINT__
12 # include <TString.h>
13 # include <TSystem.h>
14 # include <TGrid.h>
15 # include <TFile.h>
16 # include <TObjArray.h>
17 # include <TObjString.h>
18 # include <TError.h>
19 # include <TEnv.h>
20 # include <TROOT.h>
21 # include <AliAnalysisManager.h>
22 # include <AliAnalysisAlien.h>
23 # include <fstream>
24 #else
25 class TString;
26 #endif
27 
34 Bool_t LoadLib(const char* libName)
35 {
36  if (gSystem->Load(libName) < 0) {
37  Error("GridTerminate", "Failed to load library %s",libName);
38  return false;
39  }
40  Info("GridTerminate","Loaded library %s",libName);
41  return true;
42 }
50 Bool_t LoadPar(const char* parName)
51 {
52  if (!AliAnalysisAlien::SetupPar(parName)) {
53  Error("GridTerminate","Failed to load PAR %s",parName);
54  return false;
55  }
56  Info("GridTerminate","Loaded package %s",parName);
57  return true;
58 }
59 
68 {
69  // Load plugin
70  TFile* file = TFile::Open(Form("%s_plugin.root",name.Data()),"READ");
71  // TFile* plug = TFile::Open("plugin.root","READ");
72  if (!file) {
73  // Error("GridTerminate","Failed to open %s_plugin.root",name.Data());
74  Error("GridTerminate","Failed to open %s_plugin.root",
75  name.Data());
76  return false;
77  }
78  AliAnalysisAlien* handler =
79  static_cast<AliAnalysisAlien*>(file->Get("plugin"));
80  if (!handler) {
81  Error("GridTerminate","Failed to load plugin");
82  return false;
83  }
84  Info("GridTerminate","Setting grid handler");
85  handler->SetRunMode("terminate");
86  handler->SetMergeViaJDL(true);
87  AliAnalysisManager::GetAnalysisManager()->SetGridHandler(handler);
88 
89  return true;
90 }
91 
107 Bool_t Setup(const TString& name,
108  const TString& libs,
109  const TString& pars,
110  const TString& srcs,
111  Bool_t local=false,
112  Bool_t localLibsNotPar=true)
113 {
114  // Load basic ROOT libraries
115  gSystem->AddDynamicPath("/usr/lib");
116  if (gSystem->Load("libTree") < 0) return false;
117  if (gSystem->Load("libGeom") < 0) return false;
118  if (gSystem->Load("libVMC") < 0) return false;
119  if (gSystem->Load("libPhysics") < 0) return false;
120  if (gSystem->Load("libMinuit") < 0) return false;
121 
122  // Load basic AliROOT libraries
123  if (gSystem->Load("libSTEERBase") < 0) return false;
124  if (gSystem->Load("libESD") < 0) return false;
125  if (gSystem->Load("libAOD") < 0) return false;
126  if (gSystem->Load("libANALYSIS") < 0) return false;
127  if (gSystem->Load("libOADB") < 0) return false;
128  if (gSystem->Load("libANALYSISalice") < 0) return false;
129 
130  // Load libraries
131  if (!libs.IsNull()) {
132  TObjArray* libsArray = libs.Tokenize(" ");
133  TObjString* lib = 0;
134  TIter nextLib(libsArray);
135  while ((lib = static_cast<TObjString*>(nextLib()))) {
136  const TString& libName = lib->String();
137  if (libName.Contains("libSTEERBase") ||
138  libName.Contains("libESD") ||
139  libName.Contains("libAOD") ||
140  libName.Contains("libANALYSIS") ||
141  libName.Contains("libOADB") ||
142  libName.Contains("libANALYSISalice")) continue;
143  if (!libName.Contains(".so")) continue;
144  if (!LoadLib(libName.Data())) return false;
145  }
146  libsArray->Delete();
147  }
148 
149  // Load packages
150  if (!pars.IsNull()) {
151  TObjArray* parArray = pars.Tokenize(" ");
152  TObjString* par = 0;
153  TIter nextPar(parArray);
154  while ((par = static_cast<TObjString*>(nextPar()))) {
155  TString parName(par->String());
156  if (parName.EndsWith(".par")) parName.ReplaceAll(".par", "");
157  if (parName.EqualTo("STEERBase") ||
158  parName.EqualTo("ESD") ||
159  parName.EqualTo("AOD") ||
160  parName.EqualTo("ANALYSIS") ||
161  parName.EqualTo("OADB") ||
162  parName.EqualTo("ANALYSISalice")) continue;
163  Bool_t ret = true;
164  if (local && localLibsNotPar)
165  ret = LoadLib(Form("lib%s.so", parName.Data()));
166  else
167  ret = LoadPar(parName.Data());
168  if (!ret) return false;
169  }
170  }
171 
172  // Load sources
173  if (!srcs.IsNull()) {
174  TObjArray* srcArray = srcs.Tokenize(" ");
175  TObjString* src = 0;
176  TIter nextSrc(srcArray);
177  while ((src = static_cast<TObjString*>(nextSrc()))) {
178  const TString& srcName = src->String();
179  gROOT->ProcessLine(Form(".L %s+g", srcName.Data()));
180  }
181  }
182 
183  // Connect to the grid
184  gEnv->SetValue("XSec.GSI.DelegProxy", "2");
185  // TGrid::Connect("alien://");
186  // if (!gGrid) {
187  // Error("GridTerminate", "Failed to connect to AliEn");
188  // return false;
189  // }
190 
191  // Load the analysis manager from file
192  TString base(name);
193  base.Append(".root");
194  if (gSystem->AccessPathName(base.Data())) {
195  // Couldn't read from current directory, try sub-dir
196  TString sub(gSystem->ConcatFileName(name, base));
197  if (gSystem->AccessPathName(sub)) {
198  Error("GridTerminate","Couldn't find manager file %s",base.Data());
199  return false;
200  }
201  base = sub;
202  }
204  if (!mgr) {
205  Error("GridTerminate", "Failed to load manager from %s",base.Data());
206  return false;
207  }
208  if (!name.EqualTo(mgr->GetName())) {
209  Error("GridTerminate","Read manager %s is not %s",
210  mgr->GetName(),name.Data());
211  return false;
212  }
213  Info("GridTerminate","Loaded analysis manager");
214 
215  // If we do a local merge, do not do any else
216  if (local) return true;
217 
218  return LoadHandler(name);
219 }
220 
237  const TString& libs,
238  const TString& pars,
239  const TString& srcs,
240  Bool_t local=false,
241  Bool_t localLibsNotPar=true)
242 {
243  if (!Setup(name, libs, pars, srcs, local, localLibsNotPar)) return false;
244 
245  // Run the terminate job
246  Info("GridTerminate","Starting terminate job - %s", local ? "locally" : "on the grid");
247 
248  AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
249  if (!local) {
250 
251  AliAnalysisAlien* handler = static_cast<AliAnalysisAlien*>(mgr->GetGridHandler());
252  if (!handler) {
253  Error("GridTerminate", "Manager does now have an AliEn handler");
254  return false;
255  }
256  if (mgr->StartAnalysis("grid") < 0) return false;
257 
258  std::ofstream outJobs(Form("%s_merge.jobid", mgr->GetName()));
259  outJobs << handler->GetGridJobIDs() << std::endl;
260  outJobs.close();
261 
262  std::ofstream outStages(Form("%s_merge.stage", mgr->GetName()));
263  outStages << handler->GetGridStages() << std::endl;
264  outStages.close();
265 
266  return true;
267  }
268 
269  // mgr->SetDebugLevel(2);
270  mgr->SetSkipTerminate(false);
271  TTree* dummy = 0;
272  if (mgr->StartAnalysis("gridterminate", dummy, -1, 0) < 0) return false;
273 
274  return true;
275 }
276 
277 //
278 // EOF
279 //
280 
TSystem * gSystem
Bool_t LoadHandler(const TString &name)
Definition: GridTerminate.C:67
Bool_t LoadPar(const char *parName)
Definition: GridTerminate.C:50
Bool_t Setup(const TString &name, const TString &libs, const TString &pars, const TString &srcs, Bool_t local=false, Bool_t localLibsNotPar=true)
void SetupPar(char *pararchivename)
Definition: ana.C:601
TFile * file
Bool_t GridTerminate(const TString &name, const TString &libs, const TString &pars, const TString &srcs, Bool_t local=false, Bool_t localLibsNotPar=true)
bool Bool_t
Definition: External.C:53
AliAnalysisManager * LoadAnalysisManager(const char *fname)
Bool_t LoadLib(const char *libName)
Definition: GridTerminate.C:34