+ if(iPrim >= 0)
+ return rl->Stack()->Particle(iPrim) ;
+ else
+ return 0 ;
+}
+
+//____________________________________________________________________________
+Int_t AliPHOSRecParticle::GetPrimaryIndex() const
+{
+ // Get the primary track index in TreeK which deposits the most energy
+ // in Digits which forms EmcRecPoint, which produces TrackSegment,
+ // which the RecParticle is created from
+
+
+ AliRunLoader* rl = AliRunLoader::Instance() ;
+ AliPHOSLoader * phosLoader = static_cast<AliPHOSLoader*>(rl->GetLoader("PHOSLoader"));
+ rl->GetEvent(rl->GetEventNumber()) ;
+ rl->LoadHits("READ");
+ rl->LoadDigits("READ");
+ rl->LoadRecPoints("READ");
+ rl->LoadTracks("READ");
+
+ // Get TrackSegment corresponding to this RecParticle
+ const AliPHOSTrackSegment *ts = phosLoader->TrackSegment(fPHOSTrackSegment);
+
+ // Get EmcRecPoint corresponding to this TrackSegment
+ Int_t emcRecPointIndex = ts->GetEmcIndex();
+
+ const AliPHOSEmcRecPoint *emcRecPoint = phosLoader->EmcRecPoint(emcRecPointIndex);
+
+ // Get the list of digits forming this EmcRecParticle
+ Int_t nDigits = emcRecPoint->GetDigitsMultiplicity();
+ Int_t *digitList = emcRecPoint->GetDigitsList();
+
+ // Find the digit with maximum amplitude
+ Int_t maxAmp = 0;
+ Int_t bestDigitIndex = -1;
+ for (Int_t iDigit=0; iDigit<nDigits; iDigit++) {
+ const AliPHOSDigit * digit = phosLoader->Digit(digitList[iDigit]);
+ if (digit->GetAmp() > maxAmp) {
+ maxAmp = digit->GetAmp();
+ bestDigitIndex = iDigit;
+ }
+ }
+ if (bestDigitIndex>-1) {
+ const AliPHOSDigit * digit = phosLoader->Digit(digitList[bestDigitIndex]);
+ if (digit==0) return -12345;
+ }
+
+ // Get the list of primary tracks producing this digit
+ // and find which track has more track energy.
+ //Int_t nPrimary = digit->GetNprimary();
+ //TParticle *track = 0;
+ //Double_t energyEM = 0;
+ //Double_t energyHadron = 0;
+ //Int_t trackEM = -1;
+ //Int_t trackHadron = -1;
+ //for (Int_t iPrim=0; iPrim<nPrimary; iPrim++) {
+ // Int_t iPrimary = digit->GetPrimary(iPrim+1);
+ // if (iPrimary == -1 || TMath::Abs(iPrimary)>10000000)
+ // continue ; //PH 10000000 is the shift of the track
+ // //PH indexes in the underlying event
+ // track = gime->Primary(iPrimary);
+ // Int_t pdgCode = track->GetPdgCode();
+ // Double_t energy = track->Energy();
+ // if (pdgCode==22 || TMath::Abs(pdgCode)==11) {
+ // if (energy > energyEM) {
+ // energyEM = energy;
+ // trackEM = iPrimary;
+ // }
+ // }
+ // else {
+ // if (energy > energyHadron) {
+ // energyHadron = energy;
+ // trackHadron = iPrimary;
+ // }
+ // }
+ //}
+ // Preferences are given to electromagnetic tracks
+ //if (trackEM != -1) return trackEM; // track is gamma or e+-
+ //if (trackHadron != -1) return trackHadron; // track is hadron
+ //return -12345; // no track found :(
+
+
+ // Get the list of hits producing this digit,
+ // find which hit has deposited more energy
+ // and find the primary track.
+
+ TClonesArray *hits = phosLoader->Hits();
+ if (hits==0) return -12345;
+
+ Double_t maxedep = 0;
+ Int_t maxtrack = -1;
+ Int_t nHits = hits ->GetEntries();
+ Int_t id = (phosLoader->Digit(digitList[bestDigitIndex]))->GetId();
+
+ for (Int_t iHit=0; iHit<nHits; iHit++) {
+ const AliPHOSHit * hit = phosLoader->Hit(iHit);
+ if(hit->GetId() == id){
+ Double_t edep = hit->GetEnergy();
+ Int_t track = hit->GetPrimary();
+ if(edep > maxedep){
+ maxedep = edep;
+ maxtrack = track;
+ }
+ }
+ }
+
+ if (maxtrack != -1) return maxtrack; // track is hadron
+ return -12345; // no track found :(
+}
+
+//____________________________________________________________________________
+const TParticle * AliPHOSRecParticle::GetPrimary(Int_t index) const
+{
+ // Get one of the primary particles at the origine of the RecParticle
+ if ( index > GetNPrimariesToRecParticles() ) {
+ if (fDebug)
+ Warning("GetPrimary", "AliPHOSRecParticle::GetPrimary -> %d is larger that the number of primaries %d",
+ index, GetNPrimaries()) ;
+ return 0 ;
+ }
+ else {
+ Int_t dummy ;
+ AliRunLoader* rl = AliRunLoader::Instance() ;
+ AliPHOSLoader * phosLoader = static_cast<AliPHOSLoader*>(rl->GetLoader("PHOSLoader"));
+
+ Int_t emcRPindex = static_cast<AliPHOSTrackSegment*>(phosLoader->TrackSegments()->At(GetPHOSTSIndex()))->GetEmcIndex();
+ Int_t primaryindex = static_cast<AliPHOSEmcRecPoint*>(phosLoader->EmcRecPoints()->At(emcRPindex))->GetPrimaries(dummy)[index] ;
+ return rl->Stack()->Particle(primaryindex) ;
+ }
+ // return 0 ;