]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/trains/PluginHelper.C
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / trains / PluginHelper.C
1 /**
2  * @file   PluginHelper.C
3  * @author Christian Holm Christensen <cholm@master.hehi.nbi.dk>
4  * @date   Tue Oct 16 18:57:18 2012
5  * 
6  * @brief  Base class for helpers using the AliAnalysisAlien plugin
7  *
8  * @ingroup pwglf_forward_trains_helper
9  * 
10  */
11 #ifndef PLUGINHELPER_C
12 #define PLUGINHELPER_C
13 #include "Helper.C"
14 #ifndef __CINT__
15 # include "AvailableSoftware.C"
16 # include "ParUtilities.C"
17 # include "OutputUtilities.C"
18 # include <TUrl.h>
19 # include <TString.h>
20 # include <TEnv.h>
21 # include <TProof.h>
22 # include <AliAnalysisManager.h>
23 # include <AliAnalysisAlien.h>
24 #else
25 class TUrl;
26 class AliAnalysisAlien;
27 #endif
28
29 // ===================================================================
30 /**
31  * Handle analysis on using the AliAnalysisAlien plugin - i.e., AAF or Grid 
32  * 
33  * This helper is triggered by a URL of the form 
34  *
35  * @code
36  * <protocol>://[<user>@][<host>][:<port>]/[<file>][?<options>][#<anchor>]
37  * @endcode 
38  * where &lt;options&gt; contains <tt>plugin</tt>
39  * <dl>
40  *   <dt>&lt;options&gt;</dt>
41  *   <dd>List of options separated by an &amp;
42  *     <dl>
43  *       <dt><tt>storage=&lt;url&gt;</tt></dt>
44  *       <dd>Specify a non-default storage location for special output
45  *         (e.g., AOD trees).  &lt;url&gt; should be a valid XRootd 
46  *         server URI accessible to the slaves - e.g., 
47  *         <tt>root://lxplus.cern.ch:10930//tmp</tt>.</dd>
48  *       <dt><tt>mode=[default,rec,sim,train,custom]</tt></dt>
49  *       <dd>Set the AliROOT mode.  If not specified <tt>default</tt> 
50  *         is assumed.  See also CreateAliROOTPar</dd>
51  *       <dt><tt>par</tt></dt>
52  *       <dd>Use par files</dd>
53  *       <dt><tt>aliroot=&lt;version&gt;</tt></dt>
54  *       <dd>Set AliROOT version to use </dd>
55  *       <dt><tt>root=&lt;version&gt;</tt></dt>
56  *       <dd>Set ROOT version to use </dd>
57  *     </dl>
58  *   </dd>
59  * </dl>  
60  *
61  * @ingroup pwglf_forward_trains_helper
62  */
63 struct PluginHelper : public Helper
64 {
65   /** 
66    * Constructor 
67    * 
68    * @param url  Url 
69    * @param verbose Verbosity level 
70    */
71   PluginHelper(const TUrl& url, Int_t verbose)
72     : Helper(url, verbose), fHandler(0), fUsePars(false), 
73       fExtraLibs(), fExtraPars(), fExtraSrcs()
74   {
75     fHandler = new AliAnalysisAlien();
76
77     fOptions.Add("aliroot", "VERSION", "AliROOT version", "last");
78     fOptions.Add("root",    "VERSION", "ROOT version", "last");
79     fOptions.Add("par", "Use par files");
80     fOptions.Add("mode", "default|rec|sim", "AliROOT mode", "default");
81     fOptions.Add("storage", "URL", "Location for external storage", "");    
82     fOptions.Add("plugin", "Use AliEn handler");
83
84     fExtraLibs.SetOwner();
85     fExtraPars.SetOwner();
86     fExtraSrcs.SetOwner();
87   }
88   /** 
89    * Copy constructor 
90    * 
91    * @param o Object to copy from 
92    */
93   PluginHelper(const PluginHelper& o) 
94     : Helper(o), fHandler(o.fHandler), fUsePars(o.fUsePars), 
95       fExtraLibs(), fExtraPars(), fExtraSrcs()
96   {}
97   /** 
98    * Assignment operator 
99    * 
100    * @param o Object to assign from 
101    * 
102    * @return Reference to this 
103    */
104   PluginHelper& operator=(const PluginHelper& o) 
105   {
106     if (&o == this) return *this;
107     Helper::operator=(o);
108     fHandler = o.fHandler;
109     fUsePars = o.fUsePars;
110     return *this;
111   }
112   /** 
113    * Destructor 
114    */
115   virtual ~PluginHelper() {}
116   /** 
117    * Load a library/PAR/script 
118    * 
119    * @param name   Name 
120    * @param slaves If true, also load on slaves
121    * 
122    * @return true on success 
123    */
124   virtual Bool_t LoadLibrary(const TString& name, 
125                              Bool_t slaves=true)
126   {
127     if (!fUsePars) {
128       TString fullName(MakeLibraryName(name));
129       Int_t ret = gSystem->Load(fullName);
130       if (ret < 0) return false;
131       if (slaves) {
132         fHandler->AddAdditionalLibrary(fullName);
133         fExtraLibs.Add(new TObjString(fullName));
134       }
135     }
136     else { 
137       if (!ParUtilities::Find(name)) { 
138         Error("PluginHelper::LoadLibrary", "Failed to find PAR file %s", 
139               name.Data());
140         return false;
141       }
142       if (!ParUtilities::Build(name)) { 
143         Error("PluginHelper::LoadLibrary", "Failed to build PAR file %s", 
144               name.Data());
145         return false;
146       }
147       fHandler->EnablePackage(name);
148       fExtraPars.Add(new TObjString(name));
149     }
150     return true;
151   }
152   /** 
153    * Load a source file, and compile it 
154    * 
155    * @param name Name of the source file 
156    * @param copy If true, copy here instead of link
157    * 
158    * @return true on success
159    */
160   virtual Bool_t LoadSource(const TString& name, bool copy=false)
161   {
162     static TString s;
163     if (!Helper::LoadSource(name, copy)) return false;
164     s.Append(Form(" %s", gSystem->BaseName(name.Data())));
165     fHandler->SetAnalysisSource(s);
166     fExtraSrcs.Add(new TObjString(name));
167     return true;
168   }
169   
170   /** 
171    * Set-up to load the AliROOT libraries 
172    * 
173    * @return true on success
174    */
175   virtual Bool_t LoadAliROOT()
176   {
177     if (!gSystem->Getenv("ALICE_ROOT")) { 
178       Error("PluginHelper::LoadAliROOT", "Local AliROOT not available");
179       return false;
180     }
181     Bool_t tmp = fUsePars;
182     fUsePars   = false;
183     
184     if (!LoadLibrary("STEERBase"))     return false;
185     if (!LoadLibrary("ESD"))           return false;
186     if (!LoadLibrary("AOD"))           return false;
187     if (!LoadLibrary("ANALYSIS"))      return false;
188     if (!LoadLibrary("OADB"))          return false;
189     if (!LoadLibrary("ANALYSISalice")) return false;
190     fUsePars = tmp;
191
192     return true;
193   }
194   /** 
195    * Set-up done before task set-ups 
196    * 
197    * @return true on success 
198    */
199   virtual Bool_t PreSetup() 
200   {
201     // --- Set prefered GSI method ---------------------------------
202     gEnv->SetValue("XSec.GSI.DelegProxy", "2");
203
204     TString aliroot("last");
205     TString root("last");
206     if (fOptions.Has("aliroot")) aliroot = fOptions.Get("aliroot");
207     if (fOptions.Has("root"))    root    = fOptions.Get("root");
208
209     AvailableSoftware::Check(aliroot, root);
210     fOptions.Set("aliroot", aliroot);
211     fOptions.Set("root", root);
212
213     fUsePars = fOptions.Has("par");
214
215     fHandler->SetROOTVersion(root);
216     fHandler->SetAliROOTVersion(aliroot);
217     // Execute through interpreter until patch is applied
218     fHandler->SetDropToShell(false);
219     if (fOptions.Has("mode"))
220       fHandler->SetAliRootMode(fOptions.Get("mode"));
221     else 
222       fHandler->SetAliRootMode("default");
223     
224     return true;
225   }
226   /** 
227    * Set-up done after the task set-ups 
228    *
229    * @return true on success 
230    */
231   virtual Bool_t PostSetup() 
232   {
233     AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
234     if (!mgr) { 
235       Error("PluginHelper::PostSetup", "No analysis manager defined");
236       return false;
237     }
238     mgr->SetGridHandler(fHandler);
239
240     fHandler->SetJobTag(mgr->GetName());
241     fHandler->SetAnalysisMacro(Form("%s.C", mgr->GetName()));
242
243     if (fOptions.Has("storage"))
244       OutputUtilities::RegisterStorage(fOptions.Get("storage"));
245
246     return true;
247   };
248   /** 
249    * Pure virtual overload
250    * 
251    * @return 
252    */
253   virtual Long64_t Run(Long64_t) = 0;
254   /** 
255    * Pure virtual overload 
256    * 
257    * @return Short description
258    */
259   virtual const char* Desc() const = 0;
260   /** 
261    * Pure virtual overload 
262    * 
263    * @return URI help string 
264    */
265   virtual const Char_t* UrlHelp() const = 0;
266   /** 
267    * Overload 
268    * 
269    * @param option Options 
270    */
271   virtual void Print(Option_t* option="") const 
272   {
273     Helper::Print(option);
274     fHandler->Print(option);
275   }
276   AliAnalysisAlien* fHandler;
277   Bool_t fUsePars;
278   TList  fExtraLibs;
279   TList  fExtraPars;
280   TList  fExtraSrcs;
281 };
282 #endif
283 //
284 // EOF
285 //