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