]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG3/dielectron/AliDielectronMC.cxx
Fixes for bug #77230: PWG3 par files
[u/mrichter/AliRoot.git] / PWG3 / dielectron / AliDielectronMC.cxx
CommitLineData
b2a297fa 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 <AliMCEventHandler.h>
29#include <AliMCEvent.h>
30#include <AliMCParticle.h>
31#include <AliAODMCParticle.h>
32#include <AliStack.h>
33#include <AliESDEvent.h>
34#include <AliESDtrack.h>
35#include <AliLog.h>
36
37#include "AliDielectronMC.h"
38
39AliDielectronMC* AliDielectronMC::fgInstance=0x0;
40
41//____________________________________________________________
42AliDielectronMC* AliDielectronMC::Instance()
43{
44 //
45 // return pointer to singleton implementation
46 //
47 if (fgInstance) return fgInstance;
48
49 AnalysisType type=kUNSET;
3505bfad 50 Bool_t hasMC=kFALSE;
51 if (AliAnalysisManager::GetAnalysisManager()){
52 if (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()->IsA()==AliESDInputHandler::Class()) type=kESD;
53 else if (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()->IsA()==AliAODHandler::Class()) type=kAOD;
8df8e382 54
3505bfad 55 AliMCEventHandler* mcHandler = dynamic_cast<AliMCEventHandler*> (AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
56 hasMC=mcHandler!=0x0;
57 }
58
b2a297fa 59 fgInstance=new AliDielectronMC(type);
3505bfad 60 fgInstance->SetHasMC(hasMC);
b2a297fa 61
62 return fgInstance;
63}
64
65//____________________________________________________________
66AliDielectronMC::AliDielectronMC(AnalysisType type):
67 fMCEvent(0x0),
68 fStack(0x0),
572b0139 69 fAnaType(type),
70 fHasMC(kTRUE)
b2a297fa 71{
72 //
73 // default constructor
74 //
75}
76
77
78//____________________________________________________________
79AliDielectronMC::~AliDielectronMC()
80{
81 //
82 // default destructor
83 //
84
85}
86
87//____________________________________________________________
88void AliDielectronMC::Initialize()
89{
90 //
91 // initialize MC class
92 //
93 if (!ConnectMCEvent()) AliError("Initialization of MC object failed!");
94}
95
96//____________________________________________________________
97Int_t AliDielectronMC::GetNMCTracks()
98{
99 //
100 // return the number of generated tracks from MC event
101 //
102 if (!fMCEvent){ AliError("No fMCEvent"); return 0; }
103 return fMCEvent->GetNumberOfTracks();
104}
105
106//____________________________________________________________
107Int_t AliDielectronMC::GetNMCTracksFromStack()
108{
109 //
110 // return the number of generated tracks from stack
111 //
112 if (!fStack){ AliError("No fStack"); return -999; }
113 return fStack->GetNtrack();
114}
115
2a14a7b1 116//____________________________________________________________
117Int_t AliDielectronMC::GetNPrimary()
118{
119 //
120 // return the number of primary track from MC event
121 //
122 if (!fMCEvent){ AliError("No fMCEvent"); return 0; }
123 return fMCEvent->GetNumberOfPrimaries();
124}
125
126//____________________________________________________________
127Int_t AliDielectronMC::GetNPrimaryFromStack()
128{
129 //
130 // return the number of primary track from stack
131 //
132 if (!fStack){ AliError("No fStack"); return -999; }
133 return fStack->GetNprimary();
134}
135
b2a297fa 136//____________________________________________________________
83e37742 137AliVParticle* AliDielectronMC::GetMCTrackFromMCEvent(Int_t itrk)
b2a297fa 138{
139 //
140 // return MC track directly from MC event
141 //
83e37742 142 if (itrk<0) return NULL;
b2a297fa 143 if (!fMCEvent){ AliError("No fMCEvent"); return NULL;}
83e37742 144 AliVParticle * track = fMCEvent->GetTrack(itrk); // tracks from MC event
b2a297fa 145 return track;
146}
147
148//____________________________________________________________
149Bool_t AliDielectronMC::ConnectMCEvent()
150{
151 //
152 // connect stack object from the mc handler
153 //
8df8e382 154 fMCEvent=0x0;
b2a297fa 155 AliMCEventHandler* mcHandler = dynamic_cast<AliMCEventHandler*> (AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
156 if (!mcHandler){ AliError("Could not retrive MC event handler!"); return kFALSE; }
8df8e382 157 if (!mcHandler->InitOk() ) return kFALSE;
158 if (!mcHandler->TreeK() ) return kFALSE;
159 if (!mcHandler->TreeTR() ) return kFALSE;
b2a297fa 160
161 AliMCEvent* mcEvent = mcHandler->MCEvent();
162 if (!mcEvent){ AliError("Could not retrieve MC event!"); return kFALSE; }
163 fMCEvent = mcEvent;
164
165 if (!UpdateStack()) return kFALSE;
166 return kTRUE;
167}
168
169//____________________________________________________________
170Bool_t AliDielectronMC::UpdateStack()
171{
172 //
173 // update stack with new event
174 //
175 if (!fMCEvent){ AliError("No fMCEvent"); return kFALSE;}
176 AliStack* stack = fMCEvent->Stack();
177 if (!stack){ AliError("Could not retrive stack!"); return kFALSE; }
178 fStack = stack;
179 return kTRUE;
180}
181
182//____________________________________________________________
8df8e382 183AliMCParticle* AliDielectronMC::GetMCTrack( const AliESDtrack* _track)
b2a297fa 184{
185 //
186 // return MC track
187 //
9143d69f 188 if (!fMCEvent){ AliError("No fMCEvent"); return NULL;}
189
190 Int_t nStack = fMCEvent->GetNumberOfTracks();
b2a297fa 191 Int_t label = TMath::Abs(_track->GetLabel());
9143d69f 192 if(label>nStack)return NULL;
193
b2a297fa 194 AliMCParticle *mctrack = dynamic_cast<AliMCParticle *>(fMCEvent->GetTrack(label));
195 return mctrack;
196}
197
198//____________________________________________________________
8df8e382 199TParticle* AliDielectronMC::GetMCTrackFromStack(const AliESDtrack* _track)
b2a297fa 200{
201 //
202 // return MC track from stack
203 //
204 Int_t label = TMath::Abs(_track->GetLabel());
205 if (!fStack) AliWarning("fStack is not available. Update stack first.");
206 TParticle* mcpart = fStack->Particle(label);
207 if (!mcpart) return NULL;
208 return mcpart;
209}
210
211//____________________________________________________________
8df8e382 212AliMCParticle* AliDielectronMC::GetMCTrackMother(const AliESDtrack* _track)
b2a297fa 213{
214 //
215 // return MC track mother
216 //
217 AliMCParticle* mcpart = GetMCTrack(_track);
218 if (!mcpart) return NULL;
b2a297fa 219 AliMCParticle* mcmother = dynamic_cast<AliMCParticle *>(fMCEvent->GetTrack(mcpart->GetMother()));
220 if (!mcmother) return NULL;
221 return mcmother;
222}
223
224//____________________________________________________________
8df8e382 225AliMCParticle* AliDielectronMC::GetMCTrackMother(const AliMCParticle* _particle){
226 //
227 // return MC track mother
228 //
229 AliMCParticle* mcmother = dynamic_cast<AliMCParticle *>(fMCEvent->GetTrack(_particle->GetMother()));
230 return mcmother;
231}
232
233//____________________________________________________________
234AliAODMCParticle* AliDielectronMC::GetMCTrackMother(const AliAODMCParticle* _particle){
235 //
236 // return MC track mother
237 //
238 AliAODMCParticle* mcmother = dynamic_cast<AliAODMCParticle *>(fMCEvent->GetTrack(_particle->GetMother()));
239 return mcmother;
240}
241
242//____________________________________________________________
243TParticle* AliDielectronMC::GetMCTrackMotherFromStack(const AliESDtrack* _track)
b2a297fa 244{
245 //
246 // return MC track mother from stack
247 //
248 TParticle* mcpart = GetMCTrackFromStack(_track);
249 if ( !mcpart || mcpart->GetFirstMother()<=0 ) return NULL;
250 TParticle* mcmother = fStack->Particle(mcpart->GetFirstMother());
251 if (!mcmother) return NULL;
252 return mcmother;
253}
254
255//____________________________________________________________
8df8e382 256Int_t AliDielectronMC::GetMCPID(const AliESDtrack* _track)
b2a297fa 257{
258 //
259 // return PDG code of the track from the MC truth info
260 //
261 AliMCParticle* mcpart = GetMCTrack(_track);
262 if (!mcpart) return -999;
263 return mcpart->PdgCode();
264}
265
266//____________________________________________________________
8df8e382 267Int_t AliDielectronMC::GetMCPIDFromStack(const AliESDtrack* _track)
b2a297fa 268{
269 //
270 // return MC PDG code from stack
271 //
272 TParticle* mcpart = GetMCTrackFromStack(_track);
273 if (!mcpart) return -999;
274 return mcpart->GetPdgCode();
275}
276
277//____________________________________________________________
8df8e382 278Int_t AliDielectronMC::GetMotherPDG( const AliESDtrack* _track)
b2a297fa 279{
280 //
281 // return PDG code of the mother track from the MC truth info
282 //
283 AliMCParticle* mcmother = GetMCTrackMother(_track);
284 if (!mcmother) return -999;
285 return mcmother->PdgCode();
286}
287
288//____________________________________________________________
8df8e382 289Int_t AliDielectronMC::GetMotherPDGFromStack(const AliESDtrack* _track)
b2a297fa 290{
291 //
292 // return PDG code of the mother track from stack
293 //
294 TParticle* mcmother = GetMCTrackMotherFromStack(_track);
295 if (!mcmother) return -999;
296 return mcmother->GetPdgCode();
297}
298
299//____________________________________________________________
8df8e382 300Int_t AliDielectronMC::GetMCProcess(const AliESDtrack* _track)
b2a297fa 301{
302 //
303 // return process number of the track
304 //
305 AliMCParticle* mcpart = GetMCTrack(_track);
306 if (!mcpart) return -999;
307 return 0;
308}
309
310//____________________________________________________________
8df8e382 311Int_t AliDielectronMC::GetMCProcessFromStack(const AliESDtrack* _track)
b2a297fa 312{
313 //
314 // return process number of the track
315 //
316 TParticle* mcpart = GetMCTrackFromStack(_track);
317 if (!mcpart) return -999;
318 return mcpart->GetUniqueID();
319}
320
321//____________________________________________________________
8df8e382 322Int_t AliDielectronMC::NumberOfDaughters(const AliESDtrack* track)
323{
324 //
325 // returns the number of daughters
326 //
327 AliMCParticle *mcmother=GetMCTrackMother(track);
328 if(!mcmother||!mcmother->Particle()) return -999;
329// return mcmother->GetFirstDaughter()>0?mcmother->GetLastDaughter()-mcmother->GetFirstDaughter()+1:0;
330 return mcmother->Particle()->GetNDaughters();
331}
332
333//____________________________________________________________
334Int_t AliDielectronMC::NumberOfDaughters(const AliMCParticle* particle)
335{
336 //
337 // returns the number of daughters
338 //
339 AliMCParticle *mcmother=GetMCTrackMother(particle);
340 if(!mcmother||!mcmother->Particle()) return -999;
341// return mcmother->GetFirstDaughter()>0?mcmother->GetLastDaughter()-mcmother->GetFirstDaughter()+1:0;
342 return mcmother->Particle()->GetNDaughters();
343}
344
345//____________________________________________________________
346Int_t AliDielectronMC::NumberOfDaughters(const AliAODMCParticle* particle)
347{
348 //
349 // returns the number of daughters
350 //
351 AliAODMCParticle *mcmother=GetMCTrackMother(particle);
352 if(!mcmother) return -999;
353 return mcmother->GetNDaughters();
354}
355
356//____________________________________________________________
357Int_t AliDielectronMC::GetMCProcessMother(const AliESDtrack* _track)
b2a297fa 358{
359 //
360 // return process number of the mother of the track
361 //
362 AliMCParticle* mcmother = GetMCTrackMother(_track);
363 if (!mcmother) return -999;
364 return 0;
365}
366
367//____________________________________________________________
8df8e382 368Int_t AliDielectronMC::GetMCProcessMotherFromStack(const AliESDtrack* _track)
b2a297fa 369{
370 //
371 // return process number of the mother of the track
372 //
373 TParticle* mcmother = GetMCTrackMotherFromStack(_track);
374 if (!mcmother) return -999;
375 return mcmother->GetUniqueID();
376}
377
378//____________________________________________________________
379Bool_t AliDielectronMC::IsMCMotherToEE(const AliVParticle *particle, Int_t pdgMother)
380{
381 //
382 // Check if the Mother 'particle' is of type pdgMother and decays to e+e-
383 //
384
385 if (!fMCEvent) return kFALSE;
8df8e382 386 if (!particle) return kFALSE;
b2a297fa 387
388 if (particle->IsA()==AliMCParticle::Class()){
389 return IsMCMotherToEEesd(static_cast<const AliMCParticle*>(particle),pdgMother);
390 } else if (particle->IsA()==AliAODMCParticle::Class()){
391 return IsMCMotherToEEaod(static_cast<const AliAODMCParticle*>(particle),pdgMother);
392 } else {
393 AliError("Unknown particle type");
394 }
395 return kFALSE;
396
397}
398
399//____________________________________________________________
400Bool_t AliDielectronMC::IsMCMotherToEEesd(const AliMCParticle *particle, Int_t pdgMother)
401{
402 //
403 // Check if the Mother 'particle' is of type pdgMother and decays to e+e-
404 // ESD case
405 //
406
407 //check pdg code
408 if (particle->PdgCode()!=pdgMother) return kFALSE;
409 Int_t ifirst = particle->GetFirstDaughter();
410 Int_t ilast = particle->GetLastDaughter();
411
412 //check number of daughters
413 if ((ilast-ifirst)!=1) return kFALSE;
414 AliMCParticle *firstD=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(ifirst));
415 AliMCParticle *secondD=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(ilast));
8df8e382 416
417 //TODO: check how you can get rid of the hardcoded numbers. One should make use of the PdgCodes set in AliDielectron!!!
b2a297fa 418 if (firstD->Charge()>0){
419 if (firstD->PdgCode()!=-11) return kFALSE;
420 if (secondD->PdgCode()!=11) return kFALSE;
421 }else{
422 if (firstD->PdgCode()!=11) return kFALSE;
423 if (secondD->PdgCode()!=-11) return kFALSE;
424 }
425
426 return kTRUE;
427}
428
429//____________________________________________________________
430Bool_t AliDielectronMC::IsMCMotherToEEaod(const AliAODMCParticle *particle, Int_t pdgMother)
431{
432 //
433 // Check if the Mother 'particle' is of type pdgMother and decays to e+e-
434 // AOD case
435 //
436 if (particle->GetPdgCode()!=pdgMother) return kFALSE;
437 if (particle->GetNDaughters()!=2) return kFALSE;
438
439 Int_t ifirst = particle->GetDaughter(0);
440 Int_t ilast = particle->GetDaughter(1);
441
442 //check number of daughters
443 if ((ilast-ifirst)!=1) return kFALSE;
444 AliAODMCParticle *firstD=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(ifirst));
445 AliAODMCParticle *secondD=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(ilast));
446
8df8e382 447 //TODO: check how you can get rid of the hardcoded numbers. One should make use of the PdgCodes set in AliDielectron!!!
b2a297fa 448 if (firstD->Charge()>0){
449 if (firstD->GetPdgCode()!=11) return kFALSE;
450 if (secondD->GetPdgCode()!=-11) return kFALSE;
451 }else{
452 if (firstD->GetPdgCode()!=-11) return kFALSE;
453 if (secondD->GetPdgCode()!=11) return kFALSE;
454 }
455 return kTRUE;
456}
457
458//____________________________________________________________
a655b716 459Int_t AliDielectronMC::GetLabelMotherWithPdg(const AliVParticle *particle1, const AliVParticle *particle2, Int_t pdgMother)
b2a297fa 460{
461 //
462 // test if mother of particle 1 and 2 has pdgCode pdgMother and is the same;
463 //
a655b716 464 if (!fMCEvent) return -1;
b2a297fa 465
a655b716 466 if (fAnaType==kESD) return GetLabelMotherWithPdgESD(particle1, particle2, pdgMother);
467 else if (fAnaType==kAOD) return GetLabelMotherWithPdgAOD(particle1, particle2, pdgMother);
b2a297fa 468
a655b716 469 return -1;
b2a297fa 470}
471
472//____________________________________________________________
a655b716 473Int_t AliDielectronMC::GetLabelMotherWithPdgESD(const AliVParticle *particle1, const AliVParticle *particle2, Int_t pdgMother)
b2a297fa 474{
475 //
a655b716 476 // test if mother of particle 1 and 2 has pdgCode +-11 (electron),
477 // have the same mother and the mother had pdg code pdgMother
b2a297fa 478 // ESD case
8df8e382 479 //TODO: check how you can get rid of the hardcoded numbers. One should make use of the PdgCodes set in AliDielectron!!!
b2a297fa 480 //
481
482 AliMCParticle *mcPart1=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(particle1->GetLabel()));
483 AliMCParticle *mcPart2=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(particle2->GetLabel()));
484
a655b716 485 if (!mcPart1||!mcPart2) return -1;
b2a297fa 486
487 Int_t lblMother1=mcPart1->GetMother();
488 Int_t lblMother2=mcPart2->GetMother();
489
490 AliMCParticle *mcMother1=static_cast<AliMCParticle*>(GetMCTrackFromMCEvent(lblMother1));
a655b716 491 if (!mcMother1) return -1;
492 if (lblMother1!=lblMother2) return -1;
493 if (TMath::Abs(mcPart1->PdgCode())!=11) return -1;
572b0139 494 if (mcPart1->PdgCode()!=-mcPart2->PdgCode()) return -1;
a655b716 495 if (mcMother1->PdgCode()!=pdgMother) return -1;
b2a297fa 496
a655b716 497 return lblMother1;
b2a297fa 498}
499
500//____________________________________________________________
a655b716 501Int_t AliDielectronMC::GetLabelMotherWithPdgAOD(const AliVParticle *particle1, const AliVParticle *particle2, Int_t pdgMother)
b2a297fa 502{
503 //
a655b716 504 // test if mother of particle 1 and 2 has pdgCode +-11 (electron),
505 // have the same mother and the mother had pdg code pdgMother
b2a297fa 506 // AOD case
8df8e382 507 //TODO: check how you can get rid of the hardcoded numbers. One should make use of the PdgCodes set in AliDielectron!!!
b2a297fa 508 //
509 AliAODMCParticle *mcPart1=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(particle1->GetLabel()));
510 AliAODMCParticle *mcPart2=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(particle2->GetLabel()));
511
a655b716 512 if (!mcPart1||!mcPart2) return -1;
b2a297fa 513
514 Int_t lblMother1=mcPart1->GetMother();
515 Int_t lblMother2=mcPart2->GetMother();
516
517 AliAODMCParticle *mcMother1=static_cast<AliAODMCParticle*>(GetMCTrackFromMCEvent(lblMother1));
518
a655b716 519 if (!mcMother1) return -1;
520 if (lblMother1!=lblMother2) return -1;
521 if (TMath::Abs(mcPart1->GetPdgCode())!=11) return -1;
572b0139 522 if (mcPart1->GetPdgCode()!=-mcPart2->GetPdgCode()) return -1;
a655b716 523 if (mcMother1->GetPdgCode()!=pdgMother) return -1;
b2a297fa 524
a655b716 525 return lblMother1;
b2a297fa 526}
527
6551594b 528//____________________________________________________________
529void AliDielectronMC::GetDaughters(const TObject *mother, AliVParticle* &d1, AliVParticle* &d2)
530{
531 //
532 // Get First two daughters of the mother
533 //
534
535 Int_t lblD1=-1;
536 Int_t lblD2=-1;
537 d1=0;
538 d2=0;
539 if (!fMCEvent) return;
540 if (fAnaType==kAOD){
541 const AliAODMCParticle *aodMother=static_cast<const AliAODMCParticle*>(mother);
542 lblD1=aodMother->GetDaughter(0);
543 lblD2=aodMother->GetDaughter(1);
544 } else if (fAnaType==kESD){
545 const AliMCParticle *aodMother=static_cast<const AliMCParticle*>(mother);
546 lblD1=aodMother->GetFirstDaughter();
547 lblD2=aodMother->GetLastDaughter();
548 }
549 d1=fMCEvent->GetTrack(lblD1);
550 d2=fMCEvent->GetTrack(lblD2);
551}
8df8e382 552
553//____________________________________________________________
554Bool_t AliDielectronMC::HaveSameMother(const AliDielectronPair * pair)
555{
556 //
557 // Check whether two particles have the same mother
558 //
559
560 const AliVParticle * daughter1 = pair->GetFirstDaughter();
561 const AliVParticle * daughter2 = pair->GetSecondDaughter();
562
83e37742 563 AliVParticle *mcDaughter1=GetMCTrackFromMCEvent(daughter1->GetLabel());
564 AliVParticle *mcDaughter2=GetMCTrackFromMCEvent(daughter2->GetLabel());
8df8e382 565 if (!mcDaughter1 || !mcDaughter2) return 0;
566
83e37742 567 Int_t labelMother1=-1;
568 Int_t labelMother2=-1;
8df8e382 569
83e37742 570 if (mcDaughter1->IsA()==AliMCParticle::Class()){
571 labelMother1=(static_cast<AliMCParticle*>(mcDaughter1))->GetMother();
572 labelMother2=(static_cast<AliMCParticle*>(mcDaughter2))->GetMother();
573 } else if (mcDaughter1->IsA()==AliAODMCParticle::Class()) {
574 labelMother1=(static_cast<AliAODMCParticle*>(mcDaughter1))->GetMother();
575 labelMother2=(static_cast<AliAODMCParticle*>(mcDaughter2))->GetMother();
576 }
8df8e382 577
83e37742 578 Bool_t sameMother=(labelMother1>-1)&&(labelMother2>-1)&&(labelMother1==labelMother2);
8df8e382 579
83e37742 580 return sameMother;
8df8e382 581}
582
583
584
585
586
587
588
589