]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/trains/GridTerminate.C
Remove code to dump Job IDs and stages until merged into trunk of AliAnalysisAlien
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / trains / GridTerminate.C
1 /**
2  * @file   Terminate.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  */
10 #ifndef __CINT__
11 # include <TString.h>
12 # include <TSystem.h>
13 # include <TGrid.h>
14 # include <TFile.h>
15 # include <TObjArray.h>
16 # include <TObjString.h>
17 # include <TError.h>
18 # include <TEnv.h>
19 # include <TROOT.h>
20 # include <AliAnalysisManager.h>
21 # include <AliAnalysisAlien.h>
22 # include <fstream>
23 #else
24 class TString;
25 #endif
26 /** 
27  * Load a library 
28  * 
29  * @param libName Library name 
30  * 
31  * @return true on success 
32  */
33 Bool_t LoadLib(const char* libName)
34 {
35   if (gSystem->Load(libName) < 0) {
36     Error("GridTerminate", "Failed to load library %s",libName);
37     return false;
38   }
39   Info("GridTerminate","Loaded library %s",libName);
40   return true;
41 }
42 /** 
43  * Load a PAR 
44  * 
45  * @param parName PAR file name 
46  * 
47  * @return true on success
48  */
49 Bool_t LoadPar(const char* parName)
50 {
51   if (!AliAnalysisAlien::SetupPar(parName)) {
52     Error("GridTerminate","Failed to load PAR %s",parName);
53     return false;
54   }
55   Info("GridTerminate","Loaded package %s",parName);
56   return true;
57 }
58 /** 
59  * Submit the terminate job 
60  * 
61  * @param name Name of the job
62  * @param libs Libraries to load (space separated string)
63  * @param pars PARs to load (space separated string)
64  * @param srcs Sources to load (space separated string)
65  * 
66  * @return true on success 
67  */
68 Bool_t GridTerminate(const TString& name, 
69                      const TString& libs, 
70                      const TString& pars, 
71                      const TString& srcs)
72 {
73   // Load basic ROOT libraries
74   gSystem->AddDynamicPath("/usr/lib");
75   if (gSystem->Load("libTree.so")       < 0) return false;
76   if (gSystem->Load("libGeom.so")       < 0) return false;
77   if (gSystem->Load("libVMC.so")        < 0) return false;
78   if (gSystem->Load("libPhysics.so")    < 0) return false;
79   if (gSystem->Load("libMinuit.so")     < 0) return false;
80
81   // Load basic AliROOT libraries
82   if (gSystem->Load("libSTEERBase")     < 0) return false;
83   if (gSystem->Load("libESD")           < 0) return false;
84   if (gSystem->Load("libAOD")           < 0) return false;
85   if (gSystem->Load("libANALYSIS")      < 0) return false;
86   if (gSystem->Load("libOADB")          < 0) return false;
87   if (gSystem->Load("libANALYSISalice") < 0) return false;
88
89   // Load libraries
90   TObjArray*  libsArray = libs.Tokenize(" ");
91   TObjString* lib       = 0;
92   TIter       nextLib(libsArray);
93   while ((lib = static_cast<TObjString*>(nextLib()))) {
94     const TString& libName = lib->String();
95     if (libName.Contains("libSTEERBase") ||
96         libName.Contains("libESD")       ||
97         libName.Contains("libAOD")       ||
98         libName.Contains("libANALYSIS")  ||
99         libName.Contains("libOADB")      ||
100         libName.Contains("libANALYSISalice")) continue;
101     if (!libName.Contains(".so")) continue;
102     if (!LoadLib(libName.Data())) return false;
103   }
104   libsArray->Delete();
105   
106   // Load packages
107   TObjArray*  parArray = pars.Tokenize(" ");
108   TObjString* par      = 0;
109   TIter       nextPar(parArray);
110   while ((par = static_cast<TObjString*>(nextPar()))) { 
111     TString parName(par->String());
112     if (parName.EndsWith(".par")) parName.ReplaceAll(".par", "");
113     if (parName.Contains("STEERBase") ||
114         parName.Contains("ESD")       ||
115         parName.Contains("AOD")       ||
116         parName.Contains("ANALYSIS")  ||
117         parName.Contains("OADB")      ||
118         parName.Contains("ANALYSISalice")) continue;
119     if (!LoadPar(parName.Data())) return false;
120   }
121
122   // Load sources
123   TObjArray*  srcArray = srcs.Tokenize(" ");
124   TObjString* src      = 0;
125   TIter       nextSrc(srcArray);
126   while ((src = static_cast<TObjString*>(nextSrc()))) { 
127     const TString& srcName = src->String();
128     gROOT->ProcessLine(Form(".L %s+g", srcName.Data()));
129   }
130
131   // Load the analysis manager from file
132   TString base(name);
133   base.Append(".root");
134   if (gSystem->AccessPathName(base.Data())) {
135     // Couldn't read from current directory, try sub-dir
136     TString sub(gSystem->ConcatFileName(name, base));
137     if (gSystem->AccessPathName(sub)) {
138       Error("GridTerminate","Couldn't find manager file %s",base.Data());
139       return false;
140     }
141     base = sub;
142   }
143   gEnv->SetValue("XSec.GSI.DelegProxy", "2");
144   AliAnalysisManager* mgr= AliAnalysisAlien::LoadAnalysisManager(base);
145   if (!mgr) {
146     Error("GridTerminate", "Failed to load manager from %s",base.Data());
147     return false;
148   }
149   if (!name.EqualTo(mgr->GetName())) {
150     Error("GridTerminate","Read manager %s is not %s",
151           mgr->GetName(),name.Data());
152     return false;
153   }
154   Info("GridTerminate","Loaded analysis manager");
155
156   // Load plugin
157   TFile* plug = TFile::Open(Form("%s_plugin.root",name.Data()),"READ");
158   // TFile* plug = TFile::Open("plugin.root","READ");
159   if (!plug) {
160     // Error("GridTerminate","Failed to open %s_plugin.root",name.Data());
161     Error("GridTerminate","Failed to open plugin.root");
162     return false;
163   }
164   AliAnalysisAlien* handler = 
165     static_cast<AliAnalysisAlien*>(plug->Get("plugin"));
166   if (!handler) {
167     Error("GridTerminate","Failed to load plugin");
168     return false;
169   }
170   Info("GridTerminate","Setting grid handler");
171   handler->SetRunMode("terminate");
172   mgr->SetGridHandler(handler);
173
174   // Run the terminate job
175   Info("GridTerminate","Starting terminate job");
176   if (mgr->StartAnalysis("grid") < 0) return false;
177
178 #if 0
179   std::ofstream outJobs(Form("%s_merge.jobid", mgr->GetName()));
180   outJobs << handler->GetGridJobIDs() << std::endl;
181   outJobs.close();
182   
183   std::ofstream outStages(Form("%s_merge.stage", mgr->GetName()));
184   outStages << handler->GetGridStages() << std::endl;
185   outStages.close();
186 #endif
187
188   return true;
189 }
190 // 
191 // EOF
192 //
193