]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HMPID/AnalysisTrainHMPID.C
remove print
[u/mrichter/AliRoot.git] / HMPID / AnalysisTrainHMPID.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 AnalysisTrainHMPID.C
5 // Grid full mode as below (other modes: test, offline, submit, terminate)
6 //    root[1] AnalysisTrainHMPID("grid", "full")
7 // CAF mode (requires root v5-23-02 + aliroot v4-16-Rev08)
8 //    root[2] AnalysisTrainHMPID("proof")
9 // Local mode requires AliESds.root or AliAOD.root in ./data directory
10 //    root[3] AnalysisTrainHMPID("local")
11 // In proof and grid modes, a token is needed and sourcing the produced environment file.
12 //
13
14 // =============================================================================
15 // ### General Steering variables
16 // =============================================================================
17 //== general setup variables
18 TString     kTrainName         = "hmpidAnalysis"; // (no blancs or special characters)
19 TString     kJobTag            = "HMPID Tasks analysis train configured"; //
20 Bool_t      kUsePAR            = kFALSE;  // use par files for extra libs
21 Bool_t      kUseCPAR           = kFALSE;  // use par files for common libs
22 Bool_t      kFillAOD           = kFALSE;  // switch of AOD filling for on the fly analysis
23
24 Int_t       iAODanalysis       = 0;      // Analysis on input AOD's
25 Int_t       iAODhandler        = 1;      // Analysis produces an AOD or dAOD's
26 Int_t       iESDfilter         = 0;      // ESD to AOD filter (barrel + muon tracks)
27 Int_t       iPhysicsSelection  = 1;      // Physics selection task
28 Int_t       iCentrality        = 0;      // Physics selection task
29 Bool_t      kUseKinefilter     = kFALSE; // use Kinematics filter
30 Bool_t      kUseMuonfilter     = kFALSE; // use muon filter
31 TString     kCommonOutputFileName = "HmpidOutput.root";
32
33
34 //== general process variables
35
36 // ### Other flags to steer the analysis
37 //==============================================================================
38 Bool_t      kSkipTerminate      = kFALSE; // Do not call Teminate
39 Bool_t      kDebugLevel         = kTRUE; // activate debugging
40 Int_t       kUseSysInfo         = 0; // activate debugging
41 Long64_t    kNumberOfEvents     = 1234567890; // number of events to process from the chain
42 Bool_t      kUseMC              = kFALSE;  // use MC info
43 Bool_t      kIsMC               = kFALSE;  // is MC info, if false it overwrites Use(AOD)MC
44 Bool_t      kUseESDTags         = kFALSE;  // use ESD tags for selection
45 Bool_t      kUseTR              = kFALSE;  // use track references
46
47 // ### Analysis modules to be included. Some may not be yet fully implemented.
48 //==============================================================================
49 Int_t       iHMPID             = 1;      // Basic HMPID analysis task
50 Int_t       iHMPIDperf         = 0;      // Basic HMPID performance task
51 Int_t       iJETAN             = 0;      // Jet analysis (PWG4) // 1 write standard 2 write non-standard jets
52 Int_t       iHMPIDJets         = 0;      // Jet chemistry with HMPID
53 Int_t       iJETANLib          = 0;
54 Int_t       kHighPtFilterMask  = 16;     // change depending on the used AOD Filter
55
56
57 //==============================================================================
58 // ### PROOF Steering varibales
59 //==============================================================================
60 //== proof setup variables
61 TString     kProofCluster      = "alice-caf.cern.ch";
62 Bool_t      kProofUseAFPAR     = kTRUE;  // use AF special par file
63 TString     kProofAFversion    = "VO_ALICE@AliRoot::v4-20-08-AN";
64 //== proof input and output variables
65 TString     kProofDataSet      = "/alice/sim/LHC10d2_117220";
66 //== proof process variables
67 Bool_t      kProofClearPackages = kFALSE;
68 Int_t       kProofEvents = 10000;
69 Int_t       kProofOffset = 0;
70
71 //==============================================================================
72 // ### Grid plugin Steering varibiables
73 //==============================================================================
74 //== grid plugin setup variables
75 Bool_t      kPluginUse         = kTRUE;   // do not change
76 Bool_t      kPluginUseProductionMode  = kFALSE;   // use the plugin in production mode
77 TString     kPluginRootVersion       = "v5-28-00d";  // *CHANGE ME IF MORE RECENT IN GRID*
78 TString     kPluginAliRootVersion    = "v4-21-26-AN";  // *CHANGE ME IF MORE RECENT IN GRID*                                          
79 Bool_t      kPluginMergeViaJDL       = kTRUE;  // merge via JDL
80 Bool_t      kPluginFastReadOption    = kFALSE;  // use xrootd flags to reduce timeouts
81 Bool_t      kPluginOverwriteMode     = kTRUE;  // overwrite existing collections
82 Int_t       kPluginOutputToRunNumber = 1;     // write the output to subdirs named after run number
83 TString kPluginExecutableCommand = "aliroot -b -q";
84
85 // == grid plugin input and output variables
86 TString     kGridDatadir      = "/alice/data/2010/LHC10e";
87 TString     kGridLocalRunList = "";
88 TString     kGridWorkDir      = "";   // Alien working directory
89 TString     kGridOutdir       = ""; // AliEn output directory. If blank will become output_<kTrainName>
90 TString     kGridDataSet      = ""; // sub working directory not to confuse different run xmls 
91 Int_t       kGridRunRange[2]       = {128260,128260}; // Set the run range
92 TString     kGridRunPattern        = "%03d"; // important for leading zeroes!!
93 TString     kGridPassPattern       = "/ESDs/pass2";
94 TString     kGridExtraFiles        = ""; // files that will be added to the input list in the JDL...
95 Int_t       kGridMaxMergeFiles      = 12; // Number of files merged in a chunk grid run range
96 Int_t       kGridMaxMergeStages     = 3;  // Number of stages in the merging procedure
97 TString     kGridMergeExclude       = "AliAOD.root"; // Files that should not be merged
98 TString     kGridOutputStorages      = "disk=2"; // Make replicas on the storages
99 // == grid process variables
100 Int_t       kGridRunsPerMaster     = 1; // Number of runs per master job
101 Int_t       kGridFilesPerJob       = 100; // Maximum number of files per job (gives size of AOD)
102
103 //==============================================================================
104 // ### Local Steering variables
105 //==============================================================================
106 //== local setup variables
107 //== local input and output variables
108 TString     kLocalXMLDataset   = ""; // Change local xml dataset for local interactive analysis
109 TString     kLocalDataList   = "local_deltaaod.txt"; // Change local xml dataset for local interactive analysis
110 // == local process variables
111
112
113
114 // Temporaries.
115 TString anaPars = "";
116 TString anaLibs = "";
117 TString anaLibsExtra = "";
118 TString anaSources = "";
119 // Function signatures
120 class AliAnalysisAlien;
121
122 //______________________________________________________________________________
123 void AnalysisTrainHMPID(const char *analysis_mode="local", const char *plugin_mode="",
124                         const char *config_file="",Int_t iOffset = 0)
125 {
126 // Main analysis train macro. If a configuration file is provided, all parameters
127 // are taken from there but may be altered by CheckModuleFlags.
128
129    if (strlen(config_file) && !LoadConfig(config_file)) return;
130
131    if(iOffset)kProofOffset = iOffset;
132    TString smode(analysis_mode);
133    smode.ToUpper();
134    // Check compatibility of selected modules
135    CheckModuleFlags(smode);
136
137    printf("==================================================================\n");
138    printf("===========    RUNNING ANALYSIS TRAIN %s IN %s MODE   ==========\n", kTrainName.Data(),smode.Data());
139    printf("==================================================================\n");
140    printf("=  Configuring analysis train for:                               =\n");
141    if (iAODanalysis) printf("=  AOD analysis                                                  =\n");
142    else              printf("=  ESD analysis                                                  =\n");
143    if (iPhysicsSelection)   printf("=  Physics selection                                             =\n");
144    if (iCentrality)  printf("=  Centrality                                                    =\n");
145    if (iESDfilter)   printf("=  ESD filter                                                    =\n");
146    if (iJETAN)       printf("=  Jet analysis                                                  =\n");
147    if (iHMPID)       printf("=  HMPID generic                                                 =\n");
148    if (iHMPIDperf)   printf("=  HMPID performance                                             =\n");
149    if (iHMPIDJets)   printf("=  HMPID Jet chemistry                                           =\n");
150    printf("==================================================================\n");
151    printf(":: use MC truth      %d\n", (UInt_t)kUseMC);
152    printf(":: use KINE filter   %d\n", (UInt_t)kUseKinefilter);
153    printf(":: use track refs    %d\n", (UInt_t)kUseTR);
154    printf(":: use tags          %d\n", (UInt_t)kUseESDTags);
155    printf(":: use debugging     %d\n", (UInt_t)kDebugLevel);
156    printf(":: use PAR files     %d\n", (UInt_t)kUsePAR);
157    printf(":: use AliEn plugin  %d\n", (UInt_t)kPluginUse);
158    
159    //==========================================================================
160    // Connect to back-end system
161    if (!Connect(smode)) {
162       ::Error("AnalysisTrain", "Could not connect to %s back-end", analysis_mode);
163       return;
164    }   
165
166    // Load common libraries and set include path
167    if (!LoadCommonLibraries(smode)) {
168       ::Error("AnalysisTrain", "Could not load common libraries");
169       return;
170    }
171
172     
173    // Make the analysis manager and connect event handlers
174    AliAnalysisManager *mgr  = new AliAnalysisManager("HMPIDTrain", "HMPID train");
175    if (kCommonOutputFileName.Length()>0)mgr->SetCommonFileName(kCommonOutputFileName.Data());
176    mgr->SetNSysInfo(0);
177    if (!strcmp(plugin_mode, "test")) mgr->SetNSysInfo(1);
178    if (kUseSysInfo)mgr->SetNSysInfo(kUseSysInfo);
179    mgr->SetSkipTerminate(kSkipTerminate);
180
181    // Load analysis specific libraries
182    if (!LoadAnalysisLibraries(smode)) {
183       ::Error("AnalysisTrain", "Could not load analysis libraries");
184      return;
185    }   
186
187    // Create input handler (input container created automatically)
188    if (iAODanalysis) {
189    // AOD input handler
190       AliAODInputHandler *aodH = new AliAODInputHandler();
191       mgr->SetInputEventHandler(aodH);
192    } else {
193    // ESD input handler
194       AliESDInputHandler *esdHandler = new AliESDInputHandler();
195       if (kUseESDTags) esdHandler->SetReadTags();
196       esdHandler->SetReadFriends(kTRUE);
197       mgr->SetInputEventHandler(esdHandler);
198    }
199
200    // Monte Carlo handler
201    if (kUseMC && !iAODanalysis) {
202       AliMCEventHandler* mcHandler = new AliMCEventHandler();
203       mgr->SetMCtruthEventHandler(mcHandler);
204       mcHandler->SetReadTR(kUseTR); 
205    }   
206
207    // AOD output container, created automatically when setting an AOD handler
208    if (iAODhandler) {
209       // AOD output handler
210       AliAODHandler* aodHandler   = new AliAODHandler();
211       aodHandler->SetOutputFileName("AliAOD.root");
212       aodHandler->SetFillAODforRun(kFillAOD);
213       
214       mgr->SetOutputEventHandler(aodHandler);
215       AliAnalysisDataContainer *cout_aod = mgr->GetCommonOutputContainer();
216       cout_aod->SetSpecialOutput();
217    }
218
219    // Debugging if needed
220    if (kDebugLevel){
221       mgr->SetDebugLevel(3);
222    }
223    if(kUseSysInfo>0){
224       mgr->RegisterExtraFile("syswatch.root");
225       if(kGridMergeExclude.Length())kGridMergeExclude += " ";
226       kGridMergeExclude += "syswatch.root";
227    } else {
228       AliLog::SetGlobalLogLevel(AliLog::kError);
229    }
230
231    //==========================================================================
232    // Create the chain. In this example it is created only from ALIEN files but
233    // can be done to work in batch or grid mode as well.
234    TChain *chain = CreateChain(smode, plugin_mode);
235         
236    //==========================================================================
237    // Load the tasks configuration macros for all wagons. These files are supposed now to be
238    // in the current workdir, but in AliEn they will be in the file catalog, 
239    // mapped from AliRoot and pecified in the jdl input list.
240    if(iPhysicsSelection && !iAODanalysis){
241      gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
242      AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection(kIsMC,kTRUE,kTRUE); // last flag also adds information on  
243    }
244    
245    if(iCentrality && !iAODanalysis){
246      gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
247      AliCentralitySelectionTask *taskCentrality = AddTaskCentrality();
248      taskCentrality->SetPass(2); // remember to set the pass you are processing!!!   
249    }     
250
251    if (iESDfilter && !iAODanalysis) {
252       //  ESD filter task configuration.
253       gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskESDFilter.C");
254       AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(kUseKinefilter,kUseMuonfilter);
255       if(kIsMC){
256          mgr->RegisterExtraFile("pyxsec_hists.root");
257          if(kGridMergeExclude.Length())kGridMergeExclude += " ";
258          kGridMergeExclude += "pyxsec_hists.root";
259       }
260    }   
261
262     // Jet analysis
263    if (iJETAN) {
264       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJets.C");
265       AliAnalysisTaskJets *taskjets = 0;
266       if (iJETAN&1) taskjets = AddTaskJets(kHighPtFilterMask); 
267       if (!taskjets) ::Warning("AnalysisTrainHMPID", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
268    }
269
270    if(iHMPID){
271      gROOT->LoadMacro("$ALICE_ROOT/HMPID/AddTaskHMPID.C");
272      AliHMPIDAnalysisTask *taskHmpid = AddTaskHMPID(kUseMC);
273      if (!taskHmpid) ::Warning("AnalysisTrainHMPID", "AliHMPIDAnalysisTask cannot run for this train conditions - EXCLUDED");
274    }
275    
276    if(iHMPIDperf){
277      gROOT->LoadMacro("$ALICE_ROOT/HMPID/AddTaskHMPIDPerformance.C");
278      AliHMPIDPerformanceTask *taskHmpidPerformance = AddTaskHMPIDPerformance(kUseMC);
279      if (!taskHmpidPerformance) ::Warning("AnalysisTrainHMPID", "AliHMPIDPerformanceTask cannot run for this train conditions - EXCLUDED");
280    }
281       
282    if(iHMPIDJets){
283      gROOT->LoadMacro("$ALICE_ROOT/HMPID/AddTaskJetsHMPID.C");
284      AliAnalysisTaskJetsHMPID *taskHmpidJets = AddTaskJetsHMPID("clustersAOD_ANTIKT04_B0_Filter00256_Cut00150_Skip00","jeteventbackground_clustersAOD_KT04_B0_Filter00256_Cut00150_Skip00");
285      if (!taskHmpidJets) ::Warning("AnalysisTrainHMPID", "AliAnalysisTaskJetsHMPID cannot run for this train conditions - EXCLUDED");
286    }
287    
288    if (kPluginUse) {
289       AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
290       AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
291    }
292
293    if (mgr->InitAnalysis()) {
294      mgr->PrintStatus();
295      if (!strcmp(plugin_mode,"submit") && smode=="GRID"){
296        TString alien_workdir = gGrid->GetHomeDirectory();
297        alien_workdir += kGridWorkDir.Data();
298        if(kGridDataSet.Length()>0)alien_workdir += Form("/%s",kGridDataSet.Data());
299        AliAnalysisAlien *gridhandler = (AliAnalysisAlien*)mgr->GetGridHandler();
300        printf("=== AnalysisTrainHMPID:: Registering jdl in the work directory alien://%s/%s, should be done by the manager! ===\n",
301               alien_workdir.Data(),gridhandler->GetGridOutputDir());
302
303        TString dest;
304        dest = Form("%s/%s/%s.jdl",alien_workdir.Data(),gridhandler->GetGridOutputDir(),kTrainName.Data());
305        if(AliAnalysisAlien::FileExists(dest.Data())){
306      //  Printf("%s exist on grid removing...",dest.Data());
307      //  gGrid->Rm(dest.Data());
308        }
309        TFile::Cp(Form("file:%s.jdl",kTrainName.Data()),Form("alien://%s",dest.Data()));
310
311
312        TString dest;
313        dest = Form("%s/%s/%s_merge.jdl",alien_workdir.Data(),gridhandler->GetGridOutputDir(),kTrainName.Data());
314        if(AliAnalysisAlien::FileExists(dest.Data())){
315          //      Printf("%s exist on grid removing...",dest.Data());
316          //      gGrid->Rm(dest.Data());
317        }
318        TFile::Cp(Form("file:%s_merge.jdl",kTrainName.Data()),Form("alien://%s",dest.Data()));
319      }
320
321      AliLog::SetGlobalLogLevel(AliLog::kError);
322      if((kUseSysInfo>0 && smode=="LOCAL") || !strcmp(plugin_mode, "test")){
323        TFile *fM = TFile::Open("manager_local.root","RECREATE");
324        mgr->Write();
325        fM->Close();
326      }
327
328      StartAnalysis(smode, chain);
329        
330      if((kUseSysInfo>0 && smode=="LOCAL") || !strcmp(plugin_mode, "test")){
331        for(int i = 0;i < mgr->GetTopTasks()->GetEntries();i++){
332          mgr->ProfileTask(i);
333        }
334      }
335      if (!strcmp(plugin_mode, "offline") && smode=="GRID"){
336        // Offline mode path files
337        //       PatchJDL();
338        PatchAnalysisMacro();
339      }
340    }
341 }
342
343
344 //______________________________________________________________________________
345 void StartAnalysis(const char *mode, TChain *chain) {
346
347    Int_t imode = -1;
348    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
349    if (!strcmp(mode, "LOCAL")) imode = 0;
350    if (!strcmp(mode, "PROOF")) imode = 1;
351    if (!strcmp(mode, "GRID"))  imode = 2;
352    switch (imode) {
353       case 0:
354          if (!chain) {
355             ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Cannot create the chain");
356             return;
357          }
358          mgr->StartAnalysis(mode, chain, kNumberOfEvents);
359          return;
360       case 1:
361          if (!kProofDataSet.Length()) {
362             ::Error("AnalysisTrainHMPID.C::StartAnalysis", "kProofDataSet is empty");
363             return;
364          }
365          mgr->StartAnalysis(mode, kProofDataSet, kProofEvents,kProofOffset);
366          return;
367       case 2:
368          if (kPluginUse) {
369             if (!mgr->GetGridHandler()) {
370                ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Grid plugin not initialized");
371                return;
372             }
373             mgr->StartAnalysis("grid");
374          } else {
375             if (!chain) {
376                ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Cannot create the chain");
377                return;
378             }
379             mgr->StartAnalysis(mode, chain);
380          }
381       return;
382    }
383 }
384
385 //______________________________________________________________________________
386 void CheckModuleFlags(const char *mode) {
387 // Checks selected modules and insure compatibility
388    Int_t imode = -1;
389    if (!strcmp(mode, "LOCAL")) imode = 0;
390    if (!strcmp(mode, "PROOF")) imode = 1;
391    if (!strcmp(mode, "GRID"))  imode = 2;
392
393
394    if (kUseCPAR) {
395      kPluginAliRootVersion    = ""; // NO aliroot if we use CPAR
396    }
397
398    if (imode==1) {
399       if (!kUsePAR) {
400          ::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "PAR files enabled due to PROOF analysis");
401          kUsePAR = kTRUE;
402       }   
403    }  
404    if (imode != 2) {
405       ::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "AliEn plugin disabled since not in GRID mode");
406       kPluginUse = kFALSE; 
407    }
408
409    if(!kIsMC){
410       // switch off anything related to MC
411       kUseMC = kFALSE;
412       kUseTR = kFALSE;
413    }
414
415    if (iAODanalysis) {
416    // AOD analysis
417       if (kUseMC)
418          ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "MC usage disabled in analysis on AOD's");
419       kUseMC = kFALSE;
420       kUseTR = kFALSE;
421       if (iESDfilter)
422          ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "ESD filter disabled in analysis on AOD's");
423       iESDfilter   = 0;
424       if (iPhysicsSelection)
425          ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "Physics Selection disabled in analysis on AOD's");
426       iPhysicsSelection   = 0;
427       if (!iAODhandler) {
428          if (iJETAN) 
429             ::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "JETAN disabled in analysis on AOD's without AOD handler");
430          iJETAN = 0;
431       }
432       if(iHMPID)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "HMPID analysis disabled in analysis on AOD's");
433       iHMPID = 0;
434       if(iHMPIDperf)::Info("AnalysisTrainPWG4Jets.C::CheckModuleFlags", "HMPID performance disabled in analysis on AOD's");
435       iHMPIDperf = 0;
436    } else {   
437    // ESD analysis
438       if (!kUseMC){
439          kUseTR = kFALSE;
440          if(kUseKinefilter)::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "Kine Filter disabled in analysis without MC");
441          kUseKinefilter = kFALSE;
442       }
443       if (iJETAN){
444          iESDfilter=1;
445       }
446       if (!iESDfilter){
447          kUseKinefilter = kFALSE;
448          kUseMuonfilter = kFALSE;
449       }
450    }
451
452    iJETANLib = iJETAN && 1;
453    if (iESDfilter) {iAODhandler=1;}
454    if (kUseKinefilter && !kUseMC) kUseKinefilter = kFALSE;
455    
456 }
457
458
459 //______________________________________________________________________________
460 Bool_t Connect(const char *mode) {
461 // Connect <username> to the back-end system.
462    Int_t imode = -1;
463    if (!strcmp(mode, "LOCAL")) imode = 0;
464    if (!strcmp(mode, "PROOF")) imode = 1;
465    if (!strcmp(mode, "GRID"))  imode = 2;
466    TString username = gSystem->Getenv("alien_API_USER");
467    switch (imode) {
468      case 0:
469        break;
470      case 1:
471        if  (!username.Length()) {
472          ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode), "Make sure you:\n \
473                         1. Have called: alien-token-init <username>\n \
474                         2. Have called: >source /tmp/gclient_env_$UID");
475          return kFALSE;
476        }
477        ::Info("AnalysisTrainHMPID.C::Connect", "Connecting user <%s> to PROOF cluster <%s>", 
478                 username.Data(), kProofCluster.Data());
479        gEnv->SetValue("XSec.GSI.DelegProxy", "2");
480        TProof::Open(Form("%s@%s", username.Data(), kProofCluster.Data()));       
481        if (!gProof) {
482          if (strcmp(gSystem->Getenv("XrdSecGSISRVNAMES"), "lxfsrd0506.cern.ch"))
483            ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode), "Environment XrdSecGSISRVNAMES different from lxfsrd0506.cern.ch");
484            return kFALSE;
485          }
486        if(kProofClearPackages)gProof->ClearPackages();
487        break;
488      case 2:      
489        if  (!username.Length()) {
490          ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode), "Make sure you:\n \
491                         1. Have called: alien-token-init <username>\n \
492                         2. Have called: >source /tmp/gclient_env_$UID");
493          return kFALSE;
494        }
495        if (kPluginUse && !gSystem->Getenv("alien_CLOSE_SE")) {
496          ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode), 
497                         "When using the AliEn plugin it is preferable to define the \
498                         variable alien_CLOSE_SE in your environment.");
499          return kFALSE;
500        }
501        ::Info("AnalysisTrainHMPID.C::Connect", "Connecting user <%s> to AliEn ...", 
502                 username.Data());
503        TGrid::Connect("alien://");
504        if (!gGrid || !gGrid->IsConnected()) return kFALSE;
505        break;
506      default:
507        ::Error("AnalysisTrainHMPID.C::Connect", "Unknown run mode: %s", mode);
508        return kFALSE;
509    }
510    ::Info("AnalysisTrainHMPID.C::Connect","Connected in %s mode", mode);
511    return kTRUE;
512 }
513
514
515 //______________________________________________________________________________
516 Bool_t LoadCommonLibraries(const char *mode)
517 {
518 // Load common analysis libraries.
519    Int_t imode = -1;
520    if (!strcmp(mode, "LOCAL")) imode = 0;
521    if (!strcmp(mode, "PROOF")) imode = 1;
522    if (!strcmp(mode, "GRID"))  imode = 2;
523    if (!gSystem->Getenv("ALICE_ROOT")) {
524       ::Error("AnalysisTrainHMPID.C::LoadCommonLibraries", "Analysis train requires that analysis libraries are compiled with a local AliRoot"); 
525       return kFALSE;
526    }   
527    Bool_t success = kTRUE;
528    // ROOT libraries
529    gSystem->Load("libTree.so");
530    gSystem->Load("libGeom.so");
531    gSystem->Load("libVMC.so");
532    gSystem->Load("libPhysics.so");
533    
534    // Load framework classes. Par option ignored here.
535    switch (imode) {
536       case 0:
537       case 2:
538          if (kUseCPAR) {
539             success &= LoadLibrary("STEERBase", mode, kTRUE);
540             success &= LoadLibrary("ESD", mode, kTRUE);
541             success &= LoadLibrary("AOD", mode, kTRUE);
542             success &= LoadLibrary("ANALYSIS", mode, kTRUE);
543             success &= LoadLibrary("ANALYSISalice", mode, kTRUE);
544             success &= LoadLibrary("CORRFW", mode, kTRUE);
545          } else {   
546             success &= LoadLibrary("libSTEERBase.so", mode);
547             success &= LoadLibrary("libESD.so", mode);
548             success &= LoadLibrary("libAOD.so", mode);
549             success &= LoadLibrary("libANALYSIS.so", mode);
550             success &= LoadLibrary("libANALYSISalice.so", mode);
551             success &= LoadLibrary("libCORRFW.so", mode);
552             gROOT->ProcessLine(".include $ALICE_ROOT/include");
553          }   
554          break;
555       case 1:
556          if (!kProofUseAFPAR) {
557             success &= LoadLibrary("STEERBase", mode);
558             success &= LoadLibrary("ESD", mode);
559             success &= LoadLibrary("AOD", mode);
560             success &= LoadLibrary("ANALYSIS", mode);
561             success &= LoadLibrary("ANALYSISalice", mode);
562             success &= LoadLibrary("CORRFW", mode);
563          } else { 
564             success &= !gProof->EnablePackage(kProofAFversion);
565             success &= LoadLibrary("CORRFW", mode);
566          }
567          break;         
568       default:
569          ::Error("AnalysisTrainHMPID.C::LoadCommonLibraries", "Unknown run mode: %s", mode);
570          return kFALSE;
571    }
572    if (success) {
573       ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Load common libraries:    SUCCESS");
574       ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Include path for Aclic compilation:\n%s",
575               gSystem->GetIncludePath());
576    } else {           
577       ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Load common libraries:    FAILED");
578    }   
579       
580    return success;
581 }
582
583
584 //______________________________________________________________________________
585 Bool_t LoadAnalysisLibraries(const char *mode)
586 {
587 // Load common analysis libraries.
588    Bool_t success = kTRUE;
589    if (iESDfilter) {
590       if (!LoadLibrary("PWG3base", mode, kTRUE) ||
591           !LoadLibrary("PWG3muon", mode, kTRUE)) return kFALSE;
592    }   
593
594    if(iHMPID){
595      if (!LoadSource(Form("%s/HMPID/AliHMPIDAnalysisTask.cxx",gSystem->ExpandPathName("$ALICE_ROOT")), mode, kTRUE))return kFALSE;
596    }
597    
598    if(iHMPIDperf){
599      if (!LoadSource(Form("%s/HMPID/AliHMPIDPerformanceTask.cxx",gSystem->ExpandPathName("$ALICE_ROOT")), mode, kTRUE))return kFALSE;
600    }   
601
602    if(iHMPIDJets){
603      if (!LoadSource(Form("%s/HMPID/AliAnalysisTaskJetsHMPID.cxx",gSystem->ExpandPathName("$ALICE_ROOT")), mode, kTRUE))return kFALSE;
604    }
605    
606    if (iJETANLib) {
607      if (!LoadLibrary("JETAN", mode, kTRUE)) return kFALSE;
608    }
609
610    ::Info("AnalysisTrainHMPID.C::LoadAnalysisLibraries", "Load other libraries:   SUCCESS");
611    return kTRUE;
612 }
613
614
615 //______________________________________________________________________________
616 Bool_t LoadLibrary(const char *module, const char *mode, Bool_t rec=kFALSE)
617 {
618 // Load a module library in a given mode. Reports success.
619    Int_t imode = -1;
620    Int_t result;
621    TString smodule(module);
622    if (!strcmp(mode, "LOCAL")) imode = 0;
623    if (!strcmp(mode, "PROOF")) imode = 1;
624    if (!strcmp(mode, "GRID"))  imode = 2;
625    TString mod(module);
626    if (!mod.Length()) {
627       ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Empty module name");
628       return kFALSE;
629    }   
630    // If a library is specified, just load it
631    if (smodule.EndsWith(".so")) {
632       mod.Remove(mod.Index(".so"));
633       result = gSystem->Load(mod);
634       if (result < 0) {
635          ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not load library %s", module);
636          return kFALSE;
637       }
638       if (rec) anaLibs += Form("%s.so ",mod.Data()); 
639       if (rec) anaLibsExtra += Form("%s.so ",mod.Data()); 
640       return kTRUE;
641    } 
642    // Check if the library is already loaded
643    if (strlen(gSystem->GetLibraries(Form("%s.so", module), "", kFALSE)) > 0)
644       return kTRUE;    
645    switch (imode) {
646       case 0:
647       case 2:
648          if (kUsePAR) {
649             result = SetupPar(module);
650             if (rec) anaPars += Form("%s.par ", module);
651          } else {
652             result = gSystem->Load(Form("lib%s.so", module));
653             if (rec) anaLibs += Form("lib%s.so ", module);
654          }   
655          break;
656       case 1:
657              if(!gSystem->AccessPathName(module)){
658                ::Info("AnalysisTrainHMPID.C::LoadLibrary", "Removing directory %s",module);
659                gSystem->Exec(Form("rm -rf %s",module));
660          }
661          result = gProof->UploadPackage(module);
662          if (result<0) {
663             result = gProof->UploadPackage(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", module)));
664             if (result<0) {
665                ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not find module %s.par in current directory nor in $ALICE_ROOT", module);
666                return kFALSE;
667             }
668          }   
669          result = gProof->EnablePackage(module);
670          break;
671       default:
672          return kFALSE;
673    }         
674    if (result < 0) {
675       ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not load module %s", module);
676       return kFALSE;
677    }
678    return kTRUE;
679 }           
680
681 //______________________________________________________________________________
682 Bool_t LoadSource(const char *source, const char *mode, Bool_t rec=kFALSE)
683 {
684 // Load a module library in a given mode. Reports success.
685    Int_t imode = -1;
686    Int_t result = -1;
687    if (!strcmp(mode, "LOCAL")) imode = 0;
688    if (!strcmp(mode, "PROOF")) imode = 1;
689    if (!strcmp(mode, "GRID"))  imode = 2;
690    TString ssource(source);
691    TString basename = gSystem->BaseName(ssource.Data());
692    if (!ssource.Length()) {
693       ::Error("AnalysisTrainHMPID.C::LoadSource", "Empty task name");
694       return kFALSE;
695    }
696    // we have a source code so compile it
697    if (ssource.EndsWith(".cxx")) {
698      // need to copy it here other wise the path is also used on grid...
699      ssource.Remove(ssource.Index(".cxx"));
700      basename.Remove(basename.Index(".cxx"));
701      Printf("LoadSources:: Copying...  path %s{cxx,h}",ssource.Data());
702      gSystem->Exec(Form("cp %s.cxx . ",ssource.Data()));
703      gSystem->Exec(Form("cp %s.h . ",ssource.Data()));
704      // Path to code
705      // only needed for local compilation, in grid and proof mode 
706      // the task headers are uploaded 
707      //     path.Remove(path.Index(gSystem->BaseName(path.Data())));
708      // Printf("LoadSources:: Including path %s",path.Data());
709      //  if(path.Length()>0)gROOT->ProcessLine(Form(".include %s",path.Data()));
710      Printf("LoadSources:: Loading...  path %s",basename.Data());
711      switch (imode) {
712      case 0:
713        result = gROOT->LoadMacro(Form("%s.cxx++g",basename.Data()));
714        break;
715      case 1:
716        result = gProof->Load(Form("%s.cxx++g",basename.Data()));
717        break;
718      case 2:
719        result = gROOT->LoadMacro(Form("%s.cxx++g",basename.Data()));
720        if (rec){
721        // what we want to compile
722          anaSources += Form("%s.cxx ",basename.Data());
723        // what we need as input...
724          anaLibs += Form("%s.cxx %s.h ",basename.Data(),basename.Data());
725        }
726        break;
727      default:
728        return kFALSE;
729      }
730    }
731    if (result < 0) {
732       ::Error("AnalysisTrainHMPID.C::LoadSources", "Could not load source %s", source);
733       return kFALSE;
734    }
735    return kTRUE;
736 }
737
738 //______________________________________________________________________________
739 TChain *CreateChain(const char *mode, const char *plugin_mode)
740 {
741 // Create the input chain
742    Int_t imode = -1;
743    if (!strcmp(mode, "LOCAL")) imode = 0;
744    if (!strcmp(mode, "PROOF")) imode = 1;
745    if (!strcmp(mode, "GRID"))  imode = 2;
746    TChain *chain = NULL;
747    // Local chain
748    switch (imode) {
749       case 0:
750          if (iAODanalysis) {
751             if (!kLocalXMLDataset.Length()) {
752                // Local AOD
753                chain = new TChain("aodTree");
754                TString line;
755                ifstream in;
756                in.open(kLocalDataList.Data());
757                    while (in.good()) {
758                   in >> line;
759                   if (line.Length() == 0) continue;
760                // cout << " line = " << line << endl;
761                chain->Add(line.Data());
762                    }
763             } else {
764                // Interactive AOD
765                chain = CreateChainSingle(kLocalXMLDataset, "aodTree");
766             }
767          } else {
768             if (!kLocalXMLDataset.Length()) {
769                // Local ESD
770                chain = new TChain("esdTree");
771                TString line;
772                ifstream in;
773                in.open(kLocalDataList.Data());
774                while (in.good()) {
775                   in >> line;
776                   if (line.Length() == 0) continue;
777                   cout << " line = " << line << endl;
778                   chain->Add(line.Data());
779                }
780             } else {
781               // Interactive ESD
782               chain = CreateChainSingle(kLocalXMLDataset, "esdTree");
783                 }
784          }
785          break;
786       case 1:
787          break;
788       case 2:
789          if (kPluginUse) {
790             AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
791             AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
792          } else {
793             TString           treeName = "esdTree";
794             if (iAODanalysis) treeName = "aodTree";
795             chain = CreateChainSingle("wn.xml", treeName);
796          }
797          break;      
798       default:   
799    }
800    if (chain && chain->GetNtrees()) return chain;
801    return NULL;
802 }   
803
804 //______________________________________________________________________________
805 TChain* CreateChainSingle(const char* xmlfile, const char *treeName)
806 {
807    printf("*******************************\n");
808    printf("*** Getting the ESD Chain   ***\n");
809    printf("*******************************\n");
810    TAlienCollection * myCollection  = TAlienCollection::Open(xmlfile);
811
812    if (!myCollection) {
813       ::Error("AnalysisTrainHMPID.C::CreateChainSingle", "Cannot create an AliEn collection from %s", xmlfile) ;
814       return NULL ;
815    }
816
817    TChain* chain = new TChain(treeName);
818    myCollection->Reset() ;
819    while ( myCollection->Next() ) chain->Add(myCollection->GetTURL("")) ;
820    chain->ls();
821    return chain;
822 }
823
824 //______________________________________________________________________________
825 Int_t SetupPar(char* pararchivename)
826 {
827    if (!pararchivename || !strlen(pararchivename)) return -1;
828    char processline[1024];
829    if (gSystem->AccessPathName(Form("%s.par", pararchivename))) {
830       if (!gSystem->AccessPathName(Form("%s/%s.par", gSystem->Getenv("ALICE_ROOT"),pararchivename))) {
831          ::Info("AnalysisTrainHMPID.C::SetupPar", "Getting %s.par from $ALICE_ROOT", pararchivename);
832          TFile::Cp(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", pararchivename)), 
833                    Form("%s.par",pararchivename));
834       } else {
835          ::Error("AnalysisTrainHMPID.C::SetupPar", "Cannot find %s.par", pararchivename);
836          return -1;
837       }   
838    }
839    gSystem->Exec(Form("tar xvzf %s.par", pararchivename));
840
841    TString ocwd = gSystem->WorkingDirectory();
842    if (!gSystem->ChangeDirectory(pararchivename)) return -1;
843         
844    // check for BUILD.sh and execute
845    if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
846       printf("*******************************\n");
847       printf("*** Building PAR archive    ***\n");
848       printf("*******************************\n");          
849       if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
850          Error("runProcess","Cannot Build the PAR Archive! - Abort!");
851          return -1;
852       }
853    }
854
855         // check for SETUP.C and execute
856         if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
857             printf("*******************************\n");
858             printf("*** Setup PAR archive       ***\n");
859             printf("*******************************\n");
860             gROOT->Macro("PROOF-INF/SETUP.C");
861         }       
862         if (!gSystem->ChangeDirectory(ocwd.Data())) return -1;
863    return 0;
864 }
865
866 //______________________________________________________________________________
867 AliAnalysisAlien* CreateAlienHandler(const char *plugin_mode)
868 {
869 // Check if user has a valid token, otherwise make one. This has limitations.
870 // One can always follow the standard procedure of calling alien-token-init then
871 //   source /tmp/gclient_env_$UID in the current shell.
872    if (!AliAnalysisGrid::CreateToken()) return NULL;
873    AliAnalysisAlien *plugin = new AliAnalysisAlien();
874 // Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
875    plugin->SetRunMode(plugin_mode);
876    if (kPluginUseProductionMode) plugin->SetProductionMode();
877    plugin->SetJobTag(kJobTag);
878    plugin->SetNtestFiles(1);
879 //   plugin->SetPreferedSE("ALICE::NIHAM::File");
880 // Set versions of used packages
881    plugin->SetAPIVersion("V1.1x");
882    plugin->SetROOTVersion(kPluginRootVersion);
883    plugin->SetAliROOTVersion(kPluginAliRootVersion);
884
885 // Declare input data to be processed.
886 // Method 1: Create automatically XML collections using alien 'find' command.
887 // Define production directory LFN
888    plugin->SetGridDataDir(kGridDatadir.Data());
889 // Set data search pattern
890    if (iAODanalysis) plugin->SetDataPattern(" *AliAOD.root");
891    else plugin->SetDataPattern(Form(" %s/*/*ESDs.root",kGridPassPattern.Data()));
892 // ...then add run numbers to be considered
893    plugin->SetRunPrefix("000"); // if real data
894    plugin->SetRunRange(kGridRunRange[0], kGridRunRange[1]);
895
896    if(kGridLocalRunList.Length()>0){
897      ifstream in1;
898      in1.open(kGridLocalRunList.Data());
899      int iRun;
900      // just use run numbers, negatives will be excluded
901      while(in1>>iRun){
902        if(iRun>0){
903        Printf("AnalysisTrainHMPID Adding run number from File %s", Form(kGridRunPattern.Data(),iRun));
904        plugin->AddRunNumber(Form(kGridRunPattern.Data(),iRun));
905        } else{
906          Printf("AnalysisTrainHMPID Skipping run number from File %d", iRun);
907        }
908      }
909    }
910
911 // Method 2: Declare existing data files (raw collections, xml collections, root file)
912 // If no path mentioned data is supposed to be in the work directory (see SetGridWorkingDir())
913 // XML collections added via this method can be combined with the first method if
914 // the content is compatible (using or not tags)
915 //   plugin->AddDataFile("Hijing.xml");
916 //   plugin->AddDataFile("/alice/data/2008/LHC08c/000057657/raw/Run57657.Merged.RAW.tag.root");
917 // Define alien work directory where all files will be copied. Relative to alien $HOME.
918    TString alien_workdir = "";
919
920    alien_workdir += kGridWorkDir.Data();
921    if(kGridDataSet.Length()>0)alien_workdir += Form("/%s",kGridDataSet.Data());
922    plugin->SetGridWorkingDir(alien_workdir.Data());
923
924    // Declare alien output directory. Relative to working directory.
925    if (!kGridOutdir.Length()) kGridOutdir = Form("output_%s",kTrainName.Data());
926    plugin->SetGridOutputDir(kGridOutdir);
927
928    // set extra libs before par file compilation
929    anaLibs += kGridExtraFiles;
930    anaLibs  = anaLibs.Strip();   
931    Printf("anaLibs %s",anaLibs.Data());
932    Printf("anaLibsExtra %s",anaLibsExtra.Data());
933
934    if (anaLibs.Length())          plugin->SetAdditionalLibs(anaLibs.Data());
935    if (anaLibsExtra.Length())     plugin->SetAdditionalRootLibs(anaLibsExtra.Data());
936
937    TString ana_sources = "";
938    TString ana_add = "";
939    if (kUsePAR && anaPars.Length()) {
940       printf("%s\n", anaPars.Data());
941       TObjArray *arr;
942       TObjString *objstr;
943       arr = anaPars.Tokenize(" ");
944       TIter next(arr);
945       while ((objstr=(TObjString*)next())) plugin->EnablePackage(objstr->GetString());
946       delete arr;
947    } 
948    
949 // Declare the analysis source files names separated by blancs. To be compiled runtime
950 // using ACLiC on the worker nodes.
951    ana_sources = anaSources.Strip();
952 // Declare all libraries (other than the default ones for the framework. These will be
953 // loaded by the generated analysis macro. Add all extra files (task .cxx/.h) here.
954
955    if (ana_sources.Length()) plugin->SetAnalysisSource(ana_sources);
956    plugin->SetExecutableCommand(kPluginExecutableCommand.Data());
957    // Declare the output file names separated by blancs.
958    // (can be like: file.root or file.root@ALICE::Niham::File)
959    plugin->SetUseSubmitPolicy(kFALSE);
960    plugin->SetMergeExcludes(kGridMergeExclude);
961    plugin->SetMaxMergeFiles(kGridMaxMergeFiles);
962    plugin->SetMaxMergeStages(kGridMaxMergeStages);
963    plugin->SetNrunsPerMaster(kGridRunsPerMaster);
964    plugin->SetMergeViaJDL(kPluginMergeViaJDL);
965    // Use fastread option
966    plugin->SetFastReadOption(kPluginFastReadOption);
967    // UseOverwrite mode
968    plugin->SetOverwriteMode(kPluginOverwriteMode);
969    // Optionally define the files to be archived.
970    //   plugin->SetOutputArchive("log_archive.zip:stdout,stderr@ALICE::NIHAM::File root_archive.zip:AliAOD.root,AOD.tag.root@ALICE::NIHAM::File");
971    plugin->SetOutputToRunNo(kPluginOutputToRunNumber);     // write the output to subdirs named after run number
972 //   plugin->SetDefaultOutputs(kFALSE);
973
974    // Put default output files to archive
975    TString listhists = "";
976    TString listaods  = "";
977    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
978    TIter next(mgr->GetOutputs());
979    AliAnalysisDataContainer *output;
980    while ((output=(AliAnalysisDataContainer*)next())) {
981       const char *filename = output->GetFileName();
982       if (!(strcmp(filename, "default"))) {
983          if (!mgr->GetOutputEventHandler()) continue;
984          filename = mgr->GetOutputEventHandler()->GetOutputFileName();
985          if (listaods.Length()) listaods += " ";
986          listaods += filename;
987       } else {
988          if(!listhists.Contains(filename)){
989             if (listhists.Length()) listhists += " ";
990             listhists += filename;
991          }
992       }
993    }
994
995    if (mgr->GetExtraFiles().Length()) {
996      if (listaods.Length()) listaods += " ";
997      listaods += mgr->GetExtraFiles();
998    }
999
1000    TString outputArchive;
1001    outputArchive = Form("log_archive.zip:std*r@%s",kGridOutputStorages.Data());
1002    listaods.ReplaceAll(" ", ",");
1003    listhists.ReplaceAll(" ", ",");
1004    if (listhists.Length()) listhists = Form("hist_archive.zip:%s@%s", listhists.Data(), kGridOutputStorages.Data());
1005    if (listaods.Length())  listaods  = Form("aod_archive.zip:%s@%s", listaods.Data(), kGridOutputStorages.Data());
1006
1007    if (!listhists.Length() && !listaods.Length()) {
1008       ::Fatal("AnalysisTrainHMPID", "No task output !");
1009    }
1010
1011    if (listaods.Length()) {
1012       outputArchive += " ";
1013       outputArchive += listaods;
1014    }
1015    if (listhists.Length()) {
1016       outputArchive += " ";
1017       outputArchive += listhists;
1018    }
1019 //   plugin->SetOutputArchive(outputArchive);
1020
1021 // Optionally set a name for the generated analysis macro (default MyAnalysis.C)
1022    plugin->SetAnalysisMacro(Form("%s.C", kTrainName.Data()));
1023 // Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
1024    plugin->SetSplitMaxInputFileNumber(kGridFilesPerJob);
1025 // Optionally set number of failed jobs that will trigger killing waiting sub-jobs.
1026 //   plugin->SetMaxInitFailed(5);
1027 // Optionally resubmit threshold.
1028 //   plugin->SetMasterResubmitThreshold(90);
1029 // Optionally set time to live (default 30000 sec)
1030    plugin->SetTTL(30000);
1031 // Optionally set input format (default xml-single)
1032    plugin->SetInputFormat("xml-single");
1033 // Optionally modify the name of the generated JDL (default analysis.jdl)
1034    plugin->SetJDLName(Form("%s.jdl", kTrainName.Data()));
1035 // Optionally modify the executable name (default analysis.sh)
1036    plugin->SetExecutable(Form("%s.sh", kTrainName.Data()));
1037 // Optionally modify job price (default 1)
1038    plugin->SetPrice(1);      
1039 // Optionally modify split mode (default 'se')    
1040    plugin->SetSplitMode("se");
1041    plugin->SetCheckCopy(kFALSE);
1042    return plugin;
1043 }
1044
1045 //______________________________________________________________________________
1046 Bool_t LoadConfig(const char *filename)
1047 {
1048 // Read train configuration from file
1049    if (gSystem->AccessPathName(filename)) {
1050       ::Error("AnalysisTrainHMPID.C::LoadConfig", "Config file name not found");
1051       return kFALSE;
1052    }   
1053    gROOT->ProcessLine(Form(".x %s", filename));
1054    ::Info("AnalysisTrainHMPID.C::LoadConfig", "Train configuration loaded from file %s", filename);
1055    return kTRUE;
1056 }
1057
1058 //______________________________________________________________________________
1059 Bool_t PatchJDL(){
1060   Printf(">>> Patching JDL");
1061   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
1062   AliAnalysisAlien*    gridHandler = (AliAnalysisAlien*)mgr->GetGridHandler();
1063   TGridJDL *jdl = gridHandler->GetGridJDL();
1064   if(iJETAN)jdl->AddToPackages("fastjet","v2.4.0");
1065   gridHandler->WriteJDL(kFALSE);
1066   Printf("<<<  Patching JDL");
1067   return kTRUE;
1068 }
1069
1070 //______________________________________________________________________________
1071 Bool_t PatchAnalysisMacro(){
1072   Printf(">>> Patching AnalysisMacro");
1073   gSystem->Exec(Form("mv %s.C %s.C_tmp",kTrainName.Data(),kTrainName.Data()));
1074
1075   ifstream in1; 
1076   in1.open(Form("%s.C_tmp", kTrainName.Data()));
1077   char cLine[250];
1078   TString st;
1079   while(in1.getline(cLine,250)){
1080     st += cLine;
1081     st += "\n";
1082   }
1083   Int_t index= -1;
1084   index = st.Index("gSystem->Load(\"libPhysics\");");
1085   index += strlen("gSystem->Load(\"libPhysics\");");
1086   /*
1087     TObjArray *arr;
1088     TObjString *objstr;
1089     arr = anaLibs.Tokenize(" ");
1090     TIter next(arr);
1091
1092     add += "\n\n // added by CKB \n";
1093     while ((objstr=(TObjString*)next())){
1094       if(objstr->GetString().Contains("PWG3"))continue;
1095       if(objstr->GetString().EndsWith(".so"))add += Form("gSystem->Load(\"%s\");\n",objstr->GetString().Data());
1096     }
1097     delete arr; 
1098     */
1099     //    add += Form("AliLog::SetGlobalLogLevel(%d);\n",AliLog::GetGlobalLogLevel());
1100   TString add = "";
1101
1102   if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1103   add += "\n\n // added by CKB \n";
1104   add += "\n gSystem->AddIncludePath(\"./\"); \n";
1105   if(gGrid && kPluginAliRootVersion.Length()==0){
1106     add += "\n // Dirty hack for TRD reference data \n";
1107     add += "\n gSystem->Setenv(\"ALICE_ROOT\",\"";
1108     add += Form("alien://%s/rootfiles/",gGrid->GetHomeDirectory());
1109     add += "\"); \n";
1110   }
1111   add += "// BKC \n\n";
1112   st.Insert(index,add.Data());
1113
1114   if(kUseCPAR && kPluginAliRootVersion.Length()==0){
1115     index = st.Index("gSystem->AddIncludePath(\"-I$"); // uncommen $ALICE_ROOT include for par files
1116     if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1117     st.Insert(index,"// CKB comment out whehn no aliroot is provided \n //");
1118   }
1119
1120   ofstream out;
1121   out.open(Form("%s.C", kTrainName.Data()));
1122   if (out.bad()) {
1123     return kFALSE;
1124   }
1125   out << st << endl;
1126   Printf("<<< Patching AnalysisMacro");
1127
1128   Printf(">>> Patching Merge Macro");
1129   gSystem->Exec(Form("mv %s_merge.C %s_merge.C_tmp",kTrainName.Data(),kTrainName.Data()));
1130
1131   ifstream in2; 
1132   in2.open(Form("%s_merge.C_tmp", kTrainName.Data()));
1133   TString st2;
1134   while(in2.getline(cLine,250)){
1135     st2 += cLine;
1136     st2 += "\n";
1137   }
1138   index = st2.Index("gSystem->Load(\"libPhysics\");");
1139   index += strlen("gSystem->Load(\"libPhysics\");");
1140   TString add2 = "";
1141   add2 += "\n gSystem->AddIncludePath(\"./\"); \n";
1142   if(gGrid&&kPluginAliRootVersion.Length()==0){
1143     add2 += "\n // Dirty hack for TRD reference data \n";
1144     add2 += "\n gSystem->Setenv(\"ALICE_ROOT\",\"";
1145     add2 += Form("alien://%s/rootfiles/",gGrid->GetHomeDirectory());
1146     add2 += "\"); \n";
1147   }
1148   add2 += "// BKC \n\n";
1149   if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1150   st2.Insert(index,add.Data());
1151
1152   if(kUseCPAR&&kPluginAliRootVersion.Length()==0){
1153     index = st2.Index("gSystem->AddIncludePath(\"-I$"); // uncommen $ALICE_ROOT include for par files
1154     if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1155     st2.Insert(index,"// CKB comment out whehn no aliroot is provided \n //");
1156   }
1157
1158   // do not exclude the extra files from merign, this is done explicitly in this train script
1159   index = st2.Index("mergeExcludes +="); // uncommen $ALICE_ROOT include for par files
1160   if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1161   st2.Insert(index,"// CKB comment out, handled explicitly by the train macro \n //");
1162
1163
1164   ofstream out2;
1165   out2.open(Form("%s_merge.C", kTrainName.Data()));
1166   if (out2.bad()) {
1167     return kFALSE;
1168   }
1169   out2 << st2 << endl;
1170   Printf("<<< Patching Merging Macro");
1171
1172
1173   return kTRUE;
1174
1175 }