]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/trains/GridTerminate.C
Various fixes
[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  * Submit the terminate job 
61  * 
62  * @param name Name of the job
63  * @param libs Libraries to load (space separated string)
64  * @param pars PARs to load (space separated string)
65  * @param srcs Sources to load (space separated string)
66  * 
67  * @return true on success 
68  */
69 Bool_t GridTerminate(const TString& name, 
70                      const TString& libs, 
71                      const TString& pars, 
72                      const TString& srcs,
73                      Bool_t         local=false)
74 {
75   // Load basic ROOT libraries
76   gSystem->AddDynamicPath("/usr/lib");
77   if (gSystem->Load("libTree.so")       < 0) return false;
78   if (gSystem->Load("libGeom.so")       < 0) return false;
79   if (gSystem->Load("libVMC.so")        < 0) return false;
80   if (gSystem->Load("libPhysics.so")    < 0) return false;
81   if (gSystem->Load("libMinuit.so")     < 0) return false;
82
83   // Load basic AliROOT libraries
84   if (gSystem->Load("libSTEERBase")     < 0) return false;
85   if (gSystem->Load("libESD")           < 0) return false;
86   if (gSystem->Load("libAOD")           < 0) return false;
87   if (gSystem->Load("libANALYSIS")      < 0) return false;
88   if (gSystem->Load("libOADB")          < 0) return false;
89   if (gSystem->Load("libANALYSISalice") < 0) return false;
90
91   // Load libraries
92   if (!libs.IsNull()) {
93     TObjArray*  libsArray = libs.Tokenize(" ");
94     TObjString* lib       = 0;
95     TIter       nextLib(libsArray);
96     while ((lib = static_cast<TObjString*>(nextLib()))) {
97       const TString& libName = lib->String();
98       if (libName.Contains("libSTEERBase") ||
99           libName.Contains("libESD")       ||
100           libName.Contains("libAOD")       ||
101           libName.Contains("libANALYSIS")  ||
102           libName.Contains("libOADB")      ||
103           libName.Contains("libANALYSISalice")) continue;
104       if (!libName.Contains(".so")) continue;
105       if (!LoadLib(libName.Data())) return false;
106     }
107     libsArray->Delete();
108   }
109   
110   // Load packages
111   if (!pars.IsNull()) {
112     TObjArray*  parArray = pars.Tokenize(" ");
113     TObjString* par      = 0;
114     TIter       nextPar(parArray);
115     while ((par = static_cast<TObjString*>(nextPar()))) { 
116       TString parName(par->String());
117       if (parName.EndsWith(".par")) parName.ReplaceAll(".par", "");
118       if (parName.Contains("STEERBase") ||
119           parName.Contains("ESD")       ||
120           parName.Contains("AOD")       ||
121           parName.Contains("ANALYSIS")  ||
122           parName.Contains("OADB")      ||
123           parName.Contains("ANALYSISalice")) continue;
124       if (!LoadPar(parName.Data())) return false;
125     }
126   }
127
128   // Load sources
129   if (!srcs.IsNull()) {
130     TObjArray*  srcArray = srcs.Tokenize(" ");
131     TObjString* src      = 0;
132     TIter       nextSrc(srcArray);
133     while ((src = static_cast<TObjString*>(nextSrc()))) { 
134       const TString& srcName = src->String();
135       gROOT->ProcessLine(Form(".L %s+g", srcName.Data()));
136     }
137   }
138
139   // Connect to the grid
140   gEnv->SetValue("XSec.GSI.DelegProxy", "2");
141   // TGrid::Connect("alien://");
142   // if (!gGrid) {
143   //   Error("GridTerminate", "Failed to connect to AliEn");
144   //   return false;
145   // }
146
147   // Load the analysis manager from file
148   TString base(name);
149   base.Append(".root");
150   if (gSystem->AccessPathName(base.Data())) {
151     // Couldn't read from current directory, try sub-dir
152     TString sub(gSystem->ConcatFileName(name, base));
153     if (gSystem->AccessPathName(sub)) {
154       Error("GridTerminate","Couldn't find manager file %s",base.Data());
155       return false;
156     }
157     base = sub;
158   }
159   AliAnalysisManager* mgr= AliAnalysisAlien::LoadAnalysisManager(base);
160   if (!mgr) {
161     Error("GridTerminate", "Failed to load manager from %s",base.Data());
162     return false;
163   }
164   if (!name.EqualTo(mgr->GetName())) {
165     Error("GridTerminate","Read manager %s is not %s",
166           mgr->GetName(),name.Data());
167     return false;
168   }
169   Info("GridTerminate","Loaded analysis manager");
170
171   // Load plugin
172   TFile* plug = TFile::Open(Form("%s_plugin.root",name.Data()),"READ");
173   // TFile* plug = TFile::Open("plugin.root","READ");
174   if (!plug) {
175     // Error("GridTerminate","Failed to open %s_plugin.root",name.Data());
176     Error("GridTerminate","Failed to open plugin.root");
177     return false;
178   }
179   AliAnalysisAlien* handler = 
180     static_cast<AliAnalysisAlien*>(plug->Get("plugin"));
181   if (!handler) {
182     Error("GridTerminate","Failed to load plugin");
183     return false;
184   }
185   Info("GridTerminate","Setting grid handler");
186   handler->SetRunMode("terminate");
187   if (local) handler->SetMergeViaJDL(false);
188   mgr->SetGridHandler(handler);
189
190   // Run the terminate job
191   Info("GridTerminate","Starting terminate job");
192   if (mgr->StartAnalysis("grid") < 0) return false;
193
194 #if 1
195   std::ofstream outJobs(Form("%s_merge.jobid", mgr->GetName()));
196   outJobs << handler->GetGridJobIDs() << std::endl;
197   outJobs.close();
198   
199   std::ofstream outStages(Form("%s_merge.stage", mgr->GetName()));
200   outStages << handler->GetGridStages() << std::endl;
201   outStages.close();
202 #endif
203
204   return true;
205 }
206 // 
207 // EOF
208 //
209