]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG3/dielectron/AliDielectronMC.cxx
Coverity
[u/mrichter/AliRoot.git] / PWG3 / dielectron / AliDielectronMC.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 //#                                                   # 
18 //#              Class AliDielectronMC                #
19 //#       Cut Class for Jpsi->e+e- analysis           #
20 //#                                                   #
21 //#   by WooJin J. Park, GSI / W.J.Park@gsi.de        #
22 //#                                                   #
23 //#####################################################
24
25 #include <AliAnalysisManager.h>
26 #include <AliAODHandler.h>
27 #include <AliESDInputHandler.h>
28 #include <AliAODInputHandler.h>
29 #include <AliMCEventHandler.h>
30 #include <AliMCEvent.h>
31 #include <AliMCParticle.h>
32 #include <AliAODMCParticle.h>
33 #include <AliStack.h>
34 #include <AliESDEvent.h>
35 #include <AliAODEvent.h>
36 #include <AliESDtrack.h>
37 #include <AliAODTrack.h>
38 #include <AliLog.h>
39
40 #include <TClonesArray.h>
41 #include "AliDielectronMC.h"
42
43 AliDielectronMC* AliDielectronMC::fgInstance=0x0;
44
45 //____________________________________________________________
46 AliDielectronMC* AliDielectronMC::Instance()
47 {
48   //
49   // return pointer to singleton implementation
50   //
51   if (fgInstance) return fgInstance;
52
53   AnalysisType type=kUNSET;
54   Bool_t hasMC=kFALSE;
55   if (AliAnalysisManager::GetAnalysisManager()){
56     if (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()->IsA()==AliESDInputHandler::Class()) type=kESD;
57     else if (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()->IsA()==AliAODInputHandler::Class()) type=kAOD;
58
59     AliMCEventHandler* mcHandler = dynamic_cast<AliMCEventHandler*> (AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
60     if(type == kESD) hasMC=mcHandler!=0x0;
61     }
62  
63   fgInstance=new AliDielectronMC(type);
64  
65   fgInstance->SetHasMC(hasMC);
66    
67   return fgInstance;
68 }
69
70 //____________________________________________________________
71 AliDielectronMC::AliDielectronMC(AnalysisType type):
72   fMCEvent(0x0),
73   fStack(0x0),
74   fAnaType(type),
75   fHasMC(kTRUE),
76   fMcArray(0x0)
77 {
78   //
79   // default constructor
80   //
81 }
82
83
84 //____________________________________________________________
85 AliDielectronMC::~AliDielectronMC()
86 {
87   //
88   // default destructor
89   //
90   
91 }
92
93 //____________________________________________________________
94 void AliDielectronMC::Initialize()
95 {
96   //
97   // initialize MC class
98   //
99   if (!ConnectMCEvent()) AliError("Initialization of MC object failed!");
100 }
101
102 //____________________________________________________________
103 Int_t AliDielectronMC::GetNMCTracks()
104 {
105   //
106   //  return the number of generated tracks from MC event
107   //
108   if(fAnaType == kESD){
109     if (!fMCEvent){ AliError("No fMCEvent"); return 0; }
110     return fMCEvent->GetNumberOfTracks();}
111   else if(fAnaType == kAOD){
112     if(!fMcArray) { AliError("No fMcArray"); return 0; }
113     return fMcArray->GetEntriesFast();
114   }
115   return 0;
116 }
117
118 //____________________________________________________________
119 Int_t AliDielectronMC::GetNMCTracksFromStack()
120 {
121   //
122   //  return the number of generated tracks from stack
123   //
124   if (!fStack){ AliError("No fStack"); return -999; }
125   return fStack->GetNtrack();
126 }
127
128 //____________________________________________________________
129 Int_t AliDielectronMC::GetNPrimary()
130 {
131   //
132   //  return the number of primary track from MC event
133   //
134   if (!fMCEvent){ AliError("No fMCEvent"); return 0; }
135   return fMCEvent->GetNumberOfPrimaries();
136 }
137
138 //____________________________________________________________
139 Int_t AliDielectronMC::GetNPrimaryFromStack()
140 {
141   //
142   //  return the number of primary track from stack
143   //
144   if (!fStack){ AliError("No fStack"); return -999; }
145   return fStack->GetNprimary();
146 }
147
148 //____________________________________________________________
149 AliVParticle* AliDielectronMC::GetMCTrackFromMCEvent(Int_t itrk)
150 {
151   //
152   // return MC track directly from MC event
153   //
154   if (itrk<0) return NULL;
155   AliVParticle * track=0x0;
156   if(fAnaType == kESD){
157     if (!fMCEvent){ AliError("No fMCEvent"); return NULL;}
158     track = fMCEvent->GetTrack(itrk); //  tracks from MC event (ESD)
159   } else if(fAnaType == kAOD) {
160     if (!fMcArray){ AliError("No fMCEvent"); return NULL;}
161     track = (AliVParticle*)fMcArray->At(itrk); //  tracks from MC event (AOD)
162   }
163   return track;
164 }
165
166 //____________________________________________________________
167 Bool_t AliDielectronMC::ConnectMCEvent()
168 {
169   //
170   // connect stack object from the mc handler
171   //
172   if(fAnaType == kESD){
173     fMCEvent=0x0;
174     AliMCEventHandler* mcHandler = dynamic_cast<AliMCEventHandler*> (AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
175     if (!mcHandler){ AliError("Could not retrive MC event handler!"); return kFALSE; }
176     if (!mcHandler->InitOk() ) return kFALSE;
177     if (!mcHandler->TreeK() )  return kFALSE;
178     if (!mcHandler->TreeTR() ) return kFALSE;
179     
180     AliMCEvent* mcEvent = mcHandler->MCEvent();
181     if (!mcEvent){ AliError("Could not retrieve MC event!"); return kFALSE; }
182     fMCEvent = mcEvent;
183     
184     if (!UpdateStack()) return kFALSE;
185   }
186   else if(fAnaType == kAOD)
187   {
188     fMcArray = 0x0;
189     AliAODEvent *aod=((AliAODInputHandler*)((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler()))->GetEvent();
190     fMcArray = dynamic_cast<TClonesArray*>(aod->FindListObject(AliAODMCParticle::StdBranchName()));
191     if(!fMcArray) return kFALSE;
192   }
193   return kTRUE;
194 }
195
196 //____________________________________________________________
197 Bool_t AliDielectronMC::UpdateStack()
198 {
199   //
200   // update stack with new event
201   //
202   if (!fMCEvent){ AliError("No fMCEvent"); return kFALSE;}
203   AliStack* stack = fMCEvent->Stack();
204   if (!stack){ AliError("Could not retrive stack!"); return kFALSE; }
205   fStack = stack;
206   return kTRUE;
207 }
208
209 //____________________________________________________________
210 AliMCParticle* AliDielectronMC::GetMCTrack( const AliESDtrack* _track)
211 {
212   //
213   // return MC track
214   //
215   if (!fMCEvent){ AliError("No fMCEvent"); return NULL;}
216
217   Int_t nStack = fMCEvent->GetNumberOfTracks();
218   Int_t label = TMath::Abs(_track->GetLabel());
219   if(label>nStack)return NULL;
220
221   AliMCParticle *mctrack = dynamic_cast<AliMCParticle *>(fMCEvent->GetTrack(label));
222   return mctrack;
223 }
224
225
226 //____________________________________________________________
227 AliAODMCParticle* AliDielectronMC::GetMCTrack( const AliAODTrack* _track)
228 {
229   //
230   // return MC track
231   //
232  if(!fMcArray) { AliError("No fMCArray"); return NULL;}
233  Int_t label = _track->GetLabel();
234  if(label < 0) return NULL;
235  AliAODMCParticle *mctrack = (AliAODMCParticle*)fMcArray->At(label);
236  return mctrack; 
237 }
238
239 //____________________________________________________________
240 TParticle* AliDielectronMC::GetMCTrackFromStack(const AliESDtrack* _track)
241 {
242   //
243   // return MC track from stack
244   //
245   Int_t label = TMath::Abs(_track->GetLabel());
246   if (!fStack) AliWarning("fStack is not available. Update stack first.");
247   TParticle* mcpart = fStack->Particle(label);
248   if (!mcpart) return NULL;
249   return mcpart;
250 }
251
252 //____________________________________________________________
253 AliMCParticle* AliDielectronMC::GetMCTrackMother(const AliESDtrack* _track)
254 {
255   //
256   // return MC track mother
257   //
258   AliMCParticle* mcpart = GetMCTrack(_track);
259   if (!mcpart) return NULL;
260   AliMCParticle* mcmother = dynamic_cast<AliMCParticle *>(fMCEvent->GetTrack(mcpart->GetMother()));
261   if (!mcmother) return NULL;
262   return mcmother;
263 }
264
265 //______________________________________________________________
266 AliAODMCParticle* AliDielectronMC::GetMCTrackMother(const AliAODTrack* _track)
267 {
268  //
269  // return MC track mother
270  //
271  AliAODMCParticle* mcpart = GetMCTrack(_track);
272  if (!mcpart) return NULL;
273  if(mcpart->GetMother() < 0) return NULL;
274  AliAODMCParticle* mcmother = dynamic_cast<AliAODMCParticle *>(fMcArray->At(mcpart->GetMother()));
275  if (!mcmother) return NULL;
276  return mcmother;
277 }
278 //____________________________________________________________
279 AliMCParticle* AliDielectronMC::GetMCTrackMother(const AliMCParticle* _particle){
280   //
281   // return MC track mother
282   //
283   AliMCParticle* mcmother = dynamic_cast<AliMCParticle *>(fMCEvent->GetTrack(_particle->GetMother()));
284   return mcmother;
285 }
286
287 //____________________________________________________________
288 AliAODMCParticle* AliDielectronMC::GetMCTrackMother(const AliAODMCParticle* _particle){
289   //
290   // return MC track mother
291   //
292   if( _particle->GetMother() < 0) return NULL;
293   AliAODMCParticle* mcmother = dynamic_cast<AliAODMCParticle *>(fMcArray->At(_particle->GetMother()));
294   return mcmother;
295 }
296
297 //____________________________________________________________
298 TParticle* AliDielectronMC::GetMCTrackMotherFromStack(const AliESDtrack* _track)
299 {
300   //
301   // return MC track mother from stack
302   //
303   TParticle* mcpart = GetMCTrackFromStack(_track);
304   if ( !mcpart || mcpart->GetFirstMother()<=0 ) return NULL; 
305   TParticle* mcmother = fStack->Particle(mcpart->GetFirstMother());
306   if (!mcmother) return NULL;
307   return mcmother;
308 }
309
310 //____________________________________________________________
311 Int_t AliDielectronMC::GetMCPID(const AliESDtrack* _track)
312 {
313   //
314   // return PDG code of the track from the MC truth info
315   //
316   AliMCParticle* mcpart = GetMCTrack(_track);
317   if (!mcpart) return -999;
318   return mcpart->PdgCode();
319 }
320
321 //__________________________________________________________
322 Int_t AliDielectronMC::GetMCPID(const AliAODTrack* _track)
323 {
324  //
325  // return PDG code of the track from the MC truth info
326  //
327  AliAODMCParticle* mcpart = GetMCTrack(_track);
328  if (!mcpart) return -999;
329  return mcpart->PdgCode();
330 }
331
332 //____________________________________________________________
333 Int_t AliDielectronMC::GetMCPIDFromStack(const AliESDtrack* _track)
334 {
335   // 
336   // return MC PDG code from stack
337   //
338   TParticle* mcpart = GetMCTrackFromStack(_track);
339   if (!mcpart) return -999;
340   return mcpart->GetPdgCode();
341 }
342
343 //____________________________________________________________
344 Int_t AliDielectronMC::GetMotherPDG( const AliESDtrack* _track)
345 {
346   //
347   // return PDG code of the mother track from the MC truth info
348   //
349   AliMCParticle* mcmother = GetMCTrackMother(_track);
350   if (!mcmother) return -999;
351   return mcmother->PdgCode();
352 }
353
354 //________________________________________________________
355 Int_t AliDielectronMC::GetMotherPDG( const AliAODTrack* _track)
356 {
357   //
358   // return PDG code of the mother track from the MC truth info
359   //
360   AliAODMCParticle* mcmother = GetMCTrackMother(_track);
361   if (!mcmother) return -999;
362   return mcmother->PdgCode();
363 }
364
365 //____________________________________________________________
366 Int_t AliDielectronMC::GetMotherPDGFromStack(const AliESDtrack* _track)
367 {
368   //
369   // return PDG code of the mother track from stack
370   //
371   TParticle* mcmother = GetMCTrackMotherFromStack(_track);
372   if (!mcmother) return -999;
373   return mcmother->GetPdgCode();
374 }
375
376 //____________________________________________________________
377 Int_t AliDielectronMC::GetMCProcess(const AliESDtrack* _track)
378 {
379   //
380   // return process number of the track
381   //
382   AliMCParticle* mcpart = GetMCTrack(_track);
383   if (!mcpart) return -999;
384   return 0;
385 }
386
387 //____________________________________________________________
388 Int_t AliDielectronMC::GetMCProcessFromStack(const AliESDtrack* _track)
389 {
390   //
391   // return process number of the track
392   //
393   TParticle* mcpart = GetMCTrackFromStack(_track);
394   if (!mcpart) return -999;
395   return mcpart->GetUniqueID();
396 }
397
398 //____________________________________________________________
399 Int_t AliDielectronMC::NumberOfDaughters(const AliESDtrack* track)
400 {
401   //
402   // returns the number of daughters
403   //
404   AliMCParticle *mcmother=GetMCTrackMother(track);
405   if(!mcmother||!mcmother->Particle()) return -999;
406 //   return mcmother->GetFirstDaughter()>0?mcmother->GetLastDaughter()-mcmother->GetFirstDaughter()+1:0;
407   return mcmother->Particle()->GetNDaughters();
408 }
409
410 //_________________________________________________________
411 Int_t AliDielectronMC::NumberOfDaughters(const AliAODTrack* track)
412 {
413   //
414   // returns the number of daughters
415   //
416   AliAODMCParticle *mcmother=GetMCTrackMother(track);
417   if(!mcmother) return -999;
418   return NumberOfDaughters(mcmother);  
419
420 }
421
422 //____________________________________________________________
423 Int_t AliDielectronMC::NumberOfDaughters(const AliMCParticle* particle)
424 {
425   //
426   // returns the number of daughters
427   //
428   AliMCParticle *mcmother=GetMCTrackMother(particle);
429   if(!mcmother||!mcmother->Particle()) return -999;
430   //return mcmother->GetFirstDaughter()>0?mcmother->GetLastDaughter()-mcmother->GetFirstDaughter()+1:0;
431   return mcmother->Particle()->GetNDaughters();
432 }
433
434 //____________________________________________________________
435 Int_t AliDielectronMC::NumberOfDaughters(const AliAODMCParticle* particle)
436 {
437   //
438   // returns the number of daughters
439   //
440   AliAODMCParticle *mcmother=GetMCTrackMother(particle);
441   if(!mcmother) return -999;
442   return mcmother->GetNDaughters();
443 }
444
445 //____________________________________________________________
446 Int_t AliDielectronMC::GetMCProcessMother(const AliESDtrack* _track)
447 {
448   //
449   // return process number of the mother of the track
450   //
451   AliMCParticle* mcmother = GetMCTrackMother(_track);
452   if (!mcmother) return -999;
453   return 0;
454 }
455
456 //____________________________________________________________
457 Int_t AliDielectronMC::GetMCProcessMotherFromStack(const AliESDtrack* _track)
458 {
459   //
460   // return process number of the mother of the track
461   //
462   TParticle* mcmother = GetMCTrackMotherFromStack(_track);
463   if (!mcmother) return -999;
464   return mcmother->GetUniqueID();
465 }
466
467 //____________________________________________________________
468 Bool_t AliDielectronMC::IsMCMotherToEE(const AliVParticle *particle, Int_t pdgMother)
469 {
470   //
471   // Check if the Mother 'particle' is of type pdgMother and decays to e+e-
472   //
473   if (fAnaType==kESD && !fMCEvent) return kFALSE;
474   if (fAnaType==kAOD && !fMcArray) return kFALSE;
475   if (!particle) return kFALSE;
476   
477   if (particle->IsA()==AliMCParticle::Class()){
478     return IsMCMotherToEEesd(static_cast<const AliMCParticle*>(particle),pdgMother);
479   } else if (particle->IsA()==AliAODMCParticle::Class()){
480    return IsMCMotherToEEaod(static_cast<const AliAODMCParticle*>(particle),pdgMother);
481   } else {
482     AliError("Unknown particle type");
483   }
484   return kFALSE;
485 }
486
487 //____________________________________________________________
488 Bool_t AliDielectronMC::IsMCMotherToEEesd(const AliMCParticle *particle, Int_t pdgMother)
489 {
490   //
491   // Check if the Mother 'particle' is of type pdgMother and decays to e+e-
492   // ESD case
493   //
494   
495   //check pdg code
496   if (particle->PdgCode()!=pdgMother) return kFALSE;
497   Int_t ifirst = particle->GetFirstDaughter();
498   Int_t ilast  = particle->GetLastDaughter();
499   
500   //check number of daughters
501   if ((ilast-ifirst)!=1) return kFALSE;
502   AliMCParticle *firstD=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(ifirst));
503   AliMCParticle *secondD=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(ilast));
504
505   //TODO: check how you can get rid of the hardcoded numbers. One should make use of the PdgCodes set in AliDielectron!!!
506   if (firstD->Charge()>0){
507     if (firstD->PdgCode()!=-11) return kFALSE;
508     if (secondD->PdgCode()!=11) return kFALSE;
509   }else{
510     if (firstD->PdgCode()!=11) return kFALSE;
511     if (secondD->PdgCode()!=-11) return kFALSE;
512   }
513   
514   return kTRUE;
515 }
516
517 //____________________________________________________________
518 Bool_t AliDielectronMC::IsMCMotherToEEaod(const AliAODMCParticle *particle, Int_t pdgMother)
519 {
520   //
521   // Check if the Mother 'particle' is of type pdgMother and decays to e+e-
522   // AOD case
523   //
524
525   if (particle->GetPdgCode()!=pdgMother) return kFALSE;
526   if (particle->GetNDaughters()!=2) return kFALSE;
527  
528   Int_t ifirst = particle->GetDaughter(0);
529   Int_t ilast  = particle->GetDaughter(1);
530   
531   //check number of daughters
532   if ((ilast-ifirst)!=1) return kFALSE;
533   
534   AliAODMCParticle *firstD=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(ifirst));
535   AliAODMCParticle *secondD=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(ilast));
536    
537   //TODO: check how you can get rid of the hardcoded numbers. One should make use of the PdgCodes set in AliDielectron!!!
538  
539   if (firstD->Charge()>0){
540     if (firstD->GetPdgCode()!=-11) return kFALSE;
541     if (secondD->GetPdgCode()!=11) return kFALSE;
542   }else{
543     if (firstD->GetPdgCode()!=11) return kFALSE;
544     if (secondD->GetPdgCode()!=-11) return kFALSE;
545   }
546   return kTRUE;
547 }
548
549 //____________________________________________________________
550 Int_t AliDielectronMC::GetLabelMotherWithPdg(const AliVParticle *particle1, const AliVParticle *particle2, Int_t pdgMother)
551 {
552   //
553   // test if mother of particle 1 and 2 has pdgCode pdgMother and is the same;
554   //
555   if (fAnaType==kESD){
556   if (!fMCEvent) return -1;
557   return GetLabelMotherWithPdgESD(particle1, particle2, pdgMother);
558   }
559   else if (fAnaType==kAOD)
560   {
561   if (!fMcArray) return -1;
562   return GetLabelMotherWithPdgAOD(particle1, particle2, pdgMother);
563   }  
564
565   return -1;
566 }
567
568 //____________________________________________________________
569 Int_t AliDielectronMC::GetLabelMotherWithPdgESD(const AliVParticle *particle1, const AliVParticle *particle2, Int_t pdgMother)
570 {
571   //
572   // test if mother of particle 1 and 2 has pdgCode +-11 (electron),
573   //    have the same mother and the mother had pdg code pdgMother
574   // ESD case
575   //TODO: check how you can get rid of the hardcoded numbers. One should make use of the PdgCodes set in AliDielectron!!!
576   //
577   
578   AliMCParticle *mcPart1=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(particle1->GetLabel()));
579   AliMCParticle *mcPart2=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(particle2->GetLabel()));
580   
581   if (!mcPart1||!mcPart2) return -1;
582   
583   Int_t lblMother1=mcPart1->GetMother();
584   Int_t lblMother2=mcPart2->GetMother();
585   
586   AliMCParticle *mcMother1=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(lblMother1));
587   if (!mcMother1) return -1;
588   if (lblMother1!=lblMother2) return -1;
589   if (TMath::Abs(mcPart1->PdgCode())!=11) return -1;
590   if (mcPart1->PdgCode()!=-mcPart2->PdgCode()) return -1;
591   if (mcMother1->PdgCode()!=pdgMother) return -1;
592   
593   return lblMother1;
594 }
595
596 //____________________________________________________________
597 Int_t AliDielectronMC::GetLabelMotherWithPdgAOD(const AliVParticle *particle1, const AliVParticle *particle2, Int_t pdgMother)
598 {
599   //
600   // test if mother of particle 1 and 2 has pdgCode +-11 (electron),
601   //    have the same mother and the mother had pdg code pdgMother
602   // AOD case
603   //TODO: check how you can get rid of the hardcoded numbers. One should make use of the PdgCodes set in AliDielectron!!!
604   //
605   AliAODMCParticle *mcPart1=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(particle1->GetLabel()));
606   AliAODMCParticle *mcPart2=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(particle2->GetLabel()));
607   
608   if (!mcPart1||!mcPart2) return -1;
609   
610   Int_t lblMother1=mcPart1->GetMother();
611   Int_t lblMother2=mcPart2->GetMother();
612   
613   AliAODMCParticle *mcMother1=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(lblMother1));
614   
615   if (!mcMother1) return -1;
616   if (lblMother1!=lblMother2) return -1;
617   if (TMath::Abs(mcPart1->GetPdgCode())!=11) return -1;
618   if (mcPart1->GetPdgCode()!=-mcPart2->GetPdgCode()) return -1;
619   if (mcMother1->GetPdgCode()!=pdgMother) return -1;
620   
621   return lblMother1;
622 }
623
624 //____________________________________________________________
625 void AliDielectronMC::GetDaughters(const TObject *mother, AliVParticle* &d1, AliVParticle* &d2)
626 {
627   //
628   // Get First two daughters of the mother
629   //
630   Int_t lblD1=-1;
631   Int_t lblD2=-1;
632   d1=0;
633   d2=0;
634   if (fAnaType==kAOD){
635     if(!fMcArray) return;
636     const AliAODMCParticle *aodMother=static_cast<const AliAODMCParticle*>(mother);
637     lblD1=aodMother->GetDaughter(0);
638     lblD2=aodMother->GetDaughter(1);
639     d1 = (AliVParticle*)fMcArray->At(lblD1);
640     d2 = (AliVParticle*)fMcArray->At(lblD2);
641    } else if (fAnaType==kESD){
642     if (!fMCEvent) return;
643     const AliMCParticle *aodMother=static_cast<const AliMCParticle*>(mother);
644     lblD1=aodMother->GetFirstDaughter();
645     lblD2=aodMother->GetLastDaughter();
646     d1=fMCEvent->GetTrack(lblD1);
647     d2=fMCEvent->GetTrack(lblD2);
648    }
649 }
650
651 //____________________________________________________________
652 Bool_t AliDielectronMC::HaveSameMother(const AliDielectronPair * pair)
653 {
654   //
655   // Check whether two particles have the same mother
656   //
657
658   const AliVParticle * daughter1 = pair->GetFirstDaughter();
659   const AliVParticle * daughter2 = pair->GetSecondDaughter();
660
661   AliVParticle *mcDaughter1=GetMCTrackFromMCEvent(daughter1->GetLabel());
662   AliVParticle *mcDaughter2=GetMCTrackFromMCEvent(daughter2->GetLabel());
663   if (!mcDaughter1 || !mcDaughter2) return 0;
664
665   Int_t labelMother1=-1;
666   Int_t labelMother2=-1;
667
668   if (mcDaughter1->IsA()==AliMCParticle::Class()){
669     labelMother1=(static_cast<AliMCParticle*>(mcDaughter1))->GetMother();
670     labelMother2=(static_cast<AliMCParticle*>(mcDaughter2))->GetMother();
671   } else if (mcDaughter1->IsA()==AliAODMCParticle::Class()) {
672     labelMother1=(static_cast<AliAODMCParticle*>(mcDaughter1))->GetMother();
673     labelMother2=(static_cast<AliAODMCParticle*>(mcDaughter2))->GetMother();
674   }
675
676   Bool_t sameMother=(labelMother1>-1)&&(labelMother2>-1)&&(labelMother1==labelMother2);
677
678   return sameMother;
679 }
680
681 //________________________________________________________________
682 Int_t AliDielectronMC::IsJpsiPrimary(const AliDielectronPair * pair)
683 {
684  // return: "0" for primary jpsi 
685  //         "1" for secondary jpsi (from beauty)
686  //         "2" for background  
687  if(!HaveSameMother(pair)) return 2;
688  AliVParticle *mcDaughter1=GetMCTrackFromMCEvent((pair->GetFirstDaughter())->GetLabel());
689  Int_t labelMother=-1;
690
691   if (mcDaughter1->IsA()==AliMCParticle::Class()){
692      labelMother=(static_cast<AliMCParticle*>(mcDaughter1))->GetMother();
693      } else if (mcDaughter1->IsA()==AliAODMCParticle::Class()) {
694      labelMother=(static_cast<AliAODMCParticle*>(mcDaughter1))->GetMother();
695      }
696
697  AliVParticle* mcMother=GetMCTrackFromMCEvent(labelMother);
698  if(!IsMCMotherToEE(mcMother,443)) return 2;
699  return IsJpsiPrimary(mcMother);
700 }
701
702 //______________________________________________________________
703 Int_t AliDielectronMC::IsJpsiPrimary(const AliVParticle * particle)
704 {
705   // return: "0" for primary jpsi
706   //         "1" for secondary jpsi (come from B decay)
707  Int_t labelMoth=-1;
708  Int_t pdgCode;
709
710  if (particle->IsA()==AliMCParticle::Class()){
711      labelMoth = (static_cast<const AliMCParticle*>(particle))->GetMother();
712      while(labelMoth>0){
713        particle = GetMCTrackFromMCEvent(labelMoth);
714        pdgCode = TMath::Abs((static_cast<const AliMCParticle*>(particle))->PdgCode());
715        if((pdgCode>500 && pdgCode<600) || (pdgCode>5000 && pdgCode<6000)) return 1;
716        labelMoth = (static_cast<const AliMCParticle*>(particle))->GetMother();
717        }
718     }
719  else if (particle->IsA()==AliAODMCParticle::Class()){
720      labelMoth = (static_cast<const AliAODMCParticle*>(particle))->GetMother();
721      while(labelMoth>0){
722      particle = GetMCTrackFromMCEvent(labelMoth);
723      pdgCode = TMath::Abs((static_cast<const AliAODMCParticle*>(particle))->PdgCode());
724      if((pdgCode>500 && pdgCode<600) || (pdgCode>5000 && pdgCode<6000)) return 1;
725      labelMoth = (static_cast<const AliAODMCParticle*>(particle))->GetMother();
726      }
727   }
728   return 0;
729 }