more methods facilitate to merge
[u/mrichter/AliRoot.git] / ANALYSIS / macros / AnalysisTrainNew.C
CommitLineData
a42c0df6 1//===================== ANALYSIS TRAIN =========================================
c6e7c68c 2// To use: copy this macro to your work directory, modify the global part to match
3// your needs, then run root.
4// root[0] .L AnalysisTrain.C
5// Grid full mode as below (other modes: test, offline, submit, terminate)
6// root[1] AnalysisTrainNew("grid", "full")
7// CAF mode (requires root v5-23-02 + aliroot v4-16-Rev08)
8// root[2] AnalysisTrainNew("proof")
15dac83a 9// Local mode requires AliESds.root or AliAOD.root in ./data directory
c6e7c68c 10// root[3] AnalysisTrainNew("local")
7581b1bc 11// In proof and grid modes, a token is needed and sourcing the produced environment file.
a42c0df6 12//
13// If 'saveTrain' flag is set, the train will generate a directory name and run
14// in this directory. A configuration file 'ConfigTrain.C' will be generated.
15// One can replay at any time the train via:
16// root[1] AnalysisTrainNew(ana_mode, plugin_mode, "train_default_<date>/ConfigTrain.C")
c6e7c68c 17
a42c0df6 18//================== TRAIN NAME ============================================
19TString train_name = "default"; // enters file names, so no blancs or special characters
20//==============================================================================
21
22// ### Settings that make sense in PROOF only
23//==============================================================================
24TString proof_cluster = "alicecaf.cern.ch";
25Bool_t useAFPAR = kFALSE; // use AF special par file
26TString AFversion = "AF-v4-16";
7581b1bc 27// Change CAF dataset here
a42c0df6 28TString proof_dataset = "/COMMON/COMMON/LHC09a4_run8100X#/esdTree";
b160b576 29TString proof_outdir = "";
c6e7c68c 30
a42c0df6 31// ### Settings that make sense when using the Alien plugin
32//==============================================================================
33Bool_t usePLUGIN = kTRUE; // do not change
34// Usage of par files ONLY in grid mode and ONLY if the code is not available
7581b1bc 35// in the deployed AliRoot versions. Par file search path: local dir, if not there $ALICE_ROOT.
36// To refresh par files, remove the ones in the workdir, then do "make <target.par>" in
37// AliRoot.
a42c0df6 38Bool_t usePAR = kFALSE; // use par files for extra libs
39Bool_t useCPAR = kFALSE; // use par files for common libs
b160b576 40TString root_version = "v5-23-04";
41TString aliroot_version = "v4-17-01";
a42c0df6 42// Change production base directory here
43TString alien_datadir = "/alice/sim/PDC_09/LHC09a4/";
44// Use up to 10 non-zero run numbers
45Int_t run_numbers[10] = {81272, 81273 , 81274, 0, 0,
46 0, 0, 0, 0, 0};
47// ### Settings that make sense only for local analysis
48//==============================================================================
49// Change local xml dataset for local interactive analysis
50TString local_xmldataset = "";
c6e7c68c 51
a42c0df6 52// ### Other flags to steer the analysis
53//==============================================================================
54Bool_t useDBG = kTRUE; // activate debugging
55Bool_t useMC = kTRUE; // use MC info
56Bool_t useTAGS = kFALSE; // use ESD tags for selection
57Bool_t useKFILTER = kTRUE; // use Kinematics filter
58Bool_t useTR = kFALSE; // use track references
59Bool_t useCORRFW = kFALSE; // do not change
b160b576 60Bool_t useAODTAGS = kTRUE; // use AOD tags
a42c0df6 61Bool_t saveTrain = kTRUE; // save train configuration as:
b160b576 62Bool_t saveProofToAlien = kFALSE; // save proof outputs in AliEn
a42c0df6 63 // train_[trainName]_ddMonthyyyy_time.C
64// ### Analysis modules to be included. Some may not be yet fully implemented.
65//==============================================================================
66Int_t iAODanalysis = 0; // Analysis on input AOD's
67Int_t iAODhandler = 1; // Analysis produces an AOD or dAOD's
68Int_t iESDfilter = 1; // ESD to AOD filter (barrel + muon tracks)
69Int_t iMUONcopyAOD = 0; // Task that copies only muon events in a separate AOD (PWG3)
b160b576 70Int_t iJETAN = 0; // Jet analysis (PWG4) - needs ESD filter
a42c0df6 71Int_t iPWG4partcorr = 1; // Gamma-hadron correlations task (PWG4)
7cbd7add 72Int_t iPWG3vertexing = 0; // Vertexing HF task (PWG2)
73Int_t iPWG2femto = 0; // Femtoscopy task (PWG2)
74Int_t iPWG2spectra = 0; // Spectra PWG2 tasks (protons, cascades, V0 check, strange)
a42c0df6 75Int_t iPWG2flow = 0; // Flow analysis task (PWG2)
7cbd7add 76Int_t iPWG2res = 0; // Resonances task (PWG2)
77Int_t iPWG2kink = 0; // Kink analysis task (PWG2)
a42c0df6 78
79// Temporaries.
c6e7c68c 80TString anaPars = "";
81TString anaLibs = "";
82// Function signatures
83class AliAnalysisGrid;
84
85//______________________________________________________________________________
a42c0df6 86void AnalysisTrainNew(const char *analysis_mode="grid",
87 const char *plugin_mode="full",
88 const char *config_file="")
c6e7c68c 89{
a42c0df6 90// Main analysis train macro. If a configuration file is provided, all parameters
91// are taken from there but may be altered by CheckModuleFlags.
92 if (strlen(config_file) && !LoadConfig(config_file)) return;
c6e7c68c 93 TString smode(analysis_mode);
a42c0df6 94 smode.ToUpper();
95 if (saveTrain) WriteConfig();
c6e7c68c 96 // Check compatibility of selected modules
97 CheckModuleFlags(smode);
98
99 printf("==================================================================\n");
a42c0df6 100 printf("=========== RUNNING ANALYSIS TRAIN %s IN %s MODE ==========\n", train_name.Data(),smode.Data());
c6e7c68c 101 printf("==================================================================\n");
102 printf("= Configuring analysis train for: =\n");
103 if (iAODanalysis) printf("= AOD analysis =\n");
104 else printf("= ESD analysis =\n");
105 if (iESDfilter) printf("= ESD filter =\n");
c6e7c68c 106 if (iMUONcopyAOD) printf("= MUON copy AOD =\n");
107 if (iJETAN) printf("= Jet analysis =\n");
f0e8e44e 108 if (iPWG2spectra) printf("= PWG2 proton, checkCascade, checkV0, strange =\n");
c6e7c68c 109 if (iPWG2femto) printf("= PWG2 femtoscopy =\n");
110 if (iPWG2flow) printf("= PWG2 flow =\n");
111 if (iPWG2res) printf("= PWG2 resonances =\n");
081194a7 112 if (iPWG2kink) printf("= PWG2 kink analysis =\n");
c6e7c68c 113 if (iPWG3vertexing) printf("= PWG3 vertexing =\n");
274ff025 114 if (iPWG4partcorr) printf("= PWG4 gamma-hadron, pi0 and gamma-jet correlations =\n");
c6e7c68c 115 printf("==================================================================\n");
116 printf(":: use MC truth %d\n", (UInt_t)useMC);
099d8f33 117 printf(":: use KINE filter %d\n", (UInt_t)useKFILTER);
c6e7c68c 118 printf(":: use track refs %d\n", (UInt_t)useTR);
099d8f33 119 printf(":: use tags %d\n", (UInt_t)useTAGS);
120 printf(":: use AOD tags %d\n", (UInt_t)useAODTAGS);
c6e7c68c 121 printf(":: use debugging %d\n", (UInt_t)useDBG);
122 printf(":: use PAR files %d\n", (UInt_t)usePAR);
123 printf(":: use AliEn plugin %d\n", (UInt_t)usePLUGIN);
124
b160b576 125 //==========================================================================
c6e7c68c 126 // Connect to back-end system
127 if (!Connect(smode)) {
128 ::Error("AnalysisTrain", "Could not connect to %s back-end", analysis_mode);
129 return;
130 }
131
132 // Load common libraries and set include path
133 if (!LoadCommonLibraries(smode)) {
134 ::Error("AnalysisTrain", "Could not load common libraries");
135 return;
b160b576 136 }
c6e7c68c 137
b160b576 138 // Make the analysis manager and connect event handlers
139 AliAnalysisManager *mgr = new AliAnalysisManager("Analysis Train", "Production train");
140 if (saveProofToAlien) mgr->SetSpecialOutputLocation(proof_outdir);
141
c6e7c68c 142 // Load analysis specific libraries
143 if (!LoadAnalysisLibraries(smode)) {
144 ::Error("AnalysisTrain", "Could not load analysis libraries");
145 return;
146 }
147
c6e7c68c 148 // Create input handler (input container created automatically)
149 if (iAODanalysis) {
150 // AOD input handler
151 AliAODInputHandler *aodH = new AliAODInputHandler();
152 mgr->SetInputEventHandler(aodH);
153 } else {
154 // ESD input handler
155 AliESDInputHandler *esdHandler = new AliESDInputHandler();
099d8f33 156 if (useTAGS) esdHandler->SetReadTags();
c6e7c68c 157 mgr->SetInputEventHandler(esdHandler);
158 }
159 // Monte Carlo handler
160 if (useMC && !iAODanalysis) {
161 AliMCEventHandler* mcHandler = new AliMCEventHandler();
162 mgr->SetMCtruthEventHandler(mcHandler);
163 mcHandler->SetReadTR(useTR);
164 }
165 // AOD output container, created automatically when setting an AOD handler
166 if (iAODhandler) {
167 // AOD output handler
168 AliAODHandler* aodHandler = new AliAODHandler();
b160b576 169 aodHandler->SetOutputFileName("AliAOD.root");
c6e7c68c 170 mgr->SetOutputEventHandler(aodHandler);
b160b576 171 if (iAODanalysis) {
172 aodHandler->SetCreateNonStandardAOD();
173 if (iPWG3vertexing) aodHandler->SetOutputFileName("AliAOD.VertexingHF.root");
174 }
c6e7c68c 175 }
176 // Debugging if needed
177 if (useDBG) mgr->SetDebugLevel(3);
178
179 //==========================================================================
180 // Create the chain. In this example it is created only from ALIEN files but
181 // can be done to work in batch or grid mode as well.
182 TChain *chain = CreateChain(smode, plugin_mode);
183
184 //==========================================================================
185 // Load the tasks configuration macros for all wagons. These files are supposed now to be
186 // in the current workdir, but in AliEn they will be in the file catalog,
187 // mapped from AliRoot and pecified in the jdl input list.
188
189 // For now connection to top input container and common AOD output container
190 // is done in this macro, but in future these containers will be connected
191 // from each task configuration macro.
192
193 if (iESDfilter && !iAODanalysis) {
194 // ESD filter task configuration.
195 gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskESDFilter.C");
099d8f33 196 AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(useKFILTER);
c6e7c68c 197 }
198
099d8f33 199 // AOD tags
200 if (useAODTAGS) {
201 AliAnalysisTaskTagCreator* tagTask = new AliAnalysisTaskTagCreator("AOD Tag Creator");
202 mgr->AddTask(tagTask);
203 AliAnalysisDataContainer *coutTags = mgr->CreateContainer("cTag", TTree::Class(),
204 AliAnalysisManager::kOutputContainer, "AOD.tag.root");
205 mgr->ConnectInput (tagTask, 0, mgr->GetCommonInputContainer());
206 mgr->ConnectOutput(tagTask, 1, coutTags);
c6e7c68c 207 }
208
209 // Jet analysis
210 if (iJETAN) {
211 gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJets.C");
274ff025 212 AliAnalysisTaskJets *taskjets = AddTaskJets("AOD", "UA1");
081194a7 213 if (!taskjets) ::Warning("AnalysisTrainNew", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
c6e7c68c 214 }
215
216 // Proton analysis
217 if (iPWG2spectra) {
f0e8e44e 218 // protons
c85963a0 219 gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskProtons.C");
c6e7c68c 220 AliAnalysisTaskProtons *taskprotons = AddTaskProtons();
081194a7 221 if (!taskprotons) ::Warning("AnalysisTrainNew", "AliAnalysisTaskProtons cannot run for this train conditions - EXCLUDED");
f0e8e44e 222 // cascades
274ff025 223 gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckCascade.C");
f0e8e44e 224 AliAnalysisTaskCheckCascade *taskcheckcascade = AddTaskCheckCascade();
081194a7 225 if (!taskcheckcascade) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckCascade cannot run for this train conditions - EXCLUDED");
6e7f8264 226 // v0's
274ff025 227 gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckV0.C");
6e7f8264 228 AliAnalysisTaskCheckV0 *taskcheckV0 = AddTaskCheckV0();
081194a7 229 if (!taskcheckV0) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckV0 cannot run for this train conditions - EXCLUDED");
6e7f8264 230 // strangeness
274ff025 231 gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskStrange.C");
6e7f8264 232 AliAnalysisTaskStrange *taskstrange = AddTaskStrange();
081194a7 233 if (!taskstrange) ::Warning("AnalysisTrainNew", "AliAnalysisTaskStrange cannot run for this train conditions - EXCLUDED");
c6e7c68c 234 }
235
081194a7 236 // Femtoscopy analysis modules
237 if (iPWG2femto) {
238 gROOT->LoadMacro("$ALICE_ROOT/PWG2/FEMTOSCOPY/macros/AddTaskFemto.C");
239 AliAnalysisTaskFemto *taskfemto = AddTaskFemto();
240 if (!taskfemto) ::Warning("AnalysisTrainNew", "AliAnalysisTaskFemto cannot run for this train conditions - EXCLUDED");
241 }
242
243 // Kink analysis
244 if (iPWG2kink) {
245 gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKink.C");
246 AliAnalysisKinkESDMC *taskkink = AddTaskKink();
247 if (!taskkink) ::Warning("AnalysisTrainNew", "AliAnalysisKinkESDMC cannot run for this train conditions - EXCLUDED");
248 gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonance.C");
249 AliResonanceKinkPID *taskkinkres = AddTaskKinkResonance();
250 if (!taskkinkres) ::Warning("AnalysisTrainNew", "AliResonanceKinkPID cannot run for this train conditions - EXCLUDED");
251 gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonanceLikeSign.C");
252 AliResonanceKinkLikeSign *taskkinklikesign = AddTaskKinkResonanceLikeSign();
253 if (!taskkinklikesign) ::Warning("AnalysisTrainNew", "AliResonanceKinkLikeSign cannot run for this train conditions - EXCLUDED");
254 }
255
256 // PWG3 vertexing
15dac83a 257 if (iPWG3vertexing) {
258 gROOT->LoadMacro("$ALICE_ROOT/PWG3/vertexingHF/AddTaskVertexingHF.C");
259 AliAnalysisTaskSEVertexingHF *taskvertexingHF = AddTaskVertexingHF();
081194a7 260 if (!taskvertexingHF) ::Warning("AnalysisTrainNew", "AliAnalysisTaskSEVertexingHF cannot run for this train conditions - EXCLUDED");
15dac83a 261 }
262
c6e7c68c 263 // PWG4 hadron correlations
264 if (iPWG4partcorr) {
099d8f33 265 gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskPartCorr.C");
081194a7 266 AliAnalysisTaskParticleCorrelation *taskpartcorrPHOS = AddTaskPartCorr("AOD", "PHOS");
267 if (!taskpartcorrPHOS) ::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation PHOS cannot run for this train conditions - EXCLUDED");
274ff025 268 AliAnalysisTaskParticleCorrelation *taskpartcorrEMCAL = AddTaskPartCorr("AOD", "EMCAL");
081194a7 269 if (!taskpartcorrEMCAL) ::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation EMCAL cannot run for this train conditions - EXCLUDED");
15dac83a 270 }
c6e7c68c 271 //==========================================================================
272 // FOR THE REST OF THE TASKS THE MACRO AddTaskXXX() is not yet implemented/
273 // Run the analysis
274 //
275 if (mgr->InitAnalysis()) {
276 mgr->PrintStatus();
a42c0df6 277 if (saveTrain) gSystem->ChangeDirectory(train_name);
c6e7c68c 278 StartAnalysis(smode, chain);
279 }
280}
281
282//______________________________________________________________________________
283void StartAnalysis(const char *mode, TChain *chain) {
284// Start analysis.
285 Int_t imode = -1;
286 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
287 if (!strcmp(mode, "LOCAL")) imode = 0;
288 if (!strcmp(mode, "PROOF")) imode = 1;
289 if (!strcmp(mode, "GRID")) imode = 2;
290 switch (imode) {
291 case 0:
292 if (!chain) {
a42c0df6 293 ::Error("AnalysisTrainNew.C::StartAnalysis", "Cannot create the chain");
c6e7c68c 294 return;
295 }
081194a7 296 mgr->StartAnalysis(mode, chain);
c6e7c68c 297 return;
298 case 1:
a42c0df6 299 if (!proof_dataset.Length()) {
300 ::Error("AnalysisTrainNew.C::StartAnalysis", "proof_dataset is empty");
c6e7c68c 301 return;
302 }
b160b576 303 mgr->StartAnalysis(mode, proof_dataset, 1000);
c6e7c68c 304 return;
305 case 2:
306 if (usePLUGIN) {
307 if (!mgr->GetGridHandler()) {
a42c0df6 308 ::Error("AnalysisTrainNew.C::StartAnalysis", "Grid plugin not initialized");
c6e7c68c 309 return;
310 }
311 mgr->StartAnalysis("grid");
312 } else {
313 if (!chain) {
a42c0df6 314 ::Error("AnalysisTrainNew.C::StartAnalysis", "Cannot create the chain");
c6e7c68c 315 return;
316 }
317 mgr->StartAnalysis(mode, chain);
318 }
319 return;
320 }
321}
322
323//______________________________________________________________________________
324void CheckModuleFlags(const char *mode) {
325// Checks selected modules and insure compatibility
326 Int_t imode = -1;
327 if (!strcmp(mode, "LOCAL")) imode = 0;
328 if (!strcmp(mode, "PROOF")) imode = 1;
329 if (!strcmp(mode, "GRID")) imode = 2;
330 if (imode==1) {
081194a7 331 if (!usePAR) {
a42c0df6 332 ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PAR files enabled due to PROOF analysis");
081194a7 333 usePAR = kTRUE;
334 }
274ff025 335 }
081194a7 336 if (imode != 2) {
a42c0df6 337 ::Info("AnalysisTrainNew.C::CheckModuleFlags", "AliEn plugin disabled since not in GRID mode");
081194a7 338 usePLUGIN = kFALSE;
339 }
c6e7c68c 340 if (iAODanalysis) {
341 // AOD analysis
081194a7 342 if (useMC)
a42c0df6 343 ::Info("AnalysisTrainNew.C::CheckModuleFlags", "MC usage disabled in analysis on AOD's");
c6e7c68c 344 useMC = kFALSE;
345 useTR = kFALSE;
081194a7 346 if (iESDfilter)
a42c0df6 347 ::Info("AnalysisTrainNew.C::CheckModuleFlags", "ESD filter disabled in analysis on AOD's");
c6e7c68c 348 iESDfilter = 0;
081194a7 349 if (!iAODhandler) {
350 if (iJETAN)
a42c0df6 351 ::Info("AnalysisTrainNew.C::CheckModuleFlags", "JETAN disabled in analysis on AOD's without AOD handler");
081194a7 352 iJETAN = 0;
353 }
c6e7c68c 354 // Disable tasks that do not work yet on AOD data
081194a7 355 if (iPWG2kink)
a42c0df6 356 ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2kink disabled in analysis on AOD's");
7cbd7add 357 iPWG2kink = 0;
c6e7c68c 358 } else {
359 // ESD analysis
360 iMUONcopyAOD = 0;
c6e7c68c 361 }
099d8f33 362 if (iJETAN) iESDfilter=1;
c6e7c68c 363 if (iESDfilter) iAODhandler=1;
15dac83a 364 if (iPWG2spectra || iPWG2flow || iPWG3vertexing) useCORRFW = kTRUE;
099d8f33 365 if (useKFILTER && !useMC) useKFILTER = kFALSE;
366 if (useAODTAGS && !iAODhandler) useAODTAGS = kFALSE;
c6e7c68c 367}
368
369//______________________________________________________________________________
370Bool_t Connect(const char *mode) {
371// Connect <username> to the back-end system.
372 Int_t imode = -1;
373 if (!strcmp(mode, "LOCAL")) imode = 0;
374 if (!strcmp(mode, "PROOF")) imode = 1;
375 if (!strcmp(mode, "GRID")) imode = 2;
376 TString username = gSystem->Getenv("alien_API_USER");
377 switch (imode) {
378 case 0:
379 break;
380 case 1:
381 if (!username.Length()) {
a42c0df6 382 ::Error(Form("AnalysisTrainNew.C::Connect <%s>", mode), "Make sure you:\n \
c6e7c68c 383 1. Have called: alien-token-init <username>\n \
384 2. Have called: >source /tmp/gclient_env_$UID");
385 return kFALSE;
386 }
a42c0df6 387 ::Info("AnalysisTrainNew.C::Connect", "Connecting user <%s> to PROOF cluster <%s>",
388 username.Data(), proof_cluster.Data());
b160b576 389 gEnv->SetValue("XSec.GSI.DelegProxy", "2");
7cbd7add 390// TProof::Open(Form("%s@%s:31093", username.Data(), proof_cluster.Data()));
391 TProof::Open(Form("%s@%s", username.Data(), proof_cluster.Data()));
c6e7c68c 392 if (!gProof) {
393 if (strcmp(gSystem->Getenv("XrdSecGSISRVNAMES"), "lxfsrd0506.cern.ch"))
a42c0df6 394 ::Error(Form("AnalysisTrainNew.C::Connect <%s>", mode), "Environment XrdSecGSISRVNAMES different from lxfsrd0506.cern.ch");
c6e7c68c 395 return kFALSE;
396 }
b160b576 397 TGrid::Connect("alien://");
398 if (gGrid) {
399 TString homedir = gGrid->GetHomeDirectory();
400 TString workdir = homedir + train_name;
401 if (!gGrid->Cd(workdir)) {
402 gGrid->Cd(homedir);
403 if (gGrid->Mkdir(workdir)) {
404 gGrid->Cd(train_name);
405 ::Info("AnalysisTrainNew::Connect()", "Directory %s created", gGrid->Pwd());
406 }
407 }
408 gGrid->Mkdir("proof_output");
409 gGrid->Cd("proof_output");
410 proof_outdir = Form("alien://%s", gGrid->Pwd());
411 }
c6e7c68c 412 break;
413 case 2:
414 if (!username.Length()) {
a42c0df6 415 ::Error(Form("AnalysisTrainNew.C::Connect <%s>", mode), "Make sure you:\n \
c6e7c68c 416 1. Have called: alien-token-init <username>\n \
417 2. Have called: >source /tmp/gclient_env_$UID");
418 return kFALSE;
419 }
420 if (usePLUGIN && !gSystem->Getenv("alien_CLOSE_SE")) {
a42c0df6 421 ::Error(Form("AnalysisTrainNew.C::Connect <%s>", mode),
c6e7c68c 422 "When using the AliEn plugin it is preferable to define the \
423 variable alien_CLOSE_SE in your environment.");
424 return kFALSE;
425 }
a42c0df6 426 ::Info("AnalysisTrainNew.C::Connect", "Connecting user <%s> to AliEn ...",
c6e7c68c 427 username.Data());
428 TGrid::Connect("alien://");
429 if (!gGrid || !gGrid->IsConnected()) return kFALSE;
430 break;
431 default:
a42c0df6 432 ::Error("AnalysisTrainNew.C::Connect", "Unknown run mode: %s", mode);
c6e7c68c 433 return kFALSE;
434 }
a42c0df6 435 ::Info("AnalysisTrainNew.C::Connect","Connected in %s mode", mode);
c6e7c68c 436 return kTRUE;
437}
438
439//______________________________________________________________________________
440Bool_t LoadCommonLibraries(const char *mode)
441{
442// Load common analysis libraries.
443 Int_t imode = -1;
444 if (!strcmp(mode, "LOCAL")) imode = 0;
445 if (!strcmp(mode, "PROOF")) imode = 1;
446 if (!strcmp(mode, "GRID")) imode = 2;
447 if (!gSystem->Getenv("ALICE_ROOT")) {
a42c0df6 448 ::Error("AnalysisTrainNew.C::LoadCommonLibraries", "Analysis train requires that analysis libraries are compiled with a local AliRoot");
c6e7c68c 449 return kFALSE;
450 }
451 Bool_t success = kTRUE;
452 // ROOT libraries
453 gSystem->Load("libTree.so");
454 gSystem->Load("libGeom.so");
455 gSystem->Load("libVMC.so");
456 gSystem->Load("libPhysics.so");
457
458 // Load framework classes. Par option ignored here.
459 switch (imode) {
460 case 0:
461 case 2:
7581b1bc 462 if (useCPAR) {
463 success &= LoadLibrary("STEERBase", mode, kTRUE);
464 success &= LoadLibrary("ESD", mode, kTRUE);
465 success &= LoadLibrary("AOD", mode, kTRUE);
466 success &= LoadLibrary("ANALYSIS", mode, kTRUE);
467 success &= LoadLibrary("ANALYSISalice", mode, kTRUE);
468 if (useCORRFW) success &= LoadLibrary("CORRFW", mode, kTRUE);
469 } else {
470 success &= LoadLibrary("libSTEERBase.so", mode);
471 success &= LoadLibrary("libESD.so", mode);
472 success &= LoadLibrary("libAOD.so", mode);
473 success &= LoadLibrary("libANALYSIS.so", mode);
474 success &= LoadLibrary("libANALYSISalice.so", mode);
475 if (useCORRFW) success &= LoadLibrary("libCORRFW.so", mode);
476 gROOT->ProcessLine(".include $ALICE_ROOT/include");
477 }
c6e7c68c 478 break;
479 case 1:
480 Int_t ires = -1;
a42c0df6 481 if (useAFPAR && !gSystem->AccessPathName(AFversion)) ires = gProof->UploadPackage(AFversion);
c6e7c68c 482 if (ires < 0) {
483 success &= LoadLibrary("STEERBase", mode);
484 success &= LoadLibrary("ESD", mode);
485 success &= LoadLibrary("AOD", mode);
486 success &= LoadLibrary("ANALYSIS", mode);
487 success &= LoadLibrary("ANALYSISalice", mode);
488 if (useCORRFW) success &= LoadLibrary("CORRFW", mode);
489 } else {
490 ires = gProof->EnablePackage(AFversion);
2b142d4b 491 if (ires<0) success = kFALSE;
c6e7c68c 492 if (useCORRFW) success &= LoadLibrary("CORRFW", mode);
493 }
c6e7c68c 494 break;
495 default:
a42c0df6 496 ::Error("AnalysisTrainNew.C::LoadCommonLibraries", "Unknown run mode: %s", mode);
c6e7c68c 497 return kFALSE;
498 }
499 if (success) {
a42c0df6 500 ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Load common libraries: SUCCESS");
501 ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Include path for Aclic compilation:\n%s",
c6e7c68c 502 gSystem->GetIncludePath());
503 } else {
a42c0df6 504 ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Load common libraries: FAILED");
c6e7c68c 505 }
506
507 return success;
508}
509
510//______________________________________________________________________________
511Bool_t LoadAnalysisLibraries(const char *mode)
512{
513// Load common analysis libraries.
514 Bool_t success = kTRUE;
099d8f33 515 if (iESDfilter) {
c6e7c68c 516 if (!LoadLibrary("PWG3base", mode, kTRUE) ||
517 !LoadLibrary("PWG3muon", mode, kTRUE)) return kFALSE;
518 }
519 // JETAN
520 if (iJETAN) {
521 if (!LoadLibrary("JETAN", mode, kTRUE)) return kFALSE;
522 }
523
524 // PWG4 particle correlations
274ff025 525 if (iPWG4partcorr) {
c6e7c68c 526 if (!LoadLibrary("PWG4PartCorrBase", mode, kTRUE) ||
527 !LoadLibrary("PWG4PartCorrDep", mode, kTRUE)) return kFALSE;
c6e7c68c 528 }
529 // PWG2 task protons
530 if (iPWG2spectra) {
531 if (!LoadLibrary("PWG2spectra", mode, kTRUE)) return kFALSE;
532 }
533 // PWG2 flow
534 if (iPWG2flow) {
535 if (!LoadLibrary("PWG2AOD", mode, kTRUE) ||
536 !LoadLibrary("PWG2flow", mode, kTRUE)) return kFALSE;
537 }
538 // PWG2 resonances
539 if (iPWG2res) {
540 if (!LoadLibrary("PWG2resonances", mode, kTRUE)) return kFALSE;
541 }
081194a7 542 // PWG2 kink
543 if (iPWG2kink) {
544 if (!LoadLibrary("PWG2kink", mode, kTRUE)) return kFALSE;
545 }
c6e7c68c 546 // PWG2 femtoscopy
547 if (iPWG2femto) {
548 if (!LoadLibrary("PWG2AOD", mode, kTRUE) ||
549 !LoadLibrary("PWG2femtoscopy", mode, kTRUE) ||
550 !LoadLibrary("PWG2femtoscopyUser", mode, kTRUE)) return kFALSE;
a42c0df6 551 TFile::Cp(gSystem->ExpandPathName("$(ALICE_ROOT)/PWG2/FEMTOSCOPY/macros/ConfigFemtoAnalysis.C"), Form("%s/ConfigFemtoAnalysis.C", train_name.Data()));
552 anaLibs += "ConfigFemtoAnalysis.C ";
c6e7c68c 553 }
554 // Vertexing HF
555 if (iPWG3vertexing) {
15dac83a 556 if (!LoadLibrary("PWG3base", mode, kTRUE) ||
557 !LoadLibrary("PWG3vertexingHF", mode, kTRUE)) return kFALSE;
c6e7c68c 558 }
a42c0df6 559 ::Info("AnalysisTrainNew.C::LoadAnalysisLibraries", "Load other libraries: SUCCESS");
c6e7c68c 560 return kTRUE;
561}
562
563//______________________________________________________________________________
564Bool_t LoadLibrary(const char *module, const char *mode, Bool_t rec=kFALSE)
565{
566// Load a module library in a given mode. Reports success.
567 Int_t imode = -1;
568 Int_t result;
569 TString smodule(module);
570 if (!strcmp(mode, "LOCAL")) imode = 0;
571 if (!strcmp(mode, "PROOF")) imode = 1;
572 if (!strcmp(mode, "GRID")) imode = 2;
573 TString mod(module);
574 if (!mod.Length()) {
a42c0df6 575 ::Error("AnalysisTrainNew.C::LoadLibrary", "Empty module name");
c6e7c68c 576 return kFALSE;
577 }
578 // If a library is specified, just load it
579 if (smodule.EndsWith(".so")) {
580 mod.Remove(mod.Index(".so"));
581 result = gSystem->Load(mod);
582 if (result < 0) {
a42c0df6 583 ::Error("AnalysisTrainNew.C::LoadLibrary", "Could not load library %s", module);
c6e7c68c 584 return kFALSE;
585 }
586 if (rec) anaLibs += Form("%s.so ",mod.Data());
587 return kTRUE;
588 }
589 // Check if the library is already loaded
590 if (strlen(gSystem->GetLibraries(Form("%s.so", module), "", kFALSE)) > 0)
591 return kTRUE;
592 switch (imode) {
593 case 0:
594 case 2:
595 if (usePAR) {
596 result = SetupPar(module);
597 if (rec) anaPars += Form("%s.par ", module);
598 } else {
599 result = gSystem->Load(Form("lib%s.so", module));
600 if (rec) anaLibs += Form("lib%s.so ", module);
601 }
602 break;
603 case 1:
604 result = gProof->UploadPackage(module);
605 if (result<0) {
606 result = gProof->UploadPackage(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", module)));
607 if (result<0) {
a42c0df6 608 ::Error("AnalysisTrainNew.C::LoadLibrary", "Could not find module %s.par in current directory nor in $ALICE_ROOT", module);
c6e7c68c 609 return kFALSE;
610 }
611 }
612 result = gProof->EnablePackage(module);
613 break;
614 default:
615 return kFALSE;
616 }
617 if (result < 0) {
a42c0df6 618 ::Error("AnalysisTrainNew.C::LoadLibrary", "Could not load module %s", module);
c6e7c68c 619 return kFALSE;
620 }
621 return kTRUE;
622}
623
624
625//______________________________________________________________________________
626TChain *CreateChain(const char *mode, const char *plugin_mode)
627{
628// Create the input chain
629 Int_t imode = -1;
630 if (!strcmp(mode, "LOCAL")) imode = 0;
631 if (!strcmp(mode, "PROOF")) imode = 1;
632 if (!strcmp(mode, "GRID")) imode = 2;
633 TChain *chain = NULL;
634 // Local chain
635 switch (imode) {
636 case 0:
637 if (iAODanalysis) {
a42c0df6 638 if (!local_xmldataset.Length()) {
c6e7c68c 639 // Local AOD
640 chain = new TChain("aodTree");
274ff025 641 if (gSystem->AccessPathName("data/AliAOD.root"))
a42c0df6 642 ::Error("AnalysisTrainNew.C::CreateChain", "File: AliAOD.root not in ./data dir");
b160b576 643 else {
644 if (!saveTrain) chain->Add("data/AliAOD.root");
645 else chain->Add("../data/AliAOD.root");
646 }
c6e7c68c 647 } else {
648 // Interactive AOD
a42c0df6 649 chain = CreateChainSingle(local_xmldataset, "aodTree");
c6e7c68c 650 }
651 } else {
a42c0df6 652 if (!local_xmldataset.Length()) {
c6e7c68c 653 // Local ESD
654 chain = new TChain("esdTree");
099d8f33 655 if (gSystem->AccessPathName("data/AliESDs.root"))
a42c0df6 656 ::Error("AnalysisTrainNew.C::CreateChain", "File: AliESDs.root not in ./data dir");
b160b576 657 else {
658 if (!saveTrain) chain->Add("data/AliESDs.root");
659 else chain->Add("../data/AliESDs.root");
660 }
c6e7c68c 661 } else {
662 // Interactive ESD
a42c0df6 663 chain = CreateChainSingle(local_xmldataset, "esdTree");
c6e7c68c 664 }
665 }
666 break;
667 case 1:
668 break;
669 case 2:
670 if (usePLUGIN) {
671 AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
672 AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
673 } else {
674 TString treeName = "esdTree";
675 if (iAODanalysis) treeName = "aodTree";
676 chain = CreateChainSingle("wn.xml", treeName);
677 }
678 break;
679 default:
680 }
681 if (chain && chain->GetNtrees()) return chain;
682 return NULL;
683}
684
685//______________________________________________________________________________
686TChain* CreateChainSingle(const char* xmlfile, const char *treeName)
687{
688 printf("*******************************\n");
689 printf("*** Getting the ESD Chain ***\n");
690 printf("*******************************\n");
691 TAlienCollection * myCollection = TAlienCollection::Open(xmlfile);
692
693 if (!myCollection) {
a42c0df6 694 ::Error("AnalysisTrainNew.C::CreateChainSingle", "Cannot create an AliEn collection from %s", xmlfile) ;
c6e7c68c 695 return NULL ;
696 }
697
698 TChain* chain = new TChain(treeName);
699 myCollection->Reset() ;
700 while ( myCollection->Next() ) chain->Add(myCollection->GetTURL("")) ;
701 chain->ls();
702 return chain;
703}
704
705//______________________________________________________________________________
706Int_t SetupPar(char* pararchivename)
707{
708 if (!pararchivename || !strlen(pararchivename)) return -1;
709 char processline[1024];
710 if (gSystem->AccessPathName(Form("%s.par", pararchivename))) {
711 if (!gSystem->AccessPathName(Form("%s/%s.par", gSystem->Getenv("ALICE_ROOT"),pararchivename))) {
a42c0df6 712 ::Info("AnalysisTrainNew.C::SetupPar", "Getting %s.par from $ALICE_ROOT", pararchivename);
c6e7c68c 713 TFile::Cp(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", pararchivename)),
714 Form("%s.par",pararchivename));
715 } else {
a42c0df6 716 ::Error("AnalysisTrainNew.C::SetupPar", "Cannot find %s.par", pararchivename);
c6e7c68c 717 return -1;
718 }
719 }
a42c0df6 720 if (usePLUGIN && saveTrain) gSystem->Exec(Form("ln -s ../%s.par %s",pararchivename, train_name.Data()));
c6e7c68c 721 gSystem->Exec(Form("tar xvzf %s.par", pararchivename));
722
723 TString ocwd = gSystem->WorkingDirectory();
724 if (!gSystem->ChangeDirectory(pararchivename)) return -1;
725
726 // check for BUILD.sh and execute
727 if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
728 printf("*******************************\n");
729 printf("*** Building PAR archive ***\n");
730 printf("*******************************\n");
731 if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
732 Error("runProcess","Cannot Build the PAR Archive! - Abort!");
733 return -1;
734 }
735 }
736
737 // check for SETUP.C and execute
738 if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
739 printf("*******************************\n");
740 printf("*** Setup PAR archive ***\n");
741 printf("*******************************\n");
742 gROOT->Macro("PROOF-INF/SETUP.C");
743 }
744 if (!gSystem->ChangeDirectory(ocwd.Data())) return -1;
745 return 0;
746}
747
748//______________________________________________________________________________
749AliAnalysisGrid* CreateAlienHandler(const char *plugin_mode)
750{
751// Check if user has a valid token, otherwise make one. This has limitations.
752// One can always follow the standard procedure of calling alien-token-init then
753// source /tmp/gclient_env_$UID in the current shell.
754 if (!AliAnalysisGrid::CreateToken()) return NULL;
755 AliAnalysisAlien *plugin = new AliAnalysisAlien();
756// Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
757 plugin->SetRunMode(plugin_mode);
758 plugin->SetNtestFiles(1);
a42c0df6 759 plugin->SetPreferedSE("ALICE::NIHAM::FILE");
c6e7c68c 760// Set versions of used packages
761 plugin->SetAPIVersion("V2.4");
762 plugin->SetROOTVersion(root_version);
763 plugin->SetAliROOTVersion(aliroot_version);
764// Declare input data to be processed.
765// Method 1: Create automatically XML collections using alien 'find' command.
766// Define production directory LFN
767 plugin->SetGridDataDir(alien_datadir);
768// Set data search pattern
274ff025 769 if (iAODanalysis) plugin->SetDataPattern("*AliAOD.root");
770 else plugin->SetDataPattern("*AliESDs.root");
c6e7c68c 771// ...then add run numbers to be considered
772 for (Int_t i=0; i<10; i++) {
773 if (run_numbers[i]==0) break;
774 plugin->AddRunNumber(run_numbers[i]);
775 }
776// Method 2: Declare existing data files (raw collections, xml collections, root file)
777// If no path mentioned data is supposed to be in the work directory (see SetGridWorkingDir())
778// XML collections added via this method can be combined with the first method if
779// the content is compatible (using or not tags)
780// plugin->AddDataFile("tag.xml");
781// plugin->AddDataFile("/alice/data/2008/LHC08c/000057657/raw/Run57657.Merged.RAW.tag.root");
782// Define alien work directory where all files will be copied. Relative to alien $HOME.
274ff025 783 if (iAODanalysis) plugin->SetGridWorkingDir("analysisAOD");
784 else plugin->SetGridWorkingDir("analysisESD");
c6e7c68c 785// Declare alien output directory. Relative to working directory.
a42c0df6 786 plugin->SetGridOutputDir(Form("output_%s",train_name.Data())); // In this case will be $HOME/work/output
c6e7c68c 787
788 TString ana_sources = "";
789 TString ana_add = "";
790 if (usePAR && anaPars.Length()) {
7581b1bc 791 printf("%s\n", anaPars.Data());
c6e7c68c 792 TObjArray *arr;
793 TObjString *objstr;
794 arr = anaPars.Tokenize(" ");
795 TIter next(arr);
796 while ((objstr=(TObjString*)next())) plugin->EnablePackage(objstr->GetString());
797 delete arr;
798 }
799
800// Declare the analysis source files names separated by blancs. To be compiled runtime
801// using ACLiC on the worker nodes.
802 ana_sources = ana_sources.Strip();
803// Declare all libraries (other than the default ones for the framework. These will be
804// loaded by the generated analysis macro. Add all extra files (task .cxx/.h) here.
805 anaLibs = anaLibs.Strip();
806 if (ana_sources.Length()) plugin->SetAnalysisSource(ana_sources);
807 if (anaLibs.Length()) plugin->SetAdditionalLibs(anaLibs);
808
809// Declare the output file names separated by blancs.
810// (can be like: file.root or file.root@ALICE::Niham::File)
811 plugin->SetDefaultOutputs();
812 plugin->SetMergeExcludes("AliAOD.root");
c6e7c68c 813// Optionally define the files to be archived.
814// plugin->SetOutputArchive("log_archive.zip:stdout,stderr@ALICE::NIHAM::File root_archive.zip:*.root@ALICE::NIHAM::File");
815 plugin->SetOutputArchive("log_archive.zip:stdout,stderr");
816// Optionally set a name for the generated analysis macro (default MyAnalysis.C)
a42c0df6 817 plugin->SetAnalysisMacro(Form("%s.C", train_name.Data()));
c6e7c68c 818// Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
a42c0df6 819 plugin->SetSplitMaxInputFileNumber(100);
c6e7c68c 820// Optionally set number of failed jobs that will trigger killing waiting sub-jobs.
821// plugin->SetMaxInitFailed(5);
822// Optionally resubmit threshold.
823// plugin->SetMasterResubmitThreshold(90);
824// Optionally set time to live (default 30000 sec)
825 plugin->SetTTL(30000);
826// Optionally set input format (default xml-single)
827 plugin->SetInputFormat("xml-single");
828// Optionally modify the name of the generated JDL (default analysis.jdl)
a42c0df6 829 plugin->SetJDLName(Form("%s.jdl", train_name.Data()));
7ceeacc7 830// Optionally modify the executable name (default analysis.sh)
a42c0df6 831 plugin->SetExecutable(Form("%s.sh", train_name.Data()));
c6e7c68c 832// Optionally modify job price (default 1)
833 plugin->SetPrice(1);
834// Optionally modify split mode (default 'se')
835 plugin->SetSplitMode("se");
836 return plugin;
837}
a42c0df6 838
839//______________________________________________________________________________
840void WriteConfig()
841{
842// Write train configuration in a file. The file name has the format:
843// train_[trainName]_ddMonthyyyy_time.C
844 gSystem->Exec("date +%d%b%Y_%Hh%M > date.tmp");
845 ifstream fdate("date.tmp");
846 if (!fdate.is_open()) {
847 ::Error("AnalysisTrainNew.C::Export","Could not generate file name");
848 return;
849 }
850 const char date[64];
851 fdate.getline(date,64);
852 fdate.close();
853 gSystem->Exec("rm date.tmp");
854 train_name = Form("train_%s_%s", train_name.Data(), date);
855 TString cdir = gSystem->WorkingDirectory();
856 gSystem->MakeDirectory(train_name);
857 gSystem->ChangeDirectory(train_name);
858 ofstream out;
859 out.open("ConfigTrain.C", ios::out);
860 if (out.bad()) {
861 ::Error("AnalysisTrainNew.C::Export", "Cannot open ConfigTrain.C for writing");
862 return;
863 }
864 out << "{" << endl;
865 out << " train_name = " << "\"" << train_name.Data() << "\";" << endl;
866 out << " proof_cluster = " << "\"" << proof_cluster.Data() << "\";" << endl;
867 out << " useAFPAR = " << useAFPAR << ";" << endl;
868 if (useAFPAR)
869 out << " AFversion = " << AFversion.Data() << ";" << endl;
870 out << " proof_dataset = " << "\"" << proof_dataset.Data() << "\";" << endl;
871 out << " usePLUGIN = " << usePLUGIN << ";" << endl;
872 out << " usePAR = " << usePAR << ";" << endl;
873 out << " useCPAR = " << useCPAR << ";" << endl;
874 out << " root_version = " << "\"" << root_version.Data() << "\";" << endl;
875 out << " aliroot_version = " << "\"" << aliroot_version.Data() << "\";" << endl;
876 out << " alien_datadir = " << "\"" << alien_datadir.Data() << "\";" << endl;
877 for (Int_t i=0; i<10; i++) {
878 if (run_numbers[i])
879 out << " run_numbers[" << i << "] = " << run_numbers[i] << ";" << endl;
880 }
881 out << " useDBG = " << useDBG << ";" << endl;
882 out << " useMC = " << useMC << ";" << endl;
883 out << " useTAGS = " << useTAGS << ";" << endl;
884 out << " useKFILTER = " << useKFILTER << ";" << endl;
885 out << " useTR = " << useTR << ";" << endl;
886 out << " useCORRFW = " << useCORRFW << ";" << endl;
887 out << " useAODTAGS = " << useAODTAGS << ";" << endl;
888 out << " saveTrain = " << "kFALSE;" << endl << endl;
889 out << " // Analysis modules" << endl;
890 out << " iAODanalysis = " << iAODanalysis << ";" << endl;
891 out << " iAODhandler = " << iAODhandler << ";" << endl;
892 out << " iESDfilter = " << iESDfilter << ";" << endl;
893 out << " iMUONcopyAOD = " << iMUONcopyAOD << ";" << endl;
894 out << " iJETAN = " << iJETAN << ";" << endl;
895 out << " iPWG4partcorr = " << iPWG4partcorr << ";" << endl;
896 out << " iPWG2femto = " << iPWG2femto << ";" << endl;
897 out << " iPWG2spectra = " << iPWG2spectra << ";" << endl;
898 out << " iPWG2flow = " << iPWG2flow << ";" << endl;
899 out << " iPWG2res = " << iPWG2res << ";" << endl;
900 out << " iPWG2kink = " << iPWG2kink << ";" << endl;
901 out << "}" << endl;
902 ::Info("AnalysisTrainNew.C::WriteConfig", "Train configuration wrote to file %s", Form("config_%s.C", train_name.Data()));
903 gSystem->ChangeDirectory(cdir);
904}
905
906//______________________________________________________________________________
907Bool_t LoadConfig(const char *filename)
908{
909// Read train configuration from file
910 if (gSystem->AccessPathName(filename)) {
911 ::Error("AnalysisTrainNew.C::LoadConfig", "Config file name not found");
912 return kFALSE;
913 }
914 gROOT->ProcessLine(Form(".x %s", filename));
915 ::Info("AnalysisTrainNew.C::LoadConfig", "Train configuration loaded from file %s", filename);
916 return kTRUE;
917}