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.
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
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 Bool_t kUseKinefilter = kFALSE; // use Kinematics filter
28 Bool_t kUseMuonfilter = kFALSE; // use muon filter
29 TString kCommonOutputFileName = "HmpidOutput.root";
32 //== general process variables
34 // ### Other flags to steer the analysis
35 //==============================================================================
36 Bool_t kSkipTerminate = kFALSE; // Do not call Teminate
37 Bool_t kDebugLevel = kTRUE; // activate debugging
38 Int_t kUseSysInfo = 0; // activate debugging
39 Bool_t kUseMC = kTRUE; // use MC info
40 Bool_t kIsMC = kTRUE; // is MC info, if false it overwrites Use(AOD)MC
41 Bool_t kUseESDTags = kTRUE; // use ESD tags for selection
42 Bool_t kUseTR = kFALSE; // use track references
44 // ### Analysis modules to be included. Some may not be yet fully implemented.
45 //==============================================================================
46 Int_t iHMPID = 1; // Basic HMPID analysis task
47 Int_t iJETAN = 0; // Jet analysis (PWG4) // 1 write standard 2 write non-standard jets
49 Int_t kHighPtFilterMask = 16; // change depending on the used AOD Filter
52 //==============================================================================
53 // ### PROOF Steering varibales
54 //==============================================================================
55 //== proof setup variables
56 TString kProofCluster = "alice-caf.cern.ch";
57 Bool_t kProofUseAFPAR = kTRUE; // use AF special par file
58 TString kProofAFversion = "VO_ALICE@AliRoot::v4-20-08-AN";
59 //== proof input and output variables
60 TString kProofDataSet = "/alice/sim/LHC10d2_117220";
61 //== proof process variables
62 Bool_t kProofClearPackages = kFALSE;
63 Int_t kProofEvents = 10000;
64 Int_t kProofOffset = 0;
66 //==============================================================================
67 // ### Grid plugin Steering varibiables
68 //==============================================================================
69 //== grid plugin setup variables
70 Bool_t kPluginUse = kTRUE; // do not change
71 Bool_t kPluginUseProductionMode = kFALSE; // use the plugin in production mode
72 TString kPluginRootVersion = "v5-27-06b"; // *CHANGE ME IF MORE RECENT IN GRID*
73 TString kPluginAliRootVersion = "v4-21-05-AN"; // *CHANGE ME IF MORE RECENT IN GRID*
74 Bool_t kPluginMergeViaJDL = kTRUE; // merge via JDL
75 Bool_t kPluginFastReadOption = kFALSE; // use xrootd flags to reduce timeouts
76 Bool_t kPluginOverwriteMode = kTRUE; // overwrite existing collections
77 Int_t kPluginOutputToRunNumber = 1; // write the output to subdirs named after run number
78 TString kPluginExecutableCommand = "root -b -q";
80 // == grid plugin input and output variables
81 TString kGridDatadir = "/alice/sim/LHC10d4a";
82 TString kGridLocalRunList = "";
83 TString kGridWorkDir = "HmpidAnalysis/LHC10d4a"; // Alien working directory
84 TString kGridOutdir = ""; // AliEn output directory. If blank will become output_<kTrainName>
85 TString kGridDataSet = ""; // sub working directory not to confuse different run xmls
86 Int_t kGridRunRange[2] = {120820, 120820}; // Set the run range
87 TString kGridRunPattern = "%03d"; // important for leading zeroes!!
88 TString kGridPassPattern = "";
89 TString kGridExtraFiles = ""; // files that will be added to the input list in the JDL...
90 Int_t kGridMaxMergeFiles = 12; // Number of files merged in a chunk grid run range
91 TString kGridMergeExclude = "AliAOD.root"; // Files that should not be merged
92 TString kGridOutputStorages = "disk=2"; // Make replicas on the storages
93 // == grid process variables
94 Int_t kGridRunsPerMaster = 1; // Number of runs per master job
95 Int_t kGridFilesPerJob = 100; // Maximum number of files per job (gives size of AOD)
97 //==============================================================================
98 // ### Local Steering variables
99 //==============================================================================
100 //== local setup variables
101 //== local input and output variables
102 TString kLocalXMLDataset = ""; // Change local xml dataset for local interactive analysis
103 TString kLocalDataList = "local_deltaaod.txt"; // Change local xml dataset for local interactive analysis
104 // == local process variables
109 TString anaPars = "";
110 TString anaLibs = "";
111 TString anaLibsExtra = "";
112 TString anaSources = "";
113 // Function signatures
114 class AliAnalysisAlien;
116 //______________________________________________________________________________
117 void AnalysisTrainHMPID(const char *analysis_mode="local", const char *plugin_mode="",
118 const char *config_file="",Int_t iOffset = 0)
120 // Main analysis train macro. If a configuration file is provided, all parameters
121 // are taken from there but may be altered by CheckModuleFlags.
123 if (strlen(config_file) && !LoadConfig(config_file)) return;
125 if(iOffset)kProofOffset = iOffset;
126 TString smode(analysis_mode);
128 // Check compatibility of selected modules
129 CheckModuleFlags(smode);
131 printf("==================================================================\n");
132 printf("=========== RUNNING ANALYSIS TRAIN %s IN %s MODE ==========\n", kTrainName.Data(),smode.Data());
133 printf("==================================================================\n");
134 printf("= Configuring analysis train for: =\n");
135 printf("= ESD analysis =\n");
136 if (iPhysicsSelection) printf("= Physics selection =\n");
137 if (iESDfilter) printf("= ESD filter =\n");
138 if (iJETAN) printf("= Jet analysis =\n");
139 printf("==================================================================\n");
140 printf(":: use MC truth %d\n", (UInt_t)kUseMC);
141 printf(":: use KINE filter %d\n", (UInt_t)kUseKinefilter);
142 printf(":: use track refs %d\n", (UInt_t)kUseTR);
143 printf(":: use tags %d\n", (UInt_t)kUseESDTags);
144 printf(":: use debugging %d\n", (UInt_t)kDebugLevel);
145 printf(":: use PAR files %d\n", (UInt_t)kUsePAR);
146 printf(":: use AliEn plugin %d\n", (UInt_t)kPluginUse);
148 //==========================================================================
149 // Connect to back-end system
150 if (!Connect(smode)) {
151 ::Error("AnalysisTrain", "Could not connect to %s back-end", analysis_mode);
155 // Load common libraries and set include path
156 if (!LoadCommonLibraries(smode)) {
157 ::Error("AnalysisTrain", "Could not load common libraries");
163 // Make the analysis manager and connect event handlers
164 AliAnalysisManager *mgr = new AliAnalysisManager("HMPIDTrain", "HMPID train");
165 if (kCommonOutputFileName.Length()>0)mgr->SetCommonFileName(kCommonOutputFileName.Data());
167 if (!strcmp(plugin_mode, "test")) mgr->SetNSysInfo(1);
168 if (kUseSysInfo)mgr->SetNSysInfo(kUseSysInfo);
169 mgr->SetSkipTerminate(kSkipTerminate);
171 // Load analysis specific libraries
172 if (!LoadAnalysisLibraries(smode)) {
173 ::Error("AnalysisTrain", "Could not load analysis libraries");
177 // Create input handler (input container created automatically)
179 AliESDInputHandler *esdHandler = new AliESDInputHandler();
180 if (kUseESDTags) esdHandler->SetReadTags();
181 esdHandler->SetReadFriends(kFALSE);
182 mgr->SetInputEventHandler(esdHandler);
184 // Monte Carlo handler
186 AliMCEventHandler* mcHandler = new AliMCEventHandler();
187 mgr->SetMCtruthEventHandler(mcHandler);
188 mcHandler->SetReadTR(kUseTR);
191 // AOD output container, created automatically when setting an AOD handler
193 // AOD output handler
194 AliAODHandler* aodHandler = new AliAODHandler();
195 aodHandler->SetOutputFileName("AliAOD.root");
196 aodHandler->SetFillAODforRun(kFillAOD);
198 mgr->SetOutputEventHandler(aodHandler);
199 AliAnalysisDataContainer *cout_aod = mgr->GetCommonOutputContainer();
200 cout_aod->SetSpecialOutput();
203 // Debugging if needed
205 mgr->SetDebugLevel(3);
208 mgr->RegisterExtraFile("syswatch.root");
209 if(kGridMergeExclude.Length())kGridMergeExclude += " ";
210 kGridMergeExclude += "syswatch.root";
212 AliLog::SetGlobalLogLevel(AliLog::kError);
215 //==========================================================================
216 // Create the chain. In this example it is created only from ALIEN files but
217 // can be done to work in batch or grid mode as well.
218 TChain *chain = CreateChain(smode, plugin_mode);
220 //==========================================================================
221 // Load the tasks configuration macros for all wagons. These files are supposed now to be
222 // in the current workdir, but in AliEn they will be in the file catalog,
223 // mapped from AliRoot and pecified in the jdl input list.
224 if(iPhysicsSelection){
225 gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
226 AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection(kIsMC,kTRUE,kTRUE); // last flag also adds information on
230 // ESD filter task configuration.
231 gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskESDFilter.C");
232 AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(kUseKinefilter,kUseMuonfilter);
234 mgr->RegisterExtraFile("pyxsec_hists.root");
235 if(kGridMergeExclude.Length())kGridMergeExclude += " ";
236 kGridMergeExclude += "pyxsec_hists.root";
242 gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJets.C");
243 AliAnalysisTaskJets *taskjets = 0;
244 if (iJETAN&1) taskjets = AddTaskJets(kHighPtFilterMask);
245 if (!taskjets) ::Warning("AnalysisTrainHMPID", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
249 gROOT->LoadMacro("$ALICE_ROOT/HMPID/AddTaskHMPID.C");
250 AliHMPIDAnalysisTask *taskHmpid = AddTaskHMPID(kUseMC);
251 if (!taskHmpid) ::Warning("AnalysisTrainHMPID", "AliAnalysisTaskHMPID cannot run for this train conditions - EXCLUDED");
255 AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
256 AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
259 if (mgr->InitAnalysis()) {
261 if (!strcmp(plugin_mode,"submit") && smode=="GRID"){
262 TString alien_workdir = gGrid->GetHomeDirectory();
263 alien_workdir += kGridWorkDir.Data();
264 if(kGridDataSet.Length()>0)alien_workdir += Form("/%s",kGridDataSet.Data());
265 AliAnalysisAlien *gridhandler = (AliAnalysisAlien*)mgr->GetGridHandler();
266 printf("=== AnalysisTrainHMPID:: Registering jdl in the work directory alien://%s/%s, should be done by the manager! ===\n",
267 alien_workdir.Data(),gridhandler->GetGridOutputDir());
270 dest = Form("%s/%s/%s.jdl",alien_workdir.Data(),gridhandler->GetGridOutputDir(),kTrainName.Data());
271 if(AliAnalysisAlien::FileExists(dest.Data())){
272 // Printf("%s exist on grid removing...",dest.Data());
273 // gGrid->Rm(dest.Data());
275 TFile::Cp(Form("file:%s.jdl",kTrainName.Data()),Form("alien://%s",dest.Data()));
279 dest = Form("%s/%s/%s_merge.jdl",alien_workdir.Data(),gridhandler->GetGridOutputDir(),kTrainName.Data());
280 if(AliAnalysisAlien::FileExists(dest.Data())){
281 // Printf("%s exist on grid removing...",dest.Data());
282 // gGrid->Rm(dest.Data());
284 TFile::Cp(Form("file:%s_merge.jdl",kTrainName.Data()),Form("alien://%s",dest.Data()));
287 AliLog::SetGlobalLogLevel(AliLog::kError);
288 if((kUseSysInfo>0 && smode=="LOCAL") || !strcmp(plugin_mode, "test")){
289 TFile *fM = TFile::Open("manager_local.root","RECREATE");
294 StartAnalysis(smode, chain);
296 if((kUseSysInfo>0 && smode=="LOCAL") || !strcmp(plugin_mode, "test")){
297 for(int i = 0;i < mgr->GetTopTasks()->GetEntries();i++){
301 if (!strcmp(plugin_mode, "offline") && smode=="GRID"){
302 // Offline mode path files
304 PatchAnalysisMacro();
310 //______________________________________________________________________________
311 void StartAnalysis(const char *mode, TChain *chain) {
314 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
315 if (!strcmp(mode, "LOCAL")) imode = 0;
316 if (!strcmp(mode, "PROOF")) imode = 1;
317 if (!strcmp(mode, "GRID")) imode = 2;
321 ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Cannot create the chain");
324 mgr->StartAnalysis(mode, chain,kNumberOfEvents);
327 if (!kProofDataSet.Length()) {
328 ::Error("AnalysisTrainHMPID.C::StartAnalysis", "kProofDataSet is empty");
331 mgr->StartAnalysis(mode, kProofDataSet, kProofEvents,kProofOffset);
335 if (!mgr->GetGridHandler()) {
336 ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Grid plugin not initialized");
339 mgr->StartAnalysis("grid");
342 ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Cannot create the chain");
345 mgr->StartAnalysis(mode, chain);
351 //______________________________________________________________________________
352 void CheckModuleFlags(const char *mode) {
353 // Checks selected modules and insure compatibility
355 if (!strcmp(mode, "LOCAL")) imode = 0;
356 if (!strcmp(mode, "PROOF")) imode = 1;
357 if (!strcmp(mode, "GRID")) imode = 2;
361 kPluginAliRootVersion = ""; // NO aliroot if we use CPAR
366 ::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "PAR files enabled due to PROOF analysis");
371 ::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "AliEn plugin disabled since not in GRID mode");
376 // switch off anthin related to MC
384 if(kUseKinefilter)::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "Kine Filter disabled in analysis without MC");
385 kUseKinefilter = kFALSE;
391 kUseKinefilter = kFALSE;
392 kUseMuonfilter = kFALSE;
395 iJETANLib = iJETAN && 1;
396 if (iESDfilter) {iAODhandler=1;}
397 if (kUseKinefilter && !kUseMC) kUseKinefilter = kFALSE;
402 //______________________________________________________________________________
403 Bool_t Connect(const char *mode) {
404 // Connect <username> to the back-end system.
406 if (!strcmp(mode, "LOCAL")) imode = 0;
407 if (!strcmp(mode, "PROOF")) imode = 1;
408 if (!strcmp(mode, "GRID")) imode = 2;
409 TString username = gSystem->Getenv("alien_API_USER");
414 if (!username.Length()) {
415 ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode), "Make sure you:\n \
416 1. Have called: alien-token-init <username>\n \
417 2. Have called: >source /tmp/gclient_env_$UID");
420 ::Info("AnalysisTrainHMPID.C::Connect", "Connecting user <%s> to PROOF cluster <%s>",
421 username.Data(), kProofCluster.Data());
422 gEnv->SetValue("XSec.GSI.DelegProxy", "2");
423 TProof::Open(Form("%s@%s", username.Data(), kProofCluster.Data()));
425 if (strcmp(gSystem->Getenv("XrdSecGSISRVNAMES"), "lxfsrd0506.cern.ch"))
426 ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode), "Environment XrdSecGSISRVNAMES different from lxfsrd0506.cern.ch");
429 if(kProofClearPackages)gProof->ClearPackages();
432 if (!username.Length()) {
433 ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode), "Make sure you:\n \
434 1. Have called: alien-token-init <username>\n \
435 2. Have called: >source /tmp/gclient_env_$UID");
438 if (kPluginUse && !gSystem->Getenv("alien_CLOSE_SE")) {
439 ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode),
440 "When using the AliEn plugin it is preferable to define the \
441 variable alien_CLOSE_SE in your environment.");
444 ::Info("AnalysisTrainHMPID.C::Connect", "Connecting user <%s> to AliEn ...",
446 TGrid::Connect("alien://");
447 if (!gGrid || !gGrid->IsConnected()) return kFALSE;
450 ::Error("AnalysisTrainHMPID.C::Connect", "Unknown run mode: %s", mode);
453 ::Info("AnalysisTrainHMPID.C::Connect","Connected in %s mode", mode);
458 //______________________________________________________________________________
459 Bool_t LoadCommonLibraries(const char *mode)
461 // Load common analysis libraries.
463 if (!strcmp(mode, "LOCAL")) imode = 0;
464 if (!strcmp(mode, "PROOF")) imode = 1;
465 if (!strcmp(mode, "GRID")) imode = 2;
466 if (!gSystem->Getenv("ALICE_ROOT")) {
467 ::Error("AnalysisTrainHMPID.C::LoadCommonLibraries", "Analysis train requires that analysis libraries are compiled with a local AliRoot");
470 Bool_t success = kTRUE;
472 gSystem->Load("libTree.so");
473 gSystem->Load("libGeom.so");
474 gSystem->Load("libVMC.so");
475 gSystem->Load("libPhysics.so");
477 // Load framework classes. Par option ignored here.
482 success &= LoadLibrary("STEERBase", mode, kTRUE);
483 success &= LoadLibrary("ESD", mode, kTRUE);
484 success &= LoadLibrary("AOD", mode, kTRUE);
485 success &= LoadLibrary("ANALYSIS", mode, kTRUE);
486 success &= LoadLibrary("ANALYSISalice", mode, kTRUE);
487 success &= LoadLibrary("CORRFW", mode, kTRUE);
489 success &= LoadLibrary("libSTEERBase.so", mode);
490 success &= LoadLibrary("libESD.so", mode);
491 success &= LoadLibrary("libAOD.so", mode);
492 success &= LoadLibrary("libANALYSIS.so", mode);
493 success &= LoadLibrary("libANALYSISalice.so", mode);
494 success &= LoadLibrary("libCORRFW.so", mode);
495 gROOT->ProcessLine(".include $ALICE_ROOT/include");
499 if (!kProofUseAFPAR) {
500 success &= LoadLibrary("STEERBase", mode);
501 success &= LoadLibrary("ESD", mode);
502 success &= LoadLibrary("AOD", mode);
503 success &= LoadLibrary("ANALYSIS", mode);
504 success &= LoadLibrary("ANALYSISalice", mode);
505 success &= LoadLibrary("CORRFW", mode);
507 success &= !gProof->EnablePackage(kProofAFversion);
508 success &= LoadLibrary("CORRFW", mode);
512 ::Error("AnalysisTrainHMPID.C::LoadCommonLibraries", "Unknown run mode: %s", mode);
516 ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Load common libraries: SUCCESS");
517 ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Include path for Aclic compilation:\n%s",
518 gSystem->GetIncludePath());
520 ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Load common libraries: FAILED");
527 //______________________________________________________________________________
528 Bool_t LoadAnalysisLibraries(const char *mode)
530 // Load common analysis libraries.
531 Bool_t success = kTRUE;
533 if (!LoadLibrary("PWG3base", mode, kTRUE) ||
534 !LoadLibrary("PWG3muon", mode, kTRUE)) return kFALSE;
538 if (!LoadSource(Form("%s/HMPID/AliHMPIDAnalysisTask.cxx",gSystem->ExpandPathName("$ALICE_ROOT")), mode, kTRUE))return kFALSE;
542 if (!LoadLibrary("JETAN", mode, kTRUE)) return kFALSE;
545 ::Info("AnalysisTrainHMPID.C::LoadAnalysisLibraries", "Load other libraries: SUCCESS");
550 //______________________________________________________________________________
551 Bool_t LoadLibrary(const char *module, const char *mode, Bool_t rec=kFALSE)
553 // Load a module library in a given mode. Reports success.
556 TString smodule(module);
557 if (!strcmp(mode, "LOCAL")) imode = 0;
558 if (!strcmp(mode, "PROOF")) imode = 1;
559 if (!strcmp(mode, "GRID")) imode = 2;
562 ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Empty module name");
565 // If a library is specified, just load it
566 if (smodule.EndsWith(".so")) {
567 mod.Remove(mod.Index(".so"));
568 result = gSystem->Load(mod);
570 ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not load library %s", module);
573 if (rec) anaLibs += Form("%s.so ",mod.Data());
574 if (rec) anaLibsExtra += Form("%s.so ",mod.Data());
577 // Check if the library is already loaded
578 if (strlen(gSystem->GetLibraries(Form("%s.so", module), "", kFALSE)) > 0)
584 result = SetupPar(module);
585 if (rec) anaPars += Form("%s.par ", module);
587 result = gSystem->Load(Form("lib%s.so", module));
588 if (rec) anaLibs += Form("lib%s.so ", module);
592 if(!gSystem->AccessPathName(module)){
593 ::Info("AnalysisTrainHMPID.C::LoadLibrary", "Removing directory %s",module);
594 gSystem->Exec(Form("rm -rf %s",module));
596 result = gProof->UploadPackage(module);
598 result = gProof->UploadPackage(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", module)));
600 ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not find module %s.par in current directory nor in $ALICE_ROOT", module);
604 result = gProof->EnablePackage(module);
610 ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not load module %s", module);
616 //______________________________________________________________________________
617 Bool_t LoadSource(const char *source, const char *mode, Bool_t rec=kFALSE)
619 // Load a module library in a given mode. Reports success.
622 if (!strcmp(mode, "LOCAL")) imode = 0;
623 if (!strcmp(mode, "PROOF")) imode = 1;
624 if (!strcmp(mode, "GRID")) imode = 2;
625 TString ssource(source);
626 TString basename = gSystem->BaseName(ssource.Data());
627 if (!ssource.Length()) {
628 ::Error("AnalysisTrainHMPID.C::LoadSource", "Empty task name");
631 // we have a source code so compile it
632 if (ssource.EndsWith(".cxx")) {
633 // need to copy it here other wise the path is also used on grid...
634 ssource.Remove(ssource.Index(".cxx"));
635 basename.Remove(basename.Index(".cxx"));
636 Printf("LoadSources:: Copying... path %s{cxx,h}",ssource.Data());
637 gSystem->Exec(Form("cp %s.cxx . ",ssource.Data()));
638 gSystem->Exec(Form("cp %s.h . ",ssource.Data()));
640 // only needed for local compilation, in grid and proof mode
641 // the task headers are uploaded
642 // path.Remove(path.Index(gSystem->BaseName(path.Data())));
643 // Printf("LoadSources:: Including path %s",path.Data());
644 // if(path.Length()>0)gROOT->ProcessLine(Form(".include %s",path.Data()));
645 Printf("LoadSources:: Loading... path %s",basename.Data());
648 result = gROOT->LoadMacro(Form("%s.cxx++g",basename.Data()));
651 result = gProof->Load(Form("%s.cxx++g",basename.Data()));
654 result = gROOT->LoadMacro(Form("%s.cxx++g",basename.Data()));
656 // what we want to compile
657 anaSources += Form("%s.cxx ",basename.Data());
658 // what we need as input...
659 anaLibs += Form("%s.cxx %s.h ",basename.Data(),basename.Data());
667 ::Error("AnalysisTrainHMPID.C::LoadSources", "Could not load source %s", source);
673 //______________________________________________________________________________
674 TChain *CreateChain(const char *mode, const char *plugin_mode)
676 // Create the input chain
678 if (!strcmp(mode, "LOCAL")) imode = 0;
679 if (!strcmp(mode, "PROOF")) imode = 1;
680 if (!strcmp(mode, "GRID")) imode = 2;
681 TChain *chain = NULL;
685 if (!kLocalXMLDataset.Length()) {
687 chain = new TChain("esdTree");
690 in.open(kLocalDataList.Data());
693 if (line.Length() == 0) continue;
694 cout << " line = " << line << endl;
695 chain->Add(line.Data());
699 chain = CreateChainSingle(kLocalXMLDataset, "esdTree");
706 AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
707 AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
709 TString treeName = "esdTree";
710 chain = CreateChainSingle("wn.xml", treeName);
715 if (chain && chain->GetNtrees()) return chain;
719 //______________________________________________________________________________
720 TChain* CreateChainSingle(const char* xmlfile, const char *treeName)
722 printf("*******************************\n");
723 printf("*** Getting the ESD Chain ***\n");
724 printf("*******************************\n");
725 TAlienCollection * myCollection = TAlienCollection::Open(xmlfile);
728 ::Error("AnalysisTrainHMPID.C::CreateChainSingle", "Cannot create an AliEn collection from %s", xmlfile) ;
732 TChain* chain = new TChain(treeName);
733 myCollection->Reset() ;
734 while ( myCollection->Next() ) chain->Add(myCollection->GetTURL("")) ;
739 //______________________________________________________________________________
740 Int_t SetupPar(char* pararchivename)
742 if (!pararchivename || !strlen(pararchivename)) return -1;
743 char processline[1024];
744 if (gSystem->AccessPathName(Form("%s.par", pararchivename))) {
745 if (!gSystem->AccessPathName(Form("%s/%s.par", gSystem->Getenv("ALICE_ROOT"),pararchivename))) {
746 ::Info("AnalysisTrainHMPID.C::SetupPar", "Getting %s.par from $ALICE_ROOT", pararchivename);
747 TFile::Cp(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", pararchivename)),
748 Form("%s.par",pararchivename));
750 ::Error("AnalysisTrainHMPID.C::SetupPar", "Cannot find %s.par", pararchivename);
754 gSystem->Exec(Form("tar xvzf %s.par", pararchivename));
756 TString ocwd = gSystem->WorkingDirectory();
757 if (!gSystem->ChangeDirectory(pararchivename)) return -1;
759 // check for BUILD.sh and execute
760 if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
761 printf("*******************************\n");
762 printf("*** Building PAR archive ***\n");
763 printf("*******************************\n");
764 if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
765 Error("runProcess","Cannot Build the PAR Archive! - Abort!");
770 // check for SETUP.C and execute
771 if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
772 printf("*******************************\n");
773 printf("*** Setup PAR archive ***\n");
774 printf("*******************************\n");
775 gROOT->Macro("PROOF-INF/SETUP.C");
777 if (!gSystem->ChangeDirectory(ocwd.Data())) return -1;
781 //______________________________________________________________________________
782 AliAnalysisAlien* CreateAlienHandler(const char *plugin_mode)
784 // Check if user has a valid token, otherwise make one. This has limitations.
785 // One can always follow the standard procedure of calling alien-token-init then
786 // source /tmp/gclient_env_$UID in the current shell.
787 if (!AliAnalysisGrid::CreateToken()) return NULL;
788 AliAnalysisAlien *plugin = new AliAnalysisAlien();
789 // Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
790 plugin->SetRunMode(plugin_mode);
791 if (kPluginUseProductionMode) plugin->SetProductionMode();
792 plugin->SetJobTag(kJobTag);
793 plugin->SetNtestFiles(1);
794 // plugin->SetPreferedSE("ALICE::NIHAM::File");
795 // Set versions of used packages
796 plugin->SetAPIVersion("V1.1x");
797 plugin->SetROOTVersion(kPluginRootVersion);
798 plugin->SetAliROOTVersion(kPluginAliRootVersion);
800 // Declare input data to be processed.
801 // Method 1: Create automatically XML collections using alien 'find' command.
802 // Define production directory LFN
803 plugin->SetGridDataDir(kGridDatadir.Data());
804 // Set data search pattern
805 plugin->SetDataPattern(Form(" %s/*/*ESDs.root",kGridPassPattern.Data()));
806 // ...then add run numbers to be considered
807 plugin->SetRunRange(kGridRunRange[0], kGridRunRange[1]);
809 if(kGridLocalRunList.Length()>0){
811 in1.open(kGridLocalRunList.Data());
813 // just use run numbers, negatives will be excluded
816 Printf("AnalysisTrainHMPID Adding run number from File %s", Form(kGridRunPattern.Data(),iRun));
817 plugin->AddRunNumber(Form(kGridRunPattern.Data(),iRun));
819 Printf("AnalysisTrainHMPID Skipping run number from File %d", iRun);
824 // Method 2: Declare existing data files (raw collections, xml collections, root file)
825 // If no path mentioned data is supposed to be in the work directory (see SetGridWorkingDir())
826 // XML collections added via this method can be combined with the first method if
827 // the content is compatible (using or not tags)
828 // plugin->AddDataFile("Hijing.xml");
829 // plugin->AddDataFile("/alice/data/2008/LHC08c/000057657/raw/Run57657.Merged.RAW.tag.root");
830 // Define alien work directory where all files will be copied. Relative to alien $HOME.
831 TString alien_workdir = "";
833 alien_workdir += kGridWorkDir.Data();
834 if(kGridDataSet.Length()>0)alien_workdir += Form("/%s",kGridDataSet.Data());
835 plugin->SetGridWorkingDir(alien_workdir.Data());
837 // Declare alien output directory. Relative to working directory.
838 if (!kGridOutdir.Length()) kGridOutdir = Form("output_%s",kTrainName.Data());
839 plugin->SetGridOutputDir(kGridOutdir);
841 // set extra libs before par file compilation
842 anaLibs += kGridExtraFiles;
843 anaLibs = anaLibs.Strip();
844 Printf("anaLibs %s",anaLibs.Data());
845 Printf("anaLibsExtra %s",anaLibsExtra.Data());
847 if (anaLibs.Length()) plugin->SetAdditionalLibs(anaLibs.Data());
848 if (anaLibsExtra.Length()) plugin->SetAdditionalRootLibs(anaLibsExtra.Data());
850 TString ana_sources = "";
851 TString ana_add = "";
852 if (kUsePAR && anaPars.Length()) {
853 printf("%s\n", anaPars.Data());
856 arr = anaPars.Tokenize(" ");
858 while ((objstr=(TObjString*)next())) plugin->EnablePackage(objstr->GetString());
862 // Declare the analysis source files names separated by blancs. To be compiled runtime
863 // using ACLiC on the worker nodes.
864 ana_sources = anaSources.Strip();
865 // Declare all libraries (other than the default ones for the framework. These will be
866 // loaded by the generated analysis macro. Add all extra files (task .cxx/.h) here.
868 if (ana_sources.Length()) plugin->SetAnalysisSource(ana_sources);
869 plugin->SetExecutableCommand(kPluginExecutableCommand.Data());
870 // Declare the output file names separated by blancs.
871 // (can be like: file.root or file.root@ALICE::Niham::File)
872 plugin->SetUseSubmitPolicy(kFALSE);
873 plugin->SetMergeExcludes(kGridMergeExclude);
874 plugin->SetMaxMergeFiles(kGridMaxMergeFiles);
875 plugin->SetNrunsPerMaster(kGridRunsPerMaster);
876 plugin->SetMergeViaJDL(kPluginMergeViaJDL);
877 // Use fastread option
878 plugin->SetFastReadOption(kPluginFastReadOption);
880 plugin->SetOverwriteMode(kPluginOverwriteMode);
881 // Optionally define the files to be archived.
882 // plugin->SetOutputArchive("log_archive.zip:stdout,stderr@ALICE::NIHAM::File root_archive.zip:AliAOD.root,AOD.tag.root@ALICE::NIHAM::File");
883 plugin->SetOutputToRunNo(kPluginOutputToRunNumber); // write the output to subdirs named after run number
884 // plugin->SetDefaultOutputs(kFALSE);
886 // Put default output files to archive
887 TString listhists = "";
888 TString listaods = "";
889 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
890 TIter next(mgr->GetOutputs());
891 AliAnalysisDataContainer *output;
892 while ((output=(AliAnalysisDataContainer*)next())) {
893 const char *filename = output->GetFileName();
894 if (!(strcmp(filename, "default"))) {
895 if (!mgr->GetOutputEventHandler()) continue;
896 filename = mgr->GetOutputEventHandler()->GetOutputFileName();
897 if (listaods.Length()) listaods += " ";
898 listaods += filename;
900 if(!listhists.Contains(filename)){
901 if (listhists.Length()) listhists += " ";
902 listhists += filename;
907 if (mgr->GetExtraFiles().Length()) {
908 if (listaods.Length()) listaods += " ";
909 listaods += mgr->GetExtraFiles();
912 TString outputArchive;
913 outputArchive = Form("log_archive.zip:std*r@%s",kGridOutputStorages.Data());
914 listaods.ReplaceAll(" ", ",");
915 listhists.ReplaceAll(" ", ",");
916 if (listhists.Length()) listhists = Form("hist_archive.zip:%s@%s", listhists.Data(), kGridOutputStorages.Data());
917 if (listaods.Length()) listaods = Form("aod_archive.zip:%s@%s", listaods.Data(), kGridOutputStorages.Data());
919 if (!listhists.Length() && !listaods.Length()) {
920 ::Fatal("AnalysisTrainHMPID", "No task output !");
923 if (listaods.Length()) {
924 outputArchive += " ";
925 outputArchive += listaods;
927 if (listhists.Length()) {
928 outputArchive += " ";
929 outputArchive += listhists;
931 // plugin->SetOutputArchive(outputArchive);
933 // Optionally set a name for the generated analysis macro (default MyAnalysis.C)
934 plugin->SetAnalysisMacro(Form("%s.C", kTrainName.Data()));
935 // Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
936 plugin->SetSplitMaxInputFileNumber(kGridFilesPerJob);
937 // Optionally set number of failed jobs that will trigger killing waiting sub-jobs.
938 // plugin->SetMaxInitFailed(5);
939 // Optionally resubmit threshold.
940 // plugin->SetMasterResubmitThreshold(90);
941 // Optionally set time to live (default 30000 sec)
942 plugin->SetTTL(30000);
943 // Optionally set input format (default xml-single)
944 plugin->SetInputFormat("xml-single");
945 // Optionally modify the name of the generated JDL (default analysis.jdl)
946 plugin->SetJDLName(Form("%s.jdl", kTrainName.Data()));
947 // Optionally modify the executable name (default analysis.sh)
948 plugin->SetExecutable(Form("%s.sh", kTrainName.Data()));
949 // Optionally modify job price (default 1)
951 // Optionally modify split mode (default 'se')
952 plugin->SetSplitMode("se");
953 plugin->SetCheckCopy(kFALSE);
957 //______________________________________________________________________________
958 Bool_t LoadConfig(const char *filename)
960 // Read train configuration from file
961 if (gSystem->AccessPathName(filename)) {
962 ::Error("AnalysisTrainHMPID.C::LoadConfig", "Config file name not found");
965 gROOT->ProcessLine(Form(".x %s", filename));
966 ::Info("AnalysisTrainHMPID.C::LoadConfig", "Train configuration loaded from file %s", filename);
970 //______________________________________________________________________________
972 Printf(">>> Patching JDL");
973 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
974 AliAnalysisAlien* gridHandler = (AliAnalysisAlien*)mgr->GetGridHandler();
975 TGridJDL *jdl = gridHandler->GetGridJDL();
976 if(iJETAN)jdl->AddToPackages("fastjet","v2.4.0");
977 gridHandler->WriteJDL(kFALSE);
978 Printf("<<< Patching JDL");
982 //______________________________________________________________________________
983 Bool_t PatchAnalysisMacro(){
984 Printf(">>> Patching AnalysisMacro");
985 gSystem->Exec(Form("mv %s.C %s.C_tmp",kTrainName.Data(),kTrainName.Data()));
988 in1.open(Form("%s.C_tmp", kTrainName.Data()));
991 while(in1.getline(cLine,250)){
996 index = st.Index("gSystem->Load(\"libPhysics\");");
997 index += strlen("gSystem->Load(\"libPhysics\");");
1001 arr = anaLibs.Tokenize(" ");
1004 add += "\n\n // added by CKB \n";
1005 while ((objstr=(TObjString*)next())){
1006 if(objstr->GetString().Contains("PWG3"))continue;
1007 if(objstr->GetString().EndsWith(".so"))add += Form("gSystem->Load(\"%s\");\n",objstr->GetString().Data());
1011 // add += Form("AliLog::SetGlobalLogLevel(%d);\n",AliLog::GetGlobalLogLevel());
1014 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1015 add += "\n\n // added by CKB \n";
1016 add += "\n gSystem->AddIncludePath(\"./\"); \n";
1017 if(gGrid && kPluginAliRootVersion.Length()==0){
1018 add += "\n // Dirty hack for TRD reference data \n";
1019 add += "\n gSystem->Setenv(\"ALICE_ROOT\",\"";
1020 add += Form("alien://%s/rootfiles/",gGrid->GetHomeDirectory());
1023 add += "// BKC \n\n";
1024 st.Insert(index,add.Data());
1026 if(kUseCPAR && kPluginAliRootVersion.Length()==0){
1027 index = st.Index("gSystem->AddIncludePath(\"-I$"); // uncommen $ALICE_ROOT include for par files
1028 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1029 st.Insert(index,"// CKB comment out whehn no aliroot is provided \n //");
1033 out.open(Form("%s.C", kTrainName.Data()));
1038 Printf("<<< Patching AnalysisMacro");
1040 Printf(">>> Patching Merge Macro");
1041 gSystem->Exec(Form("mv %s_merge.C %s_merge.C_tmp",kTrainName.Data(),kTrainName.Data()));
1044 in2.open(Form("%s_merge.C_tmp", kTrainName.Data()));
1046 while(in2.getline(cLine,250)){
1050 index = st2.Index("gSystem->Load(\"libPhysics\");");
1051 index += strlen("gSystem->Load(\"libPhysics\");");
1053 add2 += "\n gSystem->AddIncludePath(\"./\"); \n";
1054 if(gGrid&&kPluginAliRootVersion.Length()==0){
1055 add2 += "\n // Dirty hack for TRD reference data \n";
1056 add2 += "\n gSystem->Setenv(\"ALICE_ROOT\",\"";
1057 add2 += Form("alien://%s/rootfiles/",gGrid->GetHomeDirectory());
1060 add2 += "// BKC \n\n";
1061 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1062 st2.Insert(index,add.Data());
1064 if(kUseCPAR&&kPluginAliRootVersion.Length()==0){
1065 index = st2.Index("gSystem->AddIncludePath(\"-I$"); // uncommen $ALICE_ROOT include for par files
1066 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1067 st2.Insert(index,"// CKB comment out whehn no aliroot is provided \n //");
1070 // do not exclude the extra files from merign, this is done explicitly in this train script
1071 index = st2.Index("mergeExcludes +="); // uncommen $ALICE_ROOT include for par files
1072 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1073 st2.Insert(index,"// CKB comment out, handled explicitly by the train macro \n //");
1077 out2.open(Form("%s_merge.C", kTrainName.Data()));
1081 out2 << st2 << endl;
1082 Printf("<<< Patching Merging Macro");