]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/macros/AnalysisTrainNew.C
Plot rather virtual memory usage per process than total memory.
[u/mrichter/AliRoot.git] / ANALYSIS / macros / AnalysisTrainNew.C
index ebb91f437d0744ec4f436be0654dcfce12c79573..eb9053aec6b2cf60170219bee28df221207cbc46 100644 (file)
 //    root[1] AnalysisTrainNew(ana_mode, plugin_mode, "train_default_<date>/ConfigTrain.C")
 
 //==================   TRAIN NAME   ============================================
-TString     train_name         = "LHC09a5"; // enters file names, so no blancs or special characters
+TString     train_name         = "test"; // *CHANGE ME* (no blancs or special characters)
+TString     job_tag            = "test"; // *CHANGE ME*
 //==============================================================================
 
 // ### Settings that make sense in PROOF only
 //==============================================================================
 TString     proof_cluster      = "alicecaf.cern.ch";
 Bool_t      useAFPAR           = kFALSE;  // use AF special par file
-TString     AFversion          = "AF-v4-16";
+TString     AFversion          = "AF-v4-17";
 // Change CAF dataset here
 TString     proof_dataset      = "/COMMON/COMMON/LHC09a4_run8100X#/esdTree";
 TString     proof_outdir       = "";
 
 // ### Settings that make sense when using the Alien plugin
 //==============================================================================
+Int_t       runOnData          = 0;       // Set to 1 if processing real data
+Int_t       iCollision         = 0;       // 0=pp, 1=Pb-Pb
 Bool_t      usePLUGIN          = kTRUE;   // do not change
+Bool_t      useProductionMode  = kTRUE;   // use the plugin in production mode
 // Usage of par files ONLY in grid mode and ONLY if the code is not available
 // in the deployed AliRoot versions. Par file search path: local dir, if not there $ALICE_ROOT.
 // To refresh par files, remove the ones in the workdir, then do "make <target.par>" in 
 // AliRoot.
 Bool_t      usePAR             = kFALSE;  // use par files for extra libs
 Bool_t      useCPAR            = kFALSE;  // use par files for common libs
-TString     root_version       = "v5-23-04";
-TString     aliroot_version    = "v4-17-03";
+TString     root_version       = "v5-25-04-3";  // *CHANGE ME IF MORE RECENT IN GRID*
+TString     aliroot_version    = "v4-18-16-AN";  // *CHANGE ME IF MORE RECENT IN GRID*                                          
 // Change production base directory here
-TString     alien_datadir      = "/alice/sim/PDC_09/LHC09a5/";
+TString     alien_datadir      = "/alice/sim/PDC_09/LHC09a4";
 // AliEn output directory. If blank will become output_<train_name>
-TString     alien_outdir       = "/alice/sim/PDC_09/LHC09a5/AOD";
+TString     alien_outdir       = "/alice/sim/PDC_09/LHC09a4/AOD3";
+// TString     alien_outdir       = "";
+// Output folder to write delta AOD's. Considered if not null.
+TString     outputSingleFolder = "";
+//TString     outputSingleFolder = "deltas";
 // Number of files merged in a chunk
-Int_t       maxMergeFiles      = 50;
+Int_t       maxMergeFiles      = 20;
 // Files that should not be merged
-TString     mergeExclude       = "AliAOD.root AliAOD.VertexingHF.root AOD.tag.root";
+TString     mergeExclude       = "AliAOD.root AliAOD.VertexingHF.root AliAOD.Jets.root deltaAODPartCorr.root AliAOD.Muons.root resonances.root forward.root";
+// Make replicas on the storages below
+TString     outputStorages      = "disk=4";
 // Number of runs per master job
 Int_t       nRunsPerMaster     = 10;
 // Maximum number of files per job (gives size of AOD)
 Int_t       nFilesPerJob       = 100;
+// Int_t       nFilesPerJob       = 1; (AOD->delta AOD production case)
 // Set the run range
-Int_t       run_range[2]       =  {90000, 90040};
+Int_t       run_range[2]       =  {81007, 81656};  // LHC09a7   *CHANGE ME*
 // ### Settings that make sense only for local analysis
 //==============================================================================
 // Change local xml dataset for local interactive analysis
@@ -63,37 +74,73 @@ TString     local_xmldataset   = "";
 Bool_t      useDATE            = kFALSE; // use date in train name
 Bool_t      useDBG             = kFALSE; // activate debugging
 Bool_t      useMC              = kTRUE;  // use MC info
-Bool_t      useTAGS            = kFALSE; // use ESD tags for selection
-Bool_t      useKFILTER         = kTRUE;  // use Kinematics filter
+Bool_t      useTAGS            = kTRUE; // use ESD tags for selection
+Bool_t      useKFILTER         = kTRUE; // use Kinematics filter
 Bool_t      useTR              = kTRUE;  // use track references
 Bool_t      useCORRFW          = kFALSE; // do not change
-Bool_t      useAODTAGS         = kTRUE;  // use AOD tags
+Bool_t      useAODTAGS         = kFALSE;  // use AOD tags
 Bool_t      saveTrain          = kTRUE;  // save train configuration as: 
+Bool_t      saveCanvases       = kTRUE; // save canvases created in Terminate
 Bool_t      saveProofToAlien   = kFALSE; // save proof outputs in AliEn
                                          // train_[trainName]_ddMonthyyyy_time.C
 // ### Analysis modules to be included. Some may not be yet fully implemented.
 //==============================================================================
 Int_t       iAODanalysis       = 0;      // Analysis on input AOD's
-Int_t       iAODhandler        = 1;      // Analysis produces an AOD or dAOD's
-Int_t       iESDfilter         = 1;      // ESD to AOD filter (barrel + muon tracks)
+Int_t       iAODhandler        = 0;      // Analysis produces an AOD or dAOD's
+Int_t       iESDfilter         = 0;      // ESD to AOD filter (barrel + muon tracks)
 Int_t       iMUONcopyAOD       = 0;      // Task that copies only muon events in a separate AOD (PWG3)
-Int_t       iJETAN             = 1;      // Jet analysis (PWG4) - needs ESD filter
-Int_t       iPWG4partcorr      = 1;      // Gamma-hadron correlations task (PWG4)
+Int_t       iJETAN             = 0;      // Jet analysis (PWG4)
+Int_t       iJETANdelta        = 0;      // Jet delta AODs
+Int_t       iPWG4partcorr      = 0;      // Gamma-hadron correlations task (PWG4)
 Int_t       iPWG4gammaconv     = 0;      // Gamma conversion analysis (PWG4)
-Int_t       iPWG3vertexing     = 1;      // Vertexing HF task (PWG2)
-Int_t       iPWG2femto         = 1;      // Femtoscopy task (PWG2)
-Int_t       iPWG2spectra       = 1;      // Spectra PWG2 tasks (protons, cascades, V0 check, strange)
-Int_t       iPWG2flow          = 0;      // Flow analysis task (PWG2)
+Int_t       iPWG4omega3pi      = 0;      // Omega to 3 pi analysis (PWG4)
+Int_t       iPWG3vertexing     = 0;      // Vertexing HF task (PWG3)
+Int_t       iPWG3hfe           = 0;      // Electrons analysis (PWG3)
+Int_t       iPWG3d2h           = 0;      // D0->2 hadrons (PWG3)
+Int_t        iPWG3d0mass       = 0;      // D0 mass (PWG3D2H)                                                                              
+Int_t        iPWG3d0massLS     = 0;      // D0 mass LS (PWG3D2H)                                                                           
+Int_t        iPWG3dplus        = 0;      // D+ analysis (PWG3D2H)                                                                          
+Int_t        iPWG3LSd0         = 0;      // LS D0 analysis (PWG3D2H)                                                                       
+Int_t        iPWG3LSjpsi       = 0;      // LS J/Psi analysis (PWG3D2H)                                                                    
+Int_t        iPWG3CFd0         = 0;      // CF D0 analysis (PWG3D2H)                                                                       
+Int_t        iPWG3promptd0     = 0;      // prompt D0 analysis (PWG3D2H)                                                                   
+Int_t       iPWG3MuonTrain     = 0;      // Muon analysis train
+Int_t       iPWG2femto         = 0;      // Femtoscopy task (PWG2)
+Int_t       iPWG2spectra       = 1;      // Spectra tasks (PWG2
+Int_t        iPWG2protons      = 0;         // Proton-antiproton analysis
+Int_t        iPWG2checkcascade = 1;         // Check cascades task
+Int_t        iPWG2perfcascade  = 0;         // Check performance cascade
+Int_t        iPWG2checkv0      = 0;         // Check V0 task
+Int_t        iPWG2strange      = 0;         // Strangeness task
+Int_t        iPWG2central      = 0;         // Anisothropy in central collisions
+Int_t       iPWG2flow          = 0;      // Flow analysis tasks (PWG2)
 Int_t       iPWG2res           = 0;      // Resonances task (PWG2)
-Int_t       iPWG2kink          = 1;      // Kink analysis task (PWG2)
-Int_t       iPWG2evchar        = 1;      // Event characteristics (PWG2)
-Int_t       iPWG2unicor        = 1;      // Unicor analysis (PWG2)
+Int_t        iPWG2rsneff       = 0;      // Resonances efficiency
+Int_t       iPWG2kink          = 0;      // Kink analysis tasks (PWG2)
+Int_t        iPWG2kinkESDMC    = 0;         // Kink ESD-MC comparison (PWG2)
+Int_t        iPWG2kinkLSKstar  = 0;      // Kink like-sign K* (PWG2)
+Int_t        iPWG2kinkLSL1520  = 0;      // Kink like-sign L1520 (PWG2)
+Int_t        iPWG2kinkLSPhi    = 0;      // Kink like-sign Phi (PWG2)
+Int_t        iPWG2kinkKstarESD = 0;      // Kink Kstar ESD (PWG2)
+Int_t        iPWG2kinkKstarMC  = 0;      // Kink Kstar MC (PWG2)
+Int_t        iPWG2kinkL1520ESD = 0;      // Kink L1520 ESD (PWG2)
+Int_t        iPWG2kinkL1520MC  = 0;      // Kink L1520 MC (PWG2)
+Int_t        iPWG2kinkPhiESD   = 0;      // Kink resonances Phi ESD (PWG2)
+Int_t        iPWG2kinkPhiMC    = 0;      // Kink resonances Phi MC (PWG2)
+Int_t       iPWG2evchar        = 0;      // Event characteristics (PWG2)
+Int_t       iPWG2unicor        = 0;      // Unicor analysis (PWG2)
+Int_t       iPWG2forward       = 0;      // FMD analysis (PWG2)
 
+// ### Configuration macros used for each module
+//==============================================================================
+TString     configPWG2femto    = "$ALICE_ROOT/PWG2/FEMTOSCOPY/macros/Train/Train3/ConfigFemtoAnalysis.C";
+//TString     configPWG3d2h      = "$ALICE_ROOT/PWG3/vertexingHF/ConfigVertexingHF_highmult.C";
+TString     configPWG3d2h      = "$ALICE_ROOT/PWG3/vertexingHF/ConfigVertexingHF.C";
 // Temporaries.
 TString anaPars = "";
 TString anaLibs = "";
 // Function signatures
-class AliAnalysisGrid;
+class AliAnalysisAlien;
 
 //______________________________________________________________________________
 void AnalysisTrainNew(const char *analysis_mode="grid", 
@@ -105,9 +152,9 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
    if (strlen(config_file) && !LoadConfig(config_file)) return;
    TString smode(analysis_mode);
    smode.ToUpper();
-   if (saveTrain)              WriteConfig();
    // Check compatibility of selected modules
    CheckModuleFlags(smode);
+   if (saveTrain)              WriteConfig();
 
    printf("==================================================================\n");
    printf("===========    RUNNING ANALYSIS TRAIN %s IN %s MODE   ==========\n", train_name.Data(),smode.Data());
@@ -118,16 +165,56 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
    if (iESDfilter)   printf("=  ESD filter                                                    =\n");
    if (iMUONcopyAOD) printf("=  MUON copy AOD                                                 =\n");
    if (iJETAN)       printf("=  Jet analysis                                                  =\n");
-   if (iPWG2spectra) printf("=  PWG2 proton, checkCascade, checkV0, strange, performance cascade =\n");
-   if (iPWG2femto)   printf("=  PWG2 femtoscopy                                               =\n");
+   if (iJETANdelta)  printf("=     Jet delta AODs                                             =\n");
+   if (iPWG2spectra) {
+      printf("=  PWG2 SPECTRA tasks :                                          =\n");
+      if (iPWG2protons)      printf("=     PWG2 proton-antiproton                                     =\n");
+      if (iPWG2checkcascade) printf("=     PWG2 check cascades                                        =\n");
+      if (iPWG2perfcascade)  printf("=     PWG2 performance cascades                                  =\n");
+      if (iPWG2checkv0)      printf("=     PWG2 check V0                                              =\n");
+      if (iPWG2strange)      printf("=     PWG2 strangeness                                           =\n");
+      if (iPWG2central)      printf("=     PWG2 central                                               =\n");
+   }   
+   if (iPWG2femto) {
+      printf("=  PWG2 femtoscopy                                               =\n");
+      printf("   +++ configured by: %s\n", configPWG2femto.Data());
+   }   
    if (iPWG2flow)    printf("=  PWG2 flow                                                     =\n");
    if (iPWG2res)     printf("=  PWG2 resonances                                               =\n");
-   if (iPWG2kink)    printf("=  PWG2 kink analysis                                            =\n");
-   if (iPWG2evchar)  printf("=  PWG2 event characteristics                                    =\n");
-   if (iPWG2unicor)    printf("=  PWG2 Unicor analysis                                        =\n");
-   if (iPWG3vertexing) printf("=  PWG3 vertexing                                            =\n");
-   if (iPWG4partcorr)  printf("=  PWG4 gamma-hadron, pi0 and gamma-jet correlations         =\n");
-   if (iPWG4gammaconv) printf("=  PWG4 gamma conversion                                     =\n");
+      if (iPWG2rsneff)    printf("=     PWG2 resonances efficiency                                    =\n");
+   if (iPWG2kink) {
+      printf("=  PWG2 kink analysis tasks:                                     =\n");
+      if (iPWG2kinkESDMC)    printf("=     PWG2 ESD-MC kinks                                          =\n");
+      if (iPWG2kinkLSKstar)  printf("=     PWG2 kink like-sign analysis K*                            =\n");
+      if (iPWG2kinkLSL1520)  printf("=     PWG2 kink like-sign analysis L1520                         =\n");
+      if (iPWG2kinkLSPhi)    printf("=     PWG2 kink like-sign analysis Phi                           =\n");
+      if (iPWG2kinkKstarESD) printf("=     PWG2 kink Kstar ESD analysis                               =\n");
+      if (iPWG2kinkKstarMC)  printf("=     PWG2 kink Kstar MC analysis                                =\n");
+      if (iPWG2kinkL1520ESD) printf("=     PWG2 kink L1520 ESD analysis                               =\n");
+      if (iPWG2kinkL1520MC)  printf("=     PWG2 kink L1520 MC analysis                                =\n");
+      if (iPWG2kinkPhiESD) printf("=     PWG2 kink Phi ESD analysis                                 =\n");
+      if (iPWG2kinkPhiMC)  printf("=     PWG2 kink Phi MC analysis                                  =\n");
+  }   
+   if (iPWG2evchar)    printf("=  PWG2 event characteristics                                    =\n");
+   if (iPWG2unicor)    printf("=  PWG2 Unicor analysis                                          =\n");
+   if (iPWG2forward)   printf("=  PWG2 forward: sharing, density, bkg. correction, dNdEta       =\n");
+   if (iPWG3vertexing) printf("=  PWG3 vertexing                                                =\n");
+   if (iPWG3hfe)       printf("=  PWG3 electrons                                                =\n");
+   if (iPWG3d2h) {
+      printf("=  PWG3 D0->2 hadrons tasks                                      =\n");
+      printf("   +++ configured by: %s\n", configPWG3d2h.Data());
+      if (iPWG3d0mass)       printf("=     PWG3 D0 mass                                               =\n");                               
+      if (iPWG3d0massLS)     printf("=     PWG3 D0 mass LS                                            =\n");                               
+      if (iPWG3dplus)        printf("=     PWG3 D+ analysis                                           =\n");                               
+      if (iPWG3LSd0)         printf("=     PWG3 LS D0                                                 =\n");                               
+      if (iPWG3LSjpsi)       printf("=     PWG3 LS J/Psi                                              =\n");                               
+      if (iPWG3CFd0)         printf("=     PWG3 CF D0                                                 =\n");                               
+      if (iPWG3promptd0)     printf("=     PWG3 prompt D0                                             =\n");                               
+   }         
+   if (iPWG3MuonTrain) printf("=  PWG3 muon train                                               =\n");
+   if (iPWG4partcorr)  printf("=  PWG4 gamma-hadron, pi0 and gamma-jet correlations             =\n");
+   if (iPWG4gammaconv) printf("=  PWG4 gamma conversion                                         =\n");
+   if (iPWG4omega3pi)  printf("=  PWG4 omega to 3 pions                                         =\n");
    printf("==================================================================\n");
    printf(":: use MC truth      %d\n", (UInt_t)useMC);
    printf(":: use KINE filter   %d\n", (UInt_t)useKFILTER);
@@ -154,7 +241,7 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
    // Make the analysis manager and connect event handlers
    AliAnalysisManager *mgr  = new AliAnalysisManager("Analysis Train", "Production train");
    if (saveProofToAlien) mgr->SetSpecialOutputLocation(proof_outdir);
-
+   if (!strcmp(plugin_mode, "test")) mgr->SetNSysInfo(1);
    // Load analysis specific libraries
    if (!LoadAnalysisLibraries(smode)) {
       ::Error("AnalysisTrain", "Could not load analysis libraries");
@@ -165,6 +252,7 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
    if (iAODanalysis) {
    // AOD input handler
       AliAODInputHandler *aodH = new AliAODInputHandler();
+      if (iPWG3d2h) aodH->AddFriend("AliAOD.VertexingHF.root");
       mgr->SetInputEventHandler(aodH);
    } else {   
    // ESD input handler
@@ -185,12 +273,15 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
       aodHandler->SetOutputFileName("AliAOD.root");
       mgr->SetOutputEventHandler(aodHandler);
       if (iAODanalysis) {
+         aodHandler->SetFillAOD(kFALSE);
          aodHandler->SetCreateNonStandardAOD();
-         if (iPWG3vertexing) aodHandler->SetOutputFileName("AliAOD.VertexingHF.root");
+         if (iJETAN)         aodHandler->SetOutputFileName("AliAOD.Jets.root");
+//         if (iPWG3vertexing) aodHandler->SetOutputFileName("AliAOD.VertexingHF.root");
       } 
    }
    // Debugging if needed
    if (useDBG) mgr->SetDebugLevel(3);
+   if (saveCanvases) mgr->SetSaveCanvases(kTRUE);
 
    //==========================================================================
    // Create the chain. In this example it is created only from ALIEN files but
@@ -205,11 +296,52 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
    // For now connection to top input container and common AOD output container
    // is done in this macro, but in future these containers will be connected
    // from each task configuration macro.
+                                                                                                                                           
+   AddAnalysisTasks();                                                                                                                     
+                                                                                                                                           
+   // Run the analysis                                                                                                                     
+   //                                                                                                                                      
+   if (usePLUGIN) {                                                                                                                        
+      AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);                                                                     
+      AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);                                                              
+   }                                                                                                                                       
+                                                                                                                                           
+   if (mgr->InitAnalysis()) {                                                                                                              
+      mgr->PrintStatus();                                                                                                                  
+      if (saveTrain || strlen(config_file)) gSystem->ChangeDirectory(train_name);                                                          
+      StartAnalysis(smode, chain);                                                                                                         
+      if (saveTrain && smode=="GRID") {                                                                                                    
+         AliAnalysisAlien *gridhandler = (AliAnalysisAlien*)mgr->GetGridHandler();                                                         
+         TString alien_workdir = gGrid->GetHomeDirectory();                                                                                
+         if (iAODanalysis) alien_workdir += "analysisAOD";                                                                                 
+         else              alien_workdir += "analysisESD";                                                                                 
+         alien_outdir = gridhandler->GetGridOutputDir();                                                                                   
+         printf("=== Registering ConfigTrain.C in the work directory <%s> ===\n",                                                          
+                alien_workdir.Data());                                                                                                     
+         if (AliAnalysisAlien::FileExists(Form("%s/%sConfig.C", alien_workdir.Data(), train_name.Data())))                                 
+            gGrid->Rm(Form("%s/%sConfig.C", alien_workdir.Data(), train_name.Data()));                                                     
+         if (strcmp(plugin_mode, "test"))                                                                                                  
+            TFile::Cp(Form("file:%sConfig.C",train_name.Data()), Form("alien://%s/%sConfig.C", alien_workdir.Data(), train_name.Data()));  
+      }                                                                                                                                    
+   }                                                                                                                                       
+}                                                                                                                                          
+                                                                                                                                            
+//______________________________________________________________________________                                                           
+void AddAnalysisTasks()                                                                                                                    
+{                                                                                                                                          
+// Add all analysis task wagons to the train                                                                                               
+   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();                                                                     
 
    if (iESDfilter && !iAODanalysis) {
       //  ESD filter task configuration.
       gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskESDFilter.C");
-      AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(useKFILTER);
+      if (iMUONcopyAOD) {
+         printf("Registering delta AOD file\n");
+         mgr->RegisterExtraFile("AliAOD.Muons.root");
+         AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(useKFILTER, kTRUE);
+      } else {
+         AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(useKFILTER, kFALSE);      
+      }   
    }   
 
    // AOD tags
@@ -222,55 +354,108 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
       mgr->ConnectOutput(tagTask, 1, coutTags);
    }   
     
-    // Jet analysis
-   if (iJETAN) {
-      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJets.C");
-      AliAnalysisTaskJets *taskjets = AddTaskJets("AOD", "UA1");
-      if (!taskjets) ::Warning("AnalysisTrainNew", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
-   }
-       
+// ********** PWG2 wagons ******************************************************
+   AliAnalysisManager::SetCommonFileName("PWG2histograms.root");
+
    // Proton analysis
    if (iPWG2spectra) {
       // protons
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskProtons.C");
-      AliAnalysisTaskProtons *taskprotons = AddTaskProtons();
-      if (!taskprotons) ::Warning("AnalysisTrainNew", "AliAnalysisTaskProtons cannot run for this train conditions - EXCLUDED");
+      if (iPWG2protons) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskProtons.C");
+        AliAnalysisTaskProtons *taskprotons = AddTaskProtons();
+        if (!taskprotons) ::Warning("AnalysisTrainNew", "AliAnalysisTaskProtons cannot run for this train conditions - EXCLUDED");
+      }  
       // cascades
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckCascade.C");
-      AliAnalysisTaskCheckCascade *taskcheckcascade = AddTaskCheckCascade();      
-      if (!taskcheckcascade) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckCascade cannot run for this train conditions - EXCLUDED");
+      if (iPWG2checkcascade) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckCascade.C");
+        AliAnalysisTaskCheckCascade *taskcheckcascade = AddTaskCheckCascade(iCollision,runOnData);
+        if (!taskcheckcascade) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckCascade cannot run for this train conditions - EXCLUDED");
+      }  
       // v0's
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckV0.C");
-      AliAnalysisTaskCheckV0 *taskcheckV0 = AddTaskCheckV0();
-      if (!taskcheckV0) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckV0 cannot run for this train conditions - EXCLUDED");
+      if (iPWG2checkv0) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckV0.C");
+        AliAnalysisTaskCheckV0 *taskcheckV0 = AddTaskCheckV0();
+        if (!taskcheckV0) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckV0 cannot run for this train conditions - EXCLUDED");
+      }  
       // strangeness
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskStrange.C");
-      AliAnalysisTaskStrange *taskstrange = AddTaskStrange();
-      if (!taskstrange) ::Warning("AnalysisTrainNew", "AliAnalysisTaskStrange cannot run for this train conditions - EXCLUDED");
+      if (iPWG2strange) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskStrange.C");
+        AliAnalysisTaskStrange *taskstrange = AddTaskStrange();
+        if (!taskstrange) ::Warning("AnalysisTrainNew", "AliAnalysisTaskStrange cannot run for this train conditions - EXCLUDED");
+      }  
       // performance cascades
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckPerformanceCascade.C");
-      AliAnalysisTaskCheckPerformanceCascade *taskperfcascade = AddTaskCheckPerformanceCascade();
-      if (!taskperfcascade) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckPerformanceCascade cannot run for this train conditions - EXCLUDED");
-   }   
+      if (iPWG2perfcascade) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCheckPerformanceCascade.C");
+        AliAnalysisTaskCheckPerformanceCascade *taskperfcascade = AddTaskCheckPerformanceCascade();
+        if (!taskperfcascade) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCheckPerformanceCascade cannot run for this train conditions - EXCLUDED");
+      }  
+       // central
+      if (iPWG2central) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/SPECTRA/macros/AddTaskCentral.C");
+        AliAnalysisTaskCentral *taskcentral = AddTaskCentral();
+        if (!taskcentral) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCentral cannot run for this train conditions - EXCLUDED");
+      }  
+  }   
    
    // Femtoscopy analysis modules
    if (iPWG2femto) {
       gROOT->LoadMacro("$ALICE_ROOT/PWG2/FEMTOSCOPY/macros/AddTaskFemto.C");
-      AliAnalysisTaskFemto *taskfemto = AddTaskFemto();
+      AliAnalysisTaskFemto *taskfemto = AddTaskFemto(configPWG2femto);
       if (!taskfemto) ::Warning("AnalysisTrainNew", "AliAnalysisTaskFemto cannot run for this train conditions - EXCLUDED");
    }   
 
    // Kink analysis
    if (iPWG2kink) {
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKink.C");
-      AliAnalysisKinkESDMC *taskkink = AddTaskKink();
-      if (!taskkink) ::Warning("AnalysisTrainNew", "AliAnalysisKinkESDMC cannot run for this train conditions - EXCLUDED");
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonance.C");
-      AliAnalysisTaskKinkResonance *taskkinkres = AddTaskKinkResonance();
-      if (!taskkinkres) ::Warning("AnalysisTrainNew", "AliAnalysisTaskKinkResonance cannot run for this train conditions - EXCLUDED");
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonanceLikeSign.C");
-      AliResonanceKinkLikeSign *taskkinklikesign = AddTaskKinkResonanceLikeSign();
-      if (!taskkinklikesign) ::Warning("AnalysisTrainNew", "AliResonanceKinkLikeSign cannot run for this train conditions - EXCLUDED");
+      if (iPWG2kinkESDMC) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKink.C");
+        AliAnalysisKinkESDMC *taskkink1 = AddTaskKink();
+        if (!taskkink1) ::Warning("AnalysisTrainNew", "AliAnalysisKinkESDMC cannot run for this train conditions - EXCLUDED");
+      }   
+      if (iPWG2kinkLSKstar) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResLikeSignKstar.C");
+        AliResonanceKinkLikeSign *taskkink2 = AddTaskKinkResLikeSignKstar();
+        if (!taskkink2) ::Warning("AnalysisTrainNew", "AliResonanceKinkLikeSign cannot run for this train conditions - EXCLUDED");
+      }  
+      if (iPWG2kinkLSL1520) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResLikeSignL1520.C");
+        AliResonanceKinkLikeSign *taskkink3 = AddTaskKinkResLikeSignL1520();
+        if (!taskkink3) ::Warning("AnalysisTrainNew", "AliResonanceKinkLikeSign cannot run for this train conditions - EXCLUDED");
+      }  
+      if (iPWG2kinkLSPhi) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResLikeSignPhi.C");
+        AliResonanceKinkLikeSign *taskkink4 = AddTaskKinkResLikeSignPhi();
+        if (!taskkink4) ::Warning("AnalysisTrainNew", "AliResonanceKinkLikeSign cannot run for this train conditions - EXCLUDED");
+      }  
+      if (iPWG2kinkKstarESD) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonanceKstarESD.C");
+        AliAnalysisTaskKinkResonance *taskkink5 = AddTaskKinkResonanceKstarESD();
+        if (!taskkink5) ::Warning("AnalysisTrainNew", "AliAnalysisKinkResonanceKstarESD cannot run for this train conditions - EXCLUDED");
+      }   
+      if (iPWG2kinkKstarMC) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonanceKstarMC.C");
+        AliAnalysisTaskKinkResonance *taskkink6 = AddTaskKinkResonanceKstarMC();
+        if (!taskkink6) ::Warning("AnalysisTrainNew", "AliAnalysisKinkResonanceKstarMC cannot run for this train conditions - EXCLUDED");
+      }   
+      if (iPWG2kinkL1520ESD) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonanceL1520ESD.C");
+        AliAnalysisTaskKinkResonance *taskkink7 = AddTaskKinkResonanceL1520ESD();
+        if (!taskkink7) ::Warning("AnalysisTrainNew", "AliAnalysisKinkResonanceL1520ESD cannot run for this train conditions - EXCLUDED");
+      }   
+      if (iPWG2kinkL1520MC) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonanceL1520MC.C");
+        AliAnalysisTaskKinkResonance *taskkink8 = AddTaskKinkResonanceL1520MC();
+        if (!taskkink8) ::Warning("AnalysisTrainNew", "AliAnalysisKinkResonanceL1520MC cannot run for this train conditions - EXCLUDED");
+      }   
+      if (iPWG2kinkPhiESD) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonancePhiESD.C");
+        AliAnalysisTaskKinkResonance *taskkink9 = AddTaskKinkResonancePhiESD();
+        if (!taskkink9) ::Warning("AnalysisTrainNew", "AliAnalysisKinkResonancePhiESD cannot run for this train conditions - EXCLUDED");
+      }   
+      if (iPWG2kinkPhiMC) {
+        gROOT->LoadMacro("$ALICE_ROOT/PWG2/KINK/macros/AddTaskKinkResonancePhiMC.C");
+        AliAnalysisTaskKinkResonance *taskkink10 = AddTaskKinkResonancePhiMC();
+        if (!taskkink10) ::Warning("AnalysisTrainNew", "AliAnalysisKinkResonancePhiMC cannot run for this train conditions - EXCLUDED");
+      }   
    }   
 
    // Event characterization
@@ -285,8 +470,17 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
    if (iPWG2unicor) {
       gROOT->LoadMacro("$ALICE_ROOT/PWG2/UNICOR/AddTaskUnicor.C");
       AliAnalysisTaskUnicor *taskunicor = AddTaskUnicor();
-      if (!taskspddndeta) ::Warning("AnalysisTrainNew", "AliAnalysisTaskUnicor cannot run for this train conditions - EXCLUDED");
+      if (!taskunicor) ::Warning("AnalysisTrainNew", "AliAnalysisTaskUnicor cannot run for this train conditions - EXCLUDED");
+   }   
+
+   // FMD
+   AliAnalysisManager::SetCommonFileName("forward.root");
+   if (iPWG2forward) {
+      gROOT->LoadMacro("$ALICE_ROOT/PWG2/FORWARD/analysis/AddTaskFMD.C");
+      AliFMDAnalysisTaskSE *taskfmd = AddTaskFMD();
+      if (!taskfmd) ::Warning("AnalysisTrainNew", "AliFMDAnalysisTaskSE cannot run for this train conditions - EXCLUDED");
    }   
+   AliAnalysisManager::SetCommonFileName("PWG2histograms.root");
 
    // Flow analysis
    if (iPWG2flow) {
@@ -306,11 +500,6 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
       // Analysis type can be ESD, AOD, MC, ESDMC0, ESDMC1
       TString type = "AOD";
       if (!iAODanalysis) type = "ESD";
-      if (useMC) {
-         type += "MC";
-         if (!kineFromESD) type += "1";
-         else type += "0";
-      }   
       // Boolean to fill/not fill the QA histograms
       Bool_t QA = kTRUE;   
       // Boolean to use/not use weights for the Q vector
@@ -320,49 +509,108 @@ void AnalysisTrainNew(const char *analysis_mode="grid",
    }   
 
    // PWG2 resonances
+   AliAnalysisManager::SetCommonFileName("resonances.root");
    if (iPWG2res) {
-      gROOT->LoadMacro("$ALICE_ROOT/PWG2/RESONANCES/macros/AddAnalysisTaskRsn.C");
-      AddAnalysisTaskRsn(AliLog::kError, kFALSE, "output_rsn.root", !(Bool_t)iAODhandler);
+      gROOT->LoadMacro("$ALICE_ROOT/PWG2/RESONANCES/macros/train/AddAnalysisTaskRsnNew.C");
+      gROOT->LoadMacro("$ALICE_ROOT/PWG2/RESONANCES/macros/train/AddAnalysisTaskRsnEffNew.C");
+      TString path = gROOT->GetMacroPath();
+      path += ":$ALICE_ROOT/PWG2/RESONANCES/macros/train";
+      gROOT->SetMacroPath(path);
+      AddAnalysisTaskRsn(kTRUE);
+      if (iPWG2rsneff) AddAnalysisTaskRsnEff();
    }   
+   AliAnalysisManager::SetCommonFileName("PWG2histograms.root");
+
+// ********** PWG3 wagons ******************************************************
+   AliAnalysisManager::SetCommonFileName("PWG3histograms.root");
            
    // PWG3 vertexing
    if (iPWG3vertexing) {
       gROOT->LoadMacro("$ALICE_ROOT/PWG3/vertexingHF/AddTaskVertexingHF.C");
+      if (!iPWG3d2h) TFile::Cp(gSystem->ExpandPathName(configPWG3d2h.Data()), Form("%s/ConfigVertexingHF.C", train_name.Data()));
       AliAnalysisTaskSEVertexingHF *taskvertexingHF = AddTaskVertexingHF();
       if (!taskvertexingHF) ::Warning("AnalysisTrainNew", "AliAnalysisTaskSEVertexingHF cannot run for this train conditions - EXCLUDED");
+      else mgr->RegisterExtraFile("AliAOD.VertexingHF.root");
    }   
       
+   // PWG3 electrons
+   if (iPWG3hfe) {
+      gROOT->LoadMacro("$ALICE_ROOT/PWG3/hfe/AddTaskHFE.C");
+      AliAnalysisTaskHFE *taskHFE = AddTaskHFE();
+      if (!taskHFE) ::Warning("AnalysisTrainNew", "AliAnalysisTaskHFE cannot run for this train conditions - EXCLUDED");
+   }   
+
+   // PWG3 D2h
+   if (iPWG3d2h) {
+      gROOT->LoadMacro("$ALICE_ROOT/PWG3/vertexingHF/AddD2HTrain.C");
+      TFile::Cp(gSystem->ExpandPathName(configPWG3d2h.Data()), Form("%s/ConfigVertexingHF.C", train_name.Data()));
+      AddD2HTrain(iPWG3d0mass,iPWG3d0massLS,iPWG3dplus, iPWG3LSd0, iPWG3LSjpsi, iPWG3CFd0, iPWG3promptd0);                                 
+   }   
+
+   // PWG3 muon
+   if (iPWG3MuonTrain) {
+      gROOT->LoadMacro("$ALICE_ROOT/PWG3/muon/AddPWG3MuonTrain.C");
+      // iESDAnalysis, iAODAnalysis -> flags to select if the train is AOD or ESD based
+      // iMuonDistributions, iSingleMuonANAlysis -> flags to switch on/off analysis wagons
+      Int_t isESDAnalysis = (iAODanalysis)?0:1;
+      Int_t isAODAnalysis = (iAODanalysis)?1:0;
+      Int_t addMuonDistributions = 1;
+      Int_t addSingleMuonAnalysis = 1;
+      Int_t addMuonHFAnalysis = 1;
+      Int_t ntaskmuon = AddPWG3MuonTrain(isESDAnalysis,isAODAnalysis,addMuonDistributions,addSingleMuonAnalysis,addMuonHFAnalysis);
+      printf("Added %d muon tasks\n", ntaskmuon);
+   }   
+      
+// ********** PWG4 wagons ******************************************************
+   AliAnalysisManager::SetCommonFileName("PWG4histograms.root");
+
+    // Jet analysis
+   if (iJETAN) {
+      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJets.C");
+      if (!iAODanalysis) {
+         // Default jet reconstructor running on ESD's
+         AliAnalysisTaskJets * taskjets = AddTaskJets();
+         if (!taskjets) ::Warning("AnalysisTrainNew", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
+         if (iJETANdelta) {
+            AddTaskJetsDelta("AliAOD.Jets.root");
+            mgr->RegisterExtraFile("AliAOD.Jets.root");
+         }   
+      } else {
+         // AOD-based analysis. Add all reconstructors to write into delta AOD's
+         if (iJETANdelta) {
+            Int_t ntasksjets = AddTaskJetsDelta("AliAOD.Jets.root");
+            if (ntasksjets) printf("Added %d jet reconstructors\n", ntasksjets);
+         }
+      }      
+   }
    // PWG4 hadron correlations
    if (iPWG4partcorr) {
+      Bool_t isSimulation = (runOnData)?kFALSE:kTRUE;
       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskPartCorr.C");
-      AliAnalysisTaskParticleCorrelation *taskpartcorrPHOS = AddTaskPartCorr("AOD", "PHOS");
+      AliAnalysisTaskParticleCorrelation *taskpartcorrPHOS = AddTaskPartCorr("AOD", "PHOS", useMC, kFALSE, isSimulation);
       if (!taskpartcorrPHOS) ::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation PHOS cannot run for this train conditions - EXCLUDED");
-      AliAnalysisTaskParticleCorrelation *taskpartcorrEMCAL = AddTaskPartCorr("AOD", "EMCAL");
+      AliAnalysisTaskParticleCorrelation *taskpartcorrEMCAL = AddTaskPartCorr("AOD", "EMCAL", useMC, kFALSE, isSimulation);
       if (!taskpartcorrEMCAL) ::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation EMCAL cannot run for this train conditions - EXCLUDED");
+      mgr->RegisterExtraFile("deltaAODPartCorr.root");
    }   
 
    // PWG4 gamma conversion analysis
    if (iPWG4gammaconv) {
       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskGammaConversion.C");
-      TString fileIn = "";
-      TString arguments = " -data-list "+  fileIn+ " -output-file-name common" + " -run-on-train"+" -run-jet"+" -run-chic"+" -run-neutralmeson";
-      AliAnalysisTaskGammaConversion * taskGammaConversion = AddTaskGammaConversion(arguments,mgr->GetCommonInputContainer());
+      TString cdir = gSystem->WorkingDirectory();
+      gSystem->ChangeDirectory(gSystem->ExpandPathName("$ALICE_ROOT/PWG4/macros/"));
+      TString gcArguments = "-run-on-train -run-jet -run-chic -run-neutralmeson -run-cf";
+      AliAnalysisTaskGammaConversion * taskGammaConversion = AddTaskGammaConversion(gcArguments,mgr->GetCommonInputContainer());
+      gSystem->ChangeDirectory(cdir);
       if (!taskGammaConversion) ::Warning("AnalysisTrainNew", "AliAnalysisTaskGammaConversion cannot run for these train conditions - EXCLUDED");
    }   
-   //==========================================================================
-   // FOR THE REST OF THE TASKS THE MACRO AddTaskXXX() is not yet implemented/
-   // Run the analysis
-   //    
-   if (mgr->InitAnalysis()) {
-      mgr->PrintStatus();
-      if (saveTrain || strlen(config_file)) gSystem->ChangeDirectory(train_name);
-      StartAnalysis(smode, chain);
-      if (saveTrain && smode=="GRID") {
-         printf("=== Registering ConfigTrain.C in the output directory <%s> ===\n",
-                alien_outdir.Data());
-         TFile::Cp("file:ConfigTrain.C", Form("alien://%s/ConfigTrain.C", alien_outdir.Data()));
-      }
-   }
+
+   // PWG4 omega to 3 pions analysis
+   if (iPWG4omega3pi) {
+      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskomega3pi.C");
+      AliAnalysisTaskOmegaPi0PiPi *taskomega3pi = AddTaskomega3pi();
+      if (!taskomega3pi) ::Warning("AnalysisTrainNew", "AliAnalysisTaskomega3pi cannot run for these train conditions - EXCLUDED");
+   }   
 }
 
 //______________________________________________________________________________
@@ -413,6 +661,7 @@ void CheckModuleFlags(const char *mode) {
    if (!strcmp(mode, "LOCAL")) imode = 0;
    if (!strcmp(mode, "PROOF")) imode = 1;
    if (!strcmp(mode, "GRID"))  imode = 2;
+   if (!iJETAN) iJETANdelta = 0;
    if (imode==1) {
       if (!usePAR) {
          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PAR files enabled due to PROOF analysis");
@@ -427,8 +676,11 @@ void CheckModuleFlags(const char *mode) {
    // AOD analysis
       if (useMC)
          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "MC usage disabled in analysis on AOD's");
+      if (useAODTAGS)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "AOD tags usage disabled in analysis on AOD's");
       useMC = kFALSE;
       useTR = kFALSE;
+      useAODTAGS = kFALSE;
       if (iESDfilter)
          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "ESD filter disabled in analysis on AOD's");
       iESDfilter   = 0;
@@ -436,23 +688,64 @@ void CheckModuleFlags(const char *mode) {
          if (iJETAN) 
             ::Info("AnalysisTrainNew.C::CheckModuleFlags", "JETAN disabled in analysis on AOD's without AOD handler");
          iJETAN = 0;
-         if (iPWG4gammaconv)
-            ::Info("AnalysisTrainNew.C::CheckModuleFlags", "iPWG4gammaconv disabled on AOD's");
+         iJETANdelta = 0;
       }
       // Disable tasks that do not work yet on AOD data
-      if (iPWG2kink)         
+      if (iPWG4gammaconv)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG4gammaconv disabled on AOD's without AOD handler");
+      iPWG4gammaconv = 0;   
+      if (iPWG2central)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2central disabled on AOD's");
+         iPWG2central = 0;
+      if (iPWG2flow)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2flow disabled on AOD's");
+         iPWG2flow = 0;
+      if (iPWG2femto)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2femto disabled on AOD's");
+         iPWG2femto = 0;
+      if (iPWG2res)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2res disabled on AOD's");
+         iPWG2res = 0;
+       if (iPWG2rsneff)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2rsneff disabled on AOD's");
+         iPWG2rsneff = 0;
+     if (iPWG2kink)         
          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2kink disabled in analysis on AOD's");
          iPWG2kink = 0;
       if (iPWG2unicor)         
          ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2unicor disabled in analysis on AOD's");
          iPWG2unicor = 0;
+      if (iPWG2evchar)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG4evchar disabled on AOD's");
+         iPWG2evchar = 0;
+      if (iPWG2forward)         
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2forward disabled in analysis on AOD's");
+         iPWG2forward = 0;
+      if (iPWG3hfe)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG3hfe disabled on AOD's");
+         iPWG3hfe = 0;
+      if (iPWG4omega3pi)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG4omega3pi disabled on AOD's");
+         iPWG4omega3pi = 0;
    } else {   
    // ESD analysis
-      iMUONcopyAOD = 0;
-   }       
-   if (iJETAN) iESDfilter=1;
-   if (iESDfilter) iAODhandler=1;
-   if (iPWG2spectra || iPWG2flow || iPWG3vertexing) useCORRFW = kTRUE;
+      if (!useMC) useTR = kFALSE;
+      if (!useTR) {
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "iPWG2evchar disabled if not reading track references");
+         iPWG2evchar = 0;
+      }   
+   }
+   if (!useMC) {
+      if (iPWG2perfcascade)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2perfcascade disabled without MC info");
+      iPWG2perfcascade = 0;   
+      if (iPWG2central)
+         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG2central disabled without MC info");
+         iPWG2central = 0;
+   }   
+   if (iJETAN && !iAODanalysis) iESDfilter=1;
+   if (iESDfilter) {iAODhandler=1; useCORRFW = kTRUE;}
+   if (iPWG2spectra || iPWG2flow || iPWG3vertexing || iPWG3hfe || iPWG2res || iPWG2rsneff) useCORRFW = kTRUE;
    if (useKFILTER && !useMC) useKFILTER = kFALSE;
    if (useAODTAGS && !iAODhandler) useAODTAGS = kFALSE;
 }
@@ -545,6 +838,7 @@ Bool_t LoadCommonLibraries(const char *mode)
    gSystem->Load("libGeom.so");
    gSystem->Load("libVMC.so");
    gSystem->Load("libPhysics.so");
+   gSystem->Load("libMinuit.so");
    
    // Load framework classes. Par option ignored here.
    switch (imode) {
@@ -603,23 +897,37 @@ Bool_t LoadAnalysisLibraries(const char *mode)
 {
 // Load common analysis libraries.
    Bool_t success = kTRUE;
-   if (iESDfilter) {
+   if (iESDfilter || iPWG3MuonTrain) {
       if (!LoadLibrary("PWG3base", mode, kTRUE) ||
           !LoadLibrary("PWG3muon", mode, kTRUE)) return kFALSE;
    }   
    // JETAN
    if (iJETAN) {
       if (!LoadLibrary("JETAN", mode, kTRUE)) return kFALSE;
-   }               
+   }
+   if (iJETANdelta) {
+      if (!LoadLibrary("JETAN", mode, kTRUE) ||
+          !LoadLibrary("CGAL", mode, kTRUE) ||
+          !LoadLibrary("fastjet", mode, kTRUE) ||
+          !LoadLibrary("siscone", mode, kTRUE) ||
+          !LoadLibrary("SISConePlugin", mode, kTRUE) ||
+          !LoadLibrary("FASTJETAN", mode, kTRUE)) return kFALSE;
+   }     
    // PWG4 particle correlations
    if (iPWG4partcorr) {   
-      if (!LoadLibrary("PWG4PartCorrBase", mode, kTRUE) ||
+      if (!LoadLibrary("EMCALUtils", mode, kTRUE) ||
+          !LoadLibrary("PHOSUtils", mode, kTRUE) ||
+          !LoadLibrary("PWG4PartCorrBase", mode, kTRUE) ||
           !LoadLibrary("PWG4PartCorrDep", mode, kTRUE)) return kFALSE;
    }
    // PWG4 gamma conversion
    if (iPWG4gammaconv) {
       if (!LoadLibrary("PWG4GammaConv", mode, kTRUE)) return kFALSE;
    }      
+   // PWG4 omega to 3 pions
+   if (iPWG4omega3pi) {
+      if (!LoadLibrary("PWG4omega3pi", mode, kTRUE)) return kFALSE;
+   }      
    // PWG2 task protons 
    if (iPWG2spectra) {
       if (!LoadLibrary("PWG2spectra", mode, kTRUE)) return kFALSE;
@@ -630,7 +938,7 @@ Bool_t LoadAnalysisLibraries(const char *mode)
           !LoadLibrary("PWG2flowTasks", mode, kTRUE)) return kFALSE;
    }
    // PWG2 resonances
-   if (iPWG2res) {
+   if (iPWG2res || iPWG2rsneff) {
       if (!LoadLibrary("PWG2resonances", mode, kTRUE)) return kFALSE;
    }   
    // PWG2 kink
@@ -650,14 +958,23 @@ Bool_t LoadAnalysisLibraries(const char *mode)
       if (!LoadLibrary("PWG2AOD", mode, kTRUE) ||
           !LoadLibrary("PWG2femtoscopy", mode, kTRUE) ||
           !LoadLibrary("PWG2femtoscopyUser", mode, kTRUE)) return kFALSE;
-      TFile::Cp(gSystem->ExpandPathName("$(ALICE_ROOT)/PWG2/FEMTOSCOPY/macros/ConfigFemtoAnalysis.C"), Form("%s/ConfigFemtoAnalysis.C", train_name.Data()));
-      anaLibs += "ConfigFemtoAnalysis.C ";
+//      TFile::Cp(gSystem->ExpandPathName("$(ALICE_ROOT)/PWG2/FEMTOSCOPY/macros/ConfigFemtoAnalysis.C"), Form("%s/ConfigFemtoAnalysis.C", train_name.Data()));
+//      anaLibs += "ConfigFemtoAnalysis.C ";
    }   
-   // Vertexing HF
-   if (iPWG3vertexing) {
+   // PWG2 FORWARD
+   if (iPWG2forward) {
+      if (!LoadLibrary("PWG2forward", mode, kTRUE)) return kFALSE;
+   }   
+   // PWG3 Vertexing HF
+   if (iPWG3vertexing || iPWG3d2h) {
       if (!LoadLibrary("PWG3base", mode, kTRUE) ||
           !LoadLibrary("PWG3vertexingHF", mode, kTRUE)) return kFALSE;
    }   
+   // PWG3 hfe
+   if (iPWG3hfe) {
+      if (!LoadLibrary("PWG3hfe", mode, kTRUE)) return kFALSE;
+   }   
+   
    ::Info("AnalysisTrainNew.C::LoadAnalysisLibraries", "Load other libraries:   SUCCESS");
    return kTRUE;
 }
@@ -770,8 +1087,8 @@ TChain *CreateChain(const char *mode, const char *plugin_mode)
          break;
       case 2:
          if (usePLUGIN) {
-            AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
-            AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
+//            AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
+//            AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
          } else {
             TString           treeName = "esdTree";
             if (iAODanalysis) treeName = "aodTree";
@@ -848,7 +1165,7 @@ Int_t SetupPar(char* pararchivename)
 }
 
 //______________________________________________________________________________
-AliAnalysisGrid* CreateAlienHandler(const char *plugin_mode)
+AliAnalysisAlien* CreateAlienHandler(const char *plugin_mode)
 {
 // Check if user has a valid token, otherwise make one. This has limitations.
 // One can always follow the standard procedure of calling alien-token-init then
@@ -857,10 +1174,14 @@ AliAnalysisGrid* CreateAlienHandler(const char *plugin_mode)
    AliAnalysisAlien *plugin = new AliAnalysisAlien();
 // Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
    plugin->SetRunMode(plugin_mode);
+   if (useProductionMode) plugin->SetProductionMode();
+   if (!outputSingleFolder.IsNull()) plugin->SetOutputSingleFolder(outputSingleFolder);
+   plugin->SetJobTag(job_tag);
    plugin->SetNtestFiles(1);
-   plugin->SetPreferedSE("ALICE::Legnaro::SE");
+//   plugin->SetPreferedSE("ALICE::NIHAM::File");
 // Set versions of used packages
-   plugin->SetAPIVersion("V2.4");
+//   plugin->SetAPIVersion("V2.4");
+   plugin->SetAPIVersion("V1.1x");
    plugin->SetROOTVersion(root_version);
    plugin->SetAliROOTVersion(aliroot_version);
 // Declare input data to be processed.
@@ -871,7 +1192,7 @@ AliAnalysisGrid* CreateAlienHandler(const char *plugin_mode)
    if (iAODanalysis) plugin->SetDataPattern("*AliAOD.root");
    else              plugin->SetDataPattern("*ESD.tag.root");
 // ...then add run numbers to be considered
-   plugin->SetRunRange(run_range[0], run_range[1]);
+   if (!iAODanalysis) plugin->SetRunRange(run_range[0], run_range[1]);
 //   for (Int_t i=0; i<10; i++) {
 //      if (run_numbers[i]==0) break;
 //      plugin->AddRunNumber(run_numbers[i]);
@@ -900,6 +1221,12 @@ AliAnalysisGrid* CreateAlienHandler(const char *plugin_mode)
       while ((objstr=(TObjString*)next())) plugin->EnablePackage(objstr->GetString());
       delete arr;
    } 
+// Add external packages
+   if (iJETANdelta) {
+      plugin->AddExternalPackage("boost::v1_38_0");
+      plugin->AddExternalPackage("cgal::v3.3.1");
+      plugin->AddExternalPackage("fastjet::v2.4.0");
+   }   
    
 // Declare the analysis source files names separated by blancs. To be compiled runtime
 // using ACLiC on the worker nodes.
@@ -917,8 +1244,54 @@ AliAnalysisGrid* CreateAlienHandler(const char *plugin_mode)
    plugin->SetMaxMergeFiles(maxMergeFiles);
    plugin->SetNrunsPerMaster(nRunsPerMaster);
 // Optionally define the files to be archived.
-//   plugin->SetOutputArchive("log_archive.zip:stdout,stderr@ALICE::NIHAM::File root_archive.zip:*.root@ALICE::NIHAM::File");
-   plugin->SetOutputArchive("log_archive.zip:stdout,stderr");
+//   plugin->SetOutputArchive("log_archive.zip:stdout,stderr@ALICE::NIHAM::File root_archive.zip:AliAOD.root,AOD.tag.root@ALICE::NIHAM::File");
+   
+   
+   // Put default output files to archive
+   TString listhists = "";
+   TString listaods  = "";
+   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+   TIter next(mgr->GetOutputs());
+   AliAnalysisDataContainer *output;
+   while ((output=(AliAnalysisDataContainer*)next())) {
+      const char *filename = output->GetFileName();
+      if (!(strcmp(filename, "default"))) {
+         if (!mgr->GetOutputEventHandler()) continue;
+         filename = mgr->GetOutputEventHandler()->GetOutputFileName();
+         if (listaods.Length()) listaods += ",";
+         listaods += filename;
+         listaods += ",";
+         listaods += "pyxsec_hists.root";
+      } else {
+         if (!strcmp(filename, "pyxsec_hists.root")) continue;
+         if (listhists.Contains(filename)) continue;
+         if (listhists.Length()) listhists += ",";
+         listhists += filename;
+      }
+   }
+   if (mgr->GetExtraFiles().Length()) {
+      if (listaods.Length()) listaods += ",";
+      listaods += mgr->GetExtraFiles();
+      listaods.ReplaceAll(" ", ",");
+   }
+   if (listhists.Length()) listhists = Form("hist_archive.zip:%s@%s", listhists.Data(), outputStorages.Data());
+   if (listaods.Length())  listaods  = Form("aod_archive.zip:%s@%s", listaods.Data(), outputStorages.Data());
+   if (!listhists.Length() && !listaods.Length()) {
+      ::Fatal("AnalysisTrainNew", "No task output !");
+   }
+   TString outputArchive = Form("log_archive.zip:stderr@%s", outputStorages.Data());
+   if (listaods.Length()) {
+      outputArchive += " ";
+      outputArchive += listaods;
+   }   
+   if (listhists.Length()) {
+      outputArchive += " ";
+      outputArchive += listhists;
+   }   
+// Set friends
+//   if (iAODanalysis && iPWG3d2h) 
+//      plugin->SetFriendChainName("AliAOD.VertexingHF.root");
+   plugin->SetOutputArchive(outputArchive);
 // Optionally set a name for the generated analysis macro (default MyAnalysis.C)
    plugin->SetAnalysisMacro(Form("%s.C", train_name.Data()));
 // Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
@@ -928,7 +1301,7 @@ AliAnalysisGrid* CreateAlienHandler(const char *plugin_mode)
 // Optionally resubmit threshold.
 //   plugin->SetMasterResubmitThreshold(90);
 // Optionally set time to live (default 30000 sec)
-   plugin->SetTTL(30000);
+   plugin->SetTTL(70000);
 // Optionally set input format (default xml-single)
    plugin->SetInputFormat("xml-single");
 // Optionally modify the name of the generated JDL (default analysis.jdl)
@@ -938,7 +1311,7 @@ AliAnalysisGrid* CreateAlienHandler(const char *plugin_mode)
 // Optionally modify job price (default 1)
    plugin->SetPrice(1);      
 // Optionally modify split mode (default 'se')    
-   plugin->SetSplitMode("se");
+//   plugin->SetSplitMode("se");
    return plugin;
 }
 
@@ -947,13 +1320,13 @@ void WriteConfig()
 {
 // Write train configuration in a file. The file name has the format:
 // train_[trainName]_ddMonthyyyy_time.C
-   gSystem->Exec("date +%d%b%Y_%Hh%M > date.tmp");
-   ifstream fdate("date.tmp");
-   if (!fdate.is_open()) {
-      ::Error("AnalysisTrainNew.C::Export","Could not generate file name");
-      return;
-   }
    if (useDATE) {
+      gSystem->Exec("date +%d%b%Y_%Hh%M > date.tmp");
+      ifstream fdate("date.tmp");
+      if (!fdate.is_open()) {
+         ::Error("AnalysisTrainNew.C::Export","Could not generate file name");
+         return;
+      }
       const char date[64];
       fdate.getline(date,64);
       fdate.close();
@@ -966,7 +1339,7 @@ void WriteConfig()
    gSystem->MakeDirectory(train_name);
    gSystem->ChangeDirectory(train_name);
    ofstream out;
-   out.open("ConfigTrain.C", ios::out); 
+   out.open(Form("%sConfig.C",train_name.Data()), ios::out); 
    if (out.bad()) {
       ::Error("AnalysisTrainNew.C::Export", "Cannot open ConfigTrain.C for writing");
       return;
@@ -1010,15 +1383,49 @@ void WriteConfig()
    out << "   iESDfilter      = " << iESDfilter << ";" << endl;
    out << "   iMUONcopyAOD    = " << iMUONcopyAOD << ";" << endl;
    out << "   iJETAN          = " << iJETAN << ";" << endl;
+   out << "   iJETANdelta     = " << iJETANdelta << ";" << endl;
    out << "   iPWG4partcorr   = " << iPWG4partcorr << ";" << endl;
    out << "   iPWG4gammaconv  = " << iPWG4gammaconv << ";" << endl;
+   out << "   iPWG4omega3pi   = " << iPWG4omega3pi << ";" << endl;
+   out << "   iPWG3vertexing  = " << iPWG3vertexing << ";" << endl;   
+   out << "   iPWG3hfe        = " << iPWG3hfe << ";" << endl;   
+   out << "   iPWG3d2h        = " << iPWG3d2h << ";" << endl;   
+   out << "     iPWG3d0mass       = " << iPWG3d0mass << ";" << endl;
+   out << "     iPWG3d0massLS     = " << iPWG3d0massLS << ";" << endl;
+   out << "     iPWG3dplus        = " << iPWG3dplus << ";" << endl;
+   out << "     iPWG3LSd0         = " << iPWG3LSd0 << ";" << endl;
+   out << "     iPWG3LSjpsi       = " << iPWG3LSjpsi << ";" << endl;
+   out << "     iPWG3CFd0         = " << iPWG3CFd0 << ";" << endl;
+   out << "     iPWG3promptd0     = " << iPWG3promptd0 << ";" << endl;
+   out << "   iPWG3MuonTrain      = " << iPWG3MuonTrain << ";" << endl;   
    out << "   iPWG2femto      = " << iPWG2femto << ";" << endl;
    out << "   iPWG2spectra    = " << iPWG2spectra << ";" << endl;
+   out << "     iPWG2protons      = " << iPWG2protons << ";" << endl;
+   out << "     iPWG2checkcascade = " << iPWG2checkcascade << ";" << endl;
+   out << "     iPWG2perfcascade  = " << iPWG2perfcascade << ";" << endl;
+   out << "     iPWG2checkv0      = " << iPWG2checkv0 << ";" << endl;
+   out << "     iPWG2strange      = " << iPWG2strange << ";" << endl;
+   out << "     iPWG2central      = " << iPWG2central << ";" << endl;
    out << "   iPWG2flow       = " << iPWG2flow << ";" << endl;
    out << "   iPWG2res        = " << iPWG2res << ";" << endl;
+   out << "     iPWG2rsneff   = " << iPWG2rsneff << ";" << endl;
    out << "   iPWG2kink       = " << iPWG2kink << ";" << endl;
+   out << "     iPWG2kinkESDMC    = " << iPWG2kinkESDMC << ";" << endl;
+   out << "     iPWG2kinkLSKstar  = " << iPWG2kinkLSKstar << ";" << endl;
+   out << "     iPWG2kinkLSL1520  = " << iPWG2kinkLSL1520 << ";" << endl;
+   out << "     iPWG2kinkLSPhi    = " << iPWG2kinkLSPhi << ";" << endl;
+   out << "     iPWG2kinkKstarESD = " << iPWG2kinkKstarESD << ";" << endl;
+   out << "     iPWG2kinkKstarMC  = " << iPWG2kinkKstarMC << ";" << endl;
+   out << "     iPWG2kinkL1520ESD = " << iPWG2kinkL1520ESD << ";" << endl;
+   out << "     iPWG2kinkL1520MC  = " << iPWG2kinkL1520MC << ";" << endl;
+   out << "     iPWG2kinkPhiESD   = " << iPWG2kinkPhiESD << ";" << endl;
+   out << "     iPWG2kinkPhiMC    = " << iPWG2kinkPhiMC << ";" << endl;
    out << "   iPWG2unicor     = " << iPWG2unicor << ";" << endl;
    out << "   iPWG2evchar     = " << iPWG2evchar << ";" << endl;
+   out << "   iPWG2forward    = " << iPWG2forward << ";" << endl << endl;
+   out << "// Configuration fot the wagons" << endl;
+   out << "   configPWG2femto = \"" << configPWG2femto << "\";" << endl;
+   out << "   configPWG3d2h   = \"" << configPWG3d2h << "\";" << endl;
    out << "}" << endl;
    ::Info("AnalysisTrainNew.C::WriteConfig", "Train configuration wrote to file %s", Form("config_%s.C", train_name.Data()));
    gSystem->ChangeDirectory(cdir);