]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/macros/AnalysisTrainNew.C
New train version containing gamma conversion task (PWG4)
[u/mrichter/AliRoot.git] / ANALYSIS / macros / AnalysisTrainNew.C
1 //===================== ANALYSIS TRAIN =========================================
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")
9 // Local mode requires AliESds.root or AliAOD.root in ./data directory
10 //    root[3] AnalysisTrainNew("local")
11 // In proof and grid modes, a token is needed and sourcing the produced environment file.
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")
17
18 //==================   TRAIN NAME   ============================================
19 //TString     train_name         = "GAMMAtest"; // *CHANGE ME* (no blancs or special characters)
20 TString     train_name         = "LHC09b4AOD"; // *CHANGE ME* (no blancs or special characters)
21 TString     job_tag            = "LHC09b4: standard AODs with MC info, AOD tags and pyxsec_hist"; // *CHANGE ME*
22 //==============================================================================
23
24 // ### Settings that make sense in PROOF only
25 //==============================================================================
26 TString     proof_cluster      = "alicecaf.cern.ch";
27 Bool_t      useAFPAR           = kFALSE;  // use AF special par file
28 TString     AFversion          = "AF-v4-16";
29 // Change CAF dataset here
30 TString     proof_dataset      = "/COMMON/COMMON/LHC09a4_run8100X#/esdTree";
31 TString     proof_outdir       = "";
32
33 // ### Settings that make sense when using the Alien plugin
34 //==============================================================================
35 Bool_t      usePLUGIN          = kTRUE;   // do not change
36 Bool_t      useProductionMode  = kTRUE;   // use the plugin in production mode
37 // Usage of par files ONLY in grid mode and ONLY if the code is not available
38 // in the deployed AliRoot versions. Par file search path: local dir, if not there $ALICE_ROOT.
39 // To refresh par files, remove the ones in the workdir, then do "make <target.par>" in 
40 // AliRoot.
41 Bool_t      usePAR             = kFALSE;  // use par files for extra libs
42 Bool_t      useCPAR            = kFALSE;  // use par files for common libs
43 TString     root_version       = "v5-24-00";  // *CHANGE ME IF MORE RECENT IN GRID*
44 TString     aliroot_version    = "v4-18-03-AN";  // *CHANGE ME IF MORE RECENT IN GRID*                                          
45 // Change production base directory here
46 TString     alien_datadir      = "/alice/sim/LHC09b4/";
47 // AliEn output directory. If blank will become output_<train_name>
48 TString     alien_outdir       = "/alice/sim/LHC09b4/AOD";
49 // TString     alien_outdir       = "";
50 // Number of files merged in a chunk
51 Int_t       maxMergeFiles      = 50;
52 // Files that should not be merged
53 TString     mergeExclude       = "AliAOD.root AliAOD.VertexingHF.root";
54 // Files that should be archived
55 TString     outputArchive      = "aod_archive.zip:AliAOD.root,AOD.tag.root,pyxsec_hists.root@ALICE::NIHAM::File";
56 // Number of runs per master job
57 Int_t       nRunsPerMaster     = 1;
58 // Maximum number of files per job (gives size of AOD)
59 Int_t       nFilesPerJob       = 100;
60 // Set the run range
61 Int_t       run_range[2]       =  {117003, 117019};  // LHC09a4   *CHANGE ME*
62 //Int_t       run_range[2]       =  {90000, 90040}; // LHC09a5
63 // ### Settings that make sense only for local analysis
64 //==============================================================================
65 // Change local xml dataset for local interactive analysis
66 TString     local_xmldataset   = "";
67
68 // ### Other flags to steer the analysis
69 //==============================================================================
70 Bool_t      useDATE            = kFALSE; // use date in train name
71 Bool_t      useDBG             = kTRUE; // activate debugging
72 Bool_t      useMC              = kTRUE;  // use MC info
73 Bool_t      useTAGS            = kFALSE; // use ESD tags for selection
74 Bool_t      useKFILTER         = kTRUE;  // use Kinematics filter
75 Bool_t      useTR              = kTRUE;  // use track references
76 Bool_t      useCORRFW          = kFALSE; // do not change
77 Bool_t      useAODTAGS         = kTRUE;  // use AOD tags
78 Bool_t      saveTrain          = kTRUE;  // save train configuration as: 
79 Bool_t      saveProofToAlien   = kFALSE; // save proof outputs in AliEn
80                                          // train_[trainName]_ddMonthyyyy_time.C
81 // ### Analysis modules to be included. Some may not be yet fully implemented.
82 //==============================================================================
83 Int_t       iAODanalysis       = 0;      // Analysis on input AOD's
84 Int_t       iAODhandler        = 1;      // Analysis produces an AOD or dAOD's
85 Int_t       iESDfilter         = 1;      // ESD to AOD filter (barrel + muon tracks)
86 Int_t       iMUONcopyAOD       = 0;      // Task that copies only muon events in a separate AOD (PWG3)
87 Int_t       iJETAN             = 0;      // Jet analysis (PWG4) - needs ESD filter
88 Int_t       iPWG4partcorr      = 0;      // Gamma-hadron correlations task (PWG4)
89 Int_t       iPWG4gammaconv     = 0;      // Gamma conversion analysis (PWG4)  # NOT YET WORKING - WAITING FOR FEEDBACK #
90 Int_t       iPWG4omega3pi      = 0;      // Omega to 3 pi analysis (PWG4)
91 Int_t       iPWG3vertexing     = 0;      // Vertexing HF task (PWG2)
92 Int_t       iPWG2femto         = 0;      // Femtoscopy task (PWG2)
93 Int_t       iPWG2spectra       = 0;      // Spectra tasks (PWG2
94 Int_t        iPWG2protons      = 0;         // Proton-antiproton analysis
95 Int_t        iPWG2checkcascade = 0;         // Check cascades task
96 Int_t        iPWG2perfcascade  = 0;         // Check performance cascade
97 Int_t        iPWG2checkv0      = 0;         // Check V0 task
98 Int_t        iPWG2strange      = 0;         // Strangeness task
99 Int_t       iPWG2flow          = 0;      // Flow analysis tasks (PWG2)   = crashes in AliCFManager
100 Int_t       iPWG2res           = 0;      // Resonances task (PWG2)
101 Int_t       iPWG2kink          = 0;      // Kink analysis tasks (PWG2)
102 Int_t        iPWG2kinkESDMC    = 0;         // Kink ESD-MC comparison (PWG2)
103 Int_t        iPWG2kinkres      = 0;         // Kink resonances (PWG2)
104 Int_t        iPWG2kinklikesign = 0;         // Kink like-sign (PWG2)
105 Int_t       iPWG2evchar        = 0;      // Event characteristics (PWG2)
106 Int_t       iPWG2unicor        = 0;      // Unicor analysis (PWG2)
107
108 // Temporaries.
109 TString anaPars = "";
110 TString anaLibs = "";
111 // Function signatures
112 class AliAnalysisAlien;
113
114 //______________________________________________________________________________
115 void AnalysisTrainNew(const char *analysis_mode="grid", 
116                       const char *plugin_mode="full",
117                       const char *config_file="")
118 {
119 // Main analysis train macro. If a configuration file is provided, all parameters
120 // are taken from there but may be altered by CheckModuleFlags.
121    if (strlen(config_file) && !LoadConfig(config_file)) return;
122    TString smode(analysis_mode);
123    smode.ToUpper();
124    if (saveTrain)              WriteConfig();
125    // Check compatibility of selected modules
126    CheckModuleFlags(smode);
127
128    printf("==================================================================\n");
129    printf("===========    RUNNING ANALYSIS TRAIN %s IN %s MODE   ==========\n", train_name.Data(),smode.Data());
130    printf("==================================================================\n");
131    printf("=  Configuring analysis train for:                               =\n");
132    if (iAODanalysis) printf("=  AOD analysis                                                  =\n");
133    else              printf("=  ESD analysis                                                  =\n");
134    if (iESDfilter)   printf("=  ESD filter                                                    =\n");
135    if (iMUONcopyAOD) printf("=  MUON copy AOD                                                 =\n");
136    if (iJETAN)       printf("=  Jet analysis                                                  =\n");
137    if (iPWG2spectra) {
138       printf("=  PWG2 SPECTRA tasks :                                          =\n");
139       if (iPWG2protons)      printf("=     PWG2 proton-antiproton                                     =\n");
140       if (iPWG2checkcascade) printf("=     PWG2 check cascades                                        =\n");
141       if (iPWG2perfcascade)  printf("=     PWG2 performance cascades                                  =\n");
142       if (iPWG2checkv0)      printf("=     PWG2 check V0                                              =\n");
143       if (iPWG2strange)      printf("=     PWG2 strangeness                                           =\n");
144    }   
145    if (iPWG2femto)   printf("=  PWG2 femtoscopy                                               =\n");
146    if (iPWG2flow)    printf("=  PWG2 flow                                                     =\n");
147    if (iPWG2res)     printf("=  PWG2 resonances                                               =\n");
148    if (iPWG2kink) {
149       printf("=  PWG2 kink analysis tasks:                                     =\n");
150       if (iPWG2kinkESDMC)    printf("=     PWG2 ESD-MC kinks                                          =\n");
151       if (iPWG2kinkres)      printf("=     PWG2 kink resonances                                       =\n");
152       if (iPWG2kinklikesign) printf("=     PWG2 kink like-sign analysis                               =\n");
153    }   
154    if (iPWG2evchar)    printf("=  PWG2 event characteristics                                =\n");
155    if (iPWG2unicor)    printf("=  PWG2 Unicor analysis                                      =\n");
156    if (iPWG3vertexing) printf("=  PWG3 vertexing                                            =\n");
157    if (iPWG4partcorr)  printf("=  PWG4 gamma-hadron, pi0 and gamma-jet correlations         =\n");
158    if (iPWG4gammaconv) printf("=  PWG4 gamma conversion                                     =\n");
159    if (iPWG4omega3pi)  printf("=  PWG4 omega to 3 pions                                     =\n");
160    printf("==================================================================\n");
161    printf(":: use MC truth      %d\n", (UInt_t)useMC);
162    printf(":: use KINE filter   %d\n", (UInt_t)useKFILTER);
163    printf(":: use track refs    %d\n", (UInt_t)useTR);
164    printf(":: use tags          %d\n", (UInt_t)useTAGS);
165    printf(":: use AOD tags      %d\n", (UInt_t)useAODTAGS);
166    printf(":: use debugging     %d\n", (UInt_t)useDBG);
167    printf(":: use PAR files     %d\n", (UInt_t)usePAR);
168    printf(":: use AliEn plugin  %d\n", (UInt_t)usePLUGIN);
169
170    //==========================================================================
171    // Connect to back-end system
172    if (!Connect(smode)) {
173       ::Error("AnalysisTrain", "Could not connect to %s back-end", analysis_mode);
174       return;
175    }   
176
177    // Load common libraries and set include path
178    if (!LoadCommonLibraries(smode)) {
179       ::Error("AnalysisTrain", "Could not load common libraries");
180       return;
181    }
182     
183    // Make the analysis manager and connect event handlers
184    AliAnalysisManager *mgr  = new AliAnalysisManager("Analysis Train", "Production train");
185    if (saveProofToAlien) mgr->SetSpecialOutputLocation(proof_outdir);
186
187    // Load analysis specific libraries
188    if (!LoadAnalysisLibraries(smode)) {
189       ::Error("AnalysisTrain", "Could not load analysis libraries");
190       return;
191    }   
192
193    // Create input handler (input container created automatically)
194    if (iAODanalysis) {
195    // AOD input handler
196       AliAODInputHandler *aodH = new AliAODInputHandler();
197       mgr->SetInputEventHandler(aodH);
198    } else {   
199    // ESD input handler
200       AliESDInputHandler *esdHandler = new AliESDInputHandler();
201       if (useTAGS) esdHandler->SetReadTags();
202       mgr->SetInputEventHandler(esdHandler);       
203    }
204    // Monte Carlo handler
205    if (useMC && !iAODanalysis) {
206       AliMCEventHandler* mcHandler = new AliMCEventHandler();
207       mgr->SetMCtruthEventHandler(mcHandler);
208       mcHandler->SetReadTR(useTR); 
209    }   
210    // AOD output container, created automatically when setting an AOD handler
211    if (iAODhandler) {
212       // AOD output handler
213       AliAODHandler* aodHandler   = new AliAODHandler();
214       aodHandler->SetOutputFileName("AliAOD.root");
215       mgr->SetOutputEventHandler(aodHandler);
216       if (iAODanalysis) {
217          aodHandler->SetCreateNonStandardAOD();
218          if (iJETAN)         aodHandler->SetOutputFileName("AliAOD.Jets.root");
219          if (iPWG3vertexing) aodHandler->SetOutputFileName("AliAOD.VertexingHF.root");
220       } 
221    }
222    // Debugging if needed
223    if (useDBG) mgr->SetDebugLevel(3);
224
225    //==========================================================================
226    // Create the chain. In this example it is created only from ALIEN files but
227    // can be done to work in batch or grid mode as well.
228    TChain *chain = CreateChain(smode, plugin_mode);
229         
230    //==========================================================================
231    // Load the tasks configuration macros for all wagons. These files are supposed now to be
232    // in the current workdir, but in AliEn they will be in the file catalog, 
233    // mapped from AliRoot and pecified in the jdl input list.
234     
235    // For now connection to top input container and common AOD output container
236    // is done in this macro, but in future these containers will be connected
237    // from each task configuration macro.
238
239    if (iESDfilter && !iAODanalysis) {
240       //  ESD filter task configuration.
241       gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskESDFilter.C");
242       AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(useKFILTER);
243    }   
244
245    // AOD tags
246    if (useAODTAGS) {
247       AliAnalysisTaskTagCreator* tagTask = new AliAnalysisTaskTagCreator("AOD Tag Creator");
248       mgr->AddTask(tagTask);
249       AliAnalysisDataContainer *coutTags = mgr->CreateContainer("cTag",  TTree::Class(), 
250                                            AliAnalysisManager::kOutputContainer, "AOD.tag.root");
251       mgr->ConnectInput (tagTask, 0, mgr->GetCommonInputContainer());
252       mgr->ConnectOutput(tagTask, 1, coutTags);
253    }   
254     
255     // Jet analysis
256    if (iJETAN) {
257       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJets.C");
258       AliAnalysisTaskJets *taskjets = 0;
259       TString anatype = mgr->GetInputEventHandler()->GetDataType();
260       taskjets = AddTaskJets(anatype, "UA1");
261       taskjets = AddTaskJets(anatype, "Fastjet");
262       if (!taskjets) ::Warning("AnalysisTrainNew", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
263    }
264        
265    // Proton analysis
266    if (iPWG2spectra) {
267       // protons
268       if (iPWG2protons) {
269         gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskProtons.C");
270         AliAnalysisTaskProtons *taskprotons = AddTaskProtons();
271         if (!taskprotons) ::Warning("AnalysisTrainNew", "AliAnalysisTaskProtons cannot run for this train conditions - EXCLUDED");
272       }  
273       // cascades
274       if (iPWG2checkcascade) {
275         gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckCascade.C");
276         AliAnalysisTaskCheckCascade *taskcheckcascade = AddTaskCheckCascade();      
277         if (!taskcheckcascade) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckCascade cannot run for this train conditions - EXCLUDED");
278       }  
279       // v0's
280       if (iPWG2checkv0) {
281         gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckV0.C");
282         AliAnalysisTaskCheckV0 *taskcheckV0 = AddTaskCheckV0();
283         if (!taskcheckV0) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckV0 cannot run for this train conditions - EXCLUDED");
284       }  
285       // strangeness
286       if (iPWG2strange) {
287         gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskStrange.C");
288         AliAnalysisTaskStrange *taskstrange = AddTaskStrange();
289         if (!taskstrange) ::Warning("AnalysisTrainNew", "AliAnalysisTaskStrange cannot run for this train conditions - EXCLUDED");
290       }  
291       // performance cascades
292       if (iPWG2perfcascade) {
293         gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckPerformanceCascade.C");
294         AliAnalysisTaskCheckPerformanceCascade *taskperfcascade = AddTaskCheckPerformanceCascade();
295         if (!taskperfcascade) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckPerformanceCascade cannot run for this train conditions - EXCLUDED");
296       }  
297    }   
298    
299    // Femtoscopy analysis modules
300    if (iPWG2femto) {
301       gROOT->LoadMacro("$ALICE_ROOT/PWG2/FEMTOSCOPY/macros/AddTaskFemto.C");
302       AliAnalysisTaskFemto *taskfemto = AddTaskFemto();
303       if (!taskfemto) ::Warning("AnalysisTrainNew", "AliAnalysisTaskFemto cannot run for this train conditions - EXCLUDED");
304    }   
305
306    // Kink analysis
307    if (iPWG2kink) {
308       if (iPWG2kinkESDMC) {
309         gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKink.C");
310         AliAnalysisKinkESDMC *taskkink = AddTaskKink();
311         if (!taskkink) ::Warning("AnalysisTrainNew", "AliAnalysisKinkESDMC cannot run for this train conditions - EXCLUDED");
312       }   
313       if (iPWG2kinkres) {
314         gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonance.C");
315         AliAnalysisTaskKinkResonance *taskkinkres = AddTaskKinkResonance();
316         if (!taskkinkres) ::Warning("AnalysisTrainNew", "AliAnalysisTaskKinkResonance cannot run for this train conditions - EXCLUDED");
317       }  
318       if (iPWG2kinklikesign) {
319         gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonanceLikeSign.C");
320         AliResonanceKinkLikeSign *taskkinklikesign = AddTaskKinkResonanceLikeSign();
321         if (!taskkinklikesign) ::Warning("AnalysisTrainNew", "AliResonanceKinkLikeSign cannot run for this train conditions - EXCLUDED");
322       }  
323    }   
324
325    // Event characterization
326    if (iPWG2evchar) {
327       gROOT->LoadMacro("$ALICE_ROOT/PWG2/EVCHAR/macros/AddTaskSPDdNdEta.C");
328       AliAnalysisTaskSPDdNdEta *taskspddndeta = AddTaskSPDdNdEta();
329       if (!taskspddndeta) ::Warning("AnalysisTrainNew", "AliAnalysisTaskSPDdNdEta cannot run for this train conditions - EXCLUDED");
330       taskspddndeta->SetReadMC(useMC);
331    }   
332
333    // Unicor
334    if (iPWG2unicor) {
335       gROOT->LoadMacro("$ALICE_ROOT/PWG2/UNICOR/AddTaskUnicor.C");
336       AliAnalysisTaskUnicor *taskunicor = AddTaskUnicor();
337       if (!taskspddndeta) ::Warning("AnalysisTrainNew", "AliAnalysisTaskUnicor cannot run for this train conditions - EXCLUDED");
338    }   
339
340    // Flow analysis
341    if (iPWG2flow) {
342       gROOT->LoadMacro("$ALICE_ROOT/PWG2/FLOW/macros/AddTaskFlow.C");
343       Bool_t SP       = kTRUE;
344       Bool_t LYZ1SUM  = kTRUE;
345       Bool_t LYZ1PROD = kTRUE;
346       Bool_t LYZ2SUM  = kFALSE; 
347       Bool_t LYZ2PROD = kFALSE;
348       Bool_t LYZEP    = kFALSE; 
349       Bool_t GFC      = kTRUE;
350       Bool_t QC       = kTRUE;
351       Bool_t FQD      = kFALSE;
352       Bool_t MCEP     = kFALSE; //does not work yet 24/12/08
353       Bool_t kineFromESD = kTRUE;
354       Bool_t METHODS[] = {SP,LYZ1SUM,LYZ1PROD,LYZ2SUM,LYZ2PROD,LYZEP,GFC,QC,FQD,MCEP};
355       // Analysis type can be ESD, AOD, MC, ESDMC0, ESDMC1
356       TString type = "AOD";
357       if (!iAODanalysis) type = "ESD";
358       if (useMC) {
359          type += "MC";
360          if (!kineFromESD) type += "1";
361          else type += "0";
362       }   
363       // Boolean to fill/not fill the QA histograms
364       Bool_t QA = kTRUE;   
365       // Boolean to use/not use weights for the Q vector
366       Bool_t WEIGHTS[] = {kFALSE,kFALSE,kFALSE}; //Phi, v'(pt), v'(eta)
367       AliAnalysisTaskFlowEvent* taskFE = AddTaskFlow(type,METHODS,QA,WEIGHTS);
368       if (!taskFE) ::Warning("AnalysisTrainNew", "AliAnalysisTaskFlowEvent cannot run for this train conditions - EXCLUDED");
369    }   
370
371    // PWG2 resonances
372    if (iPWG2res) {
373       gROOT->LoadMacro("$ALICE_ROOT/PWG2/RESONANCES/macros/train/AddAnalysisTaskRsn.C");
374       TString path = gROOT->GetMacroPath();
375       path += ":$ALICE_ROOT/PWG2/RESONANCES/macros/train";
376       gROOT->SetMacroPath(path);
377       AddAnalysisTaskRsn(kTRUE);
378    }   
379            
380    // PWG3 vertexing
381    if (iPWG3vertexing) {
382       gROOT->LoadMacro("$ALICE_ROOT/PWG3/vertexingHF/AddTaskVertexingHF.C");
383       AliAnalysisTaskSEVertexingHF *taskvertexingHF = AddTaskVertexingHF();
384       if (!taskvertexingHF) ::Warning("AnalysisTrainNew", "AliAnalysisTaskSEVertexingHF cannot run for this train conditions - EXCLUDED");
385    }   
386       
387    // PWG4 hadron correlations
388    if (iPWG4partcorr) {
389       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskPartCorr.C");
390       AliAnalysisTaskParticleCorrelation *taskpartcorrPHOS = AddTaskPartCorr("AOD", "PHOS");
391       if (!taskpartcorrPHOS) ::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation PHOS cannot run for this train conditions - EXCLUDED");
392       AliAnalysisTaskParticleCorrelation *taskpartcorrEMCAL = AddTaskPartCorr("AOD", "EMCAL");
393       if (!taskpartcorrEMCAL) ::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation EMCAL cannot run for this train conditions - EXCLUDED");
394    }   
395
396    // PWG4 gamma conversion analysis
397    if (iPWG4gammaconv) {
398       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskGammaConversion.C");
399       TString cdir = gSystem->WorkingDirectory();
400       gSystem->ChangeDirectory(gSystem->ExpandPathName("$ALICE_ROOT/PWG4/macros/"));
401       TString gcArguments = "-run-on-train -run-jet -run-chic -run-neutralmeson";
402       AliAnalysisTaskGammaConversion * taskGammaConversion = AddTaskGammaConversion(gcArguments,mgr->GetCommonInputContainer());
403       gSystem->ChangeDirectory(cdir);
404       if (!taskGammaConversion) ::Warning("AnalysisTrainNew", "AliAnalysisTaskGammaConversion cannot run for these train conditions - EXCLUDED");
405    }   
406
407    // PWG4 omega to 3 pions analysis
408    if (iPWG4omega3pi) {
409       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskomega3pi.C");
410       AliAnalysisTaskOmegaPi0PiPi *taskomega3pi = AddTaskomega3pi();
411       if (!taskomega3pi) ::Warning("AnalysisTrainNew", "AliAnalysisTaskomega3pi cannot run for these train conditions - EXCLUDED");
412    }   
413
414    //==========================================================================
415    // FOR THE REST OF THE TASKS THE MACRO AddTaskXXX() is not yet implemented/
416    // Run the analysis
417    //    
418    if (mgr->InitAnalysis()) {
419       mgr->PrintStatus();
420       if (saveTrain || strlen(config_file)) gSystem->ChangeDirectory(train_name);
421       StartAnalysis(smode, chain);
422       if (saveTrain && smode=="GRID") {
423          AliAnalysisAlien *gridhandler = (AliAnalysisAlien*)mgr->GetGridHandler();
424          TString alien_workdir = gGrid->GetHomeDirectory();
425          if (iAODanalysis) alien_workdir += "analysisAOD";
426          else              alien_workdir += "analysisESD";
427          alien_outdir = gridhandler->GetGridOutputDir();
428          printf("=== Registering ConfigTrain.C in the work directory <%s> ===\n",
429                 alien_workdir.Data());
430          if (AliAnalysisAlien::FileExists(Form("%s/%sConfig.C", alien_workdir.Data(), train_name.Data())))
431             gGrid->Rm(Form("%s/%sConfig.C", alien_workdir.Data(), train_name.Data()));
432          TFile::Cp(Form("file:%sConfig.C",train_name.Data()), Form("alien://%s/%sConfig.C", alien_workdir.Data(), train_name.Data()));
433       }
434    }
435 }
436
437 //______________________________________________________________________________
438 void StartAnalysis(const char *mode, TChain *chain) {
439 // Start analysis.
440    Int_t imode = -1;
441    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
442    if (!strcmp(mode, "LOCAL")) imode = 0;
443    if (!strcmp(mode, "PROOF")) imode = 1;
444    if (!strcmp(mode, "GRID"))  imode = 2;
445    switch (imode) {
446       case 0:
447          if (!chain) {
448             ::Error("AnalysisTrainNew.C::StartAnalysis", "Cannot create the chain");
449             return;
450          }   
451          mgr->StartAnalysis(mode, chain);
452          return;
453       case 1:
454          if (!proof_dataset.Length()) {
455             ::Error("AnalysisTrainNew.C::StartAnalysis", "proof_dataset is empty");
456             return;
457          }   
458          mgr->StartAnalysis(mode, proof_dataset, 1000);
459          return;
460       case 2:
461          if (usePLUGIN) {
462             if (!mgr->GetGridHandler()) {
463                ::Error("AnalysisTrainNew.C::StartAnalysis", "Grid plugin not initialized");
464                return;
465             }   
466             mgr->StartAnalysis("grid");
467          } else {
468             if (!chain) {
469                ::Error("AnalysisTrainNew.C::StartAnalysis", "Cannot create the chain");
470                return;
471             }   
472             mgr->StartAnalysis(mode, chain);
473          }   
474          return;
475    }      
476 }          
477     
478 //______________________________________________________________________________
479 void CheckModuleFlags(const char *mode) {
480 // Checks selected modules and insure compatibility
481    Int_t imode = -1;
482    if (!strcmp(mode, "LOCAL")) imode = 0;
483    if (!strcmp(mode, "PROOF")) imode = 1;
484    if (!strcmp(mode, "GRID"))  imode = 2;
485    if (imode==1) {
486       if (!usePAR) {
487          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PAR files enabled due to PROOF analysis");
488          usePAR = kTRUE;
489       }   
490    }  
491    if (imode != 2) {
492       ::Info("AnalysisTrainNew.C::CheckModuleFlags", "AliEn plugin disabled since not in GRID mode");
493       usePLUGIN = kFALSE; 
494    }   
495    if (iAODanalysis) {
496    // AOD analysis
497       if (useMC)
498          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "MC usage disabled in analysis on AOD's");
499       if (useAODTAGS)
500          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "AOD tags usage disabled in analysis on AOD's");
501       useMC = kFALSE;
502       useTR = kFALSE;
503       useAODTAGS = kFALSE;
504       if (iESDfilter)
505          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "ESD filter disabled in analysis on AOD's");
506       iESDfilter   = 0;
507       if (!iAODhandler) {
508          if (iJETAN) 
509             ::Info("AnalysisTrainNew.C::CheckModuleFlags", "JETAN disabled in analysis on AOD's without AOD handler");
510          iJETAN = 0;
511          if (iPWG4gammaconv)
512             ::Info("AnalysisTrainNew.C::CheckModuleFlags", "iPWG4gammaconv disabled on AOD's without AOD handler");
513          iPWG4gammaconv = 0;   
514          if (iPWG4partcorr)
515             ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG4partcorr disabled on AOD's without AOD handler");
516          iPWG4partcorr = 0;
517       }
518       // Disable tasks that do not work yet on AOD data
519       if (iPWG2flow)
520          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2flow disabled on AOD's");
521          iPWG2flow = 0;
522       if (iPWG2femto)
523          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2femto disabled on AOD's");
524          iPWG2femto = 0;
525       if (iPWG2res)
526          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2res disabled on AOD's");
527          iPWG2res = 0;
528       if (iPWG2kink)         
529          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2kink disabled in analysis on AOD's");
530          iPWG2kink = 0;
531       if (iPWG2unicor)         
532          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2unicor disabled in analysis on AOD's");
533          iPWG2unicor = 0;
534       if (iPWG2evchar)
535          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG4evchar disabled on AOD's");
536          iPWG2evchar = 0;
537       if (iPWG4omega3pi)
538          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG4omega3pi disabled on AOD's");
539          iPWG4omega3pi = 0;
540       if (iPWG4partcorr)
541          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG4partcorr disabled on AOD's");
542          iPWG4partcorr = 0;
543    } else {   
544    // ESD analysis
545       iMUONcopyAOD = 0;
546       if (!useMC) useTR = kFALSE;
547       if (!useTR) {
548          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "iPWG2evchar disabled if not reading track references");
549          iPWG2evchar = 0;
550       }   
551    }
552    if (!useMC) {
553       if (iPWG2perfcascade)
554          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2perfcascade disabled without MC info");
555       iPWG2perfcascade = 0;   
556    }   
557    if (iJETAN && !iAODanalysis) iESDfilter=1;
558    if (iESDfilter) {iAODhandler=1; useCORRFW = kTRUE;}
559    if (iPWG2spectra || iPWG2flow || iPWG3vertexing) useCORRFW = kTRUE;
560    if (useKFILTER && !useMC) useKFILTER = kFALSE;
561    if (useAODTAGS && !iAODhandler) useAODTAGS = kFALSE;
562 }
563
564 //______________________________________________________________________________
565 Bool_t Connect(const char *mode) {
566 // Connect <username> to the back-end system.
567    Int_t imode = -1;
568    if (!strcmp(mode, "LOCAL")) imode = 0;
569    if (!strcmp(mode, "PROOF")) imode = 1;
570    if (!strcmp(mode, "GRID"))  imode = 2;
571    TString username = gSystem->Getenv("alien_API_USER");
572    switch (imode) {
573       case 0:
574          break;
575       case 1:
576          if  (!username.Length()) {
577             ::Error(Form("AnalysisTrainNew.C::Connect <%s>", mode), "Make sure you:\n \
578                            1. Have called: alien-token-init <username>\n \
579                            2. Have called: >source /tmp/gclient_env_$UID");
580             return kFALSE;
581          }
582          ::Info("AnalysisTrainNew.C::Connect", "Connecting user <%s> to PROOF cluster <%s>", 
583                 username.Data(), proof_cluster.Data());
584          gEnv->SetValue("XSec.GSI.DelegProxy", "2");
585 //         TProof::Open(Form("%s@%s:31093", username.Data(), proof_cluster.Data()));       
586          TProof::Open(Form("%s@%s", username.Data(), proof_cluster.Data()));       
587          if (!gProof) {
588             if (strcmp(gSystem->Getenv("XrdSecGSISRVNAMES"), "lxfsrd0506.cern.ch"))
589                ::Error(Form("AnalysisTrainNew.C::Connect <%s>", mode), "Environment XrdSecGSISRVNAMES different from lxfsrd0506.cern.ch");
590             return kFALSE;
591          }
592          TGrid::Connect("alien://");
593          if (gGrid) {
594             TString homedir = gGrid->GetHomeDirectory();
595             TString workdir = homedir + train_name;
596             if (!gGrid->Cd(workdir)) {
597                gGrid->Cd(homedir);
598                if (gGrid->Mkdir(workdir)) {
599                   gGrid->Cd(train_name);
600                   ::Info("AnalysisTrainNew::Connect()", "Directory %s created", gGrid->Pwd());
601                }
602             }
603             gGrid->Mkdir("proof_output");
604             gGrid->Cd("proof_output");
605             proof_outdir = Form("alien://%s", gGrid->Pwd());
606          }   
607          break;
608       case 2:      
609          if  (!username.Length()) {
610             ::Error(Form("AnalysisTrainNew.C::Connect <%s>", mode), "Make sure you:\n \
611                            1. Have called: alien-token-init <username>\n \
612                            2. Have called: >source /tmp/gclient_env_$UID");
613             return kFALSE;
614          }
615          if (usePLUGIN && !gSystem->Getenv("alien_CLOSE_SE")) {
616             ::Error(Form("AnalysisTrainNew.C::Connect <%s>", mode), 
617                            "When using the AliEn plugin it is preferable to define the \
618                            variable alien_CLOSE_SE in your environment.");
619             return kFALSE;
620          }
621          ::Info("AnalysisTrainNew.C::Connect", "Connecting user <%s> to AliEn ...", 
622                 username.Data());
623          TGrid::Connect("alien://");
624          if (!gGrid || !gGrid->IsConnected()) return kFALSE;
625          break;
626       default:
627          ::Error("AnalysisTrainNew.C::Connect", "Unknown run mode: %s", mode);
628          return kFALSE;
629    }
630    ::Info("AnalysisTrainNew.C::Connect","Connected in %s mode", mode);
631    return kTRUE;
632 }
633
634 //______________________________________________________________________________
635 Bool_t LoadCommonLibraries(const char *mode)
636 {
637 // Load common analysis libraries.
638    Int_t imode = -1;
639    if (!strcmp(mode, "LOCAL")) imode = 0;
640    if (!strcmp(mode, "PROOF")) imode = 1;
641    if (!strcmp(mode, "GRID"))  imode = 2;
642    if (!gSystem->Getenv("ALICE_ROOT")) {
643       ::Error("AnalysisTrainNew.C::LoadCommonLibraries", "Analysis train requires that analysis libraries are compiled with a local AliRoot"); 
644       return kFALSE;
645    }   
646    Bool_t success = kTRUE;
647    // ROOT libraries
648    gSystem->Load("libTree.so");
649    gSystem->Load("libGeom.so");
650    gSystem->Load("libVMC.so");
651    gSystem->Load("libPhysics.so");
652    
653    // Load framework classes. Par option ignored here.
654    switch (imode) {
655       case 0:
656       case 2:
657          if (useCPAR) {
658             success &= LoadLibrary("STEERBase", mode, kTRUE);
659             success &= LoadLibrary("ESD", mode, kTRUE);
660             success &= LoadLibrary("AOD", mode, kTRUE);
661             success &= LoadLibrary("ANALYSIS", mode, kTRUE);
662             success &= LoadLibrary("ANALYSISalice", mode, kTRUE);
663             if (useCORRFW) success &= LoadLibrary("CORRFW", mode, kTRUE);
664          } else {   
665             success &= LoadLibrary("libSTEERBase.so", mode);
666             success &= LoadLibrary("libESD.so", mode);
667             success &= LoadLibrary("libAOD.so", mode);
668             success &= LoadLibrary("libANALYSIS.so", mode);
669             success &= LoadLibrary("libANALYSISalice.so", mode);
670             if (useCORRFW) success &= LoadLibrary("libCORRFW.so", mode);
671             gROOT->ProcessLine(".include $ALICE_ROOT/include");
672          }   
673          break;
674       case 1:
675          Int_t ires = -1;
676          if (useAFPAR && !gSystem->AccessPathName(AFversion)) ires = gProof->UploadPackage(AFversion);
677          if (ires < 0) {
678             success &= LoadLibrary("STEERBase", mode);
679             success &= LoadLibrary("ESD", mode);
680             success &= LoadLibrary("AOD", mode);
681             success &= LoadLibrary("ANALYSIS", mode);
682             success &= LoadLibrary("ANALYSISalice", mode);
683             if (useCORRFW) success &= LoadLibrary("CORRFW", mode);
684          } else { 
685             ires = gProof->EnablePackage(AFversion);
686             if (ires<0) success = kFALSE;
687             if (useCORRFW) success &= LoadLibrary("CORRFW", mode);
688          }
689          break;         
690       default:
691          ::Error("AnalysisTrainNew.C::LoadCommonLibraries", "Unknown run mode: %s", mode);
692          return kFALSE;
693    }
694    if (success) {
695       ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Load common libraries:    SUCCESS");
696       ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Include path for Aclic compilation:\n%s",
697               gSystem->GetIncludePath());
698    } else {           
699       ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Load common libraries:    FAILED");
700    }   
701       
702    return success;
703 }
704
705 //______________________________________________________________________________
706 Bool_t LoadAnalysisLibraries(const char *mode)
707 {
708 // Load common analysis libraries.
709    Bool_t success = kTRUE;
710    if (iESDfilter) {
711       if (!LoadLibrary("PWG3base", mode, kTRUE) ||
712           !LoadLibrary("PWG3muon", mode, kTRUE)) return kFALSE;
713    }   
714    // JETAN
715    if (iJETAN) {
716       if (!LoadLibrary("JETAN", mode, kTRUE)) return kFALSE;
717    }               
718    // PWG4 particle correlations
719    if (iPWG4partcorr) {   
720       if (!LoadLibrary("PWG4PartCorrBase", mode, kTRUE) ||
721           !LoadLibrary("PWG4PartCorrDep", mode, kTRUE)) return kFALSE;
722    }
723    // PWG4 gamma conversion
724    if (iPWG4gammaconv) {
725       if (!LoadLibrary("PWG4GammaConv", mode, kTRUE)) return kFALSE;
726    }      
727    // PWG4 omega to 3 pions
728    if (iPWG4omega3pi) {
729       if (!LoadLibrary("PWG4omega3pi", mode, kTRUE)) return kFALSE;
730    }      
731    // PWG2 task protons 
732    if (iPWG2spectra) {
733       if (!LoadLibrary("PWG2spectra", mode, kTRUE)) return kFALSE;
734    }
735    // PWG2 flow
736    if (iPWG2flow) {
737       if (!LoadLibrary("PWG2flowCommon", mode, kTRUE) ||
738           !LoadLibrary("PWG2flowTasks", mode, kTRUE)) return kFALSE;
739    }
740    // PWG2 resonances
741    if (iPWG2res) {
742       if (!LoadLibrary("PWG2resonances", mode, kTRUE)) return kFALSE;
743    }   
744    // PWG2 kink
745    if (iPWG2kink) {
746       if (!LoadLibrary("PWG2kink", mode, kTRUE)) return kFALSE;
747    }   
748    // PWG2 unicor
749    if (iPWG2unicor) {
750       if (!LoadLibrary("PWG2unicor", mode, kTRUE)) return kFALSE;
751    }   
752    // PWG2 evchar
753    if (iPWG2evchar) {
754       if (!LoadLibrary("PWG2evchar", mode, kTRUE)) return kFALSE;
755    }   
756    // PWG2 femtoscopy
757    if (iPWG2femto) {
758       if (!LoadLibrary("PWG2AOD", mode, kTRUE) ||
759           !LoadLibrary("PWG2femtoscopy", mode, kTRUE) ||
760           !LoadLibrary("PWG2femtoscopyUser", mode, kTRUE)) return kFALSE;
761       TFile::Cp(gSystem->ExpandPathName("$(ALICE_ROOT)/PWG2/FEMTOSCOPY/macros/ConfigFemtoAnalysis.C"), Form("%s/ConfigFemtoAnalysis.C", train_name.Data()));
762       anaLibs += "ConfigFemtoAnalysis.C ";
763    }   
764    // Vertexing HF
765    if (iPWG3vertexing) {
766       if (!LoadLibrary("PWG3base", mode, kTRUE) ||
767           !LoadLibrary("PWG3vertexingHF", mode, kTRUE)) return kFALSE;
768    }   
769    ::Info("AnalysisTrainNew.C::LoadAnalysisLibraries", "Load other libraries:   SUCCESS");
770    return kTRUE;
771 }
772
773 //______________________________________________________________________________
774 Bool_t LoadLibrary(const char *module, const char *mode, Bool_t rec=kFALSE)
775 {
776 // Load a module library in a given mode. Reports success.
777    Int_t imode = -1;
778    Int_t result;
779    TString smodule(module);
780    if (!strcmp(mode, "LOCAL")) imode = 0;
781    if (!strcmp(mode, "PROOF")) imode = 1;
782    if (!strcmp(mode, "GRID"))  imode = 2;
783    TString mod(module);
784    if (!mod.Length()) {
785       ::Error("AnalysisTrainNew.C::LoadLibrary", "Empty module name");
786       return kFALSE;
787    }   
788    // If a library is specified, just load it
789    if (smodule.EndsWith(".so")) {
790       mod.Remove(mod.Index(".so"));
791       result = gSystem->Load(mod);
792       if (result < 0) {
793          ::Error("AnalysisTrainNew.C::LoadLibrary", "Could not load library %s", module);
794          return kFALSE;
795       }
796       if (rec) anaLibs += Form("%s.so ",mod.Data()); 
797       return kTRUE;
798    } 
799    // Check if the library is already loaded
800    if (strlen(gSystem->GetLibraries(Form("%s.so", module), "", kFALSE)) > 0)
801       return kTRUE;    
802    switch (imode) {
803       case 0:
804       case 2:
805          if (usePAR) {
806             result = SetupPar(module);
807             if (rec) anaPars += Form("%s.par ", module);
808          } else {
809             result = gSystem->Load(Form("lib%s.so", module));
810             if (rec) anaLibs += Form("lib%s.so ", module);
811          }   
812          break;
813       case 1:
814          result = gProof->UploadPackage(module);
815          if (result<0) {
816             result = gProof->UploadPackage(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", module)));
817             if (result<0) {
818                ::Error("AnalysisTrainNew.C::LoadLibrary", "Could not find module %s.par in current directory nor in $ALICE_ROOT", module);
819                return kFALSE;
820             }
821          }   
822          result = gProof->EnablePackage(module);
823          break;
824       default:
825          return kFALSE;
826    }         
827    if (result < 0) {
828       ::Error("AnalysisTrainNew.C::LoadLibrary", "Could not load module %s", module);
829       return kFALSE;
830    }
831    return kTRUE;
832 }           
833
834
835 //______________________________________________________________________________
836 TChain *CreateChain(const char *mode, const char *plugin_mode)
837 {
838 // Create the input chain
839    Int_t imode = -1;
840    if (!strcmp(mode, "LOCAL")) imode = 0;
841    if (!strcmp(mode, "PROOF")) imode = 1;
842    if (!strcmp(mode, "GRID"))  imode = 2;
843    TChain *chain = NULL;
844    // Local chain
845    switch (imode) {
846       case 0:
847          if (iAODanalysis) {
848             if (!local_xmldataset.Length()) {
849                // Local AOD
850                chain = new TChain("aodTree");
851                if (gSystem->AccessPathName("data/AliAOD.root")) 
852                   ::Error("AnalysisTrainNew.C::CreateChain", "File: AliAOD.root not in ./data dir");
853                else {
854                   if (!saveTrain) chain->Add("data/AliAOD.root");
855                   else            chain->Add("../data/AliAOD.root");
856                }   
857             } else {
858                // Interactive AOD
859                chain = CreateChainSingle(local_xmldataset, "aodTree");
860             }
861          } else {      
862             if (!local_xmldataset.Length()) {
863                // Local ESD
864                chain = new TChain("esdTree");
865                if (gSystem->AccessPathName("data/AliESDs.root")) 
866                   ::Error("AnalysisTrainNew.C::CreateChain", "File: AliESDs.root not in ./data dir");
867                else {
868                   if (!saveTrain) chain->Add("data/AliESDs.root");
869                   else            chain->Add("../data/AliESDs.root");
870                }   
871             } else {
872                // Interactive ESD
873                chain = CreateChainSingle(local_xmldataset, "esdTree");
874             }   
875          }
876          break;
877       case 1:
878          break;
879       case 2:
880          if (usePLUGIN) {
881             AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
882             AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
883          } else {
884             TString           treeName = "esdTree";
885             if (iAODanalysis) treeName = "aodTree";
886             chain = CreateChainSingle("wn.xml", treeName);
887          }
888          break;      
889       default:   
890    }
891    if (chain && chain->GetNtrees()) return chain;
892    return NULL;
893 }   
894
895 //______________________________________________________________________________
896 TChain* CreateChainSingle(const char* xmlfile, const char *treeName)
897 {
898    printf("*******************************\n");
899    printf("*** Getting the ESD Chain   ***\n");
900    printf("*******************************\n");
901    TAlienCollection * myCollection  = TAlienCollection::Open(xmlfile);
902
903    if (!myCollection) {
904       ::Error("AnalysisTrainNew.C::CreateChainSingle", "Cannot create an AliEn collection from %s", xmlfile) ;
905       return NULL ;
906    }
907
908    TChain* chain = new TChain(treeName);
909    myCollection->Reset() ;
910    while ( myCollection->Next() ) chain->Add(myCollection->GetTURL("")) ;
911    chain->ls();
912    return chain;
913 }
914
915 //______________________________________________________________________________
916 Int_t SetupPar(char* pararchivename)
917 {
918    if (!pararchivename || !strlen(pararchivename)) return -1;
919    char processline[1024];
920    if (gSystem->AccessPathName(Form("%s.par", pararchivename))) {
921       if (!gSystem->AccessPathName(Form("%s/%s.par", gSystem->Getenv("ALICE_ROOT"),pararchivename))) {
922          ::Info("AnalysisTrainNew.C::SetupPar", "Getting %s.par from $ALICE_ROOT", pararchivename);
923          TFile::Cp(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", pararchivename)), 
924                    Form("%s.par",pararchivename));
925       } else {
926          ::Error("AnalysisTrainNew.C::SetupPar", "Cannot find %s.par", pararchivename);
927          return -1;
928       }   
929    }
930    if (usePLUGIN && saveTrain) gSystem->Exec(Form("ln -s ../%s.par %s",pararchivename, train_name.Data()));
931    gSystem->Exec(Form("tar xvzf %s.par", pararchivename));
932
933    TString ocwd = gSystem->WorkingDirectory();
934    if (!gSystem->ChangeDirectory(pararchivename)) return -1;
935         
936    // check for BUILD.sh and execute
937    if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
938       printf("*******************************\n");
939       printf("*** Building PAR archive    ***\n");
940       printf("*******************************\n");          
941       if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
942          Error("runProcess","Cannot Build the PAR Archive! - Abort!");
943          return -1;
944       }
945    }
946
947         // check for SETUP.C and execute
948         if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
949             printf("*******************************\n");
950             printf("*** Setup PAR archive       ***\n");
951             printf("*******************************\n");
952             gROOT->Macro("PROOF-INF/SETUP.C");
953         }       
954         if (!gSystem->ChangeDirectory(ocwd.Data())) return -1;
955    return 0;
956 }
957
958 //______________________________________________________________________________
959 AliAnalysisAlien* CreateAlienHandler(const char *plugin_mode)
960 {
961 // Check if user has a valid token, otherwise make one. This has limitations.
962 // One can always follow the standard procedure of calling alien-token-init then
963 //   source /tmp/gclient_env_$UID in the current shell.
964    if (!AliAnalysisGrid::CreateToken()) return NULL;
965    AliAnalysisAlien *plugin = new AliAnalysisAlien();
966 // Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
967    plugin->SetRunMode(plugin_mode);
968    if (useProductionMode) plugin->SetProductionMode();
969    plugin->SetJobTag(job_tag);
970    plugin->SetNtestFiles(1);
971 //   plugin->SetPreferedSE("ALICE::Legnaro::SE");
972 // Set versions of used packages
973    plugin->SetAPIVersion("V2.4");
974    plugin->SetROOTVersion(root_version);
975    plugin->SetAliROOTVersion(aliroot_version);
976 // Declare input data to be processed.
977 // Method 1: Create automatically XML collections using alien 'find' command.
978 // Define production directory LFN
979    plugin->SetGridDataDir(alien_datadir);
980 // Set data search pattern
981    if (iAODanalysis) plugin->SetDataPattern("*AliAOD.root");
982    else              plugin->SetDataPattern("*ESD.tag.root");
983 // ...then add run numbers to be considered
984    plugin->SetRunRange(run_range[0], run_range[1]);
985 //   for (Int_t i=0; i<10; i++) {
986 //      if (run_numbers[i]==0) break;
987 //      plugin->AddRunNumber(run_numbers[i]);
988 //   }   
989 // Method 2: Declare existing data files (raw collections, xml collections, root file)
990 // If no path mentioned data is supposed to be in the work directory (see SetGridWorkingDir())
991 // XML collections added via this method can be combined with the first method if
992 // the content is compatible (using or not tags)
993 //   plugin->AddDataFile("tag.xml");
994 //   plugin->AddDataFile("/alice/data/2008/LHC08c/000057657/raw/Run57657.Merged.RAW.tag.root");
995 // Define alien work directory where all files will be copied. Relative to alien $HOME.
996    if (iAODanalysis) plugin->SetGridWorkingDir("analysisAOD");
997    else              plugin->SetGridWorkingDir("analysisESD");
998 // Declare alien output directory. Relative to working directory.
999    if (!alien_outdir.Length()) alien_outdir = Form("output_%s",train_name.Data());
1000    plugin->SetGridOutputDir(alien_outdir);
1001
1002    TString ana_sources = "";
1003    TString ana_add = "";
1004    if (usePAR && anaPars.Length()) {
1005       printf("%s\n", anaPars.Data());
1006       TObjArray *arr;
1007       TObjString *objstr;
1008       arr = anaPars.Tokenize(" ");
1009       TIter next(arr);
1010       while ((objstr=(TObjString*)next())) plugin->EnablePackage(objstr->GetString());
1011       delete arr;
1012    } 
1013    
1014 // Declare the analysis source files names separated by blancs. To be compiled runtime
1015 // using ACLiC on the worker nodes.
1016    ana_sources = ana_sources.Strip();
1017 // Declare all libraries (other than the default ones for the framework. These will be
1018 // loaded by the generated analysis macro. Add all extra files (task .cxx/.h) here.
1019    anaLibs     = anaLibs.Strip();   
1020    if (ana_sources.Length()) plugin->SetAnalysisSource(ana_sources);
1021    if (anaLibs.Length())     plugin->SetAdditionalLibs(anaLibs);
1022      
1023 // Declare the output file names separated by blancs.
1024 // (can be like: file.root or file.root@ALICE::Niham::File)
1025    plugin->SetDefaultOutputs();
1026    plugin->SetMergeExcludes(mergeExclude);
1027    plugin->SetMaxMergeFiles(maxMergeFiles);
1028    plugin->SetNrunsPerMaster(nRunsPerMaster);
1029 // Optionally define the files to be archived.
1030 //   plugin->SetOutputArchive("log_archive.zip:stdout,stderr@ALICE::NIHAM::File root_archive.zip:AliAOD.root,AOD.tag.root@ALICE::NIHAM::File");
1031    plugin->SetOutputArchive(Form("log_archive.zip:stdout,stderr %s",outputArchive.Data()));
1032 // Optionally set a name for the generated analysis macro (default MyAnalysis.C)
1033    plugin->SetAnalysisMacro(Form("%s.C", train_name.Data()));
1034 // Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
1035    plugin->SetSplitMaxInputFileNumber(nFilesPerJob);
1036 // Optionally set number of failed jobs that will trigger killing waiting sub-jobs.
1037 //   plugin->SetMaxInitFailed(5);
1038 // Optionally resubmit threshold.
1039 //   plugin->SetMasterResubmitThreshold(90);
1040 // Optionally set time to live (default 30000 sec)
1041    plugin->SetTTL(30000);
1042 // Optionally set input format (default xml-single)
1043    plugin->SetInputFormat("xml-single");
1044 // Optionally modify the name of the generated JDL (default analysis.jdl)
1045    plugin->SetJDLName(Form("%s.jdl", train_name.Data()));
1046 // Optionally modify the executable name (default analysis.sh)
1047    plugin->SetExecutable(Form("%s.sh", train_name.Data()));
1048 // Optionally modify job price (default 1)
1049    plugin->SetPrice(1);      
1050 // Optionally modify split mode (default 'se')    
1051    plugin->SetSplitMode("se");
1052    return plugin;
1053 }
1054
1055 //______________________________________________________________________________
1056 void WriteConfig()
1057 {
1058 // Write train configuration in a file. The file name has the format:
1059 // train_[trainName]_ddMonthyyyy_time.C
1060    if (useDATE) {
1061       gSystem->Exec("date +%d%b%Y_%Hh%M > date.tmp");
1062       ifstream fdate("date.tmp");
1063       if (!fdate.is_open()) {
1064          ::Error("AnalysisTrainNew.C::Export","Could not generate file name");
1065          return;
1066       }
1067       const char date[64];
1068       fdate.getline(date,64);
1069       fdate.close();
1070       gSystem->Exec("rm date.tmp");
1071       train_name = Form("train_%s_%s", train_name.Data(), date);
1072    } else {
1073       train_name = Form("train_%s", train_name.Data());
1074    }   
1075    TString cdir = gSystem->WorkingDirectory();
1076    gSystem->MakeDirectory(train_name);
1077    gSystem->ChangeDirectory(train_name);
1078    ofstream out;
1079    out.open(Form("%sConfig.C",train_name.Data()), ios::out); 
1080    if (out.bad()) {
1081       ::Error("AnalysisTrainNew.C::Export", "Cannot open ConfigTrain.C for writing");
1082       return;
1083    }
1084    out << "{" << endl;
1085    out << "   train_name      = " << "\"" << train_name.Data() << "\";" << endl;
1086    out << "   proof_cluster   = " << "\"" << proof_cluster.Data() << "\";" << endl;
1087    out << "   useAFPAR        = " << useAFPAR << ";" << endl;
1088    if (useAFPAR) 
1089       out << "   AFversion       = " << AFversion.Data() << ";" << endl;
1090    out << "   proof_dataset   = " << "\"" << proof_dataset.Data() << "\";" << endl;
1091    out << "   usePLUGIN       = " << usePLUGIN << ";" << endl;
1092    out << "   usePAR          = " << usePAR << ";" << endl;
1093    out << "   useCPAR         = " << useCPAR << ";" << endl;
1094    out << "   root_version    = " << "\"" << root_version.Data() << "\";" << endl;
1095    out << "   aliroot_version = " << "\"" << aliroot_version.Data() << "\";" << endl;
1096    out << "   alien_datadir   = " << "\"" << alien_datadir.Data() << "\";" << endl;
1097    if (!alien_outdir.Length()) alien_outdir = Form("output_%s",train_name.Data());
1098    out << "   alien_outdir    = " << "\"" << alien_outdir.Data() << "\";" << endl;
1099    out << "   maxMergeFiles   = " << maxMergeFiles << ";" << endl;
1100    out << "   mergeExclude    = " << "\"" << mergeExclude.Data() << "\";" << endl;
1101    out << "   nRunsPerMaster  = " << nRunsPerMaster << ";" << endl;
1102    out << "   nFilesPerJob    = " << nFilesPerJob << ";" << endl;
1103 //   for (Int_t i=0; i<10; i++) {
1104 //      if (run_numbers[i]) 
1105 //         out << "   run_numbers[" << i << "]  = " << run_numbers[i] << ";" << endl;
1106 //   }
1107    out << "   run_range[0]    = " << run_range[0] << ";" << endl;
1108    out << "   run_range[1]    = " << run_range[1] << ";" << endl;
1109    out << "   useDBG          = " << useDBG << ";" << endl;
1110    out << "   useMC           = " << useMC << ";" << endl;
1111    out << "   useTAGS         = " << useTAGS << ";" << endl;
1112    out << "   useKFILTER      = " << useKFILTER << ";" << endl;
1113    out << "   useTR           = " << useTR << ";" << endl;
1114    out << "   useCORRFW       = " << useCORRFW << ";" << endl;
1115    out << "   useAODTAGS      = " << useAODTAGS << ";" << endl;
1116    out << "   saveTrain       = " << "kFALSE;" << endl << endl;
1117    out << "   // Analysis modules" << endl;
1118    out << "   iAODanalysis    = " << iAODanalysis << ";" << endl;
1119    out << "   iAODhandler     = " << iAODhandler << ";" << endl;
1120    out << "   iESDfilter      = " << iESDfilter << ";" << endl;
1121    out << "   iMUONcopyAOD    = " << iMUONcopyAOD << ";" << endl;
1122    out << "   iJETAN          = " << iJETAN << ";" << endl;
1123    out << "   iPWG4partcorr   = " << iPWG4partcorr << ";" << endl;
1124    out << "   iPWG4gammaconv  = " << iPWG4gammaconv << ";" << endl;
1125    out << "   iPWG4omega3pi   = " << iPWG4omega3pi << ";" << endl;
1126    out << "   iPWG2femto      = " << iPWG2femto << ";" << endl;
1127    out << "   iPWG2spectra    = " << iPWG2spectra << ";" << endl;
1128    out << "     iPWG2protons      = " << iPWG2protons << ";" << endl;
1129    out << "     iPWG2checkcascade = " << iPWG2checkcascade << ";" << endl;
1130    out << "     iPWG2perfcascade  = " << iPWG2perfcascade << ";" << endl;
1131    out << "     iPWG2checkv0      = " << iPWG2checkv0 << ";" << endl;
1132    out << "     iPWG2strange      = " << iPWG2strange << ";" << endl;
1133    out << "   iPWG2flow       = " << iPWG2flow << ";" << endl;
1134    out << "   iPWG2res        = " << iPWG2res << ";" << endl;
1135    out << "   iPWG2kink       = " << iPWG2kink << ";" << endl;
1136    out << "     iPWG2kinkESDMC    = " << iPWG2kinkESDMC << ";" << endl;
1137    out << "     iPWG2kinkres      = " << iPWG2kinkres << ";" << endl;
1138    out << "     iPWG2kinklikesign = " << iPWG2kinklikesign << ";" << endl;
1139    out << "   iPWG2unicor     = " << iPWG2unicor << ";" << endl;
1140    out << "   iPWG2evchar     = " << iPWG2evchar << ";" << endl;
1141    out << "}" << endl;
1142    ::Info("AnalysisTrainNew.C::WriteConfig", "Train configuration wrote to file %s", Form("config_%s.C", train_name.Data()));
1143    gSystem->ChangeDirectory(cdir);
1144 }   
1145
1146 //______________________________________________________________________________
1147 Bool_t LoadConfig(const char *filename)
1148 {
1149 // Read train configuration from file
1150    if (gSystem->AccessPathName(filename)) {
1151       ::Error("AnalysisTrainNew.C::LoadConfig", "Config file name not found");
1152       return kFALSE;
1153    }   
1154    gROOT->ProcessLine(Form(".x %s", filename));
1155    ::Info("AnalysisTrainNew.C::LoadConfig", "Train configuration loaded from file %s", filename);
1156    return kTRUE;
1157 }