AliPhysics  5403132 (5403132)
Railway.C
Go to the documentation of this file.
1 
18 #ifndef TRAIN_HELPER_C
19 #define TRAIN_HELPER_C
20 #ifndef __CINT__
21 # include "Option.C"
22 # include "ChainBuilder.C"
23 # include <TUrl.h>
24 # include <TString.h>
25 # include <TMap.h>
26 # include <TObjString.h>
27 # include <TSystem.h>
28 # include <TROOT.h>
29 # include <TError.h>
30 # include <TObjArray.h>
31 # include <TFile.h>
32 # include <TChain.h>
33 # include <AliAnalysisManager.h>
34 # include <iostream>
35 #else
36 class TString;
37 class TUrl;
38 class TMap;
39 class Option;
40 class OptionList;
41 class TChain;
42 #endif
43 
49 struct Railway
50 {
51  enum EMode {
55  };
56  enum EOperation {
62  };
63  enum EInput {
64  kESD,
65  kAOD,
67  };
68  Railway(const Railway& o)
70  {}
71  Railway& operator=(const Railway&) { return *this; }
118  static Railway* Create(const TUrl& url, Int_t verbose=0);
122  static Railway* CreateObject(const TString& cl,
123  const TUrl& url,
124  Int_t verbose=0)
125  {
126  if (verbose < 3) gSystem->RedirectOutput("/dev/null","w");
127  if (cl.Contains("proof", TString::kIgnoreCase) ||
128  cl.Contains("vaf", TString::kIgnoreCase) ||
129  cl.Contains("lite", TString::kIgnoreCase) ||
130  cl.Contains("aaf", TString::kIgnoreCase)) {
131  gSystem->Load("libProof");
132  gSystem->Load("libProofPlayer");
133  }
134  // (Always) recompile and with debug symbols
135  gROOT->LoadMacro(Form("%s.C++g",cl.Data()));
136  Long_t ptr = gROOT->ProcessLine(Form("new %s(\"%s\", %d);",
137  cl.Data(), url.GetUrl(), verbose));
138  if (verbose < 3) gSystem->RedirectOutput(0);
139  if (!ptr) {
140  Warning("Railway::CreateObject", "Failed to instantize a %s", cl.Data());
141  return 0;
142  }
143  Railway* h = reinterpret_cast<Railway*>(ptr);
144  return h;
145  }
151  static void ShowUrlHelp(const TString& cl)
152  {
153  Railway* h = CreateObject(cl, "", true);
154  if (!h) return;
155 
156  std::cout << " " << h->UrlHelp() << std::endl;
157  }
163  static void ShowFullHelp(const TString& cl)
164  {
165  Railway* h = CreateObject(cl, "", true);
166  if (!h) return;
167 
168  std::cout << h->Desc() << ":\n"
169  << "==============\n"
170  << " " << h->UrlHelp() << "\n\n"
171  << "Options: " << std::endl;
172  h->Options().Help(std::cout);
173  std::cout << std::endl;
174  }
175  /* @} */
193  virtual void UsePar(Bool_t& use);
201  virtual Bool_t AddIncludePath(const TString& path)
202  {
203  TString p(path);
204  if (!p.BeginsWith("-")) {
205  // IF the path does not begin with a -, we assume its a path
206  p.Prepend("-I");
207  }
208  gSystem->AddIncludePath(p);
209  return true;
210  }
220  virtual Bool_t LoadLibrary(const TString& name,
221  Bool_t slave=true,
222  Bool_t forcePar=false) = 0;
231  virtual Bool_t LoadSource(const TString& name, bool copy=false)
232  {
233  TString local = name;
234  if (!AuxFile(local, copy)) {
235  Warning("LoadSource", "Failed to add aux source file %s", name.Data());
236  return false;
237  }
238  // TString base(gSystem->BaseName(name));
239  Info("LoadSource", "Building %s", local.Data());
240  // gROOT->ProcessLine("gSystem->RedirectOutput(\"build.log\",\"a\");");
241  gROOT->LoadMacro(Form("%s+g", local.Data()));
242  // gROOT->ProcessLine("gSystem->RedirectOutput(0);");
243  Info("LoadSource", "End of loading source");
244  return true;
245  }
255  virtual Bool_t LoadAux(const TString& name, Bool_t copy=false)
256  {
257  TString local = name;
258  if (!AuxFile(local, copy)) return false;
259  return true;
260  }
261 
265  virtual Bool_t LoadROOT()
266  {
267  if (gSystem->Load("libTree") < 0) return false;
268  if (gSystem->Load("libGeom") < 0) return false;
269  if (gSystem->Load("libVMC") < 0) return false;
270  if (gSystem->Load("libPhysics") < 0) return false;
271  if (gSystem->Load("libMinuit") < 0) return false;
272  return true;
273  }
279  virtual Bool_t LoadAliROOT()
280  {
281  if (!gSystem->Getenv("ALICE_ROOT")) {
282  Error("Railway::LoadAliROOT", "Local AliROOT not available");
283  return false;
284  }
285  if (!LoadLibrary("STEERBase")) return false;
286  if (!LoadLibrary("ESD")) return false;
287  if (!LoadLibrary("AOD")) return false;
288  if (!LoadLibrary("ANALYSIS")) return false;
289  if (!LoadLibrary("OADB")) return false;
290  if (!LoadLibrary("ANALYSISalice")) return false;
291  return true;
292  }
299  {
300  if (!gSystem->Getenv("ALICE_PHYSICS")) {
301  Error("Railway::LoadAliPhysics", "Local AliPhysics not available");
302  return false;
303  }
304  if (!LoadLibrary("OADB")) return false;
305  return true;
306  }
307  /* @} */
317  virtual UShort_t Mode() const = 0;
321  virtual const char* ModeString() const { return "unknown"; }
327  virtual UShort_t Operation() const { return kFull; }
328  /* @} */
338  virtual Short_t InputType() const
339  {
340  UShort_t ret = DeduceType(fUrl.GetAnchor());
341  if (ret != kUser) return ret;
342 
343  if (fOptions.Has("pattern")) ret = DeduceType(fOptions.Get("pattern"));
344  if (ret != kUser) return ret;
345 
346  ret = DeduceType(fUrl.GetFile());
347  return ret;
348  }
354  virtual Bool_t IsMC() const { return fOptions.Has("mc"); }
361  virtual TString OutputPath() const { return ""; }
369  virtual TString OutputLocation() const
370  {
371  AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
372  if (!mgr || !mgr->GetOutputEventHandler()) return "";
373 
374  TString path(OutputPath());
375  if (path.IsNull()) {
376  path = gSystem->WorkingDirectory();
377  // mgr->GetName();
378  }
379 
380  TUrl u(fUrl);
381  u.SetFile(path);
382  u.SetAnchor("aodTree");
383  TString opt(u.GetOptions());
384  // if (opt.Contains("AliESDs"))
385  opt.ReplaceAll("AliESDs", "AliAOD");
386  // else opt.Append("&pattern=AliAOD*.root");
387  u.SetOptions(opt);
388 
389  return u.GetUrl();
390  }
391  /* @} */
401  virtual Bool_t PreSetup() = 0;
407  virtual Bool_t PostSetup() = 0;
415  virtual Long64_t Run(Long64_t nEvents=-1) = 0;
419  virtual Bool_t AddMonitor(const TString&) { return true; }
420  /* @} */
430  virtual void Print(Option_t* ="") const
431  {
432  std::cout << "Url: " << fUrl.GetUrl() << std::endl;
433  fOptions.Show(std::cout);
434  }
438  virtual const Char_t* UrlHelp() const = 0;
442  virtual const char* Desc() const = 0;
448  const TUrl& Url() const { return fUrl; }
454  const OptionList& Options() const { return fOptions; }
460  virtual void AuxSave(const TString& /*escaped*/,
461  Bool_t /*asShellScript*/) {}
462 protected:
469  Railway(const TUrl& url, Int_t verbose)
470  : fUrl(url), fOptions(), fVerbose(verbose)
471  {
472  fOptions.Add("mc", "Assume simulation input");
473  Printf("URL is %s", fUrl.GetUrl());
474  }
475 
477  {
478  std::cout << "Url options: \"" << fUrl.GetOptions() << "\"" << std::endl;
479  return fOptions.Parse(fUrl.GetOptions(), "&");
480  }
484  virtual ~Railway()
485  {
486  }
498  const TString& MakeLibraryName(const TString& name)
499  {
500  static TString libName;
501 
502  libName = name;
503 
504  if (!libName.BeginsWith("lib")) {
505  // Check if the library corresponds to a compiled macro
506  if (!gSystem->AccessPathName(Form("%s_C.so", libName.Data())))
507  libName.Append("_C");
508  else if (!gSystem->AccessPathName(Form("../%s_C.so", libName.Data())))
509  libName = Form("../%s_C", libName.Data());
510  else
511  libName = Form("lib%s", libName.Data());
512  }
513  if (!libName.EndsWith(".so")) libName.Append(".so");
514 
515  return libName;
516  }
525  virtual Bool_t AuxFile(TString& name, bool copy=false)
526  {
527  TString path(gSystem->ExpandPathName(name.Data()));
528  TString search(gSystem->GetIncludePath());
529  search.ReplaceAll("-I", ":");
530  search.ReplaceAll(" ", "");
531  search.Append(":");
532  search.Append(gROOT->GetMacroPath());
533  // If not absolute, prepend up-one
534  if (!path.BeginsWith("/")) search.Prepend("../:");
535 
536  // Remove any trailing AcLic instructions
537  if (path.EndsWith("+") || path.EndsWith("+g")) {
538  do {
539  char c = path[path.Length()-1];
540  if (c != '+' && c != 'g') break;
541  path.Remove(path.Length()-1);
542  } while (path.Length());
543  }
544 
545 #if 0
546  TObjArray* comps = search.Tokenize(":");
547  Printf("Searching for %s in ", path.Data());
548  TIter nextC(comps);
549  TObject* objC = 0;
550  while ((objC = nextC())) Printf(" %s", objC->GetName());
551  comps->Delete();
552 #endif
553  TString query = gSystem->Which(search, path);
554  // Printf("Got back %s", query.Data());
555 
556  if (gSystem->AccessPathName(query.Data())) {
557  // File not accessible
558  Warning("Railway::AuxFile", "File %s not accessible", path.Data());
559  return false;
560  }
561  TString base(gSystem->BaseName(query.Data()));
562  name = base;
563  if (gSystem->AccessPathName(base.Data()) == 0) {
564  // File or link exists - remove it
565  if (gSystem->Unlink(base) != 0) {
566  Error("Railway::AuxFile", "Failed to remove old %s", base.Data());
567  return false;
568  }
569  }
570  if (copy)
571  TFile::Cp(query, base);
572  else
573  gSystem->Exec(Form("ln -s %s .", query.Data()));
574  return true;
575  }
576  /* @} */
588  {
589  // --- Copy of URL -----------------------------------------------
590  TString su(fUrl.GetUrl());
591  if (su.BeginsWith("local", TString::kIgnoreCase))
592  su.Replace(0, 5, "file", 4);
593  TUrl url(su);
594 
595  // -- Check the source -------------------------------------------
596  TString src = url.GetFile();
597  if (src.IsNull()) {
598  Error("LocalChain", "No input source specified: %s (%s)",
599  url.GetUrl(), fUrl.GetUrl());
600  return 0;
601  }
602 
603  // --- Check possible pattern ------------------------------------
604  TString pattern = fOptions.AsString("pattern", "");
605  pattern.ReplaceAll("@", "#");
606  pattern.ReplaceAll(":", "#");
607 
608  // --- Get the tree name -----------------------------------------
609  TString treeName = url.GetAnchor();
610 
611  // --- Create flags for the chain builder ------------------------
612  UShort_t flags = 0;
613  if (fOptions.Has("mc") &&
614  AliAnalysisManager::GetAnalysisManager()
615  ->GetMCtruthEventHandler() != 0) flags |= ChainBuilder::kMC;
616  if (fOptions.Has("recursive")) flags |= ChainBuilder::kRecursive;
617  if (fOptions.Has("trackref")) flags |= ChainBuilder::kTrRef;
618  if (fOptions.Has("clean")) flags |= ChainBuilder::kClean;
619  if (fOptions.Has("scan")) flags |= ChainBuilder::kScan;
620  if (fVerbose > 5) flags |= ChainBuilder::kVerbose;
621 
622  // --- Check input -----------------------------------------------
623  UShort_t type = ChainBuilder::CheckSource(src, flags);
624  if (type == ChainBuilder::kInvalid) {
625  Error("LocalChain", "Cannot generate TChain from %s", src.Data());
626  return 0;
627  }
628 
629  // --- Create the chain ------------------------------------------
630  TChain* chain = ChainBuilder::Create(type, src, treeName, pattern, flags);
631  if (!chain) {
632  Error("LocalChain", "No chain defined "
633  "(url=%s, src=%s, treeName=%s, pattern=%s, flags=0x%x)",
634  url.GetUrl(), src.Data(), treeName.Data(), pattern.Data(), flags);
635  fOptions.Show(std::cout);
636  return 0;
637  }
638 
639  return chain;
640  }
641  /* @} */
653  static UShort_t DeduceType(const TString& str)
654  {
655  if (str.IsNull()) return kUser;
656  if (str.Contains("aod", TString::kIgnoreCase)) return kAOD;
657  if (str.Contains("esd", TString::kIgnoreCase)) return kESD;
658  return kUser;
659  }
660  /* @} */
661  // --- Data members ------------------------------------------------
662  TUrl fUrl; // The URI
665 };
666 
667 
668 
669 
670 // ===================================================================
672 Railway*
673 Railway::Create(const TUrl& url, Int_t verbose)
674 {
675  if (!url.IsValid()) {
676  Warning("Railway::Create", "URL is invalid");
677  return 0;
678  }
679 
680  TString prot(url.GetProtocol());
681  prot.ToLower();
682 
683  TUrl tmp(url);
684  TString opts(tmp.GetOptions());
685  TString host(url.GetHost());
686  TString cl = "";
687  if (prot.EqualTo("alien")) {
688  // Create an AliEn helper
689  cl = "GridRailway";
690  }
691  else if (prot.EqualTo("local")) {
692  // Create Lite helper
693  cl = "LocalRailway";
694  }
695  else if (prot.EqualTo("proof")) {
696  // Create a Proof helper
697  if (host.IsNull())
698  cl = "LiteRailway";
699  else if (host.BeginsWith("alice-caf")) {
700  // AAF
701  ::Warning("Railway::Create", "CAF has been decommissioned");
702  cl = opts.Contains("plugin") ? "AAFPluginRailway" : "AAFRailway";
703  }
704  else if (host.BeginsWith("alivaf")) {
705  // VAF
706  cl = "VAFRailway";
707  }
708  else
709  cl = "ProofRailway";
710  }
711  else if (prot.EqualTo("lite")) {
712  // Create a Proof helper
713  cl = "LiteRailway";
714  }
715  else if (prot.EqualTo("help")) {
716  // Special HELP protocol
717  if (host.Contains("options")) {
718  std::cout << "Possible URL types and options are:" << std::endl;
719  ShowFullHelp("LocalRailway");
720  ShowFullHelp("ProofRailway");
721  ShowFullHelp("LiteRailway");
722  ShowFullHelp("VAFRailway");
723  ShowFullHelp("AAFRailway");
724  ShowFullHelp("AAFPluginRailway");
725  ShowFullHelp("GridRailway");
726  return 0;
727  }
728  std::cout << "Possible URL types are:" << std::endl;
729  ShowUrlHelp("LocalRailway");
730  ShowUrlHelp("ProofRailway");
731  ShowUrlHelp("LiteRailway");
732  ShowUrlHelp("VAFRailway");
733  ShowUrlHelp("AAFRailway");
734  ShowUrlHelp("AAFPluginRailway");
735  ShowUrlHelp("GridRailway");
736  return 0;
737  }
738  // --- Check if we got a scheme ------------------------------------
739  if (cl.IsNull()) {
740  Error("Railway::Create", "Unknown scheme: %s", prot.Data());
741  return 0;
742  }
743 
744  // --- Use interpreter to make our object --------------------------
745  Railway* helper = CreateObject(cl, url, verbose);
746  if (!helper) {
747  Error("Railway::Create", "Failed to make object of class %s", cl.Data());
748  return 0;
749  }
750 
751  // --- Parse options -----------------------------------------------
752  if (!helper->ParseOptions()) {
753  Error("Railway::Create", "Failed to parse options: %s", url.GetOptions());
754  delete helper;
755  helper = 0;
756  }
757  Printf("URL of helper is %s", url.GetUrl());
758  return helper;
759 }
760 
761 #endif
virtual Bool_t ParseOptions()
Definition: Railway.C:476
void Help(std::ostream &o, const char *prefix=" ") const
Definition: Option.C:903
OptionList fOptions
Definition: Railway.C:663
const char * url
const TString & MakeLibraryName(const TString &name)
Definition: Railway.C:498
static UShort_t CheckSource(TString &src, UShort_t flags)
Definition: ChainBuilder.C:81
static Railway * Create(const TUrl &url, Int_t verbose=0)
Definition: Railway.C:673
long long Long64_t
Definition: External.C:43
virtual Bool_t LoadAliPhysics()
Definition: Railway.C:298
virtual Long64_t Run(Long64_t nEvents=-1)=0
TSystem * gSystem
char Char_t
Definition: External.C:18
virtual void Print(Option_t *="") const
Definition: Railway.C:430
virtual const Char_t * UrlHelp() const =0
Railway & operator=(const Railway &)
Definition: Railway.C:71
Int_t fVerbose
Definition: Railway.C:664
TCanvas * c
Definition: TestFitELoss.C:172
virtual void AuxSave(const TString &, Bool_t)
Definition: Railway.C:460
const char * AsString(const TString &name, const TString &def="") const
Definition: Option.C:687
virtual Bool_t LoadAliROOT()
Definition: Railway.C:279
static TChain * Create(const TUrl &url)
Definition: ChainBuilder.C:155
virtual const char * Desc() const =0
virtual Bool_t LoadROOT()
Definition: Railway.C:265
Definition: External.C:92
const TString & Get(const TString &name) const
Definition: Option.C:596
virtual Bool_t PreSetup()=0
static UShort_t DeduceType(const TString &str)
Definition: Railway.C:653
TChain * LocalChain()
Definition: Railway.C:587
static void ShowUrlHelp(const TString &cl)
Definition: Railway.C:151
EInput
Definition: Railway.C:63
virtual UShort_t Mode() const =0
Bool_t Has(const TString &name) const
Definition: Option.C:584
Bool_t Parse(const TString &tmp, const TString &delims)
Definition: Option.C:796
int Int_t
Definition: External.C:63
EOperation
Definition: Railway.C:56
Railway(const Railway &o)
Definition: Railway.C:68
static Railway * CreateObject(const TString &cl, const TUrl &url, Int_t verbose=0)
Definition: Railway.C:122
virtual Bool_t PostSetup()=0
const OptionList & Options() const
Definition: Railway.C:454
Definition: Option.C:68
virtual Bool_t IsMC() const
Definition: Railway.C:354
Double_t nEvents
plot quality messages
virtual Short_t InputType() const
Definition: Railway.C:338
TUrl fUrl
Definition: Railway.C:662
virtual Bool_t AuxFile(TString &name, bool copy=false)
Definition: Railway.C:525
virtual Bool_t AddMonitor(const TString &)
Definition: Railway.C:419
short Short_t
Definition: External.C:23
virtual Bool_t AddIncludePath(const TString &path)
Definition: Railway.C:201
virtual const char * ModeString() const
Definition: Railway.C:321
Option * Add(const TString &name, const TString &arg, const TString &desc, const TString &val="")
Definition: Option.C:421
const TUrl & Url() const
Definition: Railway.C:448
virtual TString OutputLocation() const
Definition: Railway.C:369
virtual Bool_t LoadAux(const TString &name, Bool_t copy=false)
Definition: Railway.C:255
virtual ~Railway()
Definition: Railway.C:484
Railway(const TUrl &url, Int_t verbose)
Definition: Railway.C:469
virtual TString OutputPath() const
Definition: Railway.C:361
unsigned short UShort_t
Definition: External.C:28
static void ShowFullHelp(const TString &cl)
Definition: Railway.C:163
virtual UShort_t Operation() const
Definition: Railway.C:327
const char Option_t
Definition: External.C:48
void Show(std::ostream &o, const char *prefix=" ") const
Definition: Option.C:928
virtual void UsePar(Bool_t &use)
Definition: Railway.C:671
bool Bool_t
Definition: External.C:53
Build a chain.
virtual Bool_t LoadLibrary(const TString &name, Bool_t slave=true, Bool_t forcePar=false)=0
virtual Bool_t LoadSource(const TString &name, bool copy=false)
Definition: Railway.C:231