]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/EMCAL/macros/runEMCalAnalysis.C
ed6cf441d69a9f1d2704aa943603c7de299c5648
[u/mrichter/AliRoot.git] / PWG / EMCAL / macros / runEMCalAnalysis.C
1 // runEMCalAnalysis.C
2 // =====================
3 // This macro can be used to run EMCal analysis within the EMCal part of the Jet Framework.
4 //
5 // Examples:
6 // -> Analyze ESDs from the pA pilot run on the AliEn grid with your task in AnaClass.cxx/.h
7 //     dataType = "ESD", useGrid = kTRUE, pattern = "*ESDs/pass2/*ESDs.root", addCXXs = "AnaClass.cxx", 
8 //     addHs = "AnaClass.h", gridDir = "/alice/data/2012/LHC12g", gridMode = "full", runNumbers = "188359 188362"
9 //     
10 // -> Analyze AODs (up to 96 files) locally given in files_aod.txt
11 //     dataType = "AOD", useGrid = kFALSE, numLocalFiles = 96
12 //
13 // MERGING ON ALIEN
14 // ++++++++++++++++
15 // If you run on the grid, you can monitor the jobs with alimonitor.cern.ch. When enough of them are in DONE state,
16 // you have to merge the output. This can be done automatically, if you just change the gridMode to "terminate" and
17 // give the EXACT name of the task whose output should be merged in uniqueName.
18 // 
19 //
20 // Authors: R. Haake, S. Aiola, M. Verweij
21
22 #include <ctime>
23 #include "TGrid.h"
24
25 AliAnalysisGrid* CreateAlienHandler(const char* uniqueName, const char* gridDir, const char* gridMode, const char* runNumbers, 
26                                      const char* pattern, TString additionalCode, TString additionalHeaders, Int_t maxFilesPerWorker, 
27                                      Int_t workerTTL, Bool_t isMC);
28                                     
29 //______________________________________________________________________________
30 void runEMCalAnalysis(
31          Bool_t         useGrid             = kTRUE,                      // local or grid
32          const char*    gridMode            = "test",                      // set the grid run mode (can be "full", "test", "offline", "submit" or "terminate")
33          const char*    dataType            = "AOD",                       // set the analysis type, AOD, ESD or sESD
34          const char*    pattern             = "*ESDs/pass2/AOD145/*AOD.root",    // file pattern (here one can specify subdirs like passX etc.) (used on grid)
35          const char*    gridDir             = "/alice/data/2011/LHC11h_2",   // dir on alien, where the files live (used on grid)
36          const char*    runNumbers          = "167903 167915",             // considered run numbers (used on grid)
37          UInt_t         numLocalFiles       = 1,                          // number of files analyzed locally  
38          const char*    runPeriod           = "LHC11h",                    // set the run period (used on grid)
39          const char*    uniqueName          = "EMCal_LEGOTrainTest",     // sets base string for the name of the task on the grid
40          UInt_t         pSel                = AliVEvent::kAny,             // used event selection for every task except for the analysis tasks
41          Bool_t         useTender           = kTRUE,                       // trigger, if tender, track and cluster selection should be used (always)
42          Bool_t         isMC                = kFALSE,                      // trigger, if MC handler should be used
43          // Here you have to specify additional code files you want to use but that are not in aliroot
44          const char*    addCXXs             = "",
45          const char*    addHs               = "",
46
47          // These two settings depend on the dataset and your quotas on the AliEN services
48          Int_t          maxFilesPerWorker   = 4,
49          Int_t          workerTTL           = 7200
50
51          )
52 {
53
54   // Some pre-settings and constants
55   enum AlgoType {kKT, kANTIKT};
56   enum JetType  {kFULLJETS, kCHARGEDJETS, kNEUTRALJETS};
57   gSystem->SetFPEMask();
58   gSystem->Setenv("ETRAIN_ROOT", ".");
59   gSystem->Setenv("ETRAIN_PERIOD", runPeriod);
60   // change this objects to strings
61   TString usedData(dataType);
62   TString additionalCXXs(addCXXs);
63   TString additionalHs(addHs);
64   cout << dataType << " analysis chosen" << endl;
65   if (useGrid)  
66   {
67     cout << "-- using AliEn grid.\n";
68     if (usedData == "sESD") 
69     {
70       cout << "Skimmed ESD analysis not available on the grid!" << endl;
71       return;
72     }
73   }
74   else
75     cout << "-- using local analysis.\n";
76   
77
78   // Load necessary libraries
79   LoadLibs();
80
81   // Create analysis manager
82   AliAnalysisManager* mgr = new AliAnalysisManager(uniqueName);
83
84   // Check type of input and create handler for it
85   TString localFiles("-1");
86   if(usedData == "AOD")
87   {
88     localFiles = "files_aod.txt";
89     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/train/AddAODHandler.C");
90     AliAODInputHandler* aodH = AddAODHandler();
91   }
92   else if((usedData == "ESD") || (usedData == "sESD"))
93   {
94     if (usedData == "ESD")
95       localFiles = "files_esd.txt";
96     else
97       localFiles = "files_sesd.txt";
98     
99     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/train/AddESDHandler.C");
100     AliESDInputHandler* esdH = AddESDHandler();
101   }
102   else
103   {
104     cout << "Data type not recognized! You have to specify ESD, AOD, or sESD!\n";
105   }
106
107   if(!useGrid)
108     cout << "Using " << localFiles.Data() << " as input file list.\n";
109
110   // Create MC handler, if MC is demanded
111   if (isMC && (usedData != "AOD"))
112   {
113     AliMCEventHandler* mcH = new AliMCEventHandler();
114     mcH->SetPreReadMode(AliMCEventHandler::kLmPreRead);
115     mcH->SetReadTR(kTRUE);
116     mgr->SetMCtruthEventHandler(mcH); 
117   }
118   
119   // ################# Now: Add some basic tasks
120
121   // Physics selection task
122   gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/AddTaskEmcalPhysicsSelection.C");
123   AliPhysicsSelectionTask *physSelTask = AddTaskEmcalPhysicsSelection(kTRUE, kTRUE, pSel, 5, 5, 10, kTRUE, -1, -1, -1, -1);
124
125   if (!physSelTask) 
126   {
127     cout << "no physSelTask"; 
128     return; 
129   }
130
131   // Centrality task
132   if (usedData == "ESD") {
133     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
134     AliCentralitySelectionTask *centralityTask = AddTaskCentrality(kTRUE);
135   }
136
137   // Compatibility task, only needed for skimmed ESD
138   if (usedData == "sESD") {
139     gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/AddTaskEmcalCompat.C");
140     AliEmcalCompatTask *comptask = AddTaskEmcalCompat();
141   }
142
143   // Setup task
144   gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/AddTaskEmcalSetup.C");
145   AliEmcalSetupTask *setupTask = AddTaskEmcalSetup();
146   setupTask->SetGeoPath("$ALICE_ROOT/OADB/EMCAL");
147   
148   // Tender Supplies
149   if (useTender) {
150     gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/AddTaskEmcalPreparation.C");
151     //adjust pass when running locally. On grid give empty string, will be picked up automatically from path to ESD/AOD file
152     AliAnalysisTaskSE *clusm = AddTaskEmcalPreparation(runPeriod,"pass1"); 
153   }
154
155   // Names of the different objects passed around; these are the default names; added here mostly for documentation purposes
156   // rhoName is only set if the background subtraction is switched on (doBkg)
157   TString tracksName = "AODFilterTracks";
158   TString clustersName = "EmcCaloClusters";
159
160   // ################# Now: Call jet preparation macro (picotracks, hadronic corrected caloclusters, ...) 
161   gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/AddTaskMatchingChain.C");
162   AddTaskMatchingChain(runPeriod,pSel,
163                        clustersName,1.,kTRUE,
164                        0.1,kTRUE,kTRUE);
165   
166   // ################# Now: Add analysis task
167   // Here you can put in your AddTaskMacro for your task
168   gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/AddTaskEmcalSample.C");
169   AliAnalysisTaskEmcalSample* anaTask = AddTaskEmcalSample(tracksName, clustersName, 4);
170
171   // Set the physics selection for all given tasks
172   TObjArray *toptasks = mgr->GetTasks();
173   for (Int_t i=0; i<toptasks->GetEntries(); ++i) 
174   {
175     AliAnalysisTaskSE *task = dynamic_cast<AliAnalysisTaskSE*>(toptasks->At(i));
176     if (!task)
177       continue;
178     if (task->InheritsFrom("AliPhysicsSelectionTask"))
179       continue;
180     ::Info("setPSel", "Set physics selection for %s (%s)", task->GetName(), task->ClassName());
181     task->SelectCollisionCandidates(pSel);
182   }
183
184   mgr->SetUseProgressBar(1, 25);
185         
186   if (!mgr->InitAnalysis()) 
187     return;
188   mgr->PrintStatus();
189
190   if (useGrid) 
191   {  // GRID CALCULATION
192
193     AliAnalysisGrid *plugin = CreateAlienHandler(uniqueName, gridDir, gridMode, runNumbers, pattern, additionalCXXs, additionalHs, maxFilesPerWorker, workerTTL, isMC);
194     mgr->SetGridHandler(plugin);
195
196     // start analysis
197     cout << "Starting GRID Analysis...";
198     mgr->SetDebugLevel(0);
199     mgr->StartAnalysis("grid",10);
200   }
201   else
202   {  // LOCAL CALCULATION
203
204     TChain* chain = 0;
205     if (usedData == "AOD") 
206     {
207       gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/CreateAODChain.C");
208       chain = CreateAODChain(localFiles.Data(), numLocalFiles);
209     }
210     else
211     {  // ESD or skimmed ESD
212       gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/CreateESDChain.C");
213       chain = CreateESDChain(localFiles.Data(), numLocalFiles);
214     }
215     
216     // start analysis
217     cout << "Starting LOCAL Analysis...";
218     mgr->SetDebugLevel(2);
219     mgr->StartAnalysis("local", chain);
220   }
221 }
222
223 //______________________________________________________________________________
224 void LoadLibs()
225 {
226   // Load common libraries (better too many than too few)
227   gSystem->Load("libTree");
228   gSystem->Load("libVMC");
229   gSystem->Load("libGeom");
230   gSystem->Load("libGui");
231   gSystem->Load("libXMLParser");
232   gSystem->Load("libMinuit");
233   gSystem->Load("libMinuit2");
234   gSystem->Load("libProof");
235   gSystem->Load("libPhysics");
236   gSystem->Load("libSTEERBase");
237   gSystem->Load("libESD");
238   gSystem->Load("libAOD");
239   gSystem->Load("libOADB");
240   gSystem->Load("libANALYSIS");
241   gSystem->Load("libCDB");
242   gSystem->Load("libRAWDatabase");
243   gSystem->Load("libSTEER");
244   gSystem->Load("libEVGEN");
245   gSystem->Load("libANALYSISalice");
246   gSystem->Load("libCORRFW");
247   gSystem->Load("libTOFbase");
248   //gSystem->Load("libTOFrec");
249   gSystem->Load("libRAWDatabase");
250   gSystem->Load("libRAWDatarec");
251   gSystem->Load("libTPCbase");
252   gSystem->Load("libTPCrec");
253   gSystem->Load("libITSbase");
254   gSystem->Load("libITSrec");
255   gSystem->Load("libTRDbase");
256   gSystem->Load("libTender");
257   gSystem->Load("libSTAT");
258   gSystem->Load("libTRDrec");
259   gSystem->Load("libHMPIDbase");
260   gSystem->Load("libPWGPP");
261   gSystem->Load("libPWGHFbase");
262   gSystem->Load("libPWGDQdielectron");
263   gSystem->Load("libPWGHFhfe");
264   gSystem->Load("libEMCALUtils");
265   gSystem->Load("libPHOSUtils");
266   gSystem->Load("libPWGCaloTrackCorrBase");
267   gSystem->Load("libEMCALraw");
268   gSystem->Load("libEMCALbase");
269   gSystem->Load("libEMCALrec");
270   gSystem->Load("libTRDbase");
271   gSystem->Load("libVZERObase");
272   gSystem->Load("libVZEROrec");
273   gSystem->Load("libTender");
274   gSystem->Load("libTenderSupplies");
275   gSystem->Load("libPWGTools");
276   gSystem->Load("libPWGEMCAL");
277   gSystem->Load("libESDfilter");
278   gSystem->Load("libPWGGAEMCALTasks");
279   gSystem->Load("libPWGCFCorrelationsBase");
280   gSystem->Load("libPWGCFCorrelationsDPhi");
281
282   // include paths
283   gSystem->AddIncludePath("-Wno-deprecated");
284   gSystem->AddIncludePath("-I$ALICE_ROOT -I$ALICE_ROOT/include -I$ALICE_ROOT/EMCAL");
285   gSystem->AddIncludePath("-I$ALICE_ROOT/PWGDQ/dielectron -I$ALICE_ROOT/PWGHF/hfe");
286 }
287
288 AliAnalysisGrid* CreateAlienHandler(const char* uniqueName, const char* gridDir, const char* gridMode, const char* runNumbers, 
289                                      const char* pattern, TString additionalCode, TString additionalHeaders, Int_t maxFilesPerWorker, 
290                                      Int_t workerTTL, Bool_t isMC)
291 {
292   TDatime currentTime;
293   TString tmpName(uniqueName);
294
295   // Only add current date and time when not in terminate mode! In this case the exact name has to be supplied by the user
296   if(strcmp(gridMode, "terminate"))
297   {
298     tmpName += "_";
299     tmpName += currentTime.GetDate();
300     tmpName += "_";
301     tmpName += currentTime.GetTime();
302   }
303
304   TString tmpAdditionalLibs("");
305   tmpAdditionalLibs = Form("libTree libVMC libGeom libGui libXMLParser libMinuit libMinuit2 libProof libPhysics libSTEERBase libESD libAOD libOADB libANALYSIS libCDB libRAWDatabase libSTEER libANALYSISalice libCORRFW libTOFbase libRAWDatabase libRAWDatarec libTPCbase libTPCrec libITSbase libITSrec libTRDbase libTender libSTAT libTRDrec libHMPIDbase libPWGPP libPWGHFbase libPWGDQdielectron libPWGHFhfe libEMCALUtils libPHOSUtils libPWGCaloTrackCorrBase libEMCALraw libEMCALbase libEMCALrec libTRDbase libVZERObase libVZEROrec libTender libTenderSupplies libESDfilter libPWGTools libPWGEMCAL libPWGGAEMCALTasks libPWGCFCorrelationsBase libPWGCFCorrelationsDPhi  %s %s",additionalCode.Data(),additionalHeaders.Data());
306
307
308   TString macroName("");
309   TString execName("");
310   TString jdlName("");
311   macroName = Form("%s.C", tmpName.Data());
312   execName = Form("%s.sh", tmpName.Data());
313   jdlName = Form("%s.jdl", tmpName.Data());
314
315   AliAnalysisAlien *plugin = new AliAnalysisAlien();
316   plugin->SetOverwriteMode();
317   plugin->SetRunMode(gridMode);
318      
319   // Here you can set the (Ali)ROOT version you want to use
320   plugin->SetAPIVersion("V1.1x");
321   plugin->SetROOTVersion("v5-34-08-6");
322   plugin->SetAliROOTVersion("vAN-20140525");
323
324   plugin->SetGridDataDir(gridDir); // e.g. "/alice/sim/LHC10a6"
325   plugin->SetDataPattern(pattern); //dir structure in run directory
326   if (!isMC)
327    plugin->SetRunPrefix("000");
328
329   plugin->AddRunList(runNumbers);
330
331   plugin->SetGridWorkingDir(Form("work/%s",tmpName.Data()));
332   plugin->SetGridOutputDir("output"); // In this case will be $HOME/work/output
333
334   plugin->SetAnalysisSource(additionalCode.Data());
335   plugin->SetAdditionalLibs(tmpAdditionalLibs.Data());
336
337   plugin->SetDefaultOutputs(kTRUE);
338   //plugin->SetMergeExcludes("");
339   plugin->SetAnalysisMacro(macroName.Data());
340   plugin->SetSplitMaxInputFileNumber(maxFilesPerWorker);
341   plugin->SetExecutable(execName.Data());
342   plugin->SetTTL(workerTTL);
343   plugin->SetInputFormat("xml-single");
344   plugin->SetJDLName(jdlName.Data());
345   plugin->SetPrice(1);      
346   plugin->SetSplitMode("se");
347   plugin->SetNtestFiles(1);
348
349   return plugin;
350 }