]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HMPID/AnalysisTrainHMPID.C
AliTPCCalibCE.cxx.diff New algorithm using in addition the laser tracks...
[u/mrichter/AliRoot.git] / HMPID / AnalysisTrainHMPID.C
CommitLineData
66cdae53 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
18TString kTrainName = "hmpidAnalysis"; // (no blancs or special characters)
19TString kJobTag = "HMPID Tasks analysis train configured"; //
20Bool_t kUsePAR = kFALSE; // use par files for extra libs
21Bool_t kUseCPAR = kFALSE; // use par files for common libs
22Bool_t kFillAOD = kTRUE; // switch of AOD filling for on the fly analysis
23
24Int_t iAODhandler = 1; // Analysis produces an AOD or dAOD's
25Int_t iESDfilter = 0; // ESD to AOD filter (barrel + muon tracks)
26Int_t iPhysicsSelection = 1; // Physics selection task
27Bool_t kUseKinefilter = kFALSE; // use Kinematics filter
28Bool_t kUseMuonfilter = kFALSE; // use muon filter
29TString kCommonOutputFileName = "HMPIDTaskOutput.root";
30
31
32//== general process variables
33
34// ### Other flags to steer the analysis
35//==============================================================================
36Bool_t kSkipTerminate = kFALSE; // Do not call Teminate
37Bool_t kDebugLevel = kTRUE; // activate debugging
38Int_t kUseSysInfo = 0; // activate debugging
39Bool_t kUseMC = kTRUE; // use MC info
40Bool_t kIsMC = kTRUE; // is MC info, if false it overwrites Use(AOD)MC
41Bool_t kUseESDTags = kTRUE; // use ESD tags for selection
42Bool_t kUseTR = kFALSE; // use track references
43
44// ### Analysis modules to be included. Some may not be yet fully implemented.
45//==============================================================================
46Int_t iHMPID = 1; // Basic HMPID analysis task
47Int_t iJETAN = 0; // Jet analysis (PWG4) // 1 write standard 2 write non-standard jets
48Int_t iJETANLib = 0;
49Int_t kHighPtFilterMask = 16; // change depending on the used AOD Filter
50
51
52//==============================================================================
53// ### PROOF Steering varibales
54//==============================================================================
55//== proof setup variables
56TString kProofCluster = "alice-caf.cern.ch";
57Bool_t kProofUseAFPAR = kTRUE; // use AF special par file
58TString kProofAFversion = "VO_ALICE@AliRoot::v4-19-20-AN";
59//== proof input and output variables
60TString kProofDataSet = "/alice/sim/LHC10c9_117118";
61//== proof process variables
62Bool_t kProofClearPackages = kFALSE;
63Int_t kProofEvents = 10000;
64Int_t kProofOffset = 0;
65
66//==============================================================================
67// ### Grid plugin Steering varibiables
68//==============================================================================
69//== grid plugin setup variables
70Bool_t kPluginUse = kTRUE; // do not change
71Bool_t kPluginUseProductionMode = kFALSE; // use the plugin in production mode
72TString kPluginRootVersion = "v5-26-00b-6"; // *CHANGE ME IF MORE RECENT IN GRID*
73TString kPluginAliRootVersion = "v4-19-22-AN"; // *CHANGE ME IF MORE RECENT IN GRID*
74Bool_t kPluginMergeViaJDL = kFALSE; // merge via JDL
75Bool_t kPluginFastReadOption = kFALSE; // use xrootd flags to reduce timeouts
76Bool_t kPluginOverwriteMode = kTRUE; // overwrite existing collections
77Int_t kPluginOutputToRunNumber = 1; // write the output to subdirs named after run number
78TString kPluginExecutableCommand = "root -b -q";
79
80// == grid plugin input and output variables
81TString kGridDatadir = "/alice/sim/LHC10d6";
82TString kGridLocalRunList = "";
83TString kGridWorkDir = "HmpidAnalysis/LHC10d6"; // Alien working directory
84TString kGridOutdir = ""; // AliEn output directory. If blank will become output_<kTrainName>
85TString kGridDataSet = ""; // sub working directory not to confuse different run xmls
86Int_t kGridRunRange[2] = {104316, 104316}; // Set the run range
87TString kGridRunPattern = "%03d"; // important for leading zeroes!!
88TString kGridPassPattern = "";
89TString kGridExtraFiles = ""; // files that will be added to the input list in the JDL...
90Int_t kGridMaxMergeFiles = 12; // Number of files merged in a chunk grid run range
91TString kGridMergeExclude = "AliAOD.root"; // Files that should not be merged
92TString kGridOutputStorages = "disk=2"; // Make replicas on the storages
93// == grid process variables
94Int_t kGridRunsPerMaster = 1; // Number of runs per master job
95Int_t kGridFilesPerJob = 100; // Maximum number of files per job (gives size of AOD)
96
97//==============================================================================
98// ### Local Steering variables
99//==============================================================================
100//== local setup variables
101//== local input and output variables
102TString kLocalXMLDataset = ""; // Change local xml dataset for local interactive analysis
103TString kLocalDataList = "local_deltaaod.txt"; // Change local xml dataset for local interactive analysis
104// == local process variables
105
106
107
108// Temporaries.
109TString anaPars = "";
110TString anaLibs = "";
111TString anaLibsExtra = "";
112TString anaSources = "";
113// Function signatures
114class AliAnalysisAlien;
115
116//______________________________________________________________________________
117void AnalysisTrainHMPID(const char *analysis_mode="local", const char *plugin_mode="",
118 const char *config_file="",Int_t iOffset = 0)
119{
120// Main analysis train macro. If a configuration file is provided, all parameters
121// are taken from there but may be altered by CheckModuleFlags.
122
123 if (strlen(config_file) && !LoadConfig(config_file)) return;
124
125 if(iOffset)kProofOffset = iOffset;
126 TString smode(analysis_mode);
127 smode.ToUpper();
128 // Check compatibility of selected modules
129 CheckModuleFlags(smode);
130
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);
147
148 //==========================================================================
149 // Connect to back-end system
150 if (!Connect(smode)) {
151 ::Error("AnalysisTrain", "Could not connect to %s back-end", analysis_mode);
152 return;
153 }
154
155 // Load common libraries and set include path
156 if (!LoadCommonLibraries(smode)) {
157 ::Error("AnalysisTrain", "Could not load common libraries");
158 return;
159 }
160
161
162
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());
166 mgr->SetNSysInfo(0);
167 if (!strcmp(plugin_mode, "test")) mgr->SetNSysInfo(1);
168 if (kUseSysInfo)mgr->SetNSysInfo(kUseSysInfo);
169 mgr->SetSkipTerminate(kSkipTerminate);
170
171 // Load analysis specific libraries
172 if (!LoadAnalysisLibraries(smode)) {
173 ::Error("AnalysisTrain", "Could not load analysis libraries");
174 return;
175 }
176
177 // Create input handler (input container created automatically)
178 // ESD input handler
179 AliESDInputHandler *esdHandler = new AliESDInputHandler();
180 if (kUseESDTags) esdHandler->SetReadTags();
181 esdHandler->SetReadFriends(kFALSE);
182 mgr->SetInputEventHandler(esdHandler);
183
184 // Monte Carlo handler
185 if (kUseMC) {
186 AliMCEventHandler* mcHandler = new AliMCEventHandler();
187 mgr->SetMCtruthEventHandler(mcHandler);
188 mcHandler->SetReadTR(kUseTR);
189 }
190
191 // AOD output container, created automatically when setting an AOD handler
192 if (iAODhandler) {
193 // AOD output handler
194 AliAODHandler* aodHandler = new AliAODHandler();
195 aodHandler->SetOutputFileName("AliAOD.root");
196 aodHandler->SetFillAODforRun(kFillAOD);
197
198 mgr->SetOutputEventHandler(aodHandler);
199 AliAnalysisDataContainer *cout_aod = mgr->GetCommonOutputContainer();
200 cout_aod->SetSpecialOutput();
201 }
202
203 // Debugging if needed
204 if (kDebugLevel){
205 mgr->SetDebugLevel(3);
206 }
207 if(kUseSysInfo>0){
208 mgr->RegisterExtraFile("syswatch.root");
209 if(kGridMergeExclude.Length())kGridMergeExclude += " ";
210 kGridMergeExclude += "syswatch.root";
211 } else {
212 AliLog::SetGlobalLogLevel(AliLog::kError);
213 }
214
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);
219
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
227 }
228
229 if (iESDfilter) {
230 // ESD filter task configuration.
231 gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskESDFilter.C");
232 AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(kUseKinefilter,kUseMuonfilter);
233 if(kIsMC){
234 mgr->RegisterExtraFile("pyxsec_hists.root");
235 if(kGridMergeExclude.Length())kGridMergeExclude += " ";
236 kGridMergeExclude += "pyxsec_hists.root";
237 }
238 }
239
240 // Jet analysis
241 if (iJETAN) {
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");
246 }
247
248 if(iHMPID){
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");
252 }
253
254 if (kPluginUse) {
255 AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
256 AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
257 }
258
259 if (mgr->InitAnalysis()) {
260 mgr->PrintStatus();
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());
268
269 TString dest;
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());
274 }
275 TFile::Cp(Form("file:%s.jdl",kTrainName.Data()),Form("alien://%s",dest.Data()));
276
277
278 TString dest;
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());
283 }
284 TFile::Cp(Form("file:%s_merge.jdl",kTrainName.Data()),Form("alien://%s",dest.Data()));
285 }
286
287 AliLog::SetGlobalLogLevel(AliLog::kError);
288 if((kUseSysInfo>0 && smode=="LOCAL") || !strcmp(plugin_mode, "test")){
289 TFile *fM = TFile::Open("manager_local.root","RECREATE");
290 mgr->Write();
291 fM->Close();
292 }
293
294 StartAnalysis(smode, chain);
295
296 if((kUseSysInfo>0 && smode=="LOCAL") || !strcmp(plugin_mode, "test")){
297 for(int i = 0;i < mgr->GetTopTasks()->GetEntries();i++){
298 mgr->ProfileTask(i);
299 }
300 }
301 if (!strcmp(plugin_mode, "offline") && smode=="GRID"){
302 // Offline mode path files
303 // PatchJDL();
304 PatchAnalysisMacro();
305 }
306 }
307}
308
309
310//______________________________________________________________________________
311void StartAnalysis(const char *mode, TChain *chain) {
312
313 Int_t imode = -1;
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;
318 switch (imode) {
319 case 0:
320 if (!chain) {
321 ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Cannot create the chain");
322 return;
323 }
324 mgr->StartAnalysis(mode, chain,kNumberOfEvents);
325 return;
326 case 1:
327 if (!kProofDataSet.Length()) {
328 ::Error("AnalysisTrainHMPID.C::StartAnalysis", "kProofDataSet is empty");
329 return;
330 }
331 mgr->StartAnalysis(mode, kProofDataSet, kProofEvents,kProofOffset);
332 return;
333 case 2:
334 if (kPluginUse) {
335 if (!mgr->GetGridHandler()) {
336 ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Grid plugin not initialized");
337 return;
338 }
339 mgr->StartAnalysis("grid");
340 } else {
341 if (!chain) {
342 ::Error("AnalysisTrainHMPID.C::StartAnalysis", "Cannot create the chain");
343 return;
344 }
345 mgr->StartAnalysis(mode, chain);
346 }
347 return;
348 }
349}
350
351//______________________________________________________________________________
352void CheckModuleFlags(const char *mode) {
353// Checks selected modules and insure compatibility
354 Int_t imode = -1;
355 if (!strcmp(mode, "LOCAL")) imode = 0;
356 if (!strcmp(mode, "PROOF")) imode = 1;
357 if (!strcmp(mode, "GRID")) imode = 2;
358
359
360 if (kUseCPAR) {
361 kPluginAliRootVersion = ""; // NO aliroot if we use CPAR
362 }
363
364 if (imode==1) {
365 if (!kUsePAR) {
366 ::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "PAR files enabled due to PROOF analysis");
367 kUsePAR = kTRUE;
368 }
369 }
370 if (imode != 2) {
371 ::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "AliEn plugin disabled since not in GRID mode");
372 kPluginUse = kFALSE;
373 }
374
375 if(!kIsMC){
376 // switch off anthin related to MC
377 kUseMC = 0;
378 kUseTR = kFALSE;
379 }
380
381 // ESD analysis
382 if (!kUseMC){
383 kUseTR = kFALSE;
384 if(kUseKinefilter)::Info("AnalysisTrainHMPID.C::CheckModuleFlags", "Kine Filter disabled in analysis without MC");
385 kUseKinefilter = kFALSE;
386 }
387 if (iJETAN){
388 iESDfilter=1;
389 }
390 if (!iESDfilter){
391 kUseKinefilter = kFALSE;
392 kUseMuonfilter = kFALSE;
393 }
394
395 iJETANLib = iJETAN && 1;
396 if (iESDfilter) {iAODhandler=1;}
397 if (kUseKinefilter && !kUseMC) kUseKinefilter = kFALSE;
398
399}
400
401
402//______________________________________________________________________________
403Bool_t Connect(const char *mode) {
404// Connect <username> to the back-end system.
405 Int_t imode = -1;
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");
410 switch (imode) {
411 case 0:
412 break;
413 case 1:
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");
418 return kFALSE;
419 }
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()));
424 if (!gProof) {
425 if (strcmp(gSystem->Getenv("XrdSecGSISRVNAMES"), "lxfsrd0506.cern.ch"))
426 ::Error(Form("AnalysisTrainHMPID.C::Connect <%s>", mode), "Environment XrdSecGSISRVNAMES different from lxfsrd0506.cern.ch");
427 return kFALSE;
428 }
429 if(kProofClearPackages)gProof->ClearPackages();
430 break;
431 case 2:
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");
436 return kFALSE;
437 }
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.");
442 return kFALSE;
443 }
444 ::Info("AnalysisTrainHMPID.C::Connect", "Connecting user <%s> to AliEn ...",
445 username.Data());
446 TGrid::Connect("alien://");
447 if (!gGrid || !gGrid->IsConnected()) return kFALSE;
448 break;
449 default:
450 ::Error("AnalysisTrainHMPID.C::Connect", "Unknown run mode: %s", mode);
451 return kFALSE;
452 }
453 ::Info("AnalysisTrainHMPID.C::Connect","Connected in %s mode", mode);
454 return kTRUE;
455}
456
457
458//______________________________________________________________________________
459Bool_t LoadCommonLibraries(const char *mode)
460{
461// Load common analysis libraries.
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 if (!gSystem->Getenv("ALICE_ROOT")) {
467 ::Error("AnalysisTrainHMPID.C::LoadCommonLibraries", "Analysis train requires that analysis libraries are compiled with a local AliRoot");
468 return kFALSE;
469 }
470 Bool_t success = kTRUE;
471 // ROOT libraries
472 gSystem->Load("libTree.so");
473 gSystem->Load("libGeom.so");
474 gSystem->Load("libVMC.so");
475 gSystem->Load("libPhysics.so");
476
477 // Load framework classes. Par option ignored here.
478 switch (imode) {
479 case 0:
480 case 2:
481 if (kUseCPAR) {
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);
488 } else {
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");
496 }
497 break;
498 case 1:
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);
506 } else {
507 success &= !gProof->EnablePackage(kProofAFversion);
508 success &= LoadLibrary("CORRFW", mode);
509 }
510 break;
511 default:
512 ::Error("AnalysisTrainHMPID.C::LoadCommonLibraries", "Unknown run mode: %s", mode);
513 return kFALSE;
514 }
515 if (success) {
516 ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Load common libraries: SUCCESS");
517 ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Include path for Aclic compilation:\n%s",
518 gSystem->GetIncludePath());
519 } else {
520 ::Info("AnalysisTrainHMPID.C::LoadCommodLibraries", "Load common libraries: FAILED");
521 }
522
523 return success;
524}
525
526
527//______________________________________________________________________________
528Bool_t LoadAnalysisLibraries(const char *mode)
529{
530// Load common analysis libraries.
531 Bool_t success = kTRUE;
532 if (iESDfilter) {
533 if (!LoadLibrary("PWG3base", mode, kTRUE) ||
534 !LoadLibrary("PWG3muon", mode, kTRUE)) return kFALSE;
535 }
536
537 if(iHMPID){
538 if (!LoadSource(Form("%s/HMPID/AliHMPIDAnalysisTask.cxx",gSystem->ExpandPathName("$ALICE_ROOT")), mode, kTRUE))return kFALSE;
539 }
540
541 if (iJETANLib) {
542 if (!LoadLibrary("JETAN", mode, kTRUE)) return kFALSE;
543 }
544
545 ::Info("AnalysisTrainHMPID.C::LoadAnalysisLibraries", "Load other libraries: SUCCESS");
546 return kTRUE;
547}
548
549
550//______________________________________________________________________________
551Bool_t LoadLibrary(const char *module, const char *mode, Bool_t rec=kFALSE)
552{
553// Load a module library in a given mode. Reports success.
554 Int_t imode = -1;
555 Int_t result;
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;
560 TString mod(module);
561 if (!mod.Length()) {
562 ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Empty module name");
563 return kFALSE;
564 }
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);
569 if (result < 0) {
570 ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not load library %s", module);
571 return kFALSE;
572 }
573 if (rec) anaLibs += Form("%s.so ",mod.Data());
574 if (rec) anaLibsExtra += Form("%s.so ",mod.Data());
575 return kTRUE;
576 }
577 // Check if the library is already loaded
578 if (strlen(gSystem->GetLibraries(Form("%s.so", module), "", kFALSE)) > 0)
579 return kTRUE;
580 switch (imode) {
581 case 0:
582 case 2:
583 if (kUsePAR) {
584 result = SetupPar(module);
585 if (rec) anaPars += Form("%s.par ", module);
586 } else {
587 result = gSystem->Load(Form("lib%s.so", module));
588 if (rec) anaLibs += Form("lib%s.so ", module);
589 }
590 break;
591 case 1:
592 if(!gSystem->AccessPathName(module)){
593 ::Info("AnalysisTrainHMPID.C::LoadLibrary", "Removing directory %s",module);
594 gSystem->Exec(Form("rm -rf %s",module));
595 }
596 result = gProof->UploadPackage(module);
597 if (result<0) {
598 result = gProof->UploadPackage(gSystem->ExpandPathName(Form("$ALICE_ROOT/%s.par", module)));
599 if (result<0) {
600 ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not find module %s.par in current directory nor in $ALICE_ROOT", module);
601 return kFALSE;
602 }
603 }
604 result = gProof->EnablePackage(module);
605 break;
606 default:
607 return kFALSE;
608 }
609 if (result < 0) {
610 ::Error("AnalysisTrainHMPID.C::LoadLibrary", "Could not load module %s", module);
611 return kFALSE;
612 }
613 return kTRUE;
614}
615
616//______________________________________________________________________________
617Bool_t LoadSource(const char *source, const char *mode, Bool_t rec=kFALSE)
618{
619// Load a module library in a given mode. Reports success.
620 Int_t imode = -1;
621 Int_t result = -1;
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");
629 return kFALSE;
630 }
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()));
639 // Path to code
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());
646 switch (imode) {
647 case 0:
648 result = gROOT->LoadMacro(Form("%s.cxx++g",basename.Data()));
649 break;
650 case 1:
651 result = gProof->Load(Form("%s.cxx++g",basename.Data()));
652 break;
653 case 2:
654 result = gROOT->LoadMacro(Form("%s.cxx++g",basename.Data()));
655 if (rec){
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());
660 }
661 break;
662 default:
663 return kFALSE;
664 }
665 }
666 if (result < 0) {
667 ::Error("AnalysisTrainHMPID.C::LoadSources", "Could not load source %s", source);
668 return kFALSE;
669 }
670 return kTRUE;
671}
672
673//______________________________________________________________________________
674TChain *CreateChain(const char *mode, const char *plugin_mode)
675{
676// Create the input chain
677 Int_t imode = -1;
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;
682 // Local chain
683 switch (imode) {
684 case 0:
685 if (!kLocalXMLDataset.Length()) {
686 // Local ESD
687 chain = new TChain("esdTree");
688 TString line;
689 ifstream in;
690 in.open(kLocalDataList.Data());
691 while (in.good()) {
692 in >> line;
693 if (line.Length() == 0) continue;
694 cout << " line = " << line << endl;
695 chain->Add(line.Data());
696 }
697 } else {
698 // Interactive ESD
699 chain = CreateChainSingle(kLocalXMLDataset, "esdTree");
700 }
701 break;
702 case 1:
703 break;
704 case 2:
705 if (kPluginUse) {
706 AliAnalysisGrid *alienHandler = CreateAlienHandler(plugin_mode);
707 AliAnalysisManager::GetAnalysisManager()->SetGridHandler(alienHandler);
708 } else {
709 TString treeName = "esdTree";
710 chain = CreateChainSingle("wn.xml", treeName);
711 }
712 break;
713 default:
714 }
715 if (chain && chain->GetNtrees()) return chain;
716 return NULL;
717}
718
719//______________________________________________________________________________
720TChain* CreateChainSingle(const char* xmlfile, const char *treeName)
721{
722 printf("*******************************\n");
723 printf("*** Getting the ESD Chain ***\n");
724 printf("*******************************\n");
725 TAlienCollection * myCollection = TAlienCollection::Open(xmlfile);
726
727 if (!myCollection) {
728 ::Error("AnalysisTrainHMPID.C::CreateChainSingle", "Cannot create an AliEn collection from %s", xmlfile) ;
729 return NULL ;
730 }
731
732 TChain* chain = new TChain(treeName);
733 myCollection->Reset() ;
734 while ( myCollection->Next() ) chain->Add(myCollection->GetTURL("")) ;
735 chain->ls();
736 return chain;
737}
738
739//______________________________________________________________________________
740Int_t SetupPar(char* pararchivename)
741{
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));
749 } else {
750 ::Error("AnalysisTrainHMPID.C::SetupPar", "Cannot find %s.par", pararchivename);
751 return -1;
752 }
753 }
754 gSystem->Exec(Form("tar xvzf %s.par", pararchivename));
755
756 TString ocwd = gSystem->WorkingDirectory();
757 if (!gSystem->ChangeDirectory(pararchivename)) return -1;
758
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!");
766 return -1;
767 }
768 }
769
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");
776 }
777 if (!gSystem->ChangeDirectory(ocwd.Data())) return -1;
778 return 0;
779}
780
781//______________________________________________________________________________
782AliAnalysisAlien* CreateAlienHandler(const char *plugin_mode)
783{
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(2);
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);
799
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/*/*ESD.tag.root",kGridPassPattern.Data()));
806// ...then add run numbers to be considered
807 plugin->SetRunRange(kGridRunRange[0], kGridRunRange[1]);
808
809 if(kGridLocalRunList.Length()>0){
810 ifstream in1;
811 in1.open(kGridLocalRunList.Data());
812 int iRun;
813 // just use run numbers, negatives will be excluded
814 while(in1>>iRun){
815 if(iRun>0){
816 Printf("AnalysisTrainHMPID Adding run number from File %s", Form(kGridRunPattern.Data(),iRun));
817 plugin->AddRunNumber(Form(kGridRunPattern.Data(),iRun));
818 } else{
819 Printf("AnalysisTrainHMPID Skipping run number from File %d", iRun);
820 }
821 }
822 }
823
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 = "";
832
833 alien_workdir += kGridWorkDir.Data();
834 if(kGridDataSet.Length()>0)alien_workdir += Form("/%s",kGridDataSet.Data());
835 plugin->SetGridWorkingDir(alien_workdir.Data());
836
837 // Declare alien output directory. Relative to working directory.
838 if (!kGridOutdir.Length()) kGridOutdir = Form("output_%s",kTrainName.Data());
839 plugin->SetGridOutputDir(kGridOutdir);
840
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());
846
847 if (anaLibs.Length()) plugin->SetAdditionalLibs(anaLibs.Data());
848 if (anaLibsExtra.Length()) plugin->SetAdditionalRootLibs(anaLibsExtra.Data());
849
850 TString ana_sources = "";
851 TString ana_add = "";
852 if (kUsePAR && anaPars.Length()) {
853 printf("%s\n", anaPars.Data());
854 TObjArray *arr;
855 TObjString *objstr;
856 arr = anaPars.Tokenize(" ");
857 TIter next(arr);
858 while ((objstr=(TObjString*)next())) plugin->EnablePackage(objstr->GetString());
859 delete arr;
860 }
861
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.
867
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->SetMergeExcludes(kGridMergeExclude);
873 plugin->SetMaxMergeFiles(kGridMaxMergeFiles);
874 plugin->SetNrunsPerMaster(kGridRunsPerMaster);
875 plugin->SetMergeViaJDL(kPluginMergeViaJDL);
876 // Use fastread option
877 plugin->SetFastReadOption(kPluginFastReadOption);
878 // UseOverwrite mode
879 plugin->SetOverwriteMode(kPluginOverwriteMode);
880 // Optionally define the files to be archived.
881 // plugin->SetOutputArchive("log_archive.zip:stdout,stderr@ALICE::NIHAM::File root_archive.zip:AliAOD.root,AOD.tag.root@ALICE::NIHAM::File");
882 plugin->SetOutputToRunNo(kPluginOutputToRunNumber); // write the output to subdirs named after run number
883// plugin->SetDefaultOutputs(kFALSE);
884
885 // Put default output files to archive
886 TString listhists = "";
887 TString listaods = "";
888 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
889 TIter next(mgr->GetOutputs());
890 AliAnalysisDataContainer *output;
891 while ((output=(AliAnalysisDataContainer*)next())) {
892 const char *filename = output->GetFileName();
893 if (!(strcmp(filename, "default"))) {
894 if (!mgr->GetOutputEventHandler()) continue;
895 filename = mgr->GetOutputEventHandler()->GetOutputFileName();
896 if (listaods.Length()) listaods += " ";
897 listaods += filename;
898 } else {
899 if(!listhists.Contains(filename)){
900 if (listhists.Length()) listhists += " ";
901 listhists += filename;
902 }
903 }
904 }
905
906 if (mgr->GetExtraFiles().Length()) {
907 if (listaods.Length()) listaods += " ";
908 listaods += mgr->GetExtraFiles();
909 }
910
911 TString outputArchive;
912 outputArchive = Form("log_archive.zip:std*r@%s",kGridOutputStorages.Data());
913 listaods.ReplaceAll(" ", ",");
914 listhists.ReplaceAll(" ", ",");
915 if (listhists.Length()) listhists = Form("hist_archive.zip:%s@%s", listhists.Data(), kGridOutputStorages.Data());
916 if (listaods.Length()) listaods = Form("aod_archive.zip:%s@%s", listaods.Data(), kGridOutputStorages.Data());
917
918 if (!listhists.Length() && !listaods.Length()) {
919 ::Fatal("AnalysisTrainHMPID", "No task output !");
920 }
921
922 if (listaods.Length()) {
923 outputArchive += " ";
924 outputArchive += listaods;
925 }
926 if (listhists.Length()) {
927 outputArchive += " ";
928 outputArchive += listhists;
929 }
930// plugin->SetOutputArchive(outputArchive);
931
932// Optionally set a name for the generated analysis macro (default MyAnalysis.C)
933 plugin->SetAnalysisMacro(Form("%s.C", kTrainName.Data()));
934// Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
935 plugin->SetSplitMaxInputFileNumber(kGridFilesPerJob);
936// Optionally set number of failed jobs that will trigger killing waiting sub-jobs.
937// plugin->SetMaxInitFailed(5);
938// Optionally resubmit threshold.
939// plugin->SetMasterResubmitThreshold(90);
940// Optionally set time to live (default 30000 sec)
941 plugin->SetTTL(30000);
942// Optionally set input format (default xml-single)
943 plugin->SetInputFormat("xml-single");
944// Optionally modify the name of the generated JDL (default analysis.jdl)
945 plugin->SetJDLName(Form("%s.jdl", kTrainName.Data()));
946// Optionally modify the executable name (default analysis.sh)
947 plugin->SetExecutable(Form("%s.sh", kTrainName.Data()));
948// Optionally modify job price (default 1)
949 plugin->SetPrice(1);
950// Optionally modify split mode (default 'se')
951 plugin->SetSplitMode("se");
952 plugin->SetCheckCopy(kFALSE);
953 return plugin;
954}
955
956//______________________________________________________________________________
957Bool_t LoadConfig(const char *filename)
958{
959// Read train configuration from file
960 if (gSystem->AccessPathName(filename)) {
961 ::Error("AnalysisTrainHMPID.C::LoadConfig", "Config file name not found");
962 return kFALSE;
963 }
964 gROOT->ProcessLine(Form(".x %s", filename));
965 ::Info("AnalysisTrainHMPID.C::LoadConfig", "Train configuration loaded from file %s", filename);
966 return kTRUE;
967}
968
969//______________________________________________________________________________
970Bool_t PatchJDL(){
971 Printf(">>> Patching JDL");
972 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
973 AliAnalysisAlien* gridHandler = (AliAnalysisAlien*)mgr->GetGridHandler();
974 TGridJDL *jdl = gridHandler->GetGridJDL();
975 if(iJETAN)jdl->AddToPackages("fastjet","v2.4.0");
976 gridHandler->WriteJDL(kFALSE);
977 Printf("<<< Patching JDL");
978 return kTRUE;
979}
980
981//______________________________________________________________________________
982Bool_t PatchAnalysisMacro(){
983 Printf(">>> Patching AnalysisMacro");
984 gSystem->Exec(Form("mv %s.C %s.C_tmp",kTrainName.Data(),kTrainName.Data()));
985
986 ifstream in1;
987 in1.open(Form("%s.C_tmp", kTrainName.Data()));
988 char cLine[250];
989 TString st;
990 while(in1.getline(cLine,250)){
991 st += cLine;
992 st += "\n";
993 }
994 Int_t index= -1;
995 index = st.Index("gSystem->Load(\"libPhysics\");");
996 index += strlen("gSystem->Load(\"libPhysics\");");
997 /*
998 TObjArray *arr;
999 TObjString *objstr;
1000 arr = anaLibs.Tokenize(" ");
1001 TIter next(arr);
1002
1003 add += "\n\n // added by CKB \n";
1004 while ((objstr=(TObjString*)next())){
1005 if(objstr->GetString().Contains("PWG3"))continue;
1006 if(objstr->GetString().EndsWith(".so"))add += Form("gSystem->Load(\"%s\");\n",objstr->GetString().Data());
1007 }
1008 delete arr;
1009 */
1010 // add += Form("AliLog::SetGlobalLogLevel(%d);\n",AliLog::GetGlobalLogLevel());
1011 TString add = "";
1012
1013 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1014 add += "\n\n // added by CKB \n";
1015 add += "\n gSystem->AddIncludePath(\"./\"); \n";
1016 if(gGrid && kPluginAliRootVersion.Length()==0){
1017 add += "\n // Dirty hack for TRD reference data \n";
1018 add += "\n gSystem->Setenv(\"ALICE_ROOT\",\"";
1019 add += Form("alien://%s/rootfiles/",gGrid->GetHomeDirectory());
1020 add += "\"); \n";
1021 }
1022 add += "// BKC \n\n";
1023 st.Insert(index,add.Data());
1024
1025 if(kUseCPAR && kPluginAliRootVersion.Length()==0){
1026 index = st.Index("gSystem->AddIncludePath(\"-I$"); // uncommen $ALICE_ROOT include for par files
1027 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1028 st.Insert(index,"// CKB comment out whehn no aliroot is provided \n //");
1029 }
1030
1031 ofstream out;
1032 out.open(Form("%s.C", kTrainName.Data()));
1033 if (out.bad()) {
1034 return kFALSE;
1035 }
1036 out << st << endl;
1037 Printf("<<< Patching AnalysisMacro");
1038
1039 Printf(">>> Patching Merge Macro");
1040 gSystem->Exec(Form("mv %s_merge.C %s_merge.C_tmp",kTrainName.Data(),kTrainName.Data()));
1041
1042 ifstream in2;
1043 in2.open(Form("%s_merge.C_tmp", kTrainName.Data()));
1044 TString st2;
1045 while(in2.getline(cLine,250)){
1046 st2 += cLine;
1047 st2 += "\n";
1048 }
1049 index = st2.Index("gSystem->Load(\"libPhysics\");");
1050 index += strlen("gSystem->Load(\"libPhysics\");");
1051 TString add2 = "";
1052 add2 += "\n gSystem->AddIncludePath(\"./\"); \n";
1053 if(gGrid&&kPluginAliRootVersion.Length()==0){
1054 add2 += "\n // Dirty hack for TRD reference data \n";
1055 add2 += "\n gSystem->Setenv(\"ALICE_ROOT\",\"";
1056 add2 += Form("alien://%s/rootfiles/",gGrid->GetHomeDirectory());
1057 add2 += "\"); \n";
1058 }
1059 add2 += "// BKC \n\n";
1060 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1061 st2.Insert(index,add.Data());
1062
1063 if(kUseCPAR&&kPluginAliRootVersion.Length()==0){
1064 index = st2.Index("gSystem->AddIncludePath(\"-I$"); // uncommen $ALICE_ROOT include for par files
1065 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1066 st2.Insert(index,"// CKB comment out whehn no aliroot is provided \n //");
1067 }
1068
1069 // do not exclude the extra files from merign, this is done explicitly in this train script
1070 index = st2.Index("mergeExcludes +="); // uncommen $ALICE_ROOT include for par files
1071 if(index<0)Printf("%s:%d index out of bounds",(char*)__FILE__,__LINE__);
1072 st2.Insert(index,"// CKB comment out, handled explicitly by the train macro \n //");
1073
1074
1075 ofstream out2;
1076 out2.open(Form("%s_merge.C", kTrainName.Data()));
1077 if (out2.bad()) {
1078 return kFALSE;
1079 }
1080 out2 << st2 << endl;
1081 Printf("<<< Patching Merging Macro");
1082
1083
1084 return kTRUE;
1085
1086}