]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/trains/trainMain.cxx
Added some classes to the build of PWGLF/forward2
[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 <TRint.h>
29 #include <TROOT.h>
30 #include <TList.h>
31 #include <TObjString.h>
32 #include <TString.h>
33
34 #include <iostream>
35 #include <iomanip>
36
37 /** 
38  * Custom timer to do a deferred start after the application 
39  * has been started 
40  */
41 struct Deferred : public TTimer
42 {
43   Deferred(const TString& name, const TString& cls, 
44            const TCollection* opts, bool spawn)
45     : TTimer(1000, false), 
46       fName(name), 
47       fClass(cls), 
48       fOptions(opts),
49       fSpawn(spawn)
50   {
51     Start(1000, true);
52   }
53   Deferred(const Deferred& o)
54     : TTimer(), 
55       fName(o.fName),
56       fClass(o.fClass),
57       fOptions(o.fOptions),
58       fSpawn(o.fSpawn)
59   {}
60   Deferred& operator=(const Deferred& o) 
61   { 
62     if (&o == this) return *this;
63     fName    = o.fName;
64     fClass   = o.fClass;
65     fOptions = o.fOptions;
66     fSpawn   = o.fSpawn;
67     return *this; 
68   }
69   Bool_t Notify()
70   {
71     // gSystem->RemoveTimer(this);
72     //Info("Notify","Will run train setup: %s (%s)",fName.Data(),fClass.Data());
73     return TrainSetup::Main(fName, fClass, fOptions, true, fSpawn);
74   }
75   TString fName;
76   TString fClass;
77   const TCollection* fOptions;
78   Bool_t  fSpawn;
79 };
80
81 /** 
82  * Append directory to header and script search path
83  * 
84  * @param dir Directory
85  * 
86  * @ingroup pwglf_forward_trains_run
87  */
88 void AppendPath(const char* dir)
89 {
90   gROOT->SetMacroPath(Form("%s:%s",gROOT->GetMacroPath(), dir));
91   gSystem->AddIncludePath(Form("-I%s", dir));
92 }
93 /** 
94  * Print a fake option description.  Used for options specific to this
95  * program.
96  * 
97  * @param o    Output stream 
98  * @param opt  Option (including meta argument)
99  * @param desc Option description.
100  * 
101  * @ingroup pwglf_forward_trains_run
102  */
103 void PrintFakeOption(std::ostream& o, const char* opt, const char* desc)
104 {
105   o << "  --" << std::left << std::setw(30) << opt << " " << desc 
106     << std::right << std::endl;
107 }
108
109 void ProgramUsage(const char* progname, std::ostream& o)
110 {
111   o << "Usage: " << progname 
112     << " --class=CLASS --name=NAME --url=URI [OPTIONS]\n\n"
113     << progname << " specific options:\n";
114   PrintFakeOption(o, "class=CLASS",       "Train class");
115   PrintFakeOption(o, "name=NAME",         "Name of train");
116   PrintFakeOption(o, "include=DIRECTORY", "Append dir to macro/header path");
117   PrintFakeOption(o, "batch",             "Batch mode");
118   PrintFakeOption(o, "spawn",             "Spawn interactive ROOT shell");
119 }
120 /** 
121  * Print usage information 
122  * 
123  * @param progname Program name 
124  * @param o        Output stream
125  * 
126  * @ingroup pwglf_forward_trains_run
127  */
128 void Usage(const char* progname, std::ostream& o)
129 {
130   ProgramUsage(progname, o);
131   PrintFakeOption(o, "url=URI",           "Execution URI");
132   o << "\nAlternatively to using --url=URI, one can use\n";
133   PrintFakeOption(o, "where=BASE_URI",    "Set protocol, user, host, "
134                   "and port URI");
135   PrintFakeOption(o, "file=FILE_OR_PATH", "File/path part of URI");
136   PrintFakeOption(o, "options=OPTIONS",   "Query options for URI");
137   PrintFakeOption(o, "anchor=ANCHOR",     "Query anchor for URI");
138 }
139
140 int
141 main(int argc, char** argv)
142 {
143   TList optList;
144   TList paths;
145   TString name;
146   TString cls;
147   TString where;
148   TString file;
149   TString opts;
150   TString anchor;
151   Bool_t  batch   = false;
152   Bool_t  help    = false;
153   Bool_t  urlSeen = false;
154   Bool_t  spawn   = false;
155   // --- Parse options -----------------------------------------------
156   for (int i = 1; i < argc; i++) { 
157     if (argv[i][0] == '-' && argv[i][1] == '-') { 
158       TString arg(argv[i]);
159       TString val("");
160       arg.ReplaceAll("\"'", "");
161       Int_t   eq = arg.Index("=");
162       if (eq != kNPOS) val = arg(eq+1, arg.Length()-eq-1);
163       if      (arg.BeginsWith("--class"))   cls  = val;
164       else if (arg.BeginsWith("--name"))    name = val;
165       else if (arg.BeginsWith("--include")) paths.Add(new TObjString(val)); 
166       // AppendPath(val);
167       else if (arg.BeginsWith("--batch"))   batch  = true;
168       else if (arg.BeginsWith("--help"))    help   = true;
169       else if (arg.BeginsWith("--where"))   where  = val;
170       else if (arg.BeginsWith("--file"))    file   = val;
171       else if (arg.BeginsWith("--opts"))    opts   = val;
172       else if (arg.BeginsWith("--anchor"))  anchor = val;
173       else if (arg.BeginsWith("--spawn"))   spawn  = true;
174       else {
175         if (arg.BeginsWith("--url")) urlSeen = true;
176         optList.Add(new TObjString(&(argv[i][2])));
177       }
178     }
179   }
180   // --- Set batch mode early ----------------------------------------
181   // Info("main", "Batch mode is set to %d", batch);
182   gROOT->SetBatch(batch);
183
184   // --- Add to load paths -------------------------------------------
185   // Info("main", "Adding load paths");
186   TIter nextP(&paths);
187   TObject* path = 0;
188   while ((path = nextP())) AppendPath(path->GetName());
189
190   // --- Initial check or URI/WHERE ----------------------------------
191   if (!where.IsNull()) {
192     if (urlSeen) {
193       Error("main", "option --url and --where mutually exclusive");
194       return 1;
195     }
196     TUrl u(where);
197     if (!file.IsNull())   u.SetFile(file);
198     if (!opts.IsNull())   u.SetOptions(opts);
199     if (!anchor.IsNull()) u.SetAnchor(anchor);
200     optList.Add(new TObjString(Form("url=%s", u.GetUrl())));
201   }
202
203   // --- check for help ----------------------------------------------
204   if (help) {
205     if (cls.IsNull()) {
206       Usage(argv[0], std::cout);
207       return 0;
208     }
209     ProgramUsage(argv[0], std::cout);
210     optList.Add(new TObjString("help"));
211   }
212
213   // --- Check name and class ----------------------------------------
214   if (name.IsNull()) { 
215     Error("main", "No name specified");
216     return 1;
217   }
218   if (cls.IsNull()) { 
219     Error("main", "No class specified");
220     return 1;
221   }
222
223   // --- Setup script path -------------------------------------------
224   const char* aliPath  = gSystem->ExpandPathName("$ALICE_ROOT");
225   const char* fwdPath  = gSystem->ExpandPathName("$ALICE_ROOT/PWGLF/FORWARD/");
226   AppendPath(aliPath);
227   AppendPath(Form("%s/include",          aliPath));
228   AppendPath(Form("%s/trains",           fwdPath));
229   AppendPath(Form("%s/analysis2",        fwdPath));
230   AppendPath(Form("%s/analysis2/trains", fwdPath));
231
232   // --- Set-up Application ------------------------------------------
233   TApplication* app = 0;
234   if (spawn) {
235     // Info("main", "Creating interpreter application");
236     TRint* rint = new TRint("runTrain", 0, 0, 0, 0, true);
237     rint->SetPrompt("runTrain[%3d]> ");
238     app = rint;
239   }
240   else if (!batch) {
241     // Info("main", "Creating GUI application");
242     app = new TGApplication("runTrain", 0, 0);
243   }
244   if (app && !batch) app->InitializeGraphics();
245
246   // --- run, possibly in a timer ------------------------------------
247   Bool_t ret = true;
248   if (!app) 
249     ret = TrainSetup::Main(name, cls, &optList);
250   else {
251     // optList.ls();
252     new Deferred(name, cls, &optList, spawn);
253     gApplication->Run();
254   }
255
256   // --- Return ------------------------------------------------------
257   return ret ? 0 : 1;
258 }
259 //
260 // EOF
261 //
262
263         
264