]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/EMCAL/AliAnalysisTaskEmcal.cxx
Add pt resolution histograms (ESD analysis only)
[u/mrichter/AliRoot.git] / PWG / EMCAL / AliAnalysisTaskEmcal.cxx
1 // $Id$
2 //
3 // Emcal base analysis task.
4 //
5 // Author: S.Aiola, M. Verweij
6
7 #include "AliAnalysisTaskEmcal.h"
8
9 #include <TClonesArray.h>
10 #include <TList.h>
11 #include <TObject.h>
12 #include <TH1F.h>
13 #include <TProfile.h>
14 #include <TSystem.h>
15 #include <TFile.h>
16 #include <TChain.h>
17 #include <TKey.h>
18
19 #include "AliAODEvent.h"
20 #include "AliAnalysisManager.h"
21 #include "AliCentrality.h"
22 #include "AliEMCALGeometry.h"
23 #include "AliESDEvent.h"
24 #include "AliEmcalParticle.h"
25 #include "AliEventplane.h"
26 #include "AliInputEventHandler.h"
27 #include "AliLog.h"
28 #include "AliMCParticle.h"
29 #include "AliVCluster.h"
30 #include "AliVEventHandler.h"
31 #include "AliVParticle.h"
32 #include "AliVCaloTrigger.h"
33 #include "AliGenPythiaEventHeader.h"
34 #include "AliAODMCHeader.h"
35 #include "AliMCEvent.h"
36 #include "AliAnalysisUtils.h"
37 #include "AliEmcalTriggerPatchInfo.h"
38
39 #include "AliParticleContainer.h"
40 #include "AliClusterContainer.h"
41
42 ClassImp(AliAnalysisTaskEmcal)
43
44 //________________________________________________________________________
45 AliAnalysisTaskEmcal::AliAnalysisTaskEmcal() : 
46   AliAnalysisTaskSE("AliAnalysisTaskEmcal"),
47   fForceBeamType(kNA),
48   fGeneralHistograms(kFALSE),
49   fInitialized(kFALSE),
50   fCreateHisto(kTRUE),
51   fCaloCellsName(),
52   fCaloTriggersName(),
53   fCaloTriggerPatchInfoName(),
54   fMinCent(-999),
55   fMaxCent(-999),
56   fMinVz(-999),
57   fMaxVz(-999),
58   fTrackPtCut(0),
59   fMinNTrack(0),
60   fUseAliAnaUtils(kFALSE),
61   fAliAnalysisUtils(0x0),
62   fOffTrigger(AliVEvent::kAny),
63   fTrigClass(),
64   fTriggerTypeSel(kND),
65   fNbins(500),
66   fMinBinPt(0),
67   fMaxBinPt(250),
68   fMinPtTrackInEmcal(0),
69   fEventPlaneVsEmcal(-1),
70   fMinEventPlane(-1e6),
71   fMaxEventPlane(1e6),
72   fCentEst("V0M"),
73   fIsEmbedded(kFALSE),
74   fIsPythia(kFALSE),
75   fSelectPtHardBin(-999),
76   fMinMCLabel(0),
77   fMCLabelShift(0),
78   fNcentBins(4),
79   fNeedEmcalGeom(kTRUE),
80   fIsEsd(kFALSE),
81   fGeom(0),
82   fTracks(0),
83   fCaloClusters(0),
84   fCaloCells(0),
85   fCaloTriggers(0),
86   fTriggerPatchInfo(0),
87   fCent(0),
88   fCentBin(-1),
89   fEPV0(-1.0),
90   fEPV0A(-1.0),
91   fEPV0C(-1.0),
92   fNVertCont(0),
93   fBeamType(kNA),
94   fPythiaHeader(0),
95   fPtHard(0),
96   fPtHardBin(0),
97   fNTrials(0),
98   fXsection(0),
99   fParticleCollArray(),
100   fClusterCollArray(),
101   fMainTriggerPatch(0x0),
102   fTriggerType(kND),
103   fOutput(0),
104   fHistTrialsAfterSel(0),
105   fHistEventsAfterSel(0),
106   fHistXsectionAfterSel(0),
107   fHistTrials(0),
108   fHistEvents(0),
109   fHistXsection(0),
110   fHistPtHard(0),
111   fHistCentrality(0),
112   fHistZVertex(0),
113   fHistEventPlane(0),
114   fHistEventRejection(0)
115 {
116   // Default constructor.
117
118   fVertex[0] = 0;
119   fVertex[1] = 0;
120   fVertex[2] = 0;
121
122   fParticleCollArray.SetOwner(kTRUE);
123   fClusterCollArray.SetOwner(kTRUE);
124 }
125
126 //________________________________________________________________________
127 AliAnalysisTaskEmcal::AliAnalysisTaskEmcal(const char *name, Bool_t histo) : 
128   AliAnalysisTaskSE(name),
129   fForceBeamType(kNA),
130   fGeneralHistograms(kFALSE),
131   fInitialized(kFALSE),
132   fCreateHisto(histo),
133   fCaloCellsName(),
134   fCaloTriggersName(),
135   fCaloTriggerPatchInfoName(),
136   fMinCent(-999),
137   fMaxCent(-999),
138   fMinVz(-999),
139   fMaxVz(-999),
140   fTrackPtCut(0),
141   fMinNTrack(0),
142   fUseAliAnaUtils(kFALSE),
143   fAliAnalysisUtils(0x0),
144   fOffTrigger(AliVEvent::kAny),
145   fTrigClass(),
146   fTriggerTypeSel(kND),
147   fNbins(500),
148   fMinBinPt(0),
149   fMaxBinPt(250),
150   fMinPtTrackInEmcal(0),
151   fEventPlaneVsEmcal(-1),
152   fMinEventPlane(-1e6),
153   fMaxEventPlane(1e6),
154   fCentEst("V0M"),
155   fIsEmbedded(kFALSE),
156   fIsPythia(kFALSE),
157   fSelectPtHardBin(-999),
158   fMinMCLabel(0),
159   fMCLabelShift(0),
160   fNcentBins(4),
161   fNeedEmcalGeom(kTRUE),
162   fIsEsd(kFALSE),
163   fGeom(0),
164   fTracks(0),
165   fCaloClusters(0),
166   fCaloCells(0),
167   fCaloTriggers(0),
168   fTriggerPatchInfo(0),
169   fCent(0),
170   fCentBin(-1),
171   fEPV0(-1.0),
172   fEPV0A(-1.0),
173   fEPV0C(-1.0),
174   fNVertCont(0),
175   fBeamType(kNA),
176   fPythiaHeader(0),
177   fPtHard(0),
178   fPtHardBin(0),
179   fNTrials(0),
180   fXsection(0),
181   fParticleCollArray(),
182   fClusterCollArray(),
183   fMainTriggerPatch(0x0),
184   fTriggerType(kND),
185   fOutput(0),
186   fHistTrialsAfterSel(0),
187   fHistEventsAfterSel(0),
188   fHistXsectionAfterSel(0),
189   fHistTrials(0),
190   fHistEvents(0),
191   fHistXsection(0),
192   fHistPtHard(0),
193   fHistCentrality(0),
194   fHistZVertex(0),
195   fHistEventPlane(0),
196   fHistEventRejection(0)
197 {
198   // Standard constructor.
199
200   fVertex[0] = 0;
201   fVertex[1] = 0;
202   fVertex[2] = 0;
203
204   fParticleCollArray.SetOwner(kTRUE);
205   fClusterCollArray.SetOwner(kTRUE);
206
207   if (fCreateHisto) {
208     DefineOutput(1, TList::Class()); 
209   }
210 }
211
212 //________________________________________________________________________
213 AliAnalysisTaskEmcal::~AliAnalysisTaskEmcal()
214 {
215   // Destructor
216 }
217
218 //________________________________________________________________________
219 void AliAnalysisTaskEmcal::SetClusPtCut(Double_t cut, Int_t c)
220 {
221   AliClusterContainer *cont = GetClusterContainer(c);
222   if (cont) cont->SetClusPtCut(cut);
223   else AliError(Form("%s in SetClusPtCut(...): container %d not found",GetName(),c));
224 }
225
226 //________________________________________________________________________
227 void AliAnalysisTaskEmcal::SetClusTimeCut(Double_t min, Double_t max, Int_t c)
228 {
229   AliClusterContainer *cont = GetClusterContainer(c);
230   if (cont) cont->SetClusTimeCut(min,max);
231   else AliError(Form("%s in SetClusTimeCut(...): container %d not found",GetName(),c));
232 }
233
234 //________________________________________________________________________
235 void AliAnalysisTaskEmcal::SetTrackPtCut(Double_t cut, Int_t c)
236 {
237   AliParticleContainer *cont = GetParticleContainer(c);
238   if (cont) cont->SetParticlePtCut(cut);
239   else AliError(Form("%s in SetTrackPtCut(...): container %d not found",GetName(),c));
240
241   fTrackPtCut = cut;
242 }
243
244 //________________________________________________________________________
245 void AliAnalysisTaskEmcal::SetTrackEtaLimits(Double_t min, Double_t max, Int_t c)
246 {
247   AliParticleContainer *cont = GetParticleContainer(c);
248   if (cont) cont->SetParticleEtaLimits(min,max);
249   else AliError(Form("%s in SetTrackPtCut(...): container %d not found",GetName(),c));
250 }
251
252 //________________________________________________________________________
253 void AliAnalysisTaskEmcal::SetTrackPhiLimits(Double_t min, Double_t max, Int_t c)
254 {
255   AliParticleContainer *cont = GetParticleContainer(c);
256   if (cont) cont->SetParticlePhiLimits(min,max);
257   else AliError(Form("%s in SetTrackPhiLimits(...): container %d not found",GetName(),c));
258 }
259
260 //________________________________________________________________________
261 void AliAnalysisTaskEmcal::UserCreateOutputObjects()
262 {
263   // Create user output.
264
265   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
266   if (mgr) {
267     AliVEventHandler *evhand = mgr->GetInputEventHandler();
268     if (evhand) {
269       if (evhand->InheritsFrom("AliESDInputHandler")) {
270         fIsEsd = kTRUE;
271       }
272       else {
273         fIsEsd = kFALSE;        
274       }
275     }
276     else {
277       AliError("Event handler not found!");
278     }
279   }
280   else {
281     AliError("Analysis manager not found!");
282   }  
283
284   if (!fCreateHisto)
285     return;
286
287   OpenFile(1);
288   fOutput = new TList();
289   fOutput->SetOwner();
290
291   if (fForceBeamType == kpp)
292     fNcentBins = 1;
293
294   if (!fGeneralHistograms)
295     return;
296
297   if (fIsPythia) {
298     fHistTrialsAfterSel = new TH1F("fHistTrialsAfterSel", "fHistTrialsAfterSel", 11, 0, 11);
299     fHistTrialsAfterSel->GetXaxis()->SetTitle("p_{T} hard bin");
300     fHistTrialsAfterSel->GetYaxis()->SetTitle("trials");
301     fOutput->Add(fHistTrialsAfterSel);
302     
303     fHistEventsAfterSel = new TH1F("fHistEventsAfterSel", "fHistEventsAfterSel", 11, 0, 11);
304     fHistEventsAfterSel->GetXaxis()->SetTitle("p_{T} hard bin");
305     fHistEventsAfterSel->GetYaxis()->SetTitle("total events");
306     fOutput->Add(fHistEventsAfterSel);
307
308     fHistXsectionAfterSel = new TProfile("fHistXsectionAfterSel", "fHistXsectionAfterSel", 11, 0, 11);
309     fHistXsectionAfterSel->GetXaxis()->SetTitle("p_{T} hard bin");
310     fHistXsectionAfterSel->GetYaxis()->SetTitle("xsection");
311     fOutput->Add(fHistXsectionAfterSel);
312     
313     fHistTrials = new TH1F("fHistTrials", "fHistTrials", 11, 0, 11);
314     fHistTrials->GetXaxis()->SetTitle("p_{T} hard bin");
315     fHistTrials->GetYaxis()->SetTitle("trials");
316     fOutput->Add(fHistTrials);
317
318     fHistEvents = new TH1F("fHistEvents", "fHistEvents", 11, 0, 11);
319     fHistEvents->GetXaxis()->SetTitle("p_{T} hard bin");
320     fHistEvents->GetYaxis()->SetTitle("total events");
321     fOutput->Add(fHistEvents);
322
323     fHistXsection = new TProfile("fHistXsection", "fHistXsection", 11, 0, 11);
324     fHistXsection->GetXaxis()->SetTitle("p_{T} hard bin");
325     fHistXsection->GetYaxis()->SetTitle("xsection");
326     fOutput->Add(fHistXsection);
327
328     const Int_t ptHardLo[11] = { 0, 5,11,21,36,57, 84,117,152,191,234};
329     const Int_t ptHardHi[11] = { 5,11,21,36,57,84,117,152,191,234,1000000};
330     
331     for (Int_t i = 1; i < 12; i++) {
332       fHistTrialsAfterSel->GetXaxis()->SetBinLabel(i, Form("%d-%d",ptHardLo[i-1],ptHardHi[i-1]));
333       fHistEventsAfterSel->GetXaxis()->SetBinLabel(i, Form("%d-%d",ptHardLo[i-1],ptHardHi[i-1]));
334       
335       fHistTrials->GetXaxis()->SetBinLabel(i, Form("%d-%d",ptHardLo[i-1],ptHardHi[i-1]));
336       fHistXsection->GetXaxis()->SetBinLabel(i, Form("%d-%d",ptHardLo[i-1],ptHardHi[i-1]));
337       fHistEvents->GetXaxis()->SetBinLabel(i, Form("%d-%d",ptHardLo[i-1],ptHardHi[i-1]));
338     }
339
340     fHistPtHard = new TH1F("fHistPtHard", "fHistPtHard", fNbins*2, fMinBinPt, fMaxBinPt*4);
341     fHistPtHard->GetXaxis()->SetTitle("p_{T,hard} (GeV/c)");
342     fHistPtHard->GetYaxis()->SetTitle("counts");
343     fOutput->Add(fHistPtHard);
344   }
345
346   fHistCentrality = new TH1F("fHistCentrality","Event centrality distribution", 200, 0, 100);
347   fHistCentrality->GetXaxis()->SetTitle("Centrality (%)");
348   fHistCentrality->GetYaxis()->SetTitle("counts");
349   fOutput->Add(fHistCentrality);
350
351   fHistZVertex = new TH1F("fHistZVertex","Z vertex position", 60, -30, 30);
352   fHistZVertex->GetXaxis()->SetTitle("z");
353   fHistZVertex->GetYaxis()->SetTitle("counts");
354   fOutput->Add(fHistZVertex);
355
356   fHistEventPlane = new TH1F("fHistEventPlane","Event plane", 120, -TMath::Pi(), TMath::Pi());
357   fHistEventPlane->GetXaxis()->SetTitle("event plane");
358   fHistEventPlane->GetYaxis()->SetTitle("counts");
359   fOutput->Add(fHistEventPlane);
360
361   fHistEventRejection = new TH1I("fHistEventRejection","Reasons to reject event",20,0,20);
362   fHistEventRejection->Fill("PhysSel",0);
363   fHistEventRejection->Fill("trigger",0);
364   fHistEventRejection->Fill("trigTypeSel",0);
365   fHistEventRejection->Fill("Cent",0);
366   fHistEventRejection->Fill("vertex contr.",0);
367   fHistEventRejection->Fill("Vz",0);
368   fHistEventRejection->Fill("trackInEmcal",0);
369   fHistEventRejection->Fill("minNTrack",0);
370   fHistEventRejection->Fill("VtxSel2013pA",0);
371   fHistEventRejection->Fill("PileUp",0);
372   fHistEventRejection->Fill("EvtPlane",0);
373   fHistEventRejection->Fill("SelPtHardBin",0);
374   fHistEventRejection->GetYaxis()->SetTitle("counts");
375   fOutput->Add(fHistEventRejection);
376
377   PostData(1, fOutput);
378 }
379
380 //________________________________________________________________________
381 Bool_t AliAnalysisTaskEmcal::FillGeneralHistograms()
382 {
383   if (fIsPythia) {
384     fHistEventsAfterSel->Fill(fPtHardBin, 1);
385     fHistTrialsAfterSel->Fill(fPtHardBin, fNTrials);
386     fHistXsectionAfterSel->Fill(fPtHardBin, fXsection);
387     fHistPtHard->Fill(fPtHard);
388   }
389
390   fHistCentrality->Fill(fCent);
391   fHistZVertex->Fill(fVertex[2]);
392   fHistEventPlane->Fill(fEPV0);
393
394   return kTRUE;
395 }
396
397 //________________________________________________________________________
398 void AliAnalysisTaskEmcal::UserExec(Option_t *) 
399 {
400   // Main loop, called for each event.
401
402   fMainTriggerPatch = NULL;
403
404   if (!fInitialized)
405     ExecOnce();
406
407   if (!fInitialized)
408     return;
409
410   if (!RetrieveEventObjects())
411     return;
412
413   if (!IsEventSelected()) 
414     return;
415
416   if (fGeneralHistograms && fCreateHisto) {
417     if (!FillGeneralHistograms())
418       return;
419   }
420
421   if (!Run())
422     return;
423
424   if (fCreateHisto) {
425     if (!FillHistograms())
426       return;
427   }
428     
429   if (fCreateHisto && fOutput) {
430     // information for this iteration of the UserExec in the container
431     PostData(1, fOutput);
432   }
433 }
434
435 //________________________________________________________________________
436 Bool_t AliAnalysisTaskEmcal::AcceptCluster(AliVCluster *clus, Int_t c) const
437 {
438   // Return true if cluster is accepted.
439
440   if (!clus)
441     return kFALSE;
442
443   AliClusterContainer *cont = GetClusterContainer(c);
444   if (!cont) {
445     AliError(Form("%s:Container %d not found",GetName(),c));
446     return 0;
447   }
448
449   return cont->AcceptCluster(clus);
450 }
451
452 //________________________________________________________________________
453 Bool_t AliAnalysisTaskEmcal::AcceptTrack(AliVParticle *track, Int_t c) const
454 {
455   // Return true if track is accepted.
456
457   if (!track)
458     return kFALSE;
459
460   AliParticleContainer *cont = GetParticleContainer(c);
461   if (!cont) {
462     AliError(Form("%s:Container %d not found",GetName(),c));
463     return 0;
464   }
465
466   return cont->AcceptParticle(track);
467 }
468
469 //________________________________________________________________________
470 Bool_t AliAnalysisTaskEmcal::PythiaInfoFromFile(const char* currFile, Float_t &fXsec, Float_t &fTrials, Int_t &pthard)
471 {
472   //
473   // Get the cross section and the trails either from pyxsec.root or from pysec_hists.root
474   // Get the pt hard bin from the file path
475   // This is to called in Notify and should provide the path to the AOD/ESD file
476   // (Partially copied from AliAnalysisHelperJetTasks)
477
478   TString file(currFile);  
479   fXsec = 0;
480   fTrials = 1;
481
482   if (file.Contains(".zip#")) {
483     Ssiz_t pos1 = file.Index("root_archive",12,0,TString::kExact);
484     Ssiz_t pos = file.Index("#",1,pos1,TString::kExact);
485     Ssiz_t pos2 = file.Index(".root",5,TString::kExact);
486     file.Replace(pos+1,pos2-pos1,"");
487   } else {
488     // not an archive take the basename....
489     file.ReplaceAll(gSystem->BaseName(file.Data()),"");
490   }
491   AliDebug(1,Form("File name: %s",file.Data()));
492
493   // Get the pt hard bin
494   TString strPthard(file);
495
496   strPthard.Remove(strPthard.Last('/'));
497   strPthard.Remove(strPthard.Last('/'));
498   if (strPthard.Contains("AOD")) strPthard.Remove(strPthard.Last('/'));    
499   strPthard.Remove(0,strPthard.Last('/')+1);
500   if (strPthard.IsDec()) 
501     pthard = strPthard.Atoi();
502   else 
503     AliWarning(Form("Could not extract file number from path %s", strPthard.Data()));
504
505   // problem that we cannot really test the existance of a file in a archive so we have to live with open error message from root
506   TFile *fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec.root")); 
507   
508   if (!fxsec) {
509     // next trial fetch the histgram file
510     fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec_hists.root"));
511     if (!fxsec) {
512         // not a severe condition but inciate that we have no information
513       return kFALSE;
514     } else {
515       // find the tlist we want to be independtent of the name so use the Tkey
516       TKey* key = (TKey*)fxsec->GetListOfKeys()->At(0); 
517       if (!key) {
518         fxsec->Close();
519         return kFALSE;
520       }
521       TList *list = dynamic_cast<TList*>(key->ReadObj());
522       if (!list) {
523         fxsec->Close();
524         return kFALSE;
525       }
526       fXsec = ((TProfile*)list->FindObject("h1Xsec"))->GetBinContent(1);
527       fTrials  = ((TH1F*)list->FindObject("h1Trials"))->GetBinContent(1);
528       fxsec->Close();
529     }
530   } else { // no tree pyxsec.root
531     TTree *xtree = (TTree*)fxsec->Get("Xsection");
532     if (!xtree) {
533       fxsec->Close();
534       return kFALSE;
535     }
536     UInt_t   ntrials  = 0;
537     Double_t  xsection  = 0;
538     xtree->SetBranchAddress("xsection",&xsection);
539     xtree->SetBranchAddress("ntrials",&ntrials);
540     xtree->GetEntry(0);
541     fTrials = ntrials;
542     fXsec = xsection;
543     fxsec->Close();
544   }
545   return kTRUE;
546 }
547
548 //________________________________________________________________________
549 Bool_t AliAnalysisTaskEmcal::UserNotify()
550 {
551   // Called when file changes.
552
553   if (!fIsPythia || !fGeneralHistograms || !fCreateHisto)
554     return kTRUE;
555
556   TTree *tree = AliAnalysisManager::GetAnalysisManager()->GetTree();
557   if (!tree) {
558     AliError(Form("%s - UserNotify: No current tree!",GetName()));
559     return kFALSE;
560   }
561
562   Float_t xsection    = 0;
563   Float_t trials      = 0;
564   Int_t   pthardbin   = 0;
565
566   TFile *curfile = tree->GetCurrentFile();
567   if (!curfile) {
568     AliError(Form("%s - UserNotify: No current file!",GetName()));
569     return kFALSE;
570   }
571
572   TChain *chain = dynamic_cast<TChain*>(tree);
573   if (chain) tree = chain->GetTree();
574
575   Int_t nevents = tree->GetEntriesFast();
576
577   PythiaInfoFromFile(curfile->GetName(), xsection, trials, pthardbin);
578
579   // TODO: Workaround
580   if ((pthardbin < 0) || (pthardbin > 10)) pthardbin = 0;
581
582   fHistTrials->Fill(pthardbin, trials);
583   fHistXsection->Fill(pthardbin, xsection);
584   fHistEvents->Fill(pthardbin, nevents);
585
586   return kTRUE;
587 }
588
589 //________________________________________________________________________
590 void AliAnalysisTaskEmcal::ExecOnce()
591 {
592   // Init the analysis.
593
594   if (!InputEvent()) {
595     AliError(Form("%s: Could not retrieve event! Returning!", GetName()));
596     return;
597   }
598
599   if (fNeedEmcalGeom) {
600     fGeom = AliEMCALGeometry::GetInstance();
601     if (!fGeom) {
602       AliError(Form("%s: Can not create geometry", GetName()));
603       return;
604     }
605   }
606
607   
608   if (fEventPlaneVsEmcal >= 0) {
609     if (fGeom) {
610       Double_t ep = (fGeom->GetArm1PhiMax() + fGeom->GetArm1PhiMin()) / 2 * TMath::DegToRad() + fEventPlaneVsEmcal - TMath::Pi();
611       fMinEventPlane = ep - TMath::Pi() / 4;
612       fMaxEventPlane = ep + TMath::Pi() / 4;
613     }
614     else {
615       AliWarning("Could not set event plane limits because EMCal geometry was not loaded!");
616     }
617   }
618
619   //Load all requested track branches - each container knows name already
620   for (Int_t i =0; i<fParticleCollArray.GetEntriesFast(); i++) {
621     AliParticleContainer *cont = static_cast<AliParticleContainer*>(fParticleCollArray.At(i));
622     cont->SetArray(InputEvent());
623   }
624
625   if (fParticleCollArray.GetEntriesFast()>0) {
626     fTracks = GetParticleArray(0);
627     if (!fTracks) {
628       AliError(Form("%s: Could not retrieve first track branch!", GetName()));
629       return;
630     }
631   }
632
633   //Load all requested cluster branches - each container knows name already
634   for (Int_t i =0; i<fClusterCollArray.GetEntriesFast(); i++) {
635     AliClusterContainer *cont = static_cast<AliClusterContainer*>(fClusterCollArray.At(i));
636     cont->SetArray(InputEvent());
637   }
638
639   if (fClusterCollArray.GetEntriesFast()>0) {
640     fCaloClusters = GetClusterArray(0);
641     if (!fCaloClusters) {
642       AliError(Form("%s: Could not retrieve first cluster branch!", GetName()));
643       return;
644     }
645   }
646
647   if (!fCaloCellsName.IsNull() && !fCaloCells) {
648     fCaloCells =  dynamic_cast<AliVCaloCells*>(InputEvent()->FindListObject(fCaloCellsName));
649     if (!fCaloCells) {
650       AliError(Form("%s: Could not retrieve cells %s!", GetName(), fCaloCellsName.Data())); 
651       return;
652     }
653   }
654
655   if (!fCaloTriggersName.IsNull() && !fCaloTriggers) {
656     fCaloTriggers =  dynamic_cast<AliVCaloTrigger*>(InputEvent()->FindListObject(fCaloTriggersName));
657     if (!fCaloTriggers) {
658       AliError(Form("%s: Could not retrieve calo triggers %s!", GetName(), fCaloTriggersName.Data())); 
659       return;
660     }
661   }
662
663   if (!fCaloTriggerPatchInfoName.IsNull() && !fTriggerPatchInfo) {
664     fTriggerPatchInfo = GetArrayFromEvent(fCaloTriggerPatchInfoName.Data(),"AliEmcalTriggerPatchInfo");
665     if (!fTriggerPatchInfo) {
666       AliError(Form("%s: Could not retrieve calo trigger patch info %s!", GetName(), fCaloTriggerPatchInfoName.Data())); 
667       return;
668     }
669
670   }
671
672   fInitialized = kTRUE;
673 }
674
675 //_____________________________________________________
676 AliAnalysisTaskEmcal::BeamType AliAnalysisTaskEmcal::GetBeamType()
677 {
678   // Get beam type : pp-AA-pA
679   // ESDs have it directly, AODs get it from hardcoded run number ranges
680
681   if (fForceBeamType != kNA)
682     return fForceBeamType;
683
684   AliESDEvent *esd = dynamic_cast<AliESDEvent*>(InputEvent());
685   if (esd) {
686     const AliESDRun *run = esd->GetESDRun();
687     TString beamType = run->GetBeamType();
688     if (beamType == "p-p")
689       return kpp;
690     else if (beamType == "A-A")
691       return kAA;
692     else if (beamType == "p-A")
693       return kpA;
694     else
695       return kNA;
696   } else {
697     Int_t runNumber = InputEvent()->GetRunNumber();
698     if ((runNumber >= 136851 && runNumber <= 139517) ||  // LHC10h
699         (runNumber >= 166529 && runNumber <= 170593)) {  // LHC11h
700       return kAA;
701     } else if ((runNumber>=188365 && runNumber <= 188366) ||   // LHC12g
702                (runNumber >= 195344 && runNumber <= 196608)) { // LHC13b-f
703       return kpA;
704     } else {
705       return kpp;
706     }
707   }  
708 }
709
710 //_____________________________________________________
711 AliAnalysisTaskEmcal::TriggerType AliAnalysisTaskEmcal::GetTriggerType()
712 {
713   // Get trigger type: kND, kJ1, kJ2
714  
715   if (!fTriggerPatchInfo)
716     return kND;
717   
718   //number of patches in event
719   Int_t nPatch = fTriggerPatchInfo->GetEntries();
720
721   //loop over patches to define trigger type of event
722   Int_t nJ1 = 0;
723   Int_t nJ2 = 0;
724   AliEmcalTriggerPatchInfo *patch;
725   for (Int_t iPatch = 0; iPatch < nPatch; iPatch++) {
726     patch = (AliEmcalTriggerPatchInfo*)fTriggerPatchInfo->At( iPatch );
727     if (patch->IsJetHigh()) nJ1++;
728     if (patch->IsJetLow())  nJ2++;
729   }
730
731   if (nJ1>0) 
732     return kJ1;
733   else if (nJ2>0)
734     return kJ2;
735   else
736     return kND;
737 }
738
739 //________________________________________________________________________
740 Bool_t AliAnalysisTaskEmcal::IsEventSelected()
741 {
742   // Check if event is selected
743
744   if (fOffTrigger != AliVEvent::kAny) {
745     UInt_t res = 0;
746     const AliESDEvent *eev = dynamic_cast<const AliESDEvent*>(InputEvent());
747     if (eev) {
748       res = ((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected();
749     } else {
750       const AliAODEvent *aev = dynamic_cast<const AliAODEvent*>(InputEvent());
751       if (aev) {
752         res = aev->GetHeader()->GetOfflineTrigger();
753       }
754     }
755     if ((res & fOffTrigger) == 0) {
756       if (fGeneralHistograms) 
757         fHistEventRejection->Fill("PhysSel",1);
758       return kFALSE;
759     }
760   }
761
762   if (!fTrigClass.IsNull()) {
763     TString fired;
764     const AliESDEvent *eev = dynamic_cast<const AliESDEvent*>(InputEvent());
765     if (eev) {
766       fired = eev->GetFiredTriggerClasses();
767     } else {
768       const AliAODEvent *aev = dynamic_cast<const AliAODEvent*>(InputEvent());
769       if (aev) {
770         fired = aev->GetFiredTriggerClasses();
771       }
772     }
773     if (!fired.Contains("-B-")) {
774       if (fGeneralHistograms) 
775         fHistEventRejection->Fill("trigger",1);
776       return kFALSE;
777     }
778     TObjArray *arr = fTrigClass.Tokenize("|");
779     if (!arr) {
780       if (fGeneralHistograms) 
781         fHistEventRejection->Fill("trigger",1);
782       return kFALSE;
783     }
784     Bool_t match = 0;
785     for (Int_t i=0;i<arr->GetEntriesFast();++i) {
786       TObject *obj = arr->At(i);
787       if (!obj)
788         continue;
789       if (fired.Contains(obj->GetName())) {
790         match = 1;
791         break;
792       }
793     }
794     delete arr;
795     if (!match) {
796       if (fGeneralHistograms) 
797         fHistEventRejection->Fill("trigger",1);
798       return kFALSE;
799     }
800   }
801
802   if (fTriggerTypeSel != kND) {
803     if (fTriggerType != fTriggerTypeSel) {
804       if (fGeneralHistograms) 
805         fHistEventRejection->Fill("trigTypeSel",1);
806       return kFALSE;
807     }
808   }
809
810   if ((fMinCent != -999) && (fMaxCent != -999)) {
811     if (fCent<fMinCent || fCent>fMaxCent) {
812       if (fGeneralHistograms) 
813         fHistEventRejection->Fill("Cent",1);
814       return kFALSE;
815     }
816   }
817
818   if (fUseAliAnaUtils) {
819     if (!fAliAnalysisUtils)
820       fAliAnalysisUtils = new AliAnalysisUtils();
821     fAliAnalysisUtils->SetMinVtxContr(2);
822     fAliAnalysisUtils->SetMaxVtxZ(999);
823     if(fMinVz<-10.) fMinVz = -10.; 
824     if(fMinVz>10.)  fMaxVz = 10.;
825
826     if (!fAliAnalysisUtils->IsVertexSelected2013pA(InputEvent())) {
827       if (fGeneralHistograms) 
828         fHistEventRejection->Fill("VtxSel2013pA",1);
829       return kFALSE;
830     }
831
832     if (fAliAnalysisUtils->IsPileUpEvent(InputEvent())) {
833       fHistEventRejection->Fill("PileUp",1);
834       return kFALSE;
835     }
836   }
837
838   if ((fMinVz != -999) && (fMaxVz != -999)) {
839     if (fNVertCont == 0 ) {
840       if (fGeneralHistograms) 
841         fHistEventRejection->Fill("vertex contr.",1);
842       return kFALSE;
843     }
844     Double_t vz = fVertex[2];
845     if (vz<fMinVz || vz>fMaxVz) {
846       if (fGeneralHistograms) 
847         fHistEventRejection->Fill("Vz",1);
848       return kFALSE;
849     }
850   }
851
852   if (fMinPtTrackInEmcal > 0 && fGeom) {
853     Bool_t trackInEmcalOk = kFALSE;
854     Int_t ntracks = GetNParticles(0);
855     for (Int_t i = 0; i < ntracks; i++) {
856       AliVParticle *track = GetAcceptParticleFromArray(i,0);
857       if (!track)
858         continue;
859
860       Double_t phiMin = fGeom->GetArm1PhiMin() * TMath::DegToRad();
861       Double_t phiMax = fGeom->GetArm1PhiMax() * TMath::DegToRad();
862       Int_t runNumber = InputEvent()->GetRunNumber();
863       if (runNumber>=177295 && runNumber<=197470) { //small SM masked in 2012 and 2013
864         phiMin = 1.4;   
865         phiMax = TMath::Pi();
866       }
867
868       if (track->Eta() < fGeom->GetArm1EtaMin() || track->Eta() > fGeom->GetArm1EtaMax() || track->Phi() < phiMin || track->Phi() > phiMax)
869         continue;
870       if (track->Pt() > fMinPtTrackInEmcal) {
871         trackInEmcalOk = kTRUE;
872         break;
873       }
874     }
875     if (!trackInEmcalOk) {
876       if (fGeneralHistograms) 
877         fHistEventRejection->Fill("trackInEmcal",1);
878       return kFALSE;
879     }
880   }
881
882   if (fMinNTrack > 0) {
883     Int_t nTracksAcc = 0;
884     Int_t ntracks = GetNParticles(0);
885     for (Int_t i = 0; i < ntracks; i++) {
886       AliVParticle *track = GetAcceptParticleFromArray(i,0);
887       if (!track)
888         continue;
889       if (track->Pt() > fTrackPtCut) {
890         nTracksAcc++;
891         if (nTracksAcc>=fMinNTrack)
892           break;
893       }
894     }
895     if (nTracksAcc<fMinNTrack) {
896       if (fGeneralHistograms) 
897         fHistEventRejection->Fill("minNTrack",1);
898       return kFALSE;
899     }
900   }
901
902   if (!(fEPV0 > fMinEventPlane && fEPV0 <= fMaxEventPlane) &&
903       !(fEPV0 + TMath::Pi() > fMinEventPlane && fEPV0 + TMath::Pi() <= fMaxEventPlane) &&
904       !(fEPV0 - TMath::Pi() > fMinEventPlane && fEPV0 - TMath::Pi() <= fMaxEventPlane)) 
905     {
906       if (fGeneralHistograms) 
907         fHistEventRejection->Fill("EvtPlane",1);
908       return kFALSE;
909     }
910
911   if (fSelectPtHardBin != -999 && fSelectPtHardBin != fPtHardBin)  {
912       if (fGeneralHistograms) 
913         fHistEventRejection->Fill("SelPtHardBin",1);
914       return kFALSE;
915     }
916
917   return kTRUE;
918 }
919
920 //________________________________________________________________________
921 TClonesArray *AliAnalysisTaskEmcal::GetArrayFromEvent(const char *name, const char *clname)
922 {
923   // Get array from event.
924
925   TClonesArray *arr = 0;
926   TString sname(name);
927   if (!sname.IsNull()) {
928     arr = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(sname));
929     if (!arr) {
930       AliWarning(Form("%s: Could not retrieve array with name %s!", GetName(), name)); 
931       return 0;
932     }
933   } else {
934     return 0;
935   }
936
937   if (!clname)
938     return arr;
939
940   TString objname(arr->GetClass()->GetName());
941   TClass cls(objname);
942   if (!cls.InheritsFrom(clname)) {
943     AliWarning(Form("%s: Objects of type %s in %s are not inherited from %s!", 
944                     GetName(), cls.GetName(), name, clname)); 
945     return 0;
946   }
947   return arr;
948 }
949
950 //________________________________________________________________________
951 Bool_t AliAnalysisTaskEmcal::RetrieveEventObjects()
952 {
953   // Retrieve objects from event.
954
955   fVertex[0] = 0;
956   fVertex[1] = 0;
957   fVertex[2] = 0;
958   fNVertCont = 0;
959
960   const AliVVertex *vert = InputEvent()->GetPrimaryVertex();
961   if (vert) {
962     vert->GetXYZ(fVertex);
963     fNVertCont = vert->GetNContributors();
964   }
965
966   fBeamType = GetBeamType();
967
968   if (fBeamType == kAA || fBeamType == kpA ) {
969     AliCentrality *aliCent = InputEvent()->GetCentrality();
970     if (aliCent) {
971       fCent = aliCent->GetCentralityPercentile(fCentEst.Data());
972       if (fNcentBins==4) {
973         if      (fCent >=  0 && fCent <   10) fCentBin = 0;
974         else if (fCent >= 10 && fCent <   30) fCentBin = 1;
975         else if (fCent >= 30 && fCent <   50) fCentBin = 2;
976         else if (fCent >= 50 && fCent <= 100) fCentBin = 3; 
977         else {
978           AliWarning(Form("%s: Negative centrality: %f. Assuming 99", GetName(), fCent));
979           fCentBin = fNcentBins-1;
980         }
981       } else {
982         Double_t centWidth = (fMaxCent-fMinCent)/(Double_t)fNcentBins;
983         if(centWidth>0.)
984           fCentBin = TMath::FloorNint(fCent/centWidth);
985         else 
986           fCentBin = 0;
987         if (fCentBin>=fNcentBins) {
988           AliWarning(Form("%s: fCentBin too large: cent = %f fCentBin = %d. Assuming 99", GetName(),fCent,fCentBin));
989           fCentBin = fNcentBins-1;
990         }
991       }
992     } else {
993       AliWarning(Form("%s: Could not retrieve centrality information! Assuming 99", GetName()));
994       fCentBin = 3;
995     }
996     AliEventplane *aliEP = InputEvent()->GetEventplane();
997     if (aliEP) {
998       fEPV0  = aliEP->GetEventplane("V0" ,InputEvent());
999       fEPV0A = aliEP->GetEventplane("V0A",InputEvent());
1000       fEPV0C = aliEP->GetEventplane("V0C",InputEvent());
1001     } else {
1002       AliWarning(Form("%s: Could not retrieve event plane information!", GetName()));
1003     }
1004   } else {
1005     fCent = 99;
1006     fCentBin = 0;
1007   }
1008
1009   if (fIsPythia) {
1010
1011     if (MCEvent()) {
1012       fPythiaHeader = dynamic_cast<AliGenPythiaEventHeader*>(MCEvent()->GenEventHeader());
1013       if (!fPythiaHeader) {
1014         // Check if AOD
1015         AliAODMCHeader* aodMCH = dynamic_cast<AliAODMCHeader*>(InputEvent()->FindListObject(AliAODMCHeader::StdBranchName()));
1016
1017         if (aodMCH) {
1018           for (UInt_t i = 0;i<aodMCH->GetNCocktailHeaders();i++) {
1019             fPythiaHeader = dynamic_cast<AliGenPythiaEventHeader*>(aodMCH->GetCocktailHeader(i));
1020             if (fPythiaHeader) break;
1021           }
1022         }
1023       }
1024     }
1025
1026     if (fPythiaHeader) {
1027       fPtHard = fPythiaHeader->GetPtHard();
1028
1029       const Int_t ptHardLo[11] = { 0, 5,11,21,36,57, 84,117,152,191,234};
1030       const Int_t ptHardHi[11] = { 5,11,21,36,57,84,117,152,191,234,1000000};
1031       for (fPtHardBin = 0; fPtHardBin < 11; fPtHardBin++) {
1032         if (fPtHard >= ptHardLo[fPtHardBin] && fPtHard < ptHardHi[fPtHardBin])
1033           break;
1034       }
1035
1036       fXsection = fPythiaHeader->GetXsection();
1037       fNTrials = fPythiaHeader->Trials();
1038     }
1039   }
1040
1041   fTriggerType = GetTriggerType();
1042
1043   return kTRUE;
1044 }
1045
1046 //________________________________________________________________________
1047 AliParticleContainer* AliAnalysisTaskEmcal::AddParticleContainer(const char *n) 
1048 {
1049   // Add particle container
1050   // will be called in AddTask macro
1051
1052   TString tmp = TString(n);
1053   if (tmp.IsNull()) return 0;
1054
1055   AliParticleContainer *cont = 0x0;
1056   cont = new AliParticleContainer();
1057   cont->SetArrayName(n);
1058   TString contName = cont->GetArrayName();
1059  
1060   fParticleCollArray.Add(cont);
1061
1062   return cont;
1063 }
1064
1065 //________________________________________________________________________
1066 AliClusterContainer* AliAnalysisTaskEmcal::AddClusterContainer(const char *n) 
1067 {
1068   // Add cluster container
1069   // will be called in AddTask macro
1070
1071   TString tmp = TString(n);
1072   if (tmp.IsNull()) return 0;
1073
1074   AliClusterContainer *cont = 0x0;
1075   cont = new AliClusterContainer();
1076   cont->SetArrayName(n);
1077
1078   fClusterCollArray.Add(cont);
1079
1080   return cont;
1081 }
1082
1083 //________________________________________________________________________
1084 AliParticleContainer* AliAnalysisTaskEmcal::GetParticleContainer(Int_t i) const 
1085 {
1086   // Get i^th particle container
1087
1088   if (i<0 || i>fParticleCollArray.GetEntriesFast()) return 0;
1089   AliParticleContainer *cont = static_cast<AliParticleContainer*>(fParticleCollArray.At(i));
1090   return cont;
1091 }
1092
1093 //________________________________________________________________________
1094 AliClusterContainer* AliAnalysisTaskEmcal::GetClusterContainer(Int_t i) const 
1095 {
1096   // Get i^th cluster container
1097
1098   if (i<0 || i>fClusterCollArray.GetEntriesFast()) return 0;
1099   AliClusterContainer *cont = static_cast<AliClusterContainer*>(fClusterCollArray.At(i));
1100   return cont;
1101 }
1102
1103 //________________________________________________________________________
1104 AliParticleContainer* AliAnalysisTaskEmcal::GetParticleContainer(const char *name) const 
1105 {
1106   // Get particle container with name
1107
1108   AliParticleContainer *cont = static_cast<AliParticleContainer*>(fParticleCollArray.FindObject(name));
1109   return cont;
1110 }
1111
1112 //________________________________________________________________________
1113 AliClusterContainer* AliAnalysisTaskEmcal::GetClusterContainer(const char *name) const 
1114 {
1115   // Get cluster container with name
1116
1117   AliClusterContainer *cont = static_cast<AliClusterContainer*>(fClusterCollArray.FindObject(name));
1118   return cont;
1119 }
1120
1121 //________________________________________________________________________
1122 TClonesArray* AliAnalysisTaskEmcal::GetParticleArray(Int_t i) const 
1123 {
1124   // Get i^th TClonesArray with AliVParticle
1125
1126   AliParticleContainer *cont = GetParticleContainer(i);
1127   if (!cont) {
1128     AliError(Form("%s: Particle container %d not found",GetName(),i));
1129     return 0;
1130   }
1131   TString contName = cont->GetArrayName();
1132   return cont->GetArray();
1133 }
1134
1135 //________________________________________________________________________
1136 TClonesArray* AliAnalysisTaskEmcal::GetClusterArray(Int_t i) const 
1137 {
1138   // Get i^th TClonesArray with AliVCluster
1139
1140   AliClusterContainer *cont = GetClusterContainer(i);
1141   if (!cont) {
1142     AliError(Form("%s:Cluster container %d not found",GetName(),i));
1143     return 0;
1144   }
1145   return cont->GetArray();
1146 }
1147
1148 //________________________________________________________________________
1149 AliVParticle* AliAnalysisTaskEmcal::GetAcceptParticleFromArray(Int_t p, Int_t c) const 
1150 {
1151   // Get particle p if accepted from  container c
1152   // If particle not accepted return 0
1153
1154   AliParticleContainer *cont = GetParticleContainer(c);
1155   if (!cont) {
1156     AliError(Form("%s: Particle container %d not found",GetName(),c));
1157     return 0;
1158   }
1159   AliVParticle *vp = cont->GetAcceptParticle(p);
1160
1161   return vp;
1162 }
1163
1164 //________________________________________________________________________
1165 AliVCluster* AliAnalysisTaskEmcal::GetAcceptClusterFromArray(Int_t cl, Int_t c) const 
1166 {
1167   // Get particle p if accepted from  container c
1168   // If particle not accepted return 0
1169
1170   AliClusterContainer *cont = GetClusterContainer(c);
1171   if (!cont) {
1172     AliError(Form("%s: Cluster container %d not found",GetName(),c));
1173     return 0;
1174   }
1175   AliVCluster *vc = cont->GetAcceptCluster(cl);
1176
1177   return vc;
1178 }
1179
1180 //________________________________________________________________________
1181 Int_t AliAnalysisTaskEmcal::GetNParticles(Int_t i) const 
1182 {
1183   // Get number of entries in particle array i
1184
1185   AliParticleContainer *cont = GetParticleContainer(i);
1186   if (!cont) {
1187     AliError(Form("%s: Particle container %d not found",GetName(),i));
1188     return 0;
1189   }
1190   return cont->GetNEntries();
1191 }
1192
1193 //________________________________________________________________________
1194 Int_t AliAnalysisTaskEmcal::GetNClusters(Int_t i) const 
1195 {
1196   // Get number of entries in cluster array i
1197
1198   AliClusterContainer *cont = GetClusterContainer(i);
1199   if (!cont) {
1200     AliError(Form("%s: Cluster container %d not found",GetName(),i));
1201     return 0;
1202   }
1203   return cont->GetNEntries();
1204 }
1205
1206 //________________________________________________________________________
1207 AliEmcalTriggerPatchInfo* AliAnalysisTaskEmcal::GetMainTriggerPatch() 
1208 {
1209   //get main trigger match; if not known yet, look for it and cache
1210
1211   if (fMainTriggerPatch) 
1212     return fMainTriggerPatch;
1213
1214   if (!fTriggerPatchInfo) {
1215     AliError(Form("%s: fTriggerPatchInfo not available",GetName()));
1216     return 0;
1217   }
1218
1219   //number of patches in event
1220   Int_t nPatch = fTriggerPatchInfo->GetEntries();
1221
1222   //extract main trigger patch
1223   AliEmcalTriggerPatchInfo *patch;
1224   for (Int_t iPatch = 0; iPatch < nPatch; iPatch++) {
1225     
1226     patch = (AliEmcalTriggerPatchInfo*)fTriggerPatchInfo->At( iPatch );
1227     if (patch->IsMainTrigger()) {
1228       fMainTriggerPatch = patch;
1229       break;
1230     }
1231   }
1232
1233   return fMainTriggerPatch;
1234 }
1235
1236 //________________________________________________________________________
1237 void AliAnalysisTaskEmcal::AddObjectToEvent(TObject *obj)
1238 {
1239   // Add object to event
1240
1241   if (!(InputEvent()->FindListObject(obj->GetName()))) {
1242     InputEvent()->AddObject(obj);
1243   } else {
1244     AliFatal(Form("%s: Container with name %s already present. Aborting", GetName(), obj->GetName()));
1245   }
1246 }
1247
1248 //________________________________________________________________________
1249 void AliAnalysisTaskEmcal::GenerateFixedBinArray(Int_t n, Double_t min, Double_t max, Double_t* array) const
1250 {
1251   Double_t binWidth = (max-min)/n;
1252   array[0] = min;
1253   for (Int_t i = 1; i <= n; i++) {
1254     array[i] = array[i-1]+binWidth;
1255   }
1256 }
1257
1258 //________________________________________________________________________
1259 Double_t* AliAnalysisTaskEmcal::GenerateFixedBinArray(Int_t n, Double_t min, Double_t max) const
1260 {
1261   Double_t *array = new Double_t[n+1];
1262
1263   GenerateFixedBinArray(n, min, max, array);
1264
1265   return array;
1266 }