]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/trains/GridTerminate.C
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / trains / GridTerminate.C
1 /**
2  * @file   GridTerminate.C
3  * @author Christian Holm Christensen <cholm@master.hehi.nbi.dk>
4  * @date   Wed Jan 23 21:22:28 2013
5  * 
6  * @brief  Worker script to run terminate part for Grid  
7  * 
8  * 
9  * @ingroup pwglf_forward_trains_helper
10  */
11 #ifndef __CINT__
12 # include <TString.h>
13 # include <TSystem.h>
14 # include <TGrid.h>
15 # include <TFile.h>
16 # include <TObjArray.h>
17 # include <TObjString.h>
18 # include <TError.h>
19 # include <TEnv.h>
20 # include <TROOT.h>
21 # include <AliAnalysisManager.h>
22 # include <AliAnalysisAlien.h>
23 # include <fstream>
24 #else
25 class TString;
26 #endif
27 /** 
28  * Load a library 
29  * 
30  * @param libName Library name 
31  * 
32  * @return true on success 
33  */
34 Bool_t LoadLib(const char* libName)
35 {
36   if (gSystem->Load(libName) < 0) {
37     Error("GridTerminate", "Failed to load library %s",libName);
38     return false;
39   }
40   Info("GridTerminate","Loaded library %s",libName);
41   return true;
42 }
43 /** 
44  * Load a PAR 
45  * 
46  * @param parName PAR file name 
47  * 
48  * @return true on success
49  */
50 Bool_t LoadPar(const char* parName)
51 {
52   if (!AliAnalysisAlien::SetupPar(parName)) {
53     Error("GridTerminate","Failed to load PAR %s",parName);
54     return false;
55   }
56   Info("GridTerminate","Loaded package %s",parName);
57   return true;
58 }
59
60 /** 
61  * Load the analysis handler if needed 
62  * 
63  * @param name 
64  * 
65  * @return 
66  */
67 Bool_t LoadHandler(const TString& name)
68 {
69   // Load plugin
70   TFile* file = TFile::Open(Form("%s_plugin.root",name.Data()),"READ");
71   // TFile* plug = TFile::Open("plugin.root","READ");
72   if (!file) {
73     // Error("GridTerminate","Failed to open %s_plugin.root",name.Data());
74     Error("GridTerminate","Failed to open %s_plugin.root",
75           name.Data());
76     return false;
77   }
78   AliAnalysisAlien* handler = 
79     static_cast<AliAnalysisAlien*>(file->Get("plugin"));
80   if (!handler) {
81     Error("GridTerminate","Failed to load plugin");
82     return false;
83   }
84   Info("GridTerminate","Setting grid handler");
85   handler->SetRunMode("terminate");
86   handler->SetMergeViaJDL(true);
87   AliAnalysisManager::GetAnalysisManager()->SetGridHandler(handler);
88
89   return true;
90 }
91
92 /** 
93  * Setup our manager et al 
94  * 
95  * @param name            Name of the job
96  * @param libs            Libraries to load (space separated string)
97  * @param pars            PARs to load (space separated string)
98  * @param srcs            Sources to load (space separated string)
99  * @param local           If true, run local terminate job on already 
100  *                        merged and downloaded files.
101  * @param localLibsNotPar if @a local is true and this is true, then
102  *                        we load the corresponding locally compiled 
103  *                        library for each specified PAR file.
104  * 
105  * @return true on success 
106  */
107 Bool_t Setup(const TString& name, 
108              const TString& libs, 
109              const TString& pars, 
110              const TString& srcs,
111              Bool_t         local=false,
112              Bool_t         localLibsNotPar=true)
113 {
114   // Load basic ROOT libraries
115   gSystem->AddDynamicPath("/usr/lib");
116   if (gSystem->Load("libTree")       < 0) return false;
117   if (gSystem->Load("libGeom")       < 0) return false;
118   if (gSystem->Load("libVMC")        < 0) return false;
119   if (gSystem->Load("libPhysics")    < 0) return false;
120   if (gSystem->Load("libMinuit")     < 0) return false;
121
122   // Load basic AliROOT libraries
123   if (gSystem->Load("libSTEERBase")     < 0) return false;
124   if (gSystem->Load("libESD")           < 0) return false;
125   if (gSystem->Load("libAOD")           < 0) return false;
126   if (gSystem->Load("libANALYSIS")      < 0) return false;
127   if (gSystem->Load("libOADB")          < 0) return false;
128   if (gSystem->Load("libANALYSISalice") < 0) return false;
129
130   // Load libraries
131   if (!libs.IsNull()) {
132     TObjArray*  libsArray = libs.Tokenize(" ");
133     TObjString* lib       = 0;
134     TIter       nextLib(libsArray);
135     while ((lib = static_cast<TObjString*>(nextLib()))) {
136       const TString& libName = lib->String();
137       if (libName.Contains("libSTEERBase") ||
138           libName.Contains("libESD")       ||
139           libName.Contains("libAOD")       ||
140           libName.Contains("libANALYSIS")  ||
141           libName.Contains("libOADB")      ||
142           libName.Contains("libANALYSISalice")) continue;
143       if (!libName.Contains(".so")) continue;
144       if (!LoadLib(libName.Data())) return false;
145     }
146     libsArray->Delete();
147   }
148   
149   // Load packages
150   if (!pars.IsNull()) {
151     TObjArray*  parArray = pars.Tokenize(" ");
152     TObjString* par      = 0;
153     TIter       nextPar(parArray);
154     while ((par = static_cast<TObjString*>(nextPar()))) { 
155       TString parName(par->String());
156       if (parName.EndsWith(".par")) parName.ReplaceAll(".par", "");
157       if (parName.Contains("STEERBase") ||
158           parName.Contains("ESD")       ||
159           parName.Contains("AOD")       ||
160           parName.Contains("ANALYSIS")  ||
161           parName.Contains("OADB")      ||
162           parName.Contains("ANALYSISalice")) continue;
163       Bool_t ret = true;
164       if (local && localLibsNotPar) 
165         ret = LoadLib(Form("lib%s.so", parName.Data()));
166       else 
167         ret = LoadPar(parName.Data());
168       if (!ret) return false;
169     }
170   }
171
172   // Load sources
173   if (!srcs.IsNull()) {
174     TObjArray*  srcArray = srcs.Tokenize(" ");
175     TObjString* src      = 0;
176     TIter       nextSrc(srcArray);
177     while ((src = static_cast<TObjString*>(nextSrc()))) { 
178       const TString& srcName = src->String();
179       gROOT->ProcessLine(Form(".L %s+g", srcName.Data()));
180     }
181   }
182
183   // Connect to the grid
184   gEnv->SetValue("XSec.GSI.DelegProxy", "2");
185   // TGrid::Connect("alien://");
186   // if (!gGrid) {
187   //   Error("GridTerminate", "Failed to connect to AliEn");
188   //   return false;
189   // }
190
191   // Load the analysis manager from file
192   TString base(name);
193   base.Append(".root");
194   if (gSystem->AccessPathName(base.Data())) {
195     // Couldn't read from current directory, try sub-dir
196     TString sub(gSystem->ConcatFileName(name, base));
197     if (gSystem->AccessPathName(sub)) {
198       Error("GridTerminate","Couldn't find manager file %s",base.Data());
199       return false;
200     }
201     base = sub;
202   }
203   AliAnalysisManager* mgr= AliAnalysisAlien::LoadAnalysisManager(base);
204   if (!mgr) {
205     Error("GridTerminate", "Failed to load manager from %s",base.Data());
206     return false;
207   }
208   if (!name.EqualTo(mgr->GetName())) {
209     Error("GridTerminate","Read manager %s is not %s",
210           mgr->GetName(),name.Data());
211     return false;
212   }
213   Info("GridTerminate","Loaded analysis manager");
214
215   // If we do a local merge, do not do any else 
216   if (local) return true;
217
218   return LoadHandler(name);
219 }
220
221 /** 
222  * Submit the terminate job 
223  * 
224  * @param name            Name of the job
225  * @param libs            Libraries to load (space separated string)
226  * @param pars            PARs to load (space separated string)
227  * @param srcs            Sources to load (space separated string)
228  * @param local           If true, run local terminate job on already 
229  *                        merged and downloaded files.
230  * @param localLibsNotPar if @a local is true and this is true, then
231  *                        we load the corresponding locally compiled 
232  *                        library for each specified PAR file.
233  * 
234  * @return true on success 
235  */
236 Bool_t GridTerminate(const TString& name, 
237                      const TString& libs, 
238                      const TString& pars, 
239                      const TString& srcs,
240                      Bool_t         local=false,
241                      Bool_t         localLibsNotPar=true)
242 {
243   if (!Setup(name, libs, pars, srcs, local, localLibsNotPar)) return false; 
244
245   // Run the terminate job
246   Info("GridTerminate","Starting terminate job - %s", local ? "locally" : "on the grid");
247
248   AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
249   if (!local) {
250
251     AliAnalysisAlien* handler = static_cast<AliAnalysisAlien*>(mgr->GetGridHandler());
252     if (!handler) { 
253       Error("GridTerminate", "Manager does now have an AliEn handler");
254       return false;
255     }
256     if (mgr->StartAnalysis("grid") < 0) return false;
257
258     std::ofstream outJobs(Form("%s_merge.jobid", mgr->GetName()));
259     outJobs << handler->GetGridJobIDs() << std::endl;
260     outJobs.close();
261     
262     std::ofstream outStages(Form("%s_merge.stage", mgr->GetName()));
263     outStages << handler->GetGridStages() << std::endl;
264     outStages.close();
265
266     return true;
267   }
268   
269   // mgr->SetDebugLevel(2);
270   mgr->SetSkipTerminate(false);
271   TTree* dummy = 0;
272   if (mgr->StartAnalysis("gridterminate", dummy, -1, 0) < 0) return false;
273   
274   return true;
275 }
276
277 // 
278 // EOF
279 //
280