Split: fix refs to AddTaskPhysicsSelection.C
[u/mrichter/AliRoot.git] / PWGHF / hfe / macros / test / runGridpPb.C
CommitLineData
e7e590df 1#include <fstream>
2#include <iostream>
3#include <string>
4
5TString g_aliroot_version;
6TString g_root_version;
7TString g_sample;
8TString g_plugin_mode;
9TString g_train_dir;
10TArrayI g_runlist;
11
12AliAnalysisAlien *CreateGridHandler(){
13 //
14 // Setup main settings of the Alien plugin
15 //
16 AliAnalysisAlien *plugin = new AliAnalysisAlien();
17 plugin->SetRunMode(g_plugin_mode.Data());
18 if(!g_plugin_mode.CompareTo("Terminate"))
19 plugin->SetMergeViaJDL(kFALSE);
20 else
21 plugin->SetMergeViaJDL(kTRUE);
22 plugin->SetOverwriteMode();
23 plugin->SetNtestFiles(1);
24
25 plugin->SetAPIVersion("V1.1x");
26 plugin->SetROOTVersion(g_root_version.Data());
27 plugin->SetAliROOTVersion(g_aliroot_version.Data());
28
29 plugin->SetOutputToRunNo();
6a637a06 30 plugin->AddIncludePath("-I. .I$ALIEN_ROOT/api/lib -I$ROOTSYS/lib -I$ROOTSYS/include -I$ALICE_ROOT/include -I$ALICE_ROOT/PWGHF/ -I$ALICE_ROOT/PWGHF/hfe/macros -I$ALICE_ROOT/PWGHF/hfe -I$ALICE_ROOT/ANALYSIS -I$ALICE_ROOT/TENDER/Tender -I$ALICE_ROOT/TENDER/TenderSupplies -I$ALICE_ROOT/PWG/ -I$ALICE_ROOT/PWG/FLOW -I$ALICE_ROOT/PWG/Base -I$ALICE_ROOT/PWG/Tasks");
af472fff 31 plugin->SetAdditionalLibs("libGui.so libXMLParser.so libSTEERBase.so libESD.so libAOD.so libCDB.so libANALYSIS.so libANALYSISalice.so libCORRFW.so libPWGflowBase.so libPWGflowTasks.so libPWGHFhfe.so libTender.so libProof.so libRAWDatabase.so libSTEER.so libTOFbase.so libPWGTRD.so");
e7e590df 32
33 plugin->SetDefaultOutputs(kFALSE);
34 plugin->SetOutputFiles("AnalysisResults.root");
35 plugin->SetExecutableCommand("aliroot -b -q");
36 plugin->SetTTL(30000);
37 plugin->SetInputFormat("xml-single");
38 plugin->SetPrice(1);
39 plugin->SetSplitMode("se");
40 return plugin;
41}
42
43void SplitConfigEntry(const std::string &input, TString &key, TString &value){
44 //
45 // Decode key and value of a config entry
46 //
47 std::istringstream stream(input, istringstream::in);
48 std::string tmp;
49 stream >> tmp;
50 key = tmp.c_str();
51 stream >> tmp;
52 value = tmp.c_str();
53}
54
55void DecodeRunlist(const TString &val){
56 //
57 // Tokenize run list
58 //
59 TObjArray *runstrings = val.Tokenize(",");
60 TObjString *os;
61 TString runstr;
62 TIter runIter(runstrings);
63 g_runlist.Set(runstrings->GetEntries());
64 int nruns(0);
65 while((os = dynamic_cast<TObjString *>(runIter()))){
66 runstr = os->String();
67 g_runlist[nruns++] = runstr.Atoi();
68 }
69 delete runstrings;
70}
71
72bool IsMC(const TString &val){
73 //
74 // Determine whether sample is MC or Data
75 //
76 if(!val.CompareTo("MC")) return true;
77 return false;
78}
79
80bool FindDataSample(const TMap &lookup, TObjArray &sampleinfis){
81 //
82 // Find Data sample in the list of samples
83 //
84 TObjArray *entry = dynamic_cast<TObjArray *>(lookup.GetValue(g_sample.Data()));
85 if(!entry){
86 printf("Sample %s not found in the list of samples", g_sample.Data());
87 return false;
88 }
89 // Copy to output container
90 sampleinfis.SetOwner(kFALSE);
91 for(int ival = 0; ival < 4; ival++) sampleinfis.AddAt(entry->At(ival), ival);
92 return true;
93}
94
95bool GetData(TObjArray &in, TString &out, int pos){
96 //
97 // Helper function reading data string
98 //
99 TObjString *entry = dynamic_cast<TObjString *>(in.At(pos));
100 if(!entry){
101 printf("Entry at pos %d not a string\n", pos);
102 return false;
103 }
104 out = entry->String();
105 return true;
106}
107
108void AddSample(TMap &lookup,
109 const char *key, const char* datadir, const char * pattern, const char *sampletype, const char *dataformat){
110 //
111 // Add sample entry to the lookup table
112 //
113 TObjArray *infos = new TObjArray();
114 infos->AddAt(new TObjString(datadir), 0);
115 infos->AddAt(new TObjString(pattern), 1);
116 infos->AddAt(new TObjString(sampletype), 2);
117 infos->AddAt(new TObjString(dataformat), 3);
118 lookup.Add(new TObjString(key), infos);
119}
120
121void Generate_Sample_Lookup(TMap &lookup){
122 //
123 // Create Lookup table for each period
124 // Vector contains
125 // - path
126 // - pattern
127 // - MC/Data
128 // - ESD/AOD
129 //
4ec80f5d 130 AddSample(lookup, "LHC13b.pass2", "/alice/data/2013/LHC13b", "pass2/*/AliESDs.root", "Data", "ESD");
131 AddSample(lookup, "LHC13b.pass3", "/alice/data/2013/LHC13b", "pass3/*/AliESDs.root", "Data", "ESD");
132 AddSample(lookup, "LHC13b.pass2.AOD", "/alice/data/2013/LHC13b", "pass2/AOD/*/AliAOD.root", "Data", "AOD");
0b7c6a7b 133 AddSample(lookup, "LHC13b.pass3.AOD", "/alice/data/2013/LHC13b", "pass3/AOD/*/AliAOD.root", "Data", "AOD");
e7e590df 134 AddSample(lookup, "LHC13b.pass2.AOD126", "/alice/data/2013/LHC13b", "*/pass2/AOD126/*/AliAOD.root", "Data", "AOD");
4ec80f5d 135 AddSample(lookup, "LHC13c.pass1", "/alice/data/2013/LHC13c/", "pass1/*/AliESDs.root", "Data", "ESD");
136 AddSample(lookup, "LHC13c.pass2", "/alice/data/2013/LHC13c/", "pass2/*/AliESDs.root", "Data", "ESD");
e7e590df 137 AddSample(lookup, "LHC13c.pass1.AOD", "/alice/data/2013/LHC13c/", "*/pass1/AOD/*/AliAOD.root", "Data", "AOD");
0b7c6a7b 138 AddSample(lookup, "LHC13c.pass2.AOD", "/alice/data/2013/LHC13c/", "*/pass2/AOD/*/AliAOD.root", "Data", "AOD");
e7e590df 139 AddSample(lookup, "LHC13c.pass1.AOD126", "/alice/data/2013/LHC13c/", "*/pass1/AOD126/*/AliAOD.root", "Data", "AOD");
140 AddSample(lookup, "LHC13b2", "/alice/sim/2013/LHC13b2", "*/*/AliESDs.root", "MC", "ESD");
141 AddSample(lookup, "LHC13b2.AOD", "/alice/sim/2013/LHC13b2", "*/AliAOD.root", "MC", "AOD");
142 AddSample(lookup, "LHC13b2plus", "/alice/sim/2013/LHC13b2_plus", "*/*/AliESDs.root", "MC", "ESD");
143 AddSample(lookup, "LHC13b2plus.AOD", "/alice/sim/2013/LHC13b2_plus", "*/AliAOD.root", "MC", "AOD");
144 AddSample(lookup, "LHC13b3", "/alice/sim/2013/LHC13b3", "*/*/AliESDs.root", "MC", "ESD");
4ec80f5d 145 AddSample(lookup, "LHC13b3.AOD", "/alice/sim/2013/LHC13b3", "*/AliAOD.root", "MC", "AOD");
146 AddSample(lookup, "LHC13d.pass1", "/alice/data/2013/LHC13d/", "pass1/*/AliESDs.root", "Data", "ESD");
147 AddSample(lookup, "LHC13e.pass1", "/alice/data/2013/LHC13e/", "pass1/*/AliESDs.root", "Data", "ESD");
148 AddSample(lookup, "LHC13f.pass1", "/alice/data/2013/LHC13f/", "pass1/*/AliESDs.root", "Data", "ESD");
e7e590df 149 printf("Lookup table with sample information generated\n");
150}
151
152void ConfigParser(const char *configname){
153 //
154 // Parse configuration
155 //
156 std::ifstream in(configname);
157 std::string config;
158 TString key, value;
159 while(getline(in, config)){
160 SplitConfigEntry(config, key, value);
161 key.ToLower();
162 if(!key.CompareTo("aliroot")){
163 // Aliroot version
164 g_aliroot_version = value;
165 continue;
166 }
167 if(!key.CompareTo("root")){
168 // root version
169 g_root_version = value;
170 continue;
171 }
172 if(!key.CompareTo("sample")){
173 // sample name
174 g_sample = value;
175 continue;
176 }
177 if(!key.CompareTo("runlist")){
178 // Runlist
179 DecodeRunlist(value);
180 continue;
181 }
182 if(!key.CompareTo("mode")){
183 g_plugin_mode = value;
184 continue;
185 }
186 if(!key.CompareTo("traindir")){
187 g_train_dir = value;
188 continue;
189 }
190 printf("Unknown key: %s\n", key.Data());
191 }
192}
193
194bool MakeSample(AliAnalysisAlien *plugin, TMap &lookup){
195 //
196 // Fill Sample information (Data dir, pattern, run list) to the Alien plugin
197 //
198 TObjArray infos;
199 bool found = FindDataSample(lookup, infos);
200 if(!found){
201 printf("sample %s not found\n", g_sample.Data());
202 return false;
203 }
204 TString datadir, pattern, type;
205 GetData(infos, datadir, 0);
206 GetData(infos, pattern, 1);
207 GetData(infos, type, 2);
208 plugin->SetGridDataDir(datadir.Data());
209 plugin->SetDataPattern(pattern.Data());
210 if(!IsMC(type)) plugin->SetRunPrefix("000");
211 // Add runs to the sample
212 for(int irun = 0; irun < g_runlist.GetSize(); irun++){
213 plugin->AddRunNumber(g_runlist[irun]);
214 }
215 return true;
216}
217
218bool CreateTrainDir(AliAnalysisAlien *plugin, const TMap &lookup){
219 //
220 // Make train data dir name and JDL, C and sh file names
221 //
222 TObjArray infos;
223 bool found = FindDataSample(lookup, infos);
224 if(!found){
225 printf("sample %s not found\n", g_sample.Data());
226 return false;
227 }
228 TString type; GetData(infos, type, 2);
229
230 // check whether the train dir is already provided or needs to be specified
231 if(!g_train_dir.Length()){
232 // Query number of train runs before
233 const char *gridhome = gGrid->GetHomeDirectory();
234 const char gridoutdir[256];
235 sprintf(gridoutdir, "%sAnalysis_pPb/%s", gridhome, type.Data());
236 TGridResult *trainruns = gGrid->Ls(gridoutdir);
237 int nruns = trainruns->GetEntries();
238 // Get Date and time
239 TDatime time;
240 g_train_dir = Form("%d_%d%02d%02d_%02d%02d", nruns, time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute());
241 }
242
243 plugin->SetGridWorkingDir(Form("Analysis_pPb/%s/%s", type.Data(), g_train_dir.Data()));
244 plugin->SetJDLName(Form("TPCTOFanalysispPb_%s_%s.jdl", type.Data(), g_train_dir.Data()));
245 plugin->SetExecutable(Form("TPCTOFanalysispPb_%s_%s.sh", type.Data(), g_train_dir.Data()));
246 plugin->SetAnalysisMacro(Form("TPCTOFanalysispPb_%s_%s.C", type.Data(), g_train_dir.Data()));
247 return true;
248}
249
250void SetupUtil(bool isMC, bool isAOD){
251 //
252 // Setup utility packages
253 //
254 // 1. Physics Selection (ESD only)
255 // 2. Tender (ESD only)
256 // 3. PID Response (always)
257 // 4. Centrality Task (ESD only)
258 //
259
4ec80f5d 260 //===== ADD PID RESPONSE: ===
261 gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
262 AddTaskPIDResponse(isMC);
e7e590df 263
264 //==== Add tender ====
265 if(!isAOD){
6a637a06 266 gROOT->LoadMacro("$ALICE_ROOT/TENDER/TenderSupplies/AddTaskTender.C");
4ec80f5d 267 //AddTaskTender();
e7e590df 268 }
269
4ec80f5d 270 //==== Physics Selection ====
271 if(!isAOD){
63b6cbd0 272 gROOT->LoadMacro("$ALICE_ROOT/OADB/macros/AddTaskPhysicsSelection.C");
4ec80f5d 273 AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection(isMC);
274 }
e7e590df 275
276 //===== ADD CENTRALITY: ===
277 if(!isAOD){
1c06a186 278 gROOT->LoadMacro("$ALICE_ROOT/OADB/macros/AddTaskCentrality.C");
e7e590df 279 AddTaskCentrality();
280 }
281}
282
283void SetupHandlers(bool isMC, bool isAOD){
284 //
285 // Setup Handlers
286 //
287 TString macrobase = "$ALICE_ROOT/ANALYSIS/macros/train/";
288 TString macroname = macrobase;
289 if(isAOD)
290 macroname += "AddAODHandler.C";
291 else
292 macroname += "AddESDHandler.C";
293 gROOT->Macro(macroname.Data());
294
295 if(isMC && !isAOD){
296 // Add MC truth event handler, only in case of ESDs
297 gROOT->LoadMacro(Form("%s/AddMCHandler.C", macrobase.Data()));
298 AddMCHandler();
299 }
300}
301
302void SetupHFEtask(bool isMC, bool isAOD){
303 gROOT->LoadMacro("$ALICE_ROOT/PWGHF/hfe/macros/AddTaskHFEpPb.C");
304 AddTaskHFEpPb(isMC, isAOD);
0b7c6a7b 305 gROOT->LoadMacro("$ALICE_ROOT/PWGHF/hfe/macros/AddTaskHFEnpepPb.C");
306 AddTaskHFEnpepPb(isMC, isAOD);
e7e590df 307}
308
309void SetupTrain(const TMap &lookup){
310 //
311 // Setup train:
312 // Determine whether the trains run on MC or Data
313 // and ESDs or AODs and Configure Handlers, utils
314 // and HFE task according to this
315 //
316 bool isMC(false), isAOD(false);
317 TObjArray infos;
318 bool found = FindDataSample(lookup, infos);
319 if(!found) return;
320 TString type, mode;
321 GetData(infos, type, 2);
322 GetData(infos, mode, 3);
323 isMC = IsMC(type);
324 if(!mode.CompareTo("AOD")) isAOD = true;
325
326 SetupHandlers(isMC, isAOD);
327 SetupUtil(isMC, isAOD);
328 SetupHFEtask(isMC, isAOD);
329}
330
331void GenerateMergeConfigs(){
332 //
333 // Generate configurations for merging
334 // (MergeViaJDL and Terminate)
335 //
336
337 // Write config for MergeViaJDL
338 std::ofstream outMerge("configMerge.txt");
339 outMerge << "aliroot " << g_aliroot_version.Data() << std::endl;
340 outMerge << "root " << g_root_version.Data() << std::endl;
341 outMerge << "sample " << g_sample.Data() << std::endl;
342 outMerge << "mode MergeViaJDL\n";
343 outMerge << "traindir " << g_train_dir.Data() << std::endl;
344 outMerge << "runlist ";
345 for(int irun = 0; irun < g_runlist.GetSize()-1; irun++) outMerge << g_runlist[irun] << ",";
346 outMerge << g_runlist[g_runlist.GetSize()-1] << std::endl;
347 outMerge.close();
348 // Write config for Terminate
349 std::ofstream outTerminate("configTerminate.txt");
350 outTerminate << "aliroot " << g_aliroot_version.Data() << std::endl;
351 outTerminate << "root " << g_root_version.Data() << std::endl;
352 outTerminate << "sample " << g_sample.Data() << std::endl;
353 outTerminate << "mode Terminate\n";
354 outTerminate << "traindir " << g_train_dir.Data() << std::endl;
355 outTerminate << "runlist ";
356 for(int irun = 0; irun < g_runlist.GetSize()-1; irun++) outTerminate << g_runlist[irun] << ",";
357 outTerminate << g_runlist[g_runlist.GetSize()-1] << std::endl;
358 outTerminate.close();
359
360 printf("Configurations for MergeViaJDL and terminate generated\n");
361}
362
363void runGridpPb(const char *config = "config.txt"){
364 //
365 // run analysis
366 //
367
368 TGrid::Connect("alien://");
369
370 // Create Lookup with sample information
371 TMap sampleinfos;
372 Generate_Sample_Lookup(sampleinfos);
373
374 ConfigParser(config);
375
376 // Configure alien plugin
377 AliAnalysisAlien *plugin = CreateGridHandler();
378 if(!CreateTrainDir(plugin, sampleinfos)){
379 printf("Cannot setup output directory\n");
380 return;
381 }
382 if(!MakeSample(plugin, sampleinfos)){
383 printf("Cannot create data sample\n");
384 return;
385 }
386 if(!g_plugin_mode.CompareTo("full")){
387 // full mode, creating config files for the merging stage
388 GenerateMergeConfigs();
389 }
390
391 AliAnalysisManager *mgr = new AliAnalysisManager("tpctofanalysis");
392 mgr->SetGridHandler(plugin);
393
394 SetupTrain(sampleinfos);
395
396 // Run train
397 if (!mgr->InitAnalysis()) return;
398 mgr->PrintStatus();
399 // Start analysis in grid.
400 mgr->StartAnalysis("grid");
401}
402