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