ROOT logo
// $Id$
//
// Copy tags from particle level constituent to detector level
//
// Author: S. Aiola

#include "AliJetConstituentTagCopier.h"

#include <TClonesArray.h>
#include <TMath.h>
#include <TLorentzVector.h>

#include "AliNamedArrayI.h"
#include "AliVCluster.h"
#include "AliVParticle.h"
#include "AliParticleContainer.h"
#include "AliClusterContainer.h"
#include "AliLog.h"

ClassImp(AliJetConstituentTagCopier)

//________________________________________________________________________
AliJetConstituentTagCopier::AliJetConstituentTagCopier() : 
  AliAnalysisTaskEmcal("AliJetConstituentTagCopier", kFALSE),
  fCleanBeforeCopy(kFALSE),
  fMCLabelShift(0),
  fMCParticleContainer(0)
{
  // Default constructor.
}

//________________________________________________________________________
AliJetConstituentTagCopier::AliJetConstituentTagCopier(const char *name) : 
  AliAnalysisTaskEmcal(name, kFALSE),
  fCleanBeforeCopy(kFALSE),
  fMCLabelShift(0),
  fMCParticleContainer(0)
{
  // Standard constructor.
}

//________________________________________________________________________
AliJetConstituentTagCopier::~AliJetConstituentTagCopier()
{
  // Destructor
}

//________________________________________________________________________
Bool_t AliJetConstituentTagCopier::Run()
{
  for (Int_t i = 0; i < fParticleCollArray.GetEntriesFast(); i++) {
    AliParticleContainer *cont = static_cast<AliParticleContainer*>(fParticleCollArray.At(i));
    if (!cont) continue;
    if (cont == fMCParticleContainer) continue;
    DoParticleLoop(cont);
  }

  for (Int_t i = 0; i < fClusterCollArray.GetEntriesFast(); i++) {
    AliClusterContainer *cont = static_cast<AliClusterContainer*>(fClusterCollArray.At(i));
    if (!cont) continue;
    DoClusterLoop(cont);
  }

  return kTRUE;
}

//________________________________________________________________________
void AliJetConstituentTagCopier::DoClusterLoop(AliClusterContainer *cont)
{
  AliVCluster *cluster = 0;

  if (fCleanBeforeCopy) {
    cont->ResetCurrentID();
    while ((cluster = static_cast<AliVCluster*>(cont->GetNextAcceptCluster()))) {
      Int_t mcLabel = cluster->GetLabel();
      if (mcLabel > 0) cluster->SetBit(TObject::kBitMask, kFALSE);
    }
  }

  if (!fMCParticleContainer) return;
  
  Double_t totalEnergy = 0;
  cont->ResetCurrentID();
  while ((cluster = static_cast<AliVCluster*>(cont->GetNextAcceptCluster()))) {
    Int_t mcLabel = cluster->GetLabel();
    if (mcLabel > fMCLabelShift) mcLabel -= fMCLabelShift;
    if (mcLabel > 0) {
      TLorentzVector vect;
      cluster->GetMomentum(vect, fVertex);
      AliDebug(2, Form("Cluster %d, pt = %f, eta = %f, phi = %f, label = %d",
		       cont->GetCurrentID(), cluster->E(), vect.Eta(), vect.Phi(), mcLabel));
      totalEnergy += cluster->E();
      Int_t index = fMCParticleContainer->GetIndexFromLabel(mcLabel);
      if (index < 0) continue;
      AliVParticle *part = fMCParticleContainer->GetParticle(index);
      if (!part) {
	AliError(Form("%s: Could not get MC particle %d", GetName(), index));
	continue;
      }      
      AliDebug(2, Form("Matched with particle %d, pt = %f, eta = %f, phi = %f", 
		       index, part->E(), part->Eta(), part->Phi()));
      UInt_t bits = (UInt_t)part->TestBits(TObject::kBitMask);
      cluster->SetBit(bits);
    }
  }

  AliDebug(2, Form("Total energy of MC clusters = %f", totalEnergy));
}

//________________________________________________________________________
void AliJetConstituentTagCopier::DoParticleLoop(AliParticleContainer *cont)
{
  AliVParticle *track = 0;

  if (fCleanBeforeCopy) {
    cont->ResetCurrentID();
    while ((track = static_cast<AliVParticle*>(cont->GetNextAcceptParticle()))) {
      Int_t mcLabel = TMath::Abs(track->GetLabel());
      if (mcLabel > 0) track->SetBit(TObject::kBitMask, kFALSE);
    }
  }

  if (!fMCParticleContainer) return;

  cont->ResetCurrentID();
  while ((track = static_cast<AliVParticle*>(cont->GetNextAcceptParticle()))) {
    Int_t mcLabel = TMath::Abs(track->GetLabel());
    if (mcLabel > fMCLabelShift) mcLabel -= fMCLabelShift;
    if (mcLabel > 0) {
      Int_t index = fMCParticleContainer->GetIndexFromLabel(mcLabel);
      if (index < 0) continue;
      AliVParticle *part = fMCParticleContainer->GetParticle(index);
      if (!part) {
	AliError(Form("%s: Could not get MC particle %d", GetName(), index));
	continue;
      }
      AliDebug(3, Form("Track %d, pt = %f, eta = %f, phi = %f, label = %d is matched with particle %d, pt = %f, eta = %f, phi = %f", 
		       cont->GetCurrentID(), track->Pt(), track->Eta(), track->Phi(), mcLabel, index, part->Pt(), part->Eta(), part->Phi()));
      UInt_t bits = (UInt_t)part->TestBits(TObject::kBitMask);
      track->SetBit(bits);
    }
  }
}
 AliJetConstituentTagCopier.cxx:1
 AliJetConstituentTagCopier.cxx:2
 AliJetConstituentTagCopier.cxx:3
 AliJetConstituentTagCopier.cxx:4
 AliJetConstituentTagCopier.cxx:5
 AliJetConstituentTagCopier.cxx:6
 AliJetConstituentTagCopier.cxx:7
 AliJetConstituentTagCopier.cxx:8
 AliJetConstituentTagCopier.cxx:9
 AliJetConstituentTagCopier.cxx:10
 AliJetConstituentTagCopier.cxx:11
 AliJetConstituentTagCopier.cxx:12
 AliJetConstituentTagCopier.cxx:13
 AliJetConstituentTagCopier.cxx:14
 AliJetConstituentTagCopier.cxx:15
 AliJetConstituentTagCopier.cxx:16
 AliJetConstituentTagCopier.cxx:17
 AliJetConstituentTagCopier.cxx:18
 AliJetConstituentTagCopier.cxx:19
 AliJetConstituentTagCopier.cxx:20
 AliJetConstituentTagCopier.cxx:21
 AliJetConstituentTagCopier.cxx:22
 AliJetConstituentTagCopier.cxx:23
 AliJetConstituentTagCopier.cxx:24
 AliJetConstituentTagCopier.cxx:25
 AliJetConstituentTagCopier.cxx:26
 AliJetConstituentTagCopier.cxx:27
 AliJetConstituentTagCopier.cxx:28
 AliJetConstituentTagCopier.cxx:29
 AliJetConstituentTagCopier.cxx:30
 AliJetConstituentTagCopier.cxx:31
 AliJetConstituentTagCopier.cxx:32
 AliJetConstituentTagCopier.cxx:33
 AliJetConstituentTagCopier.cxx:34
 AliJetConstituentTagCopier.cxx:35
 AliJetConstituentTagCopier.cxx:36
 AliJetConstituentTagCopier.cxx:37
 AliJetConstituentTagCopier.cxx:38
 AliJetConstituentTagCopier.cxx:39
 AliJetConstituentTagCopier.cxx:40
 AliJetConstituentTagCopier.cxx:41
 AliJetConstituentTagCopier.cxx:42
 AliJetConstituentTagCopier.cxx:43
 AliJetConstituentTagCopier.cxx:44
 AliJetConstituentTagCopier.cxx:45
 AliJetConstituentTagCopier.cxx:46
 AliJetConstituentTagCopier.cxx:47
 AliJetConstituentTagCopier.cxx:48
 AliJetConstituentTagCopier.cxx:49
 AliJetConstituentTagCopier.cxx:50
 AliJetConstituentTagCopier.cxx:51
 AliJetConstituentTagCopier.cxx:52
 AliJetConstituentTagCopier.cxx:53
 AliJetConstituentTagCopier.cxx:54
 AliJetConstituentTagCopier.cxx:55
 AliJetConstituentTagCopier.cxx:56
 AliJetConstituentTagCopier.cxx:57
 AliJetConstituentTagCopier.cxx:58
 AliJetConstituentTagCopier.cxx:59
 AliJetConstituentTagCopier.cxx:60
 AliJetConstituentTagCopier.cxx:61
 AliJetConstituentTagCopier.cxx:62
 AliJetConstituentTagCopier.cxx:63
 AliJetConstituentTagCopier.cxx:64
 AliJetConstituentTagCopier.cxx:65
 AliJetConstituentTagCopier.cxx:66
 AliJetConstituentTagCopier.cxx:67
 AliJetConstituentTagCopier.cxx:68
 AliJetConstituentTagCopier.cxx:69
 AliJetConstituentTagCopier.cxx:70
 AliJetConstituentTagCopier.cxx:71
 AliJetConstituentTagCopier.cxx:72
 AliJetConstituentTagCopier.cxx:73
 AliJetConstituentTagCopier.cxx:74
 AliJetConstituentTagCopier.cxx:75
 AliJetConstituentTagCopier.cxx:76
 AliJetConstituentTagCopier.cxx:77
 AliJetConstituentTagCopier.cxx:78
 AliJetConstituentTagCopier.cxx:79
 AliJetConstituentTagCopier.cxx:80
 AliJetConstituentTagCopier.cxx:81
 AliJetConstituentTagCopier.cxx:82
 AliJetConstituentTagCopier.cxx:83
 AliJetConstituentTagCopier.cxx:84
 AliJetConstituentTagCopier.cxx:85
 AliJetConstituentTagCopier.cxx:86
 AliJetConstituentTagCopier.cxx:87
 AliJetConstituentTagCopier.cxx:88
 AliJetConstituentTagCopier.cxx:89
 AliJetConstituentTagCopier.cxx:90
 AliJetConstituentTagCopier.cxx:91
 AliJetConstituentTagCopier.cxx:92
 AliJetConstituentTagCopier.cxx:93
 AliJetConstituentTagCopier.cxx:94
 AliJetConstituentTagCopier.cxx:95
 AliJetConstituentTagCopier.cxx:96
 AliJetConstituentTagCopier.cxx:97
 AliJetConstituentTagCopier.cxx:98
 AliJetConstituentTagCopier.cxx:99
 AliJetConstituentTagCopier.cxx:100
 AliJetConstituentTagCopier.cxx:101
 AliJetConstituentTagCopier.cxx:102
 AliJetConstituentTagCopier.cxx:103
 AliJetConstituentTagCopier.cxx:104
 AliJetConstituentTagCopier.cxx:105
 AliJetConstituentTagCopier.cxx:106
 AliJetConstituentTagCopier.cxx:107
 AliJetConstituentTagCopier.cxx:108
 AliJetConstituentTagCopier.cxx:109
 AliJetConstituentTagCopier.cxx:110
 AliJetConstituentTagCopier.cxx:111
 AliJetConstituentTagCopier.cxx:112
 AliJetConstituentTagCopier.cxx:113
 AliJetConstituentTagCopier.cxx:114
 AliJetConstituentTagCopier.cxx:115
 AliJetConstituentTagCopier.cxx:116
 AliJetConstituentTagCopier.cxx:117
 AliJetConstituentTagCopier.cxx:118
 AliJetConstituentTagCopier.cxx:119
 AliJetConstituentTagCopier.cxx:120
 AliJetConstituentTagCopier.cxx:121
 AliJetConstituentTagCopier.cxx:122
 AliJetConstituentTagCopier.cxx:123
 AliJetConstituentTagCopier.cxx:124
 AliJetConstituentTagCopier.cxx:125
 AliJetConstituentTagCopier.cxx:126
 AliJetConstituentTagCopier.cxx:127
 AliJetConstituentTagCopier.cxx:128
 AliJetConstituentTagCopier.cxx:129
 AliJetConstituentTagCopier.cxx:130
 AliJetConstituentTagCopier.cxx:131
 AliJetConstituentTagCopier.cxx:132
 AliJetConstituentTagCopier.cxx:133
 AliJetConstituentTagCopier.cxx:134
 AliJetConstituentTagCopier.cxx:135
 AliJetConstituentTagCopier.cxx:136
 AliJetConstituentTagCopier.cxx:137
 AliJetConstituentTagCopier.cxx:138
 AliJetConstituentTagCopier.cxx:139
 AliJetConstituentTagCopier.cxx:140
 AliJetConstituentTagCopier.cxx:141
 AliJetConstituentTagCopier.cxx:142
 AliJetConstituentTagCopier.cxx:143