]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGJE/EMCALJetTasks/UserTasks/AliAnalysisTaskEmcalJetTagger.cxx
move setter to public
[u/mrichter/AliRoot.git] / PWGJE / EMCALJetTasks / UserTasks / AliAnalysisTaskEmcalJetTagger.cxx
1 //
2 // Jet tagger analysis task.
3 //
4 // Author: M.Verweij
5
6 #include <TClonesArray.h>
7 #include <TH1F.h>
8 #include <TH2F.h>
9 #include <TH3F.h>
10 #include <THnSparse.h>
11 #include <TList.h>
12 #include <TLorentzVector.h>
13 #include <TProfile.h>
14 #include <TChain.h>
15 #include <TSystem.h>
16 #include <TFile.h>
17 #include <TKey.h>
18
19 #include "AliVCluster.h"
20 #include "AliVTrack.h"
21 #include "AliEmcalJet.h"
22 #include "AliRhoParameter.h"
23 #include "AliLog.h"
24 #include "AliEmcalParticle.h"
25 #include "AliMCEvent.h"
26 #include "AliGenPythiaEventHeader.h"
27 #include "AliAODMCHeader.h"
28 #include "AliMCEvent.h"
29 #include "AliAnalysisManager.h"
30 #include "AliJetContainer.h"
31
32 #include "AliAODEvent.h"
33 #include "AliESDEvent.h"
34
35 #include "AliAnalysisTaskEmcalJetTagger.h"
36
37 ClassImp(AliAnalysisTaskEmcalJetTagger)
38
39 //________________________________________________________________________
40 AliAnalysisTaskEmcalJetTagger::AliAnalysisTaskEmcalJetTagger() : 
41   AliAnalysisTaskEmcalJet("AliAnalysisTaskEmcalJetTagger", kTRUE),
42   fJetTaggingType(kTag),
43   fJetTaggingMethod(kGeo),
44   fContainerBase(0),
45   fContainerTag(1),
46   fMatchingDone(0),
47   fh3PtJet1VsDeltaEtaDeltaPhi(0),
48   fh2PtJet1VsDeltaR(0),
49   fh2PtJet1VsLeadPtAllSel(0),
50   fh2PtJet1VsLeadPtTagged(0),
51   fh2PtJet1VsPtJet2(0),
52   fh3PtJetDEtaDPhiConst(0),
53   fh2PtJetDRConst(0),
54   fh3PtJetAreaDRConst(0)
55 {
56   // Default constructor.
57
58   fh3PtJet1VsDeltaEtaDeltaPhi  = new TH3F*[fNcentBins];
59   fh2PtJet1VsDeltaR            = new TH2F*[fNcentBins];
60   fh2PtJet1VsLeadPtAllSel      = new TH2F*[fNcentBins];
61   fh2PtJet1VsLeadPtTagged      = new TH2F*[fNcentBins];
62   fh2PtJet1VsPtJet2            = new TH2F*[fNcentBins];
63
64   for (Int_t i = 0; i < fNcentBins; i++) {
65     fh3PtJet1VsDeltaEtaDeltaPhi[i] = 0;
66     fh2PtJet1VsDeltaR[i]           = 0;
67     fh2PtJet1VsLeadPtAllSel[i]     = 0;
68     fh2PtJet1VsLeadPtTagged[i]     = 0;
69     fh2PtJet1VsPtJet2[i]           = 0;
70   }
71
72   SetMakeGeneralHistograms(kTRUE);
73   
74 }
75
76 //________________________________________________________________________
77 AliAnalysisTaskEmcalJetTagger::AliAnalysisTaskEmcalJetTagger(const char *name) : 
78   AliAnalysisTaskEmcalJet(name, kTRUE),  
79   fJetTaggingType(kTag),
80   fJetTaggingMethod(kGeo),
81   fContainerBase(0),
82   fContainerTag(1),
83   fMatchingDone(0),
84   fh3PtJet1VsDeltaEtaDeltaPhi(0),
85   fh2PtJet1VsDeltaR(0),
86   fh2PtJet1VsLeadPtAllSel(0),
87   fh2PtJet1VsLeadPtTagged(0),
88   fh2PtJet1VsPtJet2(0),
89   fh3PtJetDEtaDPhiConst(0),
90   fh2PtJetDRConst(0),
91   fh3PtJetAreaDRConst(0)
92 {
93   // Standard constructor.
94
95   fh3PtJet1VsDeltaEtaDeltaPhi = new TH3F*[fNcentBins];
96   fh2PtJet1VsDeltaR           = new TH2F*[fNcentBins];
97   fh2PtJet1VsLeadPtAllSel     = new TH2F*[fNcentBins];
98   fh2PtJet1VsLeadPtTagged     = new TH2F*[fNcentBins];
99   fh2PtJet1VsPtJet2           = new TH2F*[fNcentBins];
100
101   for (Int_t i = 0; i < fNcentBins; i++) {
102     fh3PtJet1VsDeltaEtaDeltaPhi[i] = 0;
103     fh2PtJet1VsDeltaR[i]           = 0;
104     fh2PtJet1VsLeadPtAllSel[i]     = 0;
105     fh2PtJet1VsLeadPtTagged[i]     = 0;
106     fh2PtJet1VsPtJet2[i]           = 0;
107   }
108
109   SetMakeGeneralHistograms(kTRUE);
110 }
111
112 //________________________________________________________________________
113 AliAnalysisTaskEmcalJetTagger::~AliAnalysisTaskEmcalJetTagger()
114 {
115   // Destructor.
116 }
117
118 //________________________________________________________________________
119 void AliAnalysisTaskEmcalJetTagger::UserCreateOutputObjects()
120 {
121   // Create user output.
122
123   AliAnalysisTaskEmcalJet::UserCreateOutputObjects();
124
125   Bool_t oldStatus = TH1::AddDirectoryStatus();
126   TH1::AddDirectory(kFALSE);
127
128   const Int_t nBinsPt          = 250;
129   const Int_t nBinsDPhi        = 100;
130   const Int_t nBinsDEta        = 100;
131   const Int_t nBinsDR          = 50;
132
133   const Double_t minPt = -50.;
134   const Double_t maxPt = 200.;
135   const Double_t minDPhi = -0.5;
136   const Double_t maxDPhi =  0.5;
137   const Double_t minDEta = -0.5;
138   const Double_t maxDEta =  0.5;
139   const Double_t minDR   =  0.;
140   const Double_t maxDR   =  0.5;
141
142   TString histName = "";
143   TString histTitle = "";
144   for (Int_t i = 0; i < fNcentBins; i++) {
145
146     histName = TString::Format("fh3PtJet1VsDeltaEtaDeltaPhi_%d",i);
147     histTitle = TString::Format("%s;#it{p}_{T,jet1};#it{#Delta#eta};#it{#Delta#varphi}",histName.Data());
148     fh3PtJet1VsDeltaEtaDeltaPhi[i] = new TH3F(histName.Data(),histTitle.Data(),nBinsPt,minPt,maxPt,nBinsDEta,minDEta,maxDEta,nBinsDPhi,minDPhi,maxDPhi);
149     fOutput->Add(fh3PtJet1VsDeltaEtaDeltaPhi[i]);
150
151     histName = TString::Format("fh2PtJet1VsDeltaR_%d",i);
152     histTitle = TString::Format("%s;#it{p}_{T,jet1};#it{#Delta R}",histName.Data());
153     fh2PtJet1VsDeltaR[i] = new TH2F(histName.Data(),histTitle.Data(),nBinsPt,minPt,maxPt,nBinsDR,minDR,maxDR);
154     fOutput->Add(fh2PtJet1VsDeltaR[i]);
155
156     histName = TString::Format("fh2PtJet1VsLeadPtAllSel_%d",i);
157     histTitle = TString::Format("%s;#it{p}_{T,jet1};#it{p}_{T,lead trk}",histName.Data());
158     fh2PtJet1VsLeadPtAllSel[i] = new TH2F(histName.Data(),histTitle.Data(),nBinsPt,minPt,maxPt,20,0.,20.);
159     fOutput->Add(fh2PtJet1VsLeadPtAllSel[i]);
160
161     histName = TString::Format("fh2PtJet1VsLeadPtTagged_%d",i);
162     histTitle = TString::Format("%s;#it{p}_{T,jet1};#it{p}_{T,lead trk}",histName.Data());
163     fh2PtJet1VsLeadPtTagged[i] =  new TH2F(histName.Data(),histTitle.Data(),nBinsPt,minPt,maxPt,20,0.,20.);
164     fOutput->Add(fh2PtJet1VsLeadPtTagged[i]);
165
166     histName = TString::Format("fh2PtJet1VsPtJet2_%d",i);
167     histTitle = TString::Format("%s;#it{p}_{T,jet1};#it{p}_{T,jet2}",histName.Data());
168     fh2PtJet1VsPtJet2[i] = new TH2F(histName.Data(),histTitle.Data(),nBinsPt,minPt,maxPt,nBinsPt,minPt,maxPt);
169     fOutput->Add(fh2PtJet1VsPtJet2[i]);
170
171   }
172
173   fh3PtJetDEtaDPhiConst = new TH3F("fh3PtJetDEtaDPhiConst","fh3PtJetDEtaDPhiConst;pT;#Delta #eta;#Delta #varphi",nBinsPt,minPt,maxPt,nBinsDEta,-1.,1.,nBinsDPhi,-1.,1.);
174   fOutput->Add(fh3PtJetDEtaDPhiConst);
175
176   fh2PtJetDRConst = new TH2F("fh2PtJetDRConst","fh2PtJetDRConst;pT;#Delta R",nBinsPt,minPt,maxPt,100,0.,1.);
177   fOutput->Add(fh2PtJetDRConst);
178
179   fh3PtJetAreaDRConst = new TH3F("fh3PtJetAreaDRConst","fh3PtJetAreaDRConst;pT;A;#Delta R",nBinsPt,minPt,maxPt,100,0.,1.,100,0.,1.);
180   fOutput->Add(fh3PtJetAreaDRConst);
181
182   // =========== Switch on Sumw2 for all histos ===========
183   for (Int_t i=0; i<fOutput->GetEntries(); ++i) {
184     TH1 *h1 = dynamic_cast<TH1*>(fOutput->At(i));
185     if (h1){
186       h1->Sumw2();
187       continue;
188     }
189     THnSparse *hn = dynamic_cast<THnSparse*>(fOutput->At(i));
190     if(hn)hn->Sumw2();
191   }
192
193   TH1::AddDirectory(oldStatus);
194
195   PostData(1, fOutput); // Post data for ALL output slots > 0 here.
196 }
197
198 //________________________________________________________________________
199 Bool_t AliAnalysisTaskEmcalJetTagger::Run()
200 {
201   // Run analysis code here, if needed. It will be executed before FillHistograms().
202
203   if(fJetTaggingMethod==kGeo)
204     MatchJetsGeo(fContainerBase,fContainerTag,0,0.3,2);
205
206   return kTRUE;
207 }
208
209 //________________________________________________________________________
210 Bool_t AliAnalysisTaskEmcalJetTagger::FillHistograms()
211 {
212   // Fill histograms.
213
214   for(int i = 0; i < GetNJets(fContainerBase);++i) {
215     AliEmcalJet *jet1 = static_cast<AliEmcalJet*>(GetAcceptJetFromArray(i, fContainerBase));
216     if(!jet1) continue;
217
218     Double_t ptJet1 =  jet1->Pt() - GetRhoVal(fContainerBase)*jet1->Area();
219     fh2PtJet1VsLeadPtAllSel[fCentBin]->Fill(ptJet1,jet1->MaxTrackPt());
220
221     //fill histo with angle between jet axis and constituents
222     for(Int_t icc=0; icc<jet1->GetNumberOfTracks(); icc++) {
223       AliVParticle *vp = static_cast<AliVParticle*>(jet1->TrackAt(icc, fTracks));
224       if(!vp) continue;
225       Double_t dEta = jet1->Eta()-vp->Eta();
226       Double_t dPhi = jet1->Phi()-vp->Phi();
227       if(dPhi<TMath::Pi()) dPhi+=TMath::TwoPi();
228       if(dPhi>TMath::Pi()) dPhi-=TMath::TwoPi();
229       fh3PtJetDEtaDPhiConst->Fill(ptJet1,dEta,dPhi);
230
231       Double_t dR = TMath::Sqrt(dPhi*dPhi+dEta*dEta);
232       fh2PtJetDRConst->Fill(ptJet1,dR);
233       fh3PtJetAreaDRConst->Fill(ptJet1,jet1->Area(),dR);
234     }
235
236     if(jet1->GetTagStatus()<1 && fJetTaggingType==kTag)
237       continue;
238
239     AliEmcalJet *jet2 = jet1->GetTaggedJet();
240     if(!jet2) continue;
241     Double_t ptJet2 =  jet2->Pt() - GetRhoVal(fContainerTag)*jet2->Area();
242     fh2PtJet1VsLeadPtTagged[fCentBin]->Fill(ptJet1,jet1->MaxTrackPt());
243
244     fh2PtJet1VsPtJet2[fCentBin]->Fill(ptJet1,ptJet2);
245
246     Double_t dPhi = GetDeltaPhi(jet1->Phi(),jet2->Phi());
247     if(dPhi>TMath::Pi())
248       dPhi -= TMath::TwoPi();
249     if(dPhi<(-1.*TMath::Pi()))
250       dPhi += TMath::TwoPi();  
251     
252     fh3PtJet1VsDeltaEtaDeltaPhi[fCentBin]->Fill(ptJet1,jet1->Eta()-jet2->Eta(),dPhi);
253     fh2PtJet1VsDeltaR[fCentBin]->Fill(ptJet1,GetDeltaR(jet1,jet2));
254   }
255
256   return kTRUE;
257
258 }
259
260 //________________________________________________________________________
261 void AliAnalysisTaskEmcalJetTagger::ResetTagging(const Int_t c) {
262
263   //Reset tagging of container c
264
265   for(int i = 0;i<GetNJets(c);i++){
266     AliEmcalJet *jet = static_cast<AliEmcalJet*>(GetJetFromArray(i, c));
267     if(fJetTaggingType==kClosest)
268       jet->ResetMatching();
269     else if(fJetTaggingType==kTag) {
270       jet->SetTaggedJet(0x0);
271       jet->SetTagStatus(-1);
272     }
273   }
274 }
275
276 //________________________________________________________________________
277 void AliAnalysisTaskEmcalJetTagger::MatchJetsGeo(Int_t c1, Int_t c2,
278                                                  Int_t iDebug, Float_t maxDist, Int_t type) {
279
280   //
281   // Match the full jets to the corresponding charged jets
282   // Translation of AliAnalysisHelperJetTasks::GetClosestJets to AliEmcalJet objects
283   // type: 0 = use acceptance cuts of container  1 = allow 0.1 one more for c2 in eta 2 = allow 0.1 more in eta and phi for c2
284
285   if(c1<0) c1 = fContainerBase;
286   if(c2<0) c2 = fContainerTag;
287
288   const Int_t nJets1 = GetNJets(c1);
289   const Int_t nJets2 = GetNJets(c2);
290
291   if(nJets1==0 || nJets2==0) return;
292
293   ResetTagging(c1);
294   ResetTagging(c2);
295
296   fMatchingDone = kFALSE;
297
298   TArrayI faMatchIndex1;
299   faMatchIndex1.Set(nJets2+1);
300   faMatchIndex1.Reset(-1);
301
302   TArrayI faMatchIndex2;
303   faMatchIndex2.Set(nJets1+1);
304   faMatchIndex2.Reset(-1);
305
306   static TArrayS iFlag(nJets1*nJets2);
307   if(iFlag.GetSize()<(nJets1*nJets2)){
308     iFlag.Set(nJets1*nJets1+1);
309   }
310   iFlag.Reset(0);
311
312   AliJetContainer *cont2 = GetJetContainer(c2);
313
314   // find the closest distance to the full jet
315   for(int i = 0;i<nJets1;i++){
316
317     AliEmcalJet *jet1 = static_cast<AliEmcalJet*>(GetAcceptJetFromArray(i, c1));
318     if(!jet1) continue;
319
320     Float_t dist = maxDist;
321     
322     for(int j = 0;j <nJets2; j++){
323       AliEmcalJet *jet2 = 0x0;
324       if(type==0)
325         jet2 = static_cast<AliEmcalJet*>(GetAcceptJetFromArray(j, c2));
326       else {
327         jet2 = static_cast<AliEmcalJet*>(GetJetFromArray(j, c2));
328         if(!jet2) continue;
329         if(type>0) {
330           if(jet2->Eta()<(cont2->GetJetEtaMin()-0.1) || jet2->Eta()>(cont2->GetJetEtaMax()+0.1))
331             continue;
332           if(type==2) {
333             if(jet2->Phi()<(cont2->GetJetPhiMin()-0.1) || jet2->Phi()>(cont2->GetJetPhiMax()+0.1))
334               continue;
335           }
336         }
337       }
338       if(!jet2)
339         continue;
340
341       Double_t dR = GetDeltaR(jet1,jet2);
342       if(dR<dist && dR<maxDist){
343         faMatchIndex2[i]=j;
344         dist = dR;
345       }
346     }//j jet loop
347     if(faMatchIndex2[i]>=0) iFlag[i*nJets2+faMatchIndex2[i]]+=1;//j closest to i
348     if(iDebug>10) Printf("Full Distance (%d)--(%d) %3.3f flag[%d] = %d",i,faMatchIndex2[i],dist,i*nJets2+faMatchIndex2[i],iFlag[i*nJets2+faMatchIndex2[i]]);
349   
350   }//i jet loop
351
352
353   // other way around
354   for(int j = 0;j<nJets2;j++){
355     AliEmcalJet *jet2 = 0x0;
356     if(type==0)
357       jet2 = static_cast<AliEmcalJet*>(GetAcceptJetFromArray(j, c2));
358     else {
359       jet2 = static_cast<AliEmcalJet*>(GetJetFromArray(j, c2));
360       if(!jet2) continue;;
361       if(type>0) {
362         if(jet2->Eta()<(cont2->GetJetEtaMin()-0.1) || jet2->Eta()>(cont2->GetJetEtaMax()+0.1))
363           continue;
364         if(type==2) {
365           if(jet2->Phi()<(cont2->GetJetPhiMin()-0.1) || jet2->Phi()>(cont2->GetJetPhiMax()+0.1))
366             continue;
367         }
368       }
369     }
370     if(!jet2)
371       continue;
372
373     Float_t dist = maxDist;
374     for(int i = 0;i<nJets1;i++){
375       AliEmcalJet *jet1 = static_cast<AliEmcalJet*>(GetAcceptJetFromArray(i, c1));
376       if(!jet1)
377         continue;
378
379       Double_t dR = GetDeltaR(jet1,jet2); 
380       if(dR<dist && dR<maxDist){
381         faMatchIndex1[j]=i;
382         dist = dR;
383       }   
384     }
385     if(faMatchIndex1[j]>=0) iFlag[faMatchIndex1[j]*nJets2+j]+=2;//i closest to j
386     if(iDebug>10) Printf("Other way Distance (%d)--(%d) %3.3f flag[%d] = %d",faMatchIndex1[j],j,dist,faMatchIndex1[j]*nJets2+j,iFlag[faMatchIndex1[j]*nJets2+j]);
387
388   }
389     
390   // check for "true" correlations
391   for(int i = 0;i<nJets1;i++){
392     AliEmcalJet *jet1 = static_cast<AliEmcalJet*>(GetJetFromArray(i, c1));
393     for(int j = 0;j<nJets2;j++){
394       AliEmcalJet *jet2 = static_cast<AliEmcalJet*>(GetJetFromArray(j, c2));
395       if(iDebug>10) AliInfo(Form("%s: Flag[%d][%d] %d ",GetName(),i,j,iFlag[i*nJets2+j]));
396       
397       // we have a uniqe correlation
398       if(iFlag[i*nJets2+j]==3){
399         
400         Double_t dR = GetDeltaR(jet1,jet2); 
401         if(iDebug>1) Printf("closest jets %d  %d  dR =  %f",j,i,dR);
402         
403         if(fJetTaggingType==kTag) {
404           jet1->SetTaggedJet(jet2);
405           jet1->SetTagStatus(1);
406           
407           jet2->SetTaggedJet(jet1);
408           jet2->SetTagStatus(1);
409         }
410         else if(fJetTaggingType==kClosest) {
411           jet1->SetClosestJet(jet2,dR);
412           jet2->SetClosestJet(jet1,dR);
413         }
414       }
415     }
416   }
417
418   fMatchingDone = kTRUE;
419   
420 }
421
422 //________________________________________________________________________
423 Double_t AliAnalysisTaskEmcalJetTagger::GetDeltaR(const AliEmcalJet* jet1, const AliEmcalJet* jet2) const {
424   //
425   // Helper function to calculate the distance between two jets
426   //
427
428   Double_t dPhi = jet1->Phi() - jet2->Phi();
429   if(dPhi>TMath::Pi())
430     dPhi -= TMath::TwoPi();
431   if(dPhi<(-1.*TMath::Pi()))
432     dPhi += TMath::TwoPi();
433   Double_t dEta = jet1->Eta() - jet2->Eta();
434   Double_t dR = TMath::Sqrt(dPhi*dPhi+dEta*dEta);
435   return dR;
436 }
437
438 //________________________________________________________________________
439 Double_t AliAnalysisTaskEmcalJetTagger::GetDeltaPhi(const AliEmcalJet* jet1, const AliEmcalJet* jet2) {
440   //
441   // Calculate azimuthal angle between the axises of the jets
442   //
443
444   return GetDeltaPhi(jet1->Phi(),jet2->Phi());
445
446 }
447
448 //________________________________________________________________________
449 Double_t AliAnalysisTaskEmcalJetTagger::GetDeltaPhi(Double_t phi1,Double_t phi2) {
450   //
451   // Calculate azimuthal angle between the axises of the jets
452   //
453
454   Double_t dPhi = phi1-phi2;
455
456   if(dPhi <-0.5*TMath::Pi())  dPhi += TMath::TwoPi();
457   if(dPhi > 1.5*TMath::Pi())  dPhi -= TMath::TwoPi();
458
459   return dPhi;
460 }
461
462 //________________________________________________________________________
463 Double_t AliAnalysisTaskEmcalJetTagger::GetFractionSharedPt(const AliEmcalJet *jet1, const AliEmcalJet *jet2) const
464 {
465   //
466   // Get fraction of shared pT between matched full and charged jet
467   // Uses charged jet pT as baseline: fraction = \Sum_{const,full jet} pT,const,i / pT,jet,ch
468   //
469
470   Double_t fraction = 0.;
471   Double_t jetPt2 = jet2->Pt();
472  
473   if(jetPt2>0) {
474
475     AliDebug(2,Form("%s: nConstituents: %d, ch: %d  chne: %d ne: %d",GetName(),jet1->GetNumberOfConstituents(),jet2->GetNumberOfTracks(),jet1->GetNumberOfTracks(),jet1->GetNumberOfClusters()));
476     
477     Double_t sumPt = 0.;
478     AliVParticle *vpf = 0x0;
479     Int_t iFound = 0;
480     for(Int_t icc=0; icc<jet2->GetNumberOfTracks(); icc++) {
481       Int_t idx = (Int_t)jet2->TrackAt(icc);
482       iFound = 0;
483       for(Int_t icf=0; icf<jet1->GetNumberOfTracks(); icf++) {
484         if(idx == jet1->TrackAt(icf) && iFound==0 ) {
485           iFound=1;
486           vpf = static_cast<AliVParticle*>(jet1->TrackAt(icf, fTracks));
487           if(!vpf) continue;
488           if(vpf->Charge()!=0)
489             sumPt += vpf->Pt();
490           continue;
491         }
492       }
493     }
494     
495     fraction = sumPt/jetPt2;
496   }
497
498   return fraction;
499
500 }
501
502 //________________________________________________________________________
503 Bool_t AliAnalysisTaskEmcalJetTagger::RetrieveEventObjects() {
504   //
505   // retrieve event objects
506   //
507
508   if (!AliAnalysisTaskEmcalJet::RetrieveEventObjects())
509     return kFALSE;
510
511   return kTRUE;
512
513 }
514
515 //_______________________________________________________________________
516 void AliAnalysisTaskEmcalJetTagger::Terminate(Option_t *) 
517 {
518   // Called once at the end of the analysis.
519 }
520