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