3 * @author Christian Holm Christensen <cholm@master.hehi.nbi.dk>
4 * @date Tue Oct 16 18:58:37 2012
9 * @ingroup pwglf_forward_trains_helper
16 # include "OutputUtilities.C"
17 # include "ParUtilities.C"
18 # include "ChainBuilder.C"
22 # include <TProofLog.h>
23 # include <TProofDebug.h>
24 # include <AliAnalysisManager.h>
32 // ===================================================================
34 * Handle analysis on a Proof farm.
36 * This helper is triggered by URIs of the form
39 * proof://[<user>@]<host>[:<port>]/<dsname>[?<options>][#<treename>]
43 * <dt><user@gt;</dt>
44 * <dd>Optional user name</dd>
45 * <dt><host@gt;</dt>
46 * <dd>PROOF cluster master host</dd>
47 * <dt><port@gt;</dt>
48 * <dd>Optional PROOF cluster port on master host</dd>
49 * <dt><dsname@gt;</dt>
50 * <dd>Data set name</dd>
51 * <dt><tt><datadir></tt></dt>
52 * <dd>is the base directory holding data files </dd>
53 * <dt><tt><collection></tt></dt>
54 * <dd>is an ASCII or XML list of input sources</dd>
55 * <dt><tt><file></tt></dt>
56 * <dd>is a single ROOT file</dd>
57 * <dt><treename@gt;</dt>
58 * <dd>Optional tree name in data set, often <tt>esdTree</tt> or
59 * <tt>aodTree</tt></dd>
60 * <dt><options@gt;</dt>
61 * <dd>List of options separated by an &
63 * <dt><tt>workers=N[x]</tt></dt>
64 * <dd>Set the number of workers to use. If <tt>x</tt> is appended,
65 * then it's maximum number of workers per slave</dd>
66 * <dt><tt>dsname</tt>[=<output dataset>]</dt>
67 * <dd>Register tree output (e.g., AOD) as a new data set on the
68 * PROOF cluster. If <output dataset> is not specified, take
69 * the name of the train.</dd>
70 * <dt><tt>par[=all]</tt></dt>
71 * <dd>Use PAR files. If the value <tt>all</tt> is given, then also
72 * PAR files of STEERBase, ESD, AOD, ANALYSIS, OADB, ANALYSISalice
74 * <dt><tt>mode=[default,rec,sim,train,custom]</tt></dt>
75 * <dd>Set the AliROOT mode. If not specified <tt>default</tt>
76 * is assumed</tt>. See also CreateAliROOTPar</dd>
77 * <dt><tt>storage=<url></tt></dt>
78 * <dd>Specify a non-default storage location for special output
79 * (e.g., AOD trees). <url> should be a valid XRootd
80 * server URI accessible to the slaves - e.g.,
81 * <tt>root://lxplus.cern.ch:10930//tmp</tt>.</dd>
86 * @ingroup pwglf_forward_trains_helper
88 struct ProofHelper : public Helper
96 ProofHelper(const TUrl& url, Int_t verbose)
97 : Helper(url, verbose),
101 fOptions.Add("workers", "N[x]", "Number of workers to use", "0");
102 fOptions.Add("dsname", "NAME", "Make output dataset", "");
103 fOptions.Add("par", "tasks|all", "Use par files", "tasks");
104 fOptions.Add("mode", "default|rec|sim", "AliROOT mode", "default");
105 fOptions.Add("storage", "URL", "Location for external storage", "");
107 if (!fUrl.GetUser() || fUrl.GetUser()[0] == '\0')
108 fUrl.SetUser(gSystem->GetUserInfo()->fUser);
110 virtual ~ProofHelper() {}
112 * Load a library/PAR/script
115 * @param par If true, upload & enable PAR
116 * @param slaves If true, also load on slaves
118 * @return true on success
120 virtual Bool_t LoadLibrary(const TString& name,
124 Int_t ret = gSystem->Load(MakeLibraryName(name));
125 if (ret < 0) return false;
126 if (slaves) fExtraLibs.Append(Form(":%s", name.Data()));
129 if (!ParUtilities::Find(name)) {
130 Error("ProofHelper::LoadLibrary", "Failed to find PAR file %s",
134 if (!ParUtilities::Build(name)) {
135 Error("ProofHelper::LoadLibrary", "Failed to build PAR file %s",
139 if (gProof->UploadPackage(name.Data(), TProof::kRemoveOld) < 0) {
140 Error("ProofHelper::LoadLibrary", "Failed to upload PAR file %s",
144 fExtraPars.Append(Form(":%s", name.Data()));
149 * Load a source file, and compile it
151 * @param name Name of the source file
153 * @return true on success
155 virtual Bool_t LoadSource(const TString& name)
157 if (!Helper::LoadSource(name)) return false;
158 fExtraSrcs.Append(Form(":%s", gSystem->BaseName(name.Data())));
162 * Set-up to load the AliROOT libraries
164 * @param par Whether to use PAR files
166 * @return true on success
168 virtual Bool_t LoadAliROOT()
170 if (!gSystem->Getenv("ALICE_ROOT")) {
171 Error("ProofHelper::LoadAliROOT", "Local AliROOT not available");
175 Bool_t tmp = fUsePars;
176 fUsePars = fBasePars;
177 if (!LoadLibrary("STEERBase")) return false;
178 if (!LoadLibrary("ESD")) return false;
179 if (!LoadLibrary("AOD")) return false;
180 if (!LoadLibrary("ANALYSIS")) return false;
181 if (!LoadLibrary("OADB")) return false;
182 if (!LoadLibrary("ANALYSISalice")) return false;
185 return CreateAliROOTPar();
188 * Get the name of the AliROOT par file to use
192 virtual const char* AliROOTParName() const
197 * Create an AliROOT par file from the executing AliROOT. This PAR
198 * file basically uses the environment of the client - that is, we
199 * assume that the used AliROOT is accessible on the slaves - e.g.,
202 * Note, the SETUP.C script take one argument - a TList of TNamed
203 * parameters. Parameters processed are
205 * - ALIROOT_MODE=[default,aliroot,rec,sim,train]
206 * - default: Load base analysis libraries
207 * - aliroot: Load $ALICE_ROOT/macros/loadlibs.C
208 * - rec: Load $ALICE_ROOT/macros/loadlibsrec.C
209 * - sim: Load $ALICE_ROOT/macros/loadlibssim.C
210 * - ALIROOT_EXTRA_LIBS Colon separated list of additional (Ali)ROOT
211 * libraries to load on the slaves.
213 * The generated PAR file is uploaded but not enabled until we have
214 * populated fExtraLibs. The enabling takes place at the end of the
217 * @return true on success, false otherwise. */
218 virtual Bool_t CreateAliROOTPar()
220 if (fBasePars) return true;
222 TString parName(AliROOTParName());
223 // Set-up directories
224 if (gSystem->MakeDirectory(parName) < 0) {
225 Error("ProofHelper::CreateAliROOTPar", "Could not make directory '%s'",
230 if (gSystem->MakeDirectory(Form("%s/PROOF-INF", parName.Data()))) {
231 Error("ProofHelper::CreateAliROOTPar",
232 "Could not make directory %s/PROOF-INF",
237 std::ofstream b(Form("%s/PROOF-INF/BUILD.sh",parName.Data()));
239 Error("ProofHelper::CreateAliROOTPar",
240 "Failed to make BUILD.sh shell script");
244 << "# echo Nothing to do\n"
248 gSystem->Exec(Form("chmod a+x %s/PROOF-INF/BUILD.sh",parName.Data()));
250 std::ofstream s(Form("%s/PROOF-INF/SETUP.C", parName.Data()));
252 Error("ProofHelper::CreateAliROOTPar",
253 "Failed to make SETUP.C ROOT script");
256 s << "void SETUP(TList* opts) {\n"
257 << " gSystem->Setenv(\"ALICE\",\""
258 << gSystem->Getenv("ALICE") << "\");\n"
259 << " gSystem->Setenv(\"ALICE_ROOT\",\""
260 << gSystem->Getenv("ALICE_ROOT") << "\");\n"
261 << " gSystem->Setenv(\"ALICE_TARGET\",\""
262 << gSystem->Getenv("ALICE_TARGET") << "\");\n"
263 << " gSystem->AddDynamicPath("
264 << "\"$(ALICE_ROOT)/lib/tgt_$(ALICE_TARGET)\");\n";
265 if (gSystem->Getenv("OADB_PATH"))
266 s << " gSystem->Setenv(\"OADB_PATH\",\""
267 << gSystem->Getenv("OADB_PATH") << "\");\n";
269 << " // Info(\"SETUP\",\"Loading ROOT libraries\");\n"
270 << " gSystem->Load(\"libTree\");\n"
271 << " gSystem->Load(\"libGeom\");\n"
272 << " gSystem->Load(\"libVMC\");\n"
273 << " gSystem->Load(\"libPhysics\");\n"
274 << " gSystem->Load(\"libMinuit\");\n"
276 s << " // Info(\"SETUP\",\"Parameter list:\");\n"
277 << " if (!opts) return;\n"
278 << " //opts->ls();\n"
280 s << " TObject* par = opts->FindObject(\"ALIROOT_MODE\");\n"
282 << " // Info(\"SETUP\",\"ALIROOT mode: %s\", par->GetTitle());\n"
283 << " TString mode(par->GetTitle());\n"
284 << " if (mode.EqualTo(\"default\",TString::kIgnoreCase)) {\n"
285 << " gSystem->Load(\"libSTEERBase\");\n"
286 << " gSystem->Load(\"libESD\");\n"
287 << " gSystem->Load(\"libAOD\");\n"
288 << " gSystem->Load(\"libANALYSIS\");\n"
289 << " gSystem->Load(\"libOADB\");\n"
290 << " gSystem->Load(\"libANALYSISalice\");\n"
292 << " else if (mode.EqualTo(\"aliroot\",TString::kIgnoreCase)) \n"
293 << " gROOT->Macro(\"$ALICE_ROOT/macros/loadlibs.C\");\n"
294 << " else if (mode.EqualTo(\"rec\",TString::kIgnoreCase)) \n"
295 << " gROOT->Macro(\"$ALICE_ROOT/macros/loadlibsrec.C\");\n"
296 << " else if (mode.EqualTo(\"sim\",TString::kIgnoreCase)) \n"
297 << " gROOT->Macro(\"$ALICE_ROOT/macros/loadlibssim.C\");\n"
298 << " else if (mode.EqualTo(\"train\",TString::kIgnoreCase)) \n"
299 << " gROOT->Macro(\"$ALICE_ROOT/macros/loadlibstrain.C\");\n"
300 << " else if (mode.EqualTo(\"custom\",TString::kIgnoreCase)) \n"
301 << " gROOT->Macro(\"$ALICE_ROOT/macros/loadlibstrain.C\");\n"
304 s << " par = opts->FindObject(\"ALIROOT_EXTRA_LIBS\");\n"
306 << " Info(\"SETUP\",\"Libaries to load: %s\n\",par->GetTitle());\n"
307 << " TString tit(par->GetTitle());\n"
308 << " TObjArray* tokens = tit.Tokenize(\":\");\n"
309 << " TObject* lib = 0;\n"
310 << " TIter next(tokens);\n"
311 << " while ((lib = next())) {\n"
312 << " TString libName(lib->GetName());\n"
313 << " if (!libName.BeginsWith(\"lib\")) libName.Prepend(\"lib\");\n"
314 << " // Info(\"SETUP\",\"Loading %s ...\",libName.Data());\n"
315 << " gSystem->Load(Form(\"lib%s\",lib->GetName()));\n"
322 Int_t ret = gSystem->Exec(Form("tar -czf %s.par %s",
323 parName.Data(), parName.Data()));
325 Error("ProofHelper::CreateAliROOTPar", "Failed to pack up PAR files");
329 ret = gProof->UploadPackage(Form("./%s.par", parName.Data()),
332 Error("ProofHelper::CreateAliROOTPar",
333 "Failed to upload the AliROOT PAR file");
336 // Note, the PAR isn't enabled until much later when we've
337 // collected all the needed libraries in fExtraLibs
341 * Get the mode identifier
343 * @return Always kProof
345 virtual UShort_t Mode() const { return kProof; }
347 * Get the mode string used for AliAnalysisManager::StartAnalysis
349 virtual const char* ModeString() const { return "proof"; }
351 * Set-up done before task set-ups
353 * @return true on success
355 virtual Bool_t PreSetup()
357 // --- Set prefered GSI method ---------------------------------
358 gEnv->SetValue("XSec.GSI.DelegProxy", "2");
360 // --- Add ALICE_ROOT directory to search path for packages ----
361 Info("ProofHelper::PreSetup", "Set location of packages");
362 gEnv->SetValue("Proof.GlobalPackageDirs",
364 gEnv->GetValue("Proof.GlobalPackageDirs", "."),
365 gSystem->Getenv("ALICE_ROOT")));
367 // --- PAR parameters --------------------------------------------
368 fUsePars = fOptions.Has("par");
369 fBasePars = (fUsePars &&
370 fOptions.Get("par").EqualTo("all",TString::kIgnoreCase));
372 // --- Connect to the cluster ------------------------------------
374 connect.SetAnchor("");
376 connect.SetOptions("");
378 if (fOptions.Has("workers"))
379 opts.Append(Form("workers=%s", fOptions.Get("workers").Data()));
381 Info("ProofHelper::PreSetup", "Connecting to %s with %soptions %s",
383 opts.IsNull() ? "no " : "",
385 TString proto(connect.GetProtocol());
386 if (proto.BeginsWith("lite") && fOptions.Has("workers"))
389 TProof::Open(connect.GetUrl(), opts);
390 // TProof::Open(connect.GetHost(), opts);
392 Error("ProofHelper::PreSetup", "Failed to open Proof connection %s",
396 Info("ProofHelper::PreSetup", "Using progress dialog=%d",
397 gProof->TestBit(TProof::kUseProgressDialog));
401 * Set-up done after the task set-ups
403 * @return true on success
405 virtual Bool_t PostSetup()
407 AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
409 Error("ProofHelper::PostSetup", "No analysis manager defined");
413 // --- Check for output ------------------------------------------
414 if (fOptions.Has("dsname"))
415 OutputUtilities::RegisterDataset(fOptions.Get("dsname"));
416 if (fOptions.Has("storage"))
417 OutputUtilities::RegisterStorage(fOptions.Get("storage"));
419 // --- If we are not using PARs for Base, enable special PAR -----
421 TString tmp(fExtraLibs.Strip(TString::kBoth,':'));
422 TList* params = new TList;
423 params->SetOwner(true);
424 params->Add(new TNamed("ALIROOT_EXTRA_LIBS", tmp.Data()));
425 if (fOptions.Has("mode"))
426 params->Add(new TNamed("ALIROOT_MODE", fOptions.Get("mode").Data()));
428 params->Add(new TNamed("ALIROOT_MODE", "default"));
429 Int_t ret = gProof->EnablePackage(AliROOTParName(), params, true);
431 Error("ProofHelper::EnableAliROOT", "Failed to enable AliROOT PAR %s",
437 // --- Load par files --------------------------------------------
438 TString tmp = fExtraPars.Strip(TString::kBoth,':');
439 TObjArray* pars = tmp.Tokenize(":");
442 while ((obj = next())) {
443 Int_t ret = gProof->EnablePackage(obj->GetName());
445 Error("ProofHelper::PostSetup", "Failed to enable PAR %s",
451 // --- Load extra sources ----------------------------------------
452 TString tmp2 = fExtraSrcs.Strip(TString::kBoth, ':');
453 TObjArray* srcs = tmp2.Tokenize(":");
455 while ((obj = next())) {
456 Int_t ret = gProof->Load(Form("%s++g", obj->GetName()), true);
458 Error("ProofHelper::PostSetup", "Failed to compile %s", obj->GetName());
467 * @param nEvents Number of events to analyse
469 * @return The return value of AliAnalysisManager::StartAnalysis
471 virtual Long64_t Run(Long64_t nEvents=-1)
473 AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
474 gProof->SetLogLevel(TMath::Max(fVerbose-2,0),
475 /* TProofDebug::kPacketizer| */
477 /* TProofDebug::kSelector|
478 TProofDebug::kOutput|
480 TProofDebug::kGlobal|*/
481 TProofDebug::kPackage);
482 TString dsName(fUrl.GetFile());
483 // if (fUrl.GetAnchor() && fUrl.GetAnchor()[0] != '\0')
484 // dsName.Append(Form("#%s", fUrl.GetAnchor()));
485 Long64_t ret = mgr->StartAnalysis(fUrl.GetProtocol(), dsName, nEvents);
488 TProof::Mgr(fUrl.GetUrl())->GetSessionLogs()->Save("*","proof.log");
492 * Print information to standard output
496 virtual void Print(Option_t* option="") const
498 Helper::Print(option);
499 std::cout << std::boolalpha
500 << " --- Other settings -------\n"
501 << " Extra libraries : " << fExtraLibs << "\n"
502 << " Extra PARs : " << fExtraPars << "\n"
503 << " Extra sources : " << fExtraSrcs << "\n"
504 << " Use PARs of tasks: " << fUsePars << "\n"
505 << " Use PARs of base : " << fBasePars
506 << std::noboolalpha << std::endl;
511 * @return Path to output - possibly a data set
513 virtual TString OutputPath() const
516 if (fOptions.Has("dsname")) {
517 ret = Form("/%s/%s/", gProof->GetGroup(), gProof->GetUser());
518 ret.Append(OutputUtilities::RegisteredDataset());
523 * @return URL help string
525 virtual const Char_t* UrlHelp() const
527 return "proof://<host>[:<port>]/[<dataset>|<path>][?<options>][#<treeName>]";
530 * @return Short description
532 virtual const char* Desc() const { return "PROOF"; }