]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/trains/trainMain.cxx
Can now specify URI in an optional way using --where, ...
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / trains / trainMain.cxx
1 /*
2   To compile, do 
3
4   rootcint -f OptionDict.C -c Option.C 
5   g++ `root-config --cflags --libs` \
6     -lVMC -lGeom -lMinuit -lXMLIO -lTree -lTreePlayer \
7     -I$ALICE_ROOT/include -L$ALICE_ROOT/lib/tgt_${ALICE_TARGET} \
8     -lSTEERBase -lESD -lAOD -lANALYSIS -lOADB -lANALYSISalice \
9     trainMain.cxx -o runTrain
10 */
11  
12 #include "AvailableSoftware.C" 
13 #include "ChainBuilder.C" 
14 #include "ParUtilities.C"
15 #include "OutputUtilities.C" 
16 #include "Option.C"
17 #include "Helper.C" 
18 #include "LocalHelper.C" 
19 #include "ProofHelper.C" 
20 #include "LiteHelper.C"
21 #include "AAFHelper.C" 
22 #include "PluginHelper.C"
23 #include "AAFPluginHelper.C" 
24 #include "GridHelper.C"
25 #include "TrainSetup.C"
26
27 #include <TGApplication.h>
28 #include <TROOT.h>
29 #include <TList.h>
30 #include <TObjString.h>
31 #include <TString.h>
32
33 #include <iostream>
34 #include <iomanip>
35
36 /** 
37  * Custom timer to do a deferred start after the application 
38  * has been started 
39  */
40 struct Deferred : public TTimer
41 {
42   Deferred(const TString& name, const TString& cls, 
43            const TCollection* opts)
44     : TTimer(1000, false), 
45       fName(name), 
46       fClass(cls), 
47       fOptions(opts)
48   {
49     Start(1000, true);
50   }
51   Deferred(const Deferred& o)
52     : TTimer(), 
53       fName(o.fName),
54       fClass(o.fClass),
55       fOptions(o.fOptions)
56   {}
57   Deferred& operator=(const Deferred& o) 
58   { 
59     if (&o == this) return *this;
60     fName    = o.fName;
61     fClass   = o.fClass;
62     fOptions = o.fOptions;
63     return *this; 
64   }
65   Bool_t Notify()
66   {
67     // gSystem->RemoveTimer(this);
68     Info("Notify", "Will run train setup: %s (%s)", 
69          fName.Data(), fClass.Data());
70     return TrainSetup::Main(fName, fClass, fOptions);
71   }
72   TString fName;
73   TString fClass;
74   const TCollection* fOptions;
75 };
76
77 /** 
78  * Append directory to header and script search path
79  * 
80  * @param dir Directory
81  * 
82  * @ingroup pwglf_forward_trains_run
83  */
84 void AppendPath(const char* dir)
85 {
86   gROOT->SetMacroPath(Form("%s:%s",gROOT->GetMacroPath(), dir));
87   gSystem->AddIncludePath(Form("-I%s", dir));
88 }
89 /** 
90  * Print a fake option description.  Used for options specific to this
91  * program.
92  * 
93  * @param o    Output stream 
94  * @param opt  Option (including meta argument)
95  * @param desc Option description.
96  * 
97  * @ingroup pwglf_forward_trains_run
98  */
99 void PrintFakeOption(std::ostream& o, const char* opt, const char* desc)
100 {
101   o << "  --" << std::left << std::setw(30) << opt << " " << desc << std::endl;
102 }
103
104 /** 
105  * Print usage information 
106  * 
107  * @param progname Program name 
108  * @param o        Output stream
109  * @param r        Optional runner. 
110  * 
111  * @ingroup pwglf_forward_trains_run
112  */
113 void Usage(const char* progname, std::ostream& o)
114 {
115   o << "Usage: " << progname 
116     << " --class=CLASS --name=NAME --url=URI [OPTIONS]\n\n"
117     << "PROGRAM OPTIONS:\n";
118   PrintFakeOption(o, "class=CLASS",       "Train class");
119   PrintFakeOption(o, "name=NAME",         "Name of train");
120   PrintFakeOption(o, "include=DIRECTORY", "Append dir to macro/header path");
121   PrintFakeOption(o, "batch",             "Batch mode");
122   PrintFakeOption(o, "url=URI",           "Execution URI");
123   o << "\nAlternatively to using --url=URI, one can use\n";
124   PrintFakeOption(o, "where=BASE_URI",    "Set protocol, user, host, "
125                   "and port URI");
126   PrintFakeOption(o, "file=FILE_OR_PATH", "File/path part of URI");
127   PrintFakeOption(o, "options=OPTIONS",   "Query options for URI");
128   PrintFakeOption(o, "anchor=ANCHOR",     "Query anchor for URI");
129
130 }
131
132 int
133 main(int argc, char** argv)
134 {
135   TList optList;
136   TString name;
137   TString cls;
138   TString where;
139   TString file;
140   TString opts;
141   TString anchor;
142   Bool_t  batch   = false;
143   Bool_t  help    = false;
144   Bool_t  urlSeen = false;
145
146   // --- Parse options -----------------------------------------------
147   for (int i = 1; i < argc; i++) { 
148     if (argv[i][0] == '-' && argv[i][1] == '-') { 
149       TString arg(argv[i]);
150       TString val("");
151       arg.ReplaceAll("\"'", "");
152       Int_t   eq = arg.Index("=");
153       if (eq != kNPOS) val = arg(eq+1, arg.Length()-eq-1);
154       if      (arg.BeginsWith("--class"))   cls  = val;
155       else if (arg.BeginsWith("--name"))    name = val;
156       else if (arg.BeginsWith("--include")) AppendPath(val);
157       else if (arg.BeginsWith("--batch"))   batch  = true;
158       else if (arg.BeginsWith("--help"))    help   = true;
159       else if (arg.BeginsWith("--where"))   where  = val;
160       else if (arg.BeginsWith("--file"))    file   = val;
161       else if (arg.BeginsWith("--opts"))    opts   = val;
162       else if (arg.BeginsWith("--anchor"))  anchor = val;
163       else {
164         if (arg.BeginsWith("--url")) urlSeen = true;
165         optList.Add(new TObjString(&(argv[i][2])));
166       }
167     }
168   }
169   // --- Initial check or URI/WHERE ----------------------------------
170   if (!where.IsNull()) {
171     if (urlSeen) {
172       Error("main", "option --url and --where mutually exclusive");
173       return 1;
174     }
175     TUrl u(where);
176     if (!file.IsNull())   u.SetFile(file);
177     if (!opts.IsNull())   u.SetOptions(opts);
178     if (!anchor.IsNull()) u.SetAnchor(anchor);
179     optList.Add(new TObjString(Form("url=%s", u.GetUrl())));
180   }
181
182   // --- check for help ----------------------------------------------
183   if (help && cls.IsNull()) {
184     if (cls.IsNull()) {
185       Usage(argv[0], std::cout);
186       return 0;
187     }
188     optList.Add(new TObjString("help"));
189   }
190
191   // --- Check name and class ----------------------------------------
192   if (name.IsNull()) { 
193     Error("main", "No name specified");
194     return 1;
195   }
196   if (cls.IsNull()) { 
197     Error("main", "No class specified");
198     return 1;
199   }
200
201   // --- Setup script path -------------------------------------------
202   const char* aliPath  = gSystem->ExpandPathName("$ALICE_ROOT");
203   const char* fwdPath  = gSystem->ExpandPathName("$ALICE_ROOT/PWGLF/FORWARD/");
204   AppendPath(aliPath);
205   AppendPath(Form("%s/include",   aliPath));
206   AppendPath(Form("%s/trains",    fwdPath));
207   AppendPath(Form("%s/analysis2", fwdPath));
208
209
210   // --- Set-up Application ------------------------------------------
211   TApplication* app = 0;
212   gROOT->SetBatch(true);
213   if (!batch) { 
214     gROOT->SetBatch(false);
215     app = new TGApplication("runTrain", 0, 0);
216     app->InitializeGraphics();
217   }
218
219   // --- run, possibly in a timer ------------------------------------
220   Bool_t ret = true;
221   if (batch) 
222     ret = TrainSetup::Main(name, cls, &optList);
223   else {
224     new Deferred(name, cls, &optList);
225     Info("main", "Running application (%s)", gROOT->IsBatch() 
226          ? "batch" : "normal");
227     gApplication->Run();
228   }
229
230   // --- Return ------------------------------------------------------
231   return ret ? 0 : 1;
232 }
233 //
234 // EOF
235 //
236
237         
238