]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/SPECTRA/PiKaPr/TestAOD/AliAnalysisTaskSpectraBoth.cxx
e9888440ca1eecae9b323dd1c73908b02298ed48
[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))return;//event selection
118         TClonesArray *arrayMC = 0;
119         Int_t npar=0;
120         AliStack* stack=0x0;
121          Double_t mcZ=-100;
122
123         if (fIsMC)
124         {
125                 TArrayF mcVertex(3);
126                 mcVertex[0]=9999.; mcVertex[1]=9999.; mcVertex[2]=9999.;
127                 AliMCEvent* mcEvent=(AliMCEvent*)MCEvent();
128                 if (!mcEvent) 
129                 {
130                         AliFatal("Error: MC particles branch not found!\n");
131                 }
132                 AliHeader* header = mcEvent->Header();
133                 if (!header) 
134                 {
135                         AliDebug(AliLog::kError, "Header not available");
136                         return;
137                 }
138         
139                 AliGenEventHeader* genHeader = header->GenEventHeader();
140                 if(genHeader)
141                 {
142                         genHeader->PrimaryVertex(mcVertex);
143                         mcZ=mcVertex[2];
144                 }
145                   if(ifAODEvent==AliSpectraBothTrackCuts::kAODobject)
146                   {
147                           arrayMC = (TClonesArray*) fAOD->GetList()->FindObject(AliAODMCParticle::StdBranchName());
148                           if (!arrayMC) 
149                           {
150                                         AliFatal("Error: MC particles branch not found!\n");
151                           }
152                           Int_t nMC = arrayMC->GetEntries();
153                           for (Int_t iMC = 0; iMC < nMC; iMC++)
154                           {
155                                   AliAODMCParticle *partMC = (AliAODMCParticle*) arrayMC->At(iMC);
156                                   if(!partMC->Charge()) continue;//Skip neutrals
157                                   //if(partMC->Eta() > fTrackCuts->GetEtaMin() && partMC->Eta() < fTrackCuts->GetEtaMax()){//charged hadron are filled inside the eta acceptance
158                                   //Printf("%f     %f-%f",partMC->Eta(),fTrackCuts->GetEtaMin(),fTrackCuts->GetEtaMax());
159                                   if(partMC->Eta() > fTrackCuts->GetEtaMin() && partMC->Eta() < fTrackCuts->GetEtaMax())
160                                                 fHistMan->GetPtHistogram(kHistPtGen)->Fill(partMC->Pt(),partMC->IsPhysicalPrimary());                                                                    
161                                   //rapidity cut
162                                   if(partMC->Y() > fTrackCuts->GetYMax()|| partMC->Y() < fTrackCuts->GetYMin() ) 
163                                         continue;       
164                                   if(partMC->IsPhysicalPrimary())
165                                          npar++;    
166                                   // check for true PID + and fill P_t histos 
167                                   Int_t charge = partMC->Charge() > 0 ? kChPos : kChNeg ;
168                                   Int_t id = fPID->GetParticleSpecie(partMC);
169                                   if(id != kSpUndefined) 
170                                   {
171                                         fHistMan->GetHistogram2D(kHistPtGenTruePrimary,id,charge)->Fill(partMC->Pt(),partMC->IsPhysicalPrimary());
172                                   }
173                           }
174                   }
175                   if(ifAODEvent==AliSpectraBothTrackCuts::kESDobject)
176                   {
177                         stack = mcEvent->Stack();
178                         Int_t nMC = stack->GetNtrack();
179                         for (Int_t iMC = 0; iMC < nMC; iMC++)
180                         {
181                                  
182                                 TParticle *partMC = stack->Particle(iMC);
183                                 
184                                 if(!partMC)     
185                                         continue;       
186         
187                                 if(!partMC->GetPDG(0))
188                                         continue;
189                                 if(TMath::Abs(partMC->GetPDG(0)->Charge()/3.0)<0.01) 
190                                         continue;//Skip neutrals
191                                 if(partMC->Eta() > fTrackCuts->GetEtaMin() && partMC->Eta() < fTrackCuts->GetEtaMax())
192                                         fHistMan->GetPtHistogram(kHistPtGen)->Fill(partMC->Pt(),stack->IsPhysicalPrimary(iMC));
193                                 if(partMC->Y()   > fTrackCuts->GetYMax() ||partMC->Y()   < fTrackCuts->GetYMin()  ) 
194                                         continue;
195                                 if(stack->IsPhysicalPrimary(iMC))
196                                          npar++;    
197                                   // check for true PID + and fill P_t histos 
198                                 Int_t charge = partMC->GetPDG(0)->Charge()/3.0 > 0 ? kChPos : kChNeg ;
199                                 Int_t id = fPID->GetParticleSpecie(partMC);
200                                 if(id != kSpUndefined) 
201                                 {
202                                         fHistMan->GetHistogram2D(kHistPtGenTruePrimary,id,charge)->Fill(partMC->Pt(),stack->IsPhysicalPrimary(iMC));
203                                 }
204                           }
205                   }
206         }
207         if(!fdotheMCLoopAfterEventCuts)
208                 if(!fEventCuts->IsSelected(fAOD,fTrackCuts,fIsMC,mcZ))return;//event selection
209         //main loop on tracks
210         Int_t ntracks=0;
211         //cout<<fAOD->GetNumberOfTracks()<<endl;
212         for (Int_t iTracks = 0; iTracks < fAOD->GetNumberOfTracks(); iTracks++) 
213         {
214                 AliVTrack* track = dynamic_cast<AliVTrack*>(fAOD->GetTrack(iTracks));
215                 AliAODTrack* aodtrack=0;
216                 AliESDtrack* esdtrack=0;
217                 Float_t dca=-999.;
218                 Float_t dcaz=-999.;
219                 Short_t ncls=-1; 
220                 Float_t chi2perndf=-1.0;
221                 if(ifAODEvent==AliSpectraBothTrackCuts::kESDobject)
222                 {
223                         esdtrack=dynamic_cast<AliESDtrack*>(track);
224                         if(!esdtrack)
225                                 continue;
226                         esdtrack->GetImpactParameters(dca,dcaz);
227                         ncls=esdtrack->GetTPCNcls();
228                         if ( ncls > 5) 
229                                 chi2perndf=(esdtrack->GetTPCchi2()/Float_t(ncls - 5));
230   
231                         else 
232                                 chi2perndf=-1;
233                         
234                 }
235                 else if (ifAODEvent==AliSpectraBothTrackCuts::kAODobject)
236                 {
237                         aodtrack=dynamic_cast<AliAODTrack*>(track);
238                         if(!aodtrack)
239                                 continue;               
240                         dca=aodtrack->DCA();
241                         dcaz=aodtrack->ZAtDCA();
242                         ncls=aodtrack->GetTPCNcls();
243                         chi2perndf=aodtrack->Chi2perNDF();
244                 }
245                 else
246                         continue;
247                 if (!fTrackCuts->IsSelected(track,kTRUE)) 
248                         continue;
249                 ntracks++;
250                 if(fmakePIDQAhisto)
251                         fPID->FillQAHistos(fHistMan, track, fTrackCuts);
252                 
253                 //calculate DCA for AOD track
254                 if(dca==-999.)
255                 {// track->DCA() does not work in old AOD production
256                         Double_t d[2], covd[3];
257                         AliVTrack* track_clone=(AliVTrack*)track->Clone("track_clone"); // need to clone because PropagateToDCA updates the track parameters
258                         Bool_t isDCA = track_clone->PropagateToDCA(fAOD->GetPrimaryVertex(),fAOD->GetMagneticField(),9999.,d,covd);
259                         delete track_clone;
260                         if(!isDCA)
261                                 d[0]=-999.;
262                         dca=d[0];
263                         dcaz=d[1];      
264                 }
265         
266                 fHistMan->GetPtHistogram(kHistPtRec)->Fill(track->Pt(),dca);  // PT histo
267                 // get identity and charge
268                 Bool_t rec[3]={false,false,false};
269                 Bool_t sel[3]={false,false,false};
270                 Int_t idRec  = fPID->GetParticleSpecie(fHistMan,track, fTrackCuts,rec);
271                 for(int irec=kSpPion;irec<kNSpecies;irec++)
272                 {
273    
274                         if(fUseMinSigma)
275                         {
276                                 if(irec>kSpPion)
277                                         break;
278                         }
279                         else
280                         {       
281                                 if(!rec[irec]) 
282                                         idRec = kSpUndefined;
283                                 else    
284                                         idRec=irec;
285                         }               
286    
287                         Int_t charge = track->Charge() > 0 ? kChPos : kChNeg;
288                 
289                         // Fill histograms, only if inside y and nsigma acceptance
290                         if(idRec != kSpUndefined && fTrackCuts->CheckYCut ((BothParticleSpecies_t)idRec))
291                         {
292                                 fHistMan->GetHistogram2D(kHistPtRecSigma,idRec,charge)->Fill(track->Pt(),dca);
293                                 fTrackCuts->GetHistoDCAzQA()->Fill(idRec,track->Pt(),dcaz);
294                                 fTrackCuts->GetHistoNclustersQA()->Fill(idRec,track->Pt(),ncls);
295                                 fTrackCuts->GetHistochi2perNDFQA()->Fill(idRec,track->Pt(),chi2perndf);
296                                 sel[idRec]=true;
297                         }
298                         //can't put a continue because we still have to fill allcharged primaries, done later
299                 
300                         /* MC Part */
301                         if (arrayMC||stack) 
302                         {
303                                 Bool_t isPrimary           = kFALSE;
304                                 Bool_t isSecondaryMaterial = kFALSE; 
305                                 Bool_t isSecondaryWeak     = kFALSE; 
306                                 Int_t idGen     =kSpUndefined;
307                                 Int_t pdgcode=0;
308                                 Int_t motherpdg=-1;
309                                 if (ifAODEvent==AliSpectraBothTrackCuts::kAODobject)
310                                 {
311                                         AliAODMCParticle *partMC = (AliAODMCParticle*) arrayMC->At(TMath::Abs(track->GetLabel()));
312                                         if (!partMC) 
313                                         { 
314                                                 AliError("Cannot get MC particle");
315                                                 continue; 
316                                         }
317                                         // Check if it is primary, secondary from material or secondary from weak decay
318                                         isPrimary           = partMC->IsPhysicalPrimary();
319                                         isSecondaryWeak     = partMC->IsSecondaryFromWeakDecay();
320                                         isSecondaryMaterial      = partMC->IsSecondaryFromMaterial();
321                                         //cout<<"AOD tagging "<<isPrimary<<" "<<isSecondaryWeak<<isSecondaryMaterial<<" "<<partMC->GetMCProcessCode()<<endl;
322
323                                         if(!isPrimary&&!isSecondaryWeak&&!isSecondaryMaterial)//old tagging for old AODs 
324                                         {
325                                                 AliError("old tagging");
326                                                 Int_t mfl=-999,codemoth=-999;
327                                                 Int_t indexMoth=partMC->GetMother(); // FIXME ignore fakes? TO BE CHECKED, on ESD is GetFirstMother()
328                                                 if(indexMoth>=0)
329                                                 {//is not fake
330                                                         AliAODMCParticle* moth = (AliAODMCParticle*) arrayMC->At(indexMoth);
331                                                         codemoth = TMath::Abs(moth->GetPdgCode());
332                                                         mfl = Int_t (codemoth/ TMath::Power(10, Int_t(TMath::Log10(codemoth))));
333                                                 }
334                                                 //Int_t uniqueID = partMC->GetUniqueID();
335                                                 //cout<<"uniqueID: "<<partMC->GetUniqueID()<<"       "<<kPDecay<<endl;
336                                                 //cout<<"status: "<<partMC->GetStatus()<<"       "<<kPDecay<<endl;
337                                                 // if(uniqueID == kPDecay)Printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
338                                                 if(mfl==3) 
339                                                         isSecondaryWeak     = kTRUE; // add if(partMC->GetStatus() & kPDecay)? FIXME
340                                                 else       
341                                                         isSecondaryMaterial = kTRUE;
342                                         }
343                                         //cout<<"AOD 2 tagging "<<isPrimary<<" "<<isSecondaryWeak<<isSecondaryMaterial<<" "<<partMC->GetMCProcessCode()<<endl;
344                                         if(isSecondaryWeak)
345                                         {       
346                                                 Int_t indexMoth=partMC->GetMother(); // FIXME ignore fakes? TO BE CHECKED, on ESD is GetFirstMother()
347                                                 if(indexMoth>=0)
348                                                 {
349                                                         AliAODMCParticle* moth = (AliAODMCParticle*) arrayMC->At(indexMoth);
350                                                         if(moth)
351                                                                 motherpdg=TMath::Abs(moth->GetPdgCode());
352                                                 }
353                                         }
354
355                                         idGen     = fPID->GetParticleSpecie(partMC);
356                                         pdgcode=partMC->GetPdgCode(); 
357                                 }
358                                 else if (ifAODEvent==AliSpectraBothTrackCuts::kESDobject)
359                                 {
360                                         TParticle *partMC =stack->Particle(TMath::Abs(track->GetLabel()));
361                                         if (!partMC) 
362                                         { 
363                                                 AliError("Cannot get MC particle");
364                                                 continue; 
365                                         }
366                                         isPrimary           = stack->IsPhysicalPrimary(TMath::Abs(track->GetLabel()));
367                                         isSecondaryWeak     = stack->IsSecondaryFromWeakDecay(TMath::Abs(track->GetLabel()));
368                                         isSecondaryMaterial      = stack->IsSecondaryFromMaterial(TMath::Abs(track->GetLabel()));
369                                         //cout<<"ESD tagging "<<isPrimary<<" "<<isSecondaryWeak<<isSecondaryMaterial<<endl;
370                                         
371                                         if(isSecondaryWeak)     
372                                         {
373
374                                                 TParticle* moth=stack->Particle(TMath::Abs(partMC->GetFirstMother()));
375                                                 if(moth)
376                                                          motherpdg = TMath::Abs(moth->GetPdgCode());
377
378                                         }
379                                         
380
381                                         idGen     = fPID->GetParticleSpecie(partMC);
382                                         pdgcode=partMC->GetPdgCode(); 
383                                 }
384                                 else
385                                         return;
386                         
387                         //        cout<<isPrimary<<" "<<isSecondaryWeak<<" "<<isSecondaryMaterial<<endl;
388                         //        cout<<" functions "<<partMC->IsPhysicalPrimary()<<" "<<partMC->IsSecondaryFromWeakDecay()<<" "<<partMC->IsSecondaryFromMaterial()<<endl;
389                   
390                                 if (isPrimary&&irec==kSpPion)
391                                         fHistMan->GetPtHistogram(kHistPtRecPrimaryAll)->Fill(track->Pt(),dca);  // PT histo of reconstrutsed primaries in defined eta
392                   
393                         //nsigma cut (reconstructed nsigma)
394                                 if(idRec == kSpUndefined) 
395                                         continue;
396                   
397                         // rapidity cut (reconstructed pt and identity)
398                                  if(!fTrackCuts->CheckYCut ((BothParticleSpecies_t)idRec)) continue;
399                   
400                   // Get true ID
401                                         
402                   
403                                  if (idRec == idGen) fHistMan->GetHistogram2D(kHistPtRecTrue,  idGen, charge)->Fill(track->Pt(),dca); 
404                   
405                                 if (isPrimary) 
406                                 {
407                                         fHistMan->GetHistogram2D(kHistPtRecSigmaPrimary, idRec, charge)->Fill(track->Pt(),dca); 
408                                         if(idGen != kSpUndefined) 
409                                         {
410                                                 fHistMan->GetHistogram2D(kHistPtRecPrimary,      idGen, charge)->Fill(track->Pt(),dca);
411                                                 if (idRec == idGen) 
412                                                         fHistMan->GetHistogram2D(kHistPtRecTruePrimary,  idGen, charge)->Fill(track->Pt(),dca); 
413                                         }
414                                 }
415                                  //25th Apr - Muons are added to Pions -- FIXME
416                                 if ( pdgcode == 13 && idRec == kSpPion) 
417                                 { 
418                                         fHistMan->GetPtHistogram(kHistPtRecTrueMuonPlus)->Fill(track->Pt(),dca); 
419                                         if(isPrimary)
420                                                 fHistMan->GetPtHistogram(kHistPtRecTruePrimaryMuonPlus)->Fill(track->Pt(),dca); 
421                                 }
422                                 if ( pdgcode == -13 && idRec == kSpPion) 
423                                 { 
424                                         fHistMan->GetPtHistogram(kHistPtRecTrueMuonMinus)->Fill(track->Pt(),dca); 
425                                         if (isPrimary) 
426                                         {
427                                                 fHistMan->GetPtHistogram(kHistPtRecTruePrimaryMuonMinus)->Fill(track->Pt(),dca); 
428                                         }
429                                 }
430                   
431                                  ///..... END FIXME
432                   
433                                 // Fill secondaries
434                                 if(isSecondaryWeak)
435                                 {  
436                                         if(fMotherWDPDGcode>0)
437                                         {
438                                                 if(motherpdg==fMotherWDPDGcode)
439                                                         fHistMan->GetHistogram2D(kHistPtRecSigmaSecondaryWeakDecay, idRec, charge)->Fill(track->Pt(),dca);
440                                         }       
441                                         else    
442                                                 fHistMan->GetHistogram2D(kHistPtRecSigmaSecondaryWeakDecay, idRec, charge)->Fill(track->Pt(),dca);
443                                 }
444                                 if(isSecondaryMaterial)  
445                                         fHistMan->GetHistogram2D(kHistPtRecSigmaSecondaryMaterial , idRec, charge)->Fill(track->Pt(),dca);
446                   
447                         }//end if(arrayMC)
448                 }
449                 if(sel[0]&&sel[1]&&sel[2])//pi+k+p
450                         fHistMan->GetPtHistogram("hHistDoubleCounts")->Fill(track->Pt(),0);
451                 else if(sel[0]&&sel[1]) //pi+k
452                         fHistMan->GetPtHistogram("hHistDoubleCounts")->Fill(track->Pt(),1);
453                 else if(sel[0]&&sel[2]) //pi+k
454                         fHistMan->GetPtHistogram("hHistDoubleCounts")->Fill(track->Pt(),2);
455                 else if(sel[1]&&sel[2]) //p+k
456                         fHistMan->GetPtHistogram("hHistDoubleCounts")->Fill(track->Pt(),3);
457
458         
459         
460         } // end loop on tracks
461
462  // cout<< ntracks<<endl;
463   fHistMan->GetGenMulvsRawMulHistogram("hHistGenMulvsRawMul")->Fill(npar,ntracks);
464   PostData(1, fHistMan  );
465   PostData(2, fEventCuts);
466   PostData(3, fTrackCuts);
467   PostData(4, fPID      );
468 }
469
470 //_________________________________________________________________
471 void   AliAnalysisTaskSpectraBoth::Terminate(Option_t *)
472 {
473   // Terminate
474 }