Removing dummy if
[u/mrichter/AliRoot.git] / PWG0 / multiplicity / run.C
1 void run(Char_t* data, Long64_t nRuns = -1, Long64_t offset = 0, Bool_t aDebug = kFALSE, Int_t aProof = 0, Int_t requiredData = 1, const char* option = "",Int_t workers = -1)
2 {
3   // aProof option: 
4   //               -2 grid, terminate mode
5   //               -1 grid, full mode
6   //                0 local
7   //                1 proof with chain
8   //                2 proof with dataset
9   //                3 local with proof dataset 
10   //
11   // requiredData option: 0 = only ESD
12   //                      1 = ESD+MC
13   //                      2 = RAW (ESD+check on event type)
14   //
15
16   if (nRuns < 0)
17     nRuns = 1234567890;
18
19   if (aProof > 0)
20   {
21     gEnv->SetValue("XSec.GSI.DelegProxy", "2");
22     TProof::Open("alice-caf.cern.ch", workers>0 ? Form("workers=%d",workers) : "");
23     
24     // Enable the needed package
25     if (1)
26     {
27       gProof->UploadPackage("$ALICE_ROOT/STEERBase");
28       gProof->EnablePackage("$ALICE_ROOT/STEERBase");
29       gProof->UploadPackage("$ALICE_ROOT/ESD");
30       gProof->EnablePackage("$ALICE_ROOT/ESD");
31       gProof->UploadPackage("$ALICE_ROOT/AOD");
32       gProof->EnablePackage("$ALICE_ROOT/AOD");
33       gProof->UploadPackage("$ALICE_ROOT/ANALYSIS");
34       gProof->EnablePackage("$ALICE_ROOT/ANALYSIS");
35       gProof->UploadPackage("$ALICE_ROOT/ANALYSISalice");
36       gProof->EnablePackage("$ALICE_ROOT/ANALYSISalice");
37     }
38     else
39     {
40       gProof->UploadPackage("$ALICE_ROOT/AF-v4-16");
41       gProof->EnablePackage("$ALICE_ROOT/AF-v4-16");
42     }
43
44     gProof->UploadPackage("$ALICE_ROOT/PWG0base");
45     gProof->EnablePackage("$ALICE_ROOT/PWG0base");
46   }
47   else
48   {
49     gSystem->Load("libVMC");
50     gSystem->Load("libTree");
51     gSystem->Load("libSTEERBase");
52     gSystem->Load("libESD");
53     gSystem->Load("libAOD");
54     gSystem->Load("libANALYSIS");
55     gSystem->Load("libANALYSISalice");
56     gSystem->Load("libPWG0base");
57     
58     gROOT->ProcessLine(gSystem->ExpandPathName(".include $ALICE_ROOT/PWG0"));
59   }
60
61   // Create the analysis manager
62   mgr = new AliAnalysisManager;
63
64   // Add ESD handler
65   AliESDInputHandler* esdH = new AliESDInputHandler;
66   esdH->SetReadFriends(kFALSE);
67   //esdH->SetInactiveBranches("AliESDACORDE FMD ALIESDTZERO ALIESDZDC AliRawDataErrorLogs CaloClusters Cascades EMCALCells EMCALTrigger ESDfriend Kinks AliESDTZERO ALIESDACORDE MuonTracks TrdTracks");
68   mgr->SetInputEventHandler(esdH);
69
70   // physics selection
71   gROOT->ProcessLine(".L $ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
72   physicsSelectionTask = AddTaskPhysicsSelection((requiredData == 2) ? kFALSE : kTRUE);
73
74   // FO efficiency (for MC)
75   if (0 && requiredData != 2)
76   {
77     //const char* fastORFile = "../dNdEta/spdFOEff_run104824_52.root";
78     //const char* fastORFile = "../dNdEta/spdFOEff_run104867_92.root";
79     //const char* fastORFile = "../dNdEta/spdFOEff_run105054_7.root";
80     const char* fastORFile = "../dNdEta/spdFOEff_run114931.root";
81   
82     Printf("NOTE: Simulating FAST-OR efficiency on the analysis level using file %s", fastORFile);
83     TFile::Open(fastORFile);
84     
85     spdFOEff = (TH1F*) gFile->Get("spdFOEff");
86     physicsSelectionTask->GetPhysicsSelection()->Initialize(114931);
87     physicsSelectionTask->GetPhysicsSelection()->GetTriggerAnalysis()->SetSPDGFOEfficiency(spdFOEff);
88   }
89   
90   //AliPWG0Helper::AnalysisMode analysisMode = AliPWG0Helper::kSPD | AliPWG0Helper::kFieldOn;
91   AliPWG0Helper::AnalysisMode analysisMode = AliPWG0Helper::kTPCSPD | AliPWG0Helper::kFieldOn;
92   //AliPWG0Helper::AnalysisMode analysisMode = AliPWG0Helper::kTPCITS | AliPWG0Helper::kFieldOn;
93   
94   AliTriggerAnalysis::Trigger trigger      = AliTriggerAnalysis::kAcceptAll | AliTriggerAnalysis::kOfflineFlag;
95   //AliTriggerAnalysis::Trigger trigger      = AliTriggerAnalysis::kAcceptAll | AliTriggerAnalysis::kOfflineFlag | AliTriggerAnalysis::kOneParticle; 
96   
97   //AliTriggerAnalysis::Trigger trigger      = AliTriggerAnalysis::kMB1Prime | AliTriggerAnalysis::kOfflineFlag;
98   //AliTriggerAnalysis::Trigger trigger      = AliTriggerAnalysis::kSPDGFOBits | AliTriggerAnalysis::kOfflineFlag;
99   //AliTriggerAnalysis::Trigger trigger      = AliTriggerAnalysis::kV0AND | AliTriggerAnalysis::kOfflineFlag; 
100
101   AliPWG0Helper::DiffTreatment diffTreatment = AliPWG0Helper::kMCFlags;
102   //AliPWG0Helper::DiffTreatment diffTreatment = AliPWG0Helper::kE710Cuts;
103   
104   AliPWG0Helper::PrintConf(analysisMode, trigger, diffTreatment);
105
106   TString taskName("AliMultiplicityTask.cxx+");
107   if (aDebug)
108     taskName += "+g";
109
110   // Create, add task
111   if (aProof > 0) {
112     gProof->Load(taskName);
113   } else
114     gROOT->Macro(taskName);
115
116   // 0 bin calculation
117   if (0)
118   {
119   }
120   
121   // V0 syst. study
122   if (0)
123   {
124     Printf("NOTE: Systematic study for VZERO enabled!");
125     //physicsSelectionTask->GetPhysicsSelection()->Initialize(104867);
126     for (Int_t i=0; i<1; i++)
127     {
128       // for MC and data
129       physicsSelectionTask->GetPhysicsSelection()->GetTriggerAnalysis(i)->SetV0HwPars(15, 61.5, 86.5);
130       physicsSelectionTask->GetPhysicsSelection()->GetTriggerAnalysis(i)->SetV0AdcThr(15);
131       // only for MC
132       //physicsSelectionTask->GetPhysicsSelection()->GetTriggerAnalysis(i)->SetV0HwPars(0, 0, 125);
133       //physicsSelectionTask->GetPhysicsSelection()->GetTriggerAnalysis(i)->SetV0AdcThr(0);
134     }
135   }
136
137   TString optionStr(option);
138   
139   // remove SAVE option if set
140   Bool_t save = kFALSE;
141   TString optionStr(option);
142   if (optionStr.Contains("SAVE"))
143   {
144     optionStr = optionStr(0,optionStr.Index("SAVE")) + optionStr(optionStr.Index("SAVE")+4, optionStr.Length());
145     save = kTRUE;
146   }
147   
148   task = new AliMultiplicityTask(optionStr);
149
150   if (!(analysisMode & AliPWG0Helper::kSPD))
151   {
152     // selection of esd tracks
153     gROOT->ProcessLine(".L $ALICE_ROOT/PWG0/CreateStandardCuts.C");
154     AliESDtrackCuts* esdTrackCuts = CreateTrackCuts(analysisMode);
155     if (!esdTrackCuts)
156     {
157       printf("ERROR: esdTrackCuts could not be created\n");
158       return;
159     }
160
161     task->SetTrackCuts(esdTrackCuts);
162   }
163   //else
164   //  task->SetDeltaPhiCut(0.05);
165
166   task->SetAnalysisMode(analysisMode);
167   task->SetTrigger(trigger);
168   task->SetDiffTreatment(diffTreatment);
169
170   if (requiredData == 1)
171     task->SetReadMC();
172
173   //task->SetUseMCVertex();
174   
175   //if (requiredData != 2)
176   //  task->SetSkipParticles();
177
178   mgr->AddTask(task);
179
180   if (requiredData == 1) {
181     // Enable MC event handler
182     AliMCEventHandler* handler = new AliMCEventHandler;
183     if (!optionStr.Contains("particle-efficiency"))
184       handler->SetReadTR(kFALSE);
185     mgr->SetMCtruthEventHandler(handler);
186   }
187
188   // pt study
189   if (optionStr.Contains("pt-spectrum-func"))
190   {
191     TF1* func = new TF1("func", "1", 0, 0.2);
192     //TF1* func = new TF1("func", "1.5 - x / 0.2 * 0.5", 0, 0.2);
193     //TF1* func = new TF1("func", "1.25 - x / 0.2 * 0.25", 0, 0.2);
194     //TF1* func = new TF1("func", "0.75 + x / 0.2 * 0.25", 0, 0.2);
195     hist = func->GetHistogram();
196     //new TCanvas; func->Draw();
197     //inputList.Add(func->GetHistogram()->Clone("pt-spectrum"));
198
199     new TCanvas; hist->Draw();
200     task->SetPtSpectrum((TH1D*) hist->Clone("pt-spectrum"));
201   }
202
203   // Attach input
204   cInput  = mgr->GetCommonInputContainer();
205   mgr->ConnectInput(task, 0, cInput);
206
207   // Attach output
208   cOutput = mgr->CreateContainer("cOutput", TList::Class(), AliAnalysisManager::kOutputContainer, "mult_Temp.root");
209   mgr->ConnectOutput(task, 0, cOutput);
210
211   // Enable debug printouts
212   if (aDebug)
213     mgr->SetDebugLevel(2);
214
215   // Run analysis
216   mgr->InitAnalysis();
217   mgr->PrintStatus();
218
219   if (aProof == 2)
220   {
221     // process dataset
222
223     mgr->StartAnalysis("proof", data, nRuns, offset);
224   
225     if (save)
226     {
227       TString path("maps/");
228       path += TString(data).Tokenize("/")->Last()->GetName();
229       
230       UInt_t triggerNoFlags = (UInt_t) trigger % (UInt_t) AliTriggerAnalysis::kStartOfFlags;
231       switch (triggerNoFlags)
232       {
233         case AliTriggerAnalysis::kAcceptAll: path += "/all"; break;
234         case AliTriggerAnalysis::kMB1: path += "/mb1"; break;
235         case AliTriggerAnalysis::kMB2: path += "/mb2"; break;
236         case AliTriggerAnalysis::kMB3: path += "/mb3"; break;
237         case AliTriggerAnalysis::kSPDGFO: path += "/spdgfo"; break;
238         case AliTriggerAnalysis::kSPDGFOBits: path += "/spdgfobits"; break;
239         case AliTriggerAnalysis::kV0AND: path += "/v0and"; break;
240         case AliTriggerAnalysis::kNSD1: path += "/nsd1"; break;
241         case AliTriggerAnalysis::kMB1Prime: path += "/mb1prime"; break;
242         default: Printf("ERROR: Trigger undefined for path to files"); return;
243       }
244       
245       if (trigger & AliTriggerAnalysis::kOneParticle)
246         path += "-onepart";
247       
248       if (analysisMode & AliPWG0Helper::kSPD)
249         path += "/spd";
250
251       if (analysisMode & AliPWG0Helper::kTPCSPD)
252         path += "/tpcspd";
253       
254       if (analysisMode & AliPWG0Helper::kTPC)
255         path += "/tpc";
256         
257       if (analysisMode & AliPWG0Helper::kTPCITS)
258         path += "/tpcits";
259
260       gSystem->mkdir(path, kTRUE);
261       
262       TString fileName("multiplicity");
263       if (optionStr.Contains("only-process-type-nd"))
264         fileName += "ND";
265       if (optionStr.Contains("only-process-type-sd"))
266         fileName += "SD";
267       if (optionStr.Contains("only-process-type-dd"))
268         fileName += "DD";
269       fileName += ".root";
270       
271       gSystem->Rename(fileName, path + "/" + fileName);
272       gSystem->Rename("event_stat.root", path + "/event_stat.root");
273       
274       Printf(">>>>> Moved files to %s", path.Data());
275     }  
276   }
277   else if (aProof == 3)
278   {
279     gROOT->ProcessLine(".L CreateChainFromDataSet.C");
280     ds = gProof->GetDataSet(data)->GetStagedSubset();
281     chain = CreateChainFromDataSet(ds, "esdTree", nRuns);
282     mgr->StartAnalysis("local", chain, nRuns, offset);
283   }
284   else if (aProof == 0)
285   {
286     // Create chain of input files
287     gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
288     chain = CreateESDChain(data, nRuns, offset);
289
290     mgr->StartAnalysis((aProof > 0) ? "proof" : "local", chain);
291   }
292   else if (aProof == -1 || aProof == -2)
293   {
294     gSystem->Unlink("EventStat_temp.root");
295     gSystem->Unlink("mult_Temp.root");
296     gSystem->Unlink("event_stat.root");
297     gSystem->Unlink("multiplicity.root");
298   
299     AliAnalysisAlien *plugin = new AliAnalysisAlien();
300   
301     if (aProof == -1)
302       plugin->SetRunMode("full");
303     else
304       plugin->SetRunMode("terminate");
305
306     plugin->SetAPIVersion("V1.1x");
307     plugin->SetROOTVersion("v5-26-00b-2");
308     //plugin->SetAliROOTVersion("v4-19-10-AN");
309   
310     //plugin->SetGridDataDir("/alice/sim/LHC10b6");
311     //plugin->SetDataPattern("*/AliESDs.root");
312     //plugin->SetRunRange(115186, 115210);
313     
314     //plugin->SetGridDataDir("/alice/data/2010/LHC10b");
315     //plugin->SetDataPattern("*/AliESDs.root");
316     //plugin->SetRunRange(114931, 114931);
317     //plugin->SetRunPrefix("000");
318     
319   //plugin->AddDataFile("/alice/cern.ch/user/j/jgrosseo/run114931_6.xml"); //LHC10b4
320   
321     TString dataStr(data);
322     dataStr.ReplaceAll("__", "#");
323     TObjArray* dataList = dataStr.Tokenize("#");
324     for (Int_t i=0; i<dataList->GetEntries(); i++)
325       plugin->AddDataFile(Form("/alice/cern.ch/user/j/jgrosseo/%s", dataList->At(i)->GetName()));
326   
327     TGrid::Connect("alien://");
328     
329     // use work dir from ds name
330     TString dir;
331     dir.Form("gridjob/dir_%s", data);
332     if (aProof == -1)
333       gGrid->Rmdir(dir);
334     
335     /*
336     Int_t n = 0;
337     while (n < 1000)
338     {
339       dir.Form("gridjob/dir%d", n);
340       TMap* mapLs = (TMap*) gGrid->Ls(dir)->At(0);
341       TObjString *valueLsPath = dynamic_cast<TObjString*>(mapLs->GetValue("path"));
342       if (!valueLsPath)
343         break;
344       n++;
345     }
346     if (n == 1000)
347       AliFatal("Clean your home dir!");
348     if (aProof == -2)
349       n--;
350     dir.Form("gridjob/dir%d", n);
351     */
352     Printf(">>>>> Using relative dir %s", dir.Data());
353   
354     plugin->SetGridWorkingDir(dir);
355     plugin->SetGridOutputDir("griddir");
356     plugin->SetDefaultOutputs();
357     
358     //
359     plugin->SetFastReadOption();
360   
361   //plugin->EnablePackage(gSystem->ExpandPathName("$ALICE_ROOT/PWG0base.par"));
362     plugin->EnablePackage(gSystem->ExpandPathName("STEERBase.par"));
363     plugin->EnablePackage(gSystem->ExpandPathName("ESD.par"));
364     plugin->EnablePackage(gSystem->ExpandPathName("AOD.par"));
365     plugin->EnablePackage(gSystem->ExpandPathName("ANALYSIS.par"));
366     plugin->EnablePackage(gSystem->ExpandPathName("ANALYSISalice.par"));
367     plugin->EnablePackage(gSystem->ExpandPathName("PWG0base.par"));
368     plugin->SetAnalysisSource("AliMultiplicityTask.cxx");
369     plugin->SetAdditionalLibs("AliMultiplicityTask.h AliMultiplicityTask.cxx");
370   
371     plugin->SetSplitMaxInputFileNumber(50);
372     plugin->SetNtestFiles(2);
373   
374     AliAnalysisManager::GetAnalysisManager()->SetGridHandler(plugin);
375     
376     mgr->StartAnalysis("grid");
377     if (aProof == -1)
378       return;
379   }
380
381   if (save)
382   {
383     TString path("maps/");
384     path += TString(data).Tokenize("/")->Last()->GetName();
385   
386     UInt_t triggerNoFlags = (UInt_t) trigger % (UInt_t) AliTriggerAnalysis::kStartOfFlags;
387     switch (triggerNoFlags)
388     {
389       case AliTriggerAnalysis::kAcceptAll: path += "/all"; break;
390       case AliTriggerAnalysis::kMB1: path += "/mb1"; break;
391       case AliTriggerAnalysis::kMB2: path += "/mb2"; break;
392       case AliTriggerAnalysis::kMB3: path += "/mb3"; break;
393       case AliTriggerAnalysis::kSPDGFO: path += "/spdgfo"; break;
394       case AliTriggerAnalysis::kSPDGFOBits: path += "/spdgfobits"; break;
395       case AliTriggerAnalysis::kV0AND: path += "/v0and"; break;
396       case AliTriggerAnalysis::kNSD1: path += "/nsd1"; break;
397       case AliTriggerAnalysis::kMB1Prime: path += "/mb1prime"; break;
398       default: Printf("ERROR: Trigger undefined for path to files"); return;
399     }
400   
401     if (trigger & AliTriggerAnalysis::kOneParticle)
402       path += "-onepart";
403   
404     if (analysisMode & AliPWG0Helper::kSPD)
405       path += "/spd";
406   
407     if (analysisMode & AliPWG0Helper::kTPC)
408       path += "/tpc";
409     
410     if (analysisMode & AliPWG0Helper::kTPCITS)
411       path += "/tpcits";
412
413     gSystem->mkdir(path, kTRUE);
414   
415     TString fileName("multiplicity");
416     if (optionStr.Contains("only-process-type-nd"))
417       fileName += "ND";
418     if (optionStr.Contains("only-process-type-sd"))
419       fileName += "SD";
420     if (optionStr.Contains("only-process-type-dd"))
421       fileName += "DD";
422     fileName += ".root";
423   
424     gSystem->Rename(fileName, path + "/" + fileName);
425     gSystem->Rename("event_stat.root", path + "/event_stat.root");
426   
427     Printf(">>>>> Moved files to %s", path.Data());
428   }
429 }