Coverity fixes (Philippe, Ivana)
[u/mrichter/AliRoot.git] / PWG3 / base / AliAnalysisTaskPtMothFromPtDaugh.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 /* $Id$ */
17
18 //////////////////////////////////////////////////////////////////////////
19 //                                                                      //
20 //             Class AnalysisTaskAliPtMothFromPtDaugh                   //
21 //   AnalysisTaskSE used for the reconstruction of mothers particles    //
22 //   spectra (pT and pTMin) starting from the pT-spectra of             //
23 //   daughters particles.                                               //
24 //                                                                      //
25 //             Authors: Giuseppe Bruno & Fiorella Fionda                //
26 //                                                                      //
27 //////////////////////////////////////////////////////////////////////////
28
29 #include <TChain.h>
30
31 #include "AliAnalysisManager.h"
32 #include "AliMCEventHandler.h"
33 #include "AliMCEvent.h"
34 #include "AliStack.h"
35 #include "TNtuple.h"
36 #include "TH1F.h"
37 #include "TFile.h"
38 #include "AliLog.h"
39 #include "AliAnalysisTaskPtMothFromPtDaugh.h"
40 #include "AliPtMothFromPtDaugh.h"
41 #include "AliAnalysisTaskSE.h"
42
43 ClassImp(AliAnalysisTaskPtMothFromPtDaugh)
44
45 //_________________________________________________________________________________
46 AliAnalysisTaskPtMothFromPtDaugh::AliAnalysisTaskPtMothFromPtDaugh() :
47   AliAnalysisTaskSE(),
48   fPtMothDaugh(0),
49   fDecayKine(0),
50   fReadKineFromNtupla(0),
51   fFileNtuplaName(0),
52   fList(0)
53 {
54   //
55   // Default Constructor
56   //
57   AliInfo("Default Constructor!\n");  
58 }
59
60 //_________________________________________________________________________________
61 AliAnalysisTaskPtMothFromPtDaugh::AliAnalysisTaskPtMothFromPtDaugh(Bool_t IsNtuplaCreated) :
62   AliAnalysisTaskSE("TaskAliPtMothFromDaugh"),
63   fPtMothDaugh(0),
64   fDecayKine(0),
65   fReadKineFromNtupla(IsNtuplaCreated),
66   fFileNtuplaName(0),
67   fList(0)
68 {
69     //
70     // Basic AnalysisTaskSE Constructor  
71     // Basic input of the analysis: TChain of galice.root files (optional)
72     // Basic ouput: TList of mothers histograms (pT and pTMin) (standard)
73     //              TNtupla with kinematics informations of mothers 
74     //              and daughters (optional)
75     // If Ntupla already exists loop on kinematics is not needed
76     // therefore input is not defined
77     //
78     if(!IsNtuplaCreated){
79            DefineInput(0,TChain::Class()); DefineOutput(2,TNtuple::Class());}
80     else {AliInfo("Ntupla is already created! Loop on events is skipped!\n");}
81     DefineOutput(1,TList::Class());
82 }
83
84 //___________________________________________________________________________________
85 AliAnalysisTaskPtMothFromPtDaugh::~AliAnalysisTaskPtMothFromPtDaugh()
86    {
87     //
88     // Destructor
89     //
90     if(fPtMothDaugh)
91      { delete fPtMothDaugh; fPtMothDaugh=0;}
92     if(fDecayKine)
93      { delete fDecayKine; fDecayKine=0;}
94     if(fFileNtuplaName)
95      { delete fFileNtuplaName; fFileNtuplaName=0;}
96     if (fList)
97      { delete fList; fList = 0; }
98    }
99
100
101
102 //_________________________________________________________________________________
103 void AliAnalysisTaskPtMothFromPtDaugh::UserCreateOutputObjects()
104 {
105   //
106   // Initialise the framework objects
107   //
108    //
109     // Initialise the framework objects. OutPut objects created are:
110     // 1) TList with mothers histograms
111     // 2) TNtuple with kinematics informations of mothers and daughters (optional)  
112     //
113     fList = new TList();
114     fList->SetOwner();
115     if(fReadKineFromNtupla) return;
116
117     if(!AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler()) {
118      Fatal("UserCreateOutputObjects", "This task needs a MC handler");
119      return;
120      }
121     fDecayKine=new TNtuple("DecayKine","Decay kinematics","pdgM:pxM:pyM:pzM:yM:etaM:pdgD:pxD:pyD:pzD:yD:etaD");
122     return;
123 }
124
125 //_________________________________________________________________________________
126 void AliAnalysisTaskPtMothFromPtDaugh::UserExec(Option_t */*option*/)
127 {
128     //
129     // Main loop. Called for every event
130     // This loop fill a TNtuple with kinematics informations for 
131     // mother and daughter particles. TNtupla contains:  
132     //                   pdg of Mothers
133     //                    px      "
134     //                    py      "
135     //                    pz      "
136     //                  rapidity  "
137     //                   eta      "
138     //                   pdg of Daughter
139     //                    px      "
140     //                    py      "
141     //                    pz      "
142     //                  rapidity  "
143     //                   eta      "         
144     //
145     AliMCEventHandler* mcHandler = dynamic_cast<AliMCEventHandler*>(AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
146     if(!mcHandler) {AliError("Could not get MC event handler!"); return; }
147     mcHandler->SetReadTR(kFALSE);
148     AliMCEvent* mcEvent = mcHandler->MCEvent();
149     if(!mcEvent){ AliError("Could not get MC event!"); return; }
150     AliStack* stack = mcEvent->Stack();
151     if(!stack){ AliError("Could not get stack!"); return; }
152     Int_t nPrims = stack->GetNprimary();
153     float *inf = new float[12];
154     for (Int_t iTrack = 0; iTrack < nPrims; ++iTrack) {
155     TParticle *part = stack->Particle(iTrack);
156     Int_t pdg=TMath::Abs(part->GetPdgCode());
157     Double_t y,y2;
158     //check if particle is in mothers list
159     Int_t labelDaugh;
160     if(fPtMothDaugh->IsMothers(pdg))
161            {
162            //check if mother particle has a selected daugh
163            if(!fPtMothDaugh->IsSelectedDaugh(part,labelDaugh,stack)) continue;
164            TParticle *pDaugh=stack->Particle(labelDaugh);
165            fPtMothDaugh->Rapidity(part,y);
166            fPtMothDaugh->Rapidity(pDaugh,y2);
167
168            inf[0]=part->GetPdgCode();
169            inf[1]=part->Px();
170            inf[2]=part->Py();
171            inf[3]=part->Pz();
172            inf[4]=y;
173            inf[5]=part->Eta();
174            inf[6]=pDaugh->GetPdgCode();
175            inf[7]=pDaugh->Px();
176            inf[8]=pDaugh->Py();
177            inf[9]=pDaugh->Pz();
178            inf[10]=y2;
179            inf[11]=pDaugh->Eta();
180            fDecayKine->Fill(inf);
181          } //close if statement for mothers particles
182         } //end of tracks loop
183     PostData(2,fDecayKine);
184     PostData(1,fList);
185     delete [] inf;
186     return;
187 }
188
189 //___________________________________________________________________________________
190 void AliAnalysisTaskPtMothFromPtDaugh::Terminate(Option_t */*option*/)
191    {
192     //
193     // Terminate method called at the end of the events loop.
194     // Get the Ntupla with kineamtics informations after the
195     // events loop or read it from the file fFileNtuplaName
196     // if the Ntupla is already created. Then use it to
197     // evaluate pT and pTMin spectra for mothers by means
198     // of the method implemented in AliPtMothFromPtDaugh
199     //
200     if(fReadKineFromNtupla) fDecayKine = ReadNtuplaFromFile(fFileNtuplaName);
201     else{ 
202          fDecayKine = dynamic_cast<TNtuple*>(GetOutputData(2));
203          fList = dynamic_cast<TList*>(GetOutputData(1));
204         }
205     if(!fDecayKine) { AliInfo("TNtupla not available!\n"); return; }
206     if(!fList) { AliInfo("TList not availble!\n"); return; }
207     fPtMothDaugh->SetDecayNtupla(fDecayKine);
208     fPtMothDaugh->EvaluatePtMoth();
209     TH1F *fHistoPt = (TH1F*)fPtMothDaugh->GetHistoPtMother()->Clone();
210     TH1F *fHistoPtMin = (TH1F*)fPtMothDaugh->GetHistoPtMinMother()->Clone();
211     fList->Add(fHistoPt);
212     fList->Add(fHistoPtMin);
213     PostData(1,fList);
214     return;
215    }
216
217 //___________________________________________________________________________________
218 TNtuple *AliAnalysisTaskPtMothFromPtDaugh::ReadNtuplaFromFile(char *inFileName)
219    {
220     // 
221     // Get Ntupla from the file inFileName
222     // after the it is created. 
223     // Input: name of the file - Output: TNtupla
224     //
225     TFile *f = new TFile(inFileName,"READ");
226     if(!f) {AliError(Form("File %s with TNtupla doesn't exist!",inFileName)); return 0x0;}
227     TNtuple *DecayKine=(TNtuple*)f->Get("DecayKine");
228     if(!DecayKine) { AliError("The TNtupla doesn't exist!\n"); return 0x0;}
229     return DecayKine;
230    }