2 * @defgroup pwglf_forward_trains_helper Analysis helpers
4 * @ingroup pwglf_forward_trains
10 * @author Christian Holm Christensen <cholm@master.hehi.nbi.dk>
11 * @date Tue Oct 16 19:00:17 2012
13 * @brief Base class for analysis helpers
15 * @ingroup pwglf_forward_trains_helper
18 #ifndef TRAIN_HELPER_C
19 #define TRAIN_HELPER_C
25 # include <TObjString.h>
29 # include <TObjArray.h>
30 # include <AliAnalysisManager.h>
41 * Helper class to set-up an analysis using a train
43 * @ingroup pwglf_forward_trains_helper
64 Helper(const Helper& o)
65 : fUrl(o.fUrl), fOptions(o.fOptions), fVerbose(o.fVerbose)
67 Helper& operator=(const Helper&) { return *this; }
69 * Create a helper object.
71 * @param url Url describing the job.
76 * local://<path>[#<treeName>][?[recursive[&]]]
79 * <path> can be a single ROOT file, a text file with one
80 * entry per file to add to the chain, or a directory containing the
81 * files. If <path> does not start with a '/' then it is
82 * interpreted as a relative path.
87 * alien:///<path>#<pattern>
95 * lite:///<path>[?[recursive[&]][workers=<n>]][#treeName]
96 * proof:///<path>[?[recursive[&]][workers=<n>]][#treeName]
100 * proof://<host>/<dsname>[?[workers=<n>[&]][dsname[=<outname>]]][#treeName]
103 * Note, if <host> is recognised as an Alice Analysis
104 * Facility, then the Grid handler (AliAnalysisAlien) is used unless
105 * the option <tt>plain</tt> was given.
107 * @return Newly allocated helper or null
109 static Helper* Create(const TUrl& url, Int_t verbose=0);
113 * @param name Name of library
114 * @param slave If true also load on slaves
116 * @return true on success, false otherwise
118 virtual Bool_t LoadLibrary(const TString& name,
119 Bool_t slave=true) = 0;
121 * Load a source file, and compile it
123 * @param name Name of the source file
125 * @return true on success
127 virtual Bool_t LoadSource(const TString& name)
129 if (!AuxFile(name)) return false;
130 TString base(gSystem->BaseName(name));
131 gROOT->LoadMacro(Form("%s++g", base.Data()));
135 * Load auxillary file - not compiled or sourced. Just copied to
138 * @param name Extra file name
140 * @return true on success
142 virtual Bool_t LoadAux(const TString& name)
144 if (!AuxFile(name)) return false;
149 * Load needed ROOT libaries
151 virtual Bool_t LoadROOT()
153 if (gSystem->Load("libTree.so") < 0) return false;
154 if (gSystem->Load("libGeom.so") < 0) return false;
155 if (gSystem->Load("libVMC.so") < 0) return false;
156 if (gSystem->Load("libPhysics.so") < 0) return false;
157 if (gSystem->Load("libMinuit.so") < 0) return false;
161 * Set-up to load the AliROOT libraries
163 * @param par Whether to use PAR files
165 * @return true on success
167 virtual Bool_t LoadAliROOT()
169 if (!gSystem->Getenv("ALICE_ROOT")) {
170 Error("Helper::LoadAliROOT", "Local AliROOT not available");
173 if (!LoadLibrary("STEERBase")) return false;
174 if (!LoadLibrary("ESD")) return false;
175 if (!LoadLibrary("AOD")) return false;
176 if (!LoadLibrary("ANALYSIS")) return false;
177 if (!LoadLibrary("OADB")) return false;
178 if (!LoadLibrary("ANALYSISalice")) return false;
182 * Get the execution mode
184 * @return Execution mode set in set-up URL
186 virtual UShort_t Mode() const = 0;
188 * Get the operation - this only makes sense for Grid jobs
190 * @return Operation type
192 virtual UShort_t Operation() const { return kFull; }
194 * Get the input data type
196 * @return Input data type
198 virtual Short_t InputType() const
200 UShort_t ret = DeduceType(fUrl.GetAnchor());
201 if (ret != kUser) return ret;
203 if (fOptions.Has("pattern")) ret = DeduceType(fOptions.Get("pattern"));
204 if (ret != kUser) return ret;
206 ret = DeduceType(fUrl.GetFile());
210 * The file part of tehe output URL - overwritten by derived classes.
213 * @return File part of output URL
215 virtual TString OutputPath() const { return ""; }
217 * Get the location of the output data. Use ful to define second pass
223 virtual TString OutputLocation() const
225 AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
226 if (!mgr || !mgr->GetOutputEventHandler()) return "";
228 TString path(OutputPath());
230 path = gSystem->WorkingDirectory();
236 u.SetAnchor("aodTree");
237 TString opt(u.GetOptions());
238 opt.ReplaceAll("AliESDs", "AliAOD");
244 * Set-up done before task setup
246 * @return true on success
248 virtual Bool_t PreSetup() = 0;
250 * Set-up done after task setup
252 * @return true on success
254 virtual Bool_t PostSetup() = 0;
258 * @param nEvents Number of events to analyse
260 * @return The return value of AliAnalysisManager::StartAnalysis
262 virtual Long64_t Run(Long64_t nEvents=-1) = 0;
264 * Print information to standard output
270 virtual void Print(Option_t* ="") const
272 std::cout << "Url: " << fUrl.GetUrl() << std::endl;
273 fOptions.Show(std::cout);
276 * @return URL help string
278 virtual const Char_t* UrlHelp() const = 0;
280 * @return Short description string
282 virtual const char* Desc() const = 0;
288 const TUrl& Url() const { return fUrl; }
290 * Get the list of options
292 * @return Reference to list of options
294 const OptionList& Options() const { return fOptions; }
296 * Create an instance of a helper class
298 static Helper* CreateObject(const TString& cl,
302 if (verbose > 3) gSystem->RedirectOutput("/dev/null","w");
303 if (cl.Contains("proof", TString::kIgnoreCase) ||
304 cl.Contains("lite", TString::kIgnoreCase) ||
305 cl.Contains("aaf", TString::kIgnoreCase)) {
306 gSystem->Load("libProof");
307 gSystem->Load("libProofPlayer");
309 // Always recompile and with debug symbols
310 gROOT->LoadMacro(Form("%s.C++g",cl.Data()));
311 Long_t ptr = gROOT->ProcessLine(Form("new %s(\"%s\", %d);",
312 cl.Data(), url.GetUrl(), verbose));
313 if (verbose > 3) gSystem->RedirectOutput(0);
315 Warning("Helper::CreateObject", "Failed to instantize a %s", cl.Data());
318 Helper* h = reinterpret_cast<Helper*>(ptr);
322 * Show help on URL using the interpreter
324 * @param cl Helper class
326 static void ShowUrlHelp(const TString& cl)
328 Helper* h = CreateObject(cl, "", true);
331 std::cout << " " << h->UrlHelp() << std::endl;
334 * Show help on URL and options using the interpreter
336 * @param cl Helper class
338 static void ShowFullHelp(const TString& cl)
340 Helper* h = CreateObject(cl, "", true);
343 std::cout << h->Desc() << ":\n"
344 << "==============\n"
345 << " " << h->UrlHelp() << "\n\n"
346 << "Options: " << std::endl;
347 h->Options().Help(std::cout);
348 std::cout << std::endl;
354 * @param url Set-up URL
355 * @param opts Pre-parsed options
357 Helper(const TUrl& url, Int_t verbose)
358 : fUrl(url), fOptions(), fVerbose(verbose)
362 virtual Bool_t ParseOptions()
364 return fOptions.Parse(fUrl.GetOptions(), "&");
373 * Normalize a library name
379 const TString& MakeLibraryName(const TString& name)
381 static TString libName;
385 if (!libName.BeginsWith("lib")) {
386 // Check if the library corresponds to a compiled macro
387 if (!gSystem->AccessPathName(Form("%s_C.so", libName.Data())))
388 libName.Append("_C");
389 else if (!gSystem->AccessPathName(Form("../%s_C.so", libName.Data())))
390 libName = Form("../%s_C", libName.Data());
392 libName = Form("lib%s", libName.Data());
394 if (!libName.EndsWith(".so")) libName.Append(".so");
399 * Link an auxilary file to working directory
401 * @param name Name of the file
403 * @return true on success
405 virtual Bool_t AuxFile(const TString& name)
407 TString path(gSystem->ExpandPathName(name.Data()));
408 // If not absolute, prepend up-one
409 if (!path.BeginsWith("/")) path.Prepend("../");
410 if (gSystem->AccessPathName(path.Data())) {
411 // File not accessible
412 Warning("Helper::AuxFile", "File %s not accessible", path.Data());
415 TString base(gSystem->BaseName(path.Data()));
416 if (gSystem->AccessPathName(base.Data()) == 0) {
417 // File or link exists - remove it
418 if (gSystem->Unlink(base) != 0) {
419 Error("Helper::AuxFile", "Failed to remove old %s", base.Data());
423 gSystem->Exec(Form("ln -s %s .", path.Data()));
427 * Deduce the top of job from a string
433 static UShort_t DeduceType(const TString& str)
435 if (str.IsNull()) return kUser;
436 if (str.Contains("aod", TString::kIgnoreCase)) return kAOD;
437 if (str.Contains("esd", TString::kIgnoreCase)) return kESD;
440 // --- Data members ------------------------------------------------
441 TUrl fUrl; // The URI
449 // ===================================================================
451 Helper::Create(const TUrl& url, Int_t verbose)
453 if (!url.IsValid()) {
454 Warning("Helper::Create", "URL is invalid");
458 TString prot(url.GetProtocol());
462 TString opts(tmp.GetOptions());
463 TString host(url.GetHost());
465 if (prot.EqualTo("alien")) {
466 // Create an AliEn helper
469 else if (prot.EqualTo("local")) {
470 // Create Lite helper
473 else if (prot.EqualTo("proof")) {
474 // Create a Proof helper
477 else if (host.BeginsWith("alice-caf")) {
479 cl = opts.Contains("plugin") ? "AAFPluginHelper" : "AAFHelper";
484 else if (prot.EqualTo("lite")) {
485 // Create a Proof helper
488 else if (prot.EqualTo("help")) {
489 // Special HELP protocol
490 if (host.Contains("options")) {
491 std::cout << "Possible URL types and options are:" << std::endl;
492 ShowFullHelp("LocalHelper");
493 ShowFullHelp("ProofHelper");
494 ShowFullHelp("LiteHelper");
495 ShowFullHelp("AAFHelper");
496 ShowFullHelp("AAFPluginHelper");
497 ShowFullHelp("GridHelper");
500 std::cout << "Possible URL types are:" << std::endl;
501 ShowUrlHelp("LocalHelper");
502 ShowUrlHelp("ProofHelper");
503 ShowUrlHelp("LiteHelper");
504 ShowUrlHelp("AAFHelper");
505 ShowUrlHelp("AAFPluginHelper");
506 ShowUrlHelp("GridHelper");
509 // --- Check if we got a scheme ------------------------------------
511 Error("Helper::Create", "Unknown scheme: %s", prot.Data());
515 // --- Use interpreter to make our object --------------------------
516 Helper* helper = CreateObject(cl, url, verbose);
518 Error("Helper::Create", "Failed to make object of class %s", cl.Data());
522 // --- Parse options -----------------------------------------------
523 if (!helper->ParseOptions()) {