]>
Commit | Line | Data |
---|---|---|
a16d922a | 1 | #include <fstream> |
2 | #include <iostream> | |
3 | #include <string> | |
4 | ||
5 | TString g_aliroot_version; | |
6 | TString g_root_version; | |
7 | TString g_sample; | |
8 | TString g_plugin_mode; | |
9 | TString g_train_dir; | |
10 | TArrayI g_runlist; | |
11 | ||
12 | AliAnalysisAlien *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(); | |
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/ANALYSIS/Tender -I$ALICE_ROOT/ANALYSIS/TenderSupplies -I$ALICE_ROOT/PWG/ -I$ALICE_ROOT/PWG/FLOW -I$ALICE_ROOT/PWG/Base -I$ALICE_ROOT/PWG/Tasks"); | |
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"); | |
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 | ||
43 | void 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 | ||
55 | void 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 | ||
72 | bool 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 | ||
80 | bool 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 | ||
95 | bool 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 | ||
108 | void 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 | ||
121 | void 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 | // | |
130 | AddSample(lookup, "LHC13b.pass2", "/alice/data/2013/LHC13b", "*/pass2/*/AliESDs.root", "Data", "ESD"); | |
131 | AddSample(lookup, "LHC13b.pass2.AOD", "/alice/data/2013/LHC13b", "*/pass2/AOD/*/AliAOD.root", "Data", "AOD"); | |
132 | AddSample(lookup, "LHC13b.pass2.AOD126", "/alice/data/2013/LHC13b", "*/pass2/AOD126/*/AliAOD.root", "Data", "AOD"); | |
133 | AddSample(lookup, "LHC13c.pass1", "/alice/data/2013/LHC13c/", "*/pass1/*/AliESDs.root", "Data", "ESD"); | |
134 | AddSample(lookup, "LHC13c.pass1.AOD", "/alice/data/2013/LHC13c/", "*/pass1/AOD/*/AliAOD.root", "Data", "AOD"); | |
135 | AddSample(lookup, "LHC13c.pass1.AOD126", "/alice/data/2013/LHC13c/", "*/pass1/AOD126/*/AliAOD.root", "Data", "AOD"); | |
136 | AddSample(lookup, "LHC13b2", "/alice/sim/2013/LHC13b2", "*/*/AliESDs.root", "MC", "ESD"); | |
137 | AddSample(lookup, "LHC13b2.AOD", "/alice/sim/2013/LHC13b2", "*/AliAOD.root", "MC", "AOD"); | |
138 | AddSample(lookup, "LHC13b2plus", "/alice/sim/2013/LHC13b2_plus", "*/*/AliESDs.root", "MC", "ESD"); | |
139 | AddSample(lookup, "LHC13b2plus.AOD", "/alice/sim/2013/LHC13b2_plus", "*/AliAOD.root", "MC", "AOD"); | |
140 | AddSample(lookup, "LHC13b3", "/alice/sim/2013/LHC13b3", "*/*/AliESDs.root", "MC", "ESD"); | |
141 | AddSample(lookup, "LHC13b3.AOD", "/alice/sim/2013/LHC13b3", "*/AliAOD.root", "MC", "AOD"); | |
142 | printf("Lookup table with sample information generated\n"); | |
143 | } | |
144 | ||
145 | void ConfigParser(const char *configname){ | |
146 | // | |
147 | // Parse configuration | |
148 | // | |
149 | std::ifstream in(configname); | |
150 | std::string config; | |
151 | TString key, value; | |
152 | while(getline(in, config)){ | |
153 | SplitConfigEntry(config, key, value); | |
154 | key.ToLower(); | |
155 | if(!key.CompareTo("aliroot")){ | |
156 | // Aliroot version | |
157 | g_aliroot_version = value; | |
158 | continue; | |
159 | } | |
160 | if(!key.CompareTo("root")){ | |
161 | // root version | |
162 | g_root_version = value; | |
163 | continue; | |
164 | } | |
165 | if(!key.CompareTo("sample")){ | |
166 | // sample name | |
167 | g_sample = value; | |
168 | continue; | |
169 | } | |
170 | if(!key.CompareTo("runlist")){ | |
171 | // Runlist | |
172 | DecodeRunlist(value); | |
173 | continue; | |
174 | } | |
175 | if(!key.CompareTo("mode")){ | |
176 | g_plugin_mode = value; | |
177 | continue; | |
178 | } | |
179 | if(!key.CompareTo("traindir")){ | |
180 | g_train_dir = value; | |
181 | continue; | |
182 | } | |
183 | printf("Unknown key: %s\n", key.Data()); | |
184 | } | |
185 | } | |
186 | ||
187 | bool MakeSample(AliAnalysisAlien *plugin, TMap &lookup){ | |
188 | // | |
189 | // Fill Sample information (Data dir, pattern, run list) to the Alien plugin | |
190 | // | |
191 | TObjArray infos; | |
192 | bool found = FindDataSample(lookup, infos); | |
193 | if(!found){ | |
194 | printf("sample %s not found\n", g_sample.Data()); | |
195 | return false; | |
196 | } | |
197 | TString datadir, pattern, type; | |
198 | GetData(infos, datadir, 0); | |
199 | GetData(infos, pattern, 1); | |
200 | GetData(infos, type, 2); | |
201 | plugin->SetGridDataDir(datadir.Data()); | |
202 | plugin->SetDataPattern(pattern.Data()); | |
203 | if(!IsMC(type)) plugin->SetRunPrefix("000"); | |
204 | // Add runs to the sample | |
205 | for(int irun = 0; irun < g_runlist.GetSize(); irun++){ | |
206 | plugin->AddRunNumber(g_runlist[irun]); | |
207 | } | |
208 | return true; | |
209 | } | |
210 | ||
211 | bool CreateTrainDir(AliAnalysisAlien *plugin, const TMap &lookup){ | |
212 | // | |
213 | // Make train data dir name and JDL, C and sh file names | |
214 | // | |
215 | TObjArray infos; | |
216 | bool found = FindDataSample(lookup, infos); | |
217 | if(!found){ | |
218 | printf("sample %s not found\n", g_sample.Data()); | |
219 | return false; | |
220 | } | |
221 | TString type; GetData(infos, type, 2); | |
222 | ||
223 | // check whether the train dir is already provided or needs to be specified | |
224 | if(!g_train_dir.Length()){ | |
225 | // Query number of train runs before | |
226 | const char *gridhome = gGrid->GetHomeDirectory(); | |
227 | const char gridoutdir[256]; | |
228 | sprintf(gridoutdir, "%sAnalysis_pPb/%s", gridhome, type.Data()); | |
229 | TGridResult *trainruns = gGrid->Ls(gridoutdir); | |
230 | int nruns = trainruns->GetEntries(); | |
231 | // Get Date and time | |
232 | TDatime time; | |
233 | g_train_dir = Form("%d_%d%02d%02d_%02d%02d", nruns, time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute()); | |
234 | } | |
235 | ||
236 | plugin->SetGridWorkingDir(Form("Analysis_pPb/%s/%s", type.Data(), g_train_dir.Data())); | |
237 | plugin->SetJDLName(Form("TPCTOFanalysispPb_%s_%s.jdl", type.Data(), g_train_dir.Data())); | |
238 | plugin->SetExecutable(Form("TPCTOFanalysispPb_%s_%s.sh", type.Data(), g_train_dir.Data())); | |
239 | plugin->SetAnalysisMacro(Form("TPCTOFanalysispPb_%s_%s.C", type.Data(), g_train_dir.Data())); | |
240 | return true; | |
241 | } | |
242 | ||
243 | void SetupUtil(bool IsMC, bool isAOD){ | |
244 | // | |
245 | // Setup utility packages | |
246 | // | |
247 | // 1. Physics Selection (ESD only) | |
248 | // 2. Tender (ESD only) | |
249 | // 3. PID Response (always) | |
250 | // 4. Centrality Task (ESD only) | |
251 | // | |
252 | ||
253 | //==== Physics Selection ==== | |
254 | if(!isAOD){ | |
255 | gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C"); | |
256 | AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection(isMC); | |
257 | } | |
258 | ||
259 | //==== Add tender ==== | |
260 | if(!isAOD){ | |
261 | gROOT->LoadMacro("AddTaskTender.C"); | |
262 | AddTaskTender(); | |
263 | } | |
264 | ||
265 | //===== ADD PID RESPONSE: === | |
266 | gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C"); | |
267 | AddTaskPIDResponse(); | |
268 | ||
269 | //===== ADD CENTRALITY: === | |
270 | if(!isAOD){ | |
271 | gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C"); | |
272 | AddTaskCentrality(); | |
273 | } | |
274 | } | |
275 | ||
276 | void SetupHandlers(bool isMC, bool isAOD){ | |
277 | // | |
278 | // Setup Handlers | |
279 | // | |
280 | TString macrobase = "$ALICE_ROOT/ANALYSIS/macros/train/"; | |
281 | TString macroname = macrobase; | |
282 | if(isAOD) | |
283 | macroname += "AddAODHandler.C"; | |
284 | else | |
285 | macroname += "AddESDHandler.C"; | |
286 | gROOT->Macro(macroname.Data()); | |
287 | ||
288 | if(isMC){ | |
289 | // Add MC truth event handler | |
290 | gROOT->LoadMacro(Form("%s/AddMCHandler.C", macrobase.Data())); | |
291 | AddMCHandler(); | |
292 | } | |
293 | } | |
294 | ||
295 | void SetupHFEtask(bool isMC, bool isAOD){ | |
296 | gROOT->LoadMacro("$ALICE_ROOT/PWGHF/hfe/macros/AddTaskHFEpPb.C"); | |
297 | AddTaskHFEpPb(isAOD); | |
298 | if(!isAOD){ | |
299 | gROOT->LoadMacro("$ALICE_ROOT/PWGHF/hfe/macros/AddTaskHFEnpepPb.C"); | |
300 | AddTaskHFEnpepPb(); | |
301 | } | |
302 | } | |
303 | ||
304 | void SetupTrain(const TMap &lookup){ | |
305 | // | |
306 | // Setup train: | |
307 | // Determine whether the trains run on MC or Data | |
308 | // and ESDs or AODs and Configure Handlers, utils | |
309 | // and HFE task according to this | |
310 | // | |
311 | bool isMC(false), isAOD(false); | |
312 | TObjArray infos; | |
313 | bool found = FindDataSample(lookup, infos); | |
314 | if(!found) return; | |
315 | TString type, mode; | |
316 | GetData(infos, type, 2); | |
317 | GetData(infos, mode, 3); | |
318 | isMC = IsMC(type); | |
319 | if(!mode.CompareTo("AOD")) isAOD = true; | |
320 | ||
321 | SetupHandlers(isMC, isAOD); | |
322 | SetupUtil(isMC, isAOD); | |
323 | SetupHFEtask(isMC, isAOD); | |
324 | } | |
325 | ||
326 | void GenerateMergeConfigs(){ | |
327 | // | |
328 | // Generate configurations for merging | |
329 | // (MergeViaJDL and Terminate) | |
330 | // | |
331 | ||
332 | // Write config for MergeViaJDL | |
333 | std::ofstream outMerge("configMerge.txt"); | |
334 | outMerge << "aliroot " << g_aliroot_version.Data() << std::endl; | |
335 | outMerge << "root " << g_root_version.Data() << std::endl; | |
336 | outMerge << "sample " << g_sample.Data() << std::endl; | |
337 | outMerge << "mode MergeViaJDL\n"; | |
338 | outMerge << "traindir " << g_train_dir.Data() << std::endl; | |
339 | outMerge << "runlist "; | |
340 | for(int irun = 0; irun < g_runlist.GetSize()-1; irun++) outMerge << g_runlist[irun] << ","; | |
341 | outMerge << g_runlist[g_runlist.GetSize()-1] << std::endl; | |
342 | outMerge.close(); | |
343 | // Write config for Terminate | |
344 | std::ofstream outTerminate("configTerminate.txt"); | |
345 | outMerge << "aliroot " << g_aliroot_version.Data() << std::endl; | |
346 | outMerge << "root " << g_root_version.Data() << std::endl; | |
347 | outMerge << "sample " << g_sample.Data() << std::endl; | |
348 | outMerge << "mode Terminate\n"; | |
349 | outMerge << "traindir " << g_train_dir.Data() << std::endl; | |
350 | outMerge << "runlist "; | |
351 | for(int irun = 0; irun < g_runlist.GetSize()-1; irun++) outMerge << g_runlist[irun] << ","; | |
352 | outMerge << g_runlist[g_runlist.GetSize()-1] << std::endl; | |
353 | outMerge.close(); | |
354 | ||
355 | printf("Configurations for MergeViaJDL and terminate generated\n"); | |
356 | } | |
357 | ||
358 | void runGridpPb(const char *config = "config.txt"){ | |
359 | // | |
360 | // run analysis | |
361 | // | |
362 | ||
363 | TGrid::Connect("alien://"); | |
364 | ||
365 | // Create Lookup with sample information | |
366 | TMap sampleinfos; | |
367 | Generate_Sample_Lookup(sampleinfos); | |
368 | ||
369 | ConfigParser(config); | |
370 | ||
371 | // Configure alien plugin | |
372 | AliAnalysisAlien *plugin = CreateGridHandler(); | |
373 | if(!CreateTrainDir(plugin, sampleinfos)){ | |
374 | printf("Cannot setup output directory\n"); | |
375 | return; | |
376 | } | |
377 | if(!MakeSample(plugin, sampleinfos)){ | |
378 | printf("Cannot create data sample\n"); | |
379 | return; | |
380 | } | |
381 | if(!g_plugin_mode.CompareTo("full")){ | |
382 | // full mode, creating config files for the merging stage | |
383 | GenerateMergeConfigs(); | |
384 | } | |
385 | ||
386 | AliAnalysisManager *mgr = new AliAnalysisManager("tpctofanalysis"); | |
387 | mgr->SetGridHandler(plugin); | |
388 | ||
389 | SetupTrain(sampleinfos); | |
390 | ||
391 | // Run train | |
392 | if (!mgr->InitAnalysis()) return; | |
393 | mgr->PrintStatus(); | |
394 | // Start analysis in grid. | |
395 | mgr->StartAnalysis("grid"); | |
396 | } | |
397 |