]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/SPECTRA/PiKaPr/TestAOD/AliAnalysisTaskSpectraBoth.cxx
Merge branch 'master_patch'
[u/mrichter/AliRoot.git] / PWGLF / SPECTRA / PiKaPr / TestAOD / AliAnalysisTaskSpectraBoth.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 //-----------------------------------------------------------------
17 //         AliAnalysisTaskSpectraBoth class
18 //-----------------------------------------------------------------
19
20 #include "TChain.h"
21 #include "TTree.h"
22 #include "TLegend.h"
23 #include "TH1F.h"
24 #include "TH2F.h"
25 #include "TH3F.h"
26 #include "TCanvas.h"
27 #include "AliAnalysisTask.h"
28 #include "AliAnalysisManager.h"
29 #include "AliAODTrack.h"
30 #include "AliAODMCParticle.h"
31 #include "AliVParticle.h"
32 #include "AliAODEvent.h"
33 #include "AliAODInputHandler.h"
34 #include "AliAnalysisTaskSpectraBoth.h"
35 #include "AliAnalysisTaskESDfilter.h"
36 #include "AliAnalysisDataContainer.h"
37 #include "AliSpectraBothHistoManager.h"
38 #include "AliSpectraBothTrackCuts.h"
39 #include "AliSpectraBothEventCuts.h"
40 #include "AliCentrality.h"
41 #include "TProof.h"
42 #include "AliPID.h"
43 #include "AliVEvent.h"
44 #include "AliESDEvent.h"
45 #include "AliPIDResponse.h"
46 #include "AliStack.h"
47 #include "AliSpectraBothPID.h"
48 #include "AliGenEventHeader.h"  
49 #include <TMCProcess.h>
50
51 #include <iostream>
52
53
54
55
56 using namespace AliSpectraNameSpaceBoth;
57 using namespace std;
58
59 ClassImp(AliAnalysisTaskSpectraBoth)
60
61 //________________________________________________________________________
62 AliAnalysisTaskSpectraBoth::AliAnalysisTaskSpectraBoth(const char *name) : AliAnalysisTaskSE(name), fAOD(0), fHistMan(0), fTrackCuts(0), fEventCuts(0),  fPID(0), fIsMC(0), fNRebin(0),fUseMinSigma(0),fCuts(0),fdotheMCLoopAfterEventCuts(0),fmakePIDQAhisto(1),fMotherWDPDGcode(-1)
63
64 {
65   // Default constructor
66   
67   DefineInput(0, TChain::Class());
68   DefineOutput(1, AliSpectraBothHistoManager::Class());
69   DefineOutput(2, AliSpectraBothEventCuts::Class());
70   DefineOutput(3, AliSpectraBothTrackCuts::Class());
71   DefineOutput(4, AliSpectraBothPID::Class());
72   fNRebin=0;
73   
74 }
75 //________________________________________________________________________
76 //________________________________________________________________________
77 void AliAnalysisTaskSpectraBoth::UserCreateOutputObjects()
78 {
79   // create output objects
80   fHistMan = new AliSpectraBothHistoManager("SpectraHistos",fNRebin,fmakePIDQAhisto);
81
82   if (!fTrackCuts) AliFatal("Track Cuts should be set in the steering macro");
83   if (!fEventCuts) AliFatal("Event Cuts should be set in the steering macro");
84   if (!fPID)       AliFatal("PID object should be set in the steering macro");
85   fTrackCuts->SetAliESDtrackCuts(fCuts);
86   fEventCuts->InitHisto();
87   fTrackCuts->InitHisto();
88
89   PostData(1, fHistMan  );
90   PostData(2, fEventCuts);
91   PostData(3, fTrackCuts);
92   PostData(4, fPID      );
93 }
94 //________________________________________________________________________
95 void AliAnalysisTaskSpectraBoth::UserExec(Option_t *)
96 {
97   // main event loop
98         Int_t ifAODEvent=AliSpectraBothTrackCuts::kotherobject;
99         fAOD = dynamic_cast<AliVEvent*>(InputEvent());
100   //    AliESDEvent* esdevent=0x0;
101  //     AliAODEvent* aodevent=0x0;
102    
103         TString nameoftrack(fAOD->ClassName());  
104         if(!nameoftrack.CompareTo("AliESDEvent"))
105         {
106                 ifAODEvent=AliSpectraBothTrackCuts::kESDobject;
107                 //esdevent=dynamic_cast<AliESDEvent*>(fAOD);
108         }
109         else if(!nameoftrack.CompareTo("AliAODEvent"))
110         {
111                 ifAODEvent=AliSpectraBothTrackCuts::kAODobject;
112                 //aodevent=dynamic_cast<AliAODEvent*>(fAOD);
113         }
114         else
115                 AliFatal("Not processing AODs or ESDS") ;
116         if(fdotheMCLoopAfterEventCuts)
117                 if(!fEventCuts->IsSelected(fAOD,fTrackCuts,fIsMC,-100,fHistMan->GetEventStatHist()))
118                         return;//event selection
119         TClonesArray *arrayMC = 0;
120         Int_t npar=0;
121         AliStack* stack=0x0;
122          Double_t mcZ=-100;
123
124         if (fIsMC)
125         {
126                 TArrayF mcVertex(3);
127                 mcVertex[0]=9999.; mcVertex[1]=9999.; mcVertex[2]=9999.;
128                 AliMCEvent* mcEvent=(AliMCEvent*)MCEvent();
129                 if (!mcEvent) 
130                 {
131                         AliFatal("Error: MC particles branch not found!\n");
132                 }
133                 AliHeader* header = mcEvent->Header();
134                 if (!header) 
135                 {
136                         AliDebug(AliLog::kError, "Header not available");
137                         return;
138                 }
139         
140                 AliGenEventHeader* genHeader = header->GenEventHeader();
141                 if(genHeader)
142                 {
143                         genHeader->PrimaryVertex(mcVertex);
144                         mcZ=mcVertex[2];
145                 }
146                   if(ifAODEvent==AliSpectraBothTrackCuts::kAODobject)
147                   {
148                           arrayMC = (TClonesArray*) fAOD->GetList()->FindObject(AliAODMCParticle::StdBranchName());
149                           if (!arrayMC) 
150                           {
151                                         AliFatal("Error: MC particles branch not found!\n");
152                           }
153                           Int_t nMC = arrayMC->GetEntries();
154                           for (Int_t iMC = 0; iMC < nMC; iMC++)
155                           {
156                                   AliAODMCParticle *partMC = (AliAODMCParticle*) arrayMC->At(iMC);
157                                   if(!partMC->Charge()) continue;//Skip neutrals
158                                   //if(partMC->Eta() > fTrackCuts->GetEtaMin() && partMC->Eta() < fTrackCuts->GetEtaMax()){//charged hadron are filled inside the eta acceptance
159                                   //Printf("%f     %f-%f",partMC->Eta(),fTrackCuts->GetEtaMin(),fTrackCuts->GetEtaMax());
160                                   if(partMC->Eta() > fTrackCuts->GetEtaMin() && partMC->Eta() < fTrackCuts->GetEtaMax())
161                                                 fHistMan->GetPtHistogram(kHistPtGen)->Fill(partMC->Pt(),partMC->IsPhysicalPrimary());                                                                    
162                                   //rapidity cut
163                                   if(partMC->Y() > fTrackCuts->GetYMax()|| partMC->Y() < fTrackCuts->GetYMin() ) 
164                                         continue;       
165                                   if(partMC->IsPhysicalPrimary())
166                                          npar++;    
167                                   // check for true PID + and fill P_t histos 
168                                   Int_t charge = partMC->Charge() > 0 ? kChPos : kChNeg ;
169                                   Int_t id = fPID->GetParticleSpecie(partMC);
170                                   if(id != kSpUndefined) 
171                                   {
172                                         fHistMan->GetHistogram2D(kHistPtGenTruePrimary,id,charge)->Fill(partMC->Pt(),partMC->IsPhysicalPrimary());
173                                   }
174                           }
175                   }
176                   if(ifAODEvent==AliSpectraBothTrackCuts::kESDobject)
177                   {
178                         stack = mcEvent->Stack();
179                         Int_t nMC = stack->GetNtrack();
180                         for (Int_t iMC = 0; iMC < nMC; iMC++)
181                         {
182                                  
183                                 TParticle *partMC = stack->Particle(iMC);
184                                 
185                                 if(!partMC)     
186                                         continue;       
187         
188                                 if(!partMC->GetPDG(0))
189                                         continue;
190                                 if(TMath::Abs(partMC->GetPDG(0)->Charge()/3.0)<0.01) 
191                                         continue;//Skip neutrals
192                                 if(partMC->Eta() > fTrackCuts->GetEtaMin() && partMC->Eta() < fTrackCuts->GetEtaMax())
193                                         fHistMan->GetPtHistogram(kHistPtGen)->Fill(partMC->Pt(),stack->IsPhysicalPrimary(iMC));
194                                 if(partMC->Y()   > fTrackCuts->GetYMax() ||partMC->Y()   < fTrackCuts->GetYMin()  ) 
195                                         continue;
196                                 if(stack->IsPhysicalPrimary(iMC))
197                                          npar++;    
198                                   // check for true PID + and fill P_t histos 
199                                 Int_t charge = partMC->GetPDG(0)->Charge()/3.0 > 0 ? kChPos : kChNeg ;
200                                 Int_t id = fPID->GetParticleSpecie(partMC);
201                                 if(id != kSpUndefined) 
202                                 {
203                                         fHistMan->GetHistogram2D(kHistPtGenTruePrimary,id,charge)->Fill(partMC->Pt(),stack->IsPhysicalPrimary(iMC));
204                                 }
205                           }
206                   }
207         }
208         if(!fdotheMCLoopAfterEventCuts)
209                 if(!fEventCuts->IsSelected(fAOD,fTrackCuts,fIsMC,mcZ,fHistMan->GetEventStatHist()))
210                         return;//event selection
211         //main loop on tracks
212         Int_t ntracks=0;
213         //cout<<fAOD->GetNumberOfTracks()<<endl;
214         for (Int_t iTracks = 0; iTracks < fAOD->GetNumberOfTracks(); iTracks++) 
215         {
216                 AliVTrack* track = dynamic_cast<AliVTrack*>(fAOD->GetTrack(iTracks));
217                 AliAODTrack* aodtrack=0;
218                 AliESDtrack* esdtrack=0;
219                 Float_t dca=-999.;
220                 Float_t dcaz=-999.;
221                 Short_t ncls=-1; 
222                 Float_t chi2perndf=-1.0;
223                 if(ifAODEvent==AliSpectraBothTrackCuts::kESDobject)
224                 {
225                         esdtrack=dynamic_cast<AliESDtrack*>(track);
226                         if(!esdtrack)
227                                 continue;
228                         esdtrack->GetImpactParameters(dca,dcaz);
229                         ncls=esdtrack->GetTPCNcls();
230                         if ( ncls > 5) 
231                                 chi2perndf=(esdtrack->GetTPCchi2()/Float_t(ncls - 5));
232   
233                         else 
234                                 chi2perndf=-1;
235                         
236                 }
237                 else if (ifAODEvent==AliSpectraBothTrackCuts::kAODobject)
238                 {
239                         aodtrack=dynamic_cast<AliAODTrack*>(track);
240                         if(!aodtrack)
241                                 continue;               
242                         dca=aodtrack->DCA();
243                         dcaz=aodtrack->ZAtDCA();
244                         ncls=aodtrack->GetTPCNcls();
245                         chi2perndf=aodtrack->Chi2perNDF();
246                 }
247                 else
248                         continue;
249                 if (!fTrackCuts->IsSelected(track,kTRUE)) 
250                         continue;       
251                         
252                 ntracks++;
253                 if(fmakePIDQAhisto)
254                         fPID->FillQAHistos(fHistMan, track, fTrackCuts);
255                 
256                 //calculate DCA for AOD track
257                 if(dca==-999.)
258                 {// track->DCA() does not work in old AOD production
259                         Double_t d[2], covd[3];
260                         AliVTrack* track_clone=(AliVTrack*)track->Clone("track_clone"); // need to clone because PropagateToDCA updates the track parameters
261                         Bool_t isDCA = track_clone->PropagateToDCA(fAOD->GetPrimaryVertex(),fAOD->GetMagneticField(),9999.,d,covd);
262                         delete track_clone;
263                         if(!isDCA)
264                                 d[0]=-999.;
265                         dca=d[0];
266                         dcaz=d[1];      
267                 }
268         
269                 fHistMan->GetPtHistogram(kHistPtRec)->Fill(track->Pt(),dca);  // PT histo
270                 // get identity and charge
271                 Bool_t rec[3]={false,false,false};
272                 Bool_t sel[3]={false,false,false};
273                 Int_t idRec  = fPID->GetParticleSpecie(fHistMan,track, fTrackCuts,rec);
274                 for(int irec=kSpPion;irec<kNSpecies;irec++)
275                 {
276    
277                         if(fUseMinSigma)
278                         {
279                                 if(irec>kSpPion)
280                                         break;
281                         }
282                         else
283                         {       
284                                 if(!rec[irec]) 
285                                         idRec = kSpUndefined;
286                                 else    
287                                         idRec=irec;
288                         }               
289    
290                         Int_t charge = track->Charge() > 0 ? kChPos : kChNeg;
291                 
292                         // Fill histograms, only if inside y and nsigma acceptance
293                         if(idRec != kSpUndefined && fTrackCuts->CheckYCut ((BothParticleSpecies_t)idRec))
294                         {
295                                 fHistMan->GetHistogram2D(kHistPtRecSigma,idRec,charge)->Fill(track->Pt(),dca);
296                                 if(fTrackCuts->GetMakeQAhisto())
297                                 { 
298                                         fTrackCuts->GetHistoDCAzQA()->Fill(idRec,track->Pt(),dcaz);
299                                         fTrackCuts->GetHistoNclustersQA()->Fill(idRec,track->Pt(),ncls);
300                                         fTrackCuts->GetHistochi2perNDFQA()->Fill(idRec,track->Pt(),chi2perndf);
301                                 }
302                                 sel[idRec]=true;
303                         }
304                         //can't put a continue because we still have to fill allcharged primaries, done later
305                 
306                         /* MC Part */
307                         if (arrayMC||stack) 
308                         {
309                                 Bool_t isPrimary           = kFALSE;
310                                 Bool_t isSecondaryMaterial = kFALSE; 
311                                 Bool_t isSecondaryWeak     = kFALSE; 
312                                 Int_t idGen     =kSpUndefined;
313                                 Int_t pdgcode=0;
314                                 Int_t motherpdg=-1;
315                                 if (ifAODEvent==AliSpectraBothTrackCuts::kAODobject)
316                                 {
317                                         AliAODMCParticle *partMC = (AliAODMCParticle*) arrayMC->At(TMath::Abs(track->GetLabel()));
318                                         if (!partMC) 
319                                         { 
320                                                 AliError("Cannot get MC particle");
321                                                 continue; 
322                                         }
323                                         // Check if it is primary, secondary from material or secondary from weak decay
324                                         isPrimary           = partMC->IsPhysicalPrimary();
325                                         isSecondaryWeak     = partMC->IsSecondaryFromWeakDecay();
326                                         isSecondaryMaterial      = partMC->IsSecondaryFromMaterial();
327                                         //cout<<"AOD tagging "<<isPrimary<<" "<<isSecondaryWeak<<isSecondaryMaterial<<" "<<partMC->GetMCProcessCode()<<endl;
328
329                                         if(!isPrimary&&!isSecondaryWeak&&!isSecondaryMaterial)//old tagging for old AODs 
330                                         {
331                                                 AliError("old tagging");
332                                                 Int_t mfl=-999,codemoth=-999;
333                                                 Int_t indexMoth=partMC->GetMother(); // FIXME ignore fakes? TO BE CHECKED, on ESD is GetFirstMother()
334                                                 if(indexMoth>=0)
335                                                 {//is not fake
336                                                         AliAODMCParticle* moth = (AliAODMCParticle*) arrayMC->At(indexMoth);
337                                                         codemoth = TMath::Abs(moth->GetPdgCode());
338                                                         mfl = Int_t (codemoth/ TMath::Power(10, Int_t(TMath::Log10(codemoth))));
339                                                 }
340                                                 //Int_t uniqueID = partMC->GetUniqueID();
341                                                 //cout<<"uniqueID: "<<partMC->GetUniqueID()<<"       "<<kPDecay<<endl;
342                                                 //cout<<"status: "<<partMC->GetStatus()<<"       "<<kPDecay<<endl;
343                                                 // if(uniqueID == kPDecay)Printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
344                                                 if(mfl==3) 
345                                                         isSecondaryWeak     = kTRUE; // add if(partMC->GetStatus() & kPDecay)? FIXME
346                                                 else       
347                                                         isSecondaryMaterial = kTRUE;
348                                         }
349                                         //cout<<"AOD 2 tagging "<<isPrimary<<" "<<isSecondaryWeak<<isSecondaryMaterial<<" "<<partMC->GetMCProcessCode()<<endl;
350                                         if(isSecondaryWeak)
351                                         {       
352                                                 Int_t indexMoth=partMC->GetMother(); // FIXME ignore fakes? TO BE CHECKED, on ESD is GetFirstMother()
353                                                 if(indexMoth>=0)
354                                                 {
355                                                         AliAODMCParticle* moth = (AliAODMCParticle*) arrayMC->At(indexMoth);
356                                                         if(moth)
357                                                                 motherpdg=TMath::Abs(moth->GetPdgCode());
358                                                 }
359                                         }
360
361                                         idGen     = fPID->GetParticleSpecie(partMC);
362                                         pdgcode=partMC->GetPdgCode(); 
363                                 }
364                                 else if (ifAODEvent==AliSpectraBothTrackCuts::kESDobject)
365                                 {
366                                         TParticle *partMC =stack->Particle(TMath::Abs(track->GetLabel()));
367                                         if (!partMC) 
368                                         { 
369                                                 AliError("Cannot get MC particle");
370                                                 continue; 
371                                         }
372                                         isPrimary           = stack->IsPhysicalPrimary(TMath::Abs(track->GetLabel()));
373                                         isSecondaryWeak     = stack->IsSecondaryFromWeakDecay(TMath::Abs(track->GetLabel()));
374                                         isSecondaryMaterial      = stack->IsSecondaryFromMaterial(TMath::Abs(track->GetLabel()));
375                                         //cout<<"ESD tagging "<<isPrimary<<" "<<isSecondaryWeak<<isSecondaryMaterial<<endl;
376                                         
377                                         if(isSecondaryWeak)     
378                                         {
379
380                                                 TParticle* moth=stack->Particle(TMath::Abs(partMC->GetFirstMother()));
381                                                 if(moth)
382                                                          motherpdg = TMath::Abs(moth->GetPdgCode());
383
384                                         }
385                                         
386
387                                         idGen     = fPID->GetParticleSpecie(partMC);
388                                         pdgcode=partMC->GetPdgCode(); 
389                                 }
390                                 else
391                                         return;
392                         
393                         //        cout<<isPrimary<<" "<<isSecondaryWeak<<" "<<isSecondaryMaterial<<endl;
394                         //        cout<<" functions "<<partMC->IsPhysicalPrimary()<<" "<<partMC->IsSecondaryFromWeakDecay()<<" "<<partMC->IsSecondaryFromMaterial()<<endl;
395                   
396                                 if (isPrimary&&irec==kSpPion)
397                                         fHistMan->GetPtHistogram(kHistPtRecPrimaryAll)->Fill(track->Pt(),dca);  // PT histo of reconstrutsed primaries in defined eta
398                   
399                         //nsigma cut (reconstructed nsigma)
400                                 if(idRec == kSpUndefined) 
401                                         continue;
402                   
403                         // rapidity cut (reconstructed pt and identity)
404                                  if(!fTrackCuts->CheckYCut ((BothParticleSpecies_t)idRec)) continue;
405                   
406                   // Get true ID
407                                         
408                   
409                                  if (idRec == idGen) fHistMan->GetHistogram2D(kHistPtRecTrue,  idGen, charge)->Fill(track->Pt(),dca); 
410                   
411                                 if (isPrimary) 
412                                 {
413                                         fHistMan->GetHistogram2D(kHistPtRecSigmaPrimary, idRec, charge)->Fill(track->Pt(),dca); 
414                                         if(idGen != kSpUndefined) 
415                                         {
416                                                 fHistMan->GetHistogram2D(kHistPtRecPrimary,      idGen, charge)->Fill(track->Pt(),dca);
417                                                 if (idRec == idGen) 
418                                                         fHistMan->GetHistogram2D(kHistPtRecTruePrimary,  idGen, charge)->Fill(track->Pt(),dca); 
419                                         }
420                                 }
421                                  //25th Apr - Muons are added to Pions -- FIXME
422                                 if ( pdgcode == 13 && idRec == kSpPion) 
423                                 { 
424                                         fHistMan->GetPtHistogram(kHistPtRecTrueMuonPlus)->Fill(track->Pt(),dca); 
425                                         if(isPrimary)
426                                                 fHistMan->GetPtHistogram(kHistPtRecTruePrimaryMuonPlus)->Fill(track->Pt(),dca); 
427                                 }
428                                 if ( pdgcode == -13 && idRec == kSpPion) 
429                                 { 
430                                         fHistMan->GetPtHistogram(kHistPtRecTrueMuonMinus)->Fill(track->Pt(),dca); 
431                                         if (isPrimary) 
432                                         {
433                                                 fHistMan->GetPtHistogram(kHistPtRecTruePrimaryMuonMinus)->Fill(track->Pt(),dca); 
434                                         }
435                                 }
436                   
437                                  ///..... END FIXME
438                   
439                                 // Fill secondaries
440                                 if(isSecondaryWeak)
441                                 {  
442                                         if(fMotherWDPDGcode>0)
443                                         {
444                                                 if(motherpdg==fMotherWDPDGcode)
445                                                         fHistMan->GetHistogram2D(kHistPtRecSigmaSecondaryWeakDecay, idRec, charge)->Fill(track->Pt(),dca);
446                                         }       
447                                         else    
448                                                 fHistMan->GetHistogram2D(kHistPtRecSigmaSecondaryWeakDecay, idRec, charge)->Fill(track->Pt(),dca);
449                                 }
450                                 if(isSecondaryMaterial)  
451                                         fHistMan->GetHistogram2D(kHistPtRecSigmaSecondaryMaterial , idRec, charge)->Fill(track->Pt(),dca);
452                   
453                         }//end if(arrayMC)
454                 }
455                 if(sel[0]&&sel[1]&&sel[2])//pi+k+p
456                         fHistMan->GetPtHistogram("hHistDoubleCounts")->Fill(track->Pt(),0);
457                 else if(sel[0]&&sel[1]) //pi+k
458                         fHistMan->GetPtHistogram("hHistDoubleCounts")->Fill(track->Pt(),1);
459                 else if(sel[0]&&sel[2]) //pi+k
460                         fHistMan->GetPtHistogram("hHistDoubleCounts")->Fill(track->Pt(),2);
461                 else if(sel[1]&&sel[2]) //p+k
462                         fHistMan->GetPtHistogram("hHistDoubleCounts")->Fill(track->Pt(),3);
463
464         
465         
466         } // end loop on tracks
467
468  // cout<< ntracks<<endl;
469   fHistMan->GetGenMulvsRawMulHistogram("hHistGenMulvsRawMul")->Fill(npar,ntracks);
470   PostData(1, fHistMan  );
471   PostData(2, fEventCuts);
472   PostData(3, fTrackCuts);
473   PostData(4, fPID      );
474 }
475
476 //_________________________________________________________________
477 void   AliAnalysisTaskSpectraBoth::Terminate(Option_t *)
478 {
479   // Terminate
480 }