]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/muondep/AccEffTemplates/AODtrain.C
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWG / muondep / AccEffTemplates / AODtrain.C
1 // ### Settings that make sense when using the Alien plugin
2 //==============================================================================
3 Int_t       runOnData          = 1;       // Set to 1 if processing real data
4 Int_t       iCollision         = 1;       // 0=pp, 1=Pb-Pb
5 //==============================================================================
6 Bool_t      usePhysicsSelection = kFALSE; // use physics selection
7 Bool_t      useTender           = kFALSE; // use tender wagon
8 Bool_t      useCentrality       = kFALSE; // centrality
9 Bool_t      useV0tender         = kFALSE;  // use V0 correction in tender
10 Bool_t      useDBG              = kFALSE;  // activate debugging
11 Bool_t      useMC               = kTRUE;  // use MC info
12 Bool_t      useKFILTER          = kTRUE;  // use Kinematics filter
13 Bool_t      useTR               = kTRUE;  // use track references
14 Bool_t      useCORRFW           = kFALSE; // do not change
15 Bool_t      useAODTAGS          = kFALSE; // use AOD tags
16 Bool_t      useSysInfo          = kFALSE; // use sys info
17
18 // ### Analysis modules to be included. Some may not be yet fully implemented.
19 //==============================================================================
20 Int_t       iAODhandler        = 1;      // Analysis produces an AOD or dAOD's
21 Int_t       iESDMCLabelAddition= 1;
22 Int_t       iESDfilter         = 1;      // ESD to AOD filter (barrel + muon tracks)
23 Int_t       iMUONcopyAOD       = 1;      // Task that copies only muon events in a separate AOD (PWG3)
24 Int_t       iMUONRefit         = 0;      // Refit ESD muon tracks before producing AODs
25 Int_t       iMUONPerformance   = 0;      // Task to study the muon performances in MC simulation
26 Int_t       iMUONEfficiency    = 0;      // Task to measure the muon efficiency
27 Int_t       iJETAN             = 0;      // Jet analysis (PWG4)
28 Int_t       iJETANdelta        = 0;      // Jet delta AODs
29 Int_t       iPWG3vertexing     = 0;      // Vertexing HF task (PWG3)
30 Int_t       iPWG3JPSIfilter    = 0;      // JPSI filtering (PWG3)
31 Int_t       iPWG3d2h           = 0;      // D0->2 hadrons (PWG3)
32
33 // ### Configuration macros used for each module
34 //==============================================================================
35 TString configPWG3d2h = (iCollision==0)?"$ALICE_ROOT/PWG3/vertexingHF/ConfigVertexingHF.C"
36 :"$ALICE_ROOT/PWG3/vertexingHF/ConfigVertexingHF_highmult.C";
37
38 // Temporaries.
39 class AliOADBPhysicsSelection;                                                                                                                  
40 AliOADBPhysicsSelection *CreateOADBphysicsSelection();
41 void AODmerge();
42 void AddAnalysisTasks(Int_t);
43 Bool_t LoadCommonLibraries();
44 Bool_t LoadAnalysisLibraries();
45 Bool_t LoadLibrary(const char *);
46 TChain *CreateChain();
47
48 //______________________________________________________________________________
49 void AODtrain(Int_t merge=0)
50 {
51   // Main analysis train macro.
52   // merge = 0: production
53   // merge = 1: intermediate merging
54   // merge = 2: final merging + terminate
55   
56   if (merge) {
57     TGrid::Connect("alien://");
58     if (!gGrid || !gGrid->IsConnected()) {
59       ::Error("QAtrain", "No grid connection");
60       return;
61     }
62   }
63   // Set temporary merging directory to current one
64   gSystem->Setenv("TMPDIR", gSystem->pwd());
65   // Set temporary compilation directory to current one
66   gSystem->SetBuildDir(gSystem->pwd(), kTRUE);
67   printf("==================================================================\n");
68   printf("===========    RUNNING FILTERING TRAIN   ==========\n");
69   printf("==================================================================\n");
70   printf("=  Configuring analysis train for:                               =\n");
71   if (usePhysicsSelection)   printf("=  Physics selection                                                =\n");
72   if (useTender)    printf("=  TENDER                                                        =\n");
73   if (iESDfilter)   printf("=  ESD filter                                                    =\n");
74   if (iMUONcopyAOD) printf("=  MUON copy AOD                                                 =\n");
75   if (iJETAN)       printf("=  Jet analysis                                                  =\n");
76   if (iJETANdelta)  printf("=     Jet delta AODs                                             =\n");
77   if (iPWG3vertexing) printf("=  PWG3 vertexing                                                =\n");
78   if (iPWG3JPSIfilter) printf("=  PWG3 j/psi filter                                             =\n");
79   if (iPWG3d2h) printf("=  PWG3 D0->2 hadrons QA                                     =\n");
80   
81   // Load common libraries and set include path
82   if (!LoadCommonLibraries()) {
83     ::Error("AnalysisTrain", "Could not load common libraries");
84     return;
85   }
86   
87   // Make the analysis manager and connect event handlers
88   AliAnalysisManager *mgr  = new AliAnalysisManager("Analysis Train", "Production train");
89   if (useSysInfo) mgr->SetNSysInfo(100);
90   // Load analysis specific libraries
91   if (!LoadAnalysisLibraries()) {
92     ::Error("AnalysisTrain", "Could not load analysis libraries");
93     return;
94   }   
95   
96   // Create input handler (input container created automatically)
97   // ESD input handler
98   AliESDInputHandler *esdHandler = new AliESDInputHandler();
99   mgr->SetInputEventHandler(esdHandler);       
100   // Monte Carlo handler
101   if (useMC) {
102     AliMCEventHandler* mcHandler = new AliMCEventHandler();
103     mgr->SetMCtruthEventHandler(mcHandler);
104     mcHandler->SetReadTR(useTR); 
105   }   
106   // AOD output container, created automatically when setting an AOD handler
107   if (iAODhandler) {
108     // AOD output handler
109     AliAODHandler* aodHandler   = new AliAODHandler();
110     aodHandler->SetOutputFileName("AliAOD.root");
111     mgr->SetOutputEventHandler(aodHandler);
112   }
113   // Debugging if needed
114   if (useDBG) mgr->SetDebugLevel(3);
115   
116   AddAnalysisTasks(merge);
117   if (merge) {
118     AODmerge();
119     if (merge > 1) {
120       mgr->InitAnalysis();
121       mgr->SetGridHandler(new AliAnalysisAlien());
122       mgr->StartAnalysis("grid terminate",0);
123     }
124     return;
125   }   
126   // Run the analysis                                                                                                                     
127   //
128   TChain *chain = CreateChain();
129   if (!chain) return;
130   
131   TStopwatch timer;
132   timer.Start();
133   mgr->SetSkipTerminate(kTRUE);
134   if (mgr->InitAnalysis()) {
135     mgr->PrintStatus();
136     mgr->StartAnalysis("local", chain);
137   }
138   timer.Print();
139 }                                                                                                                                          
140
141 //______________________________________________________________________________                                                           
142 void AddAnalysisTasks(Int_t merge){                                                                                                                                          
143   // Add all analysis task wagons to the train                                                                                               
144   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();                                                                     
145   
146   //
147   // Tender and supplies. Needs to be called for every event.
148   //
149   //AliAnalysisManager::SetCommonFileName("AODQA.root");
150   if (useTender) {
151     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/TenderSupplies/AddTaskTender.C");
152     // IF V0 tender needed, put kTRUE below
153     AliAnalysisTaskSE *tender = AddTaskTender(useV0tender);
154     //      tender->SetDebugLevel(2);
155   }
156   
157   if (usePhysicsSelection) {
158     // Physics selection task
159     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
160     mgr->RegisterExtraFile("event_stat.root");
161     AliPhysicsSelectionTask *physSelTask = AddTaskPhysicsSelection(kFALSE);
162     //      AliOADBPhysicsSelection * oadbDefaultPbPb = CreateOADBphysicsSelection();      
163     //      physSelTask->GetPhysicsSelection()->SetCustomOADBObjects(oadbDefaultPbPb,0,0);
164     //      if (!merge) mgr->AddStatisticsTask(AliVEvent::kAny);
165   }
166   // Centrality (only Pb-Pb)
167   if (iCollision && useCentrality) {
168     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
169     AliCentralitySelectionTask *taskCentrality = AddTaskCentrality();
170     taskCentrality->SelectCollisionCandidates(AliVEvent::kAny);
171   }
172   
173   if (iMUONRefit) {
174     gROOT->LoadMacro("$ALICE_ROOT/PWG/muondep/AddTaskMuonRefit.C");
175     AliAnalysisTaskMuonRefit* refit = AddTaskMuonRefit(-1., -1., kTRUE, -1., -1.);
176     refit->RemoveMonoCathodClusters(kTRUE, kFALSE);
177   }
178   
179   if(iESDMCLabelAddition) {
180     gROOT->LoadMacro("$ALICE_ROOT/PWG/muondep/AddTaskESDMCLabelAddition.C");
181     AliAnalysisTaskESDMCLabelAddition *esdmclabel = AddTaskESDMCLabelAddition();
182   }
183   
184   if (useMC && useTR && iMUONPerformance) {
185     gROOT->LoadMacro("$ALICE_ROOT/PWGPP/MUON/dep/AddTaskMuonPerformance.C");
186     AliAnalysisTaskMuonPerformance* muonPerformance = AddTaskMuonPerformance();
187     if (usePhysicsSelection) muonPerformance->SelectCollisionCandidates(AliVEvent::kAny);
188     muonPerformance->UseMCKinematics(kTRUE);
189     muonPerformance->SetMCTrigLevelFromMatchTrk(kTRUE);
190   }
191   
192   if (iMUONEfficiency) {
193     gROOT->LoadMacro("$ALICE_ROOT/PWGPP/MUON/dep/AddTaskMUONTrackingEfficiency.C");
194     AliAnalysisTaskMuonTrackingEff* muonEfficiency = AddTaskMUONTrackingEfficiency(kTRUE, kTRUE);
195     if (usePhysicsSelection) muonEfficiency->SelectCollisionCandidates(AliVEvent::kAny);
196     muonEfficiency->UseMCLabel(kTRUE);
197   }
198   
199   if (iESDfilter) {
200     //  ESD filter task configuration.
201     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/ESDfilter/macros/AddTaskESDFilter.C");
202     if (iMUONcopyAOD) {
203       printf("Registering delta AOD file\n");
204       mgr->RegisterExtraFile("AliAOD.Muons.root");
205       mgr->RegisterExtraFile("AliAOD.Dimuons.root");
206       AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(useKFILTER, kTRUE, kFALSE, kFALSE /*usePhysicsSelection*/,kFALSE,kTRUE,kTRUE,kTRUE,1100,1); // others
207     } else {
208       AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(useKFILTER, kFALSE, kFALSE, kFALSE /*usePhysicsSelection*/,kFALSE,kTRUE,kTRUE,kTRUE,1100,1); // others
209     }   
210   }   
211   
212   // ********** PWG3 wagons ******************************************************           
213   // PWG3 vertexing
214   if (iPWG3vertexing) {
215     gROOT->LoadMacro("$ALICE_ROOT/PWG3/vertexingHF/macros/AddTaskVertexingHF.C");
216     if (!iPWG3d2h) TFile::Cp(gSystem->ExpandPathName(configPWG3d2h.Data()), "file:ConfigVertexingHF.C");
217     AliAnalysisTaskSEVertexingHF *taskvertexingHF = AddTaskVertexingHF();
218     if (!taskvertexingHF) ::Warning("AnalysisTrainNew", "AliAnalysisTaskSEVertexingHF cannot run for this train conditions - EXCLUDED");
219     else mgr->RegisterExtraFile("AliAOD.VertexingHF.root");
220     taskvertexingHF->SelectCollisionCandidates(0);
221   }   
222   
223   // PWG3 JPSI filtering (only pp)
224   if (iPWG3JPSIfilter && (iCollision==0)) {
225     gROOT->LoadMacro("$ALICE_ROOT/PWG3/dielectron/macros/AddTaskJPSIFilter.C");
226     AliAnalysisTaskSE *taskJPSIfilter = AddTaskJPSIFilter();
227     if (!taskJPSIfilter) ::Warning("AnalysisTrainNew", "AliAnalysisTaskDielectronFilter cannot run for this train conditions - EXCLUDED");
228     else mgr->RegisterExtraFile("AliAOD.Dielectron.root");
229     taskJPSIfilter->SelectCollisionCandidates(0);
230   }   
231   
232   // PWG3 D2h
233   if (iPWG3d2h) {   
234     gROOT->LoadMacro("$ALICE_ROOT/PWG3/vertexingHF/AddD2HTrain.C");
235     TFile::Cp(gSystem->ExpandPathName(configPWG3d2h.Data()), "file:ConfigVertexingHF.C");
236     AddD2HTrain(kFALSE, 1,0,0,0,0,0,0,0,0,0,0);                                 
237   }
238   
239   // ********** PWG4 wagons ******************************************************
240   // Jet analysis
241   
242   // Configurations flags, move up?
243   TString kDeltaAODJetName = "AliAOD.Jets.root"; //
244   Bool_t  kIsPbPb = (iCollision==0)?false:true; // can be more intlligent checking the name of the data set
245   TString kDefaultJetBackgroundBranch = "";
246   TString kJetSubtractBranches = "";
247   UInt_t kHighPtFilterMask = 128;// from esd filter
248   UInt_t iPhysicsSelectionFlag = AliVEvent::kMB;
249   if (iJETAN) {
250     gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJets.C");
251     // Default jet reconstructor running on ESD's
252     AliAnalysisTaskJets *taskjets = AddTaskJets("AOD","UA1",0.4,kHighPtFilterMask,1.,0); // no background subtraction     
253     if (!taskjets) ::Fatal("AnalysisTrainNew", "AliAnalysisTaskJets cannot run for this train conditions - EXCLUDED");
254     if(kDeltaAODJetName.Length()>0) taskjets->SetNonStdOutputFile(kDeltaAODJetName.Data());
255     if (iJETANdelta) {
256       //            AddTaskJetsDelta("AliAOD.Jets.root"); // need to modify this accordingly in the add task jets
257       mgr->RegisterExtraFile(kDeltaAODJetName.Data());
258       TString cTmp("");
259       if(kIsPbPb){
260         // UA1 intrinsic background subtraction
261         taskjets = AddTaskJets("AOD","UA1",0.4,kHighPtFilterMask,1.,2); // background subtraction
262         if(kDeltaAODJetName.Length()>0)taskjets->SetNonStdOutputFile(kDeltaAODJetName.Data());
263       }
264       // SICONE 
265       taskjets = AddTaskJets("AOD","SISCONE",0.4,kHighPtFilterMask,0.15,0); //no background subtration to be done later....                                                                                  
266       if(kDeltaAODJetName.Length()>0)taskjets->SetNonStdOutputFile(kDeltaAODJetName.Data());
267       cTmp = taskjets->GetNonStdBranch();
268       if(cTmp.Length()>0)kJetSubtractBranches += Form("%s ",cTmp.Data());
269       
270       // Add the clusters..
271       gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJetCluster.C");
272       AliAnalysisTaskJetCluster *taskCl = 0;
273       Float_t fCenUp = 0;
274       Float_t fCenLo = 0;
275       Float_t fTrackEtaWindow = 0.9;
276       taskCl = AddTaskJetCluster("AOD","",kHighPtFilterMask,iPhysicsSelectionFlag,"KT",0.4,0,1, kDeltaAODJetName.Data(),0.15,fTrackEtaWindow,0); // this one is for the background and random jets, random cones with no skip                                                                                 
277       taskCl->SetBackgroundCalc(kTRUE);
278       taskCl->SetNRandomCones(10);
279       taskCl->SetCentralityCut(fCenLo,fCenUp);
280       taskCl->SetGhostEtamax(fTrackEtaWindow);
281       kDefaultJetBackgroundBranch = Form("%s_%s",AliAODJetEventBackground::StdBranchName(),taskCl->GetJetOutputBranch());
282       
283       taskCl = AddTaskJetCluster("AOD","",kHighPtFilterMask,iPhysicsSelectionFlag,"ANTIKT",0.4,2,1,kDeltaAODJetName.Data(),0.15);
284       taskCl->SetCentralityCut(fCenLo,fCenUp);
285       if(kIsPbPb)taskCl->SetBackgroundBranch(kDefaultJetBackgroundBranch.Data());
286       taskCl->SetNRandomCones(10);
287       kJetSubtractBranches += Form("%s ",taskCl->GetJetOutputBranch());
288       
289       taskCl = AddTaskJetCluster("AOD","",kHighPtFilterMask,iPhysicsSelectionFlag,"ANTIKT",0.2,0,1,kDeltaAODJetName.Data(),0.15);
290       taskCl->SetCentralityCut(fCenLo,fCenUp);
291       if(kIsPbPb)taskCl->SetBackgroundBranch(kDefaultJetBackgroundBranch.Data());
292       kJetSubtractBranches += Form("%s ",taskCl->GetJetOutputBranch());
293       
294       // DO THE BACKGROUND SUBTRACTION
295       if(kIsPbPb&&kJetSubtractBranches.Length()){
296         gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskJetBackgroundSubtract.C");
297         AliAnalysisTaskJetBackgroundSubtract *taskSubtract = 0;
298         taskSubtract = AddTaskJetBackgroundSubtract(kJetSubtractBranches,1,"B0","B%d");
299         taskSubtract->SetBackgroundBranch(kDefaultJetBackgroundBranch.Data());
300         if(kDeltaAODJetName.Length()>0)taskSubtract->SetNonStdOutputFile(kDeltaAODJetName.Data());
301       }
302     } 
303   }
304 }
305
306 //______________________________________________________________________________
307 Bool_t LoadCommonLibraries()
308 {
309   // Load common analysis libraries.
310   if (!gSystem->Getenv("ALICE_ROOT")) {
311     ::Error("AnalysisTrainNew.C::LoadCommonLibraries", "Analysis train requires that analysis libraries are compiled with a local AliRoot"); 
312     return kFALSE;
313   }   
314   Bool_t success = kTRUE;
315   // Load framework classes. Par option ignored here.
316   success &= LoadLibrary("libSTEERBase.so");
317   success &= LoadLibrary("libESD.so");
318   success &= LoadLibrary("libAOD.so");
319   success &= LoadLibrary("libANALYSIS.so");
320   success &= LoadLibrary("libOADB.so");
321   success &= LoadLibrary("libANALYSISalice.so");
322   success &= LoadLibrary("libCORRFW.so");
323   success &= LoadLibrary("libESDfilter.so");
324   gROOT->ProcessLine(".include $ALICE_ROOT/include");
325   if (success) {
326     ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Load common libraries:    SUCCESS");
327     ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Include path for Aclic compilation:\n%s",
328            gSystem->GetIncludePath());
329   } else {           
330     ::Info("AnalysisTrainNew.C::LoadCommodLibraries", "Load common libraries:    FAILED");
331   }   
332   return success;
333 }
334
335 //______________________________________________________________________________
336 Bool_t LoadAnalysisLibraries()
337 {
338   // Load common analysis libraries.
339   if (useTender) {
340     if (!LoadLibrary("TENDER") ||
341         !LoadLibrary("TENDERSupplies")) return kFALSE;
342   }       
343   if (iESDfilter || iPWG3MuonTrain) {
344     if (!LoadLibrary("PWGmuon")) return kFALSE;
345     //      if (!LoadLibrary("PWG3base")) return kFALSE;
346     //      if (!LoadLibrary("PWG3muon")) return kFALSE;
347   }   
348   if (iMUONRefit || iESDMCLabelAddition) {
349     if (!LoadLibrary("PWGmuondep")) return kFALSE;
350   }
351   if ((useMC && useTR && iMUONPerformance) || iMUONEfficiency) {
352     if (!LoadLibrary("PWGPPMUONdep")) return kFALSE;
353   }
354   // JETAN
355   if (iJETAN) {
356     if (!LoadLibrary("JETAN")) return kFALSE;
357   }
358   if (iJETANdelta) {
359     if (!LoadLibrary("JETAN") ||
360         !LoadLibrary("CGAL") ||
361         !LoadLibrary("fastjet") ||
362         !LoadLibrary("siscone") ||
363         !LoadLibrary("SISConePlugin") ||
364         !LoadLibrary("FASTJETAN")) return kFALSE;
365   }     
366   // PWG3 Vertexing HF
367   if (iPWG3vertexing || iPWG3d2h) {
368     if (!LoadLibrary("PWG3base") ||
369         !LoadLibrary("PWG3vertexingHF")) return kFALSE;
370   }   
371   // PWG3 dielectron
372   if (iPWG3JPSIfilter) {
373     if (!LoadLibrary("PWG3dielectron")) return kFALSE;
374   }   
375   
376   ::Info("AnalysisTrainNew.C::LoadAnalysisLibraries", "Load other libraries:   SUCCESS");
377   return kTRUE;
378 }
379
380 //______________________________________________________________________________
381 Bool_t LoadLibrary(const char *module)
382 {
383   // Load a module library in a given mode. Reports success.
384   Int_t result;
385   TString mod(module);
386   if (!mod.Length()) {
387     ::Error("AnalysisTrainNew.C::LoadLibrary", "Empty module name");
388     return kFALSE;
389   }   
390   // If a library is specified, just load it
391   if (mod.EndsWith(".so")) {
392     mod.Remove(mod.Index(".so"));
393     result = gSystem->Load(mod);
394     if (result < 0) {
395       ::Error("AnalysisTrainNew.C::LoadLibrary", "Could not load library %s", module);
396       return kFALSE;
397     }
398     return kTRUE;
399   } 
400   // Check if the library is already loaded
401   if (strlen(gSystem->GetLibraries(Form("%s.so", module), "", kFALSE)) > 0) return kTRUE;    
402   result = gSystem->Load(Form("lib%s.so", module));
403   if (result < 0) {
404     ::Error("AnalysisTrainNew.C::LoadLibrary", "Could not load module %s", module);
405     return kFALSE;
406   }
407   return kTRUE;
408 }           
409
410
411 //______________________________________________________________________________
412 TChain *CreateChain()
413 {
414   // Create the input chain
415   chain = new TChain("esdTree");
416   if (gSystem->AccessPathName("AliESDs.root")) 
417     ::Error("AnalysisTrainNew.C::CreateChain", "File: AliESDs.root not in ./data dir");
418   else 
419     chain->Add("AliESDs.root");
420   if (chain->GetNtrees()) return chain;
421   return NULL;
422 }   
423
424 //______________________________________________________________________________
425 void AODmerge()
426 {
427   // Merging method. No staging and no terminate phase.
428   TStopwatch timer;
429   timer.Start();
430   TString outputDir = "wn.xml";
431   TString outputFiles = "AliAOD.root,AliAOD.Muons.root";
432   TString mergeExcludes = "";
433   TObjArray *list = outputFiles.Tokenize(",");
434   TIter *iter = new TIter(list);
435   TObjString *str;
436   TString outputFile;
437   Bool_t merged = kTRUE;
438   while((str=(TObjString*)iter->Next())) {
439     outputFile = str->GetString();
440     // Skip already merged outputs
441     if (!gSystem->AccessPathName(outputFile)) {
442       printf("Output file <%s> found. Not merging again.",outputFile.Data());
443       continue;
444     }
445     if (mergeExcludes.Contains(outputFile.Data())) continue;
446     merged = AliAnalysisAlien::MergeOutput(outputFile, outputDir, 10, 0);
447     if (!merged) {
448       printf("ERROR: Cannot merge %s\n", outputFile.Data());
449       return;
450     }
451   }
452   // all outputs merged, validate
453   ofstream out;
454   out.open("outputs_valid_merge", ios::out);
455   out.close();
456   timer.Print();
457 }