Vc sources now use completely namespaced includes to work around messed up MacOS X
[u/mrichter/AliRoot.git] / PWGGA / EMCALJetTasks / AliEmcalJetTask.cxx
CommitLineData
48d874ff 1// $Id$
542ac102 2//
3// Emcal jet finder task.
4//
b65fac7a 5// Authors: C.Loizides, S.Aiola
48d874ff 6
c64cb1f6 7#include <vector>
04905231 8#include "AliEmcalJetTask.h"
9
6ea168d0 10#include <TChain.h>
48d874ff 11#include <TClonesArray.h>
6ea168d0 12#include <TList.h>
13#include <TLorentzVector.h>
b65fac7a 14
48d874ff 15#include "AliAnalysisManager.h"
6ea168d0 16#include "AliCentrality.h"
f5f3c8e9 17#include "AliEMCALGeometry.h"
04905231 18#include "AliESDEvent.h"
f5f3c8e9 19#include "AliEmcalJet.h"
04905231 20#include "AliEmcalParticle.h"
63206072 21#include "AliFJWrapper.h"
04905231 22#include "AliMCEvent.h"
35789a2d 23#include "AliVCluster.h"
b65fac7a 24#include "AliVEvent.h"
04905231 25#include "AliVParticle.h"
48d874ff 26
914d486c 27ClassImp(AliEmcalJetTask)
48d874ff 28
48d874ff 29//________________________________________________________________________
914d486c 30AliEmcalJetTask::AliEmcalJetTask() :
31 AliAnalysisTaskSE("AliEmcalJetTask"),
6ea168d0 32 fTracksName("Tracks"),
33 fCaloName("CaloClusters"),
34 fJetsName("Jets"),
35 fAlgo(1),
36 fRadius(0.4),
37 fType(0),
914d486c 38 fMinJetTrackPt(0.15),
39 fMinJetClusPt(0.15),
04905231 40 fPhiMin(0),
41 fPhiMax(TMath::TwoPi()),
42 fEtaMin(-1),
43 fEtaMax(1),
f5f3c8e9 44 fMinJetArea(0.01),
45 fMinJetPt(1.0),
04905231 46 fIsInit(0),
47 fIsMcPart(0),
48 fIsEmcPart(0),
b65fac7a 49 fJets(0),
04905231 50 fEvent(0),
51 fTracks(0),
52 fClus(0)
6ea168d0 53{
54 // Default constructor.
6ea168d0 55}
56
57//________________________________________________________________________
914d486c 58AliEmcalJetTask::AliEmcalJetTask(const char *name) :
63206072 59 AliAnalysisTaskSE(name),
7efbea04 60 fTracksName("Tracks"),
48d874ff 61 fCaloName("CaloClusters"),
7efbea04 62 fJetsName("Jets"),
48d874ff 63 fAlgo(1),
64 fRadius(0.4),
65 fType(0),
6ea168d0 66 fMinJetTrackPt(0.15),
914d486c 67 fMinJetClusPt(0.15),
04905231 68 fPhiMin(0),
69 fPhiMax(TMath::TwoPi()),
70 fEtaMin(-1),
71 fEtaMax(1),
f5f3c8e9 72 fMinJetArea(0.01),
73 fMinJetPt(1.0),
04905231 74 fIsInit(0),
75 fIsMcPart(0),
76 fIsEmcPart(0),
b65fac7a 77 fJets(0),
04905231 78 fEvent(0),
79 fTracks(0),
80 fClus(0)
48d874ff 81{
82 // Standard constructor.
7efbea04 83
7efbea04 84 fBranchNames="ESD:AliESDRun.,AliESDHeader.,PrimaryVertex.";
48d874ff 85}
86
87//________________________________________________________________________
914d486c 88AliEmcalJetTask::~AliEmcalJetTask()
48d874ff 89{
90 // Destructor
91}
92
93//________________________________________________________________________
914d486c 94void AliEmcalJetTask::UserCreateOutputObjects()
48d874ff 95{
96 // Create user objects.
97
914d486c 98 fJets = new TClonesArray("AliEmcalJet");
48d874ff 99 fJets->SetName(fJetsName);
48d874ff 100}
101
102//________________________________________________________________________
914d486c 103void AliEmcalJetTask::UserExec(Option_t *)
48d874ff 104{
105 // Main loop, called for each event.
106
04905231 107 if (!fIsInit) {
108 if (!DoInit())
109 return;
110 fIsInit = kTRUE;
1772fe65 111 }
b65fac7a 112
04905231 113 FindJets();
48d874ff 114}
115
116//________________________________________________________________________
914d486c 117void AliEmcalJetTask::Terminate(Option_t *)
48d874ff 118{
119 // Called once at the end of the analysis.
48d874ff 120}
121
122//________________________________________________________________________
04905231 123void AliEmcalJetTask::FindJets()
48d874ff 124{
125 // Find jets.
126
04905231 127 if (!fTracks && !fClus)
d03084cd 128 return;
129
48d874ff 130 TString name("kt");
131 fastjet::JetAlgorithm jalgo(fastjet::kt_algorithm);
04905231 132 if (fAlgo>=1) {
48d874ff 133 name = "antikt";
134 jalgo = fastjet::antikt_algorithm;
135 }
136
04905231 137 // setup fj wrapper
48d874ff 138 AliFJWrapper fjw(name, name);
f5f3c8e9 139 fjw.SetAreaType(fastjet::active_area_explicit_ghosts);
04905231 140 fjw.SetR(fRadius);
6ea168d0 141 fjw.SetAlgorithm(jalgo);
04905231 142 fjw.SetMaxRap(1);
48d874ff 143 fjw.Clear();
144
04905231 145 // get primary vertex
146 Double_t vertex[3] = {0, 0, 0};
147 fEvent->GetPrimaryVertex()->GetXYZ(vertex);
148
149 if (fTracks) {
150 const Int_t Ntracks = fTracks->GetEntries();
48d874ff 151 for (Int_t iTracks = 0; iTracks < Ntracks; ++iTracks) {
04905231 152 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(iTracks));
48d874ff 153 if (!t)
154 continue;
1772fe65 155 if (t->Pt() < fMinJetTrackPt)
914d486c 156 continue;
04905231 157 Double_t eta = t->Eta();
158 Double_t phi = t->Phi();
159 if ((eta<fEtaMin) && (eta>fEtaMax) &&
160 (phi<fPhiMin) && (phi<fPhiMax))
161 continue;
a3c8c8c8 162 // offset of 100 for consistency with cluster ids
163 fjw.AddInputVector(t->Px(), t->Py(), t->Pz(), t->P(), iTracks + 100);
48d874ff 164 }
165 }
6ea168d0 166
04905231 167 if (fClus) {
168 if (fIsEmcPart) {
169 const Int_t Nclus = fClus->GetEntries();
170 for (Int_t iClus = 0; iClus < Nclus; ++iClus) {
171 AliVCluster *c = 0;
172 Double_t cEta=0,cPhi=0,cPt=0;
173 Double_t cPx=0,cPy=0,cPz=0;
174 if (fIsEmcPart) {
175 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(iClus));
176 if (!ep)
177 continue;
178 c = ep->GetCluster();
179 if (!c)
180 continue;
181 cEta = ep->Eta();
182 cPhi = ep->Phi();
183 cPt = ep->Pt();
184 cPx = ep->Px();
185 cPy = ep->Py();
186 cPz = ep->Pz();
187 } else {
188 c = static_cast<AliVCluster*>(fClus->At(iClus));
189 if (!c)
190 continue;
191 TLorentzVector nP;
192 c->GetMomentum(nP, vertex);
193 cEta = nP.Eta();
194 cPhi = nP.Phi();
195 cPt = nP.Pt();
196 cPx = nP.Px();
197 cPy = nP.Py();
198 cPz = nP.Pz();
199 }
200 if (!c->IsEMCAL())
201 continue;
202 if (cPt < fMinJetClusPt)
203 continue;
204 if ((cEta<fEtaMin) && (cEta>fEtaMax) &&
205 (cPhi<fPhiMin) && (cPhi<fPhiMax))
206 continue;
207 // offset of 100 to skip ghost particles uid = -1
208 fjw.AddInputVector(cPx, cPy, cPz, TMath::Sqrt(cPx*cPx+cPy*cPy+cPz*cPz), -iClus - 100);
209 }
210 } else { /*CaloClusters given as input*/
211 const Int_t Nclus = fClus->GetEntries();
212 for (Int_t iClus = 0; iClus < Nclus; ++iClus) {
213 AliVCluster *c = static_cast<AliVCluster*>(fClus->At(iClus));
214 if (!c)
215 continue;
216 if (!c->IsEMCAL())
217 continue;
218 TLorentzVector nPart;
219 c->GetMomentum(nPart, vertex);
220 if (nPart.Pt() < fMinJetClusPt)
221 continue;
222 // offset of 100 to skip ghost particles uid = -1
223 fjw.AddInputVector(nPart.Px(), nPart.Py(), nPart.Pz(), nPart.P(), -iClus - 100);
224 }
48d874ff 225 }
226 }
04905231 227
7efbea04 228 // run jet finder
48d874ff 229 fjw.Run();
f5f3c8e9 230
231 // get geometry
232 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
233 if (!geom) {
02c7e943 234 AliFatal(Form("%s: Can not create geometry", GetName()));
f5f3c8e9 235 return;
236 }
04905231 237
238 // loop over fastjet jets
48d874ff 239 std::vector<fastjet::PseudoJet> jets_incl = fjw.GetInclusiveJets();
b65fac7a 240 for (UInt_t ij=0, jetCount=0; ij<jets_incl.size(); ++ij) {
f5f3c8e9 241 if (jets_incl[ij].perp()<fMinJetPt)
242 continue;
243 if (fjw.GetJetArea(ij)<fMinJetArea)
48d874ff 244 continue;
04905231 245
914d486c 246 AliEmcalJet *jet = new ((*fJets)[jetCount])
247 AliEmcalJet(jets_incl[ij].perp(), jets_incl[ij].eta(), jets_incl[ij].phi(), jets_incl[ij].m());
04905231 248
249 // loop over constituents
c64cb1f6 250 std::vector<fastjet::PseudoJet> constituents(fjw.GetJetConstituents(ij));
04905231 251 jet->SetNumberOfTracks(constituents.size());
252 jet->SetNumberOfClusters(constituents.size());
253
b65fac7a 254 Int_t nt = 0;
255 Int_t nc = 0;
f5f3c8e9 256 Double_t neutralE = 0;
111318e8 257 Double_t maxCh = 0;
258 Double_t maxNe = 0;
f5f3c8e9 259 Int_t gall = 0;
260 Int_t gemc = 0;
04905231 261 Int_t cemc = 0;
111318e8 262 Int_t ncharged = 0;
263 Int_t nneutral = 0;
02c7e943 264 Double_t mcpt = 0;
04905231 265 Double_t emcpt = 0;
b65fac7a 266
b65fac7a 267 for(UInt_t ic = 0; ic < constituents.size(); ++ic) {
48d874ff 268 Int_t uid = constituents[ic].user_index();
b65fac7a 269
04905231 270 if ((uid == -1) /*&& (constituents[ic].kt2() < 1e-25)*/) { //ghost particle
f5f3c8e9 271 ++gall;
04905231 272 Double_t gphi = constituents[ic].phi();
273 if (gphi<0)
274 gphi += TMath::TwoPi();
275 gphi *= TMath::RadToDeg();
f5f3c8e9 276 Double_t geta = constituents[ic].eta();
b65fac7a 277 if ((gphi > geom->GetArm1PhiMin()) && (gphi < geom->GetArm1PhiMax()) &&
278 (geta > geom->GetArm1EtaMin()) && (geta < geom->GetArm1EtaMax()))
04905231 279 ++gemc;
280 } else if ((uid > 0) && fTracks) { // track constituent
111318e8 281 Int_t tid = uid - 100;
04905231 282 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(tid));
283 if (!t)
284 continue;
285 Double_t cEta = t->Eta();
286 Double_t cPhi = t->Phi();
287 Double_t cPt = t->Pt();
288 Double_t cP = t->P();
289 if (t->Charge() == 0) {
290 neutralE += cP;
291 ++nneutral;
292 if (cPt > maxNe)
293 maxNe = cPt;
294 } else {
295 ++ncharged;
296 if (cPt > maxCh)
297 maxCh = cPt;
298 }
299
300 if (fIsMcPart || t->GetLabel() == 100) // check if MC particle
301 mcpt += cPt;
302
303 if (cPhi<0)
304 cPhi += TMath::TwoPi();
305 cPhi *= TMath::RadToDeg();
306 if ((cPhi > geom->GetArm1PhiMin()) && (cPhi < geom->GetArm1PhiMax()) &&
307 (cEta > geom->GetArm1EtaMin()) && (cEta < geom->GetArm1EtaMax())) {
308 emcpt += cPt;
309 ++cemc;
d03084cd 310 }
04905231 311
312 jet->AddTrackAt(tid, nt);
313 ++nt;
314 } else if (fClus) { // cluster constituent
111318e8 315 Int_t cid = -uid - 100;
04905231 316 AliVCluster *c = 0;
317 Double_t cEta=0,cPhi=0,cPt=0,cP=0;
318 if (fIsEmcPart) {
319 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(cid));
320 if (!ep)
321 continue;
322 c = ep->GetCluster();
323 if (!c)
324 continue;
325 cEta = ep->Eta();
326 cPhi = ep->Phi();
327 cPt = ep->Pt();
328 cP = ep->P();
329 } else {
330 c = static_cast<AliVCluster*>(fClus->At(cid));
331 if (!c)
332 continue;
333 TLorentzVector nP;
334 c->GetMomentum(nP, vertex);
335 cEta = nP.Eta();
336 cPhi = nP.Phi();
337 cPt = nP.Pt();
338 cP = nP.P();
339 }
340
341 neutralE += cP;
342 if (cPt > maxNe)
343 maxNe = cPt;
344
345 if (c->Chi2() == 100) // MC particle
346 mcpt += cPt;
347
348 if (cPhi<0)
349 cPhi += TMath::TwoPi();
350 cPhi *= TMath::RadToDeg();
351 if ((cPhi > geom->GetArm1PhiMin()) && (cPhi < geom->GetArm1PhiMax()) &&
352 (cEta > geom->GetArm1EtaMin()) && (cEta < geom->GetArm1EtaMax())) {
353 emcpt += cPt;
354 ++cemc;
355 }
356
357 jet->AddClusterAt(cid, nc);
358 ++nc;
359 ++nneutral;
02c7e943 360 } else {
361 AliError(Form("%s: No logical way to end up here.", GetName()));
362 continue;
48d874ff 363 }
04905231 364 } /* end of constituent loop */
365
35789a2d 366 jet->SetNumberOfTracks(nt);
367 jet->SetNumberOfClusters(nc);
f5f3c8e9 368 jet->SortConstituents();
111318e8 369 jet->SetMaxNeutralPt(maxNe);
370 jet->SetMaxChargedPt(maxCh);
b65fac7a 371 jet->SetNEF(neutralE / jet->E());
a88b5c99 372 fastjet::PseudoJet area(fjw.GetJetAreaVector(ij));
373 jet->SetArea(area.perp());
374 jet->SetAreaEta(area.eta());
375 jet->SetAreaPhi(area.phi());
111318e8 376 jet->SetNumberOfCharged(ncharged);
377 jet->SetNumberOfNeutrals(nneutral);
02c7e943 378 jet->SetMCPt(mcpt);
04905231 379 jet->SetNEmc(cemc);
380 jet->SetPtEmc(emcpt);
381
b65fac7a 382 if (gall > 0)
383 jet->SetAreaEmc(fjw.GetJetArea(ij) * gemc / gall);
f5f3c8e9 384 else
385 jet->SetAreaEmc(-1);
b65fac7a 386 if ((jet->Phi() > geom->GetArm1PhiMin() * TMath::DegToRad()) &&
1772fe65 387 (jet->Phi() < geom->GetArm1PhiMax() * TMath::DegToRad()) &&
388 (jet->Eta() > geom->GetArm1EtaMin()) &&
389 (jet->Eta() < geom->GetArm1EtaMax()))
f5f3c8e9 390 jet->SetAxisInEmcal(kTRUE);
48d874ff 391 jetCount++;
392 }
a88b5c99 393 fJets->Sort();
48d874ff 394}
04905231 395
396//________________________________________________________________________
397Bool_t AliEmcalJetTask::DoInit()
398{
399 // Init. Return true if successful.
400
401 // get input collections
402 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
403
404 // get the event
405 fEvent = InputEvent();
406 if (!fEvent) {
407 AliError(Form("%s: Could not retrieve event! Returning", GetName()));
408 return 0;
409 }
410
411 // add jets to event if not yet there
412 if (!(fEvent->FindListObject(fJetsName)))
413 fEvent->AddObject(fJets);
414 else {
f4b49fa6 415 AliError(Form("%s: Object with name %s already in event! Returning", GetName(), fJetsName.Data()));
04905231 416 return 0;
417 }
418
419 if ((fType == 0) || (fType == 1)) {
420 if (fTracksName == "Tracks")
421 am->LoadBranch("Tracks");
422 if (!fTracks && !fTracksName.IsNull()) {
423 fTracks = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fTracksName));
424 if (!fTracks) {
425 AliError(Form("%s: Pointer to tracks %s == 0", GetName(), fTracksName.Data()));
426 return 0;
427 }
428 }
429 TString objname(fTracks->GetClass()->GetName());
430 TClass cls(objname);
431 if (cls.InheritsFrom("AliMCParticle"))
432 fIsMcPart = 1;
433 }
434
435 if ((fType == 0) || (fType == 2)) {
436 if (fCaloName == "CaloClusters")
437 am->LoadBranch("CaloClusters");
438 if (!fClus && !fCaloName.IsNull()) {
439 fClus = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fCaloName));
440 if (!fClus) {
441 AliError(Form("%s: Pointer to clus %s == 0", GetName(), fCaloName.Data()));
442 return 0;
443 }
444 }
445 TString objname(fClus->GetClass()->GetName());
446 TClass cls(objname);
447 if (cls.InheritsFrom("AliEmcalParticle"))
448 fIsEmcPart = 1;
449 }
450
451 return 1;
452}