Changes for running with background subtraction
[u/mrichter/AliRoot.git] / PWG4 / macros / AnalysisTrainPWG4Jets.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] AnalysisTrainPWG-244Jets("grid", "full")
7 // CAF mode (requires root v5-23-02 + aliroot v4-16-Rev08)
8 //    root[2] AnalysisTrainPWG4Jets("proof")
9 // Local mode requires AliESds.root or AliAOD.root in ./data directory
10 //    root[3] AnalysisTrainPWG4Jets("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] AnalysisTrainPWG4Jets(ana_mode, plugin_mode, "train_default_<date>/ConfigTrain.C")
17
18 // For Usage with fastjet run in "offline" first and then "submit"
19 // jdl and analysismacro are automatically patched after "offline" mode
20
21 // =============================================================================
22 // ### General Steering variables
23 // =============================================================================
24 //== general setup variables
25 TString     kTrainName         = "testAnalysis"; // *CHANGE ME* (no blancs or special characters)
26 TString     kJobTag            = "PWG4 Jet Tasks analysis train configured"; // *CHANGE ME*
27
28 // Usage of par files ONLY in grid mode and ONLY if the code is not available
29 // in the deployed AliRoot versions. Par file search path: local dir, if not there $ALICE_ROOT.
30 // To refresh par files, remove the ones in the workdir, then do "make <target.par>" in 
31 // AliRoot.
32 Bool_t      kUsePAR             = kFALSE;  // use par files for extra libs
33 Bool_t      kUseCPAR            = kFALSE;  // use par files for common libs
34 Bool_t      kFillAOD = kFALSE;  // switch of AOD filling for on the fly analysis
35 Bool_t      kFilterAOD = kTRUE;
36 Int_t       kSaveAOD = 8;        // Bit switch 1 = Full AOD 2 = Jet AOD , 4 = PartCorr, 8 = JCORRAN 
37 //== general input and output variables
38
39 Int_t       iAODanalysis       = 1;      // Analysis on input AOD's
40 Int_t       iAODhandler        = 1;      // Analysis produces an AOD or dAOD's
41 Int_t       iCentralitySelection  = 0;      // Use the centrality
42 Int_t       iESDfilter         = 0;      // ESD to AOD filter (barrel + muon tracks)
43 Int_t       iPhysicsSelection  = 1;      // ESD to AOD filter (barrel + muon tracks)
44 UInt_t      iPhysicsSelectionFlag = 1; // set by pyshics selection and passed to the task, kMB, kUserDefined etc
45 Bool_t      useTender           = kFALSE; // use tender wagon 
46 Bool_t      kUseKinefilter     = kFALSE; // use Kinematics filter
47 Bool_t      kUseMuonfilter     = kFALSE; // use Kinematics filter
48 TString     kCommonOutputFileName = "PWG4_JetTasksOutput.root";
49 TString     kCaloQAOutputFileName = "PWG4_CaloQAOutput.root";
50
51
52 //== general process variables
53
54 // ### Other flags to steer the analysis
55 //==============================================================================
56 Bool_t      kSkipTerminate      = kTRUE; // Do not call Teminate
57 Bool_t      kUseDate            = kFALSE; // use date in train name
58 Bool_t      kUseDebug           = kTRUE; // activate debugging
59 Int_t         kErrorIgnoreLevel = -1; // takes the errror print level from .rootrc 
60 // From TError.h:
61 // const Int_t kUnset    =  -1;
62 // const Int_t kPrint    =   0;
63 // const Int_t kInfo     =   1000;
64 // const Int_t kWarning  =   2000;
65 // const Int_t kError    =   3000;
66 // const Int_t kBreak    =   4000;
67 // const Int_t kSysError =   5000;
68 // const Int_t kFatal    =   6000; 
69 Int_t         kUseSysInfo         = 0; // activate debugging
70 Long64_t    kNumberOfEvents     = 1234567890; // number of events to process from the chain
71 Bool_t      kUseMC              = kTRUE;  // use MC info
72 Bool_t      kIsMC               = kTRUE;  // is MC info, if false it overwrites Use(AOD)MC
73 Bool_t      kUseAODMC           = kTRUE;  // use MC infA
74 Bool_t      kUseESDTags         = kFALSE; // use ESD tags for selection
75 Bool_t      kUseTR              = kFALSE;  // use track references
76 Bool_t      kUseAODTags         = kFALSE;  // use AOD tags
77 Bool_t      kSaveTrain          = kFALSE;  // save train configuration as: 
78 Bool_t      kIsPbPb             = kFALSE;  // Pb+Pb
79
80
81 // ### Analysis modules to be included. Some may not be yet fully implemented.
82 //==============================================================================
83 Int_t       iJETAN             = 1;      // Jet analysis (PWG4) // 1 write standard 2 write non-standard jets, 3 wrtie both
84 Int_t       iJETSUBTRACT        = 1;      // Jet background subtration
85 TString     kDefaultJetBranch     = "";      // is currently set when filled (iJETAN or clusters) or from config macro 
86 TString     kDefaultJetBackgroundBranch     = "";      // is currently set when filled (jet clsuters  
87 TString     kJetSubtractBranches     = "";      // is currently set when filled (jet clsuters  
88
89 TString     kDefaultJetBranchMC     = "";      // is currently set when filled (iJETAN or clusters) or from config macro 
90 TString     kDefaultJetBackgroundBranchMC     = "";      // is currently set when filled (jet clsuters  
91 TString     kDefaultJetBranchMC2     = "";      // is currently set when filled (iJETAN or clusters) or from config macro 
92 TString     kDefaultJetBackgroundBranchMC2     = "";      // is currently set when filled (jet clsuters  
93 TString     kJetSubtractMask1 = "B0";
94 TString     kJetSubtractMask2 = "B%d";
95 Int_t       iDIJETAN           = 1;
96 Int_t       iJETANLib          = 1;
97 Int_t       iPWG1QASym         = 0;      // Eva's QA task compiled on the fly...
98 Int_t       iPWG4FastEmbedding = 0;      // Generate non-standard AOD for embedding
99 Int_t       iPWG4JetTasks      = 0;      // all jet tasks flag for lib laoding
100 Int_t       iPWG4JetServices   = 0;      // jet spectrum analysis
101 Int_t       iPWG4JetSpectrum   = 0;      // jet spectrum analysis
102 Int_t       iPWG4JCORRAN       = 0;      // JCORRAN module
103 Int_t       iPWG4UE            = 0;      // Underlying Event analysis
104 Int_t       iPWG4LeadingUE     = 0;      // Underlying Event analysis
105 Int_t       iPWG4CorrectionsUE = 0;      // Underlying Event analysis
106 Int_t       iPWG4TmpSourceSara = 0;      // Underlying Event analysis not in svn
107 Int_t       iPWG4Fragmentation = 0;      // Official Fragmentation
108 Int_t       iPWG4JetChem       = 0;      // Jet chemistry 
109 Int_t       iPWG4PtQAMC        = 0;      // Marta's QA tasks 
110 Int_t       iPWG4PtSpectra     = 0;      // Marta's QA tasks 
111 Int_t       iPWG4PtQATPC       = 0;      // Marta's QA tasks 
112 Int_t       iPWG4Cosmics     = 0;      // Marta's Cosmics Taks 
113 Int_t       iPWG4ThreeJets     = 0;      // Sona's thrust task
114 Int_t       iPWG4QGSep     = 0;          // Sona's QG Separation task
115 Int_t       iPWG4Minijet       = 0;      // Eva's Mini Jet Task cluster task 
116 Int_t       iPWG4KMeans        = 0;      // Andreas' KMeans task 
117 Int_t       iPWG4Cluster       = 0;      // CKB cluster task 
118 Int_t       iEMCUtilLibs       = 0;      // Flag to satisfy dependence on EMC utils
119 Int_t       iPWG4PartCorrLibs  = 0;      // Gustavo's part corr analysis
120 Int_t       iPWG4PartCorr      = 0;      // Gustavo's part corr analysis
121 Int_t       iPWG4CaloQA        = 0;      // Gustavo's part corr analysis
122 Int_t       iPWG4JetCorr       = 0;     // Paul's jet corr analysis
123 Int_t       iPWG4Tagged        = 0;      // Gustavo's part corr analysis
124 Int_t       iPWG4omega3pi      = 0;      // Omega to 3 pi analysis (PWG4) 
125 Int_t       iPWG4GammaConvLib     = 0;      // Gamma Conversio
126 Int_t       iPWG4GammaConv     = 0;      // Gamma Conversio
127 Int_t       iPWG4CaloConv     = 0;      // Gamma Conversio
128 Int_t       kHighPtFilterMask  = 32;     // change depending on the used AOD Filter
129 TString     kDeltaAODJetName   = "AliAOD.Jets.root";     
130 TString     kDeltaAODJCORRANName   = "AliAOD.JCORRAN.root";     
131 TString     kDeltaAODPartCorrName   = "AliAOD.PartCorr.root";     
132 TString     kFastEmbeddingAOD  = "emb/AliAOD.root";
133
134 //==============================================================================
135 // ### PROOF Steering varibales
136 //==============================================================================
137 //== proof setup variables
138 TString     kProofCluster      = "alicecaf.cern.ch";
139 Bool_t      kProofUseAFPAR     = kFALSE;  // use AF special par file
140 TString     kProofAFversion          = "AF-v4-17";
141 //== proof input and output variables
142 TString     kProofDataSet      = "/COMMON/COMMON/LHC09a4_run8100X#/esdTree";
143 Bool_t      kProofSaveToAlien   = kFALSE; // save proof outputs in AliEn train_[trainName]_ddMonthyyyy_time.C
144 TString     kProofOutdir       = "";
145 Bool_t      kProofClearPackages = kFALSE;
146 Int_t       kProofEvents = 10000;
147 Int_t       kProofOffset = 0;
148 //== proof process variables
149
150
151 //==============================================================================
152 // ### Grid plugin Steering varibiables
153 //==============================================================================
154 //== grid plugin setup variables
155 Bool_t      kPluginUse         = kTRUE;   // do not change
156 Bool_t      kPluginUseProductionMode  = kFALSE;   // use the plugin in production mode
157 TString     kPluginRootVersion       = "v5-27-06b";  // *CHANGE ME IF MORE RECENT IN GRID*
158 TString     kPluginAliRootVersion    = "v4-19-15-AN";  // *CHANGE ME IF MORE RECENT IN GRID*                                          
159 Bool_t      kPluginMergeViaJDL       = kTRUE;  // merge via JDL
160 Bool_t      kPluginFastReadOption   = kFALSE;  // use xrootd tweaks
161 Bool_t      kPluginOverwriteMode    = kTRUE;  // overwrite existing collections
162 Int_t       kPluginOutputToRunNumber = 1;     // write the output to subdirs named after run number
163 // TString kPluginExecutableCommand = "root -b -q";
164 TString     kPluginExecutableCommand = "source /Users/kleinb/setup_32bit_aliroot_trunk_clean_root_trunk.sh; alienroot -b -q ";
165
166 // == grid plugin input and output variables
167 TString     kGridDatadir      = "/alice/sim/PDC_08b/LHC09a1/AOD/";
168 Int_t         kGridMaxRunsFromList = 999999999;
169 Int_t         kGridOffsetRunFromList = 0; // skip the first n runs from the list
170 TString     kGridLocalRunList = "";
171 TString     kGridOutdir       = ""; // AliEn output directory. If blank will become output_<kTrainName>
172 TString     kGridDataSet      = ""; // sub working directory not to confuse different run xmls 
173 Int_t         kGridRunRange[2]       =  {0, -1}; // Set the run range
174 TString     kGridRunPattern        = "%03d"; // important for leading zeroes!!
175 TString     kGridPassPattern       = "";
176 TString     kGridExtraFiles        = ""; // files that will be added to the input list in the JDL...
177 Int_t         kGridMaxMergeFiles      = 12; // Number of files merged in a chunk grid run range
178 TString     kGridMergeExclude       = "AliAOD.root"; // Files that should not be merged
179 TString     kGridOutputStorages      = "disk=2"; // Make replicas on the storages
180 // == grid process variables
181 Int_t       kGridRunsPerMaster     = 100; // Number of runs per master job
182 Int_t       kGridFilesPerJob       = 100; // Maximum number of files per job (gives size of AOD)
183
184 //==============================================================================
185 // ### Local Steering variables
186 //==============================================================================
187 //== local setup variables
188 //== local input and output variables
189 TString     kLocalXMLDataset   = ""; // Change local xml dataset for local interactive analysis
190 TString     kLocalDataList   = "local_deltaaod.txt"; // Change local xml dataset for local interactive analysis
191 // == local process variables
192
193 TString kPluginMode;
194
195
196 // Temporaries.
197 TString anaPars = "";
198 TString anaLibs = "";
199 TString anaLibsExtra = "";
200 TString anaSources = "";
201 // Function signatures
202 class AliAnalysisAlien;
203
204 //______________________________________________________________________________
205 void AnalysisTrainPWG4Jets(const char *analysis_mode="local", 
206                            const char *plugin_mode="",
207                            const char *config_file="",Int_t iOffset = 0,Int_t iTotal = 0)
208 {
209 // Main analysis train macro. If a configuration file is provided, all parameters
210 // are taken from there but may be altered by CheckModuleFlags.
211
212   // these flag may be needed by the config file
213   kPluginMode = plugin_mode;
214   
215   if (strlen(config_file) && !LoadConfig(config_file)) return;
216   if(iTotal>0)kGridMaxRunsFromList = iTotal; // overwrites the settings from config file
217   if(iOffset)kGridOffsetRunFromList = iOffset;
218   if(iOffset)kProofOffset = iOffset;
219
220
221    TString smode(analysis_mode);
222    smode.ToUpper();
223    if (kSaveTrain)WriteConfig();
224    // Check compatibility of selected modules 
225    CheckModuleFlags(smode);
226    //     gROOT->ProcessLine(".trace");
227
228    printf("==================================================================\n");
229    printf("===========    RUNNING ANALYSIS TRAIN %s IN %s MODE   ==========\n", kTrainName.Data(),smode.Data());
230    printf("==================================================================\n");
231                               printf("=  Configuring analysis train for:                               =\n");
232    if (iAODanalysis) printf("=  AOD analysis                                                  =\n");
233    else                     printf("=  ESD analysis                                                  =\n");
234    if (iPhysicsSelection)   printf("=  Physics selection                                                =\n");
235    if(iCentralitySelection)printf("=  Centrality selection                                                =\n");
236    if (useTender)   printf("=  Using tender                                                =\n");
237    if (iESDfilter)   printf("=  ESD filter                                                    =\n");
238    if (iJETAN)       printf("=  Jet analysis                                                  =\n");
239    printf("==================================================================\n");
240
241    char *printMask = ":: %20s  %10d\n";
242    printf(printMask,"Fill AOD",(UInt_t)kFillAOD);
243    printf(printMask,"Save AOD", (UInt_t)kSaveAOD);
244    printf(printMask,"MC truth", (UInt_t)kUseMC);
245    printf(printMask,"KINE filter", (UInt_t)kUseKinefilter);
246    printf(printMask,"track refs", (UInt_t)kUseTR);
247    printf(printMask,"tags", (UInt_t)kUseESDTags);
248    printf(printMask,"AOD tags", (UInt_t)kUseAODTags);
249    printf(printMask,"debugging", (UInt_t)kUseDebug);
250    printf(printMask,"PAR files", (UInt_t)kUsePAR);
251    printf(printMask,"AliEn plugin", (UInt_t)kPluginUse);
252    printf(printMask,"JETAN subtract", (UInt_t)iJETSUBTRACT);
253    printf(printMask,"PWG1 QA sym", iPWG1QASym);
254    printf(printMask,"PWG4 Source Sara",iPWG4TmpSourceSara);
255    printf(printMask,"PWG4 Fragmentation",iPWG4Fragmentation);
256    printf(printMask,"PWG4 Jet Chem",iPWG4JetChem);
257    printf(printMask,"PWG4 Jet tasks",iPWG4JetTasks);
258    printf(printMask,"PWG4 Jet Services",iPWG4JetServices);     
259    printf(printMask,"PWG4 Jet Spectrum",iPWG4JetSpectrum);
260    printf(printMask,"PWG4 JCORRAN",iPWG4JCORRAN);
261    printf(printMask,"PWG4 UE",iPWG4UE); 
262    printf(printMask,"PWG4 Leading UE",iPWG4LeadingUE); 
263    printf(printMask,"PWG4 Corrections UE",iPWG4CorrectionsUE); 
264    printf(printMask,"PWG4 Pt QA MC",iPWG4PtQAMC);
265    printf(printMask,"PWG4 Pt Spectra",iPWG4PtSpectra);
266    printf(printMask,"PWG4 Pt QA TPC",iPWG4PtQATPC);     
267    printf(printMask,"PWG4 Cosmics",iPWG4Cosmics);     
268    printf(printMask,"PWG4 Three Jets",iPWG4ThreeJets);
269    printf(printMask,"PWG4 QGSep",iPWG4QGSep);
270    printf(printMask,"PWG4 Minijet",iPWG4Minijet);
271    printf(printMask,"PWG4 KMeans",iPWG4KMeans);
272    printf(printMask,"PWG4 Cluster",iPWG4Cluster);
273    printf(printMask,"PWG4 Part Corr",iPWG4PartCorr);
274    printf(printMask,"PWG4 Calo QA",iPWG4CaloQA);
275    printf(printMask,"PWG4 Jet Corr",iPWG4JetCorr);
276    printf(printMask,"PWG4 JCORRAN",iPWG4JCORRAN);
277    printf(printMask,"PWG4 Tagged",iPWG4Tagged);
278    printf(printMask,"PWG4 omega to 3 pi ",iPWG4omega3pi);
279    printf(printMask,"PWG4 Gamma Conv",iPWG4GammaConv);
280    printf(printMask,"PWG4 Calo Conv",iPWG4CaloConv);
281    printf(printMask,"HighPt FilterMask",kHighPtFilterMask);    
282    
283    //==========================================================================
284    // Connect to back-end system
285    if (!Connect(smode)) {
286       ::Error("AnalysisTrain", "Could not connect to %s back-end", analysis_mode);
287       return;
288    }   
289
290    // Load common libraries and set include path
291    if (!LoadCommonLibraries(smode)) {
292       ::Error("AnalysisTrain", "Could not load common libraries");
293       return;
294    }
295
296
297     
298    // Make the analysis manager and connect event handlers
299    AliAnalysisManager *mgr  = new AliAnalysisManager("PWG4Train", "pwg4 mini train");
300    if (kCommonOutputFileName.Length()>0)mgr->SetCommonFileName(kCommonOutputFileName.Data());
301    if (kProofSaveToAlien) mgr->SetSpecialOutputLocation(kProofOutdir);
302    mgr->SetNSysInfo(0);
303    if (!strcmp(plugin_mode, "test")) mgr->SetNSysInfo(1);
304    if (kUseSysInfo)mgr->SetNSysInfo(kUseSysInfo);
305    mgr->SetSkipTerminate(kSkipTerminate);
306
307    // Load analysis specific libraries
308    if (!LoadAnalysisLibraries(smode)) {
309       ::Error("AnalysisTrain", "Could not load analysis libraries");
310      return;
311    }   
312
313    // Create input handler (input container created automatically)
314    if (iAODanalysis) {
315    // AOD input handler
316       AliAODInputHandler *aodH = new AliAODInputHandler();
317       mgr->SetInputEventHandler(aodH);
318       //      if (iPWG4JetTasks) aodH->AddFriend(Form("deltas/%s",kDeltaAODJetName.Data()));
319       //      if (iPWG4PartCorr) aodH->AddFriend(Form("deltas/%s"kDeltaAODJetName.Data()));
320    } else {   
321    // ESD input handler
322       AliESDInputHandler *esdHandler = new AliESDInputHandler();
323       if (kUseESDTags) esdHandler->SetReadTags();
324       esdHandler->SetReadFriends(kFALSE);
325       mgr->SetInputEventHandler(esdHandler);       
326       //      if(iPWG4PtQATPC&& !kTrainName.Contains("pass5"))esdHandler->SetActiveBranches("ESDfriend");
327
328    }
329
330    // Monte Carlo handler
331    if (kUseMC && !iAODanalysis) {
332       AliMCEventHandler* mcHandler = new AliMCEventHandler();
333       mgr->SetMCtruthEventHandler(mcHandler);
334       mcHandler->SetReadTR(kUseTR); 
335    }   
336    // AOD output container, created automatically when setting an AOD handler
337    if (iAODhandler) {
338       // AOD output handler
339       AliAODHandler* aodHandler   = new AliAODHandler();
340       aodHandler->SetOutputFileName("AliAOD.root");
341       aodHandler->SetFillAODforRun(kFillAOD);
342       //
343       mgr->SetOutputEventHandler(aodHandler);
344       AliAnalysisDataContainer * cout_aod = mgr->GetCommonOutputContainer();
345       cout_aod->SetSpecialOutput();
346    }
347    // Debugging if needed
348
349    if (kUseDebug){
350      mgr->SetDebugLevel(3);
351    }
352    if(kUseSysInfo>0){
353      mgr->RegisterExtraFile("syswatch.root");
354      if(kGridMergeExclude.Length())kGridMergeExclude += " ";
355      kGridMergeExclude += "syswatch.root";
356    }
357    else{
358      AliLog::SetGlobalLogLevel(AliLog::kError);
359    }
360
361
362    //==========================================================================
363    // Create the chain. In this example it is created only from ALIEN files but
364    // can be done to work in batch or grid mode as well.
365    TChain *chain = CreateChain(smode, plugin_mode);
366         
367    //==========================================================================
368    // Load the tasks configuration macros for all wagons. These files are supposed now to be
369    // in the current workdir, but in AliEn they will be in the file catalog, 
370    // mapped from AliRoot and pecified in the jdl input list.
371     
372    // For now connection to top input container and common AOD output container
373    // is done in this macro, but in future these containers will be connected
374    // from each task configuration macro.
375    
376    //                                                                                                                              
377    // Tender and supplies. Needs to be called for every event.                                                                     
378    //                                                                                                                              
379    if (useTender) {
380       gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/TenderSupplies/AddTaskTender.C");
381       AliAnalysisTaskSE *tender = AddTaskTender(kTRUE);
382       //      tender->SelectCollisionCandidates();                                                                                      
383       tender->SetDebugLevel(2);
384    }
385
386
387
388    if(iPhysicsSelection && !iAODanalysis){
389      gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
390      Int_t iTriggerHIC = 0;
391      Bool_t rejectBkg = true;
392      if(kIsPbPb){
393        iTriggerHIC = 2;
394        rejectBkg = false; // for the moment...
395      }
396      AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection(kIsMC,rejectBkg);  
397      iPhysicsSelectionFlag = AliVEvent::kMB;
398      //     mgr->RegisterExtraFile("EventStat_tmp.root");
399    }
400
401    if(iCentralitySelection){
402
403      // has to run before AOD filter
404      gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
405      //     const char* file1="$ALICE_ROOT/ANALYSIS/macros/AliCentralityBy1D_137161.root";
406      //     const char* file2="$ALICE_ROOT/ANALYSIS/macros/AliCentralityByFunction_137161.root";
407      AliCentralitySelectionTask *taskC = AddTaskCentrality();
408       if (!taskC) ::Warning("AnalysisTrainPWG4Jets", "AliCentralitySelectionTask cannot run for this train conditions - EXCLUDED");
409    }
410
411    if (iESDfilter && !iAODanalysis) {
412       //  ESD filter task configuration.
413       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskESDFilterPWG4Train.C");
414       AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(kUseKinefilter,kUseMuonfilter);
415       taskesdfilter->SetEnableFillAOD(!kFilterAOD);
416
417       if(kIsMC){
418         mgr->RegisterExtraFile("pyxsec_hists.root");
419         if(kGridMergeExclude.Length())kGridMergeExclude += " ";
420         kGridMergeExclude += "pyxsec_hists.root";
421
422       }
423    }   
424
425    // AOD tags
426    if (kUseAODTags) {
427       AliAnalysisTaskTagCreator* tagTask = new AliAnalysisTaskTagCreator("AOD Tag Creator");
428       mgr->AddTask(tagTask);
429       AliAnalysisDataContainer *coutTags = mgr->CreateContainer("cTag",  TTree::Class(), 
430                                            AliAnalysisManager::kOutputContainer, "AOD.tag.root");
431       mgr->ConnectInput (tagTask, 0, mgr->GetCommonInputContainer());
432       mgr->ConnectOutput(tagTask, 1, coutTags);
433    }   
434     
435    if (iPWG4FastEmbedding) {
436      Printf("adding fast embedding task\n");
437      gROOT->LoadMacro("src/AliAnalysisTaskFastEmbedding.cxx++g");
438      gROOT->LoadMacro("macros/AddTaskFastEmbedding.C");
439      AliAnalysisTask *taskEmbedding = AddTaskFastEmbedding(kFastEmbeddingAOD);
440    }
441
442     // Jet analysis
443    if (iJETAN) {
444       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJets.C");
445       AliAnalysisTaskJets *taskjets = 0;
446       if(iJETAN&1){
447         /*
448         taskjets = AddTaskJets(kHighPtFilterMask); 
449         taskjets->SetName("StandardJets");
450         taskjets->SetNonStdBranch("");
451         */
452       }
453       if(iJETAN&2){
454         // Set only few jet finders  backgroudn subtraction w an wo 
455         
456         taskjets = AddTaskJets("AOD","UA1",0.4,kHighPtFilterMask,1.,0); // no background subtraction
457         if(kDeltaAODJetName.Length()>0)taskjets->SetNonStdOutputFile(kDeltaAODJetName.Data());
458
459         // STANDARD UA jet finders pT cut 1 GeV background mode 2 R = 0.4
460         taskjets = AddTaskJets("AOD","UA1",0.4,kHighPtFilterMask,1.,2); // background subtraction
461         if(kDeltaAODJetName.Length()>0)taskjets->SetNonStdOutputFile(kDeltaAODJetName.Data());
462         // EXTRA UA1 Jet finder same as above with R = 0.2
463
464
465         TString cTmp("");
466
467         // SICONE 
468         taskjets = AddTaskJets("AOD","SISCONE",0.4,kHighPtFilterMask,0.15,0); //no background subtraction to be done later....
469         if(kDeltaAODJetName.Length()>0)taskjets->SetNonStdOutputFile(kDeltaAODJetName.Data());
470         cTmp = taskjets->GetNonStdBranch();
471         if(cTmp.Length()>0)kJetSubtractBranches += Form("%s ",cTmp.Data());
472         if(kUseAODMC){
473           // STANDARD UA jet finders pT cut 1 GeV background mode 2 R = 0.4
474           taskjets = AddTaskJets("AODMC","UA1",0.4,kHighPtFilterMask,1.,2); // background subtraction
475           taskjets = AddTaskJets("AODMC2","UA1",0.4,kHighPtFilterMask,1.,2); // background subtraction
476         }
477
478
479         if(kDeltaAODJetName.Length()>0)mgr->RegisterExtraFile(kDeltaAODJetName.Data()); 
480       }
481       if (!taskjets) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
482    }
483
484
485    if (iPWG4FastEmbedding && iJETAN) {
486      AliAnalysisTaskJets *taskEmbJets = AddTaskJets("AODextra", "FASTKT", 0.4, kHighPtFilterMask);
487      taskEmbJets->ReadAODFromOutput();
488      taskEmbJets = AddTaskJets("AODextraonly", "FASTKT", 0.4, kHighPtFilterMask);
489      taskEmbJets->ReadAODFromOutput();
490    }
491
492    if(iPWG4Cluster){
493      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJetCluster.C");
494      AliAnalysisTaskJetCluster *taskCl = 0;
495      Float_t fCenUp = 0;
496      Float_t fCenLo = 0;
497      if(kIsPbPb&&!kIsMC){
498        fCenLo = 0;
499        fCenUp = 80;
500      }
501      if(iPWG4Cluster&1){
502        if(kIsPbPb){
503          taskCl = AddTaskJetCluster("AOD","",kHighPtFilterMask,iPhysicsSelectionFlag,"KT",0.4,0,1, kDeltaAODJetName.Data(),0.15); // this one is for the background and random jets
504          taskCl->SetBackgroundCalc(kTRUE);
505          taskCl->SetCentralityCut(fCenLo,fCenUp);
506          taskCl->SetGhostEtamax(0.9);
507          kDefaultJetBackgroundBranch = Form("%s_%s",AliAODJetEventBackground::StdBranchName(),taskCl->GetJetOutputBranch());
508          
509          taskCl = AddTaskJetCluster("AOD","",kHighPtFilterMask,iPhysicsSelectionFlag,"KT",0.2,0,1, kDeltaAODJetName.Data(),0.15); // this one is for the background and random jets
510          taskCl->SetBackgroundCalc(kTRUE);
511          taskCl->SetCentralityCut(fCenLo,fCenUp);
512          taskCl->SetGhostEtamax(0.9);
513        }
514        else{
515          taskCl = AddTaskJetCluster("AOD","",kHighPtFilterMask,iPhysicsSelectionFlag,"KT",0.6,0,1,kDeltaAODJetName.Data()); // this one is for the background jets
516          taskCl->SetBackgroundCalc(kTRUE);
517          kDefaultJetBackgroundBranch = Form("%s_%s",AliAODJetEventBackground::StdBranchName(),taskCl->GetJetOutputBranch());
518        } 
519
520        taskCl = AddTaskJetCluster("AOD","",kHighPtFilterMask,iPhysicsSelectionFlag,"ANTIKT",0.4,0,1,kDeltaAODJetName.Data(),0.15);
521        taskCl->SetCentralityCut(fCenLo,fCenUp);
522        taskCl->SetBackgroundBranch(kDefaultJetBackgroundBranch.Data());
523        taskCl->SetNRandomCones(10);
524        kDefaultJetBranch = taskCl->GetJetOutputBranch();
525        if(kDeltaAODJetName.Length()==0)taskCl->SetJetTriggerPtCut(40.);
526        kJetSubtractBranches += Form("%s ",taskCl->GetJetOutputBranch());
527   
528        taskCl = AddTaskJetCluster("AOD","",kHighPtFilterMask,iPhysicsSelectionFlag,"ANTIKT",0.2,0,1,kDeltaAODJetName.Data(),0.15);
529        taskCl->SetCentralityCut(fCenLo,fCenUp);
530        taskCl->SetBackgroundBranch(kDefaultJetBackgroundBranch.Data());
531        kJetSubtractBranches += Form("%s ",taskCl->GetJetOutputBranch());
532
533        if(kUseAODMC){
534          if(kIsPbPb){
535            taskCl = AddTaskJetCluster("AODMC","",kHighPtFilterMask,iPhysicsSelectionFlag,"KT",0.4,0,1, kDeltaAODJetName.Data(),0.15); // this one is for the background and random jets
536            taskCl->SetBackgroundCalc(kTRUE);
537            taskCl->SetGhostEtamax(0.9);
538            kDefaultJetBackgroundBranchMC = Form("%s_%s",AliAODJetEventBackground::StdBranchName(),taskCl->GetJetOutputBranch());
539            
540            taskCl = AddTaskJetCluster("AODMC2","",kHighPtFilterMask,iPhysicsSelectionFlag,"KT",0.4,0,1, kDeltaAODJetName.Data(),0.15); // this one is for the background and random jets
541            taskCl->SetBackgroundCalc(kTRUE);
542            taskCl->SetGhostEtamax(0.9);
543            kDefaultJetBackgroundBranchMC2 = Form("%s_%s",AliAODJetEventBackground::StdBranchName(),taskCl->GetJetOutputBranch()); 
544          }
545          else{
546            // pp background calcs...
547          }
548          
549          taskCl = AddTaskJetCluster("AODMC","",kHighPtFilterMask,iPhysicsSelectionFlag,"ANTIKT",0.4,0,1, kDeltaAODJetName.Data(),0.15); // this one is for the background and random jets
550          taskCl->SetGhostEtamax(0.9);
551          taskCl->SetBackgroundBranch(kDefaultJetBackgroundBranchMC.Data());      
552          kDefaultJetBranchMC = taskCl->GetJetOutputBranch();
553
554          taskCl = AddTaskJetCluster("AODMC2","",kHighPtFilterMask,iPhysicsSelectionFlag,"ANTIKT",0.4,0,1, kDeltaAODJetName.Data(),0.15); // this one is for the background and random jets
555          taskCl->SetBackgroundBranch(kDefaultJetBackgroundBranchMC2.Data());     
556          kDefaultJetBranchMC2 = taskCl->GetJetOutputBranch();
557
558        }
559        
560        
561        if (!taskCl) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskCluster cannot run for this train conditions - EXCLUDED");
562      }
563    }
564
565    if(iJETSUBTRACT){
566       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJetBackgroundSubtract.C");
567       AliAnalysisTaskJetBackgroundSubtract *taskSubtract = 0;
568       if(kJetSubtractBranches.Length()){
569
570         taskSubtract = AddTaskJetBackgroundSubtract(kJetSubtractBranches,3,kJetSubtractMask1.Data(),kJetSubtractMask2.Data());
571         taskSubtract->SetBackgroundBranch(kDefaultJetBackgroundBranch.Data());   
572
573         taskSubtract = AddTaskJetBackgroundSubtract(kJetSubtractBranches,2,kJetSubtractMask1.Data(),kJetSubtractMask2.Data());
574         taskSubtract->SetBackgroundBranch(kDefaultJetBackgroundBranch.Data());   
575         taskSubtract->SelectCollisionCandidates(iPhysicsSelectionFlag);
576         //      taskSubtract->SetDebugLevel(3);
577
578         taskSubtract = AddTaskJetBackgroundSubtract(kJetSubtractBranches,1,kJetSubtractMask1.Data(),kJetSubtractMask2.Data());
579         taskSubtract->SetBackgroundBranch(kDefaultJetBackgroundBranch.Data());   
580         taskSubtract->SelectCollisionCandidates(iPhysicsSelectionFlag);
581         if(kJetSubtractBranches.Contains(kDefaultJetBranch.Data())){
582           kDefaultJetBranch.ReplaceAll(taskSubtract->GetToReplace(),Form(taskSubtract->GetReplacementMask(),taskSubtract->GetSubtractionMethod()));
583         }
584       }
585       if (!taskSubtract) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskJetBackgroundSubtrac cannot run for this train conditions - EXCLUDED");     
586    }
587
588    if (iDIJETAN) {
589       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskDiJets.C");
590       AliAnalysisTaskDiJets *taskdijets = 0;
591       if(iDIJETAN&1)taskdijets = AddTaskDiJets(); 
592       if (!taskdijets) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
593       if(iDIJETAN&2){
594         taskdijets = AddTaskDiJets("jetsAOD_CDF07"); 
595         taskdijets = AddTaskDiJets("jetsAOD_DA07"); 
596         taskdijets = AddTaskDiJets("jetsAOD_FASTJET07"); 
597         taskdijets = AddTaskDiJets("jetsAOD_FASTKT07"); 
598         taskdijets = AddTaskDiJets("jetsAOD_SISCONE07"); 
599         taskdijets = AddTaskDiJets("jetsAOD_UA107");
600       }
601    }
602
603    if(iPWG1QASym){
604      gROOT->LoadMacro("$ALICE_ROOT/PWG1/PilotTrain/AddTaskQAsym.C");
605      AliAnalysisTaskQASym *taskQASym = AddTaskQAsym(-1);
606      if (!taskQASym) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskQASym cannot run for this train conditions - EXCLUDED");
607    }
608
609
610
611    if(iPWG4TmpSourceSara){
612      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskEta.C");
613      AliAnalysisTaskEta *taskEta = AddTaskEta();
614      if (!taskEta) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskEta cannot run for this train conditions - EXCLUDED");
615    }
616
617
618    if(iPWG4JetServices){
619      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJetServices.C");
620      AliAnalysisTaskJetServices *taskjetServ = 0;
621      taskjetServ = AddTaskJetServices();
622      if (!taskjetServ) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskJetServices cannot run for this train conditions - EXCLUDED");
623      if(kGridRunRange[0]>0)taskjetServ->SetRunRange(kGridRunRange[0],kGridRunRange[1]);
624      else taskjetServ->SetRunRange(110000,160000);
625      taskjetServ->SetMCData(kIsMC);
626      taskjetServ->SetUsePhysicsSelection((Bool_t)iPhysicsSelection);
627      taskjetServ->SetPhysicsSelectionFlag(iPhysicsSelectionFlag); // 
628      taskjetServ->SetNonStdFile(kDeltaAODJetName.Data());
629      if(kIsPbPb){
630        if(kDeltaAODJetName.Length()>0)taskjetServ->SetFilterAODCollisions(kTRUE);
631        taskjetServ->SetZVertexCut(8.);
632      }
633      if(iAODanalysis){
634        //       taskjetServ->SetDebugLevel(3);
635        taskjetServ->SetAODInput(kTRUE);
636      }
637    }
638
639    if(iPWG4JetSpectrum){
640      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJetSpectrum2.C");
641      AliAnalysisTaskJetSpectrum2 *taskjetSpectrum = 0;
642      if(iPWG4JetSpectrum&1){
643        if(kIsPbPb){
644          for(int i = 0;i<5;i++){
645            TString tmp(kDefaultJetBranch.Data());
646            //      tmp.ReplaceAll(Form(kJetSubtractMask2.Data(),1),kJetSubtractMask1.Data());
647            //      tmp.ReplaceAll(Form(kJetSubtractMask2.Data(),1),kJetSubtractMask1.Data());
648            tmp.ReplaceAll(Form(kJetSubtractMask2.Data(),1),Form(kJetSubtractMask1.Data(),0));
649            taskjetSpectrum = AddTaskJetSpectrum2(kDefaultJetBranch.Data(),tmp.Data(),"",kHighPtFilterMask,AliVEvent::kMB,0,kFALSE,0,i);
650            //      taskjetSpectrum->SetDebugLevel(3);
651            //      taskjetSpectrum->SetMinJetPt(10);
652            taskjetSpectrum->SetTrackEtaWindow(0.8);
653            taskjetSpectrum->SetJetEtaWindow(0.4);
654            if(iAODanalysis)SetAODInput(taskjetSpectrum);
655
656            
657            // check the old subtracted vs. the new subtracted
658            TString tmp2(kDefaultJetBranch.Data());
659            tmp2.ReplaceAll(Form(kJetSubtractMask2.Data(),1),Form(kJetSubtractMask2.Data(),2));
660            taskjetSpectrum = AddTaskJetSpectrum2(tmp2.Data(),kDefaultJetBranch.Data(),"",kHighPtFilterMask,AliVEvent::kMB,0,kFALSE,0,i);
661            //      taskjetSpectrum->SetDebugLevel(3);
662            //      taskjetSpectrum->SetMinJetPt(10);
663            taskjetSpectrum->SetTrackEtaWindow(0.8);
664            taskjetSpectrum->SetJetEtaWindow(0.4);
665            if(iAODanalysis)SetAODInput(taskjetSpectrum);
666
667            // check the old subtracted vs. the new subtracted
668            TString tmp3(kDefaultJetBranch.Data());
669            tmp3.ReplaceAll(Form(kJetSubtractMask2.Data(),1),Form(kJetSubtractMask2.Data(),3));
670
671            taskjetSpectrum = AddTaskJetSpectrum2(tmp3.Data(),kDefaultJetBranch.Data(),"",kHighPtFilterMask,AliVEvent::kMB,0,kFALSE,0,i);
672            //      taskjetSpectrum->SetDebugLevel(3);
673            //      taskjetSpectrum->SetMinJetPt(10);
674            taskjetSpectrum->SetTrackEtaWindow(0.8);
675            taskjetSpectrum->SetJetEtaWindow(0.4);
676            if(iAODanalysis)SetAODInput(taskjetSpectrum);
677
678
679            taskjetSpectrum = AddTaskJetSpectrum2(Form("jetsAOD_UA104_B2_Filter%05d_Cut01000",kHighPtFilterMask),kDefaultJetBranch.Data(),"",kHighPtFilterMask,AliVEvent::kMB,0,kFALSE,0,i);
680            //      taskjetSpectrum->SetDebugLevel(3);
681            //      taskjetSpectrum->SetMinJetPt(10);
682            taskjetSpectrum->SetTrackEtaWindow(0.8);
683            taskjetSpectrum->SetJetEtaWindow(0.4);
684            if(iAODanalysis)SetAODInput(taskjetSpectrum);
685
686          }
687        }
688        if (!taskjetSpectrum) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskJetSpectrum2 cannot run for this train conditions - EXCLUDED");
689      }
690    }
691
692    if(iPWG4Fragmentation){
693      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskFragmentationFunction.C");
694        AliAnalysisTaskFragmentationFunction *taskFrag = 0;
695      if(kUseAODMC){
696
697      }
698      else{
699        taskFrag = AddTaskFragmentationFunction(kDefaultJetBranch.Data(),"","","",kHighPtFilterMask);
700      }
701      if (!taskFrag) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskFragmentationFunction cannot run for this train conditions - EXCLUDED");
702    }
703
704    if(iPWG4JetChem){
705      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJetChem.C");
706      AliAnalysisTask *taskChem = AddTaskJetChem(kHighPtFilterMask);
707      if (!taskChem) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskJetChem cannot run for this train conditions - EXCLUDED");
708    }
709
710
711    
712    if(iPWG4JCORRAN){
713      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJCORRANTask.C");
714      AliJCORRANTask* corran = AddTaskJCORRAN(kDeltaAODJCORRANName.Data(),0);
715      if(!corran)::Warning("AnalysisTrainPWG4Jets", "AliJCORRANTask cannot run for this train conditions - EXCLUDED");
716      else{
717        if(kDeltaAODJCORRANName.Length()>0)mgr->RegisterExtraFile(kDeltaAODJCORRANName.Data()); 
718      }
719    }
720
721    if(iPWG4UE){
722      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskUE.C");
723      AliAnalysisTaskUE *taskUE = 0;
724      if(iPWG4UE&1)taskUE = AddTaskUE(); 
725      if(iPWG4UE&2){
726        taskUE  =AddTaskUE("jetsAOD_CDF04","CDF", "LJ", "TRANSV","MSP"); //finder not yet in train
727        //       taskUE  =AddTaskUE("jetsAOD_CDF07","CDF", "LJ", "TRANSV","MSP");
728        taskUE  =AddTaskUE("jetsAOD_SISCONE04","CDF", "LJ", "TRANSV","MSP");
729        //       taskUE  =AddTaskUE("jetsAOD_SISCONE07","CDF", "LJ", "TRANSV","MSP"); //finder not yet in train
730        taskUE  =AddTaskUE("jetsAOD_ICDF","CDF","LJ","TRANSV","MSP");
731        taskUE  =AddTaskUE("jetsAOD_FASTKT04","CDF", "LJ", "TRANSV","MSP");
732        //       taskUE  =AddTaskUE("jetsAOD_FASTKT07","CDF", "LJ", "TRANSV","MSP"); //finder not yet in train
733        taskUE  =AddTaskUE("jetsAOD_NONE","CDF", "MP_eta05", "TRANSV","MSP");
734        taskUE  =AddTaskUE("jetsAOD_NONE","CDF", "MP_eta09", "TRANSV","MSP");
735      }
736
737      if (!taskUE) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskUE cannot run for this train conditions - EXCLUDED");
738    }
739
740    if(iPWG4LeadingUE){
741      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskLeadingTrackUE.C");
742      AliAnalysisTaskLeadingTrackUE *taskLeadingUE = AddTaskLeadingTrackUE(kUseMC);
743      if (!taskLeadingUE) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTasLeadingTrackkUE cannot run for this train conditions - EXCLUDED");
744      //     taskLeadingUE->SetFilterBit(64);
745    }
746
747
748    if(iPWG4CorrectionsUE){
749      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskCorrectionsUE.C");
750      AliAnalysisTaskCorrectionsUE *taskCorrectionsUE = 0;
751      if(iPWG4CorrectionsUE&1)taskCorrectionsUE = AddTaskCorrectionsUE("jetsAOD_NONE","CDF","MP_eta05","TRANSV","MSP",kFALSE);
752      if (!taskCorrectionsUE) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskCorrectionsUE cannot run for this train conditions - EXCLUDED");
753    }
754
755    if(iPWG4ThreeJets){
756      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskThreeJets.C");
757      AliAnalysisTaskThreeJets *taskThree = AddTaskThreeJets();
758      if(!taskThree)::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskThreets cannot run for this train conditions - EXCLUDED");
759    }
760    if(iPWG4QGSep){
761      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskQGSep.C");
762      AliAnalysisTaskQGSep *taskQGSep = AddTaskQGSep(kUseMC,iAODanalysis);
763      if(!taskQGSep)::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskQGSep cannot run for this train conditions - EXCLUDED");
764    }
765   
766
767    if(iPWG4Minijet){
768      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskMinijet.C");
769      AliAnalysisTaskMinijet *taskMini = AddTaskMinijet(-1,"esd",kUseMC,kGridDataSet);
770      // if we ha highmult trigger add another task
771      if(!taskMini)::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskMinjet cannot run for this train conditions - EXCLUDED");
772    }
773
774
775    if(iPWG4PtQAMC){
776      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskPWG4HighPtQAMC.C");
777      AliPWG4HighPtQAMC *taskQAMC = 0;
778      if(kUseMC){
779        if(iPWG4PtQAMC&1){
780          taskQAMC = AddTaskPWG4HighPtQAMC(kGridDataSet.Data(),0);
781          taskQAMC = AddTaskPWG4HighPtQAMC(kGridDataSet.Data(),1);
782          taskQAMC = AddTaskPWG4HighPtQAMC(kGridDataSet.Data(),2);
783        }
784      }
785      if (!taskQAMC) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskQAMC cannot run for this train conditions - EXCLUDED");
786    }
787
788    if(iPWG4PtQATPC){
789      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskPWG4HighPtQATPConly.C");
790      AliPWG4HighPtQATPConly *taskQATPC = 0;
791      if(iPWG4PtQATPC&1)taskQATPC = AddTaskPWG4HighPtQATPConly(kGridDataSet.Data(),1);
792      if(iPWG4PtQATPC&2)taskQATPC = AddTaskPWG4HighPtQATPConly(kGridDataSet.Data(),2);
793
794  if (!taskQATPC) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskQATPC cannot run for this train conditions - EXCLUDED");
795    }
796
797    if(iPWG4Cosmics){
798      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskPWG4CosmicCandidates.C");
799
800      AliPWG4CosmicCandidates *taskPWG4CosmicCandidates = AddTaskPWG4CosmicCandidates(0);
801      taskPWG4CosmicCandidates = AddTaskPWG4CosmicCandidates(1);
802
803      if (!taskPWG4CosmicCandidates) ::Warning("AnalysisTrainPWG4Jets", "AddTaskPWG4CosmicCandidates cannot run for this train conditions - EXCLUDED");
804    }
805
806
807    if(iPWG4PtSpectra){
808      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskPWG4HighPtSpectra.C");
809      AliPWG4HighPtSpectra *taskPtSpectra = AddTaskPWG4HighPtSpectra(kGridDataSet.Data());
810      if (!taskPtSpectra) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskPtSpectra cannot run for this train conditions - EXCLUDED");
811    }
812    if(iPWG4KMeans){
813      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskKMeans.C");
814      AliAnalysisTaskKMeans *taskKMeans = AddTaskKMeans();
815      if (!taskKMeans) ::Warning("AnalysisTrainPWG4Jets", "AliAnalysisTaskKMenans cannot run for this train conditions - EXCLUDED");
816    }
817
818    if(iPWG4PartCorr){
819      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskPartCorr.C");
820      AliAnalysisTaskParticleCorrelation *taskpartcorrPHOS = AddTaskPartCorr("AOD", "PHOS",kFALSE,kIsMC);
821      if (!taskpartcorrPHOS) ::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation PHOS cannot run for this train conditions - EXCLUDED");
822      AliAnalysisTaskParticleCorrelation *taskpartcorrEMCAL = AddTaskPartCorr("AOD", "EMCAL",kFALSE,kIsMC);
823      if (!taskpartcorrEMCAL) ::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation EMCAL cannot run for this train conditions - EXCLUDED");
824      if(kDeltaAODPartCorrName.Length()>0)mgr->RegisterExtraFile(kDeltaAODPartCorrName.Data());
825    } 
826
827    if(iPWG4CaloQA){
828      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/QA/AddTaskCalorimeterQA.C");
829      AliAnalysisTaskParticleCorrelation *taskcaloQA =  AddTaskCalorimeterQA("ESD",kFALSE,kIsMC,kCaloQAOutputFileName.Data());
830      if(!taskcaloQA)::Warning("AnalysisTrainNew", "AliAnalysisTaskParticleCorrelation QA cannot run - EXCLUDED");
831      //  if(kCaloQAOutputFileName.Length()>0)mgr->RegisterExtraFile(kCaloQAOutputFileName.Data());
832    } 
833
834    if(iPWG4JetCorr){
835      //     using namespace JetCorrelHD;
836      TString cdir = gSystem->WorkingDirectory();
837      gSystem->ChangeDirectory(gSystem->ExpandPathName("$ALICE_ROOT/PWG4/macros/"));
838      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJetCorrel.C");
839      AliAnalysisTaskJetCorrel *taskjetcorr = AddTaskJetCorrel();
840      gSystem->ChangeDirectory(cdir);
841      if (!taskjetcorr) ::Warning("AnalysisTrainNew", "AliAnalysisTaskJetCorrel  cannot run for this train conditions - EXCLUDED");
842    } 
843
844    if(iPWG4Tagged){
845      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskTaggedPhotons.C");
846      AliAnalysisTaskTaggedPhotons * taskTagged = AddTaskTaggedPhotons(kFALSE); // EMCAL
847      taskTagged = AddTaskTaggedPhotons(kTRUE); // PHOS 
848      if (!taskTagged) ::Warning("AnalysisTrainNew", "AliAnalysisTaskTaggedPhotons  cannot run for this train conditions - EXCLUDED");     
849    }
850    if (iPWG4omega3pi) {
851      gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskomega3pi.C");
852      AliAnalysisTaskOmegaPi0PiPi *taskomega3pi = AddTaskomega3pi();
853      if (!taskomega3pi) ::Warning("AnalysisTrainNew", "AliAnalysisTaskomega3pi cannot run\
854  for these train conditions - EXCLUDED");
855    }
856
857    // PWG4 gamma conversion analysis
858    if (iPWG4GammaConv) {
859       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskGammaConversion.C");
860       TString cdir = gSystem->WorkingDirectory();
861       gSystem->ChangeDirectory(gSystem->ExpandPathName("$ALICE_ROOT/PWG4/macros/"));
862       //      TString gcArguments = "-run-on-train -run-jet -run-chic -run-neutralmeson -run-cf";
863       //      TString gcArguments = "-run-on-train -run-jet -run-neutralmeson -run-cf -use-own-xyz";
864       //      TString gcArguments = "-run-on-train -run-jet -run-neutralmeson -run-cf -use-own-xyz";
865       TString gcArguments = "-run-on-train -run-jet -run-omega-meson -use-own-xyz -run-neutralmeson -no-aod";
866       //      TString kGCAnalysisCutSelectionId="9003562000100310";
867       //      TString kGCAnalysisCutSelectionId="9003562000100312";
868       //      gcArguments.Append(Form(" -set-cut-selection %s ",kGCAnalysisCutSelectionId.Data()));
869       if(!kIsMC)gcArguments += " -mc-off";
870       AliAnalysisTaskGammaConversion * taskGammaConversion = AddTaskGammaConversion(gcArguments,mgr->GetCommonInputContainer());
871       gSystem->ChangeDirectory(cdir);
872       taskGammaConversion->SelectCollisionCandidates();
873       if (!taskGammaConversion) ::Warning("AnalysisTrainNew", "AliAnalysisTaskGammaConversion cannot run for these train conditions - EXCLUDED");
874    }   
875
876    if (iPWG4CaloConv) {
877       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskCaloConv.C");
878       AliAnalysisTaskCaloConv * taskCaloConv = AddTaskCaloConv();
879       if (!taskCaloConv) ::Warning("AnalysisTrainNew", "AliAnalysisTaskCaloConv cannot run for these train conditions - EXCLUDED");
880    }   
881
882
883
884    //==========================================================================
885    // FOR THE REST OF THE TASKS THE MACRO AddTaskXXX() is not yet implemented/
886    // Run the analysis
887    //    
888    if (kPluginUse) {
889       AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
890       AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
891    }
892    
893    if (mgr->InitAnalysis()) {
894      mgr->PrintStatus();
895      // if (kSaveTrain || strlen(config_file)) gSystem->ChangeDirectory(kTrainName);
896      if (!strcmp(plugin_mode,"submit")&&smode=="GRID"){
897        TString alien_workdir = gGrid->GetHomeDirectory();
898        if (iAODanalysis) alien_workdir += "analysisAOD";
899        else              alien_workdir += "analysisESD";
900        if(kGridDataSet.Length()>0)alien_workdir += Form("/%s",kGridDataSet.Data());
901        AliAnalysisAlien *gridhandler = (AliAnalysisAlien*)mgr->GetGridHandler();
902        printf("=== AnalysisTrainPWG4Jets:: Registering jdl in the work directory alien://%s/%s, should be done by the manager! ===\n",
903               alien_workdir.Data(),gridhandler->GetGridOutputDir());
904
905        TString dest;
906        dest = Form("%s/%s/%s.jdl",alien_workdir.Data(),gridhandler->GetGridOutputDir(),kTrainName.Data());
907        if(AliAnalysisAlien::FileExists(dest.Data())){
908          //      Printf("%s exist on grid removing...",dest.Data());
909          //      gGrid->Rm(dest.Data());
910        }
911        TFile::Cp(Form("file:%s.jdl",kTrainName.Data()),Form("alien://%s",dest.Data()));
912
913
914        TString dest;
915        dest = Form("%s/%s/%s_merge.jdl",alien_workdir.Data(),gridhandler->GetGridOutputDir(),kTrainName.Data());
916        if(AliAnalysisAlien::FileExists(dest.Data())){
917          //      Printf("%s exist on grid removing...",dest.Data());
918          //      gGrid->Rm(dest.Data());
919        }
920        TFile::Cp(Form("file:%s_merge.jdl",kTrainName.Data()),Form("alien://%s",dest.Data()));
921
922        
923        dest = Form("%s/rootfiles/STEER/LQ1dRef_v1.root",gGrid->GetHomeDirectory());
924        if(AliAnalysisAlien::FileExists(dest.Data())){
925          Printf("%s exist on grid removing...",dest.Data());
926          gGrid->Rm(dest.Data());
927        }
928        TFile::Cp(Form("file:%s/STEER/LQ1dRef_v1.root",
929                       gSystem->ExpandPathName("$ALICE_ROOT")),Form("alien://%s",dest.Data()));
930      }
931      AliLog::SetGlobalLogLevel(AliLog::kError);
932      if((kUseSysInfo>0&&smode=="LOCAL")||!strcmp(plugin_mode, "test")){
933        TFile * fM = TFile::Open("manager_local.root","RECREATE");
934        mgr->Write();
935        fM->Close();
936      }
937      
938      // grmpf, aliroot error handler overwrites root
939      gErrorIgnoreLevel = kErrorIgnoreLevel;
940      if(gErrorIgnoreLevel>3000) AliLog::SetGlobalLogLevel(AliLog::kFatal);
941      StartAnalysis(smode, chain);
942      if((kUseSysInfo>0&&smode=="LOCAL")||!strcmp(plugin_mode, "test")){
943        for(int i = 0;i < mgr->GetTopTasks()->GetEntries();i++){
944          mgr->ProfileTask(i);
945        }
946      }
947      if (!strcmp(plugin_mode, "offline")&&smode=="GRID"){
948        // Offline mode path files
949        //       PatchJDL();
950        //       PatchAnalysisMacro();
951      }
952
953      if (kSaveTrain && smode=="GRID") {
954        AliAnalysisAlien *gridhandler = (AliAnalysisAlien*)mgr->GetGridHandler();
955        TString alien_workdir = gGrid->GetHomeDirectory();
956        if (iAODanalysis) alien_workdir += "analysisAOD";
957        else              alien_workdir += "analysisESD";
958        if(kGridDataSet.Length()>0)alien_workdir += Form("/%s",kGridDataSet.Data());
959        //     kGridOutdir = gridhandler->GetGridOutputDir();
960        printf("=== Registering ConfigTrain.C in the work directory <%s> ===\n",
961                 alien_workdir.Data());
962        if (AliAnalysisAlien::FileExists(Form("%s/%sConfig.C", alien_workdir.Data(), kTrainName.Data())))
963          gGrid->Rm(Form("%s/%sConfig.C", alien_workdir.Data(), kTrainName.Data()));
964        if (strcmp(plugin_mode, "test"))
965          TFile::Cp(Form("file:%sConfig.C",kTrainName.Data()), Form("alien://%s/%sConfig.C", alien_workdir.Data(), kTrainName.Data()));
966      }
967    }
968 }
969
970 //______________________________________________________________________________
971 void StartAnalysis(const char *mode, TChain *chain) {
972 // Start analysis.
973    Int_t imode = -1;
974    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
975    if (!strcmp(mode, "LOCAL")) imode = 0;
976    if (!strcmp(mode, "PROOF")) imode = 1;
977    if (!strcmp(mode, "GRID"))  imode = 2;
978    switch (imode) {
979       case 0:
980          if (!chain) {
981             ::Error("AnalysisTrainPWG4Jets.C::StartAnalysis", "Cannot create the chain");
982             return;
983          }   
984          mgr->StartAnalysis(mode, chain,kNumberOfEvents);
985          return;
986       case 1:
987          if (!kProofDataSet.Length()) {
988             ::Error("AnalysisTrainPWG4Jets.C::StartAnalysis", "kProofDataSet is empty");
989             return;
990          }   
991          mgr->StartAnalysis(mode, kProofDataSet, kProofEvents,kProofOffset);
992          return;
993       case 2:
994          if (kPluginUse) {
995             if (!mgr->GetGridHandler()) {
996                ::Error("AnalysisTrainPWG4Jets.C::StartAnalysis", "Grid plugin not initialized");
997                return;
998             }   
999             mgr->StartAnalysis("grid",chain,kNumberOfEvents);
1000          } else {
1001             if (!chain) {
1002                ::Error("AnalysisTrainPWG4Jets.C::StartAnalysis", "Cannot create the chain");
1003                return;
1004             }   
1005             mgr->StartAnalysis(mode, chain);
1006             mgr->StartAnalysis(mode, chain,kNumberOfEvents);
1007          }   
1008          return;
1009    }      
1010 }          
1011     
1012 //______________________________________________________________________________
1013 void CheckModuleFlags(const char *mode) {
1014 // Checks selected modules and insure compatibility
1015    Int_t imode = -1;
1016    if (!strcmp(mode, "LOCAL")) imode = 0;
1017    if (!strcmp(mode, "PROOF")) imode = 1;
1018    if (!strcmp(mode, "GRID"))  imode = 2;
1019
1020
1021    if (kUseCPAR) {
1022      kPluginAliRootVersion    = ""; // NO aliroot if we use CPAR
1023    }
1024
1025    if (imode==1) {
1026       if (!kUsePAR) {
1027          ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PAR files enabled due to PROOF analysis");
1028          kUsePAR = kTRUE;
1029       }   
1030    }  
1031    if (imode != 2) {
1032       ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "AliEn plugin disabled since not in GRID mode");
1033       kPluginUse = kFALSE; 
1034    }   
1035
1036
1037    if(!kIsMC){
1038      // switch off anthin related to MC
1039      kUseMC = 0;
1040      kUseAODMC = 0;
1041      kUseTR = kFALSE;
1042    }
1043
1044    // Decide if we have PbPb
1045    if(kGridDataSet.CompareTo("LHC10h")==0||kGridDataSet.Contains("LHC10h")) {
1046      Printf("Using settings for Pb+Pb");
1047      kIsPbPb = true;
1048    }
1049
1050    
1051
1052    if (iAODanalysis) {
1053    // AOD analysis
1054       if (kUseMC)
1055          ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "MC usage disabled in analysis on AOD's");
1056       if (kUseAODTags)
1057          ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "AOD tags usage disabled in analysis on AOD's");
1058       kUseMC = kFALSE;
1059       kUseTR = kFALSE;
1060       kUseAODTags = kFALSE;
1061       if (iESDfilter)
1062          ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "ESD filter disabled in analysis on AOD's");
1063       iESDfilter   = 0;
1064       if (iPhysicsSelection)
1065          ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "Physics Selection disabled in analysis on AOD's");
1066       iPhysicsSelection   = 0;
1067       if (!iAODhandler) {
1068          if (iJETAN) 
1069             ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "JETAN disabled in analysis on AOD's without AOD handler");
1070          iJETAN = 0;
1071          iDIJETAN = 0;
1072       }
1073       // Disable tasks that do not work yet on AOD data
1074       if(iPWG4JCORRAN)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4 JCORRAN disabled in analysis on AOD's");
1075       iPWG4JCORRAN = 0;
1076       if( iPWG4PtQAMC)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4 PtQAMC disabled in analysis on AOD's");
1077       iPWG4PtQAMC        = 0;
1078       if( iPWG4PtQATPC)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4 PtTPC disabled in analysis on AOD's");
1079       iPWG4PtQATPC        = 0;
1080       if( iPWG4Cosmics)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4 Comics disabled in analysis on AOD's");
1081       iPWG4Cosmics        = 0;
1082
1083       if( iPWG4PtSpectra)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4 PtQAMC disabled in analysis on AOD's");
1084       iPWG4PtSpectra     = 0;
1085       if(iPWG4KMeans)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4KMeans disabled on AOD's");
1086       iPWG4KMeans       = 0;
1087       if (iPWG4JetCorr)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4Jetcorr disabled on AOD's");
1088       iPWG4JetCorr = 0;
1089       if (iPWG4PartCorr)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4partcorr disabled on AOD's");
1090       iPWG4PartCorr = 0;
1091       if (iPWG4CaloQA)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4CaloQA disabled on AOD's");
1092       iPWG4CaloQA = 0;
1093       if (iPWG4Tagged)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4tagged disabled on AOD's");
1094       iPWG4Tagged = 0;
1095       if (iPWG4omega3pi)
1096         ::Info("AnalysisTrainNew.C::CheckModuleFlags", "PWG4omega3pi disabled on AOD's");
1097       iPWG4omega3pi = 0;
1098       if(iPWG1QASym)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG1 QA Sym disabled in analysis on AOD's");
1099       if (iPWG4GammaConv)::Info("AnalysisPWG4Jets.C::CheckModuleFlags", "PWG4gammaconv disabled on AOD's");
1100       iPWG4GammaConv = 0;   
1101       iPWG1QASym     = 0;
1102       iCentralitySelection = 0;
1103    } else {   
1104    // ESD analysis
1105
1106      if(kIsPbPb){
1107        iCentralitySelection = 1;
1108      }
1109
1110      if (!kUseMC){
1111        kUseTR = kFALSE;
1112        
1113        if(kUseKinefilter)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4 Kine Filter disabled in analysis without MC");
1114        kUseKinefilter = kFALSE;
1115        if( iPWG4PtQAMC)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4 PtQAMC disabled in analysis without MC");
1116        iPWG4PtQAMC        = 0;
1117        if( iPWG4CorrectionsUE)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "PWG4 CorrectionsUE disabled in analysis without MC");
1118        iPWG4CorrectionsUE = 0;
1119      }
1120      if (!kUseTR) {
1121        if(iPWG4PtQAMC)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "iPWG4QATPCMC disabled if not reading track references");
1122        iPWG4PtQAMC        = 0;
1123      }   
1124      if (iJETAN){
1125        iESDfilter=1;
1126      }
1127       if (!iESDfilter){
1128         kUseKinefilter = kFALSE;
1129         kUseMuonfilter = kFALSE;
1130       }
1131       if(!iJETAN){
1132         iPWG4JetSpectrum = iPWG4UE =  iPWG4CorrectionsUE = iPWG4ThreeJets = iPWG4QGSep = iDIJETAN = 0;
1133       }
1134    }
1135    iPWG4JetTasks = iPWG4JetServices||iPWG4JetSpectrum||iPWG4UE||iPWG4LeadingUE||iPWG4PtQAMC||iPWG4PtSpectra||iPWG4PtQATPC||iPWG4Cosmics||iPWG4ThreeJets||iPWG4QGSep||iPWG4JetChem||iPWG4Minijet||iPWG4Fragmentation;
1136    iPWG4PartCorrLibs = iPWG4PartCorr||iPWG4Tagged||iPWG4CaloQA;
1137    iPWG4GammaConvLib = iPWG4GammaConv||iPWG4CaloConv;
1138
1139
1140    iEMCUtilLibs = iPWG4PartCorrLibs||iPWG4JCORRAN||iPWG4GammaConvLib;
1141    iJETANLib = iPWG4JetTasks||iJETAN||iDIJETAN;
1142
1143    if (iESDfilter) {iAODhandler=1;}
1144    if (kUseKinefilter && !kUseMC) kUseKinefilter = kFALSE;
1145    if (kUseAODTags && !iAODhandler) kUseAODTags = kFALSE;
1146
1147
1148    
1149 }
1150
1151 //______________________________________________________________________________
1152 Bool_t Connect(const char *mode) {
1153 // Connect <username> to the back-end system.
1154    Int_t imode = -1;
1155    if (!strcmp(mode, "LOCAL")) imode = 0;
1156    if (!strcmp(mode, "PROOF")) imode = 1;
1157    if (!strcmp(mode, "GRID"))  imode = 2;
1158    TString username = gSystem->Getenv("alien_API_USER");
1159    switch (imode) {
1160       case 0:
1161          break;
1162       case 1:
1163          if  (!username.Length()) {
1164             ::Error(Form("AnalysisTrainPWG4Jets.C::Connect <%s>", mode), "Make sure you:\n \
1165                            1. Have called: alien-token-init <username>\n \
1166                            2. Have called: >source /tmp/gclient_env_$UID");
1167             return kFALSE;
1168          }
1169          ::Info("AnalysisTrainPWG4Jets.C::Connect", "Connecting user <%s> to PROOF cluster <%s>", 
1170                 username.Data(), kProofCluster.Data());
1171          gEnv->SetValue("XSec.GSI.DelegProxy", "2");
1172 //         TProof::Open(Form("%s@%s:31093", username.Data(), kProofCluster.Data()));       
1173          TProof::Open(Form("%s@%s", username.Data(), kProofCluster.Data()));       
1174          if (!gProof) {
1175             if (strcmp(gSystem->Getenv("XrdSecGSISRVNAMES"), "lxfsrd0506.cern.ch"))
1176                ::Error(Form("AnalysisTrainPWG4Jets.C::Connect <%s>", mode), "Environment XrdSecGSISRVNAMES different from lxfsrd0506.cern.ch");
1177             return kFALSE;
1178          }
1179          if(kProofClearPackages)gProof->ClearPackages();
1180
1181          if(kProofSaveToAlien){
1182            TGrid::Connect("alien://");
1183            if (gGrid) {
1184              TString homedir = gGrid->GetHomeDirectory();
1185              TString workdir = homedir + kTrainName;
1186              if (!gGrid->Cd(workdir)) {
1187                gGrid->Cd(homedir);
1188                if (gGrid->Mkdir(workdir)) {
1189                  gGrid->Cd(kTrainName);
1190                  ::Info("AnalysisTrainPWG4Jets::Connect()", "Directory %s created", gGrid->Pwd());
1191                }
1192              }
1193              gGrid->Mkdir("proof_output");
1194              gGrid->Cd("proof_output");
1195              kProofOutdir = Form("alien://%s", gGrid->Pwd());
1196            }   
1197          }
1198          break;
1199       case 2:      
1200          if  (!username.Length()) {
1201             ::Error(Form("AnalysisTrainPWG4Jets.C::Connect <%s>", mode), "Make sure you:\n \
1202                            1. Have called: alien-token-init <username>\n \
1203                            2. Have called: >source /tmp/gclient_env_$UID");
1204             return kFALSE;
1205          }
1206          if (kPluginUse && !gSystem->Getenv("alien_CLOSE_SE")) {
1207             ::Error(Form("AnalysisTrainPWG4Jets.C::Connect <%s>", mode), 
1208                            "When using the AliEn plugin it is preferable to define the \
1209                            variable alien_CLOSE_SE in your environment.");
1210             return kFALSE;
1211          }
1212          ::Info("AnalysisTrainPWG4Jets.C::Connect", "Connecting user <%s> to AliEn ...", 
1213                 username.Data());
1214          TGrid::Connect("alien://");
1215          if (!gGrid || !gGrid->IsConnected()) return kFALSE;
1216          break;
1217       default:
1218          ::Error("AnalysisTrainPWG4Jets.C::Connect", "Unknown run mode: %s", mode);
1219          return kFALSE;
1220    }
1221    ::Info("AnalysisTrainPWG4Jets.C::Connect","Connected in %s mode", mode);
1222    return kTRUE;
1223 }
1224
1225 //______________________________________________________________________________
1226 Bool_t LoadCommonLibraries(const char *mode)
1227 {
1228 // Load common analysis libraries.
1229    Int_t imode = -1;
1230    if (!strcmp(mode, "LOCAL")) imode = 0;
1231    if (!strcmp(mode, "PROOF")) imode = 1;
1232    if (!strcmp(mode, "GRID"))  imode = 2;
1233    if (!gSystem->Getenv("ALICE_ROOT")) {
1234       ::Error("AnalysisTrainPWG4Jets.C::LoadCommonLibraries", "Analysis train requires that ALICE_ROOT is set to pick up Configurations"); 
1235       return kFALSE;
1236    }   
1237    Bool_t success = kTRUE;
1238    // ROOT libraries
1239    gSystem->Load("libTree.so");
1240    gSystem->Load("libGeom.so");
1241    gSystem->Load("libVMC.so");
1242    gSystem->Load("libPhysics.so");
1243    
1244    // Load framework classes. Par option ignored here.
1245    switch (imode) {
1246       case 0:
1247       case 2:
1248          if (kUseCPAR) {
1249             success &= LoadLibrary("STEERBase", mode, kTRUE);
1250             success &= LoadLibrary("ESD", mode, kTRUE);
1251             success &= LoadLibrary("AOD", mode, kTRUE);
1252             success &= LoadLibrary("ANALYSIS", mode, kTRUE);
1253             success &= LoadLibrary("ANALYSISalice", mode, kTRUE);
1254             success &= LoadLibrary("ROOTFILES", mode, kTRUE);
1255             success &= LoadLibrary("EventMixing", mode,kTRUE);
1256             success &= LoadLibrary("CORRFW", mode, kTRUE);
1257          } else {   
1258             success &= LoadLibrary("libSTEERBase.so", mode);
1259             success &= LoadLibrary("libESD.so", mode);
1260             success &= LoadLibrary("libAOD.so", mode);
1261             success &= LoadLibrary("libANALYSIS.so", mode);
1262             success &= LoadLibrary("libANALYSISalice.so", mode);
1263             success &= LoadLibrary("libEventMixing.so", mode);
1264             success &= LoadLibrary("libCORRFW.so", mode);
1265             gROOT->ProcessLine(".include $ALICE_ROOT/include");
1266          }   
1267          break;
1268       case 1:
1269          Int_t ires = -1;
1270          if (kProofUseAFPAR && !gSystem->AccessPathName(kProofAFversion)) ires = gProof->UploadPackage(kProofAFversion);
1271          if (ires < 0) {
1272             success &= LoadLibrary("STEERBase", mode);
1273             success &= LoadLibrary("ESD", mode);
1274             success &= LoadLibrary("AOD", mode);
1275             success &= LoadLibrary("ANALYSIS", mode);
1276             success &= LoadLibrary("ANALYSISalice", mode);
1277             success &= LoadLibrary("EventMixing", mode);
1278             success &= LoadLibrary("CORRFW", mode);
1279          } else { 
1280             ires = gProof->EnablePackage(kProofAFversion);
1281             if (ires<0) success = kFALSE;
1282             success &= LoadLibrary("CORRFW", mode);
1283          }
1284          break;         
1285       default:
1286          ::Error("AnalysisTrainPWG4Jets.C::LoadCommonLibraries", "Unknown run mode: %s", mode);
1287          return kFALSE;
1288    }
1289    if (success) {
1290       ::Info("AnalysisTrainPWG4Jets.C::LoadCommodLibraries", "Load common libraries:    SUCCESS");
1291       ::Info("AnalysisTrainPWG4Jets.C::LoadCommodLibraries", "Include path for Aclic compilation:\n%s",
1292               gSystem->GetIncludePath());
1293    } else {           
1294       ::Info("AnalysisTrainPWG4Jets.C::LoadCommodLibraries", "Load common libraries:    FAILED");
1295    }   
1296       
1297    return success;
1298 }
1299
1300 //______________________________________________________________________________
1301 Bool_t LoadAnalysisLibraries(const char *mode)
1302 {
1303 // Load common analysis libraries.
1304   Bool_t success = kTRUE;
1305   if (useTender) {
1306       if (!LoadLibrary("TENDER", mode, kTRUE) ||
1307           !LoadLibrary("TENDERSupplies", mode, kTRUE)) return kFALSE;
1308    }
1309    if (iESDfilter) {
1310      /*
1311       if (!LoadLibrary("PWG3base", mode, kTRUE) ||
1312           !LoadLibrary("PWG3muon", mode, kTRUE)) return kFALSE;
1313      */
1314    }   
1315    // JETAN
1316    if (iJETANLib) {
1317      // this part needs some rework in case we do not need the fastjed finders for processing
1318      if (!LoadLibrary("JETAN", mode, kTRUE)) return kFALSE;
1319      if (!strcmp(mode, "PROOF")){
1320        gProof->Exec("gSystem->Load\(\"/afs/cern.ch/user/d/dperrino/public/libCGAL.so\"\)", kTRUE); 
1321        gProof->Exec("gSystem->Load\(\"/afs/cern.ch/user/d/dperrino/public/libfastjet.so\"\)", kTRUE); 
1322        // problem when loading siscone copiled with different gcc version??
1323        // gProof->Exec("gSystem->Load\(\"/afs/cern.ch/user/d/dperrino/public/libsiscone.so\"\)", kTRUE); 
1324        gProof->Exec("gSystem->Load\(\"/afs/cern.ch/user/d/dperrino/public/libSISConePlugin.so\"\)", kTRUE);      
1325      }
1326      if(!kUsePAR){ 
1327        if (!LoadLibrary("CGAL", mode, kTRUE)) return kFALSE;
1328        if (!LoadLibrary("fastjet", mode, kTRUE)) return kFALSE;
1329        if (!LoadLibrary("siscone", mode, kTRUE)) return kFALSE;
1330        if (!LoadLibrary("SISConePlugin", mode, kTRUE)) return kFALSE;
1331      }
1332      else{
1333        // par files plus FASTJET needs some extra work... need to change
1334        // the loading sequence in the auto generated .C file
1335        if (!LoadLibrary("libCGAL.so", mode, kTRUE)) return kFALSE;
1336        if (!LoadLibrary("libfastjet.so", mode, kTRUE)) return kFALSE;
1337        if (!LoadLibrary("libsiscone.so", mode, kTRUE)) return kFALSE;
1338        if (!LoadLibrary("libSISConePlugin.so", mode, kTRUE)) return kFALSE;
1339      }
1340      if (!LoadLibrary("FASTJETAN", mode, kTRUE)) return kFALSE;
1341    }
1342    if(iPWG4JetTasks){
1343      if (!LoadLibrary("PWG4JetTasks", mode, kTRUE)) return kFALSE;
1344    }
1345
1346    if(iPWG1QASym){
1347      if (!LoadSource(Form("%s/PWG1/AliAnalysisTaskQASym.cxx",gSystem->ExpandPathName("$ALICE_ROOT")), mode, kTRUE))return kFALSE;
1348    }
1349    if(iPWG4TmpSourceSara){
1350      if(!kUsePAR)gSystem->AddIncludePath("-I$ALICE_ROOT/include/JetTasks"); // ugly hack!!
1351      if(!LoadSource(Form("%s/PWG4/JetTasks/AliAnalysisTaskEta.cxx",gSystem->ExpandPathName("$ALICE_ROOT")), mode, kTRUE))return kFALSE;
1352    }
1353
1354    /*
1355    if(iPWG4JetChem){
1356      if(!kUsePAR)gSystem->AddIncludePath("-I$ALICE_ROOT/include/JetTasks"); // ugly hack!!
1357      if(!LoadSource(Form("%s/PWG4/JetTasks/AliAnalysisTaskJetChem.cxx",gSystem->ExpandPathName("$ALICE_ROOT")), mode, kTRUE))return kFALSE;
1358    }
1359    */
1360
1361    if(iEMCUtilLibs){
1362      if (!LoadLibrary("EMCALUtils", mode, kTRUE) ||
1363          !LoadLibrary("PHOSUtils", mode, kTRUE)) return kFALSE;
1364    }
1365
1366    if (iPWG4PartCorrLibs) {   
1367       if (!LoadLibrary("PWG4PartCorrBase", mode, kTRUE) ||
1368           !LoadLibrary("PWG4PartCorrDep", mode, kTRUE)) return kFALSE;
1369    }
1370    if(iPWG4JCORRAN){
1371      // PWG4 particle correlations
1372      if(!LoadLibrary("PWG4JCORRAN",mode,kTRUE))return kFALSE;
1373    }
1374    if (iPWG4JetCorr) { 
1375      if (!LoadLibrary("PWG4JetCorrel", mode, kTRUE)) return kFALSE;
1376    }  
1377    if (iPWG4omega3pi) {
1378      if (!LoadLibrary("PWG4omega3pi", mode, kTRUE)) return kFALSE;
1379    }
1380    if (iPWG4GammaConvLib) {
1381       if (!LoadLibrary("PWG4GammaConv", mode, kTRUE)) return kFALSE;
1382    }      
1383
1384    ::Info("AnalysisTrainPWG4Jets.C::LoadAnalysisLibraries", "Load other libraries:   SUCCESS");
1385    return kTRUE;
1386 }
1387
1388 //______________________________________________________________________________
1389 Bool_t LoadLibrary(const char *module, const char *mode, Bool_t rec=kFALSE)
1390 {
1391 // Load a module library in a given mode. Reports success.
1392    Int_t imode = -1;
1393    Int_t result;
1394    TString smodule(module);
1395    if (!strcmp(mode, "LOCAL")) imode = 0;
1396    if (!strcmp(mode, "PROOF")) imode = 1;
1397    if (!strcmp(mode, "GRID"))  imode = 2;
1398    TString mod(module);
1399    if (!mod.Length()) {
1400       ::Error("AnalysisTrainPWG4Jets.C::LoadLibrary", "Empty module name");
1401       return kFALSE;
1402    }   
1403    // If a library is specified, just load it
1404    if (smodule.EndsWith(".so")) {
1405       mod.Remove(mod.Index(".so"));
1406       result = gSystem->Load(mod);
1407       if (result < 0) {
1408          ::Error("AnalysisTrainPWG4Jets.C::LoadLibrary", "Could not load library %s", module);
1409          return kFALSE;
1410       }
1411       if (rec) anaLibs += Form("%s.so ",mod.Data()); 
1412       if (rec) anaLibsExtra += Form("%s.so ",mod.Data()); 
1413       return kTRUE;
1414    } 
1415    // Check if the library is already loaded
1416    if (strlen(gSystem->GetLibraries(Form("%s.so", module), "", kFALSE)) > 0)
1417       return kTRUE;    
1418    switch (imode) {
1419       case 0:
1420       case 2:
1421          if (kUsePAR) {
1422             result = SetupPar(module);
1423             if (rec) anaPars += Form("%s.par ", module);
1424          } else {
1425             result = gSystem->Load(Form("lib%s.so", module));
1426             if (rec) anaLibs += Form("lib%s.so ", module);
1427          }   
1428          break;
1429       case 1:
1430         if(!gSystem->AccessPathName(module)){
1431           ::Info("AnalysisTrainPWG4Jets.C::LoadLibrary", "Removing directory %s",module);
1432           gSystem->Exec(Form("rm -rf %s",module));
1433         }
1434          result = gProof->UploadPackage(module);
1435          if (result<0) {
1436             result = gProof->UploadPackage(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", module)));
1437             if (result<0) {
1438                ::Error("AnalysisTrainPWG4Jets.C::LoadLibrary", "Could not find module %s.par in current directory nor in $ALICE_ROOT", module);
1439                return kFALSE;
1440             }
1441          }   
1442          result = gProof->EnablePackage(module);
1443          break;
1444       default:
1445          return kFALSE;
1446    }         
1447    if (result < 0) {
1448       ::Error("AnalysisTrainPWG4Jets.C::LoadLibrary", "Could not load module %s", module);
1449       return kFALSE;
1450    }
1451    return kTRUE;
1452 }           
1453
1454
1455
1456 //______________________________________________________________________________
1457 Bool_t LoadSource(const char *source, const char *mode, Bool_t rec=kFALSE)
1458 {
1459 // Load a module library in a given mode. Reports success.
1460    Int_t imode = -1;
1461    Int_t result = -1;
1462    if (!strcmp(mode, "LOCAL")) imode = 0;
1463    if (!strcmp(mode, "PROOF")) imode = 1;
1464    if (!strcmp(mode, "GRID"))  imode = 2;
1465    TString ssource(source);
1466    TString basename = gSystem->BaseName(ssource.Data());
1467    if (!ssource.Length()) {
1468       ::Error("AnalysisTrainPWG4Jets.C::LoadSource", "Empty task name");
1469       return kFALSE;
1470    }   
1471    // we have a source code so compile it
1472    if (ssource.EndsWith(".cxx")) {
1473      // need to copy it here other wise the path is also used on grid...
1474      ssource.Remove(ssource.Index(".cxx"));
1475      basename.Remove(basename.Index(".cxx"));
1476      Printf("LoadSources:: Copying...  path %s{cxx,h}",ssource.Data());
1477      gSystem->Exec(Form("cp %s.cxx . ",ssource.Data()));
1478      gSystem->Exec(Form("cp %s.h . ",ssource.Data()));
1479      // Path to code
1480      // only needed for local compilation, in grid and proof mode 
1481      // the task headers are uploaded 
1482      //     path.Remove(path.Index(gSystem->BaseName(path.Data())));
1483      // Printf("LoadSources:: Including path %s",path.Data());
1484      //  if(path.Length()>0)gROOT->ProcessLine(Form(".include %s",path.Data()));
1485      Printf("LoadSources:: Loading...  path %s",basename.Data());
1486      switch (imode) {
1487      case 0:
1488        result = gROOT->LoadMacro(Form("%s.cxx++g",basename.Data()));
1489        break;
1490      case 1:
1491        result = gProof->LoadMacro(Form("%s.cxx++g",basename.Data()));
1492        break;
1493      case 2:
1494        result = gROOT->LoadMacro(Form("%s.cxx++g",basename.Data()));
1495        if (rec){
1496          // what we want to compile
1497          anaSources += Form("%s.cxx ",basename.Data()); 
1498          // what we need as input...
1499          anaLibs += Form("%s.cxx %s.h ",basename.Data(),basename.Data()); 
1500        }
1501        break;
1502      default:
1503        return kFALSE;
1504      }
1505    } 
1506    if (result < 0) {
1507       ::Error("AnalysisTrainPWG4Jets.C::LoadSources", "Could not load source %s", source);
1508       return kFALSE;
1509    }
1510    return kTRUE;
1511 }           
1512
1513
1514 //______________________________________________________________________________
1515 TChain *CreateChain(const char *mode, const char *plugin_mode)
1516 {
1517 // Create the input chain
1518    Int_t imode = -1;
1519    if (!strcmp(mode, "LOCAL")) imode = 0;
1520    if (!strcmp(mode, "PROOF")) imode = 1;
1521    if (!strcmp(mode, "GRID"))  imode = 2;
1522    TChain *chain = NULL;
1523    // Local chain
1524    switch (imode) {
1525       case 0:
1526          if (iAODanalysis) {
1527             if (!kLocalXMLDataset.Length()) {
1528                // Local AOD
1529                chain = new TChain("aodTree");
1530                TString line;
1531                ifstream in;
1532                in.open(kLocalDataList.Data());
1533                while (in.good()) {
1534                  in >> line;
1535                  if (line.Length() == 0) continue;
1536                  // cout << " line = " << line << endl;
1537                  chain->Add(line.Data());
1538                }       
1539             } else {
1540                // Interactive AOD
1541                chain = CreateChainSingle(kLocalXMLDataset, "aodTree");
1542             }
1543          } else {      
1544            if (!kLocalXMLDataset.Length()) {
1545              // Local ESD
1546              chain = new TChain("esdTree");
1547              TString line;
1548              ifstream in;
1549              in.open(kLocalDataList.Data());
1550              while (in.good()) {
1551                in >> line;
1552                if (line.Length() == 0) continue;
1553                cout << " line = " << line << endl;
1554                chain->Add(line.Data());
1555              }       
1556            } else {
1557              // Interactive ESD
1558                chain = CreateChainSingle(kLocalXMLDataset, "esdTree");
1559            }   
1560          }
1561          break;
1562       case 1:
1563          break;
1564       case 2:
1565          if (kPluginUse) {
1566 //            AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
1567 //            AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
1568          } else {
1569             TString           treeName = "esdTree";
1570             if (iAODanalysis) treeName = "aodTree";
1571             chain = CreateChainSingle("wn.xml", treeName);
1572          }
1573          break;      
1574       default:   
1575    }
1576    if (chain && chain->GetNtrees()) return chain;
1577    return NULL;
1578 }   
1579
1580 //______________________________________________________________________________
1581 TChain* CreateChainSingle(const char* xmlfile, const char *treeName)
1582 {
1583    printf("*******************************\n");
1584    printf("*** Getting the ESD Chain   ***\n");
1585    printf("*******************************\n");
1586    TAlienCollection * myCollection  = TAlienCollection::Open(xmlfile);
1587
1588    if (!myCollection) {
1589       ::Error("AnalysisTrainPWG4Jets.C::CreateChainSingle", "Cannot create an AliEn collection from %s", xmlfile) ;
1590       return NULL ;
1591    }
1592
1593    TChain* chain = new TChain(treeName);
1594    myCollection->Reset() ;
1595    while ( myCollection->Next() ) chain->Add(myCollection->GetTURL("")) ;
1596    chain->ls();
1597    return chain;
1598 }
1599
1600 //______________________________________________________________________________
1601 Int_t SetupPar(char* pararchivename)
1602 {
1603    if (!pararchivename || !strlen(pararchivename)) return -1;
1604    char processline[1024];
1605    if (gSystem->AccessPathName(Form("%s.par", pararchivename))) {
1606       if (!gSystem->AccessPathName(Form("%s/%s.par", gSystem->Getenv("ALICE_ROOT"),pararchivename))) {
1607          ::Info("AnalysisTrainPWG4Jets.C::SetupPar", "Getting %s.par from $ALICE_ROOT", pararchivename);
1608         TFile::Cp(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", pararchivename)), 
1609                   Form("%s.par",pararchivename));
1610       } else {
1611          ::Error("AnalysisTrainPWG4Jets.C::SetupPar", "Cannot find %s.par", pararchivename);
1612          return -1;
1613       }   
1614    }
1615    if (kPluginUse && kSaveTrain) gSystem->Exec(Form("ln -s ../%s.par %s",pararchivename, kTrainName.Data()));
1616    gSystem->Exec(Form("tar xvzf %s.par", pararchivename));
1617
1618    TString ocwd = gSystem->WorkingDirectory();
1619    if (!gSystem->ChangeDirectory(pararchivename)) return -1;
1620         
1621    // check for BUILD.sh and execute
1622    if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
1623       printf("*******************************\n");
1624       printf("*** Building PAR archive    ***\n");
1625       printf("*******************************\n");          
1626       if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
1627          Error("runProcess","Cannot Build the PAR Archive! - Abort!");
1628          return -1;
1629       }
1630    }
1631
1632         // check for SETUP.C and execute
1633         if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
1634             printf("*******************************\n");
1635             printf("*** Setup PAR archive       ***\n");
1636             printf("*******************************\n");
1637             gROOT->Macro("PROOF-INF/SETUP.C");
1638         }       
1639         if (!gSystem->ChangeDirectory(ocwd.Data())){
1640           Error("Change back directory",Form("Cannot change to %s",ocwd.Data()));
1641           return -1;
1642         }
1643    return 0;
1644 }
1645
1646 //______________________________________________________________________________
1647 AliAnalysisAlien* CreateAlienHandler(const char *plugin_mode)
1648 {
1649 // Check if user has a valid token, otherwise make one. This has limitations.
1650 // One can always follow the standard procedure of calling alien-token-init then
1651 //   source /tmp/gclient_env_$UID in the current shell.
1652 //   if (!AliAnalysisGrid::CreateToken()) return NULL;
1653    AliAnalysisAlien *plugin = new AliAnalysisAlien();
1654 // Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
1655    plugin->SetRunMode(plugin_mode);
1656    if (kPluginUseProductionMode) plugin->SetProductionMode();
1657    plugin->SetJobTag(kJobTag);
1658    plugin->SetNtestFiles(1);
1659 //   plugin->SetPreferedSE("ALICE::NIHAM::File");
1660 // Set versions of used packages
1661    plugin->SetAPIVersion("V1.1x");
1662    //   plugin->SetAPIVersion("V1.0x");
1663 //   plugin->SetAPIVersion("V2.4");
1664    plugin->SetROOTVersion(kPluginRootVersion);
1665    plugin->SetAliROOTVersion(kPluginAliRootVersion);
1666 // Declare input data to be processed.
1667 // Method 1: Create automatically XML collections using alien 'find' command.
1668 // Define production directory LFN
1669    plugin->SetGridDataDir(kGridDatadir.Data());
1670 // Set data search pattern
1671    if (iAODanalysis) plugin->SetDataPattern(" *AliAOD.Jets.root");
1672    //   else              plugin->SetDataPattern(Form(" %s/*/*ESD.tag.root",kGridPassPattern.Data()));
1673    else              plugin->SetDataPattern(Form(" %s/*/*AliESDs.root",kGridPassPattern.Data()));
1674 // ...then add run numbers to be considered
1675 //   plugin->SetRunRange(kGridRunRange[0], kGridRunRange[1]);
1676    for (Int_t i=kGridRunRange[0]; i<=kGridRunRange[1]; i++) {
1677      Printf("AnalysisTrainPWG4Jets Adding run number %s", Form(kGridRunPattern.Data(),i));
1678      plugin->AddRunNumber(Form(kGridRunPattern.Data(),i));
1679    }   
1680
1681    if(kGridLocalRunList.Length()>0){
1682      ifstream in1;
1683      in1.open(kGridLocalRunList.Data());
1684      int iRun;
1685      int icount = 0;
1686      /*
1687      char c;
1688      char cLine[250];
1689      while(!in1.eof()){
1690        c = in1.get();
1691        if ( (c >= '0') && (c <= '9') )
1692          {
1693            in1.putback (c)s;
1694            in1>>iRun;
1695            Printf("AnalysisTrainPWG4Jets Adding run number from File %s", Form(kGridRunPattern.Data(),iRun));
1696            plugin->AddRunNumber(Form(kGridRunPattern.Data(),iRun));
1697        }
1698      else
1699        {
1700          in1.putback (c);
1701          in1.getline(cLine,250);
1702
1703        }
1704      }
1705      */
1706      
1707      Int_t nRun = 0;
1708
1709      // just use run numbers, negatives will be excluded
1710      while(in1>>iRun){
1711        if(iRun>=0){
1712          if(iRun>=0&&nRun>=kGridOffsetRunFromList&&(nRun<kGridMaxRunsFromList)){
1713            Printf("AnalysisTrainPWG4Jets Adding run number from File %s", Form(kGridRunPattern.Data(),iRun));
1714            plugin->AddRunNumber(Form(kGridRunPattern.Data(),iRun));
1715
1716          }
1717          else{
1718            Printf("AnalysisTrainPWG4Jets Skipping run number from File %d", iRun);
1719          }
1720          nRun++;
1721        }
1722        else{
1723          Printf("AnalysisTrainPWG4Jets Skipping run number from File %d", iRun);
1724        }
1725      }
1726    }
1727
1728 // Method 2: Declare existing data files (raw collections, xml collections, root file)
1729 // If no path mentioned data is supposed to be in the work directory (see SetGridWorkingDir())
1730 // XML collections added via this method can be combined with the first method if
1731 // the content is compatible (using or not tags)
1732 //   plugin->AddDataFile("tag.xml");
1733 //   plugin->AddDataFile("/alice/data/2008/LHC08c/000057657/raw/Run57657.Merged.RAW.tag.root");
1734 // Define alien work directory where all files will be copied. Relative to alien $HOME.
1735    TString alien_workdir = "";
1736    
1737    if (iAODanalysis)  alien_workdir += "analysisAOD";
1738    else               alien_workdir += "analysisESD";    
1739    if(kGridDataSet.Length()>0)alien_workdir += Form("/%s",kGridDataSet.Data());
1740    plugin->SetGridWorkingDir(alien_workdir.Data());
1741
1742    // Declare alien output directory. Relative to working directory.
1743    if (!kGridOutdir.Length()) kGridOutdir = Form("output_%s",kTrainName.Data());
1744    plugin->SetGridOutputDir(kGridOutdir);
1745
1746    // Add external packages
1747    if (iJETAN||iDIJETAN) {
1748       plugin->AddExternalPackage("boost::v1_43_0");
1749       plugin->AddExternalPackage("cgal::v3.6");
1750       plugin->AddExternalPackage("fastjet::v2.4.2");
1751    }   
1752
1753
1754    // set extra libs before par file compilation
1755    anaLibs += kGridExtraFiles;
1756    anaLibs     = anaLibs.Strip();   
1757    Printf("anaLibs %s",anaLibs.Data());
1758    Printf("anaLibsExtra %s",anaLibsExtra.Data());
1759
1760    if (anaLibs.Length())          plugin->SetAdditionalLibs(anaLibs.Data());
1761    if (anaLibsExtra.Length())     plugin->SetAdditionalRootLibs(anaLibsExtra.Data());
1762
1763    TString ana_sources = "";
1764    TString ana_add = "";
1765    if (kUsePAR && anaPars.Length()) {
1766       printf("%s\n", anaPars.Data());
1767       TObjArray *arr;
1768       TObjString *objstr;
1769       arr = anaPars.Tokenize(" ");
1770       TIter next(arr);
1771       while ((objstr=(TObjString*)next())) plugin->EnablePackage(objstr->GetString());
1772       delete arr;
1773    } 
1774    
1775 // Declare the analysis source files names separated by blancs. To be compiled runtime
1776 // using ACLiC on the worker nodes.
1777    ana_sources = anaSources.Strip();
1778 // Declare all libraries (other than the default ones for the framework. These will be
1779 // loaded by the generated analysis macro. Add all extra files (task .cxx/.h) here.
1780
1781    if (ana_sources.Length()) plugin->SetAnalysisSource(ana_sources);
1782    plugin->SetExecutableCommand(kPluginExecutableCommand.Data());  
1783    // Declare the output file names separated by blancs.
1784    // (can be like: file.root or file.root@ALICE::Niham::File)
1785    plugin->SetMergeExcludes(kGridMergeExclude);
1786    plugin->SetMaxMergeFiles(kGridMaxMergeFiles);
1787    plugin->SetNrunsPerMaster(kGridRunsPerMaster);
1788    plugin->SetMergeViaJDL(kPluginMergeViaJDL);
1789    // Use fastread option
1790    plugin->SetFastReadOption(kPluginFastReadOption);
1791    // UseOverwrite mode
1792    plugin->SetOverwriteMode(kPluginOverwriteMode); 
1793    // Optionally define the files to be archived.
1794    //   plugin->SetOutputArchive("log_archive.zip:stdout,stderr@ALICE::NIHAM::File root_archive.zip:AliAOD.root,AOD.tag.root@ALICE::NIHAM::File");
1795    plugin->SetOutputToRunNo(kPluginOutputToRunNumber);     // write the output to subdirs named after run number
1796    
1797    // Put default output files to archive
1798    TString listhists = "";
1799    TString listaods  = "";
1800    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
1801    TIter next(mgr->GetOutputs());
1802    AliAnalysisDataContainer *output;
1803    while ((output=(AliAnalysisDataContainer*)next())) {
1804       const char *filename = output->GetFileName();
1805       if (!(strcmp(filename, "default"))) {
1806         if (!mgr->GetOutputEventHandler()) continue;
1807          filename = mgr->GetOutputEventHandler()->GetOutputFileName();
1808          if (listaods.Length()) listaods += " ";
1809          listaods += filename;
1810       } else {
1811         if(!listhists.Contains(filename)){
1812           if (listhists.Length()) listhists += " ";
1813           listhists += filename;
1814         }
1815       }
1816    }
1817
1818    if (mgr->GetExtraFiles().Length()) {
1819      if (listaods.Length()) listaods += " ";
1820      listaods += mgr->GetExtraFiles();
1821    }
1822
1823    // if we do not fill the aod we do not need to store it
1824    //   kGridMergeExclude = listaods;
1825    
1826    if(kSaveAOD>=0){
1827      TString outputFiles =  "";
1828      outputFiles += mgr->GetExtraFiles();
1829      if (listhists.Length()) outputFiles += " ";
1830      outputFiles += listhists;
1831      plugin->SetDefaultOutputs(kFALSE);
1832      Printf("%s:%d Starting with the files %s",(char*)__FILE__,__LINE__,outputFiles.Data());
1833      // remove
1834      // no harm done when we try to remove something that is not there :)
1835      if(!(kSaveAOD&(1<<0))){
1836        outputFiles.ReplaceAll("AliAOD.root ","");
1837        listaods.ReplaceAll("AliAOD.root ","");
1838      }
1839      if(!(kSaveAOD&(1<<1))){
1840        if(kDeltaAODJetName.Length())outputFiles.ReplaceAll(kDeltaAODJetName.Data(),"");
1841        if(kDeltaAODJetName.Length())listaods.ReplaceAll(kDeltaAODJetName.Data(),"");
1842        
1843      }
1844      if(!(kSaveAOD&(1<<2))){
1845        if(kDeltaAODPartCorrName.Length())outputFiles.ReplaceAll(kDeltaAODPartCorrName.Data(),"");
1846        if(kDeltaAODPartCorrName.Length())listaods.ReplaceAll(kDeltaAODPartCorrName.Data(),"");
1847      }
1848      if(!(kSaveAOD&(1<<3))){
1849        if(kDeltaAODJCORRANName.Length())outputFiles.ReplaceAll(kDeltaAODJCORRANName.Data(),"");
1850        if(kDeltaAODJCORRANName.Length())listaods.ReplaceAll(kDeltaAODJCORRANName.Data(),"");
1851      }
1852      
1853      // 
1854      plugin->SetDefaultOutputs(kFALSE);
1855      Printf("%s:%d Saving the files %s",(char*)__FILE__,__LINE__,outputFiles.Data());
1856      plugin->SetOutputFiles(outputFiles.Data());
1857    }
1858
1859    TString outputArchive;
1860    outputArchive = Form("log_archive.zip:std*@%s","disk=1");
1861    listaods.ReplaceAll(" ", ",");
1862    listhists.ReplaceAll(" ", ",");
1863    if (listhists.Length()){
1864      outputArchive += " ";
1865      outputArchive += "root_archive.zip:";
1866      outputArchive += listhists;
1867      if (listaods.Length()){
1868        outputArchive += ",";
1869        outputArchive += listaods;
1870      }
1871      outputArchive += Form("@%s", kGridOutputStorages.Data());
1872    }
1873    else{
1874      
1875      if (listaods.Length()){
1876        // we have only aod'ish output
1877        outputArchive += " ";
1878        outputArchive += "root_archive.zip:";
1879        outputArchive += listaods;
1880        outputArchive += Form("@%s", kGridOutputStorages.Data());
1881      }
1882      else{
1883        // no other outputs than std..
1884       ::Fatal("AnalysisTrainPWG4Jets", "No task output !");
1885      }
1886    }
1887
1888    plugin->SetDefaultOutputs(kFALSE);
1889    plugin->SetOutputArchive(outputArchive);
1890
1891
1892    // Optionally set a name for the generated analysis macro (default MyAnalysis.C)
1893    plugin->SetAnalysisMacro(Form("%s.C", kTrainName.Data()));
1894    // Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
1895    plugin->SetSplitMaxInputFileNumber(kGridFilesPerJob);
1896    // Optionally set number of failed jobs that will trigger killing waiting sub-jobs.
1897    //   plugin->SetMaxInitFailed(5);
1898    // Optionally resubmit threshold.
1899    // plugin->SetMasterResubmitThreshold(90);
1900    // Optionally set time to live (default 30000 sec)
1901    plugin->SetTTL(54000); // 15h...
1902    // Optionally set input format (default xml-single)
1903    plugin->SetInputFormat("xml-single");
1904    // Optionally modify the name of the generated JDL (default analysis.jdl)
1905    plugin->SetJDLName(Form("%s.jdl", kTrainName.Data()));
1906    // Optionally modify the executable name (default analysis.sh)
1907    plugin->SetExecutable(Form("%s.sh", kTrainName.Data()));
1908    // Optionally modify job price (default 1)
1909    plugin->SetPrice(1);      
1910    // Optionally modify split mode (default 'se')    
1911    plugin->SetSplitMode("se");
1912    return plugin;
1913 }
1914
1915 //______________________________________________________________________________
1916 void WriteConfig()
1917 {
1918 // Write train configuration in a file. The file name has the format:
1919 // train_[trainName]_ddMonthyyyy_time.C
1920    if (kUseDate) {
1921       gSystem->Exec("date +%d%b%Y_%Hh%M > date.tmp");
1922       ifstream fdate("date.tmp");
1923       if (!fdate.is_open()) {
1924          ::Error("AnalysisTrainPWG4Jets.C::Export","Could not generate file name");
1925          return;
1926       }
1927       const char date[64];
1928       fdate.getline(date,64);
1929       fdate.close();
1930       gSystem->Exec("rm date.tmp");
1931       kTrainName = Form("train_%s_%s", kTrainName.Data(), date);
1932    } else {
1933       kTrainName = Form("train_%s", kTrainName.Data());
1934    }   
1935    TString cdir = gSystem->WorkingDirectory();
1936    gSystem->MakeDirectory(kTrainName);
1937    gSystem->ChangeDirectory(kTrainName);
1938    ofstream out;
1939    out.open(Form("%sConfig.C",kTrainName.Data()), ios::out); 
1940    if (out.bad()) {
1941       ::Error("AnalysisTrainPWG4Jets.C::Export", "Cannot open ConfigTrain.C for writing");
1942       return;
1943    }
1944    out << "{" << endl;
1945    out << "   kTrainName      = " << "\"" << kTrainName.Data() << "\";" << endl;
1946    out << "   kProofCluster   = " << "\"" << kProofCluster.Data() << "\";" << endl;
1947    out << "   kProofUseAFPAR        = " << kProofUseAFPAR << ";" << endl;
1948    if (kProofUseAFPAR) 
1949       out << "   kProofAFversion       = " << kProofAFversion.Data() << ";" << endl;
1950    out << "   kProofDataSet   = " << "\"" << kProofDataSet.Data() << "\";" << endl;
1951    out << "   kPluginUse       = " << kPluginUse << ";" << endl;
1952    out << "   kUsePAR          = " << kUsePAR << ";" << endl;
1953    out << "   kUseCPAR         = " << kUseCPAR << ";" << endl;
1954    out << "   kPluginRootVersion    = " << "\"" << kPluginRootVersion.Data() << "\";" << endl;
1955    out << "   kPluginAliRootVersion = " << "\"" << kPluginAliRootVersion.Data() << "\";" << endl;
1956    out << "   kGridDatadir   = " << "\"" << kGridDatadir.Data() << "\";" << endl;
1957    if (!kGridOutdir.Length()) kGridOutdir = Form("output_%s",kTrainName.Data());
1958    out << "   kGridOutdir    = " << "\"" << kGridOutdir.Data() << "\";" << endl;
1959    out << "   kGridMaxMergeFiles   = " << kGridMaxMergeFiles << ";" << endl;
1960    out << "   kGridMergeExclude    = " << "\"" << kGridMergeExclude.Data() << "\";" << endl;
1961    out << "   kGridRunsPerMaster  = " << kGridRunsPerMaster << ";" << endl;
1962    out << "   kGridFilesPerJob    = " << kGridFilesPerJob << ";" << endl;
1963    out << "   kGridRunRange[0]    = " << kGridRunRange[0] << ";" << endl;
1964    out << "   kGridRunRange[1]    = " << kGridRunRange[1] << ";" << endl;
1965    out << "   kUseDebug          = " << kUseDebug << ";" << endl;
1966    out << "   kUseMC           = " << kUseMC << ";" << endl;
1967    out << "   kUseESDTags         = " << kUseESDTags << ";" << endl;
1968    out << "   kUseKinefilter      = " << kUseKinefilter << ";" << endl;
1969    out << "   kUseTR           = " << kUseTR << ";" << endl;
1970    out << "   kUseAODTags      = " << kUseAODTags << ";" << endl;
1971    out << "   kSaveTrain       = " << "kFALSE;" << endl << endl;
1972    out << "   // Analysis modules" << endl;
1973    out << "   iAODanalysis    = " << iAODanalysis << ";" << endl;
1974    out << "   iAODhandler     = " << iAODhandler << ";" << endl;
1975    out << "   iESDfilter      = " << iESDfilter << ";" << endl;
1976    out << "   iJETAN          = " << iJETAN << ";" << endl;
1977    out << "// Configuration fot the wagons" << endl;
1978    out << "}" << endl;
1979    ::Info("AnalysisTrainPWG4Jets.C::WriteConfig", "Train configuration wrote to file %s", Form("config_%s.C", kTrainName.Data()));
1980    gSystem->ChangeDirectory(cdir);
1981 }   
1982
1983 //______________________________________________________________________________
1984 Bool_t LoadConfig(const char *filename)
1985 {
1986 // Read train configuration from file
1987    if (gSystem->AccessPathName(filename)) {
1988       ::Error("AnalysisTrainPWG4Jets.C::LoadConfig", "Config file name not found");
1989       return kFALSE;
1990    }   
1991    gROOT->ProcessLine(Form(".x %s", filename));
1992    ::Info("AnalysisTrainPWG4Jets.C::LoadConfig", "Train configuration loaded from file %s", filename);
1993    return kTRUE;
1994 }
1995
1996 Bool_t PatchJDL(){
1997   Printf(">>> Patching JDL");
1998   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
1999   AliAnalysisAlien*    gridHandler = (AliAnalysisAlien*)mgr->GetGridHandler();
2000   TGridJDL *jdl = gridHandler->GetGridJDL();
2001   if(iJETAN)jdl->AddToPackages("fastjet","v2.4.0");
2002   gridHandler->WriteJDL(kFALSE);
2003   Printf("<<<  Patching JDL");
2004   return kTRUE;
2005 }
2006
2007 Bool_t PatchAnalysisMacro(){
2008   Printf(">>> Patching AnalysisMacro");
2009   gSystem->Exec(Form("mv %s.C %s.C_tmp",kTrainName.Data(),kTrainName.Data()));
2010
2011   ifstream in1; 
2012   in1.open(Form("%s.C_tmp", kTrainName.Data()));
2013   char cLine[250];
2014   TString st;
2015   while(in1.getline(cLine,250)){
2016     st += cLine;
2017     st += "\n";
2018   }
2019   Int_t index= -1;
2020   index = st.Index("gSystem->Load(\"libPhysics\");");
2021   index += strlen("gSystem->Load(\"libPhysics\");");
2022   /*
2023     TObjArray *arr;
2024     TObjString *objstr;
2025     arr = anaLibs.Tokenize(" ");
2026     TIter next(arr);
2027
2028     add += "\n\n // added by CKB \n";
2029     while ((objstr=(TObjString*)next())){
2030       if(objstr->GetString().Contains("PWG3"))continue;
2031       if(objstr->GetString().EndsWith(".so"))add += Form("gSystem->Load(\"%s\");\n",objstr->GetString().Data());
2032     }
2033     delete arr; 
2034     */
2035     //    add += Form("AliLog::SetGlobalLogLevel(%d);\n",AliLog::GetGlobalLogLevel());
2036   TString add = "";
2037
2038   if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
2039   add += "\n\n // added by CKB \n";
2040   if(kErrorIgnoreLevel>0)add += Form("gErrorIgnoreLevel = %d;\n",kErrorIgnoreLevel);
2041   add += "\n gSystem->AddIncludePath(\"./\"); \n";
2042   add += "\n gSystem->SetFPEMask(); \n";
2043
2044
2045   if(gGrid&&kPluginAliRootVersion.Length()==0){
2046     add += "\n // Dirty hack for TRD reference data \n";
2047     add += "\n gSystem->Setenv(\"ALICE_ROOT\",\"";
2048     add += Form("alien://%s/rootfiles/",gGrid->GetHomeDirectory());
2049     add += "\"); \n";
2050   }
2051   add += "// BKC \n\n";
2052   st.Insert(index,add.Data());
2053
2054   if(kUseDebug){
2055     //    st.Insert(index,"\n gROOT->ProcessLine(\".trace\"); // CKB \n");
2056   }
2057
2058   if(kUseCPAR&&kPluginAliRootVersion.Length()==0){
2059     index = st.Index("gSystem->AddIncludePath(\"-I$"); // uncommen $ALICE_ROOT include for par files
2060     if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
2061     st.Insert(index,"// CKB comment out whehn no aliroot is provided \n //");
2062   }
2063
2064   if(AliLog::GetGlobalLogLevel()==AliLog::kFatal){
2065     index = st.Index("AliLog::SetGlobal"); // ncomment setting of log level, do my own
2066     if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
2067     st.Insert(index,"AliLog::SetGlobalLogLevel(AliLog::kFatal);// CKB \n  // CKB comment out for own setting \n  //");
2068   }
2069
2070   ofstream out;
2071   out.open(Form("%s.C", kTrainName.Data()));
2072   if (out.bad()) {
2073     return kFALSE;
2074   }
2075   out << st << endl;
2076   Printf("<<< Patching AnalysisMacro");
2077
2078
2079
2080
2081
2082   Printf(">>> Patching Merge Macro");
2083   gSystem->Exec(Form("mv %s_merge.C %s_merge.C_tmp",kTrainName.Data(),kTrainName.Data()));
2084
2085   ifstream in2; 
2086   in2.open(Form("%s_merge.C_tmp", kTrainName.Data()));
2087   TString st2;
2088   while(in2.getline(cLine,250)){
2089     st2 += cLine;
2090     st2 += "\n";
2091   }
2092   index = st2.Index("gSystem->Load(\"libPhysics\");");
2093   index += strlen("gSystem->Load(\"libPhysics\");");
2094   TString add2 = "";
2095   add2 += "\n gSystem->AddIncludePath(\"./\"); \n";
2096   if(gGrid&&kPluginAliRootVersion.Length()==0){
2097     add2 += "\n // Dirty hack for TRD reference data \n";
2098     add2 += "\n gSystem->Setenv(\"ALICE_ROOT\",\"";
2099     add2 += Form("alien://%s/rootfiles/",gGrid->GetHomeDirectory());
2100     add2 += "\"); \n";
2101   }
2102   add2 += "// BKC \n\n";
2103   if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
2104   st2.Insert(index,add.Data());
2105
2106
2107   if(kUseDebug){
2108     //    st2.Insert(index,"\n gROOT->ProcessLine(\".trace\"); // CKB \n");
2109   }
2110
2111   if(kUseCPAR&&kPluginAliRootVersion.Length()==0){
2112     index = st2.Index("gSystem->AddIncludePath(\"-I$"); // uncomment $ALICE_ROOT include for par files
2113     if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
2114     st2.Insert(index,"// CKB comment out whehn no aliroot is provided \n //");
2115   }
2116
2117   // do not exclude the extra files from merign, this is done explicitly in this train script
2118   //  index = st2.Index("mergeExcludes +="); // uncommen $ALICE_ROOT include for par files
2119   //  if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
2120   // st2.Insert(index,"// CKB comment out, handled explicitly by the train macro \n //");
2121
2122
2123   ofstream out2;
2124   out2.open(Form("%s_merge.C", kTrainName.Data()));
2125   if (out2.bad()) {
2126     return kFALSE;
2127   }
2128   out2 << st2 << endl;
2129   Printf("<<< Patching Merging Macro");
2130
2131
2132
2133   return kTRUE;
2134
2135 }